File: NSAffineTransform.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (348 lines) | stat: -rw-r--r-- 8,045 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/** <title>NSAffineTransform.m</title>

   <abstract>
   This class provides a way to perform affine transforms.  It provides 
   a matrix for transforming from one coordinate system to another.
   </abstract>
   Copyright (C) 1996,1999 Free Software Foundation, Inc.

   Author: Ovidiu Predescu <ovidiu@net-community.com>
   Date: August 1997
   Author: Richard Frith-Macdonald <richard@brainstorm.co.uk>
   Date: March 1999
   
   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#import "config.h"
#include <math.h>

#import <Foundation/NSArray.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>

#import "AppKit/NSAffineTransform.h"
#import "AppKit/NSBezierPath.h"
#import "AppKit/PSOperators.h"

/* Private definitions */
#define A matrix.m11
#define B matrix.m12
#define C matrix.m21
#define D matrix.m22
#define TX matrix.tX
#define TY matrix.tY

/* A Postscript matrix looks like this:

  /  a  b  0 \
  |  c  d  0 |
  \ tx ty  1 /

 */

static const CGFloat pi = 3.1415926535897932384626434;

@implementation NSAffineTransform (GUIAdditions)

/**
 * Concatenates the receiver's matrix with the one in the current graphics 
 * context.
 */
- (void) concat
{
  // FIXME Should use GSConcatCTM:
  NSAffineTransformStruct ats = [self transformStruct];
  CGFloat floatMatrix[6];

  floatMatrix[0] = ats.m11;
  floatMatrix[1] = ats.m12;
  floatMatrix[2] = ats.m21;
  floatMatrix[3] = ats.m22;
  floatMatrix[4] = ats.tX;
  floatMatrix[5] = ats.tY;
  PSconcat(floatMatrix);
}


/**
 * Get the currently active graphics context's transformation 
 * matrix and set it into the receiver.
 */
- (void) set
{
  GSSetCTM(GSCurrentContext(), self);
}

/**
 * <p>Applies the receiver's transformation matrix to each point in 
 * the bezier path, then returns the result.  The original bezier 
 * path is not modified.
 * </p>
 */
- (NSBezierPath*) transformBezierPath: (NSBezierPath*)aPath
{
  NSBezierPath *path = [aPath copy];

  [path transformUsingAffineTransform: self];
  return AUTORELEASE(path);
}

@end /* NSAffineTransform (GUIAdditions) */

@implementation NSAffineTransform (GNUstep)

- (void) scaleTo: (CGFloat)sx : (CGFloat)sy
{
  NSAffineTransformStruct matrix = [self transformStruct];

  /* If it's rotated.  */
  if (B != 0  ||  C != 0)
    {
      // FIXME: This case does not handle shear.
      CGFloat angle = [self rotationAngle];

      // Keep the translation and add scaling
      A = sx; B = 0;
      C = 0; D = sy;
      [self setTransformStruct: matrix];

      // Prepend the rotation to the scaling and translation
      [self rotateByDegrees: angle];
    }
  else
    {
      A = sx; B = 0;
      C = 0; D = sy;
      [self setTransformStruct: matrix];
    }
}

- (void) translateToPoint: (NSPoint)point
{
  [self translateXBy: point.x yBy: point.y];
}

- (void) makeIdentityMatrix
{
  [self init];
}

- (void) setFrameOrigin: (NSPoint)point
{
  NSAffineTransformStruct matrix = [self transformStruct];
  CGFloat dx = point.x - TX;
  CGFloat dy = point.y - TY;

  [self translateXBy: dx yBy: dy];
}

- (void) setFrameRotation: (CGFloat)angle
{
  [self rotateByDegrees: angle - [self rotationAngle]];
}

- (CGFloat) rotationAngle
{
  NSAffineTransformStruct matrix = [self transformStruct];
  CGFloat rotationAngle = atan2(-C, A);

  rotationAngle *= 180.0 / pi;
  if (rotationAngle < 0.0)
    rotationAngle += 360.0;

  return rotationAngle;
}

- (void) concatenateWith: (NSAffineTransform*)anotherMatrix
{
  GSOnceMLog(@"deprecated ... use -prependTransform:");
  [self prependTransform: anotherMatrix];
}

- (void) concatenateWithMatrix: (const float[6])anotherMatrix
{
  NSAffineTransformStruct amat;
  NSAffineTransform 	*other;

  GSOnceMLog(@"deprecated ... use -prependTransform:");
  amat.m11 = anotherMatrix[0];
  amat.m12 = anotherMatrix[1];
  amat.m21 = anotherMatrix[2];
  amat.m22 = anotherMatrix[3];
  amat.tX  = anotherMatrix[4];
  amat.tY  = anotherMatrix[5];
  other = [NSAffineTransform new];
  [other setTransformStruct: amat];
  [self prependTransform: other];
  RELEASE(other);
}

- (void)inverse
{
  GSOnceMLog(@"deprecated ... use -invert:");
  [self invert];
}

- (BOOL) isRotated
{
  NSAffineTransformStruct matrix = [self transformStruct];

  if (B == 0 && C == 0)
    {
      return NO;
    }
  else
    {
      return YES;
    }
}

- (void) boundingRectFor: (NSRect)rect result: (NSRect*)newRect
{
  NSAffineTransformStruct matrix = [self transformStruct];
  /* Shortcuts of the usual rect values */
  CGFloat x = rect.origin.x;
  CGFloat y = rect.origin.y;
  CGFloat width = rect.size.width;
  CGFloat height = rect.size.height;
  CGFloat xc[3];
  CGFloat yc[3];
  CGFloat min_x;
  CGFloat max_x;
  CGFloat min_y;
  CGFloat max_y;
  int i;

  max_x = A * x + C * y + TX;
  max_y = B * x + D * y + TY;
  xc[0] = max_x + A * width;
  yc[0] = max_y + B * width;
  xc[1] = max_x + C * height;
  yc[1] = max_y + D * height;
  xc[2] = max_x + A * width + C * height;
  yc[2] = max_y + B * width + D * height;
  
  min_x = max_x;
  min_y = max_y;
  
  for (i = 0; i < 3; i++) 
    {
      if (xc[i] < min_x)
	min_x = xc[i];
      if (xc[i] > max_x)
	max_x = xc[i];

      if (yc[i] < min_y)
	 min_y = yc[i];
      if (yc[i] > max_y)
	max_y = yc[i];
    }

  newRect->origin.x = min_x;
  newRect->origin.y = min_y;
  newRect->size.width = max_x -min_x;
  newRect->size.height = max_y -min_y;
}

- (NSPoint) pointInMatrixSpace: (NSPoint)point
{
  GSOnceMLog(@"deprecated ... use -transformPoint:");
  return [self transformPoint: point];
}

- (NSPoint) deltaPointInMatrixSpace: (NSPoint)point
{
  NSAffineTransformStruct matrix = [self transformStruct];
  NSPoint new;

  new.x = A * point.x + C * point.y;
  new.y = B * point.x + D * point.y;

  return new;
}

- (NSSize) sizeInMatrixSpace: (NSSize)size
{
  GSOnceMLog(@"deprecated ... use -transformSize:");
  return [self transformSize: size];
}

- (NSRect) rectInMatrixSpace: (NSRect)rect
{
  NSRect new;

  new.origin = [self transformPoint: rect.origin];
  new.size = [self transformSize: rect.size];

  if (new.size.width < 0)
    {
      new.origin.x += new.size.width;
      new.size.width *= -1;
    }

  if (new.size.height < 0)
    {
      new.origin.y += new.size.height;
      new.size.height *= -1;
    }

  return new;
}

- (void) setMatrix: (const float[6])replace
{
  NSAffineTransformStruct matrix;

  GSOnceMLog(@"deprecated ... use -setTransformStruct:");
  matrix.m11 = replace[0];
  matrix.m12 = replace[1];
  matrix.m21 = replace[2];
  matrix.m22 = replace[3];
  matrix.tX = replace[4];
  matrix.tY = replace[5];
  [self setTransformStruct: matrix];
}

- (void) getMatrix: (float[6])replace
{
  NSAffineTransformStruct matrix = [self transformStruct];

  GSOnceMLog(@"deprecated ... use -transformStruct");
  replace[0] = matrix.m11;
  replace[1] = matrix.m12;
  replace[2] = matrix.m21;
  replace[3] = matrix.m22;
  replace[4] = matrix.tX;
  replace[5] = matrix.tY;
}

- (void) takeMatrixFromTransform: (NSAffineTransform *)aTransform
{
  NSAffineTransformStruct matrix;

  GSOnceMLog(@"deprecated ... use -transformStruct and setTransformStruct:");
  matrix = [aTransform transformStruct];
  [self setTransformStruct: matrix];
}


@end /* NSAffineTransform (GNUstep) */