affinetransform_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. goog.provide('goog.math.AffineTransformTest');
  15. goog.require('goog.array');
  16. goog.require('goog.math');
  17. goog.require('goog.math.AffineTransform');
  18. goog.require('goog.testing.jsunit');
  19. goog.setTestOnly('goog.math.AffineTransformTest');
  20. function testGetTranslateInstance() {
  21. var tx = goog.math.AffineTransform.getTranslateInstance(2, 4);
  22. assertEquals(1, tx.getScaleX());
  23. assertEquals(0, tx.getShearY());
  24. assertEquals(0, tx.getShearX());
  25. assertEquals(1, tx.getScaleY());
  26. assertEquals(2, tx.getTranslateX());
  27. assertEquals(4, tx.getTranslateY());
  28. }
  29. function testGetScaleInstance() {
  30. var tx = goog.math.AffineTransform.getScaleInstance(2, 4);
  31. assertEquals(2, tx.getScaleX());
  32. assertEquals(0, tx.getShearY());
  33. assertEquals(0, tx.getShearX());
  34. assertEquals(4, tx.getScaleY());
  35. assertEquals(0, tx.getTranslateX());
  36. assertEquals(0, tx.getTranslateY());
  37. }
  38. function testGetRotateInstance() {
  39. var tx = goog.math.AffineTransform.getRotateInstance(Math.PI / 2, 1, 2);
  40. assertRoughlyEquals(0, tx.getScaleX(), 1e-9);
  41. assertRoughlyEquals(1, tx.getShearY(), 1e-9);
  42. assertRoughlyEquals(-1, tx.getShearX(), 1e-9);
  43. assertRoughlyEquals(0, tx.getScaleY(), 1e-9);
  44. assertRoughlyEquals(3, tx.getTranslateX(), 1e-9);
  45. assertRoughlyEquals(1, tx.getTranslateY(), 1e-9);
  46. }
  47. function testGetShearInstance() {
  48. var tx = goog.math.AffineTransform.getShearInstance(2, 4);
  49. assertEquals(1, tx.getScaleX());
  50. assertEquals(4, tx.getShearY());
  51. assertEquals(2, tx.getShearX());
  52. assertEquals(1, tx.getScaleY());
  53. assertEquals(0, tx.getTranslateX());
  54. assertEquals(0, tx.getTranslateY());
  55. }
  56. function testConstructor() {
  57. assertThrows(function() { new goog.math.AffineTransform([0, 0]); });
  58. assertThrows(function() { new goog.math.AffineTransform({}); });
  59. assertThrows(function() {
  60. new goog.math.AffineTransform(0, 0, 0, 'a', 0, 0);
  61. });
  62. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  63. assertEquals(1, tx.getScaleX());
  64. assertEquals(2, tx.getShearY());
  65. assertEquals(3, tx.getShearX());
  66. assertEquals(4, tx.getScaleY());
  67. assertEquals(5, tx.getTranslateX());
  68. assertEquals(6, tx.getTranslateY());
  69. tx = new goog.math.AffineTransform();
  70. assert(tx.isIdentity());
  71. }
  72. function testIsIdentity() {
  73. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  74. assertFalse(tx.isIdentity());
  75. tx.setTransform(1, 0, 0, 1, 0, 0);
  76. assert(tx.isIdentity());
  77. }
  78. function testClone() {
  79. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  80. var copy = tx.clone();
  81. assertEquals(copy.getScaleX(), tx.getScaleX());
  82. assertEquals(copy.getShearY(), tx.getShearY());
  83. assertEquals(copy.getShearX(), tx.getShearX());
  84. assertEquals(copy.getScaleY(), tx.getScaleY());
  85. assertEquals(copy.getTranslateX(), tx.getTranslateX());
  86. assertEquals(copy.getTranslateY(), tx.getTranslateY());
  87. }
  88. function testSetTransform() {
  89. var tx = new goog.math.AffineTransform();
  90. assertThrows(function() { tx.setTransform(1, 2, 3, 4, 6); });
  91. assertThrows(function() { tx.setTransform('a', 2, 3, 4, 5, 6); });
  92. tx.setTransform(1, 2, 3, 4, 5, 6);
  93. assertEquals(1, tx.getScaleX());
  94. assertEquals(2, tx.getShearY());
  95. assertEquals(3, tx.getShearX());
  96. assertEquals(4, tx.getScaleY());
  97. assertEquals(5, tx.getTranslateX());
  98. assertEquals(6, tx.getTranslateY());
  99. }
  100. function testScale() {
  101. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  102. tx.scale(2, 3);
  103. assertEquals(2, tx.getScaleX());
  104. assertEquals(4, tx.getShearY());
  105. assertEquals(9, tx.getShearX());
  106. assertEquals(12, tx.getScaleY());
  107. assertEquals(5, tx.getTranslateX());
  108. assertEquals(6, tx.getTranslateY());
  109. }
  110. function testPreScale() {
  111. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  112. tx.preScale(2, 3);
  113. assertEquals(2, tx.getScaleX());
  114. assertEquals(6, tx.getShearY());
  115. assertEquals(6, tx.getShearX());
  116. assertEquals(12, tx.getScaleY());
  117. assertEquals(10, tx.getTranslateX());
  118. assertEquals(18, tx.getTranslateY());
  119. }
  120. function testTranslate() {
  121. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  122. tx.translate(2, 3);
  123. assertEquals(1, tx.getScaleX());
  124. assertEquals(2, tx.getShearY());
  125. assertEquals(3, tx.getShearX());
  126. assertEquals(4, tx.getScaleY());
  127. assertEquals(16, tx.getTranslateX());
  128. assertEquals(22, tx.getTranslateY());
  129. }
  130. function testPreTranslate() {
  131. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  132. tx.preTranslate(2, 3);
  133. assertEquals(1, tx.getScaleX());
  134. assertEquals(2, tx.getShearY());
  135. assertEquals(3, tx.getShearX());
  136. assertEquals(4, tx.getScaleY());
  137. assertEquals(7, tx.getTranslateX());
  138. assertEquals(9, tx.getTranslateY());
  139. }
  140. function testRotate() {
  141. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  142. tx.rotate(Math.PI / 2, 1, 1);
  143. assertRoughlyEquals(3, tx.getScaleX(), 1e-9);
  144. assertRoughlyEquals(4, tx.getShearY(), 1e-9);
  145. assertRoughlyEquals(-1, tx.getShearX(), 1e-9);
  146. assertRoughlyEquals(-2, tx.getScaleY(), 1e-9);
  147. assertRoughlyEquals(7, tx.getTranslateX(), 1e-9);
  148. assertRoughlyEquals(10, tx.getTranslateY(), 1e-9);
  149. }
  150. function testPreRotate() {
  151. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  152. tx.preRotate(Math.PI / 2, 1, 1);
  153. assertRoughlyEquals(-2, tx.getScaleX(), 1e-9);
  154. assertRoughlyEquals(1, tx.getShearY(), 1e-9);
  155. assertRoughlyEquals(-4, tx.getShearX(), 1e-9);
  156. assertRoughlyEquals(3, tx.getScaleY(), 1e-9);
  157. assertRoughlyEquals(-4, tx.getTranslateX(), 1e-9);
  158. assertRoughlyEquals(5, tx.getTranslateY(), 1e-9);
  159. }
  160. function testShear() {
  161. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  162. tx.shear(2, 3);
  163. assertEquals(10, tx.getScaleX());
  164. assertEquals(14, tx.getShearY());
  165. assertEquals(5, tx.getShearX());
  166. assertEquals(8, tx.getScaleY());
  167. assertEquals(5, tx.getTranslateX());
  168. assertEquals(6, tx.getTranslateY());
  169. }
  170. function testPreShear() {
  171. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  172. tx.preShear(2, 3);
  173. assertEquals(5, tx.getScaleX());
  174. assertEquals(5, tx.getShearY());
  175. assertEquals(11, tx.getShearX());
  176. assertEquals(13, tx.getScaleY());
  177. assertEquals(17, tx.getTranslateX());
  178. assertEquals(21, tx.getTranslateY());
  179. }
  180. function testConcatentate() {
  181. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  182. tx.concatenate(new goog.math.AffineTransform(2, 1, 6, 5, 4, 3));
  183. assertEquals(5, tx.getScaleX());
  184. assertEquals(8, tx.getShearY());
  185. assertEquals(21, tx.getShearX());
  186. assertEquals(32, tx.getScaleY());
  187. assertEquals(18, tx.getTranslateX());
  188. assertEquals(26, tx.getTranslateY());
  189. }
  190. function testPreConcatentate() {
  191. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  192. tx.preConcatenate(new goog.math.AffineTransform(2, 1, 6, 5, 4, 3));
  193. assertEquals(14, tx.getScaleX());
  194. assertEquals(11, tx.getShearY());
  195. assertEquals(30, tx.getShearX());
  196. assertEquals(23, tx.getScaleY());
  197. assertEquals(50, tx.getTranslateX());
  198. assertEquals(38, tx.getTranslateY());
  199. }
  200. function testAssociativeConcatenate() {
  201. var x =
  202. new goog.math.AffineTransform(2, 3, 5, 7, 11, 13)
  203. .concatenate(new goog.math.AffineTransform(17, 19, 23, 29, 31, 37));
  204. var y =
  205. new goog.math.AffineTransform(17, 19, 23, 29, 31, 37)
  206. .preConcatenate(new goog.math.AffineTransform(2, 3, 5, 7, 11, 13));
  207. assertEquals(x.getScaleX(), y.getScaleX());
  208. assertEquals(x.getShearY(), y.getShearY());
  209. assertEquals(x.getShearX(), y.getShearX());
  210. assertEquals(x.getScaleY(), y.getScaleY());
  211. assertEquals(x.getTranslateX(), y.getTranslateX());
  212. assertEquals(x.getTranslateY(), y.getTranslateY());
  213. }
  214. function testTransform() {
  215. var srcPts = [0, 0, 1, 0, 1, 1, 0, 1];
  216. var dstPts = [];
  217. var tx = goog.math.AffineTransform.getScaleInstance(2, 3);
  218. tx.translate(5, 10);
  219. tx.rotate(Math.PI / 4, 5, 10);
  220. tx.transform(srcPts, 0, dstPts, 0, 4);
  221. assert(
  222. goog.array.equals(
  223. [
  224. 27.071068, 28.180195, 28.485281, 30.301516, 27.071068, 32.422836,
  225. 25.656855, 30.301516
  226. ],
  227. dstPts, goog.math.nearlyEquals));
  228. }
  229. function testGetDeterminant() {
  230. var tx = goog.math.AffineTransform.getScaleInstance(2, 3);
  231. tx.translate(5, 10);
  232. tx.rotate(Math.PI / 4, 5, 10);
  233. assertRoughlyEquals(6, tx.getDeterminant(), 0.001);
  234. }
  235. function testIsInvertible() {
  236. assertTrue(new goog.math.AffineTransform(2, 3, 4, 5, 6, 7).isInvertible());
  237. assertTrue(new goog.math.AffineTransform(1, 0, 0, 1, 0, 0).isInvertible());
  238. assertFalse(new goog.math.AffineTransform(NaN, 0, 0, 1, 0, 0).isInvertible());
  239. assertFalse(new goog.math.AffineTransform(1, NaN, 0, 1, 0, 0).isInvertible());
  240. assertFalse(new goog.math.AffineTransform(1, 0, NaN, 1, 0, 0).isInvertible());
  241. assertFalse(new goog.math.AffineTransform(1, 0, 0, NaN, 0, 0).isInvertible());
  242. assertFalse(new goog.math.AffineTransform(1, 0, 0, 1, NaN, 0).isInvertible());
  243. assertFalse(new goog.math.AffineTransform(1, 0, 0, 1, 0, NaN).isInvertible());
  244. assertFalse(
  245. new goog.math.AffineTransform(Infinity, 0, 0, 1, 0, 0).isInvertible());
  246. assertFalse(
  247. new goog.math.AffineTransform(1, Infinity, 0, 1, 0, 0).isInvertible());
  248. assertFalse(
  249. new goog.math.AffineTransform(1, 0, Infinity, 1, 0, 0).isInvertible());
  250. assertFalse(
  251. new goog.math.AffineTransform(1, 0, 0, Infinity, 0, 0).isInvertible());
  252. assertFalse(
  253. new goog.math.AffineTransform(1, 0, 0, 1, Infinity, 0).isInvertible());
  254. assertFalse(
  255. new goog.math.AffineTransform(1, 0, 0, 1, 0, Infinity).isInvertible());
  256. assertFalse(new goog.math.AffineTransform(0, 0, 0, 0, 1, 0).isInvertible());
  257. }
  258. function testCreateInverse() {
  259. var tx = goog.math.AffineTransform.getScaleInstance(2, 3);
  260. tx.translate(5, 10);
  261. tx.rotate(Math.PI / 4, 5, 10);
  262. var inverse = tx.createInverse();
  263. assert(goog.math.nearlyEquals(0.353553, inverse.getScaleX()));
  264. assert(goog.math.nearlyEquals(-0.353553, inverse.getShearY()));
  265. assert(goog.math.nearlyEquals(0.235702, inverse.getShearX()));
  266. assert(goog.math.nearlyEquals(0.235702, inverse.getScaleY()));
  267. assert(goog.math.nearlyEquals(-16.213203, inverse.getTranslateX()));
  268. assert(goog.math.nearlyEquals(2.928932, inverse.getTranslateY()));
  269. }
  270. function testCopyFrom() {
  271. var from = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  272. var to = new goog.math.AffineTransform();
  273. to.copyFrom(from);
  274. assertEquals(from.getScaleX(), to.getScaleX());
  275. assertEquals(from.getShearY(), to.getShearY());
  276. assertEquals(from.getShearX(), to.getShearX());
  277. assertEquals(from.getScaleY(), to.getScaleY());
  278. assertEquals(from.getTranslateX(), to.getTranslateX());
  279. assertEquals(from.getTranslateY(), to.getTranslateY());
  280. }
  281. function testToString() {
  282. var tx = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  283. assertEquals('matrix(1,2,3,4,5,6)', tx.toString());
  284. }
  285. function testEquals() {
  286. var tx1 = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  287. var tx2 = new goog.math.AffineTransform(1, 2, 3, 4, 5, 6);
  288. assertEqualsMethod(tx1, tx2, true);
  289. tx2 = new goog.math.AffineTransform(-1, 2, 3, 4, 5, 6);
  290. assertEqualsMethod(tx1, tx2, false);
  291. tx2 = new goog.math.AffineTransform(1, -1, 3, 4, 5, 6);
  292. assertEqualsMethod(tx1, tx2, false);
  293. tx2 = new goog.math.AffineTransform(1, 2, -3, 4, 5, 6);
  294. assertEqualsMethod(tx1, tx2, false);
  295. tx2 = new goog.math.AffineTransform(1, 2, 3, -4, 5, 6);
  296. assertEqualsMethod(tx1, tx2, false);
  297. tx2 = new goog.math.AffineTransform(1, 2, 3, 4, -5, 6);
  298. assertEqualsMethod(tx1, tx2, false);
  299. tx2 = new goog.math.AffineTransform(1, 2, 3, 4, 5, -6);
  300. assertEqualsMethod(tx1, tx2, false);
  301. }
  302. function assertEqualsMethod(tx1, tx2, expected) {
  303. assertEquals(expected, tx1.equals(tx2));
  304. assertEquals(expected, tx2.equals(tx1));
  305. assertEquals(true, tx1.equals(tx1));
  306. assertEquals(true, tx2.equals(tx2));
  307. }