vec3d_test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. // Use of this source code is governed by the Apache License, Version 2.0.
  3. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  4. // //
  5. // Any edits to this file must be applied to vec3f_test.js by running: //
  6. // swap_type.sh vec3d_test.js > vec3f_test.js //
  7. // //
  8. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  9. goog.provide('goog.vec.vec3dTest');
  10. goog.setTestOnly('goog.vec.vec3dTest');
  11. goog.require('goog.testing.jsunit');
  12. goog.require('goog.vec.vec3d');
  13. function testCreate() {
  14. var v = goog.vec.vec3d.create();
  15. assertElementsEquals([0, 0, 0], v);
  16. }
  17. function testCreateFromArray() {
  18. var v = goog.vec.vec3d.createFromArray([1, 2, 3]);
  19. assertElementsEquals([1, 2, 3], v);
  20. }
  21. function testCreateFromValues() {
  22. var v = goog.vec.vec3d.createFromValues(1, 2, 3);
  23. assertElementsEquals([1, 2, 3], v);
  24. }
  25. function testClone() {
  26. var v0 = goog.vec.vec3d.createFromValues(1, 2, 3);
  27. var v1 = goog.vec.vec3d.clone(v0);
  28. assertElementsEquals([1, 2, 3], v1);
  29. }
  30. function testSet() {
  31. var v = goog.vec.vec3d.create();
  32. goog.vec.vec3d.setFromValues(v, 1, 2, 3);
  33. assertElementsEquals([1, 2, 3], v);
  34. goog.vec.vec3d.setFromArray(v, [4, 5, 6]);
  35. assertElementsEquals([4, 5, 6], v);
  36. var w = goog.vec.vec3d.create();
  37. goog.vec.vec3d.setFromValues(w, 1, 2, 3);
  38. assertElementsEquals([1, 2, 3], w);
  39. goog.vec.vec3d.setFromArray(w, [4, 5, 6]);
  40. assertElementsEquals([4, 5, 6], w);
  41. }
  42. function testAdd() {
  43. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
  44. var v1 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [4, 5, 6]);
  45. var v2 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
  46. goog.vec.vec3d.add(v2, v1, v2);
  47. assertElementsEquals([1, 2, 3], v0);
  48. assertElementsEquals([4, 5, 6], v1);
  49. assertElementsEquals([5, 7, 9], v2);
  50. goog.vec.vec3d.add(goog.vec.vec3d.add(v0, v1, v2), v0, v2);
  51. assertElementsEquals([6, 9, 12], v2);
  52. }
  53. function testSubtract() {
  54. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
  55. var v1 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [4, 5, 6]);
  56. var v2 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
  57. goog.vec.vec3d.subtract(v2, v1, v2);
  58. assertElementsEquals([1, 2, 3], v0);
  59. assertElementsEquals([4, 5, 6], v1);
  60. assertElementsEquals([-3, -3, -3], v2);
  61. goog.vec.vec3d.setFromValues(v2, 0, 0, 0);
  62. goog.vec.vec3d.subtract(v1, v0, v2);
  63. assertElementsEquals([3, 3, 3], v2);
  64. v2 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
  65. goog.vec.vec3d.subtract(v2, v1, v2);
  66. assertElementsEquals([-3, -3, -3], v2);
  67. goog.vec.vec3d.subtract(goog.vec.vec3d.subtract(v1, v0, v2), v0, v2);
  68. assertElementsEquals([2, 1, 0], v2);
  69. }
  70. function testNegate() {
  71. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
  72. var v1 = goog.vec.vec3d.create();
  73. goog.vec.vec3d.negate(v0, v1);
  74. assertElementsEquals([-1, -2, -3], v1);
  75. assertElementsEquals([1, 2, 3], v0);
  76. goog.vec.vec3d.negate(v0, v0);
  77. assertElementsEquals([-1, -2, -3], v0);
  78. }
  79. function testAbs() {
  80. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [-1, -2, -3]);
  81. var v1 = goog.vec.vec3d.create();
  82. goog.vec.vec3d.abs(v0, v1);
  83. assertElementsEquals([1, 2, 3], v1);
  84. assertElementsEquals([-1, -2, -3], v0);
  85. goog.vec.vec3d.abs(v0, v0);
  86. assertElementsEquals([1, 2, 3], v0);
  87. }
  88. function testScale() {
  89. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
  90. var v1 = goog.vec.vec3d.create();
  91. goog.vec.vec3d.scale(v0, 4, v1);
  92. assertElementsEquals([4, 8, 12], v1);
  93. assertElementsEquals([1, 2, 3], v0);
  94. goog.vec.vec3d.setFromArray(v1, v0);
  95. goog.vec.vec3d.scale(v1, 5, v1);
  96. assertElementsEquals([5, 10, 15], v1);
  97. }
  98. function testMagnitudeSquared() {
  99. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
  100. assertEquals(14, goog.vec.vec3d.magnitudeSquared(v0));
  101. }
  102. function testMagnitude() {
  103. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
  104. assertEquals(Math.sqrt(14), goog.vec.vec3d.magnitude(v0));
  105. }
  106. function testNormalize() {
  107. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [2, 3, 4]);
  108. var v1 = goog.vec.vec3d.create();
  109. var v2 = goog.vec.vec3d.create();
  110. goog.vec.vec3d.scale(
  111. v0, 1 / goog.vec.vec3d.magnitude(v0), v2);
  112. goog.vec.vec3d.normalize(v0, v1);
  113. assertElementsEquals(v2, v1);
  114. assertElementsEquals([2, 3, 4], v0);
  115. goog.vec.vec3d.setFromArray(v1, v0);
  116. goog.vec.vec3d.normalize(v1, v1);
  117. assertElementsEquals(v2, v1);
  118. }
  119. function testDot() {
  120. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
  121. var v1 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [4, 5, 6]);
  122. assertEquals(32, goog.vec.vec3d.dot(v0, v1));
  123. assertEquals(32, goog.vec.vec3d.dot(v1, v0));
  124. }
  125. function testCross() {
  126. var v0 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [1, 2, 3]);
  127. var v1 = goog.vec.vec3d.setFromArray(goog.vec.vec3d.create(), [4, 5, 6]);
  128. var crossVec = goog.vec.vec3d.create();
  129. goog.vec.vec3d.cross(v0, v1, crossVec);
  130. assertElementsEquals([1, 2, 3], v0);
  131. assertElementsEquals([4, 5, 6], v1);
  132. assertElementsEquals([-3, 6, -3], crossVec);
  133. goog.vec.vec3d.setFromArray(crossVec, v1);
  134. goog.vec.vec3d.cross(crossVec, v0, crossVec);
  135. assertElementsEquals([1, 2, 3], v0);
  136. assertElementsEquals([4, 5, 6], v1);
  137. assertElementsEquals([3, -6, 3], crossVec);
  138. goog.vec.vec3d.cross(v0, v0, v0);
  139. assertElementsEquals([0, 0, 0], v0);
  140. }
  141. function testDistanceSquared() {
  142. var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
  143. var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
  144. assertEquals(0, goog.vec.vec3d.distanceSquared(v0, v1));
  145. goog.vec.vec3d.setFromValues(v0, 1, 2, 3);
  146. goog.vec.vec3d.setFromValues(v1, -1, -2, -1);
  147. assertEquals(36, goog.vec.vec3d.distanceSquared(v0, v1));
  148. }
  149. function testDistance() {
  150. var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
  151. var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
  152. assertEquals(0, goog.vec.vec3d.distance(v0, v1));
  153. goog.vec.vec3d.setFromValues(v0, 1, 2, 3);
  154. goog.vec.vec3d.setFromValues(v1, -1, -2, -1);
  155. assertEquals(6, goog.vec.vec3d.distance(v0, v1));
  156. }
  157. function testDirection() {
  158. var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
  159. var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
  160. var dirVec = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 4, 5, 6);
  161. goog.vec.vec3d.direction(v0, v1, dirVec);
  162. assertElementsEquals([0, 0, 0], dirVec);
  163. goog.vec.vec3d.setFromValues(v0, 0, 0, 0);
  164. goog.vec.vec3d.setFromValues(v1, 1, 0, 0);
  165. goog.vec.vec3d.direction(v0, v1, dirVec);
  166. assertElementsEquals([1, 0, 0], dirVec);
  167. goog.vec.vec3d.setFromValues(v0, 1, 1, 1);
  168. goog.vec.vec3d.setFromValues(v1, 0, 0, 0);
  169. goog.vec.vec3d.direction(v0, v1, dirVec);
  170. assertElementsRoughlyEqual(
  171. [-0.5773502588272095, -0.5773502588272095, -0.5773502588272095],
  172. dirVec, goog.vec.EPSILON);
  173. }
  174. function testLerp() {
  175. var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
  176. var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 10, 20, 30);
  177. var v2 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
  178. goog.vec.vec3d.lerp(v2, v1, 0, v2);
  179. assertElementsEquals([1, 2, 3], v2);
  180. goog.vec.vec3d.lerp(v2, v1, 1, v2);
  181. assertElementsEquals([10, 20, 30], v2);
  182. goog.vec.vec3d.lerp(v0, v1, .5, v2);
  183. assertElementsEquals([5.5, 11, 16.5], v2);
  184. }
  185. function testSlerp() {
  186. var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 0, 0, 1);
  187. var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 0, 0);
  188. var v2 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), -1, 0, 0);
  189. var v3 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), -5, 0, 0);
  190. var v4 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 0, 0, -1);
  191. var v5 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
  192. // Try f == 0 and f == 1.
  193. goog.vec.vec3d.slerp(v5, v1, 0, v5);
  194. assertElementsEquals([0, 0, 1], v5);
  195. goog.vec.vec3d.slerp(v5, v1, 1, v5);
  196. assertElementsEquals([1, 0, 0], v5);
  197. // Try slerp between perpendicular vectors.
  198. goog.vec.vec3d.slerp(v0, v1, .5, v5);
  199. assertElementsRoughlyEqual(
  200. [ Math.sqrt(2) / 2, 0, Math.sqrt(2) / 2 ], v5,
  201. goog.vec.EPSILON);
  202. // Try slerp between vectors of opposite directions (+Z and -Z).
  203. v5 = goog.vec.vec3d.slerp(v0, v4, .5, v5);
  204. // Axis of rotation is arbitrary, but result should be 90 degrees from both
  205. // v0 and v4 when f = 0.5.
  206. assertRoughlyEquals(Math.PI / 2, Math.acos(goog.vec.vec3d.dot(v5, v0)),
  207. goog.vec.EPSILON);
  208. assertRoughlyEquals(Math.PI / 2, Math.acos(goog.vec.vec3d.dot(v5, v4)),
  209. goog.vec.EPSILON);
  210. // f == 0.25, result should be 45-degrees to v0, and 135 to v4.
  211. v5 = goog.vec.vec3d.slerp(v0, v4, .25, v5);
  212. assertRoughlyEquals(Math.PI / 4, Math.acos(goog.vec.vec3d.dot(v5, v0)),
  213. goog.vec.EPSILON);
  214. assertRoughlyEquals(Math.PI * 3 / 4, Math.acos(goog.vec.vec3d.dot(v5, v4)),
  215. goog.vec.EPSILON);
  216. // f = 0.75, result should be 135-degrees to v0, and 45 to v4.
  217. v5 = goog.vec.vec3d.slerp(v0, v4, .75, v5);
  218. assertRoughlyEquals(Math.PI * 3 / 4, Math.acos(goog.vec.vec3d.dot(v5, v0)),
  219. goog.vec.EPSILON);
  220. assertRoughlyEquals(Math.PI / 4, Math.acos(goog.vec.vec3d.dot(v5, v4)),
  221. goog.vec.EPSILON);
  222. // Same as above, but on opposite directions of the X-axis.
  223. v5 = goog.vec.vec3d.slerp(v1, v2, .5, v5);
  224. // Axis of rotation is arbitrary, but result should be 90 degrees from both
  225. // v1 and v2 when f = 0.5.
  226. assertRoughlyEquals(Math.PI / 2, Math.acos(goog.vec.vec3d.dot(v5, v1)),
  227. goog.vec.EPSILON);
  228. assertRoughlyEquals(Math.PI / 2, Math.acos(goog.vec.vec3d.dot(v5, v2)),
  229. goog.vec.EPSILON);
  230. // f == 0.25, result should be 45-degrees to v1, and 135 to v2.
  231. v5 = goog.vec.vec3d.slerp(v1, v2, .25, v5);
  232. assertRoughlyEquals(Math.PI / 4, Math.acos(goog.vec.vec3d.dot(v5, v1)),
  233. goog.vec.EPSILON);
  234. assertRoughlyEquals(Math.PI * 3 / 4, Math.acos(goog.vec.vec3d.dot(v5, v2)),
  235. goog.vec.EPSILON);
  236. // f = 0.75, result should be 135-degrees to v1, and 45 to v2.
  237. v5 = goog.vec.vec3d.slerp(v1, v2, .75, v5);
  238. assertRoughlyEquals(Math.PI * 3 / 4, Math.acos(goog.vec.vec3d.dot(v5, v1)),
  239. goog.vec.EPSILON);
  240. assertRoughlyEquals(Math.PI / 4, Math.acos(goog.vec.vec3d.dot(v5, v2)),
  241. goog.vec.EPSILON);
  242. // Try vectors that aren't perpendicular or opposite/same direction.
  243. var v6 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(),
  244. Math.sqrt(2) / 2, Math.sqrt(2) / 2, 0);
  245. goog.vec.vec3d.slerp(v1, v6, .9, v5);
  246. // The vectors are 45 degrees apart, for f == 0.9, results should be 1/10 of
  247. // that from v6 and 9/10 of that away from v1.
  248. assertRoughlyEquals((Math.PI / 4) * 0.9, Math.acos(goog.vec.vec3d.dot(v1, v5)),
  249. goog.vec.EPSILON);
  250. assertRoughlyEquals((Math.PI / 4) * 0.1, Math.acos(goog.vec.vec3d.dot(v6, v5)),
  251. goog.vec.EPSILON);
  252. // Between vectors of the same direction, where one is non-unit-length
  253. // (magnitudes should be lerp-ed).
  254. goog.vec.vec3d.slerp(v2, v3, .5, v5);
  255. assertElementsEquals([-3, 0, 0], v5);
  256. // Between perpendicular vectors, where one is non-unit length.
  257. goog.vec.vec3d.slerp(v0, v3, .5, v5);
  258. assertRoughlyEquals(3, goog.vec.vec3d.magnitude(v5), goog.vec.EPSILON);
  259. assertElementsRoughlyEqual(
  260. [ -3 * (Math.sqrt(2) / 2), 0, 3 * (Math.sqrt(2) / 2) ], v5,
  261. goog.vec.EPSILON);
  262. // And vectors of opposite directions, where one is non-unit length.
  263. goog.vec.vec3d.slerp(v1, v3, .5, v5);
  264. // Axis of rotation is arbitrary, but result should be 90 degrees from both
  265. // v1 and v3.
  266. assertRoughlyEquals(
  267. Math.PI / 2,
  268. Math.acos(goog.vec.vec3d.dot(v5, v1) / (goog.vec.vec3d.magnitude(v5) *
  269. goog.vec.vec3d.magnitude(v1))),
  270. goog.vec.EPSILON);
  271. assertRoughlyEquals(
  272. Math.PI / 2,
  273. Math.acos(goog.vec.vec3d.dot(v5, v3) / (goog.vec.vec3d.magnitude(v3) *
  274. goog.vec.vec3d.magnitude(v5))),
  275. goog.vec.EPSILON);
  276. // Magnitude should be linearly interpolated.
  277. assertRoughlyEquals(3, goog.vec.vec3d.magnitude(v5), goog.vec.EPSILON);
  278. // Try a case where the vectors are the same direction (the same vector in
  279. // this case), but where numerical error results in a dot product
  280. // slightly greater than 1. Taking the acos of this would result in NaN.
  281. var v7 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 0.009, 0.147,
  282. 0.989);
  283. goog.vec.vec3d.slerp(v7, v7, .25, v5);
  284. assertElementsRoughlyEqual([ v7[0], v7[1], v7[2] ], v5, goog.vec.EPSILON);
  285. }
  286. function testMax() {
  287. var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 10, 20, 30);
  288. var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 5, 25, 35);
  289. var v2 = goog.vec.vec3d.create();
  290. goog.vec.vec3d.max(v0, v1, v2);
  291. assertElementsEquals([10, 25, 35], v2);
  292. goog.vec.vec3d.max(v1, v0, v1);
  293. assertElementsEquals([10, 25, 35], v1);
  294. goog.vec.vec3d.max(v2, 20, v2);
  295. assertElementsEquals([20, 25, 35], v2);
  296. }
  297. function testMin() {
  298. var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 10, 20, 30);
  299. var v1 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 5, 25, 35);
  300. var v2 = goog.vec.vec3d.create();
  301. goog.vec.vec3d.min(v0, v1, v2);
  302. assertElementsEquals([5, 20, 30], v2);
  303. goog.vec.vec3d.min(v1, v0, v1);
  304. assertElementsEquals([5, 20, 30], v1);
  305. goog.vec.vec3d.min(v2, 20, v2);
  306. assertElementsEquals([5, 20, 20], v2);
  307. }
  308. function testEquals() {
  309. var v0 = goog.vec.vec3d.setFromValues(goog.vec.vec3d.create(), 1, 2, 3);
  310. var v1 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
  311. assertElementsEquals(v0, v1);
  312. v1[0] = 4;
  313. assertFalse(goog.vec.vec3d.equals(v0, v1));
  314. v1 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
  315. v1[1] = 4;
  316. assertFalse(goog.vec.vec3d.equals(v0, v1));
  317. v1 = goog.vec.vec3d.setFromVec3d(goog.vec.vec3d.create(), v0);
  318. v1[2] = 4;
  319. assertFalse(goog.vec.vec3d.equals(v0, v1));
  320. }