1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.common.model.geometry;
19
20 import com.google.gwt.user.client.rpc.IsSerializable;
21
22
23
24
25 public class TransformMatrix implements IsSerializable {
26 public float m11, m12, m13, m21, m22, m23;
27
28 public TransformMatrix() {
29 m11 = m22 = 1f;
30 }
31
32 public TransformMatrix(TransformMatrix m) {
33 m11 = m.m11; m12 = m.m12; m13 = m.m13;
34 m21 = m.m21; m22 = m.m22; m23 = m.m23;
35 }
36 public TransformMatrix(float m11, float m12, float m13, float m21, float m22, float m23) {
37 this.m11 = m11; this.m12 = m12; this.m13 = m13;
38 this.m21 = m21; this.m22 = m22; this.m23 = m23;
39 }
40
41 @Override
42 public boolean equals(Object o) {
43 if (o instanceof TransformMatrix) {
44 TransformMatrix t = (TransformMatrix)o;
45 return (m11 == t.m11) && (m12 == t.m12) && (m13 == t.m13)
46 && (m21 == t.m21) && (m22 == t.m22) && (m23 == t.m23);
47 }
48 return false;
49 }
50
51 public boolean equals(TransformMatrix m, float tol) {
52 return (m11 - tol < m.m11) && (m11 + tol > m.m11)
53 && (m12 - tol < m.m12) && (m12 + tol > m.m12)
54 && (m13 - tol < m.m13) && (m13 + tol > m.m13)
55 && (m21 - tol < m.m21) && (m21 + tol > m.m21)
56 && (m22 - tol < m.m22) && (m22 + tol > m.m22)
57 && (m23 - tol < m.m23) && (m23 + tol > m.m23);
58 }
59
60 @Override
61 public int hashCode() {
62 return (int)(17 * m11) + (int)(19 * m12) + (int)(23 * m13)
63 + (int)(29 * m21) + (int)(31 * m22) + (int)(37 * m23);
64 }
65
66 public TransformMatrix copyTo(TransformMatrix dest) {
67 dest.m11 = m11; dest.m12 = m12; dest.m13 = m13;
68 dest.m21 = m21; dest.m22 = m22; dest.m23 = m23;
69 return dest;
70 }
71
72 public TransformMatrix rotation(float r) {
73 m11 = (float)Math.cos(r); m12 = -(float)Math.sin(r); m13 = 0f;
74 m21 = (float)Math.sin(r); m22 = (float)Math.cos(r); m23 = 0f;
75 return this;
76 }
77
78 public TransformMatrix scaling(Point p) {
79 return scaling(p.x, p.y);
80 }
81
82 public TransformMatrix scaling(float x, float y) {
83 m11 = x; m12 = 0f; m13 = 0f;
84 m21 = 0f; m22 = y; m23 = 0f;
85 return this;
86 }
87
88 public TransformMatrix translation(Point p) {
89 return translation(p.x, p.y);
90 }
91
92 public TransformMatrix translation(float x, float y) {
93 m11 = 1f; m12 = 0f; m13 = x;
94 m21 = 0f; m22 = 1f; m23 = y;
95 return this;
96 }
97
98 public TransformMatrix preMultiply(TransformMatrix t) {
99 return preMultiply(t, this);
100 }
101
102 public TransformMatrix preMultiply(TransformMatrix t, TransformMatrix dest) {
103 float n11 = t.m11 * m11 + t.m12 * m21;
104 float n21 = t.m21 * m11 + t.m22 * m21;
105 float n12 = t.m11 * m12 + t.m12 * m22;
106 float n22 = t.m21 * m12 + t.m22 * m22;
107 float n13 = t.m11 * m13 + t.m12 * m23 + t.m13;
108 float n23 = t.m21 * m13 + t.m22 * m23 + t.m23;
109 dest.m11 = n11; dest.m12 = n12; dest.m13 = n13;
110 dest.m21 = n21; dest.m22 = n22; dest.m23 = n23;
111 return dest;
112 }
113
114 public TransformMatrix invert() {
115 return invert(this);
116 }
117
118 public TransformMatrix invert(TransformMatrix dest) {
119 float d = m11 * m22 - m12 * m21;
120 if (d == 0f) {
121 return null;
122 }
123 d = 1f / d;
124 float n13 = d * (m12 * m23 - m22 * m13);
125 float n23 = d * (m13 * m21 - m11 * m23);
126 dest.m11 = d * m22; dest.m12 = -d * m12; dest.m13 = n13;
127 dest.m21 = -d * m21; dest.m22 = d * m11; dest.m23 = n23;
128 return dest;
129 }
130
131 @Override
132 public String toString() {
133 StringBuffer buffer = new StringBuffer();
134 buffer.append("[");
135 buffer.append(m11);
136 buffer.append(" ");
137 buffer.append(m12);
138 buffer.append(" ");
139 buffer.append(m13);
140 buffer.append(" ");
141 buffer.append(m21);
142 buffer.append(" ");
143 buffer.append(m22);
144 buffer.append(" ");
145 buffer.append(m23);
146 buffer.append("]");
147 return buffer.toString();
148 }
149
150 public TransformMatrix parseString(String str) {
151 String[] strArray = str.substring(1, str.length() - 1).split(" ");
152 m11 = Float.parseFloat(strArray[0]); m12 = Float.parseFloat(strArray[1]); m13 = Float.parseFloat(strArray[2]);
153 m21 = Float.parseFloat(strArray[3]); m22 = Float.parseFloat(strArray[4]); m23 = Float.parseFloat(strArray[5]);
154 return this;
155 }
156 }