summaryrefslogtreecommitdiff
path: root/libjava/java/awt/geom/AffineTransform.java
blob: 5af7277d05f8c96af65f1f110276e8c4ff689e73 (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/* Copyright (C) 2000, 2001, 2002  Free Software Foundation

This file is part of GNU Classpath.

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

GNU Classpath 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */

package java.awt.geom;
import java.awt.*;
import java.io.Serializable;

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date April 16, 2000
 */

/* Status: mostly complete.  Search for fixme to see problems.
   Also, TYPE_ returns are not handled correctly.  */

public class AffineTransform implements Cloneable, Serializable
{
  public static final int TYPE_IDENTITY = 0;
  public static final int TYPE_FLIP = 64;
  public static final int TYPE_GENERAL_ROTATION = 16;
  public static final int TYPE_GENERAL_SCALE = 4;
  public static final int TYPE_GENERAL_TRANSFORM = 32;
  public static final int TYPE_MASK_ROTATION = 24;
  public static final int TYPE_MASK_SCALE = 6;
  public static final int TYPE_QUADRANT_ROTATION = 8;
  public static final int TYPE_TRANSLATION = 1;
  public static final int TYPE_UNIFORM_SCALE = 2;

  public AffineTransform ()
  {
    setToIdentity ();
  }

  public AffineTransform (AffineTransform tx)
  {
    setTransform (tx);
  }

  public AffineTransform (float m00, float m10,
			  float m01, float m11,
			  float m02, float m12)
  {
    this.m00 = m00;
    this.m10 = m10;
    this.m01 = m01;
    this.m11 = m11;
    this.m02 = m02;
    this.m12 = m12;
    this.type = TYPE_GENERAL_TRANSFORM;
  }

  public AffineTransform (float[] flatmatrix)
  {
    m00 = flatmatrix[0];
    m10 = flatmatrix[1];
    m01 = flatmatrix[2];
    m11 = flatmatrix[3];
    if (flatmatrix.length >= 6)
      {
	m02 = flatmatrix[4];
	m12 = flatmatrix[5];
      }
  }

  public AffineTransform (double m00, double m10, double m01,
			  double m11, double m02, double m12)
  {
    this.m00 = m00;
    this.m10 = m10;
    this.m01 = m01;
    this.m11 = m11;
    this.m02 = m02;
    this.m12 = m12;
    this.type = TYPE_GENERAL_TRANSFORM;
  }

  public AffineTransform (double[] flatmatrix)
  {
    m00 = flatmatrix[0];
    m10 = flatmatrix[1];
    m01 = flatmatrix[2];
    m11 = flatmatrix[3];
    if (flatmatrix.length >= 6)
      {
	m02 = flatmatrix[4];
	m12 = flatmatrix[5];
      }
  }

  public static AffineTransform getTranslateInstance (double tx, double ty)
  {
    AffineTransform t = new AffineTransform ();
    t.setToTranslation (tx, ty);
    return t;
  }

  public static AffineTransform getRotateInstance (double theta)
  {
    AffineTransform t = new AffineTransform ();
    t.setToRotation (theta);
    return t;
  }

  public static AffineTransform getRotateInstance (double theta,
						   double x, double y)
  {
    AffineTransform t = new AffineTransform ();
    t.rotate (theta, x, y);
    return t;
  }

  public static AffineTransform getScaleInstance (double sx, double sy)
  {
    AffineTransform t = new AffineTransform ();
    t.setToScale (sx, sy);
    return t;
  }

  public static AffineTransform getShearInstance (double shx, double shy)
  {
    AffineTransform t = new AffineTransform ();
    t.setToShear (shx, shy);
    return t;
  }

  public int getType ()
  {
    return type;
  }

  public double getDeterminant ()
  {
    return m00 * m11 - m01 * m10;
  }

  public void getMatrix (double[] flatmatrix)
  {
    flatmatrix[0] = m00;
    flatmatrix[1] = m10;
    flatmatrix[2] = m01;
    flatmatrix[3] = m11;
    if (flatmatrix.length >= 6)
      {
	flatmatrix[4] = m02;
	flatmatrix[5] = m12;
      }
  }

  public double getScaleX ()
  {
    return m00;
  }

  public double getScaleY ()
  {
    return m11;
  }

  public double getShearX ()
  {
    return m01;
  }

  public double getShearY ()
  {
    return m10;
  }

  public double getTranslateX ()
  {
    return m02;
  }

  public double getTranslateY ()
  {
    return m12;
  }

  public void translate (double tx, double ty)
  {
    m02 += tx * m00 + ty * m01;
    m12 += tx * m10 + ty * m11;
  }

  public void rotate (double theta)
  {
    double c = Math.cos (theta);
    double s = Math.sin (theta);
    double n00 = m00 *  c + m01 * s;
    double n01 = m00 * -s + m01 * c;
    double n10 = m10 *  c + m11 * s;
    double n11 = m10 * -s + m11 * c;

    m00 = n00;
    m01 = n01;
    m10 = n10;
    m11 = n11;
  }

  public void rotate (double theta, double x, double y)
  {
    translate (x, y);
    rotate (theta);
    translate (-x, -y);
  }

  public void scale (double sx, double sy)
  {
    m00 *= sx;
    m01 *= sy;
    m10 *= sx;
    m11 *= sy;
  }

  public void shear (double shx, double shy)
  {
    double n00 = m00 + shx * m01;
    double n01 = shx * m00 + m01;
    double n10 = m10 * shy + m11;
    double n11 = shx * m10 + m11;

    m00 = n00;
    m01 = n01;
    m10 = n10;
    m11 = n11;
  }

  public void setToIdentity ()
  {
    m00 = m11 = 1;
    m01 = m02 = m10 = m12 = 0;
    type = TYPE_IDENTITY;
  }

  public void setToTranslation (double tx, double ty)
  {
    m00 = m11 = 1;
    m01 = m10 = 0;
    m02 = tx;
    m12 = ty;
    type = TYPE_TRANSLATION;
  }

  public void setToRotation (double theta)
  {
    double c = Math.cos (theta);
    double s = Math.sin (theta);

    m00 = c;
    m01 = -s;
    m02 = 0;
    m10 = s;
    m11 = c;
    m12 = 0;
    type = TYPE_GENERAL_ROTATION;
  }

  public void setToRotation (double theta, double x, double y)
  {
    double c = Math.cos (theta);
    double s = Math.sin (theta);

    m00 = c;
    m01 = -s;
    m02 = x - x * c + y * s;
    m10 = s;
    m11 = c;
    m12 = y - x * s - y * c;
    type = TYPE_GENERAL_TRANSFORM;
  }

  public void setToScale (double sx, double sy)
  {
    m00 = sx;
    m01 = m02 = m10 = m12 = 0;
    m11 = sy;
    type = (sx == sy) ? TYPE_UNIFORM_SCALE : TYPE_GENERAL_SCALE;
  }

  public void setToShear (double shx, double shy)
  {
    m00 = m11 = 1;
    m01 = shx;
    m10 = shy;
    m02 = m12 = 0;
    type = TYPE_GENERAL_TRANSFORM;
  }

  public void setTransform (AffineTransform tx)
  {
    m00 = tx.m00;
    m01 = tx.m01;
    m02 = tx.m02;
    m10 = tx.m10;
    m11 = tx.m11;
    m12 = tx.m12;
    type = tx.type;
  }

  public void setTransform (double m00, double m10, double m01,
			    double m11, double m02, double m12)
  {
    this.m00 = m00;
    this.m10 = m10;
    this.m01 = m01;
    this.m11 = m11;
    this.m02 = m02;
    this.m12 = m12;
    this.type = 0;		// FIXME
  }

  public void concatenate (AffineTransform tx)
  {
    double n00 = m00 * tx.m00 + m01 * tx.m10;
    double n01 = m00 * tx.m01 + m01 * tx.m11;
    double n02 = m00 * tx.m02 + m01 * tx.m12 + m02;
    double n10 = m10 * tx.m00 + m11 * tx.m10;
    double n11 = m10 * tx.m01 + m11 * tx.m11;
    double n12 = m10 * tx.m02 + m11 * tx.m12 + m12;

    m00 = n00;
    m01 = n01;
    m02 = n02;
    m10 = n10;
    m11 = n11;
    m12 = n12;
  }

  public void preConcatenate (AffineTransform tx)
  {
    double n00 = tx.m00 * m00 + tx.m01 * m10;
    double n01 = tx.m00 * m01 + tx.m01 * m11;
    double n02 = tx.m00 * m02 + tx.m01 * m12 + tx.m02;
    double n10 = tx.m10 * m00 + tx.m11 * m10;
    double n11 = tx.m10 * m01 + tx.m11 * m11;
    double n12 = tx.m10 * m02 + tx.m11 * m12 + tx.m12;

    m00 = n00;
    m01 = n01;
    m02 = n02;
    m10 = n10;
    m11 = n11;
    m12 = n12;
  }

  public AffineTransform createInverse ()
    throws NoninvertibleTransformException
  {
    double det = getDeterminant ();
    if (det == 0)
      throw new NoninvertibleTransformException ("can't invert transform");

    double i00 = m11 / det;
    double i01 = -m10 / det;
    double i02 = 0;
    double i10 = m01 / det;
    double i11 = -m00 / det;
    double i12 = 0;

    return new AffineTransform (i00, i01, i02,
				i10, i11, i12);
  }

  public Point2D transform (Point2D src, Point2D dst)
  {
    if (dst == null)
      dst = new Point2D.Double ();

    // We compute and set separately to correctly overwrite if
    // src==dst.
    double x = src.getX ();
    double y = src.getY ();
    double nx = m00 * x + m01 * y + m02;
    double ny = m10 * x + m11 * y + m12;

    dst.setLocation (nx, ny);

    return dst;
  }

  public void transform (Point2D[] src, int srcOff,
			 Point2D[] dst, int dstOff,
			 int num)
  {
    while (num-- > 0)
      {
	dst[dstOff] = transform (src[srcOff], dst[dstOff]);
	++srcOff;
	++dstOff;
      }
  }

  public void transform (float[] srcPts, int srcOff,
			 float[] dstPts, int dstOff,
			 int num)
  {
    while (num-- > 0)
      {
	float x = srcPts[srcOff];
	float y = srcPts[srcOff + 1];
	srcOff += 2;
	float nx = (float) (m00 * x + m01 * y + m02);
	float ny = (float) (m10 * x + m10 * y + m12);
	dstPts[dstOff] = nx;
	dstPts[dstOff + 1] = ny;
	dstOff += 2;
      }
  }

  public void transform (double[] srcPts, int srcOff,
			 double[] dstPts, int dstOff,
			 int num)
  {
    while (num-- > 0)
      {
	double x = srcPts[srcOff];
	double y = srcPts[srcOff + 1];
	srcOff += 2;
	double nx = m00 * x + m01 * y + m02;
	double ny = m10 * x + m10 * y + m12;
	dstPts[dstOff] = nx;
	dstPts[dstOff + 1] = ny;
	dstOff += 2;
      }
  }

  public void transform (float[] srcPts, int srcOff,
			 double[] dstPts, int dstOff,
			 int num)
  {
    while (num-- > 0)
      {
	float x = srcPts[srcOff];
	float y = srcPts[srcOff + 1];
	srcOff += 2;
	double nx = m00 * x + m01 * y + m02;
	double ny = m10 * x + m10 * y + m12;
	dstPts[dstOff] = nx;
	dstPts[dstOff + 1] = ny;
	dstOff += 2;
      }
  }

  public void transform (double[] srcPts, int srcOff,
			 float[] dstPts, int dstOff,
			 int num)
  {
    while (num-- > 0)
      {
	double x = srcPts[srcOff];
	double y = srcPts[srcOff + 1];
	srcOff += 2;
	float nx = (float) (m00 * x + m01 * y + m02);
	float ny = (float) (m10 * x + m10 * y + m12);
	dstPts[dstOff] = nx;
	dstPts[dstOff + 1] = ny;
	dstOff += 2;
      }
  }

  public Point2D inverseTransform (Point2D src, Point2D dst)
    throws NoninvertibleTransformException
  {
    double det = getDeterminant ();
    if (det == 0)
      throw new NoninvertibleTransformException ("couldn't invert transform");

    if (dst == null)
      dst = new Point2D.Double ();
    double x = src.getX ();
    double y = src.getY ();
    double nx = (m11 * x + - m10 * y) / det;
    double ny = (m01 * x + - m00 * y) / det;
    dst.setLocation (nx, ny);
    return dst;
  }

  public void inverseTransform (double[] srcPts, int srcOff,
				double[] dstPts, int dstOff,
				int num)
    throws NoninvertibleTransformException
  {
    double det = getDeterminant ();
    if (det == 0)
      throw new NoninvertibleTransformException ("couldn't invert transform");

    while (num-- > 0)
      {
	double x = srcPts[srcOff];
	double y = srcPts[srcOff + 1];
	double nx = (m11 * x + - m10 * y) / det;
	double ny = (m01 * x + - m00 * y) / det;
	dstPts[dstOff] = nx;
	dstPts[dstOff + 1] = ny;
	dstOff += 2;
	srcOff += 2;
      }
  }

  public Point2D deltaTransform (Point2D src, Point2D dst)
  {
    if (dst == null)
      dst = new Point2D.Double ();
    double x = src.getX ();
    double y = src.getY ();
    double nx = m00 * x + m01 * y;
    double ny = m10 * x + m11 * y;
    dst.setLocation (nx, ny);
    return dst;
  }

  public void deltaTransform (double[] srcPts, int srcOff,
			      double[] dstPts, int dstOff,
			      int num)
  {
    while (num-- > 0)
      {
	double x = srcPts[srcOff];
	double y = srcPts[srcOff + 1];
	double nx = m00 * x + m01 * y;
	double ny = m10 * x + m11 * y;
	dstPts[dstOff] = nx;
	dstPts[dstOff + 1] = ny;
	dstOff += 2;
	srcOff += 2;
      }
  }

  public Shape createTransformedShape (Shape pSrc)
  {
    // FIXME
    return null;
  }

  public String toString ()
  {
    // FIXME
    return null;
  }

  public boolean isIdentity ()
  {
    return (m00 == 1 && m01 == 0 && m02 == 0
	    && m10 == 0 && m11 == 1 && m12 == 0);
  }

  public Object clone ()
  {
    return new AffineTransform (this);
  }

  public int hashCode ()
  {
    // FIXME
    return 23;
  }

  public boolean equals (Object obj)
  {
    if (! (obj instanceof AffineTransform))
      return false;
    AffineTransform t = (AffineTransform) obj;
    return (m00 == t.m00 && m01 == t.m01 && m02 == t.m02
	    && m10 == t.m10 && m11 == t.m11 && m12 == t.m12);
  }

  // This iterator is used to apply an AffineTransform to some other
  // iterator.  It is not private because we want to be able to access
  // it from the rest of this package.
  class Iterator implements PathIterator
  {
    // The iterator we are applied to.
    private PathIterator subIterator;

    public Iterator (PathIterator subIterator)
    {
      this.subIterator = subIterator;
    }

    public int currentSegment (double[] coords)
    {
      int r = subIterator.currentSegment (coords);
      int count = 0;

      switch (r)
	{
	case SEG_CUBICTO:
	  count = 3;
	  break;

	case SEG_QUADTO:
	  count = 2;
	  break;

	case SEG_LINETO:
	case SEG_MOVETO:
	  count = 1;
	  break;

	default:
	  // Error.  But how to report?
	case SEG_CLOSE:
	  break;
	}

      transform (coords, 0, coords, 0, count);

      return r;
    }

    public int currentSegment (float[] coords)
    {
      int r = subIterator.currentSegment (coords);
      int count = 0;

      switch (r)
	{
	case SEG_CUBICTO:
	  count = 3;
	  break;

	case SEG_QUADTO:
	  count = 2;
	  break;

	case SEG_LINETO:
	case SEG_MOVETO:
	  count = 1;
	  break;

	default:
	  // Error.  But how to report?
	case SEG_CLOSE:
	  break;
	}

      transform (coords, 0, coords, 0, count);

      return r;
    }

    public int getWindingRule ()
    {
      return subIterator.getWindingRule ();
    }

    public boolean isDone ()
    {
      return subIterator.isDone ();
    }

    public void next ()
    {
      subIterator.next ();
    }
  }

  private double m00, m01, m02;
  private double m10, m11, m12;
  private int type;
}