aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/v4math_tut.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/test/v4math_tut.cpp')
-rw-r--r--linden/indra/test/v4math_tut.cpp379
1 files changed, 379 insertions, 0 deletions
diff --git a/linden/indra/test/v4math_tut.cpp b/linden/indra/test/v4math_tut.cpp
new file mode 100644
index 0000000..055e825
--- /dev/null
+++ b/linden/indra/test/v4math_tut.cpp
@@ -0,0 +1,379 @@
1/**
2 * @file v4math_tut.cpp
3 * @author Adroit
4 * @date 2007-03
5 * @brief v4math test cases.
6 *
7 * Copyright (c) 2007-2007, Linden Research, Inc.
8 *
9 * Second Life Viewer Source Code
10 * The source code in this file ("Source Code") is provided by Linden Lab
11 * to you under the terms of the GNU General Public License, version 2.0
12 * ("GPL"), unless you have obtained a separate licensing agreement
13 * ("Other License"), formally executed by you and Linden Lab. Terms of
14 * the GPL can be found in doc/GPL-license.txt in this distribution, or
15 * online at http://secondlife.com/developers/opensource/gplv2
16 *
17 * There are special exceptions to the terms and conditions of the GPL as
18 * it is applied to this Source Code. View the full text of the exception
19 * in the file doc/FLOSS-exception.txt in this software distribution, or
20 * online at http://secondlife.com/developers/opensource/flossexception
21 *
22 * By copying, modifying or distributing this software, you acknowledge
23 * that you have read and understood your obligations described above,
24 * and agree to abide by those obligations.
25 *
26 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
27 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
28 * COMPLETENESS OR PERFORMANCE.
29 */
30
31#include <tut/tut.h>
32#include "lltut.h"
33#include "linden_common.h"
34#include "llquaternion.h"
35#include "llsd.h"
36#include "m4math.h"
37#include "v4math.h"
38
39namespace tut
40{
41 struct v4math_data
42 {
43 };
44 typedef test_group<v4math_data> v4math_test;
45 typedef v4math_test::object v4math_object;
46 tut::v4math_test v4math_testcase("v4math");
47
48 template<> template<>
49 void v4math_object::test<1>()
50 {
51 LLVector4 vec4;
52 ensure("1:LLVector4:Fail to initialize " ,((0 == vec4.mV[VX]) && (0 == vec4.mV[VY]) && (0 == vec4.mV[VZ])&& (1.0f == vec4.mV[VW])));
53 F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
54 LLVector4 vec4a(x,y,z);
55 ensure("2:LLVector4:Fail to initialize " ,((x == vec4a.mV[VX]) && (y == vec4a.mV[VY]) && (z == vec4a.mV[VZ])&& (1.0f == vec4a.mV[VW])));
56 LLVector4 vec4b(x,y,z,w);
57 ensure("3:LLVector4:Fail to initialize " ,((x == vec4b.mV[VX]) && (y == vec4b.mV[VY]) && (z == vec4b.mV[VZ])&& (w == vec4b.mV[VW])));
58 const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
59 LLVector4 vec4c(vec);
60 ensure("4:LLVector4:Fail to initialize " ,((vec[0] == vec4c.mV[VX]) && (vec[1] == vec4c.mV[VY]) && (vec[2] == vec4c.mV[VZ])&& (vec[3] == vec4c.mV[VW])));
61 LLVector3 vec3(-2.23f,1.01f,42.3f);
62 LLVector4 vec4d(vec3);
63 ensure("5:LLVector4:Fail to initialize " ,((vec3.mV[VX] == vec4d.mV[VX]) && (vec3.mV[VY] == vec4d.mV[VY]) && (vec3.mV[VZ] == vec4d.mV[VZ])&& (1.f == vec4d.mV[VW])));
64 F32 w1 = -.234f;
65 LLVector4 vec4e(vec3,w1);
66 ensure("6:LLVector4:Fail to initialize " ,((vec3.mV[VX] == vec4e.mV[VX]) && (vec3.mV[VY] == vec4e.mV[VY]) && (vec3.mV[VZ] == vec4e.mV[VZ])&& (w1 == vec4e.mV[VW])));
67 }
68
69 template<> template<>
70 void v4math_object::test<2>()
71 {
72 F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
73 LLVector4 vec4;
74 vec4.setVec(x,y,z);
75 ensure("1:setVec:Fail to initialize " ,((x == vec4.mV[VX]) && (y == vec4.mV[VY]) && (z == vec4.mV[VZ])&& (1.0f == vec4.mV[VW])));
76 vec4.clearVec();
77 ensure("2:clearVec:Fail " ,((0 == vec4.mV[VX]) && (0 == vec4.mV[VY]) && (0 == vec4.mV[VZ])&& (1.0f == vec4.mV[VW])));
78 vec4.setVec(x,y,z,w);
79 ensure("3:setVec:Fail to initialize " ,((x == vec4.mV[VX]) && (y == vec4.mV[VY]) && (z == vec4.mV[VZ])&& (w == vec4.mV[VW])));
80 vec4.zeroVec();
81 ensure("4:zeroVec:Fail " ,((0 == vec4.mV[VX]) && (0 == vec4.mV[VY]) && (0 == vec4.mV[VZ])&& (0 == vec4.mV[VW])));
82 LLVector3 vec3(-2.23f,1.01f,42.3f);
83 vec4.clearVec();
84 vec4.setVec(vec3);
85 ensure("5:setVec:Fail to initialize " ,((vec3.mV[VX] == vec4.mV[VX]) && (vec3.mV[VY] == vec4.mV[VY]) && (vec3.mV[VZ] == vec4.mV[VZ])&& (1.f == vec4.mV[VW])));
86 F32 w1 = -.234f;
87 vec4.zeroVec();
88 vec4.setVec(vec3,w1);
89 ensure("6:setVec:Fail to initialize " ,((vec3.mV[VX] == vec4.mV[VX]) && (vec3.mV[VY] == vec4.mV[VY]) && (vec3.mV[VZ] == vec4.mV[VZ])&& (w1 == vec4.mV[VW])));
90 const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
91 LLVector4 vec4a;
92 vec4a.setVec(vec);
93 ensure("7:setVec:Fail to initialize " ,((vec[0] == vec4a.mV[VX]) && (vec[1] == vec4a.mV[VY]) && (vec[2] == vec4a.mV[VZ])&& (vec[3] == vec4a.mV[VW])));
94 }
95
96 template<> template<>
97 void v4math_object::test<3>()
98 {
99 F32 x = 10.f, y = -2.3f, z = -.023f;
100 LLVector4 vec4(x,y,z);
101 ensure("magVec:Fail ", is_approx_equal(vec4.magVec(), fsqrtf(x*x + y*y + z*z)));
102 ensure("magVecSquared:Fail ", is_approx_equal(vec4.magVecSquared(), (x*x + y*y + z*z)));
103 }
104
105 template<> template<>
106 void v4math_object::test<4>()
107 {
108 F32 x = 10.f, y = -2.3f, z = -.023f;
109 LLVector4 vec4(x,y,z);
110 F32 mag = vec4.normVec();
111 mag = 1.f/ mag;
112 ensure("1:normVec: Fail " ,is_approx_equal(mag*x,vec4.mV[VX]) && is_approx_equal(mag*y, vec4.mV[VY])&& is_approx_equal(mag*z, vec4.mV[VZ]));
113 x = 0.000000001f, y = 0.000000001f, z = 0.000000001f;
114 vec4.clearVec();
115 vec4.setVec(x,y,z);
116 mag = vec4.normVec();
117 ensure("2:normVec: Fail " ,is_approx_equal(mag*x,vec4.mV[VX]) && is_approx_equal(mag*y, vec4.mV[VY])&& is_approx_equal(mag*z, vec4.mV[VZ]));
118 }
119
120 template<> template<>
121 void v4math_object::test<5>()
122 {
123 F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
124 LLVector4 vec4(x,y,z,w);
125 vec4.abs();
126 ensure("abs:Fail " ,((x == vec4.mV[VX]) && (-y == vec4.mV[VY]) && (-z == vec4.mV[VZ])&& (-w == vec4.mV[VW])));
127 vec4.clearVec();
128 ensure("isExactlyClear:Fail " ,(TRUE == vec4.isExactlyClear()));
129 vec4.zeroVec();
130 ensure("isExactlyZero:Fail " ,(TRUE == vec4.isExactlyZero()));
131 }
132
133 template<> template<>
134 void v4math_object::test<6>()
135 {
136 F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
137 LLVector4 vec4(x,y,z,w),vec4a;
138 vec4a = vec4.scaleVec(vec4);
139 ensure("scaleVec:Fail " ,(is_approx_equal(x*x, vec4a.mV[VX]) && is_approx_equal(y*y, vec4a.mV[VY]) && is_approx_equal(z*z, vec4a.mV[VZ])&& is_approx_equal(w*w, vec4a.mV[VW])));
140 }
141
142 template<> template<>
143 void v4math_object::test<7>()
144 {
145 F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
146 LLVector4 vec4(x,y,z,w);
147 ensure("1:operator [] failed " ,( x == vec4[0]));
148 ensure("2:operator [] failed " ,( y == vec4[1]));
149 ensure("3:operator [] failed " ,( z == vec4[2]));
150 ensure("4:operator [] failed " ,( w == vec4[3]));
151 x = 23.f, y = -.2361f, z = 3.25;
152 vec4.setVec(x,y,z);
153 F32 &ref1 = vec4[0];
154 ensure("5:operator [] failed " ,( ref1 == vec4[0]));
155 F32 &ref2 = vec4[1];
156 ensure("6:operator [] failed " ,( ref2 == vec4[1]));
157 F32 &ref3 = vec4[2];
158 ensure("7:operator [] failed " ,( ref3 == vec4[2]));
159 F32 &ref4 = vec4[3];
160 ensure("8:operator [] failed " ,( ref4 == vec4[3]));
161 }
162
163 template<> template<>
164 void v4math_object::test<8>()
165 {
166 F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
167 const F32 val[10] = {1.f,2.f,3.f,.34f,.1f,-.5f,2.f,1.23f,1.234f,.89f};
168 LLMatrix4 mat(val);
169 LLVector4 vec4(x,y,z,w),vec4a;
170 vec4.rotVec(mat);
171 vec4a.setVec(x,y,z,w);
172 vec4a.rotVec(mat);
173 ensure_equals("1:rotVec: Fail " ,vec4a, vec4);
174 F32 a = 2.32f, b = -23.2f, c = -34.1112f, d = 1.010112f;
175 LLQuaternion q(a,b,c,d);
176 LLVector4 vec4b(a,b,c,d),vec4c;
177 vec4b.rotVec(q);
178 vec4c.setVec(a, b, c, d);
179 vec4c.rotVec(q);
180 ensure_equals("2:rotVec: Fail " ,vec4b, vec4c);
181 }
182
183 template<> template<>
184 void v4math_object::test<9>()
185 {
186 F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
187 LLVector4 vec4(x,y,z,w),vec4a;;
188 std::ostringstream stream1, stream2;
189 stream1 << vec4;
190 vec4a.setVec(x,y,z,w);
191 stream2 << vec4a;
192 ensure("operator << failed",(stream1.str() == stream2.str()));
193 }
194
195 template<> template<>
196 void v4math_object::test<10>()
197 {
198 F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f, w1 = .23f;
199 F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f, w2 = 1.3f;
200 LLVector4 vec4(x1,y1,z1,w1),vec4a(x2,y2,z2,w2),vec4b;
201 vec4b = vec4a + vec4;
202 ensure("1:operator+:Fail to initialize " ,(is_approx_equal(x1+x2,vec4b.mV[VX]) && is_approx_equal(y1+y2,vec4b.mV[VY]) && is_approx_equal(z1+z2,vec4b.mV[VZ])));
203 x1 = -2.45f, y1 = 2.1f, z1 = 3.0f;
204 vec4.clearVec();
205 vec4a.clearVec();
206 vec4.setVec(x1,y1,z1);
207 vec4a +=vec4;
208 ensure_equals("2:operator+=: Fail to initialize", vec4a,vec4);
209 vec4a += vec4;
210 ensure("3:operator+=:Fail to initialize " ,(is_approx_equal(2*x1,vec4a.mV[VX]) && is_approx_equal(2*y1,vec4a.mV[VY]) && is_approx_equal(2*z1,vec4a.mV[VZ])));
211 }
212 template<> template<>
213 void v4math_object::test<11>()
214 {
215 F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f, w1 = .23f;
216 F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f, w2 = 1.3f;
217 LLVector4 vec4(x1,y1,z1,w1),vec4a(x2,y2,z2,w2),vec4b;
218 vec4b = vec4a - vec4;
219 ensure("1:operator-:Fail to initialize " ,(is_approx_equal(x2-x1,vec4b.mV[VX]) && is_approx_equal(y2-y1,vec4b.mV[VY]) && is_approx_equal(z2-z1,vec4b.mV[VZ])));
220 x1 = -2.45f, y1 = 2.1f, z1 = 3.0f;
221 vec4.clearVec();
222 vec4a.clearVec();
223 vec4.setVec(x1,y1,z1);
224 vec4a -=vec4;
225 ensure_equals("2:operator-=: Fail to initialize" , vec4a,-vec4);
226 vec4a -=vec4;
227 ensure("3:operator-=:Fail to initialize " ,(is_approx_equal(-2*x1,vec4a.mV[VX]) && is_approx_equal(-2*y1,vec4a.mV[VY]) && is_approx_equal(-2*z1,vec4a.mV[VZ])));
228 }
229
230 template<> template<>
231 void v4math_object::test<12>()
232 {
233 F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f;
234 F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f;
235 LLVector4 vec4(x1,y1,z1),vec4a(x2,y2,z2);
236 F32 res = vec4 * vec4a;
237 ensure("1:operator* failed " ,is_approx_equal(res, x1*x2 + y1*y2 + z1*z2));
238 vec4a.clearVec();
239 F32 mulVal = 4.2f;
240 vec4a = vec4 * mulVal;
241 ensure("2:operator* failed " ,is_approx_equal(x1*mulVal,vec4a.mV[VX]) && is_approx_equal(y1*mulVal, vec4a.mV[VY])&& is_approx_equal(z1*mulVal, vec4a.mV[VZ]));
242 vec4a.clearVec();
243 vec4a = mulVal * vec4 ;
244 ensure("3:operator* failed " ,is_approx_equal(x1*mulVal, vec4a.mV[VX]) && is_approx_equal(y1*mulVal, vec4a.mV[VY])&& is_approx_equal(z1*mulVal, vec4a.mV[VZ]));
245 vec4 *= mulVal;
246 ensure("4:operator*= failed " ,is_approx_equal(x1*mulVal, vec4.mV[VX]) && is_approx_equal(y1*mulVal, vec4.mV[VY])&& is_approx_equal(z1*mulVal, vec4.mV[VZ]));
247 }
248
249 template<> template<>
250 void v4math_object::test<13>()
251 {
252 F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f;
253 F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f;
254 LLVector4 vec4(x1,y1,z1),vec4a(x2,y2,z2),vec4b;
255 vec4b = vec4 % vec4a;
256 ensure("1:operator% failed " ,is_approx_equal(y1*z2 - y2*z1, vec4b.mV[VX]) && is_approx_equal(z1*x2 -z2*x1, vec4b.mV[VY]) && is_approx_equal(x1*y2-x2*y1, vec4b.mV[VZ]));
257 vec4 %= vec4a;
258 ensure_equals("operator%= failed " ,vec4,vec4b);
259 }
260
261 template<> template<>
262 void v4math_object::test<14>()
263 {
264 F32 x = 1.f, y = 2.f, z = -1.1f,div = 4.2f;
265 F32 t = 1.f / div;
266 LLVector4 vec4(x,y,z), vec4a;
267 vec4a = vec4/div;
268 ensure("1:operator/ failed " ,is_approx_equal(x*t, vec4a.mV[VX]) && is_approx_equal(y*t, vec4a.mV[VY])&& is_approx_equal(z*t, vec4a.mV[VZ]));
269 x = 1.23f, y = 4.f, z = -2.32f;
270 vec4.clearVec();
271 vec4a.clearVec();
272 vec4.setVec(x,y,z);
273 vec4a = vec4/div;
274 ensure("2:operator/ failed " ,is_approx_equal(x*t, vec4a.mV[VX]) && is_approx_equal(y*t, vec4a.mV[VY])&& is_approx_equal(z*t, vec4a.mV[VZ]));
275 vec4 /= div;
276 ensure("3:operator/ failed " ,is_approx_equal(x*t, vec4.mV[VX]) && is_approx_equal(y*t, vec4.mV[VY])&& is_approx_equal(z*t, vec4.mV[VZ]));
277 }
278
279 template<> template<>
280 void v4math_object::test<15>()
281 {
282 F32 x = 1.f, y = 2.f, z = -1.1f;
283 LLVector4 vec4(x,y,z), vec4a;
284 ensure("operator!= failed " ,(vec4 != vec4a));
285 vec4a = vec4;
286 ensure("operator== failed " ,(vec4 ==vec4a));
287 }
288
289 template<> template<>
290 void v4math_object::test<16>()
291 {
292 F32 x = 1.f, y = 2.f, z = -1.1f;
293 LLVector4 vec4(x,y,z), vec4a;
294 vec4a = - vec4;
295 ensure("operator- failed " , (vec4 == - vec4a));
296 }
297
298 template<> template<>
299 void v4math_object::test<17>()
300 {
301 F32 x = 1.f, y = 2.f, z = -1.1f,epsilon = .23425f;
302 LLVector4 vec4(x,y,z), vec4a(x,y,z);
303 ensure("1:are_parallel: Fail " ,(TRUE == are_parallel(vec4a,vec4,epsilon)));
304 x = 21.f, y = 12.f, z = -123.1f;
305 vec4a.clearVec();
306 vec4a.setVec(x,y,z);
307 ensure("2:are_parallel: Fail " ,(FALSE == are_parallel(vec4a,vec4,epsilon)));
308 }
309
310 template<> template<>
311 void v4math_object::test<18>()
312 {
313 F32 x = 1.f, y = 2.f, z = -1.1f;
314 F32 angle1, angle2;
315 LLVector4 vec4(x,y,z), vec4a(x,y,z);
316 angle1 = angle_between(vec4, vec4a);
317 vec4.normVec();
318 vec4a.normVec();
319 angle2 = acos(vec4 * vec4a);
320 ensure_approximately_equals("1:angle_between: Fail " ,angle1,angle2,8);
321 F32 x1 = 21.f, y1 = 2.23f, z1 = -1.1f;
322 LLVector4 vec4b(x,y,z), vec4c(x1,y1,z1);
323 angle1 = angle_between(vec4b, vec4c);
324 vec4b.normVec();
325 vec4c.normVec();
326 angle2 = acos(vec4b * vec4c);
327 ensure_approximately_equals("2:angle_between: Fail " ,angle1,angle2,8);
328 }
329
330 template<> template<>
331 void v4math_object::test<19>()
332 {
333 F32 x1 =-2.3f, y1 = 2.f,z1 = 1.2f, x2 = 1.3f, y2 = 1.f, z2 = 1.f;
334 F32 val1,val2;
335 LLVector4 vec4(x1,y1,z1),vec4a(x2,y2,z2);
336 val1 = dist_vec(vec4,vec4a);
337 val2 = fsqrtf((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2) + (z1 - z2)* (z1 -z2));
338 ensure_equals("dist_vec: Fail ",val2, val1);
339 val1 = dist_vec_squared(vec4,vec4a);
340 val2 =((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2) + (z1 - z2)* (z1 -z2));
341 ensure_equals("dist_vec_squared: Fail ",val2, val1);
342 }
343
344 template<> template<>
345 void v4math_object::test<20>()
346 {
347 F32 x1 =-2.3f, y1 = 2.f,z1 = 1.2f, w1 = -.23f, x2 = 1.3f, y2 = 1.f, z2 = 1.f,w2 = .12f;
348 F32 val = 2.3f,val1,val2,val3,val4;
349 LLVector4 vec4(x1,y1,z1,w1),vec4a(x2,y2,z2,w2);
350 val1 = x1 + (x2 - x1)* val;
351 val2 = y1 + (y2 - y1)* val;
352 val3 = z1 + (z2 - z1)* val;
353 val4 = w1 + (w2 - w1)* val;
354 LLVector4 vec4b = lerp(vec4,vec4a,val);
355 ensure("lerp failed", ((val1 ==vec4b.mV[VX])&& (val2 ==vec4b.mV[VY]) && (val3 ==vec4b.mV[VZ])&& (val4 ==vec4b.mV[VW])));
356 }
357
358 template<> template<>
359 void v4math_object::test<21>()
360 {
361 F32 x = 1.f, y = 2.f, z = -1.1f;
362 LLVector4 vec4(x,y,z);
363 LLVector3 vec3 = vec4to3(vec4);
364 ensure("vec4to3 failed", ((x == vec3.mV[VX])&& (y == vec3.mV[VY]) && (z == vec3.mV[VZ])));
365 LLVector4 vec4a = vec3to4(vec3);
366 ensure_equals("vec3to4 failed",vec4a,vec4);
367 }
368
369 template<> template<>
370 void v4math_object::test<22>()
371 {
372 F32 x = 1.f, y = 2.f, z = -1.1f;
373 LLVector4 vec4(x,y,z);
374 LLSD llsd = vec4.getValue();
375 LLVector3 vec3(llsd);
376 LLVector4 vec4a = vec3to4(vec3);
377 ensure_equals("getValue failed",vec4a,vec4);
378 }
379}