Debian Codegraph @

closure-library/closure/goog/math/vec3_test.js

raw
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
15 goog.provide('goog.math.Vec3Test');
16 goog.setTestOnly('goog.math.Vec3Test');
17
18 goog.require('goog.math.Coordinate3');
19 goog.require('goog.math.Vec3');
20 goog.require('goog.testing.jsunit');
21
22 function assertVec3Equals(a, b) {
23 assertTrue(b + ' should be equal to ' + a, goog.math.Vec3.equals(a, b));
24 }
25
26 function testVec3() {
27 var v = new goog.math.Vec3(3.14, 2.78, -7.21);
28 assertEquals(3.14, v.x);
29 assertEquals(2.78, v.y);
30 assertEquals(-7.21, v.z);
31 }
32
33
34 function testRandomUnit() {
35 var a = goog.math.Vec3.randomUnit();
36 assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
37 }
38
39
40 function testRandom() {
41 var a = goog.math.Vec3.random();
42 assertTrue(a.magnitude() <= 1.0);
43 }
44
45
46 function testFromCoordinate3() {
47 var a = new goog.math.Coordinate3(-2, 10, 4);
48 var b = goog.math.Vec3.fromCoordinate3(a);
49 assertEquals(-2, b.x);
50 assertEquals(10, b.y);
51 assertEquals(4, b.z);
52 }
53
54
55 function testClone() {
56 var a = new goog.math.Vec3(1, 2, 5);
57 var b = a.clone();
58
59 assertEquals(a.x, b.x);
60 assertEquals(a.y, b.y);
61 assertEquals(a.z, b.z);
62 }
63
64
65 function testMagnitude() {
66 var a = new goog.math.Vec3(0, 10, 0);
67 var b = new goog.math.Vec3(3, 4, 5);
68 var c = new goog.math.Vec3(-4, 3, 8);
69
70 assertEquals(10, a.magnitude());
71 assertEquals(Math.sqrt(50), b.magnitude());
72 assertEquals(Math.sqrt(89), c.magnitude());
73 }
74
75
76 function testSquaredMagnitude() {
77 var a = new goog.math.Vec3(-3, -4, -5);
78 assertEquals(50, a.squaredMagnitude());
79 }
80
81
82 function testScale() {
83 var a = new goog.math.Vec3(1, 2, 3);
84 a.scale(0.5);
85
86 assertEquals(0.5, a.x);
87 assertEquals(1, a.y);
88 assertEquals(1.5, a.z);
89 }
90
91
92 function testInvert() {
93 var a = new goog.math.Vec3(3, 4, 5);
94 a.invert();
95
96 assertEquals(-3, a.x);
97 assertEquals(-4, a.y);
98 assertEquals(-5, a.z);
99 }
100
101
102 function testNormalize() {
103 var a = new goog.math.Vec3(5, 5, 5);
104 a.normalize();
105 assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
106 }
107
108
109 function testAdd() {
110 var a = new goog.math.Vec3(1, -1, 7);
111 a.add(new goog.math.Vec3(3, 3, 3));
112 assertVec3Equals(new goog.math.Vec3(4, 2, 10), a);
113 }
114
115
116 function testSubtract() {
117 var a = new goog.math.Vec3(1, -1, 4);
118 a.subtract(new goog.math.Vec3(3, 3, 3));
119 assertVec3Equals(new goog.math.Vec3(-2, -4, 1), a);
120 }
121
122
123 function testEquals() {
124 var a = new goog.math.Vec3(1, 2, 5);
125
126 assertFalse(a.equals(null));
127 assertFalse(a.equals(new goog.math.Vec3(1, 3, 5)));
128 assertFalse(a.equals(new goog.math.Vec3(2, 2, 2)));
129
130 assertTrue(a.equals(a));
131 assertTrue(a.equals(new goog.math.Vec3(1, 2, 5)));
132 }
133
134
135 function testSum() {
136 var a = new goog.math.Vec3(0.5, 0.25, 1.2);
137 var b = new goog.math.Vec3(0.5, 0.75, -0.6);
138
139 var c = goog.math.Vec3.sum(a, b);
140 assertVec3Equals(new goog.math.Vec3(1, 1, 0.6), c);
141 }
142
143
144 function testDifference() {
145 var a = new goog.math.Vec3(0.5, 0.25, 3);
146 var b = new goog.math.Vec3(0.5, 0.75, 5);
147
148 var c = goog.math.Vec3.difference(a, b);
149 assertVec3Equals(new goog.math.Vec3(0, -0.5, -2), c);
150 }
151
152
153 function testDistance() {
154 var a = new goog.math.Vec3(3, 4, 5);
155 var b = new goog.math.Vec3(-3, -4, 5);
156
157 assertEquals(10, goog.math.Vec3.distance(a, b));
158 }
159
160
161 function testSquaredDistance() {
162 var a = new goog.math.Vec3(3, 4, 5);
163 var b = new goog.math.Vec3(-3, -4, 5);
164
165 assertEquals(100, goog.math.Vec3.squaredDistance(a, b));
166 }
167
168
169 function testVec3Equals() {
170 assertTrue(goog.math.Vec3.equals(null, null, null));
171 assertFalse(goog.math.Vec3.equals(null, new goog.math.Vec3()));
172
173 var a = new goog.math.Vec3(1, 3, 5);
174 assertTrue(goog.math.Vec3.equals(a, a));
175 assertTrue(goog.math.Vec3.equals(a, new goog.math.Vec3(1, 3, 5)));
176 assertFalse(goog.math.Vec3.equals(1, new goog.math.Vec3(3, 1, 5)));
177 }
178
179
180 function testDot() {
181 var a = new goog.math.Vec3(0, 5, 2);
182 var b = new goog.math.Vec3(3, 0, 5);
183 assertEquals(10, goog.math.Vec3.dot(a, b));
184
185 var c = new goog.math.Vec3(-5, -5, 5);
186 var d = new goog.math.Vec3(0, 7, -2);
187 assertEquals(-45, goog.math.Vec3.dot(c, d));
188 }
189
190
191 function testCross() {
192 var a = new goog.math.Vec3(3, 0, 0);
193 var b = new goog.math.Vec3(0, 2, 0);
194 assertVec3Equals(new goog.math.Vec3(0, 0, 6), goog.math.Vec3.cross(a, b));
195
196 var c = new goog.math.Vec3(1, 2, 3);
197 var d = new goog.math.Vec3(4, 5, 6);
198 assertVec3Equals(new goog.math.Vec3(-3, 6, -3), goog.math.Vec3.cross(c, d));
199 }
200
201
202 function testLerp() {
203 var a = new goog.math.Vec3(0, 0, 0);
204 var b = new goog.math.Vec3(10, 10, 10);
205
206 for (var i = 0; i <= 10; i++) {
207 var c = goog.math.Vec3.lerp(a, b, i / 10);
208 assertEquals(i, c.x);
209 assertEquals(i, c.y);
210 assertEquals(i, c.z);
211 }
212 }
213
214
215 function testRescaled() {
216 const a = new goog.math.Vec3(2, 3, 4);
217 const b = goog.math.Vec3.rescaled(a, -2);
218 assertVec3Equals(new goog.math.Vec3(-4, -6, -8), b);
219 assertVec3Equals(new goog.math.Vec3(2, 3, 4), a);
220 }