PageRenderTime 53ms CodeModel.GetById 24ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/llmath/tests/m3math_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 332 lines | 236 code | 53 blank | 43 comment | 54 complexity | 6d74f61881a289c0cc9ea195d082939c MD5 | raw file
  1/** 
  2 * @file m3math_test.cpp
  3 * @author Adroit
  4 * @date 2007-03
  5 * @brief Test cases of m3math.h
  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
 31#include "../m3math.h"
 32#include "../v3math.h"
 33#include "../v4math.h"
 34#include "../m4math.h"
 35#include "../llquaternion.h"
 36#include "../v3dmath.h"
 37
 38#include "../test/lltut.h"
 39
 40#if LL_WINDOWS
 41// disable unreachable code warnings caused by usage of skip.
 42#pragma warning(disable: 4702)
 43#endif
 44
 45#if LL_WINDOWS
 46// disable unreachable code warnings caused by usage of skip.
 47#pragma warning(disable: 4702)
 48#endif
 49
 50namespace tut
 51{
 52	struct m3math_test
 53	{
 54	};
 55	typedef test_group<m3math_test> m3math_test_t;
 56	typedef m3math_test_t::object m3math_test_object_t;
 57	tut::m3math_test_t tut_m3math_test("m3math_h");
 58
 59	//test case for setIdentity() fn.
 60	template<> template<>
 61	void m3math_test_object_t::test<1>()
 62	{
 63		LLMatrix3 llmat3_obj;
 64		llmat3_obj.setIdentity();
 65		ensure("LLMatrix3::setIdentity failed", 1.f == llmat3_obj.mMatrix[0][0] &&
 66					0.f == llmat3_obj.mMatrix[0][1] &&
 67					0.f == llmat3_obj.mMatrix[0][2] &&
 68					0.f == llmat3_obj.mMatrix[1][0] &&
 69					1.f == llmat3_obj.mMatrix[1][1] &&
 70					0.f == llmat3_obj.mMatrix[1][2] &&
 71					0.f == llmat3_obj.mMatrix[2][0] &&
 72					0.f == llmat3_obj.mMatrix[2][1] &&
 73					1.f == llmat3_obj.mMatrix[2][2]);
 74	}
 75
 76	//test case for LLMatrix3& setZero() fn.
 77	template<> template<>
 78	void m3math_test_object_t::test<2>()
 79	{
 80		LLMatrix3 llmat3_obj(30, 1, 2, 3);
 81		llmat3_obj.setZero();
 82
 83		ensure("LLMatrix3::setZero failed", 0.f == llmat3_obj.setZero().mMatrix[0][0] &&
 84					0.f == llmat3_obj.setZero().mMatrix[0][1] &&
 85					0.f == llmat3_obj.setZero().mMatrix[0][2] &&
 86					0.f == llmat3_obj.setZero().mMatrix[1][0] &&
 87					0.f == llmat3_obj.setZero().mMatrix[1][1] &&
 88					0.f == llmat3_obj.setZero().mMatrix[1][2] &&
 89					0.f == llmat3_obj.setZero().mMatrix[2][0] &&
 90					0.f == llmat3_obj.setZero().mMatrix[2][1] &&
 91					0.f == llmat3_obj.setZero().mMatrix[2][2]);
 92	}
 93
 94	//test case for setRows(const LLVector3 &x_axis, const LLVector3 &y_axis, const LLVector3 &z_axis) fns.
 95	template<> template<>
 96	void m3math_test_object_t::test<3>()
 97	{
 98		LLMatrix3 llmat3_obj;
 99		LLVector3 vect1(2, 1, 4);
100		LLVector3 vect2(3, 5, 7);
101		LLVector3 vect3(6, 9, 7);
102		llmat3_obj.setRows(vect1, vect2, vect3);
103		ensure("LLVector3::setRows failed ", 2 == llmat3_obj.mMatrix[0][0] &&
104						1 == llmat3_obj.mMatrix[0][1] &&
105						4 == llmat3_obj.mMatrix[0][2] &&
106						3 == llmat3_obj.mMatrix[1][0] &&
107						5 == llmat3_obj.mMatrix[1][1] &&
108						7 == llmat3_obj.mMatrix[1][2] &&
109						6 == llmat3_obj.mMatrix[2][0] &&
110						9 == llmat3_obj.mMatrix[2][1] &&
111						7 == llmat3_obj.mMatrix[2][2]);
112	}
113
114	//test case for getFwdRow(), getLeftRow(), getUpRow() fns.
115	template<> template<>
116	void m3math_test_object_t::test<4>()
117	{
118		LLMatrix3 llmat3_obj;
119		LLVector3 vect1(2, 1, 4);
120		LLVector3 vect2(3, 5, 7);
121		LLVector3 vect3(6, 9, 7);
122		llmat3_obj.setRows(vect1, vect2, vect3);
123		
124		ensure("LLVector3::getFwdRow failed ", vect1 == llmat3_obj.getFwdRow());
125		ensure("LLVector3::getLeftRow failed ", vect2 == llmat3_obj.getLeftRow());
126		ensure("LLVector3::getUpRow failed ", vect3 == llmat3_obj.getUpRow());
127	}
128
129	//test case for operator*(const LLMatrix3 &a, const LLMatrix3 &b)
130	template<> template<>
131	void m3math_test_object_t::test<5>()
132	{
133		LLMatrix3 llmat_obj1;
134		LLMatrix3 llmat_obj2;		
135		LLMatrix3 llmat_obj3;
136		
137		LLVector3 llvec1(1, 3, 5);
138		LLVector3 llvec2(3, 6, 1);
139		LLVector3 llvec3(4, 6, 9);
140
141		LLVector3 llvec4(1, 1, 5);
142		LLVector3 llvec5(3, 6, 8);
143		LLVector3 llvec6(8, 6, 2);
144
145		LLVector3 llvec7(0, 0, 0);
146		LLVector3 llvec8(0, 0, 0);
147		LLVector3 llvec9(0, 0, 0);
148		
149		llmat_obj1.setRows(llvec1, llvec2, llvec3);
150		llmat_obj2.setRows(llvec4, llvec5, llvec6);
151		llmat_obj3.setRows(llvec7, llvec8, llvec9);
152		llmat_obj3 = llmat_obj1 * llmat_obj2;
153		ensure("LLMatrix3::operator*(const LLMatrix3 &a, const LLMatrix3 &b) failed", 
154						50 == llmat_obj3.mMatrix[0][0] &&
155						49 == llmat_obj3.mMatrix[0][1] &&
156						39 == llmat_obj3.mMatrix[0][2] &&
157						29 == llmat_obj3.mMatrix[1][0] &&
158						45 == llmat_obj3.mMatrix[1][1] &&
159						65 == llmat_obj3.mMatrix[1][2] &&
160						94 == llmat_obj3.mMatrix[2][0] &&
161						94 == llmat_obj3.mMatrix[2][1] &&
162						86 == llmat_obj3.mMatrix[2][2]);
163	}
164
165
166	//test case for operator*(const LLVector3 &a, const LLMatrix3 &b)
167	template<> template<>
168	void m3math_test_object_t::test<6>()
169	{
170				
171		LLMatrix3 llmat_obj1;
172		
173		LLVector3 llvec(1, 3, 5);
174		LLVector3 res_vec(0, 0, 0);
175		LLVector3 llvec1(1, 3, 5);
176		LLVector3 llvec2(3, 6, 1);
177		LLVector3 llvec3(4, 6, 9);
178		
179		llmat_obj1.setRows(llvec1, llvec2, llvec3);
180		res_vec = llvec * llmat_obj1;
181
182		LLVector3 expected_result(30, 51, 53);
183
184		ensure("LLMatrix3::operator*(const LLVector3 &a, const LLMatrix3 &b) failed", res_vec == expected_result);
185	}
186
187	//test case for operator*(const LLVector3d &a, const LLMatrix3 &b) 
188	template<> template<>
189	void m3math_test_object_t::test<7>()
190	{
191		LLMatrix3 llmat_obj1;
192		LLVector3d llvec3d1;		
193		LLVector3d llvec3d2(0, 3, 4);
194
195		LLVector3 llvec1(1, 3, 5);
196		LLVector3 llvec2(3, 2, 1);
197		LLVector3 llvec3(4, 6, 0);
198		
199		llmat_obj1.setRows(llvec1, llvec2, llvec3);
200		llvec3d1 = llvec3d2 * llmat_obj1;
201
202		LLVector3d expected_result(25, 30, 3);
203		
204		ensure("LLMatrix3::operator*(const LLVector3 &a, const LLMatrix3 &b) failed", llvec3d1 == expected_result);
205	}
206
207	// test case for operator==(const LLMatrix3 &a, const LLMatrix3 &b)
208	template<> template<>
209	void m3math_test_object_t::test<8>()
210	{
211		LLMatrix3 llmat_obj1;
212		LLMatrix3 llmat_obj2;		
213		
214		LLVector3 llvec1(1, 3, 5);
215		LLVector3 llvec2(3, 6, 1);
216		LLVector3 llvec3(4, 6, 9);
217
218		llmat_obj1.setRows(llvec1, llvec2, llvec3);
219		llmat_obj2.setRows(llvec1, llvec2, llvec3);
220		ensure("LLMatrix3::operator==(const LLMatrix3 &a, const LLMatrix3 &b) failed", llmat_obj1 == llmat_obj2);
221
222		llmat_obj2.setRows(llvec2, llvec2, llvec3);
223		ensure("LLMatrix3::operator!=(const LLMatrix3 &a, const LLMatrix3 &b) failed", llmat_obj1 != llmat_obj2);
224	}
225
226	//test case for quaternion() fn.
227	template<> template<>
228	void m3math_test_object_t::test<9>()
229	{
230		LLMatrix3 llmat_obj1;
231		LLQuaternion llmat_quat;		
232		
233		LLVector3 llmat1(2.0f, 1.0f, 6.0f);
234		LLVector3 llmat2(1.0f, 1.0f, 3.0f);
235		LLVector3 llmat3(1.0f, 7.0f, 5.0f);
236
237		llmat_obj1.setRows(llmat1, llmat2, llmat3);
238		llmat_quat = llmat_obj1.quaternion();
239		ensure("LLMatrix3::quaternion failed ", is_approx_equal(-0.66666669f, llmat_quat.mQ[0]) &&
240						is_approx_equal(-0.83333337f, llmat_quat.mQ[1]) &&
241						is_approx_equal(0.0f, llmat_quat.mQ[2]) &&
242						is_approx_equal(1.5f, llmat_quat.mQ[3]));
243	}
244
245	//test case for transpose() fn.
246	template<> template<>
247	void m3math_test_object_t::test<10>()
248	{
249		LLMatrix3 llmat_obj;
250	
251		LLVector3 llvec1(1, 2, 3);
252		LLVector3 llvec2(3, 2, 1);
253		LLVector3 llvec3(2, 2, 2);
254
255		llmat_obj.setRows(llvec1, llvec2, llvec3);
256		llmat_obj.transpose();
257
258		LLVector3 resllvec1(1, 3, 2);
259		LLVector3 resllvec2(2, 2, 2);
260		LLVector3 resllvec3(3, 1, 2);
261		LLMatrix3 expectedllmat_obj;
262		expectedllmat_obj.setRows(resllvec1, resllvec2, resllvec3);
263
264		ensure("LLMatrix3::transpose failed ", llmat_obj == expectedllmat_obj);
265	}
266	
267	//test case for determinant() fn.
268	template<> template<>
269	void m3math_test_object_t::test<11>()
270	{
271		LLMatrix3 llmat_obj1;
272		
273		LLVector3 llvec1(1, 2, 3);
274		LLVector3 llvec2(3, 2, 1);
275		LLVector3 llvec3(2, 2, 2);
276		llmat_obj1.setRows(llvec1, llvec2, llvec3);
277		ensure("LLMatrix3::determinant failed ",  0.0f == llmat_obj1.determinant());
278	}
279
280	//test case for orthogonalize() fn.
281	template<> template<>
282	void m3math_test_object_t::test<12>()
283	{
284		LLMatrix3 llmat_obj;
285
286		LLVector3 llvec1(1, 4, 3);
287		LLVector3 llvec2(1, 2, 0);
288		LLVector3 llvec3(2, 4, 2);
289
290        skip("This test fails depending on architecture. Need to fix comparison operation, is_approx_equal, to work on more than one platform.");
291
292		llmat_obj.setRows(llvec1, llvec2, llvec3);
293		llmat_obj.orthogonalize();
294
295		ensure("LLMatrix3::orthogonalize failed ",
296		       is_approx_equal(0.19611614f, llmat_obj.mMatrix[0][0]) &&
297		       is_approx_equal(0.78446454f, llmat_obj.mMatrix[0][1]) &&
298		       is_approx_equal(0.58834841f, llmat_obj.mMatrix[0][2]) &&
299		       is_approx_equal(0.47628204f, llmat_obj.mMatrix[1][0]) &&
300		       is_approx_equal(0.44826545f, llmat_obj.mMatrix[1][1]) &&
301		       is_approx_equal(-0.75644795f, llmat_obj.mMatrix[1][2]) &&
302		       is_approx_equal(-0.85714286f, llmat_obj.mMatrix[2][0]) &&
303		       is_approx_equal(0.42857143f, llmat_obj.mMatrix[2][1]) &&
304		       is_approx_equal(-0.28571429f, llmat_obj.mMatrix[2][2]));
305	}
306
307	//test case for adjointTranspose() fn.
308	template<> template<>
309	void m3math_test_object_t::test<13>()
310	{
311		LLMatrix3 llmat_obj;
312
313		LLVector3 llvec1(3, 2, 1);
314		LLVector3 llvec2(6, 2, 1);
315		LLVector3 llvec3(3, 6, 8);
316
317		llmat_obj.setRows(llvec1, llvec2, llvec3);
318		llmat_obj.adjointTranspose();
319		
320		ensure("LLMatrix3::adjointTranspose failed ", 10 == llmat_obj.mMatrix[0][0] &&
321						-45 == llmat_obj.mMatrix[1][0] &&
322						30 == llmat_obj.mMatrix[2][0] &&
323						-10 == llmat_obj.mMatrix[0][1] &&
324						21 == llmat_obj.mMatrix[1][1] &&
325						-12 == llmat_obj.mMatrix[2][1] &&
326						0  == llmat_obj.mMatrix[0][2] &&
327						3 == llmat_obj.mMatrix[1][2] &&
328						-6 == llmat_obj.mMatrix[2][2]);
329	}
330
331	/* TBD: Need to add test cases for getEulerAngles() and setRot() functions */
332}