vec3_test.js 8.5 KB

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