aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/v3dmath_tut.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:56 -0500
committerJacek Antonelli2008-08-15 23:44:56 -0500
commitc07901e29ed545bbb02e3bddf148fe1104b94e9f (patch)
treef1ada64ce834acd7d92a425efb96c4b86bcf16b1 /linden/indra/test/v3dmath_tut.cpp
parentSecond Life viewer sources 1.15.0.2 (diff)
downloadmeta-impy-c07901e29ed545bbb02e3bddf148fe1104b94e9f.zip
meta-impy-c07901e29ed545bbb02e3bddf148fe1104b94e9f.tar.gz
meta-impy-c07901e29ed545bbb02e3bddf148fe1104b94e9f.tar.bz2
meta-impy-c07901e29ed545bbb02e3bddf148fe1104b94e9f.tar.xz
Second Life viewer sources 1.15.1.3
Diffstat (limited to '')
-rw-r--r--linden/indra/test/v3dmath_tut.cpp402
1 files changed, 402 insertions, 0 deletions
diff --git a/linden/indra/test/v3dmath_tut.cpp b/linden/indra/test/v3dmath_tut.cpp
new file mode 100644
index 0000000..88ade71
--- /dev/null
+++ b/linden/indra/test/v3dmath_tut.cpp
@@ -0,0 +1,402 @@
1/**
2 * @file v3dmath_tut.cpp
3 * @author Adroit
4 * @date March 2007
5 * @brief v3dmath 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 "llquaternion.h"
34#include "m3math.h"
35#include "v4math.h"
36#include "llsd.h"
37#include "v3dmath.h"
38#include "v3dmath.h"
39
40namespace tut
41{
42 struct v3dmath_data
43 {
44 };
45 typedef test_group<v3dmath_data> v3dmath_test;
46 typedef v3dmath_test::object v3dmath_object;
47 tut::v3dmath_test v3dmath_testcase("v3dmath");
48
49 template<> template<>
50 void v3dmath_object::test<1>()
51 {
52 LLVector3d vec3D;
53 ensure("1:LLVector3d:Fail to initialize ", ((0 == vec3D.mdV[VX]) && (0 == vec3D.mdV[VY]) && (0 == vec3D.mdV[VZ])));
54 F64 x = 2.32f, y = 1.212f, z = -.12f;
55 LLVector3d vec3Da(x,y,z);
56 ensure("2:LLVector3d:Fail to initialize ", ((2.32f == vec3Da.mdV[VX]) && (1.212f == vec3Da.mdV[VY]) && (-.12f == vec3Da.mdV[VZ])));
57 const F64 vec[3] = {1.2f ,3.2f, -4.2f};
58 LLVector3d vec3Db(vec);
59 ensure("3:LLVector3d:Fail to initialize ", ((1.2f == vec3Db.mdV[VX]) && (3.2f == vec3Db.mdV[VY]) && (-4.2f == vec3Db.mdV[VZ])));
60 LLVector3 vec3((F32)x,(F32)y,(F32)z);
61 LLVector3d vec3Dc(vec3);
62 ensure_equals("4:LLVector3d Fail to initialize",vec3Da,vec3Dc);
63 }
64
65 template<> template<>
66 void v3dmath_object::test<2>()
67 {
68 S32 a = -235;
69 LLSD llsd(a);
70 LLVector3d vec3d(llsd);
71 LLSD sd = vec3d.getValue();
72 LLVector3d vec3da(sd);
73 ensure("1:getValue:Fail ", (vec3d == vec3da));
74 }
75
76 template<> template<>
77 void v3dmath_object::test<3>()
78 {
79 F64 a = 232345521.411132;
80 LLSD llsd(a);
81 LLVector3d vec3d;
82 vec3d.setValue(llsd);
83 LLSD sd = vec3d.getValue();
84 LLVector3d vec3da(sd);
85 ensure("1:setValue:Fail to initialize ", (vec3d == vec3da));
86 }
87
88 template<> template<>
89 void v3dmath_object::test<4>()
90 {
91 F64 a[3] = {222231.43222, 12345.2343, -434343.33222};
92 LLSD llsd;
93 llsd[0] = a[0];
94 llsd[1] = a[1];
95 llsd[2] = a[2];
96 LLVector3d vec3D;
97 vec3D = llsd;
98 ensure("1:operator=:Fail to initialize ", ((llsd[0].asReal()== vec3D.mdV[VX]) && (llsd[1].asReal() == vec3D.mdV[VY]) && (llsd[2].asReal() == vec3D.mdV[VZ])));
99 }
100
101 template<> template<>
102 void v3dmath_object::test<5>()
103 {
104 F64 x = 2.32f, y = 1.212f, z = -.12f;
105 LLVector3d vec3D(x,y,z);
106 vec3D.clearVec();
107 ensure("1:clearVec:Fail to initialize ", ((0 == vec3D.mdV[VX]) && (0 == vec3D.mdV[VY]) && (0 == vec3D.mdV[VZ])));
108 vec3D.setVec(x,y,z);
109 ensure("2:setVec:Fail to initialize ", ((x == vec3D.mdV[VX]) && (y == vec3D.mdV[VY]) && (z == vec3D.mdV[VZ])));
110 vec3D.zeroVec();
111 ensure("3:zeroVec:Fail to initialize ", ((0 == vec3D.mdV[VX]) && (0 == vec3D.mdV[VY]) && (0 == vec3D.mdV[VZ])));
112 vec3D.clearVec();
113 LLVector3 vec3((F32)x,(F32)y,(F32)z);
114 vec3D.setVec(vec3);
115 ensure("4:setVec:Fail to initialize ", ((x == vec3D.mdV[VX]) && (y == vec3D.mdV[VY]) && (z == vec3D.mdV[VZ])));
116 vec3D.clearVec();
117 const F64 vec[3] = {x,y,z};
118 vec3D.setVec(vec);
119 ensure("5:setVec:Fail to initialize ", ((x == vec3D.mdV[VX]) && (y == vec3D.mdV[VY]) && (z == vec3D.mdV[VZ])));
120 LLVector3d vec3Da;
121 vec3Da.setVec(vec3D);
122 ensure_equals("6:setVec: Fail to initialize", vec3D, vec3Da);
123 }
124
125 template<> template<>
126 void v3dmath_object::test<6>()
127 {
128 F64 x = -2.32, y = 1.212, z = -.12;
129 LLVector3d vec3D(x,y,z);
130 vec3D.abs();
131 ensure("1:abs:Fail ", ((-x == vec3D.mdV[VX]) && (y == vec3D.mdV[VY]) && (-z == vec3D.mdV[VZ])));
132 ensure("2:isNull():Fail ", (FALSE == vec3D.isNull()));
133 vec3D.clearVec();
134 x =.00000001, y = .000001001, z = .000001001;
135 vec3D.setVec(x,y,z);
136 ensure("3:isNull():Fail ", (TRUE == vec3D.isNull()));
137 ensure("4:isExactlyZero():Fail ", (FALSE == vec3D.isExactlyZero()));
138 x =.0000000, y = .00000000, z = .00000000;
139 vec3D.setVec(x,y,z);
140 ensure("5:isExactlyZero():Fail ", (TRUE == vec3D.isExactlyZero()));
141 }
142
143 template<> template<>
144 void v3dmath_object::test<7>()
145 {
146 F64 x = -2.32, y = 1.212, z = -.12;
147 LLVector3d vec3D(x,y,z);
148
149 ensure("1:operator [] failed",( x == vec3D[0]));
150 ensure("2:operator [] failed",( y == vec3D[1]));
151 ensure("3:operator [] failed",( z == vec3D[2]));
152 vec3D.clearVec();
153 x = 23.23, y = -.2361, z = 3.25;
154 vec3D.setVec(x,y,z);
155 F64 &ref1 = vec3D[0];
156 ensure("4:operator [] failed",( ref1 == vec3D[0]));
157 F64 &ref2 = vec3D[1];
158 ensure("5:operator [] failed",( ref2 == vec3D[1]));
159 F64 &ref3 = vec3D[2];
160 ensure("6:operator [] failed",( ref3 == vec3D[2]));
161 }
162
163 template<> template<>
164 void v3dmath_object::test<8>()
165 {
166 F32 x = 1.f, y = 2.f, z = -1.f;
167 LLVector4 vec4(x,y,z);
168 LLVector3d vec3D;
169 vec3D = vec4;
170 ensure("1:operator=:Fail to initialize ", ((vec4.mV[VX] == vec3D.mdV[VX]) && (vec4.mV[VY] == vec3D.mdV[VY]) && (vec4.mV[VZ] == vec3D.mdV[VZ])));
171 }
172
173 template<> template<>
174 void v3dmath_object::test<9>()
175 {
176 F64 x1 = 1.78787878, y1 = 232322.2121, z1 = -12121.121212;
177 F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
178 LLVector3d vec3D(x1,y1,z1),vec3Da(x2,y2,z2),vec3Db;
179 vec3Db = vec3Da+ vec3D;
180 ensure("1:operator+:Fail to initialize ", ((x1+x2 == vec3Db.mdV[VX]) && (y1+y2 == vec3Db.mdV[VY]) && (z1+z2 == vec3Db.mdV[VZ])));
181 x1 = -2.45, y1 = 2.1, z1 = 3.0;
182 vec3D.clearVec();
183 vec3Da.clearVec();
184 vec3D.setVec(x1,y1,z1);
185 vec3Da += vec3D;
186 ensure_equals("2:operator+=: Fail to initialize", vec3Da,vec3D);
187 vec3Da += vec3D;
188 ensure("3:operator+=:Fail to initialize ", ((2*x1 == vec3Da.mdV[VX]) && (2*y1 == vec3Da.mdV[VY]) && (2*z1 == vec3Da.mdV[VZ])));
189 }
190
191 template<> template<>
192 void v3dmath_object::test<10>()
193 {
194 F64 x1 = 1., y1 = 2., z1 = -1.1;
195 F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
196 LLVector3d vec3D(x1,y1,z1),vec3Da(x2,y2,z2),vec3Db;
197 vec3Db = vec3Da - vec3D;
198 ensure("1:operator-:Fail to initialize ", ((x2-x1 == vec3Db.mdV[VX]) && (y2-y1 == vec3Db.mdV[VY]) && (z2-z1 == vec3Db.mdV[VZ])));
199 x1 = -2.45, y1 = 2.1, z1 = 3.0;
200 vec3D.clearVec();
201 vec3Da.clearVec();
202 vec3D.setVec(x1,y1,z1);
203 vec3Da -=vec3D;
204 ensure("2:operator-=:Fail to initialize ", ((2.45 == vec3Da.mdV[VX]) && (-2.1 == vec3Da.mdV[VY]) && (-3.0 == vec3Da.mdV[VZ])));
205 vec3Da -= vec3D;
206 ensure("3:operator-=:Fail to initialize ", ((-2*x1 == vec3Da.mdV[VX]) && (-2*y1 == vec3Da.mdV[VY]) && (-2*z1 == vec3Da.mdV[VZ])));
207 }
208 template<> template<>
209 void v3dmath_object::test<11>()
210 {
211 F64 x1 = 1., y1 = 2., z1 = -1.1;
212 F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
213 LLVector3d vec3D(x1,y1,z1),vec3Da(x2,y2,z2);
214 F64 res = vec3D * vec3Da;
215 ensure("1:operator* failed",(res == (x1*x2 + y1*y2 + z1*z2)));
216 vec3Da.clearVec();
217 F64 mulVal = 4.2;
218 vec3Da = vec3D * mulVal;
219 ensure("2:operator* failed",(x1*mulVal == vec3Da.mdV[VX]) && (y1*mulVal == vec3Da.mdV[VY])&& (z1*mulVal == vec3Da.mdV[VZ]));
220 vec3Da.clearVec();
221 vec3Da = mulVal * vec3D;
222 ensure("3:operator* failed",(x1*mulVal == vec3Da.mdV[VX]) && (y1*mulVal == vec3Da.mdV[VY])&& (z1*mulVal == vec3Da.mdV[VZ]));
223 vec3D *= mulVal;
224 ensure("4:operator*= failed",(x1*mulVal == vec3D.mdV[VX]) && (y1*mulVal == vec3D.mdV[VY])&& (z1*mulVal == vec3D.mdV[VZ]));
225 }
226
227 template<> template<>
228 void v3dmath_object::test<12>()
229 {
230 F64 x1 = 1., y1 = 2., z1 = -1.1;
231 F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
232 F64 val1, val2, val3;
233 LLVector3d vec3D(x1,y1,z1),vec3Da(x2,y2,z2), vec3Db;
234 vec3Db = vec3D % vec3Da;
235 val1 = y1*z2 - y2*z1;
236 val2 = z1*x2 -z2*x1;
237 val3 = x1*y2-x2*y1;
238 ensure("1:operator% failed",(val1 == vec3Db.mdV[VX]) && (val2 == vec3Db.mdV[VY]) && (val3 == vec3Db.mdV[VZ]));
239 vec3D %= vec3Da;
240 ensure_equals("2:operator%= failed",vec3D,vec3Db);
241 }
242
243 template<> template<>
244 void v3dmath_object::test<13>()
245 {
246 F64 x1 = 1., y1 = 2., z1 = -1.1,div = 4.2;
247 F64 t = 1.f / div;
248 LLVector3d vec3D(x1,y1,z1), vec3Da;
249 vec3Da = vec3D/div;
250 ensure("1:operator/ failed",(x1*t == vec3Da.mdV[VX]) && (y1*t == vec3Da.mdV[VY])&& (z1*t == vec3Da.mdV[VZ]));
251 x1 = 1.23, y1 = 4., z1 = -2.32;
252 vec3D.clearVec();
253 vec3Da.clearVec();
254 vec3D.setVec(x1,y1,z1);
255 vec3Da = vec3D/div;
256 ensure("2:operator/ failed",(x1*t == vec3Da.mdV[VX]) && (y1*t == vec3Da.mdV[VY])&& (z1*t == vec3Da.mdV[VZ]));
257 vec3D /= div;
258 ensure("3:operator/= failed",(x1*t == vec3D.mdV[VX]) && (y1*t == vec3D.mdV[VY])&& (z1*t == vec3D.mdV[VZ]));
259 }
260
261 template<> template<>
262 void v3dmath_object::test<14>()
263 {
264 F64 x1 = 1., y1 = 2., z1 = -1.1;
265 LLVector3d vec3D(x1,y1,z1), vec3Da;
266 ensure("1:operator!= failed",(TRUE == (vec3D !=vec3Da)));
267 vec3Da = vec3D;
268 ensure("2:operator== failed",(vec3D ==vec3Da));
269 vec3D.clearVec();
270 vec3Da.clearVec();
271 x1 = .211, y1 = 21.111, z1 = 23.22;
272 vec3D.setVec(x1,y1,z1);
273 vec3Da.setVec(x1,y1,z1);
274 ensure("3:operator== failed",(vec3D ==vec3Da));
275 ensure("4:operator!= failed",(FALSE == (vec3D !=vec3Da)));
276 }
277
278 template<> template<>
279 void v3dmath_object::test<15>()
280 {
281 F64 x1 = 1., y1 = 2., z1 = -1.1;
282 LLVector3d vec3D(x1,y1,z1), vec3Da;
283 std::ostringstream stream1, stream2;
284 stream1 << vec3D;
285 vec3Da.setVec(x1,y1,z1);
286 stream2 << vec3Da;
287 ensure("1:operator << failed",(stream1.str() == stream2.str()));
288 }
289
290 template<> template<>
291 void v3dmath_object::test<16>()
292 {
293 F64 x1 = 1.23, y1 = 2.0, z1 = 4.;
294 char buf[] = "1.23 2. 4";
295 LLVector3d vec3D, vec3Da(x1,y1,z1);
296 LLVector3d::parseVector3d(buf, &vec3D);
297 ensure_equals("1:parseVector3d: failed " , vec3D, vec3Da);
298 }
299
300 template<> template<>
301 void v3dmath_object::test<17>()
302 {
303 F64 x1 = 1., y1 = 2., z1 = -1.1;
304 LLVector3d vec3D(x1,y1,z1), vec3Da;
305 vec3Da = -vec3D;
306 ensure("1:operator- failed", (vec3D == - vec3Da));
307 }
308
309 template<> template<>
310 void v3dmath_object::test<18>()
311 {
312 F64 x = 1., y = 2., z = -1.1;
313 LLVector3d vec3D(x,y,z);
314 F64 res = (x*x + y*y + z*z) - vec3D.magVecSquared();
315 ensure("1:magVecSquared:Fail ", ((-F_APPROXIMATELY_ZERO <= res)&& (res <=F_APPROXIMATELY_ZERO)));
316 res = fsqrtf(x*x + y*y + z*z) - vec3D.magVec();
317 ensure("2:magVec: Fail ", ((-F_APPROXIMATELY_ZERO <= res)&& (res <=F_APPROXIMATELY_ZERO)));
318 }
319
320 template<> template<>
321 void v3dmath_object::test<19>()
322 {
323 F64 x = 1., y = 2., z = -1.1;
324 LLVector3d vec3D(x,y,z);
325 F64 mag = vec3D.normVec();
326 mag = 1.f/ mag;
327 ensure("1:normVec: Fail ", (x* mag == vec3D.mdV[VX]) && (y* mag == vec3D.mdV[VY])&& (z* mag == vec3D.mdV[VZ]));
328 x = 0.000000001, y = 0.000000001, z = 0.000000001;
329 vec3D.clearVec();
330 vec3D.setVec(x,y,z);
331 mag = vec3D.normVec();
332 ensure("2:normVec: Fail ", (x* mag == vec3D.mdV[VX]) && (y* mag == vec3D.mdV[VY])&& (z* mag == vec3D.mdV[VZ]));
333 }
334
335 template<> template<>
336 void v3dmath_object::test<20>()
337 {
338 F64 x1 = 1111.232222, y1 = 2222222222.22, z1 = 422222222222;
339 char buf[] = "1111.232222 2222222222.22 422222222222";
340 LLVector3d vec3Da, vec3Db(x1,y1,z1);
341 LLVector3d::parseVector3d(buf, &vec3Da);
342 ensure_equals("1:parseVector3 failed", vec3Da, vec3Db);
343 }
344
345 template<> template<>
346 void v3dmath_object::test<21>()
347 {
348 F64 x1 = 1., y1 = 2., z1 = -1.1;
349 F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
350 F64 val = 2.3f,val1,val2,val3;
351 val1 = x1 + (x2 - x1)* val;
352 val2 = y1 + (y2 - y1)* val;
353 val3 = z1 + (z2 - z1)* val;
354 LLVector3d vec3Da(x1,y1,z1),vec3Db(x2,y2,z2);
355 LLVector3d vec3d = lerp(vec3Da,vec3Db,val);
356 ensure("1:lerp failed", ((val1 ==vec3d.mdV[VX])&& (val2 ==vec3d.mdV[VY]) && (val3 ==vec3d.mdV[VZ])));
357 }
358
359 template<> template<>
360 void v3dmath_object::test<22>()
361 {
362 F64 x = 2.32, y = 1.212, z = -.12;
363 F64 min = 0.0001, max = 3.0;
364 LLVector3d vec3d(x,y,z);
365 ensure("1:clamp:Fail ", (TRUE == (vec3d.clamp(min, max))));
366 x = 0.000001f, z = 5.3f;
367 vec3d.setVec(x,y,z);
368 ensure("2:clamp:Fail ", (TRUE == (vec3d.clamp(min, max))));
369 }
370
371 template<> template<>
372 void v3dmath_object::test<23>()
373 {
374 F64 x = 10., y = 20., z = -15.;
375 F64 epsilon = .23425;
376 LLVector3d vec3Da(x,y,z), vec3Db(x,y,z);
377 ensure("1:are_parallel: Fail ", (TRUE == are_parallel(vec3Da,vec3Db,epsilon)));
378 F64 x1 = -12., y1 = -20., z1 = -100.;
379 vec3Db.clearVec();
380 vec3Db.setVec(x1,y1,z1);
381 ensure("2:are_parallel: Fail ", (FALSE == are_parallel(vec3Da,vec3Db,epsilon)));
382 }
383
384 template<> template<>
385 void v3dmath_object::test<24>()
386 {
387 F64 x = 10., y = 20., z = -15.;
388 F64 angle1, angle2;
389 LLVector3d vec3Da(x,y,z), vec3Db(x,y,z);
390 angle1 = angle_between(vec3Da, vec3Db);
391 ensure("1:angle_between: Fail ", (0 == angle1));
392 F64 x1 = -1., y1 = -20., z1 = -1.;
393 vec3Da.clearVec();
394 vec3Da.setVec(x1,y1,z1);
395 angle2 = angle_between(vec3Da, vec3Db);
396 vec3Db.normVec();
397 vec3Da.normVec();
398 F64 angle = vec3Db*vec3Da;
399 angle = acos(angle);
400 ensure("2:angle_between: Fail ", (angle == angle2));
401 }
402} \ No newline at end of file