PageRenderTime 65ms CodeModel.GetById 30ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 1ms

/core/externals/google-toolbox-for-mac/Foundation/GTMGeometryUtils.h

http://macfuse.googlecode.com/
C++ Header | 463 lines | 175 code | 49 blank | 239 comment | 6 complexity | 710094c19cbe474d0ae2ea291cace39b MD5 | raw file
  1//
  2//  GTMGeometryUtils.h
  3//
  4//  Utilities for geometrical utilities such as conversions
  5//  between different types.
  6//
  7//  Copyright 2006-2008 Google Inc.
  8//
  9//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 10//  use this file except in compliance with the License.  You may obtain a copy
 11//  of the License at
 12//
 13//  http://www.apache.org/licenses/LICENSE-2.0
 14//
 15//  Unless required by applicable law or agreed to in writing, software
 16//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 17//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 18//  License for the specific language governing permissions and limitations under
 19//  the License.
 20//
 21
 22#import <Foundation/Foundation.h>
 23#import "GTMDefines.h"
 24#if GTM_IPHONE_SDK
 25#import <CoreGraphics/CoreGraphics.h>
 26#endif //  GTM_IPHONE_SDK
 27
 28#ifdef __cplusplus
 29extern "C" {
 30#endif
 31
 32enum {
 33  GTMScaleProportionally = 0,   // Fit proportionally
 34  GTMScaleToFit,                // Forced fit (distort if necessary)
 35  GTMScaleNone,                 // Don't scale (clip)
 36  GTMScaleToFillProportionally = 101  // Scale proportionally to fill area
 37};
 38typedef NSUInteger GTMScaling;
 39
 40enum {
 41  GTMRectAlignCenter = 0,
 42  GTMRectAlignTop,
 43  GTMRectAlignTopLeft,
 44  GTMRectAlignTopRight,
 45  GTMRectAlignLeft,
 46  GTMRectAlignBottom,
 47  GTMRectAlignBottomLeft,
 48  GTMRectAlignBottomRight,
 49  GTMRectAlignRight
 50};
 51typedef NSUInteger GTMRectAlignment;
 52
 53#pragma mark -
 54#pragma mark CG - Point On Rect
 55/// Return middle of min X side of rectangle
 56//
 57//  Args:
 58//    rect - rectangle
 59//
 60//  Returns:
 61//    point located in the middle of min X side of rect
 62GTM_INLINE CGPoint GTMCGMidMinX(CGRect rect) {
 63  return CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
 64}
 65
 66/// Return middle of max X side of rectangle
 67//
 68//  Args:
 69//    rect - rectangle
 70//
 71//  Returns:
 72//    point located in the middle of max X side of rect
 73GTM_INLINE CGPoint GTMCGMidMaxX(CGRect rect) {
 74  return CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect));
 75}
 76
 77/// Return middle of max Y side of rectangle
 78//
 79//  Args:
 80//    rect - rectangle
 81//
 82//  Returns:
 83//    point located in the middle of max Y side of rect
 84GTM_INLINE CGPoint GTMCGMidMaxY(CGRect rect) {
 85  return CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
 86}
 87
 88/// Return middle of min Y side of rectangle
 89//
 90//  Args:
 91//    rect - rectangle
 92//
 93//  Returns:
 94//    point located in the middle of min Y side of rect
 95GTM_INLINE CGPoint GTMCGMidMinY(CGRect rect) {
 96  return CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
 97}
 98
 99/// Return center of rectangle
100//
101//  Args:
102//    rect - rectangle
103//
104//  Returns:
105//    point located in the center of rect
106GTM_INLINE CGPoint GTMCGCenter(CGRect rect) {
107  return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
108}
109
110#pragma mark -
111#pragma mark CG - Rect-Size Conversion
112
113/// Return size of rectangle
114//
115//  Args:
116//    rect - rectangle
117//
118//  Returns:
119//    size of rectangle
120GTM_INLINE CGSize GTMCGRectSize(CGRect rect) {
121  return CGSizeMake(CGRectGetWidth(rect), CGRectGetHeight(rect));
122}
123
124/// Return rectangle of size
125//
126//  Args:
127//    size - size
128//
129//  Returns:
130//    rectangle of size (origin 0,0)
131GTM_INLINE CGRect GTMCGRectOfSize(CGSize size) {
132  return CGRectMake(0.0f, 0.0f, size.width, size.height);
133}
134
135#pragma mark -
136#pragma mark CG - Rect Scaling and Alignment
137
138///  Scales an CGRect
139//
140//  Args:
141//    inRect: Rect to scale
142//    xScale: fraction to scale (1.0 is 100%)
143//    yScale: fraction to scale (1.0 is 100%)
144//
145//  Returns:
146//    Converted Rect
147GTM_INLINE CGRect GTMCGRectScale(CGRect inRect, CGFloat xScale, CGFloat yScale) {
148  return CGRectMake(inRect.origin.x, inRect.origin.y,
149                    inRect.size.width * xScale, inRect.size.height * yScale);
150}
151
152
153/// Align rectangles
154//
155//  Args:
156//    alignee - rect to be aligned
157//    aligner - rect to be aligned from
158//    alignment - way to align the rectangles
159CGRect GTMCGAlignRectangles(CGRect alignee, CGRect aligner,
160                            GTMRectAlignment alignment);
161/// Scale rectangle
162//
163//  Args:
164//    scalee - rect to be scaled
165//    size - size to scale to
166//    scaling - way to scale the rectangle
167CGRect GTMCGScaleRectangleToSize(CGRect scalee, CGSize size,
168                                 GTMScaling scaling);
169
170#pragma mark -
171#pragma mark CG - Miscellaneous
172
173/// Calculate the distance between two points.
174//
175//  Args:
176//    pt1 first point
177//    pt2 second point
178//
179//  Returns:
180//    Distance
181GTM_INLINE CGFloat GTMCGDistanceBetweenPoints(CGPoint pt1, CGPoint pt2) {
182  CGFloat dX = pt1.x - pt2.x;
183  CGFloat dY = pt1.y - pt2.y;
184#if CGFLOAT_IS_DOUBLE
185  return sqrt(dX * dX + dY * dY);
186#else
187  return sqrtf(dX * dX + dY * dY);
188#endif
189}
190
191#if !GTM_IPHONE_SDK
192// iPhone does not have NSTypes defined, only CGTypes. So no NSRect, NSPoint etc.
193
194#pragma mark -
195// All of the conversion routines below are basically copied from the
196// NSGeometry header in the 10.5 sdk.
197
198#pragma mark NS <-> CG Point Conversion
199
200///  Quickly convert from a CGPoint to a NSPoint.
201//
202///  CGPoints are relative to 0,0 in lower left;
203///  NSPoints are relative to 0,0 in lower left
204//
205//  Args:
206//    inPoint: CGPoint to convert
207//
208//  Returns:
209//    Converted NSPoint
210GTM_INLINE NSPoint GTMCGPointToNSPoint(CGPoint inPoint) {
211  _GTMCompileAssert(sizeof(NSPoint) == sizeof(CGPoint), NSPoint_and_CGPoint_must_be_the_same_size);
212  union convertUnion {NSPoint ns; CGPoint cg;};
213  return ((union convertUnion *)&inPoint)->ns;
214}
215
216///  Quickly convert from a NSPoint to a CGPoint.
217//
218///  CGPoints are relative to 0,0 in lower left;
219///  NSPoints are relative to 0,0 in lower left
220//
221//  Args:
222//    inPoint: NSPoint to convert
223//
224//  Returns:
225//    Converted CGPoint
226GTM_INLINE CGPoint GTMNSPointToCGPoint(NSPoint inPoint) {
227  _GTMCompileAssert(sizeof(NSPoint) == sizeof(CGPoint), NSPoint_and_CGPoint_must_be_the_same_size);
228  union convertUnion {NSPoint ns; CGPoint cg;};
229  return ((union convertUnion *)&inPoint)->cg;
230}
231
232#pragma mark -
233#pragma mark NS <-> CG Rect Conversion
234
235///  Convert from a CGRect to a NSRect.
236//
237///  NSRect are relative to 0,0 in lower left;
238///  CGRect are relative to 0,0 in lower left
239//
240//  Args:
241//    inRect: CGRect to convert
242//
243//  Returns:
244//    Converted NSRect
245GTM_INLINE NSRect GTMCGRectToNSRect(CGRect inRect) {
246  _GTMCompileAssert(sizeof(NSRect) == sizeof(CGRect), NSRect_and_CGRect_must_be_the_same_size);
247  union convertUnion {NSRect ns; CGRect cg;};
248  return ((union convertUnion *)&inRect)->ns;
249}
250
251///  Convert from a NSRect to a CGRect.
252//
253///  NSRect are relative to 0,0 in lower left;
254///  CGRect are relative to 0,0 in lower left
255//
256//  Args:
257//    inRect: NSRect to convert
258//
259//  Returns:
260//    Converted CGRect
261GTM_INLINE CGRect GTMNSRectToCGRect(NSRect inRect) {
262  _GTMCompileAssert(sizeof(NSRect) == sizeof(CGRect), NSRect_and_CGRect_must_be_the_same_size);
263  union convertUnion {NSRect ns; CGRect cg;};
264  return ((union convertUnion *)&inRect)->cg;
265}
266
267
268#pragma mark -
269#pragma mark NS <-> CG Size Conversion
270
271///  Convert from a CGSize to an NSSize.
272//
273//  Args:
274//    inSize: CGSize to convert
275//
276//  Returns:
277//    Converted NSSize
278GTM_INLINE NSSize GTMCGSizeToNSSize(CGSize inSize) {
279  _GTMCompileAssert(sizeof(NSSize) == sizeof(CGSize), NSSize_and_CGSize_must_be_the_same_size);
280  union convertUnion {NSSize ns; CGSize cg;};
281  return ((union convertUnion *)&inSize)->ns;
282}
283
284///  Convert from a NSSize to a CGSize.
285//
286//  Args:
287//    inSize: NSSize to convert
288//
289//  Returns:
290//    Converted CGSize
291GTM_INLINE CGSize GTMNSSizeToCGSize(NSSize inSize) {
292  _GTMCompileAssert(sizeof(NSSize) == sizeof(CGSize), NSSize_and_CGSize_must_be_the_same_size);
293  union convertUnion {NSSize ns; CGSize cg;};
294  return ((union convertUnion *)&inSize)->cg;
295}
296
297#pragma mark -
298#pragma mark NS - Point On Rect
299
300/// Return middle of min X side of rectangle
301//
302//  Args:
303//    rect - rectangle
304//
305//  Returns:
306//    point located in the middle of min X side of rect
307GTM_INLINE NSPoint GTMNSMidMinX(NSRect rect) {
308  return NSMakePoint(NSMinX(rect), NSMidY(rect));
309}
310
311/// Return middle of max X side of rectangle
312//
313//  Args:
314//    rect - rectangle
315//
316//  Returns:
317//    point located in the middle of max X side of rect
318GTM_INLINE NSPoint GTMNSMidMaxX(NSRect rect) {
319  return NSMakePoint(NSMaxX(rect), NSMidY(rect));
320}
321
322/// Return middle of max Y side of rectangle
323//
324//  Args:
325//    rect - rectangle
326//
327//  Returns:
328//    point located in the middle of max Y side of rect
329GTM_INLINE NSPoint GTMNSMidMaxY(NSRect rect) {
330  return NSMakePoint(NSMidX(rect), NSMaxY(rect));
331}
332
333/// Return middle of min Y side of rectangle
334//
335//  Args:
336//    rect - rectangle
337//
338//  Returns:
339//    point located in the middle of min Y side of rect
340GTM_INLINE NSPoint GTMNSMidMinY(NSRect rect) {
341  return NSMakePoint(NSMidX(rect), NSMinY(rect));
342}
343
344/// Return center of rectangle
345//
346//  Args:
347//    rect - rectangle
348//
349//  Returns:
350//    point located in the center of rect
351GTM_INLINE NSPoint GTMNSCenter(NSRect rect) {
352  return NSMakePoint(NSMidX(rect), NSMidY(rect));
353}
354
355#pragma mark -
356#pragma mark NS - Rect-Size Conversion
357
358/// Return size of rectangle
359//
360//  Args:
361//    rect - rectangle
362//
363//  Returns:
364//    size of rectangle
365GTM_INLINE NSSize GTMNSRectSize(NSRect rect) {
366  return NSMakeSize(NSWidth(rect), NSHeight(rect));
367}
368
369/// Return rectangle of size
370//
371//  Args:
372//    size - size
373//
374//  Returns:
375//    rectangle of size (origin 0,0)
376GTM_INLINE NSRect GTMNSRectOfSize(NSSize size) {
377  return NSMakeRect(0.0f, 0.0f, size.width, size.height);
378}
379
380#pragma mark -
381#pragma mark NS - Rect Scaling and Alignment
382
383///  Scales an NSRect
384//
385//  Args:
386//    inRect: Rect to scale
387//    xScale: fraction to scale (1.0 is 100%)
388//    yScale: fraction to scale (1.0 is 100%)
389//
390//  Returns:
391//    Converted Rect
392GTM_INLINE NSRect GTMNSRectScale(NSRect inRect, CGFloat xScale, CGFloat yScale) {
393  return NSMakeRect(inRect.origin.x, inRect.origin.y,
394                    inRect.size.width * xScale, inRect.size.height * yScale);
395}
396
397/// Align rectangles
398//
399//  Args:
400//    alignee - rect to be aligned
401//    aligner - rect to be aligned from
402GTM_INLINE NSRect GTMNSAlignRectangles(NSRect alignee, NSRect aligner,
403                                       GTMRectAlignment alignment) {
404  return GTMCGRectToNSRect(GTMCGAlignRectangles(GTMNSRectToCGRect(alignee),
405                                              GTMNSRectToCGRect(aligner),
406                                              alignment));
407}
408
409/// Align a rectangle to another
410//
411//  Args:
412//    scalee - rect to be scaled
413//    scaler - rect to scale to
414//    scaling - way to scale the rectangle
415//    alignment - way to align the scaled rectangle
416GTM_INLINE NSRect GTMNSScaleRectToRect(NSRect scalee,
417                                       NSRect scaler,
418                                       GTMScaling scaling,
419                                       GTMRectAlignment alignment) {
420
421  return GTMCGRectToNSRect(
422           GTMCGAlignRectangles(
423             GTMCGScaleRectangleToSize(GTMNSRectToCGRect(scalee),
424                                       GTMNSSizeToCGSize(scaler.size),
425                                       scaling),
426             GTMNSRectToCGRect(scaler),
427             alignment));
428}
429
430/// Scale rectangle
431//
432//  Args:
433//    scalee - rect to be scaled
434//    size - size to scale to
435//    scaling - way to scale the rectangle
436GTM_INLINE NSRect GTMNSScaleRectangleToSize(NSRect scalee, NSSize size,
437                                            GTMScaling scaling) {
438  return GTMCGRectToNSRect(GTMCGScaleRectangleToSize(GTMNSRectToCGRect(scalee),
439                                                     GTMNSSizeToCGSize(size),
440                                                     scaling));
441}
442
443#pragma mark -
444#pragma mark NS - Miscellaneous
445
446/// Calculate the distance between two points.
447//
448//  Args:
449//    pt1 first point
450//    pt2 second point
451//
452//  Returns:
453//    Distance
454GTM_INLINE CGFloat GTMNSDistanceBetweenPoints(NSPoint pt1, NSPoint pt2) {
455  return GTMCGDistanceBetweenPoints(GTMNSPointToCGPoint(pt1),
456                                    GTMNSPointToCGPoint(pt2));
457}
458
459#endif //  !GTM_IPHONE_SDK
460
461#ifdef __cplusplus
462}
463#endif