vec3_test.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * @license
  3. * Copyright The Closure Library Authors.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.module('goog.vec.Vec3Test');
  7. goog.setTestOnly();
  8. const Vec3 = goog.require('goog.vec.Vec3');
  9. const testSuite = goog.require('goog.testing.testSuite');
  10. testSuite({
  11. testDeprecatedConstructor() {
  12. let v = Vec3.create();
  13. assertElementsEquals(0, v[0]);
  14. assertEquals(0, v[1]);
  15. assertEquals(0, v[2]);
  16. assertElementsEquals([0, 0, 0], Vec3.create());
  17. assertElementsEquals([1, 2, 3], Vec3.createFromValues(1, 2, 3));
  18. assertElementsEquals([1, 2, 3], Vec3.createFromArray([1, 2, 3]));
  19. v = Vec3.createFromValues(1, 2, 3);
  20. assertElementsEquals([1, 2, 3], Vec3.clone(v));
  21. },
  22. testConstructor() {
  23. const v = Vec3.createFloat32();
  24. assertElementsEquals(0, v[0]);
  25. assertEquals(0, v[1]);
  26. assertEquals(0, v[2]);
  27. assertElementsEquals([0, 0, 0], Vec3.createFloat32());
  28. Vec3.setFromValues(v, 1, 2, 3);
  29. assertElementsEquals([1, 2, 3], v);
  30. const w = Vec3.createFloat64();
  31. assertElementsEquals(0, w[0]);
  32. assertEquals(0, w[1]);
  33. assertEquals(0, w[2]);
  34. assertElementsEquals([0, 0, 0], Vec3.createFloat64());
  35. Vec3.setFromValues(w, 1, 2, 3);
  36. assertElementsEquals([1, 2, 3], w);
  37. },
  38. testSet() {
  39. const v = Vec3.createFloat32();
  40. Vec3.setFromValues(v, 1, 2, 3);
  41. assertElementsEquals([1, 2, 3], v);
  42. Vec3.setFromArray(v, [4, 5, 6]);
  43. assertElementsEquals([4, 5, 6], v);
  44. const w = Vec3.createFloat32();
  45. Vec3.setFromValues(w, 1, 2, 3);
  46. assertElementsEquals([1, 2, 3], w);
  47. Vec3.setFromArray(w, [4, 5, 6]);
  48. assertElementsEquals([4, 5, 6], w);
  49. },
  50. testAdd() {
  51. const v0 = Vec3.createFloat32FromArray([1, 2, 3]);
  52. const v1 = Vec3.createFloat32FromArray([4, 5, 6]);
  53. const v2 = Vec3.cloneFloat32(v0);
  54. Vec3.add(v2, v1, v2);
  55. assertElementsEquals([1, 2, 3], v0);
  56. assertElementsEquals([4, 5, 6], v1);
  57. assertElementsEquals([5, 7, 9], v2);
  58. Vec3.add(Vec3.add(v0, v1, v2), v0, v2);
  59. assertElementsEquals([6, 9, 12], v2);
  60. },
  61. testSubtract() {
  62. const v0 = Vec3.createFloat32FromArray([1, 2, 3]);
  63. const v1 = Vec3.createFloat32FromArray([4, 5, 6]);
  64. let v2 = Vec3.cloneFloat32(v0);
  65. Vec3.subtract(v2, v1, v2);
  66. assertElementsEquals([1, 2, 3], v0);
  67. assertElementsEquals([4, 5, 6], v1);
  68. assertElementsEquals([-3, -3, -3], v2);
  69. Vec3.setFromValues(v2, 0, 0, 0);
  70. Vec3.subtract(v1, v0, v2);
  71. assertElementsEquals([3, 3, 3], v2);
  72. v2 = Vec3.cloneFloat32(v0);
  73. Vec3.subtract(v2, v1, v2);
  74. assertElementsEquals([-3, -3, -3], v2);
  75. Vec3.subtract(Vec3.subtract(v1, v0, v2), v0, v2);
  76. assertElementsEquals([2, 1, 0], v2);
  77. },
  78. testNegate() {
  79. const v0 = Vec3.createFloat32FromArray([1, 2, 3]);
  80. const v1 = Vec3.createFloat32();
  81. Vec3.negate(v0, v1);
  82. assertElementsEquals([-1, -2, -3], v1);
  83. assertElementsEquals([1, 2, 3], v0);
  84. Vec3.negate(v0, v0);
  85. assertElementsEquals([-1, -2, -3], v0);
  86. },
  87. testAbs() {
  88. const v0 = Vec3.createFloat32FromValues(-1, -2, -3);
  89. const v1 = Vec3.createFloat32();
  90. Vec3.abs(v0, v1);
  91. assertElementsEquals([1, 2, 3], v1);
  92. assertElementsEquals([-1, -2, -3], v0);
  93. Vec3.abs(v0, v0);
  94. assertElementsEquals([1, 2, 3], v0);
  95. },
  96. testScale() {
  97. const v0 = Vec3.createFloat32FromArray([1, 2, 3]);
  98. const v1 = Vec3.createFloat32();
  99. Vec3.scale(v0, 4, v1);
  100. assertElementsEquals([4, 8, 12], v1);
  101. assertElementsEquals([1, 2, 3], v0);
  102. Vec3.setFromArray(v1, v0);
  103. Vec3.scale(v1, 5, v1);
  104. assertElementsEquals([5, 10, 15], v1);
  105. },
  106. testMagnitudeSquared() {
  107. const v0 = Vec3.createFloat32FromArray([1, 2, 3]);
  108. assertEquals(14, Vec3.magnitudeSquared(v0));
  109. },
  110. testMagnitude() {
  111. const v0 = Vec3.createFloat32FromArray([1, 2, 3]);
  112. assertEquals(Math.sqrt(14), Vec3.magnitude(v0));
  113. },
  114. testNormalize() {
  115. const v0 = Vec3.createFloat32FromArray([2, 3, 4]);
  116. const v1 = Vec3.create();
  117. const v2 = Vec3.create();
  118. Vec3.scale(v0, 1 / Vec3.magnitude(v0), v2);
  119. Vec3.normalize(v0, v1);
  120. assertElementsEquals(v2, v1);
  121. assertElementsEquals([2, 3, 4], v0);
  122. Vec3.setFromArray(v1, v0);
  123. Vec3.normalize(v1, v1);
  124. assertElementsEquals(v2, v1);
  125. },
  126. testDot() {
  127. const v0 = Vec3.createFloat32FromArray([1, 2, 3]);
  128. const v1 = Vec3.createFloat32FromArray([4, 5, 6]);
  129. assertEquals(32, Vec3.dot(v0, v1));
  130. assertEquals(32, Vec3.dot(v1, v0));
  131. },
  132. testCross() {
  133. const v0 = Vec3.createFloat32FromArray([1, 2, 3]);
  134. const v1 = Vec3.createFloat32FromArray([4, 5, 6]);
  135. const crossVec = Vec3.create();
  136. Vec3.cross(v0, v1, crossVec);
  137. assertElementsEquals([1, 2, 3], v0);
  138. assertElementsEquals([4, 5, 6], v1);
  139. assertElementsEquals([-3, 6, -3], crossVec);
  140. Vec3.setFromArray(crossVec, v1);
  141. Vec3.cross(crossVec, v0, crossVec);
  142. assertElementsEquals([1, 2, 3], v0);
  143. assertElementsEquals([4, 5, 6], v1);
  144. assertElementsEquals([3, -6, 3], crossVec);
  145. Vec3.cross(v0, v0, v0);
  146. assertElementsEquals([0, 0, 0], v0);
  147. },
  148. testDistanceSquared() {
  149. const v0 = Vec3.createFloat32FromValues(1, 2, 3);
  150. const v1 = Vec3.createFloat32FromValues(1, 2, 3);
  151. assertEquals(0, Vec3.distanceSquared(v0, v1));
  152. Vec3.setFromValues(v0, 1, 2, 3);
  153. Vec3.setFromValues(v1, -1, -2, -1);
  154. assertEquals(36, Vec3.distanceSquared(v0, v1));
  155. },
  156. testDistance() {
  157. const v0 = Vec3.createFloat32FromValues(1, 2, 3);
  158. const v1 = Vec3.createFloat32FromValues(1, 2, 3);
  159. assertEquals(0, Vec3.distance(v0, v1));
  160. Vec3.setFromValues(v0, 1, 2, 3);
  161. Vec3.setFromValues(v1, -1, -2, -1);
  162. assertEquals(6, Vec3.distance(v0, v1));
  163. },
  164. testDirection() {
  165. const v0 = Vec3.createFloat32FromValues(1, 2, 3);
  166. const v1 = Vec3.createFloat32FromValues(1, 2, 3);
  167. const dirVec = Vec3.createFloat32FromValues(4, 5, 6);
  168. Vec3.direction(v0, v1, dirVec);
  169. assertElementsEquals([0, 0, 0], dirVec);
  170. Vec3.setFromValues(v0, 0, 0, 0);
  171. Vec3.setFromValues(v1, 1, 0, 0);
  172. Vec3.direction(v0, v1, dirVec);
  173. assertElementsEquals([1, 0, 0], dirVec);
  174. Vec3.setFromValues(v0, 1, 1, 1);
  175. Vec3.setFromValues(v1, 0, 0, 0);
  176. Vec3.direction(v0, v1, dirVec);
  177. assertElementsRoughlyEqual(
  178. [-0.5773502588272095, -0.5773502588272095, -0.5773502588272095], dirVec,
  179. goog.vec.EPSILON);
  180. },
  181. testLerp() {
  182. const v0 = Vec3.createFloat32FromValues(1, 2, 3);
  183. const v1 = Vec3.createFloat32FromValues(10, 20, 30);
  184. const v2 = Vec3.cloneFloat32(v0);
  185. Vec3.lerp(v2, v1, 0, v2);
  186. assertElementsEquals([1, 2, 3], v2);
  187. Vec3.lerp(v2, v1, 1, v2);
  188. assertElementsEquals([10, 20, 30], v2);
  189. Vec3.lerp(v0, v1, .5, v2);
  190. assertElementsEquals([5.5, 11, 16.5], v2);
  191. },
  192. testMax() {
  193. const v0 = Vec3.createFloat32FromValues(10, 20, 30);
  194. const v1 = Vec3.createFloat32FromValues(5, 25, 35);
  195. const v2 = Vec3.createFloat32();
  196. Vec3.max(v0, v1, v2);
  197. assertElementsEquals([10, 25, 35], v2);
  198. Vec3.max(v1, v0, v1);
  199. assertElementsEquals([10, 25, 35], v1);
  200. Vec3.max(v2, 20, v2);
  201. assertElementsEquals([20, 25, 35], v2);
  202. },
  203. testMin() {
  204. const v0 = Vec3.createFloat32FromValues(10, 20, 30);
  205. const v1 = Vec3.createFloat32FromValues(5, 25, 35);
  206. const v2 = Vec3.createFloat32();
  207. Vec3.min(v0, v1, v2);
  208. assertElementsEquals([5, 20, 30], v2);
  209. Vec3.min(v1, v0, v1);
  210. assertElementsEquals([5, 20, 30], v1);
  211. Vec3.min(v2, 20, v2);
  212. assertElementsEquals([5, 20, 20], v2);
  213. },
  214. testEquals() {
  215. const v0 = Vec3.createFloat32FromValues(1, 2, 3);
  216. let v1 = Vec3.cloneFloat32(v0);
  217. assertElementsEquals(v0, v1);
  218. v1[0] = 4;
  219. assertFalse(Vec3.equals(v0, v1));
  220. v1 = Vec3.cloneFloat32(v0);
  221. v1[1] = 4;
  222. assertFalse(Vec3.equals(v0, v1));
  223. v1 = Vec3.cloneFloat32(v0);
  224. v1[2] = 4;
  225. assertFalse(Vec3.equals(v0, v1));
  226. },
  227. });