vec3-spec.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. import * as mat3 from "../../src/gl-matrix/mat3"
  2. import * as mat4 from "../../src/gl-matrix/mat4"
  3. import * as vec3 from "../../src/gl-matrix/vec3"
  4. describe("vec3", function() {
  5. let out, vecA, vecB, result;
  6. beforeEach(function() { vecA = [1, 2, 3]; vecB = [4, 5, 6]; out = [0, 0, 0]; });
  7. describe('rotateX', function(){
  8. describe('rotation around world origin [0, 0, 0]', function(){
  9. beforeEach(function(){ vecA = [0, 1, 0]; vecB = [0, 0, 0]; result = vec3.rotateX(out, vecA, vecB, Math.PI); });
  10. it("should return the rotated vector", function(){ expect(result).toBeEqualish([0, -1, 0]); });
  11. });
  12. describe('rotation around an arbitrary origin', function(){
  13. beforeEach(function(){ vecA = [2, 7, 0]; vecB = [2, 5, 0]; result = vec3.rotateX(out, vecA, vecB, Math.PI); });
  14. it("should return the rotated vector", function(){ expect(result).toBeEqualish([2, 3, 0]); });
  15. });
  16. });
  17. describe('rotateY', function(){
  18. describe('rotation around world origin [0, 0, 0]', function(){
  19. beforeEach(function(){ vecA = [1, 0, 0]; vecB = [0, 0, 0]; result = vec3.rotateY(out, vecA, vecB, Math.PI); });
  20. it("should return the rotated vector", function(){ expect(result).toBeEqualish([-1, 0, 0]); });
  21. });
  22. describe('rotation around an arbitrary origin', function(){
  23. beforeEach(function(){ vecA = [-2, 3, 10]; vecB = [-4, 3, 10]; result = vec3.rotateY(out, vecA, vecB, Math.PI); });
  24. it("should return the rotated vector", function(){ expect(result).toBeEqualish([-6, 3, 10]); });
  25. });
  26. });
  27. describe('rotateZ', function(){
  28. describe('rotation around world origin [0, 0, 0]', function(){
  29. beforeEach(function(){ vecA = [0, 1, 0]; vecB = [0, 0, 0]; result = vec3.rotateZ(out, vecA, vecB, Math.PI); });
  30. it("should return the rotated vector", function(){ expect(result).toBeEqualish([0, -1, 0]); });
  31. });
  32. describe('rotation around an arbitrary origin', function(){
  33. beforeEach(function(){ vecA = [0, 6, -5]; vecB = [0, 0, -5]; result = vec3.rotateZ(out, vecA, vecB, Math.PI); });
  34. it("should return the rotated vector", function(){ expect(result).toBeEqualish([0, -6, -5]); });
  35. });
  36. });
  37. describe('transformMat4', function() {
  38. let matr;
  39. describe("with an identity", function() {
  40. beforeEach(function() { matr = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] });
  41. beforeEach(function() { result = vec3.transformMat4(out, vecA, matr); });
  42. it("should produce the input", function() {
  43. expect(out).toBeEqualish([1, 2, 3]);
  44. });
  45. it("should return out", function() { expect(result).toBe(out); });
  46. });
  47. describe("with a lookAt", function() {
  48. beforeEach(function() { matr = mat4.lookAt(mat4.create(), [5, 6, 7], [2, 6, 7], [0, 1, 0]); });
  49. beforeEach(function() { result = vec3.transformMat4(out, vecA, matr); });
  50. it("should rotate and translate the input", function() {
  51. expect(out).toBeEqualish([ 4, -4, -4 ]);
  52. });
  53. it("should return out", function() { expect(result).toBe(out); });
  54. });
  55. describe("with a perspective matrix (#92)", function() {
  56. it("should transform a point from perspective(pi/2, 4/3, 1, 100)", function() {
  57. matr = [0.750, 0, 0, 0,
  58. 0, 1, 0, 0,
  59. 0, 0, -1.02, -1,
  60. 0, 0, -2.02, 0];
  61. result = vec3.transformMat4([], [10, 20, 30], matr);
  62. expect(result).toBeEqualish([-0.25, -0.666666, 1.087333]);
  63. });
  64. });
  65. });
  66. describe('transformMat3', function() {
  67. let matr;
  68. describe("with an identity", function() {
  69. beforeEach(function() { matr = [1, 0, 0, 0, 1, 0, 0, 0, 1 ] });
  70. beforeEach(function() { result = vec3.transformMat3(out, vecA, matr); });
  71. it("should produce the input", function() {
  72. expect(out).toBeEqualish([1, 2, 3]);
  73. });
  74. it("should return out", function() { expect(result).toBe(out); });
  75. });
  76. describe("with 90deg about X", function() {
  77. beforeEach(function() {
  78. result = vec3.transformMat3(out, [0,1,0], [1,0,0,0,0,1,0,-1,0]);
  79. });
  80. it("should produce correct output", function() {
  81. expect(out).toBeEqualish([0,0,1]);
  82. });
  83. });
  84. describe("with 90deg about Y", function() {
  85. beforeEach(function() {
  86. result = vec3.transformMat3(out, [1,0,0], [0,0,-1,0,1,0,1,0,0]);
  87. });
  88. it("should produce correct output", function() {
  89. expect(out).toBeEqualish([0,0,-1]);
  90. });
  91. });
  92. describe("with 90deg about Z", function() {
  93. beforeEach(function() {
  94. result = vec3.transformMat3(out, [1,0,0], [0,1,0,-1,0,0,0,0,1]);
  95. });
  96. it("should produce correct output", function() {
  97. expect(out).toBeEqualish([0,1,0]);
  98. });
  99. });
  100. describe("with a lookAt normal matrix", function() {
  101. beforeEach(function() {
  102. matr = mat4.lookAt(mat4.create(), [5, 6, 7], [2, 6, 7], [0, 1, 0]);
  103. let n = mat3.create();
  104. matr = mat3.transpose(n, mat3.invert(n, mat3.fromMat4(n, matr)));
  105. });
  106. beforeEach(function() { result = vec3.transformMat3(out, [1,0,0], matr); });
  107. it("should rotate the input", function() {
  108. expect(out).toBeEqualish([ 0,0,1 ]);
  109. });
  110. it("should return out", function() { expect(result).toBe(out); });
  111. });
  112. });
  113. describe("transformQuat", function() {
  114. beforeEach(function() { result = vec3.transformQuat(out, vecA, [0.18257418567011074, 0.3651483713402215, 0.5477225570103322, 0.730296742680443]); });
  115. it("should rotate the input vector", function() { expect(out).toBeEqualish([1, 2, 3]); });
  116. it("should return out", function() { expect(result).not.toBe([1,2,3,4]); });
  117. });
  118. describe("create", function() {
  119. beforeEach(function() { result = vec3.create(); });
  120. it("should return a 3 element array initialized to 0s", function() { expect(result).toBeEqualish([0, 0, 0]); });
  121. });
  122. describe("clone", function() {
  123. beforeEach(function() { result = vec3.clone(vecA); });
  124. it("should return a 3 element array initialized to the values in vecA", function() { expect(result).toBeEqualish(vecA); });
  125. });
  126. describe("fromValues", function() {
  127. beforeEach(function() { result = vec3.fromValues(1, 2, 3); });
  128. it("should return a 3 element array initialized to the values passed", function() { expect(result).toBeEqualish([1, 2, 3]); });
  129. });
  130. describe("copy", function() {
  131. beforeEach(function() { result = vec3.copy(out, vecA); });
  132. it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3]); });
  133. it("should return out", function() { expect(result).toBe(out); });
  134. });
  135. describe("set", function() {
  136. beforeEach(function() { result = vec3.set(out, 1, 2, 3); });
  137. it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3]); });
  138. it("should return out", function() { expect(result).toBe(out); });
  139. });
  140. describe("add", function() {
  141. describe("with a separate output vector", function() {
  142. beforeEach(function() { result = vec3.add(out, vecA, vecB); });
  143. it("should place values into out", function() { expect(out).toBeEqualish([5, 7, 9]); });
  144. it("should return out", function() { expect(result).toBe(out); });
  145. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  146. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  147. });
  148. describe("when vecA is the output vector", function() {
  149. beforeEach(function() { result = vec3.add(vecA, vecA, vecB); });
  150. it("should place values into vecA", function() { expect(vecA).toBeEqualish([5, 7, 9]); });
  151. it("should return vecA", function() { expect(result).toBe(vecA); });
  152. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  153. });
  154. describe("when vecB is the output vector", function() {
  155. beforeEach(function() { result = vec3.add(vecB, vecA, vecB); });
  156. it("should place values into vecB", function() { expect(vecB).toBeEqualish([5, 7, 9]); });
  157. it("should return vecB", function() { expect(result).toBe(vecB); });
  158. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  159. });
  160. });
  161. describe("subtract", function() {
  162. it("should have an alias called 'sub'", function() { expect(vec3.sub).toEqual(vec3.subtract); });
  163. describe("with a separate output vector", function() {
  164. beforeEach(function() { result = vec3.subtract(out, vecA, vecB); });
  165. it("should place values into out", function() { expect(out).toBeEqualish([-3, -3, -3]); });
  166. it("should return out", function() { expect(result).toBe(out); });
  167. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  168. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  169. });
  170. describe("when vecA is the output vector", function() {
  171. beforeEach(function() { result = vec3.subtract(vecA, vecA, vecB); });
  172. it("should place values into vecA", function() { expect(vecA).toBeEqualish([-3, -3, -3]); });
  173. it("should return vecA", function() { expect(result).toBe(vecA); });
  174. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  175. });
  176. describe("when vecB is the output vector", function() {
  177. beforeEach(function() { result = vec3.subtract(vecB, vecA, vecB); });
  178. it("should place values into vecB", function() { expect(vecB).toBeEqualish([-3, -3, -3]); });
  179. it("should return vecB", function() { expect(result).toBe(vecB); });
  180. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  181. });
  182. });
  183. describe("multiply", function() {
  184. it("should have an alias called 'mul'", function() { expect(vec3.mul).toEqual(vec3.multiply); });
  185. describe("with a separate output vector", function() {
  186. beforeEach(function() { result = vec3.multiply(out, vecA, vecB); });
  187. it("should place values into out", function() { expect(out).toBeEqualish([4, 10, 18]); });
  188. it("should return out", function() { expect(result).toBe(out); });
  189. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  190. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  191. });
  192. describe("when vecA is the output vector", function() {
  193. beforeEach(function() { result = vec3.multiply(vecA, vecA, vecB); });
  194. it("should place values into vecA", function() { expect(vecA).toBeEqualish([4, 10, 18]); });
  195. it("should return vecA", function() { expect(result).toBe(vecA); });
  196. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  197. });
  198. describe("when vecB is the output vector", function() {
  199. beforeEach(function() { result = vec3.multiply(vecB, vecA, vecB); });
  200. it("should place values into vecB", function() { expect(vecB).toBeEqualish([4, 10, 18]); });
  201. it("should return vecB", function() { expect(result).toBe(vecB); });
  202. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  203. });
  204. });
  205. describe("divide", function() {
  206. it("should have an alias called 'div'", function() { expect(vec3.div).toEqual(vec3.divide); });
  207. describe("with a separate output vector", function() {
  208. beforeEach(function() { result = vec3.divide(out, vecA, vecB); });
  209. it("should place values into out", function() { expect(out).toBeEqualish([0.25, 0.4, 0.5]); });
  210. it("should return out", function() { expect(result).toBe(out); });
  211. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  212. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  213. });
  214. describe("when vecA is the output vector", function() {
  215. beforeEach(function() { result = vec3.divide(vecA, vecA, vecB); });
  216. it("should place values into vecA", function() { expect(vecA).toBeEqualish([0.25, 0.4, 0.5]); });
  217. it("should return vecA", function() { expect(result).toBe(vecA); });
  218. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  219. });
  220. describe("when vecB is the output vector", function() {
  221. beforeEach(function() { result = vec3.divide(vecB, vecA, vecB); });
  222. it("should place values into vecB", function() { expect(vecB).toBeEqualish([0.25, 0.4, 0.5]); });
  223. it("should return vecB", function() { expect(result).toBe(vecB); });
  224. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  225. });
  226. });
  227. describe("ceil", function() {
  228. beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2]; });
  229. describe("with a separate output vector", function() {
  230. beforeEach(function() { result = vec3.ceil(out, vecA); });
  231. it("should place values into out", function() { expect(out).toBeEqualish([3, 4, 2]); });
  232. it("should return out", function() { expect(result).toBe(out); });
  233. it("should not modify vecA", function() { expect(vecA).toBeEqualish([Math.E, Math.PI, Math.SQRT2]); });
  234. });
  235. describe("when vecA is the output vector", function() {
  236. beforeEach(function() { result = vec3.ceil(vecA, vecA); });
  237. it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 4, 2]); });
  238. it("should return vecA", function() { expect(result).toBe(vecA); });
  239. });
  240. });
  241. describe("floor", function() {
  242. beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2]; });
  243. describe("with a separate output vector", function() {
  244. beforeEach(function() { result = vec3.floor(out, vecA); });
  245. it("should place values into out", function() { expect(out).toBeEqualish([2, 3, 1]); });
  246. it("should return out", function() { expect(result).toBe(out); });
  247. it("should not modify vecA", function() { expect(vecA).toBeEqualish([Math.E, Math.PI, Math.SQRT2]); });
  248. });
  249. describe("when vecA is the output vector", function() {
  250. beforeEach(function() { result = vec3.floor(vecA, vecA); });
  251. it("should place values into vecA", function() { expect(vecA).toBeEqualish([2, 3, 1]); });
  252. it("should return vecA", function() { expect(result).toBe(vecA); });
  253. });
  254. });
  255. describe("min", function() {
  256. beforeEach(function() { vecA = [1, 3, 1]; vecB = [3, 1, 3]; });
  257. describe("with a separate output vector", function() {
  258. beforeEach(function() { result = vec3.min(out, vecA, vecB); });
  259. it("should place values into out", function() { expect(out).toBeEqualish([1, 1, 1]); });
  260. it("should return out", function() { expect(result).toBe(out); });
  261. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 3, 1]); });
  262. it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 1, 3]); });
  263. });
  264. describe("when vecA is the output vector", function() {
  265. beforeEach(function() { result = vec3.min(vecA, vecA, vecB); });
  266. it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 1, 1]); });
  267. it("should return vecA", function() { expect(result).toBe(vecA); });
  268. it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 1, 3]); });
  269. });
  270. describe("when vecB is the output vector", function() {
  271. beforeEach(function() { result = vec3.min(vecB, vecA, vecB); });
  272. it("should place values into vecB", function() { expect(vecB).toBeEqualish([1, 1, 1]); });
  273. it("should return vecB", function() { expect(result).toBe(vecB); });
  274. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 3, 1]); });
  275. });
  276. });
  277. describe("max", function() {
  278. beforeEach(function() { vecA = [1, 3, 1]; vecB = [3, 1, 3]; });
  279. describe("with a separate output vector", function() {
  280. beforeEach(function() { result = vec3.max(out, vecA, vecB); });
  281. it("should place values into out", function() { expect(out).toBeEqualish([3, 3, 3]); });
  282. it("should return out", function() { expect(result).toBe(out); });
  283. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 3, 1]); });
  284. it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 1, 3]); });
  285. });
  286. describe("when vecA is the output vector", function() {
  287. beforeEach(function() { result = vec3.max(vecA, vecA, vecB); });
  288. it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3, 3]); });
  289. it("should return vecA", function() { expect(result).toBe(vecA); });
  290. it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 1, 3]); });
  291. });
  292. describe("when vecB is the output vector", function() {
  293. beforeEach(function() { result = vec3.max(vecB, vecA, vecB); });
  294. it("should place values into vecB", function() { expect(vecB).toBeEqualish([3, 3, 3]); });
  295. it("should return vecB", function() { expect(result).toBe(vecB); });
  296. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 3, 1]); });
  297. });
  298. });
  299. describe("round", function() {
  300. beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2]; });
  301. describe("with a separate output vector", function() {
  302. beforeEach(function() { result = vec3.round(out, vecA); });
  303. it("should place values into out", function() { expect(out).toBeEqualish([3, 3, 1]); });
  304. it("should return out", function() { expect(result).toBe(out); });
  305. it("should not modify vecA", function() { expect(vecA).toBeEqualish([Math.E, Math.PI, Math.SQRT2]); });
  306. });
  307. describe("when vecA is the output vector", function() {
  308. beforeEach(function() { result = vec3.round(vecA, vecA); });
  309. it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3, 1]); });
  310. it("should return vecA", function() { expect(result).toBe(vecA); });
  311. });
  312. });
  313. describe("scale", function() {
  314. describe("with a separate output vector", function() {
  315. beforeEach(function() { result = vec3.scale(out, vecA, 2); });
  316. it("should place values into out", function() { expect(out).toBeEqualish([2, 4, 6]); });
  317. it("should return out", function() { expect(result).toBe(out); });
  318. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  319. });
  320. describe("when vecA is the output vector", function() {
  321. beforeEach(function() { result = vec3.scale(vecA, vecA, 2); });
  322. it("should place values into vecA", function() { expect(vecA).toBeEqualish([2, 4, 6]); });
  323. it("should return vecA", function() { expect(result).toBe(vecA); });
  324. });
  325. });
  326. describe("scaleAndAdd", function() {
  327. describe("with a separate output vector", function() {
  328. beforeEach(function() { result = vec3.scaleAndAdd(out, vecA, vecB, 0.5); });
  329. it("should place values into out", function() { expect(out).toBeEqualish([3, 4.5, 6]); });
  330. it("should return out", function() { expect(result).toBe(out); });
  331. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  332. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  333. });
  334. describe("when vecA is the output vector", function() {
  335. beforeEach(function() { result = vec3.scaleAndAdd(vecA, vecA, vecB, 0.5); });
  336. it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 4.5, 6]); });
  337. it("should return vecA", function() { expect(result).toBe(vecA); });
  338. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  339. });
  340. describe("when vecB is the output vector", function() {
  341. beforeEach(function() { result = vec3.scaleAndAdd(vecB, vecA, vecB, 0.5); });
  342. it("should place values into vecB", function() { expect(vecB).toBeEqualish([3, 4.5, 6]); });
  343. it("should return vecB", function() { expect(result).toBe(vecB); });
  344. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  345. });
  346. });
  347. describe("distance", function() {
  348. it("should have an alias called 'dist'", function() { expect(vec3.dist).toEqual(vec3.distance); });
  349. beforeEach(function() { result = vec3.distance(vecA, vecB); });
  350. it("should return the distance", function() { expect(result).toBeEqualish(5.196152); });
  351. });
  352. describe("squaredDistance", function() {
  353. it("should have an alias called 'sqrDist'", function() { expect(vec3.sqrDist).toEqual(vec3.squaredDistance); });
  354. beforeEach(function() { result = vec3.squaredDistance(vecA, vecB); });
  355. it("should return the squared distance", function() { expect(result).toEqual(27); });
  356. });
  357. describe("length", function() {
  358. it("should have an alias called 'len'", function() { expect(vec3.len).toEqual(vec3.length); });
  359. beforeEach(function() { result = vec3.len(vecA); });
  360. it("should return the length", function() { expect(result).toBeEqualish(3.741657); });
  361. });
  362. describe("squaredLength", function() {
  363. it("should have an alias called 'sqrLen'", function() { expect(vec3.sqrLen).toEqual(vec3.squaredLength); });
  364. beforeEach(function() { result = vec3.squaredLength(vecA); });
  365. it("should return the squared length", function() { expect(result).toEqual(14); });
  366. });
  367. describe("negate", function() {
  368. describe("with a separate output vector", function() {
  369. beforeEach(function() { result = vec3.negate(out, vecA); });
  370. it("should place values into out", function() { expect(out).toBeEqualish([-1, -2, -3]); });
  371. it("should return out", function() { expect(result).toBe(out); });
  372. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  373. });
  374. describe("when vecA is the output vector", function() {
  375. beforeEach(function() { result = vec3.negate(vecA, vecA); });
  376. it("should place values into vecA", function() { expect(vecA).toBeEqualish([-1, -2, -3]); });
  377. it("should return vecA", function() { expect(result).toBe(vecA); });
  378. });
  379. });
  380. describe("normalize", function() {
  381. beforeEach(function() { vecA = [5, 0, 0]; });
  382. describe("with a separate output vector", function() {
  383. beforeEach(function() { result = vec3.normalize(out, vecA); });
  384. it("should place values into out", function() { expect(out).toBeEqualish([1, 0, 0]); });
  385. it("should return out", function() { expect(result).toBe(out); });
  386. it("should not modify vecA", function() { expect(vecA).toBeEqualish([5, 0, 0]); });
  387. });
  388. describe("when vecA is the output vector", function() {
  389. beforeEach(function() { result = vec3.normalize(vecA, vecA); });
  390. it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 0, 0]); });
  391. it("should return vecA", function() { expect(result).toBe(vecA); });
  392. });
  393. });
  394. describe("dot", function() {
  395. beforeEach(function() { result = vec3.dot(vecA, vecB); });
  396. it("should return the dot product", function() { expect(result).toEqual(32); });
  397. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  398. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  399. });
  400. describe("cross", function() {
  401. describe("with a separate output vector", function() {
  402. beforeEach(function() { result = vec3.cross(out, vecA, vecB); });
  403. it("should place values into out", function() { expect(out).toBeEqualish([-3, 6, -3]); });
  404. it("should return out", function() { expect(result).toBe(out); });
  405. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  406. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  407. });
  408. describe("when vecA is the output vector", function() {
  409. beforeEach(function() { result = vec3.cross(vecA, vecA, vecB); });
  410. it("should place values into vecA", function() { expect(vecA).toBeEqualish([-3, 6, -3]); });
  411. it("should return vecA", function() { expect(result).toBe(vecA); });
  412. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  413. });
  414. describe("when vecB is the output vector", function() {
  415. beforeEach(function() { result = vec3.cross(vecB, vecA, vecB); });
  416. it("should place values into vecB", function() { expect(vecB).toBeEqualish([-3, 6, -3]); });
  417. it("should return vecB", function() { expect(result).toBe(vecB); });
  418. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  419. });
  420. });
  421. describe("lerp", function() {
  422. describe("with a separate output vector", function() {
  423. beforeEach(function() { result = vec3.lerp(out, vecA, vecB, 0.5); });
  424. it("should place values into out", function() { expect(out).toBeEqualish([2.5, 3.5, 4.5]); });
  425. it("should return out", function() { expect(result).toBe(out); });
  426. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  427. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  428. });
  429. describe("when vecA is the output vector", function() {
  430. beforeEach(function() { result = vec3.lerp(vecA, vecA, vecB, 0.5); });
  431. it("should place values into vecA", function() { expect(vecA).toBeEqualish([2.5, 3.5, 4.5]); });
  432. it("should return vecA", function() { expect(result).toBe(vecA); });
  433. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  434. });
  435. describe("when vecB is the output vector", function() {
  436. beforeEach(function() { result = vec3.lerp(vecB, vecA, vecB, 0.5); });
  437. it("should place values into vecB", function() { expect(vecB).toBeEqualish([2.5, 3.5, 4.5]); });
  438. it("should return vecB", function() { expect(result).toBe(vecB); });
  439. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  440. });
  441. });
  442. describe("random", function() {
  443. describe("with no scale", function() {
  444. beforeEach(function() { result = vec3.random(out); });
  445. it("should result in a unit length vector", function() { expect(vec3.len(out)).toBeEqualish(1.0); });
  446. it("should return out", function() { expect(result).toBe(out); });
  447. });
  448. describe("with a scale", function() {
  449. beforeEach(function() { result = vec3.random(out, 5.0); });
  450. it("should result in a unit length vector", function() { expect(vec3.len(out)).toBeEqualish(5.0); });
  451. it("should return out", function() { expect(result).toBe(out); });
  452. });
  453. });
  454. describe("forEach", function() {
  455. let vecArray;
  456. beforeEach(function() {
  457. vecArray = [
  458. 1, 2, 3,
  459. 4, 5, 6,
  460. 0, 0, 0
  461. ];
  462. });
  463. describe("when performing operations that take no extra arguments", function() {
  464. beforeEach(function() { result = vec3.forEach(vecArray, 0, 0, 0, vec3.normalize); });
  465. it("should update all values", function() {
  466. expect(vecArray).toBeEqualish([
  467. 0.267261, 0.534522, 0.801783,
  468. 0.455842, 0.569802, 0.683763,
  469. 0, 0, 0
  470. ]);
  471. });
  472. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  473. });
  474. describe("when performing operations that takes one extra arguments", function() {
  475. beforeEach(function() { result = vec3.forEach(vecArray, 0, 0, 0, vec3.add, vecA); });
  476. it("should update all values", function() {
  477. expect(vecArray).toBeEqualish([
  478. 2, 4, 6,
  479. 5, 7, 9,
  480. 1, 2, 3
  481. ]);
  482. });
  483. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  484. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  485. });
  486. describe("when specifying an offset", function() {
  487. beforeEach(function() { result = vec3.forEach(vecArray, 0, 3, 0, vec3.add, vecA); });
  488. it("should update all values except the first vector", function() {
  489. expect(vecArray).toBeEqualish([
  490. 1, 2, 3,
  491. 5, 7, 9,
  492. 1, 2, 3
  493. ]);
  494. });
  495. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  496. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  497. });
  498. describe("when specifying a count", function() {
  499. beforeEach(function() { result = vec3.forEach(vecArray, 0, 0, 2, vec3.add, vecA); });
  500. it("should update all values except the last vector", function() {
  501. expect(vecArray).toBeEqualish([
  502. 2, 4, 6,
  503. 5, 7, 9,
  504. 0, 0, 0
  505. ]);
  506. });
  507. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  508. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  509. });
  510. describe("when specifying a stride", function() {
  511. beforeEach(function() { result = vec3.forEach(vecArray, 6, 0, 0, vec3.add, vecA); });
  512. it("should update all values except the second vector", function() {
  513. expect(vecArray).toBeEqualish([
  514. 2, 4, 6,
  515. 4, 5, 6,
  516. 1, 2, 3
  517. ]);
  518. });
  519. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  520. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  521. });
  522. describe("when calling a function that does not modify the out variable", function() {
  523. beforeEach(function() {
  524. result = vec3.forEach(vecArray, 0, 0, 0, function(out, vec) {});
  525. });
  526. it("values should remain unchanged", function() {
  527. expect(vecArray).toBeEqualish([
  528. 1, 2, 3,
  529. 4, 5, 6,
  530. 0, 0, 0
  531. ]);
  532. });
  533. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  534. });
  535. });
  536. describe("angle", function() {
  537. beforeEach(function() { result = vec3.angle(vecA, vecB); });
  538. it("should return the angle", function() { expect(result).toBeEqualish(0.225726); });
  539. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
  540. it("should not modify vecB", function() { expect(vecB).toBeEqualish([4, 5, 6]); });
  541. });
  542. describe("str", function() {
  543. beforeEach(function() { result = vec3.str(vecA); });
  544. it("should return a string representation of the vector", function() { expect(result).toEqual("vec3(1, 2, 3)"); });
  545. });
  546. describe("exactEquals", function() {
  547. let vecC, r0, r1;
  548. beforeEach(function() {
  549. vecA = [0, 1, 2];
  550. vecB = [0, 1, 2];
  551. vecC = [1, 2, 3];
  552. r0 = vec3.exactEquals(vecA, vecB);
  553. r1 = vec3.exactEquals(vecA, vecC);
  554. });
  555. it("should return true for identical vectors", function() { expect(r0).toBe(true); });
  556. it("should return false for different vectors", function() { expect(r1).toBe(false); });
  557. it("should not modify vecA", function() { expect(vecA).toBeEqualish([0, 1, 2]); });
  558. it("should not modify vecB", function() { expect(vecB).toBeEqualish([0, 1, 2]); });
  559. });
  560. describe("equals", function() {
  561. let vecC, vecD, r0, r1, r2;
  562. beforeEach(function() {
  563. vecA = [0, 1, 2];
  564. vecB = [0, 1, 2];
  565. vecC = [1, 2, 3];
  566. vecD = [1e-16, 1, 2];
  567. r0 = vec3.equals(vecA, vecB);
  568. r1 = vec3.equals(vecA, vecC);
  569. r2 = vec3.equals(vecA, vecD);
  570. });
  571. it("should return true for identical vectors", function() { expect(r0).toBe(true); });
  572. it("should return false for different vectors", function() { expect(r1).toBe(false); });
  573. it("should return true for close but not identical vectors", function() { expect(r2).toBe(true); });
  574. it("should not modify vecA", function() { expect(vecA).toBeEqualish([0, 1, 2]); });
  575. it("should not modify vecB", function() { expect(vecB).toBeEqualish([0, 1, 2]); });
  576. });
  577. });