1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package com.nokia.mj.impl.uitestutils; |
|
18 |
|
19 /** |
|
20 * Matrix calculation implementation. |
|
21 * |
|
22 */ |
|
23 public class Matrix { |
|
24 |
|
25 static final int ARRAY_SIZE = 6; |
|
26 // Array index |
|
27 static private final int M00 = 0; |
|
28 static private final int M10 = 1; |
|
29 static private final int M01 = 2; |
|
30 static private final int M11 = 3; |
|
31 static private final int M02 = 4; |
|
32 static private final int M12 = 5; |
|
33 |
|
34 private float iComponents[]; |
|
35 |
|
36 /** |
|
37 * Construct a matrix with the following components: |
|
38 * <pre> |
|
39 * [1 0 0] |
|
40 * [0 1 0] |
|
41 * </pre> |
|
42 */ |
|
43 public Matrix() { |
|
44 iComponents = new float[ARRAY_SIZE]; |
|
45 identity(); |
|
46 } |
|
47 |
|
48 /** |
|
49 * Construct a matrix with the following components: |
|
50 * <pre> |
|
51 * [aM00 aM01 aM02] |
|
52 * [aM10 aM11 aM12] |
|
53 * </pre> |
|
54 * @param aM00 the x scaling component |
|
55 * @param aM10 the y shearing component |
|
56 * @param aM01 the x shearing component |
|
57 * @param aM11 the y scaling component |
|
58 * @param aM02 the x translation component |
|
59 * @param aM12 the y translation component |
|
60 */ |
|
61 public Matrix(float aM00, float aM10, float aM01, |
|
62 float aM11, float aM02, float aM12) { |
|
63 iComponents = new float[ARRAY_SIZE]; |
|
64 iComponents[M00] = aM00; iComponents[M01] = aM01; iComponents[M02] = aM02; |
|
65 iComponents[M10] = aM10; iComponents[M11] = aM11; iComponents[M12] = aM12; |
|
66 } |
|
67 |
|
68 /** |
|
69 * Constructor |
|
70 * Create a new matrix by coping the given one. |
|
71 * @param aMatrix the matrix to copy |
|
72 */ |
|
73 public Matrix(Matrix aMatrix) { |
|
74 iComponents = new float[aMatrix.iComponents.length]; |
|
75 for(int index = 0; index < iComponents.length; index++) { |
|
76 iComponents[index] = aMatrix.iComponents[index]; |
|
77 } |
|
78 } |
|
79 |
|
80 /** |
|
81 * |
|
82 */ |
|
83 public float getComponent(int index) { |
|
84 return iComponents[index]; |
|
85 } |
|
86 |
|
87 /** |
|
88 * Set matrix components: |
|
89 * <pre> |
|
90 * [1 0 0] |
|
91 * [0 1 0] |
|
92 * </pre> |
|
93 * |
|
94 */ |
|
95 public void identity() |
|
96 { |
|
97 iComponents[M00] = 1; iComponents[M01] = 0; iComponents[M02] = 0; |
|
98 iComponents[M10] = 0; iComponents[M11] = 1; iComponents[M12] = 0; |
|
99 } |
|
100 |
|
101 /** |
|
102 * Return transformed <code>Point</code> instance |
|
103 * |
|
104 * The transformation can be represented using matrix math on a 3x3 array. |
|
105 * Given (x,y), the transformation (x',y') can be found by: |
|
106 * [ x'] [ m00 m01 m02 ] [ x ] [ m00*x + m01*y + m02 ] |
|
107 * [ y'] = [ m10 m11 m12 ] [ y ] = [ m10*x + m11*y + m12 ] |
|
108 * [ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ] |
|
109 * |
|
110 * The bottom row of the matrix is constant, so a transform can be uniquely |
|
111 * represented by "[[m00, m01, m02], [m10, m11, m12]]". |
|
112 * @param p the source point |
|
113 * @return new point instance |
|
114 */ |
|
115 public Point transform(final Point p) |
|
116 { |
|
117 return new Point( |
|
118 iComponents[M00] * p.x + iComponents[M01] * p.y + iComponents[M02], |
|
119 iComponents[M10] * p.x + iComponents[M11] * p.y + iComponents[M12]); |
|
120 } |
|
121 |
|
122 /** |
|
123 * Return the matrix of components used in this transform. The resulting |
|
124 * values are: |
|
125 * <pre> |
|
126 * [array[0] array[2] array[4]] |
|
127 * [array[1] array[3] array[5]] |
|
128 * </pre> |
|
129 * @return array that contains the matrix components. |
|
130 */ |
|
131 float[] getComponents() { |
|
132 return iComponents; |
|
133 } |
|
134 |
|
135 |
|
136 /** |
|
137 * Return the determinant of this transform matrix. If the determinant is |
|
138 * non-zero, the transform is invertible. |
|
139 * The determinant is calculated as: |
|
140 * <pre> |
|
141 * [m00 m01 m02] |
|
142 * [m10 m11 m12] = m00 * m11 - m01 * m10 |
|
143 * [ 0 0 1 ] |
|
144 * </pre> |
|
145 * @return the determinant |
|
146 */ |
|
147 public float determinant() { |
|
148 return ((iComponents[M00] * iComponents[M11]) - |
|
149 (iComponents[M01] * iComponents[M10])); |
|
150 } |
|
151 |
|
152 /** |
|
153 * The inverse is calculated as: |
|
154 * <pre> |
|
155 * [m00 m01 m02] |
|
156 * M= [m10 m11 m12] |
|
157 * [ 0 0 1 ] |
|
158 * |
|
159 * 1 [ m11/det -m01/det (m01*m12-m02*m11)/det] |
|
160 * inverse(M)= --- x adjoint(M) = [-m10/det m00/det (m10*m02-m00*m12)/det] |
|
161 * det [ 0 0 1 ] |
|
162 * </pre> |
|
163 */ |
|
164 public Matrix inverse() { |
|
165 // The inversion is useful for undoing transformations. |
|
166 float det = determinant(); |
|
167 if (det == 0) |
|
168 { |
|
169 throw new RuntimeException("Invalid determinant"); |
|
170 } |
|
171 return new Matrix( |
|
172 iComponents[M11] / det, // iMtx[M00] |
|
173 (-iComponents[M10]) / det, // iMtx[M10] |
|
174 (-iComponents[M01]) / det, // iMtx[M01] |
|
175 iComponents[M00] / det, // iMtx[M11] |
|
176 ((iComponents[M01] * iComponents[M12]) - (iComponents[M02] * iComponents[M11])) / det, |
|
177 ((iComponents[M10] * iComponents[M02]) - (iComponents[M00] * iComponents[M12])) / det); |
|
178 } |
|
179 |
|
180 /** |
|
181 * The multiply is calculated as: |
|
182 * <pre> |
|
183 * [a00 a01 a02] [b00 b01 b02] |
|
184 * this=[a10 a11 a12] B=[b10 b11 b12] |
|
185 * [ 0 0 1 ] [ 0 0 1 ] |
|
186 * |
|
187 * [(a00*b00+a01*b10) (a00*b01+a01*b11) (a00*b02+a01*b12+a02)] |
|
188 * [this] = [this]x[B] = [(a10*b00+a11*b10) (a10*b01+a11*b11) (a10*b02+a11*b12+a12)] |
|
189 * [ 0 0 1 ] |
|
190 * </pre> |
|
191 */ |
|
192 public Matrix multiply(Matrix b) { |
|
193 if(b == null) |
|
194 { |
|
195 throw new NullPointerException(); |
|
196 } |
|
197 float a00 = iComponents[M00]; // a |
|
198 float a10 = iComponents[M10]; // b |
|
199 float a01 = iComponents[M01]; // c |
|
200 float a11 = iComponents[M11]; // d |
|
201 float a02 = iComponents[M02]; // e |
|
202 float a12 = iComponents[M12]; // f |
|
203 iComponents[M00] = (a00 * b.iComponents[M00]) + (a01 * b.iComponents[M10]); // a |
|
204 iComponents[M10] = (a10 * b.iComponents[M00]) + (a11 * b.iComponents[M10]); // b |
|
205 iComponents[M01] = (a00 * b.iComponents[M01]) + (a01 * b.iComponents[M11]); // c |
|
206 iComponents[M11] = (a10 * b.iComponents[M01]) + (a11 * b.iComponents[M11]); // d |
|
207 iComponents[M02] = (a00 * b.iComponents[M02]) + (a01 * b.iComponents[M12]) + a02; // e |
|
208 iComponents[M12] = (a10 * b.iComponents[M02]) + (a11 * b.iComponents[M12]) + a12; // f |
|
209 return this; |
|
210 } |
|
211 |
|
212 /** |
|
213 * The rotation is calculated as: |
|
214 * <pre> |
|
215 * [ cos(angle) -sin(angle) 0 ] |
|
216 * [this] x [ sin(angle) cos(angle) 0 ] |
|
217 * [ 0 0 1 ] |
|
218 * </pre> |
|
219 */ |
|
220 public Matrix rotate(float angle) { |
|
221 if (angle % 360 == 0) { |
|
222 return this; |
|
223 } |
|
224 // Must convert degrees to radians since java.lang.Math expects radians |
|
225 angle = angle * (float)Math.PI / 180.0f; |
|
226 float c = (float)Math.cos(angle); |
|
227 float s = (float)Math.sin(angle); |
|
228 float m00 = iComponents[M00]; |
|
229 float m10 = iComponents[M10]; |
|
230 float m01 = iComponents[M01]; |
|
231 float m11 = iComponents[M11]; |
|
232 iComponents[M00] = m00 * c + m01 * s; |
|
233 iComponents[M10] = m10 * c + m11 * s; |
|
234 iComponents[M01] = m01 * c - m00 * s; |
|
235 iComponents[M11] = m11 * c - m10 * s; |
|
236 return this; |
|
237 } |
|
238 |
|
239 /** |
|
240 * The multiply is calculated as: |
|
241 * <pre> |
|
242 * [m00 m01 m02] [scaleFactor 0 0] |
|
243 * this=[m10 m11 m12] B=[ 0 scaleFactor 0] |
|
244 * [ 0 0 1 ] [ 0 0 1] |
|
245 * |
|
246 * [(a00*scaleFactor) (a01*scaleFactor) a02] |
|
247 * [this] = [this]x[B] = [(a10*scaleFactor) (a11*scaleFactor) a12] |
|
248 * [ 0 0 1 ] |
|
249 * </pre> |
|
250 * @see org.w3c.dom.svg.SVGMatrix#mScale() |
|
251 */ |
|
252 public Matrix scale(float scaleFactor) { |
|
253 if(scaleFactor == 1) { |
|
254 return this; |
|
255 } |
|
256 iComponents[M00] *= scaleFactor; |
|
257 iComponents[M01] *= scaleFactor; |
|
258 iComponents[M10] *= scaleFactor; |
|
259 iComponents[M11] *= scaleFactor; |
|
260 return this; |
|
261 } |
|
262 |
|
263 /** |
|
264 * |
|
265 */ |
|
266 public Matrix translate(float x, float y) { |
|
267 if(x == 0 && y == 0) { |
|
268 return this; |
|
269 } |
|
270 iComponents[M02] += (iComponents[M00] * x) + (iComponents[M01] * y); |
|
271 iComponents[M12] += (iComponents[M10] * x) + (iComponents[M11] * y); |
|
272 return this; |
|
273 } |
|
274 } |
|