Debian Codegraph @

closure-library/closure/goog/math/affinetransform_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.math.AffineTransformTest');
16
17 goog.require('goog.array');
18 goog.require('goog.math');
19 goog.require('goog.math.AffineTransform');
20 goog.require('goog.testing.jsunit');
21
22 goog.setTestOnly('goog.math.AffineTransformTest');
23
24 function testGetTranslateInstance() {
25 var tx = goog.math.AffineTransform.getTranslateInstance(2, 4);
26 assertEquals(1, tx.getScaleX());
27 assertEquals(0, tx.getShearY());
28 assertEquals(0, tx.getShearX());
29 assertEquals(1, tx.getScaleY());
30 assertEquals(2, tx.getTranslateX());
31 assertEquals(4, tx.getTranslateY());
32 }
33
34 function testGetScaleInstance() {
35 var tx = goog.math.AffineTransform.getScaleInstance(2, 4);
36 assertEquals(2, tx.getScaleX());
37 assertEquals(0, tx.getShearY());
38 assertEquals(0, tx.getShearX());
39 assertEquals(4, tx.getScaleY());
40 assertEquals(0, tx.getTranslateX());
41 assertEquals(0, tx.getTranslateY());
42 }
43
44 function testGetRotateInstance() {
45 var tx = goog.math.AffineTransform.getRotateInstance(Math.PI / 2, 1, 2);
46 assertRoughlyEquals(0, tx.getScaleX(), 1e-9);
47 assertRoughlyEquals(1, tx.getShearY(), 1e-9);
48 assertRoughlyEquals(-1, tx.getShearX(), 1e-9);
49 assertRoughlyEquals(0, tx.getScaleY(), 1e-9);
50 assertRoughlyEquals(3, tx.getTranslateX(), 1e-9);
51 assertRoughlyEquals(1, tx.getTranslateY(), 1e-9);
52 }
53
54 function testGetShearInstance() {
55 var tx = goog.math.AffineTransform.getShearInstance(2, 4);
56 assertEquals(1, tx.getScaleX());
57 assertEquals(4, tx.getShearY());
58 assertEquals(2, tx.getShearX());
59 assertEquals(1, tx.getScaleY());
60 assertEquals(0, tx.getTranslateX());
61 assertEquals(0, tx.getTranslateY());
62 }
63
64 function testConstructor() {
65 assertThrows(function() { new goog.math.AffineTransform([0, 0]); });
66 assertThrows(function() { new goog.math.AffineTransform({}); });
67 assertThrows(function() {
68 new goog.math.AffineTransform(0, 0, 0, 'a', 0, 0);
69 });
70
71 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
72 assertEquals(1, tx.getScaleX());
73 assertEquals(2, tx.getShearY());
74 assertEquals(3, tx.getShearX());
75 assertEquals(4, tx.getScaleY());
76 assertEquals(5, tx.getTranslateX());
77 assertEquals(6, tx.getTranslateY());
78
79 tx = new goog.math.AffineTransform();
80 assert(tx.isIdentity());
81 }
82
83 function testIsIdentity() {
84 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
85 assertFalse(tx.isIdentity());
86 tx.setTransform(1, 0, 0, 1, 0, 0);
87 assert(tx.isIdentity());
88 }
89
90 function testClone() {
91 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
92 var copy = tx.clone();
93 assertEquals(copy.getScaleX(), tx.getScaleX());
94 assertEquals(copy.getShearY(), tx.getShearY());
95 assertEquals(copy.getShearX(), tx.getShearX());
96 assertEquals(copy.getScaleY(), tx.getScaleY());
97 assertEquals(copy.getTranslateX(), tx.getTranslateX());
98 assertEquals(copy.getTranslateY(), tx.getTranslateY());
99 }
100
101 function testSetTransform() {
102 var tx = new goog.math.AffineTransform();
103 assertThrows(function() { tx.setTransform(1, 2, 3, 4, 6); });
104 assertThrows(function() { tx.setTransform('a', 2, 3, 4, 5, 6); });
105
106 tx.setTransform(1, 2, 3, 4, 5, 6);
107 assertEquals(1, tx.getScaleX());
108 assertEquals(2, tx.getShearY());
109 assertEquals(3, tx.getShearX());
110 assertEquals(4, tx.getScaleY());
111 assertEquals(5, tx.getTranslateX());
112 assertEquals(6, tx.getTranslateY());
113 }
114
115 function testScale() {
116 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
117 tx.scale(2, 3);
118 assertEquals(2, tx.getScaleX());
119 assertEquals(4, tx.getShearY());
120 assertEquals(9, tx.getShearX());
121 assertEquals(12, tx.getScaleY());
122 assertEquals(5, tx.getTranslateX());
123 assertEquals(6, tx.getTranslateY());
124 }
125
126 function testPreScale() {
127 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
128 tx.preScale(2, 3);
129 assertEquals(2, tx.getScaleX());
130 assertEquals(6, tx.getShearY());
131 assertEquals(6, tx.getShearX());
132 assertEquals(12, tx.getScaleY());
133 assertEquals(10, tx.getTranslateX());
134 assertEquals(18, tx.getTranslateY());
135 }
136
137 function testTranslate() {
138 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
139 tx.translate(2, 3);
140 assertEquals(1, tx.getScaleX());
141 assertEquals(2, tx.getShearY());
142 assertEquals(3, tx.getShearX());
143 assertEquals(4, tx.getScaleY());
144 assertEquals(16, tx.getTranslateX());
145 assertEquals(22, tx.getTranslateY());
146 }
147
148 function testPreTranslate() {
149 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
150 tx.preTranslate(2, 3);
151 assertEquals(1, tx.getScaleX());
152 assertEquals(2, tx.getShearY());
153 assertEquals(3, tx.getShearX());
154 assertEquals(4, tx.getScaleY());
155 assertEquals(7, tx.getTranslateX());
156 assertEquals(9, tx.getTranslateY());
157 }
158
159 function testRotate() {
160 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
161 tx.rotate(Math.PI / 2, 1, 1);
162 assertRoughlyEquals(3, tx.getScaleX(), 1e-9);
163 assertRoughlyEquals(4, tx.getShearY(), 1e-9);
164 assertRoughlyEquals(-1, tx.getShearX(), 1e-9);
165 assertRoughlyEquals(-2, tx.getScaleY(), 1e-9);
166 assertRoughlyEquals(7, tx.getTranslateX(), 1e-9);
167 assertRoughlyEquals(10, tx.getTranslateY(), 1e-9);
168 }
169
170 function testPreRotate() {
171 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
172 tx.preRotate(Math.PI / 2, 1, 1);
173 assertRoughlyEquals(-2, tx.getScaleX(), 1e-9);
174 assertRoughlyEquals(1, tx.getShearY(), 1e-9);
175 assertRoughlyEquals(-4, tx.getShearX(), 1e-9);
176 assertRoughlyEquals(3, tx.getScaleY(), 1e-9);
177 assertRoughlyEquals(-4, tx.getTranslateX(), 1e-9);
178 assertRoughlyEquals(5, tx.getTranslateY(), 1e-9);
179 }
180
181 function testShear() {
182 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
183 tx.shear(2, 3);
184 assertEquals(10, tx.getScaleX());
185 assertEquals(14, tx.getShearY());
186 assertEquals(5, tx.getShearX());
187 assertEquals(8, tx.getScaleY());
188 assertEquals(5, tx.getTranslateX());
189 assertEquals(6, tx.getTranslateY());
190 }
191
192 function testPreShear() {
193 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
194 tx.preShear(2, 3);
195 assertEquals(5, tx.getScaleX());
196 assertEquals(5, tx.getShearY());
197 assertEquals(11, tx.getShearX());
198 assertEquals(13, tx.getScaleY());
199 assertEquals(17, tx.getTranslateX());
200 assertEquals(21, tx.getTranslateY());
201 }
202
203 function testConcatentate() {
204 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
205 tx.concatenate(new goog.math.AffineTransform(2, 1, 6, 5, 4, 3));
206 assertEquals(5, tx.getScaleX());
207 assertEquals(8, tx.getShearY());
208 assertEquals(21, tx.getShearX());
209 assertEquals(32, tx.getScaleY());
210 assertEquals(18, tx.getTranslateX());
211 assertEquals(26, tx.getTranslateY());
212 }
213
214 function testPreConcatentate() {
215 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
216 tx.preConcatenate(new goog.math.AffineTransform(2, 1, 6, 5, 4, 3));
217 assertEquals(14, tx.getScaleX());
218 assertEquals(11, tx.getShearY());
219 assertEquals(30, tx.getShearX());
220 assertEquals(23, tx.getScaleY());
221 assertEquals(50, tx.getTranslateX());
222 assertEquals(38, tx.getTranslateY());
223 }
224
225 function testAssociativeConcatenate() {
226 var x =
227 new goog.math.AffineTransform(2, 3, 5, 7, 11, 13)
228 .concatenate(new goog.math.AffineTransform(17, 19, 23, 29, 31, 37));
229 var y =
230 new goog.math.AffineTransform(17, 19, 23, 29, 31, 37)
231 .preConcatenate(new goog.math.AffineTransform(2, 3, 5, 7, 11, 13));
232 assertEquals(x.getScaleX(), y.getScaleX());
233 assertEquals(x.getShearY(), y.getShearY());
234 assertEquals(x.getShearX(), y.getShearX());
235 assertEquals(x.getScaleY(), y.getScaleY());
236 assertEquals(x.getTranslateX(), y.getTranslateX());
237 assertEquals(x.getTranslateY(), y.getTranslateY());
238 }
239
240 function testTransform() {
241 var srcPts = [0, 0, 1, 0, 1, 1, 0, 1];
242 var dstPts = [];
243 var tx = goog.math.AffineTransform.getScaleInstance(2, 3);
244 tx.translate(5, 10);
245 tx.rotate(Math.PI / 4, 5, 10);
246 tx.transform(srcPts, 0, dstPts, 0, 4);
247 assert(
248 goog.array.equals(
249 [
250 27.071068, 28.180195, 28.485281, 30.301516, 27.071068, 32.422836,
251 25.656855, 30.301516
252 ],
253 dstPts, goog.math.nearlyEquals));
254 }
255
256 function testGetDeterminant() {
257 var tx = goog.math.AffineTransform.getScaleInstance(2, 3);
258 tx.translate(5, 10);
259 tx.rotate(Math.PI / 4, 5, 10);
260 assertRoughlyEquals(6, tx.getDeterminant(), 0.001);
261 }
262
263 function testIsInvertible() {
264 assertTrue(new goog.math.AffineTransform(2, 3, 4, 5, 6, 7).isInvertible());
265 assertTrue(new goog.math.AffineTransform(1, 0, 0, 1, 0, 0).isInvertible());
266 assertFalse(new goog.math.AffineTransform(NaN, 0, 0, 1, 0, 0).isInvertible());
267 assertFalse(new goog.math.AffineTransform(1, NaN, 0, 1, 0, 0).isInvertible());
268 assertFalse(new goog.math.AffineTransform(1, 0, NaN, 1, 0, 0).isInvertible());
269 assertFalse(new goog.math.AffineTransform(1, 0, 0, NaN, 0, 0).isInvertible());
270 assertFalse(new goog.math.AffineTransform(1, 0, 0, 1, NaN, 0).isInvertible());
271 assertFalse(new goog.math.AffineTransform(1, 0, 0, 1, 0, NaN).isInvertible());
272 assertFalse(
273 new goog.math.AffineTransform(Infinity, 0, 0, 1, 0, 0).isInvertible());
274 assertFalse(
275 new goog.math.AffineTransform(1, Infinity, 0, 1, 0, 0).isInvertible());
276 assertFalse(
277 new goog.math.AffineTransform(1, 0, Infinity, 1, 0, 0).isInvertible());
278 assertFalse(
279 new goog.math.AffineTransform(1, 0, 0, Infinity, 0, 0).isInvertible());
280 assertFalse(
281 new goog.math.AffineTransform(1, 0, 0, 1, Infinity, 0).isInvertible());
282 assertFalse(
283 new goog.math.AffineTransform(1, 0, 0, 1, 0, Infinity).isInvertible());
284 assertFalse(new goog.math.AffineTransform(0, 0, 0, 0, 1, 0).isInvertible());
285 }
286
287 function testCreateInverse() {
288 var tx = goog.math.AffineTransform.getScaleInstance(2, 3);
289 tx.translate(5, 10);
290 tx.rotate(Math.PI / 4, 5, 10);
291 var inverse = tx.createInverse();
292 assert(goog.math.nearlyEquals(0.353553, inverse.getScaleX()));
293 assert(goog.math.nearlyEquals(-0.353553, inverse.getShearY()));
294 assert(goog.math.nearlyEquals(0.235702, inverse.getShearX()));
295 assert(goog.math.nearlyEquals(0.235702, inverse.getScaleY()));
296 assert(goog.math.nearlyEquals(-16.213203, inverse.getTranslateX()));
297 assert(goog.math.nearlyEquals(2.928932, inverse.getTranslateY()));
298 }
299
300 function testCopyFrom() {
301 var from = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
302 var to = new goog.math.AffineTransform();
303 to.copyFrom(from);
304 assertEquals(from.getScaleX(), to.getScaleX());
305 assertEquals(from.getShearY(), to.getShearY());
306 assertEquals(from.getShearX(), to.getShearX());
307 assertEquals(from.getScaleY(), to.getScaleY());
308 assertEquals(from.getTranslateX(), to.getTranslateX());
309 assertEquals(from.getTranslateY(), to.getTranslateY());
310 }
311
312 function testToString() {
313 var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
314 assertEquals('matrix(1,2,3,4,5,6)', tx.toString());
315 }
316
317 function testEquals() {
318 var tx1 = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
319 var tx2 = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
320 assertEqualsMethod(tx1, tx2, true);
321
322 tx2 = new goog.math.AffineTransform(-1, 2, 3, 4, 5, 6);
323 assertEqualsMethod(tx1, tx2, false);
324
325 tx2 = new goog.math.AffineTransform(1, -1, 3, 4, 5, 6);
326 assertEqualsMethod(tx1, tx2, false);
327
328 tx2 = new goog.math.AffineTransform(1, 2, -3, 4, 5, 6);
329 assertEqualsMethod(tx1, tx2, false);
330
331 tx2 = new goog.math.AffineTransform(1, 2, 3, -4, 5, 6);
332 assertEqualsMethod(tx1, tx2, false);
333
334 tx2 = new goog.math.AffineTransform(1, 2, 3, 4, -5, 6);
335 assertEqualsMethod(tx1, tx2, false);
336
337 tx2 = new goog.math.AffineTransform(1, 2, 3, 4, 5, -6);
338 assertEqualsMethod(tx1, tx2, false);
339 }
340
341 function assertEqualsMethod(tx1, tx2, expected) {
342 assertEquals(expected, tx1.equals(tx2));
343 assertEquals(expected, tx2.equals(tx1));
344 assertEquals(true, tx1.equals(tx1));
345 assertEquals(true, tx2.equals(tx2));
346 }