OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE HTML> | |
2 <script src="../../resources/testharness.js"></script> | |
3 <script src="../../resources/testharnessreport.js"></script> | |
4 <script src="./resources/geometry-interfaces-test-helpers.js"></script> | |
5 <script> | |
6 | |
7 function deg2rad(degrees) { | |
8 return degrees * Math.PI / 180; | |
9 } | |
10 | |
11 function getRotationMatrix(x, y, z, alpha_in_degrees) { | |
12 // Vector normalizing | |
13 var nx = x; | |
14 var ny = y; | |
15 var nz = z; | |
16 var length = Math.sqrt(x * x + y * y + z * z); | |
17 if(length != 0) { | |
dominicc (has gone to gerrit)
2016/09/28 08:16:06
Space before (
Maybe if (length) is more succinct
Hwanseung Lee
2016/09/28 16:20:56
Done.
| |
18 nx = x / length; | |
19 ny = y / length; | |
20 nz = z / length; | |
21 } | |
22 | |
23 // The 3D rotation matrix is described in CSS Transforms with alpha. | |
24 // Please see: https://drafts.csswg.org/css-transforms-1/#Rotate3dDefined | |
25 var alpha_in_radian = deg2rad(alpha_in_degrees / 2); | |
dominicc (has gone to gerrit)
2016/09/28 08:16:06
radian -> radians
Hwanseung Lee
2016/09/28 16:20:56
Done.
| |
26 var sc = Math.sin(alpha_in_radian) * Math.cos(alpha_in_radian); | |
27 var sq = Math.sin(alpha_in_radian) * Math.sin(alpha_in_radian); | |
28 | |
29 var m11 = 1 - 2 * (ny * ny + nz * nz) * sq; | |
30 var m12 = 2 * (nx * ny * sq + nz * sc); | |
31 var m13 = 2 * (nx * nz * sq - ny * sc); | |
32 var m14 = 0; | |
33 var m21 = 2 * (nx * ny * sq - nz * sc); | |
34 var m22 = 1 - 2 * (nx * nx + nz * nz) * sq; | |
35 var m23 = 2 * (ny * nz * sq + nx * sc); | |
36 var m24 = 0; | |
37 var m31 = 2 * (nx * nz * sq + ny * sc); | |
38 var m32 = 2 * (ny * nz * sq - nx * sc); | |
39 var m33 = 1 - 2 * (nx * nx + ny * ny) * sq; | |
40 var m34 = 0; | |
41 var m41 = 0; | |
42 var m42 = 0; | |
43 var m43 = 0; | |
44 var m44 = 1; | |
45 | |
46 return initDomMatrix([m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m 34, m41, m42, m43, m44]); | |
47 } | |
48 | |
49 // TODO(hs1217.lee): create the DOMMatrix directly when the sequence constructor is supported. | |
50 function initDomMatrix(values) { | |
51 return DOMMatrix.fromFloat64Array(new Float64Array(values)); | |
52 } | |
53 | |
54 function initDOMMatrixReadOnly(values) { | |
55 return DOMMatrixReadOnly.fromFloat64Array(new Float64Array(values)); | |
56 } | |
57 | |
58 function initDomMatrixFor2DTest() { | |
59 return initDomMatrix([1, 2, 3, 4, 5, 6]); | |
60 } | |
61 | |
62 function initDomMatrixFor3DTest() { | |
63 return initDomMatrix([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); | |
64 } | |
65 | |
66 function initDOMMatrixReadOnlyFor2DTest() { | |
67 return initDOMMatrixReadOnly([1, 2, 3, 4, 5, 6]); | |
68 } | |
69 | |
70 function initDOMMatrixReadOnlyFor3DTest() { | |
71 return initDOMMatrixReadOnly([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1 5, 16]); | |
72 } | |
73 | |
74 test(() => { | |
75 var matrix2d = initDomMatrixFor2DTest(); | |
76 var expectedMatrix = initDomMatrixFor2DTest(); | |
77 matrix2d.rotateAxisAngleSelf(); | |
78 expectedMatrix.multiplySelf(getRotationMatrix(0, 0, 0, 0)); | |
79 assert_true(matrix2d.is2D); | |
80 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
81 }, "DOMMatrix 2d - rotateAxisAngleSelf()"); | |
82 | |
83 test(() => { | |
84 var matrix2d = initDomMatrixFor2DTest(); | |
85 var expectedMatrix = initDomMatrixFor2DTest(); | |
86 matrix2d.rotateAxisAngleSelf(0, 0, 1); | |
87 expectedMatrix.multiplySelf(getRotationMatrix(0, 0, 1, 0)); | |
88 assert_true(matrix2d.is2D); | |
89 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
90 }, "DOMMatrix 2d - rotateAxisAngleSelf(0, 0, 1)"); | |
91 | |
92 test(() => { | |
93 var matrix2d = initDomMatrixFor2DTest(); | |
94 var expectedMatrix = initDomMatrixFor2DTest(); | |
95 matrix2d.rotateAxisAngleSelf(1, 1, 1); | |
96 expectedMatrix.multiplySelf(getRotationMatrix(1, 1, 1, 0)); | |
97 assert_false(matrix2d.is2D); | |
98 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
99 }, "DOMMatrix 2d - rotateAxisAngleSelf(1, 1, 1, 0)"); | |
100 | |
101 test(() => { | |
102 var matrix2d = initDomMatrixFor2DTest(); | |
103 var expectedMatrix = initDomMatrixFor2DTest(); | |
104 matrix2d.rotateAxisAngleSelf(1, 0, 0, 10); | |
105 expectedMatrix.multiplySelf(getRotationMatrix(1, 0, 0, 10)); | |
106 assert_false(matrix2d.is2D); | |
107 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
108 }, "DOMMatrix 2d - rotateAxisAngleSelf(1, 0, 0, 10)"); | |
109 | |
110 test(() => { | |
111 var matrix2d = initDomMatrixFor2DTest(); | |
112 var expectedMatrix = initDomMatrixFor2DTest(); | |
113 matrix2d.rotateAxisAngleSelf(0, 1, 0, 27); | |
114 expectedMatrix.multiplySelf(getRotationMatrix(0, 1, 0, 27)); | |
115 assert_false(matrix2d.is2D); | |
116 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
117 }, "DOMMatrix 2d - rotateAxisAngleSelf(0, 1, 0, 27)"); | |
118 | |
119 test(() => { | |
120 var matrix2d = initDomMatrixFor2DTest(); | |
121 var expectedMatrix = initDomMatrixFor2DTest(); | |
122 matrix2d.rotateAxisAngleSelf(0, 0, 1, 38); | |
123 expectedMatrix.multiplySelf(getRotationMatrix(0, 0, 1, 38)); | |
124 assert_true(matrix2d.is2D); | |
125 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
126 }, "DOMMatrix 2d - rotateAxisAngleSelf(0, 0, 1, 38)"); | |
127 | |
128 test(() => { | |
129 var matrix2d = initDomMatrixFor2DTest(); | |
130 var expectedMatrix = initDomMatrixFor2DTest(); | |
131 matrix2d.rotateAxisAngleSelf(1, 1, 1, 45); | |
132 expectedMatrix.multiplySelf(getRotationMatrix(1, 1, 1, 45)); | |
133 assert_false(matrix2d.is2D); | |
134 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
135 }, "DOMMatrix 2d - rotateAxisAngleSelf(1, 1, 1, 45)"); | |
136 | |
137 test(() => { | |
138 var matrix3d = initDomMatrixFor3DTest(); | |
139 var expectedMatrix = initDomMatrixFor3DTest(); | |
140 matrix3d.rotateAxisAngleSelf(); | |
141 expectedMatrix.multiplySelf(getRotationMatrix(0, 0, 0, 0)); | |
142 assert_false(matrix3d.is2D); | |
143 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
144 }, "DOMMatrix 3d - rotateAxisAngleSelf()"); | |
145 | |
146 test(() => { | |
147 var matrix3d = initDomMatrixFor3DTest(); | |
148 var expectedMatrix = initDomMatrixFor3DTest(); | |
149 matrix3d.rotateAxisAngleSelf(0, 0, 1); | |
150 expectedMatrix.multiplySelf(getRotationMatrix(0, 0, 1, 0)); | |
151 assert_false(matrix3d.is2D); | |
152 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
153 }, "DOMMatrix 3d - rotateAxisAngleSelf(0, 0, 1)"); | |
154 | |
155 test(() => { | |
156 var matrix3d = initDomMatrixFor3DTest(); | |
157 var expectedMatrix = initDomMatrixFor3DTest(); | |
158 matrix3d.rotateAxisAngleSelf(0, 0, 1, 0); | |
159 expectedMatrix.multiplySelf(getRotationMatrix(0, 0, 1, 0)); | |
160 assert_false(matrix3d.is2D); | |
161 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
162 }, "DOMMatrix 3d - rotateAxisAngleSelf(0, 0, 1, 0)"); | |
163 | |
164 test(() => { | |
165 var matrix3d = initDomMatrixFor3DTest(); | |
166 var expectedMatrix = initDomMatrixFor3DTest(); | |
167 matrix3d.rotateAxisAngleSelf(1, 0, 0, 19); | |
168 expectedMatrix.multiplySelf(getRotationMatrix(1, 0, 0, 19)); | |
169 assert_false(matrix3d.is2D); | |
170 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
171 }, "DOMMatrix 3d - rotateAxisAngleSelf(1, 0, 0, 19)"); | |
172 | |
173 test(() => { | |
174 var matrix3d = initDomMatrixFor3DTest(); | |
175 var expectedMatrix = initDomMatrixFor3DTest(); | |
176 matrix3d.rotateAxisAngleSelf(0, 1, 0, 46); | |
177 expectedMatrix.multiplySelf(getRotationMatrix(0, 1, 0, 46)); | |
178 assert_false(matrix3d.is2D); | |
179 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
180 }, "DOMMatrix 3d - rotateAxisAngleSelf(0, 1, 0, 46)"); | |
181 | |
182 test(() => { | |
183 var matrix3d = initDomMatrixFor3DTest(); | |
184 var expectedMatrix = initDomMatrixFor3DTest(); | |
185 matrix3d.rotateAxisAngleSelf(0, 0, 1, 65); | |
186 expectedMatrix.multiplySelf(getRotationMatrix(0, 0, 1, 65)); | |
187 assert_false(matrix3d.is2D); | |
188 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
189 }, "DOMMatrix 3d - rotateAxisAngleSelf(0, 0, 1, 65)"); | |
190 | |
191 test(() => { | |
192 var matrix3d = initDomMatrixFor3DTest(); | |
193 var expectedMatrix = initDomMatrixFor3DTest(); | |
194 matrix3d.rotateAxisAngleSelf(1, 1, 1, 67); | |
195 expectedMatrix.multiplySelf(getRotationMatrix(1, 1, 1, 67)); | |
196 assert_false(matrix3d.is2D); | |
197 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
198 }, "DOMMatrix 3d - rotateAxisAngleSelf(1, 1, 1, 67)"); | |
199 | |
200 test(() => { | |
201 var matrix2d = initDOMMatrixReadOnlyFor2DTest(); | |
202 var expectedMatrix = initDOMMatrixReadOnlyFor2DTest(); | |
203 matrix2d = matrix2d.rotateAxisAngle(); | |
204 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(0, 0, 0, 0)); | |
205 assert_true(matrix2d.is2D); | |
206 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
207 }, "DOMMatrixReadOnly 2d - rotateAxisAngle()"); | |
208 | |
209 test(() => { | |
210 var matrix2d = initDOMMatrixReadOnlyFor2DTest(); | |
211 var expectedMatrix = initDOMMatrixReadOnlyFor2DTest(); | |
212 matrix2d = matrix2d.rotateAxisAngle(0, 0, 1); | |
213 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(0, 0, 1, 0)); | |
214 assert_true(matrix2d.is2D); | |
215 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
216 }, "DOMMatrixReadOnly 2d - rotateAxisAngle(0, 0, 1)"); | |
217 | |
218 test(() => { | |
219 var matrix2d = initDOMMatrixReadOnlyFor2DTest(); | |
220 var expectedMatrix = initDOMMatrixReadOnlyFor2DTest(); | |
221 matrix2d = matrix2d.rotateAxisAngle(1, 1, 1); | |
222 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(1, 1, 1, 0)); | |
223 }, "DOMMatrixReadOnly 2d - rotateAxisAngle(1, 1, 1, 0)"); | |
224 | |
225 test(() => { | |
226 var matrix2d = initDOMMatrixReadOnlyFor2DTest(); | |
227 var expectedMatrix = initDOMMatrixReadOnlyFor2DTest(); | |
228 matrix2d = matrix2d.rotateAxisAngle(1, 0, 0, 21); | |
229 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(1, 0, 0, 21)); | |
230 assert_false(matrix2d.is2D); | |
231 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
232 }, "DOMMatrixReadOnly 2d - rotateAxisAngle(1, 0, 0, 21)"); | |
233 | |
234 test(() => { | |
235 var matrix2d = initDOMMatrixReadOnlyFor2DTest(); | |
236 var expectedMatrix = initDOMMatrixReadOnlyFor2DTest(); | |
237 matrix2d = matrix2d.rotateAxisAngle(0, 1, 0, 35); | |
238 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(0, 1, 0, 35)); | |
239 assert_false(matrix2d.is2D); | |
240 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
241 }, "DOMMatrixReadOnly 2d - rotateAxisAngle(0, 1, 0, 35)"); | |
242 | |
243 test(() => { | |
244 var matrix2d = initDOMMatrixReadOnlyFor2DTest(); | |
245 var expectedMatrix = initDOMMatrixReadOnlyFor2DTest(); | |
246 matrix2d = matrix2d.rotateAxisAngle(0, 0, 1, 55); | |
247 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(0, 0, 1, 55)); | |
248 assert_true(matrix2d.is2D); | |
249 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
250 }, "DOMMatrixReadOnly 2d - rotateAxisAngle(0, 0, 1, 55)"); | |
251 | |
252 test(() => { | |
253 var matrix2d = initDOMMatrixReadOnlyFor2DTest(); | |
254 var expectedMatrix = initDOMMatrixReadOnlyFor2DTest(); | |
255 matrix2d = matrix2d.rotateAxisAngle(1, 1, 1, 75); | |
256 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(1, 1, 1, 75)); | |
257 assert_false(matrix2d.is2D); | |
258 assert_array_almost_equals(matrix2d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
259 }, "DOMMatrixReadOnly 2d - rotateAxisAngle(1, 1, 1, 75)"); | |
260 | |
261 test(() => { | |
262 var matrix3d = initDOMMatrixReadOnlyFor3DTest(); | |
263 var expectedMatrix = initDOMMatrixReadOnlyFor3DTest(); | |
264 matrix3d = matrix3d.rotateAxisAngle(); | |
265 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(0, 0, 0, 0)); | |
266 assert_false(matrix3d.is2D); | |
267 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
268 }, "DOMMatrixReadOnly 3d - rotateAxisAngle()"); | |
269 | |
270 test(() => { | |
271 var matrix3d = initDOMMatrixReadOnlyFor3DTest(); | |
272 var expectedMatrix = initDOMMatrixReadOnlyFor3DTest(); | |
273 matrix3d = matrix3d.rotateAxisAngle(0, 0, 1); | |
274 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(0, 0, 1, 0)); | |
275 assert_false(matrix3d.is2D); | |
276 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
277 }, "DOMMatrixReadOnly 3d - rotateAxisAngle(0, 0, 1)"); | |
278 | |
279 test(() => { | |
280 var matrix3d = initDOMMatrixReadOnlyFor3DTest(); | |
281 var expectedMatrix = initDOMMatrixReadOnlyFor3DTest(); | |
282 matrix3d = matrix3d.rotateAxisAngle(0, 0, 1, 0); | |
283 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(0, 0, 1, 0)); | |
284 assert_false(matrix3d.is2D); | |
285 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
286 }, "DOMMatrixReadOnly 3d - rotateAxisAngle(0, 0, 1, 0)"); | |
287 | |
288 test(() => { | |
289 var matrix3d = initDOMMatrixReadOnlyFor3DTest(); | |
290 var expectedMatrix = initDOMMatrixReadOnlyFor3DTest(); | |
291 matrix3d = matrix3d.rotateAxisAngle(1, 1, 1, 0); | |
292 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(1, 1, 1, 0)); | |
293 assert_false(matrix3d.is2D); | |
294 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
295 }, "DOMMatrixReadOnly 3d - rotateAxisAngle(1, 1, 1, 0)"); | |
296 | |
297 test(() => { | |
298 var matrix3d = initDOMMatrixReadOnlyFor3DTest(); | |
299 var expectedMatrix = initDOMMatrixReadOnlyFor3DTest(); | |
300 matrix3d = matrix3d.rotateAxisAngle(1, 0, 0, 105); | |
301 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(1, 0, 0, 105)); | |
302 assert_false(matrix3d.is2D); | |
303 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
304 }, "DOMMatrixReadOnly 3d - rotateAxisAngle(1, 0, 0, 105)"); | |
305 | |
306 test(() => { | |
307 var matrix3d = initDOMMatrixReadOnlyFor3DTest(); | |
308 var expectedMatrix = initDOMMatrixReadOnlyFor3DTest(); | |
309 matrix3d = matrix3d.rotateAxisAngle(0, 1, 0, 45); | |
310 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(0, 1, 0, 45)); | |
311 assert_false(matrix3d.is2D); | |
312 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
313 }, "DOMMatrixReadOnly 3d - rotateAxisAngle(0, 1, 0, 45)"); | |
314 | |
315 test(() => { | |
316 var matrix3d = initDOMMatrixReadOnlyFor3DTest(); | |
317 var expectedMatrix = initDOMMatrixReadOnlyFor3DTest(); | |
318 matrix3d = matrix3d.rotateAxisAngle(0, 0, 1, 65); | |
319 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(0, 0, 1, 65)); | |
320 assert_false(matrix3d.is2D); | |
321 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
322 }, "DOMMatrixReadOnly 3d - rotateAxisAngle(0, 0, 1, 65)"); | |
323 | |
324 test(() => { | |
325 var matrix3d = initDOMMatrixReadOnlyFor3DTest(); | |
326 var expectedMatrix = initDOMMatrixReadOnlyFor3DTest(); | |
327 matrix3d = matrix3d.rotateAxisAngle(1, 1, 1, 78); | |
328 expectedMatrix = expectedMatrix.multiply(getRotationMatrix(1, 1, 1, 78)); | |
329 assert_false(matrix3d.is2D); | |
330 assert_array_almost_equals(matrix3d.toFloat64Array(), expectedMatrix.toFloat64 Array()); | |
331 }, "DOMMatrixReadOnly 3d - rotateAxisAngle(1, 1, 1, 78)"); | |
332 | |
333 </script> | |
OLD | NEW |