Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / Foundation / NSAffineTransform.m
blobf2b6a329e786046f22e5a63933ada9904cf0d225
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>
13 #include <math.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;
30    return result;
33 static inline NSAffineTransformStruct invertStruct(NSAffineTransformStruct matrix){
34    NSAffineTransformStruct result;
35    CGFloat determinant;
37    determinant=matrix.m11*matrix.m22-matrix.m21*matrix.m12;
38    if(determinant == 0.)
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;
48    return result;
51 -init {
52    _matrix=identity;
53    return self;
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"];
67         }
70 -initWithCoder:(NSCoder *)coder {
71    if([coder allowsKeyedCoding]){
72     NSKeyedUnarchiver *keyed=(NSKeyedUnarchiver *)coder;
73     
74     NSUInteger    length;
75     const uint8_t *bytes=[keyed decodeBytesForKey:@"NSTransformStruct" returnedLength:&length];
76     
77     if(length!=24)
78      _matrix=identity;
79     else {
80      CFSwappedFloat32 *words=(CFSwappedFloat32 *)bytes;
81      
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]);
88     }
89    }
90    return self;
93 -initWithTransform:(NSAffineTransform *)other {
94     // Cocoa doesn't raise when 'other' is nil
95    _matrix=[other transformStruct];
96    return self;
99 -copyWithZone:(NSZone *)zone {
100    return [[[self class] allocWithZone:zone] initWithTransform:self];
103 +(NSAffineTransform *)transform {
104    return [[self new] autorelease];
107 -(NSAffineTransformStruct)transformStruct {
108    return _matrix;
111 -(void)setTransformStruct:(NSAffineTransformStruct)matrix {
112    _matrix=matrix;
115 -(void)invert {
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 {
135    NSPoint result;
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;
140    return result;
143 -(NSSize)transformSize:(NSSize)value {
144     NSSize result;
145     
146     result.width  = _matrix.m11 * value.width + _matrix.m21 * value.height;
147     result.height = _matrix.m12 * value.width + _matrix.m22 * value.height;
148     
149     return result;
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];
178 @end