vec3f_test.js 13 KB

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