1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8 #import <Foundation/NSAffineTransform.h>
9 #import <Foundation/NSRaise.h>
10 #import <Foundation/NSKeyedArchiver.h>
11 #import <Foundation/NSKeyedUnarchiver.h>
12 #import <CoreFoundation/CFByteOrder.h>
15 @implementation NSAffineTransform
17 static NSAffineTransformStruct identity={1,0,0,1,0,0};
19 // Instead of doing start * append - this imp is doing append * start - which is quite different in matrix multiplication
20 static inline NSAffineTransformStruct multiplyStruct(NSAffineTransformStruct start, NSAffineTransformStruct append){
21 NSAffineTransformStruct result;
23 result.m11=append.m11*start.m11+append.m12*start.m21;
24 result.m12=append.m11*start.m12+append.m12*start.m22;
25 result.m21=append.m21*start.m11+append.m22*start.m21;
26 result.m22=append.m21*start.m12+append.m22*start.m22;
27 result.tX=append.tX*start.m11+append.tY*start.m21+start.tX;
28 result.tY=append.tX*start.m12+append.tY*start.m22+start.tY;
33 static inline NSAffineTransformStruct invertStruct(NSAffineTransformStruct matrix){
34 NSAffineTransformStruct result;
37 determinant=matrix.m11*matrix.m22-matrix.m21*matrix.m12;
39 [NSException raise:NSGenericException format:@"NSAffineTransform: Transform has no inverse"];
41 result.m11=matrix.m22/determinant;
42 result.m12=-matrix.m12/determinant;
43 result.m21=-matrix.m21/determinant;
44 result.m22=matrix.m11/determinant;
45 result.tX=(-matrix.m22*matrix.tX+matrix.m21*matrix.tY)/determinant;
46 result.tY=(matrix.m12*matrix.tX-matrix.m11*matrix.tY)/determinant;
56 -(void)encodeWithCoder:(NSCoder *)coder {
57 if ([coder allowsKeyedCoding]) {
58 NSKeyedArchiver *keyed=(NSKeyedArchiver *)coder;
59 CFSwappedFloat32 *words = alloca(sizeof(CFSwappedFloat32) * 6);
60 words[0] = CFConvertFloat32HostToSwapped(_matrix.m11);
61 words[1] = CFConvertFloat32HostToSwapped(_matrix.m12);
62 words[2] = CFConvertFloat32HostToSwapped(_matrix.m21);
63 words[3] = CFConvertFloat32HostToSwapped(_matrix.m22);
64 words[4] = CFConvertFloat32HostToSwapped(_matrix.tX);
65 words[5] = CFConvertFloat32HostToSwapped(_matrix.tY);
66 [keyed encodeBytes:(void*)words length:(sizeof(CFSwappedFloat32) * 6) forKey:@"NSTransformStruct"];
70 -initWithCoder:(NSCoder *)coder {
71 if([coder allowsKeyedCoding]){
72 NSKeyedUnarchiver *keyed=(NSKeyedUnarchiver *)coder;
75 const uint8_t *bytes=[keyed decodeBytesForKey:@"NSTransformStruct" returnedLength:&length];
80 CFSwappedFloat32 *words=(CFSwappedFloat32 *)bytes;
82 _matrix.m11=CFConvertFloat32SwappedToHost(words[0]);
83 _matrix.m12=CFConvertFloat32SwappedToHost(words[1]);
84 _matrix.m21=CFConvertFloat32SwappedToHost(words[2]);
85 _matrix.m22=CFConvertFloat32SwappedToHost(words[3]);
86 _matrix.tX=CFConvertFloat32SwappedToHost(words[4]);
87 _matrix.tY=CFConvertFloat32SwappedToHost(words[5]);
93 -initWithTransform:(NSAffineTransform *)other {
94 // Cocoa doesn't raise when 'other' is nil
95 _matrix=[other transformStruct];
99 -copyWithZone:(NSZone *)zone {
100 return [[[self class] allocWithZone:zone] initWithTransform:self];
103 +(NSAffineTransform *)transform {
104 return [[self new] autorelease];
107 -(NSAffineTransformStruct)transformStruct {
111 -(void)setTransformStruct:(NSAffineTransformStruct)matrix {
116 _matrix=invertStruct(_matrix);
119 -(void)appendTransform:(NSAffineTransform *)other {
120 // Cocoa doesn't raise when 'other' is nil
121 _matrix=multiplyStruct([other transformStruct], _matrix);
124 -(void)prependTransform:(NSAffineTransform *)other {
125 // Cocoa doesn't raise when 'other' is nil
126 _matrix=multiplyStruct(_matrix, [other transformStruct]);
129 -(void)translateXBy:(CGFloat)xby yBy:(CGFloat)yby {
130 NSAffineTransformStruct translate={1,0,0,1,xby,yby};
131 _matrix=multiplyStruct(_matrix, translate);
134 -(NSPoint)transformPoint:(NSPoint)point {
137 result.x=_matrix.m11*point.x+_matrix.m21*point.y+_matrix.tX;
138 result.y=_matrix.m12*point.x+_matrix.m22*point.y+_matrix.tY;
143 -(NSSize)transformSize:(NSSize)value {
146 result.width = _matrix.m11 * value.width + _matrix.m21 * value.height;
147 result.height = _matrix.m12 * value.width + _matrix.m22 * value.height;
152 -(void)rotateByDegrees:(CGFloat)angle
154 [self rotateByRadians:M_PI*angle/180.0];
157 -(void)rotateByRadians:(CGFloat)radians
159 NSAffineTransformStruct rotate={cos(radians),sin(radians),-sin(radians),cos(radians),0,0};
160 _matrix=multiplyStruct(_matrix, rotate);
163 -(void)scaleBy:(CGFloat)value {
164 NSAffineTransformStruct scale={value,0,0,value,0,0};
165 _matrix=multiplyStruct(_matrix, scale);
168 -(void)scaleXBy:(CGFloat)xvalue yBy:(CGFloat)yvalue {
169 NSAffineTransformStruct scale={xvalue,0,0,yvalue,0,0};
170 _matrix=multiplyStruct(_matrix, scale);
173 - (NSString *)description
175 return [NSString stringWithFormat:@"<%@ 0x%p> {%g, %g, %g, %g, %g, %g}", NSStringFromClass([self class]), self, _matrix.m11, _matrix.m12, _matrix.m21, _matrix.m22, _matrix.tX, _matrix.tY];