PageRenderTime 67ms CodeModel.GetById 2ms app.highlight 60ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/llmath/tests/v3dmath_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 526 lines | 472 code | 27 blank | 27 comment | 108 complexity | 72071fa6258f0e102d82154e94d45e9f MD5 | raw file
  1/**
  2 * @file v3dmath_test.cpp
  3 * @author Adroit
  4 * @date 2007-03
  5 * @brief v3dmath test cases.
  6 *
  7 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28 
 29#include "linden_common.h"
 30#include "llsd.h"
 31#include "../test/lltut.h"
 32
 33#include "../m3math.h"
 34#include "../v4math.h"
 35#include "../v3dmath.h"
 36#include "../v3dmath.h"
 37#include "../llquaternion.h"
 38
 39namespace tut
 40{
 41	struct v3dmath_data
 42	{
 43	};
 44	typedef test_group<v3dmath_data> v3dmath_test;
 45	typedef v3dmath_test::object v3dmath_object;
 46	tut::v3dmath_test v3dmath_testcase("v3dmath_h");
 47
 48	template<> template<>
 49	void v3dmath_object::test<1>()
 50	{
 51		LLVector3d vec3D;
 52		ensure("1:LLVector3d:Fail to initialize ", ((0 == vec3D.mdV[VX]) && (0 == vec3D.mdV[VY]) && (0 == vec3D.mdV[VZ])));
 53		F64 x = 2.32f, y = 1.212f, z = -.12f;
 54		LLVector3d vec3Da(x,y,z);
 55		ensure("2:LLVector3d:Fail to initialize ", ((2.32f == vec3Da.mdV[VX]) && (1.212f == vec3Da.mdV[VY]) && (-.12f == vec3Da.mdV[VZ])));
 56		const F64 vec[3] = {1.2f ,3.2f, -4.2f};
 57		LLVector3d vec3Db(vec);
 58		ensure("3:LLVector3d:Fail to initialize ", ((1.2f == vec3Db.mdV[VX]) && (3.2f == vec3Db.mdV[VY]) && (-4.2f == vec3Db.mdV[VZ])));
 59		LLVector3 vec3((F32)x,(F32)y,(F32)z);
 60		LLVector3d vec3Dc(vec3);
 61		ensure_equals("4:LLVector3d Fail to initialize",vec3Da,vec3Dc);
 62	}
 63
 64	template<> template<>
 65	void v3dmath_object::test<2>()
 66	{
 67		S32 a = -235;
 68		LLSD llsd(a);
 69		LLVector3d vec3d(llsd);
 70		LLSD sd = vec3d.getValue();
 71		LLVector3d vec3da(sd);
 72		ensure("1:getValue:Fail ", (vec3d == vec3da));
 73	}
 74
 75	template<> template<>
 76	void v3dmath_object::test<3>()
 77	{
 78		F64 a = 232345521.411132;
 79		LLSD llsd(a);
 80		LLVector3d vec3d;
 81		vec3d.setValue(llsd);
 82		LLSD sd = vec3d.getValue();
 83		LLVector3d vec3da(sd);
 84		ensure("1:setValue:Fail to initialize ", (vec3d == vec3da));
 85	}
 86
 87	template<> template<>
 88	void v3dmath_object::test<4>()
 89	{
 90		F64 a[3] = {222231.43222, 12345.2343, -434343.33222};
 91		LLSD llsd;
 92		llsd[0] = a[0];
 93		llsd[1] = a[1];
 94		llsd[2] = a[2];
 95		LLVector3d vec3D;
 96		vec3D = (LLVector3d)llsd;
 97		ensure("1:operator=:Fail to initialize ", ((llsd[0].asReal()== vec3D.mdV[VX]) && (llsd[1].asReal() == vec3D.mdV[VY]) && (llsd[2].asReal() == vec3D.mdV[VZ])));
 98	}
 99	
100	template<> template<>
101	void v3dmath_object::test<5>()
102	{
103		F64 x = 2.32f, y = 1.212f, z = -.12f;
104		LLVector3d vec3D(x,y,z);
105		vec3D.clearVec();
106		ensure("1:clearVec:Fail to initialize ", ((0 == vec3D.mdV[VX]) && (0 == vec3D.mdV[VY]) && (0 == vec3D.mdV[VZ])));
107		vec3D.setVec(x,y,z);
108		ensure("2:setVec:Fail to initialize ", ((x == vec3D.mdV[VX]) && (y == vec3D.mdV[VY]) && (z == vec3D.mdV[VZ])));
109		vec3D.zeroVec();
110		ensure("3:zeroVec:Fail to initialize ", ((0 == vec3D.mdV[VX]) && (0 == vec3D.mdV[VY]) && (0 == vec3D.mdV[VZ])));
111		vec3D.clearVec();
112		LLVector3 vec3((F32)x,(F32)y,(F32)z);
113		vec3D.setVec(vec3);
114		ensure("4:setVec:Fail to initialize ", ((x == vec3D.mdV[VX]) && (y == vec3D.mdV[VY]) && (z == vec3D.mdV[VZ])));
115		vec3D.clearVec();
116		const F64 vec[3] = {x,y,z};
117		vec3D.setVec(vec);
118		ensure("5:setVec:Fail to initialize ", ((x == vec3D.mdV[VX]) && (y == vec3D.mdV[VY]) && (z == vec3D.mdV[VZ])));
119		LLVector3d vec3Da;
120		vec3Da.setVec(vec3D);
121		ensure_equals("6:setVec: Fail to initialize", vec3D, vec3Da);
122	}
123	
124	template<> template<>
125	void v3dmath_object::test<6>()
126	{
127		F64 x = -2.32, y = 1.212, z = -.12;
128		LLVector3d vec3D(x,y,z);
129		vec3D.abs();
130		ensure("1:abs:Fail  ", ((-x == vec3D.mdV[VX]) && (y == vec3D.mdV[VY]) && (-z == vec3D.mdV[VZ])));
131		ensure("2:isNull():Fail ", (FALSE == vec3D.isNull()));	
132		vec3D.clearVec();
133		x =.00000001, y = .000001001, z = .000001001;
134		vec3D.setVec(x,y,z);
135		ensure("3:isNull():Fail ", (TRUE == vec3D.isNull()));	
136		ensure("4:isExactlyZero():Fail ", (FALSE == vec3D.isExactlyZero()));	
137		x =.0000000, y = .00000000, z = .00000000;
138		vec3D.setVec(x,y,z);
139		ensure("5:isExactlyZero():Fail ", (TRUE == vec3D.isExactlyZero()));	
140	}
141
142	template<> template<>
143	void v3dmath_object::test<7>()
144	{
145		F64 x = -2.32, y = 1.212, z = -.12;
146		LLVector3d vec3D(x,y,z);
147		
148		ensure("1:operator [] failed",( x ==  vec3D[0]));	
149		ensure("2:operator [] failed",( y ==  vec3D[1]));
150		ensure("3:operator [] failed",( z ==  vec3D[2]));
151		vec3D.clearVec();
152		x = 23.23, y = -.2361, z = 3.25;
153		vec3D.setVec(x,y,z);
154		F64 &ref1 = vec3D[0];
155		ensure("4:operator [] failed",( ref1 ==  vec3D[0]));
156		F64 &ref2 = vec3D[1];
157		ensure("5:operator [] failed",( ref2 ==  vec3D[1]));
158		F64 &ref3 = vec3D[2];
159		ensure("6:operator [] failed",( ref3 ==  vec3D[2]));
160	}
161
162	template<> template<>
163	void v3dmath_object::test<8>()
164	{
165		F32 x = 1.f, y = 2.f, z = -1.f;
166		LLVector4 vec4(x,y,z);		
167		LLVector3d vec3D;
168		vec3D = vec4;
169		ensure("1:operator=:Fail to initialize ", ((vec4.mV[VX] == vec3D.mdV[VX]) && (vec4.mV[VY] == vec3D.mdV[VY]) && (vec4.mV[VZ] == vec3D.mdV[VZ])));
170	}
171
172	template<> template<>
173	void v3dmath_object::test<9>()
174	{
175		F64 x1 = 1.78787878, y1 = 232322.2121, z1 = -12121.121212;
176		F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
177		LLVector3d vec3D(x1,y1,z1),vec3Da(x2,y2,z2),vec3Db;
178		vec3Db = vec3Da+ vec3D;
179		ensure("1:operator+:Fail to initialize ", ((x1+x2 == vec3Db.mdV[VX]) && (y1+y2 == vec3Db.mdV[VY]) && (z1+z2 == vec3Db.mdV[VZ])));
180		x1 = -2.45, y1 = 2.1, z1 = 3.0;
181		vec3D.clearVec();
182		vec3Da.clearVec();
183		vec3D.setVec(x1,y1,z1);
184		vec3Da += vec3D;
185		ensure_equals("2:operator+=: Fail to initialize", vec3Da,vec3D);
186		vec3Da += vec3D;
187		ensure("3:operator+=:Fail to initialize ", ((2*x1 == vec3Da.mdV[VX]) && (2*y1 == vec3Da.mdV[VY]) && (2*z1 == vec3Da.mdV[VZ])));
188	}
189
190	template<> template<>
191	void v3dmath_object::test<10>()
192	{
193		F64 x1 = 1., y1 = 2., z1 = -1.1;
194		F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
195		LLVector3d vec3D(x1,y1,z1),vec3Da(x2,y2,z2),vec3Db;
196		vec3Db = vec3Da - vec3D;
197		ensure("1:operator-:Fail to initialize ", ((x2-x1 == vec3Db.mdV[VX]) && (y2-y1 == vec3Db.mdV[VY]) && (z2-z1 == vec3Db.mdV[VZ])));
198		x1 = -2.45, y1 = 2.1, z1 = 3.0;
199		vec3D.clearVec();
200		vec3Da.clearVec();
201		vec3D.setVec(x1,y1,z1);
202		vec3Da -=vec3D;
203		ensure("2:operator-=:Fail to initialize ", ((2.45 == vec3Da.mdV[VX]) && (-2.1 == vec3Da.mdV[VY]) && (-3.0 == vec3Da.mdV[VZ])));
204		vec3Da -= vec3D;
205		ensure("3:operator-=:Fail to initialize ", ((-2*x1 == vec3Da.mdV[VX]) && (-2*y1 == vec3Da.mdV[VY]) && (-2*z1 == vec3Da.mdV[VZ])));
206	}
207	template<> template<>
208	void v3dmath_object::test<11>()
209	{
210		F64 x1 = 1., y1 = 2., z1 = -1.1;
211		F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
212		LLVector3d vec3D(x1,y1,z1),vec3Da(x2,y2,z2);
213		F64 res = vec3D * vec3Da;
214		ensure_approximately_equals(
215			"1:operator* failed",
216			res,
217			(x1*x2 + y1*y2 + z1*z2),
218			8);
219		vec3Da.clearVec();
220		F64 mulVal = 4.2;
221		vec3Da = vec3D * mulVal;
222		ensure_approximately_equals(
223			"2a:operator* failed",
224			vec3Da.mdV[VX],
225			x1*mulVal,
226			8);
227		ensure_approximately_equals(
228			"2b:operator* failed",
229			vec3Da.mdV[VY],
230			y1*mulVal,
231			8);
232		ensure_approximately_equals(
233			"2c:operator* failed",
234			vec3Da.mdV[VZ],
235			z1*mulVal,
236			8);
237		vec3Da.clearVec();
238		vec3Da = mulVal * vec3D;
239		ensure_approximately_equals(
240			"3a:operator* failed",
241			vec3Da.mdV[VX],
242			x1*mulVal,
243			8);
244		ensure_approximately_equals(
245			"3b:operator* failed",
246			vec3Da.mdV[VY],
247			y1*mulVal,
248			8);
249		ensure_approximately_equals(
250			"3c:operator* failed",
251			vec3Da.mdV[VZ],
252			z1*mulVal,
253			8);
254		vec3D *= mulVal;
255		ensure_approximately_equals(
256			"4a:operator*= failed",
257			vec3D.mdV[VX],
258			x1*mulVal,
259			8);
260		ensure_approximately_equals(
261			"4b:operator*= failed",
262			vec3D.mdV[VY],
263			y1*mulVal,
264			8);
265		ensure_approximately_equals(
266			"4c:operator*= failed",
267			vec3D.mdV[VZ],
268			z1*mulVal,
269			8);
270	}
271
272	template<> template<>
273	void v3dmath_object::test<12>()
274	{
275		F64 x1 = 1., y1 = 2., z1 = -1.1;
276		F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
277		F64 val1, val2, val3;
278		LLVector3d vec3D(x1,y1,z1),vec3Da(x2,y2,z2), vec3Db;
279		vec3Db = vec3D % vec3Da;
280		val1 = y1*z2 - y2*z1;
281		val2 = z1*x2 -z2*x1;
282		val3 = x1*y2-x2*y1;
283		ensure("1:operator% failed",(val1 == vec3Db.mdV[VX]) && (val2 == vec3Db.mdV[VY]) && (val3 == vec3Db.mdV[VZ])); 
284		vec3D %= vec3Da;
285		ensure("2:operator%= failed",
286		       is_approx_equal(vec3D.mdV[VX],vec3Db.mdV[VX]) &&
287		       is_approx_equal(vec3D.mdV[VY],vec3Db.mdV[VY]) &&
288		       is_approx_equal(vec3D.mdV[VZ],vec3Db.mdV[VZ]) ); 
289	}
290
291	template<> template<>
292	void v3dmath_object::test<13>()
293	{
294		F64 x1 = 1., y1 = 2., z1 = -1.1,div = 4.2;
295		F64 t = 1.f / div;
296		LLVector3d vec3D(x1,y1,z1), vec3Da;
297		vec3Da = vec3D/div;
298		ensure_approximately_equals(
299			"1a:operator/ failed",
300			vec3Da.mdV[VX],
301			x1*t,
302			8);
303		ensure_approximately_equals(
304			"1b:operator/ failed",
305			vec3Da.mdV[VY],
306			y1*t,
307			8);
308		ensure_approximately_equals(
309			"1c:operator/ failed",
310			vec3Da.mdV[VZ],
311			z1*t,
312			8);
313		x1 = 1.23, y1 = 4., z1 = -2.32;
314		vec3D.clearVec();
315		vec3Da.clearVec();
316		vec3D.setVec(x1,y1,z1);
317		vec3Da = vec3D/div;
318		ensure_approximately_equals(
319			"2a:operator/ failed",
320			vec3Da.mdV[VX],
321			x1*t,
322			8);
323		ensure_approximately_equals(
324			"2b:operator/ failed",
325			vec3Da.mdV[VY],
326			y1*t,
327			8);
328		ensure_approximately_equals(
329			"2c:operator/ failed",
330			vec3Da.mdV[VZ],
331			z1*t,
332			8);
333		vec3D /= div;
334		ensure_approximately_equals(
335			"3a:operator/= failed",
336			vec3D.mdV[VX],
337			x1*t,
338			8);
339		ensure_approximately_equals(
340			"3b:operator/= failed",
341			vec3D.mdV[VY],
342			y1*t,
343			8);
344		ensure_approximately_equals(
345			"3c:operator/= failed",
346			vec3D.mdV[VZ],
347			z1*t,
348			8);
349	}
350
351	template<> template<>
352	void v3dmath_object::test<14>()
353	{
354		F64 x1 = 1., y1 = 2., z1 = -1.1;
355		LLVector3d vec3D(x1,y1,z1), vec3Da;
356		ensure("1:operator!= failed",(TRUE == (vec3D !=vec3Da)));
357		vec3Da = vec3D;
358		ensure("2:operator== failed",(vec3D ==vec3Da)); 
359		vec3D.clearVec();
360		vec3Da.clearVec();
361		x1 = .211, y1 = 21.111, z1 = 23.22;
362		vec3D.setVec(x1,y1,z1);
363		vec3Da.setVec(x1,y1,z1);
364		ensure("3:operator== failed",(vec3D ==vec3Da)); 
365		ensure("4:operator!= failed",(FALSE == (vec3D !=vec3Da)));
366	}
367		
368	template<> template<>
369	void v3dmath_object::test<15>()
370	{
371		F64 x1 = 1., y1 = 2., z1 = -1.1;
372		LLVector3d vec3D(x1,y1,z1), vec3Da;
373		std::ostringstream stream1, stream2;
374		stream1 << vec3D;
375		vec3Da.setVec(x1,y1,z1);
376		stream2 << vec3Da;
377		ensure("1:operator << failed",(stream1.str() == stream2.str()));	
378	}
379
380	template<> template<>
381	void v3dmath_object::test<16>()
382	{
383		F64 x1 = 1.23, y1 = 2.0, z1 = 4.;
384		std::string buf("1.23 2. 4");
385		LLVector3d vec3D, vec3Da(x1,y1,z1);
386		LLVector3d::parseVector3d(buf, &vec3D);
387		ensure_equals("1:parseVector3d: failed " , vec3D, vec3Da);	
388	}
389
390	template<> template<>
391	void v3dmath_object::test<17>()
392	{
393		F64 x1 = 1., y1 = 2., z1 = -1.1;
394		LLVector3d vec3D(x1,y1,z1), vec3Da;
395		vec3Da = -vec3D;
396		ensure("1:operator- failed", (vec3D == - vec3Da));	
397	}
398
399	template<> template<>
400	void v3dmath_object::test<18>()
401	{
402		F64 x = 1., y = 2., z = -1.1;
403		LLVector3d vec3D(x,y,z);
404		F64 res = (x*x + y*y + z*z) - vec3D.magVecSquared();
405		ensure("1:magVecSquared:Fail ", ((-F_APPROXIMATELY_ZERO <= res)&& (res <=F_APPROXIMATELY_ZERO)));
406		res = (F32) sqrt(x*x + y*y + z*z) - vec3D.magVec();
407		ensure("2:magVec: Fail ", ((-F_APPROXIMATELY_ZERO <= res)&& (res <=F_APPROXIMATELY_ZERO)));	
408	}
409
410	template<> template<>
411	void v3dmath_object::test<19>()
412	{
413		F64 x = 1., y = 2., z = -1.1;
414		LLVector3d vec3D(x,y,z);
415		F64 mag = vec3D.normVec();
416		mag = 1.f/ mag;
417		ensure_approximately_equals(
418			"1a:normVec: Fail ",
419			vec3D.mdV[VX],
420			x * mag,
421			8);
422		ensure_approximately_equals(
423			"1b:normVec: Fail ",
424			vec3D.mdV[VY],
425			y * mag,
426			8);
427		ensure_approximately_equals(
428			"1c:normVec: Fail ",
429			vec3D.mdV[VZ],
430			z * mag,
431			8);
432		x = 0.000000001, y = 0.000000001, z = 0.000000001;
433		vec3D.clearVec();
434		vec3D.setVec(x,y,z);
435		mag = vec3D.normVec();
436		ensure_approximately_equals(
437			"2a:normVec: Fail ",
438			vec3D.mdV[VX],
439			x * mag,
440			8);
441		ensure_approximately_equals(
442			"2b:normVec: Fail ",
443			vec3D.mdV[VY],
444			y * mag,
445			8);
446		ensure_approximately_equals(
447			"2c:normVec: Fail ",
448			vec3D.mdV[VZ],
449			z * mag,
450			8);
451	}
452
453	template<> template<>
454	void v3dmath_object::test<20>()
455	{
456		F64 x1 = 1111.232222;
457		F64 y1 = 2222222222.22;
458		F64 z1 = 422222222222.0;
459		std::string buf("1111.232222 2222222222.22 422222222222");
460		LLVector3d vec3Da, vec3Db(x1,y1,z1);
461		LLVector3d::parseVector3d(buf, &vec3Da);
462		ensure_equals("1:parseVector3 failed", vec3Da, vec3Db);	
463	}
464
465	template<> template<>
466	void v3dmath_object::test<21>()
467	{
468		F64 x1 = 1., y1 = 2., z1 = -1.1;
469		F64 x2 = 1.2, y2 = 2.5, z2 = 1.;
470		F64 val = 2.3f,val1,val2,val3;
471		val1 = x1 + (x2 - x1)* val;
472		val2 = y1 + (y2 - y1)* val;
473		val3 = z1 + (z2 - z1)* val;
474		LLVector3d vec3Da(x1,y1,z1),vec3Db(x2,y2,z2);
475		LLVector3d vec3d = lerp(vec3Da,vec3Db,val);
476		ensure("1:lerp failed", ((val1 ==vec3d.mdV[VX])&& (val2 ==vec3d.mdV[VY]) && (val3 ==vec3d.mdV[VZ])));		
477	}
478
479	template<> template<>
480	void v3dmath_object::test<22>()
481	{
482		F64 x = 2.32, y = 1.212, z = -.12;
483		F64 min = 0.0001, max = 3.0;
484		LLVector3d vec3d(x,y,z);		
485		ensure("1:clamp:Fail ", (TRUE == (vec3d.clamp(min, max))));
486		x = 0.000001f, z = 5.3f;
487		vec3d.setVec(x,y,z);
488		ensure("2:clamp:Fail ", (TRUE == (vec3d.clamp(min, max))));
489	}
490
491	template<> template<>
492	void v3dmath_object::test<23>()
493	{
494		F64 x = 10., y = 20., z = -15.;
495		F64 epsilon = .23425;
496		LLVector3d vec3Da(x,y,z), vec3Db(x,y,z);
497		ensure("1:are_parallel: Fail ", (TRUE == are_parallel(vec3Da,vec3Db,epsilon)));
498		F64 x1 = -12., y1 = -20., z1 = -100.;
499		vec3Db.clearVec();
500		vec3Db.setVec(x1,y1,z1);
501		ensure("2:are_parallel: Fail ", (FALSE == are_parallel(vec3Da,vec3Db,epsilon)));
502	}
503
504	template<> template<>
505	void v3dmath_object::test<24>()
506	{
507#if LL_WINDOWS && _MSC_VER < 1400
508		skip("This fails on VS2003!");
509#else
510		F64 x = 10., y = 20., z = -15.;
511		F64 angle1, angle2;
512		LLVector3d vec3Da(x,y,z), vec3Db(x,y,z);
513		angle1 = angle_between(vec3Da, vec3Db);
514		ensure("1:angle_between: Fail ", (0 == angle1));
515		F64 x1 = -1., y1 = -20., z1 = -1.;
516		vec3Da.clearVec();
517		vec3Da.setVec(x1,y1,z1);
518		angle2 = angle_between(vec3Da, vec3Db);
519		vec3Db.normVec();
520		vec3Da.normVec();
521		F64 angle = vec3Db*vec3Da;
522		angle = acos(angle);
523		ensure("2:angle_between: Fail ", (angle == angle2));
524#endif
525	}
526}