vec3f_test.js 14 KB

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