vec3_test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.math.Vec3Test');
  15. goog.setTestOnly('goog.math.Vec3Test');
  16. goog.require('goog.math.Coordinate3');
  17. goog.require('goog.math.Vec3');
  18. goog.require('goog.testing.jsunit');
  19. function assertVec3Equals(a, b) {
  20. assertTrue(b + ' should be equal to ' + a, goog.math.Vec3.equals(a, b));
  21. }
  22. function testVec3() {
  23. var v = new goog.math.Vec3(3.14, 2.78, -7.21);
  24. assertEquals(3.14, v.x);
  25. assertEquals(2.78, v.y);
  26. assertEquals(-7.21, v.z);
  27. }
  28. function testRandomUnit() {
  29. var a = goog.math.Vec3.randomUnit();
  30. assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
  31. }
  32. function testRandom() {
  33. var a = goog.math.Vec3.random();
  34. assertTrue(a.magnitude() <= 1.0);
  35. }
  36. function testFromCoordinate3() {
  37. var a = new goog.math.Coordinate3(-2, 10, 4);
  38. var b = goog.math.Vec3.fromCoordinate3(a);
  39. assertEquals(-2, b.x);
  40. assertEquals(10, b.y);
  41. assertEquals(4, b.z);
  42. }
  43. function testClone() {
  44. var a = new goog.math.Vec3(1, 2, 5);
  45. var b = a.clone();
  46. assertEquals(a.x, b.x);
  47. assertEquals(a.y, b.y);
  48. assertEquals(a.z, b.z);
  49. }
  50. function testMagnitude() {
  51. var a = new goog.math.Vec3(0, 10, 0);
  52. var b = new goog.math.Vec3(3, 4, 5);
  53. var c = new goog.math.Vec3(-4, 3, 8);
  54. assertEquals(10, a.magnitude());
  55. assertEquals(Math.sqrt(50), b.magnitude());
  56. assertEquals(Math.sqrt(89), c.magnitude());
  57. }
  58. function testSquaredMagnitude() {
  59. var a = new goog.math.Vec3(-3, -4, -5);
  60. assertEquals(50, a.squaredMagnitude());
  61. }
  62. function testScale() {
  63. var a = new goog.math.Vec3(1, 2, 3);
  64. a.scale(0.5);
  65. assertEquals(0.5, a.x);
  66. assertEquals(1, a.y);
  67. assertEquals(1.5, a.z);
  68. }
  69. function testInvert() {
  70. var a = new goog.math.Vec3(3, 4, 5);
  71. a.invert();
  72. assertEquals(-3, a.x);
  73. assertEquals(-4, a.y);
  74. assertEquals(-5, a.z);
  75. }
  76. function testNormalize() {
  77. var a = new goog.math.Vec3(5, 5, 5);
  78. a.normalize();
  79. assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
  80. }
  81. function testAdd() {
  82. var a = new goog.math.Vec3(1, -1, 7);
  83. a.add(new goog.math.Vec3(3, 3, 3));
  84. assertVec3Equals(new goog.math.Vec3(4, 2, 10), a);
  85. }
  86. function testSubtract() {
  87. var a = new goog.math.Vec3(1, -1, 4);
  88. a.subtract(new goog.math.Vec3(3, 3, 3));
  89. assertVec3Equals(new goog.math.Vec3(-2, -4, 1), a);
  90. }
  91. function testEquals() {
  92. var a = new goog.math.Vec3(1, 2, 5);
  93. assertFalse(a.equals(null));
  94. assertFalse(a.equals(new goog.math.Vec3(1, 3, 5)));
  95. assertFalse(a.equals(new goog.math.Vec3(2, 2, 2)));
  96. assertTrue(a.equals(a));
  97. assertTrue(a.equals(new goog.math.Vec3(1, 2, 5)));
  98. }
  99. function testSum() {
  100. var a = new goog.math.Vec3(0.5, 0.25, 1.2);
  101. var b = new goog.math.Vec3(0.5, 0.75, -0.6);
  102. var c = goog.math.Vec3.sum(a, b);
  103. assertVec3Equals(new goog.math.Vec3(1, 1, 0.6), c);
  104. }
  105. function testDifference() {
  106. var a = new goog.math.Vec3(0.5, 0.25, 3);
  107. var b = new goog.math.Vec3(0.5, 0.75, 5);
  108. var c = goog.math.Vec3.difference(a, b);
  109. assertVec3Equals(new goog.math.Vec3(0, -0.5, -2), c);
  110. }
  111. function testDistance() {
  112. var a = new goog.math.Vec3(3, 4, 5);
  113. var b = new goog.math.Vec3(-3, -4, 5);
  114. assertEquals(10, goog.math.Vec3.distance(a, b));
  115. }
  116. function testSquaredDistance() {
  117. var a = new goog.math.Vec3(3, 4, 5);
  118. var b = new goog.math.Vec3(-3, -4, 5);
  119. assertEquals(100, goog.math.Vec3.squaredDistance(a, b));
  120. }
  121. function testVec3Equals() {
  122. assertTrue(goog.math.Vec3.equals(null, null, null));
  123. assertFalse(goog.math.Vec3.equals(null, new goog.math.Vec3()));
  124. var a = new goog.math.Vec3(1, 3, 5);
  125. assertTrue(goog.math.Vec3.equals(a, a));
  126. assertTrue(goog.math.Vec3.equals(a, new goog.math.Vec3(1, 3, 5)));
  127. assertFalse(goog.math.Vec3.equals(1, new goog.math.Vec3(3, 1, 5)));
  128. }
  129. function testDot() {
  130. var a = new goog.math.Vec3(0, 5, 2);
  131. var b = new goog.math.Vec3(3, 0, 5);
  132. assertEquals(10, goog.math.Vec3.dot(a, b));
  133. var c = new goog.math.Vec3(-5, -5, 5);
  134. var d = new goog.math.Vec3(0, 7, -2);
  135. assertEquals(-45, goog.math.Vec3.dot(c, d));
  136. }
  137. function testCross() {
  138. var a = new goog.math.Vec3(3, 0, 0);
  139. var b = new goog.math.Vec3(0, 2, 0);
  140. assertVec3Equals(new goog.math.Vec3(0, 0, 6), goog.math.Vec3.cross(a, b));
  141. var c = new goog.math.Vec3(1, 2, 3);
  142. var d = new goog.math.Vec3(4, 5, 6);
  143. assertVec3Equals(new goog.math.Vec3(-3, 6, -3), goog.math.Vec3.cross(c, d));
  144. }
  145. function testLerp() {
  146. var a = new goog.math.Vec3(0, 0, 0);
  147. var b = new goog.math.Vec3(10, 10, 10);
  148. for (var i = 0; i <= 10; i++) {
  149. var c = goog.math.Vec3.lerp(a, b, i / 10);
  150. assertEquals(i, c.x);
  151. assertEquals(i, c.y);
  152. assertEquals(i, c.z);
  153. }
  154. }