PageRenderTime 71ms CodeModel.GetById 24ms app.highlight 43ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llmath/tests/xform_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 245 lines | 168 code | 34 blank | 43 comment | 47 complexity | d725eeb1a1ed2ed85b957a4c36e9cd96 MD5 | raw file
  1/** 
  2 * @file xform_test.cpp
  3 * @author Adroit
  4 * @date March 2007 
  5 * @brief Test cases for LLXform
  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 "../test/lltut.h"
 31
 32#include "../xform.h"
 33
 34namespace tut
 35{
 36	struct xform_test
 37	{
 38	};
 39	typedef test_group<xform_test> xform_test_t;
 40	typedef xform_test_t::object xform_test_object_t;
 41	tut::xform_test_t tut_xform_test("LLXForm");
 42
 43	//test case for init(), getParent(), getRotation(), getPositionW(), getWorldRotation() fns.
 44	template<> template<>
 45	void xform_test_object_t::test<1>()
 46	{
 47		LLXform xform_obj;
 48		LLVector3 emptyVec(0.f,0.f,0.f);
 49		LLVector3 initialScaleVec(1.f,1.f,1.f);
 50
 51		ensure("LLXform empty constructor failed: ", !xform_obj.getParent() && !xform_obj.isChanged() &&
 52			xform_obj.getPosition() == emptyVec && 
 53			(xform_obj.getRotation()).isIdentity() &&
 54			xform_obj.getScale() == initialScaleVec && 
 55			xform_obj.getPositionW() == emptyVec && 
 56			(xform_obj.getWorldRotation()).isIdentity() &&
 57			!xform_obj.getScaleChildOffset());
 58	}
 59
 60	// test cases for 
 61	// setScale(const LLVector3& scale) 
 62	// setScale(const F32 x, const F32 y, const F32 z)
 63	// setRotation(const F32 x, const F32 y, const F32 z) 
 64	// setPosition(const F32 x, const F32 y, const F32 z) 
 65	// getLocalMat4(LLMatrix4 &mat)
 66	template<> template<>
 67	void xform_test_object_t::test<2>()	
 68	{
 69		LLMatrix4 llmat4;
 70		LLXform xform_obj;
 71
 72		F32 x = 3.6f;
 73		F32 y = 5.5f;
 74		F32 z = 4.2f;
 75		F32 w = 0.f;
 76		F32 posz = z + 2.122f;
 77		LLVector3 vec(x, y, z);
 78		xform_obj.setScale(x, y, z);
 79		xform_obj.setPosition(x, y, posz);
 80		ensure("setScale failed: ", xform_obj.getScale() == vec);
 81
 82		vec.setVec(x, y, posz);
 83		ensure("getPosition failed: ", xform_obj.getPosition() == vec);
 84
 85		x = x * 2.f;
 86		y = y + 2.3f;
 87		z = posz * 4.f; 
 88		vec.setVec(x, y, z);
 89		xform_obj.setPositionX(x);
 90		xform_obj.setPositionY(y);
 91		xform_obj.setPositionZ(z);
 92		ensure("setPositionX/Y/Z failed: ", xform_obj.getPosition() == vec);
 93
 94		xform_obj.setScaleChildOffset(TRUE);
 95		ensure("setScaleChildOffset failed: ", xform_obj.getScaleChildOffset());
 96
 97		vec.setVec(x, y, z);
 98
 99		xform_obj.addPosition(vec);
100		vec += vec;
101		ensure("addPosition failed: ", xform_obj.getPosition() == vec);
102
103		xform_obj.setScale(vec);
104		ensure("setScale vector failed: ", xform_obj.getScale() == vec);
105
106		LLQuaternion quat(x, y, z, w);
107		xform_obj.setRotation(quat);
108		ensure("setRotation quat failed: ", xform_obj.getRotation() == quat);
109
110		xform_obj.setRotation(x, y, z, w);
111		ensure("getRotation 2 failed: ", xform_obj.getRotation() == quat);
112
113		xform_obj.setRotation(x, y, z);
114		quat.setQuat(x,y,z); 
115		ensure("setRotation xyz failed: ", xform_obj.getRotation() == quat);
116
117		// LLXform::setRotation(const F32 x, const F32 y, const F32 z) 
118		//		Does normalization
119		// LLXform::setRotation(const F32 x, const F32 y, const F32 z, const F32 s) 
120		//		Simply copies the individual values - does not do any normalization. 
121		// Is that the expected behavior?
122	}
123
124	// test cases for inline BOOL setParent(LLXform *parent) and getParent() fn.
125	template<> template<>
126	void xform_test_object_t::test<3>()	
127	{		
128		LLXform xform_obj;
129		LLXform par;
130		LLXform grandpar;
131		xform_obj.setParent(&par); 
132		par.setParent(&grandpar); 
133		ensure("setParent/getParent failed: ", &par == xform_obj.getParent());
134		ensure("getRoot failed: ", &grandpar == xform_obj.getRoot());
135		ensure("isRoot failed: ", grandpar.isRoot() && !par.isRoot() && !xform_obj.isRoot());
136		ensure("isRootEdit failed: ", grandpar.isRootEdit() && !par.isRootEdit() && !xform_obj.isRootEdit());
137	}
138
139	template<> template<>
140	void xform_test_object_t::test<4>()	
141	{
142		LLXform xform_obj;
143		xform_obj.setChanged(LLXform::TRANSLATED | LLXform::ROTATED | LLXform::SCALED);
144		ensure("setChanged/isChanged failed: ", xform_obj.isChanged());
145
146		xform_obj.clearChanged(LLXform::TRANSLATED | LLXform::ROTATED | LLXform::SCALED);
147		ensure("clearChanged failed: ", !xform_obj.isChanged());
148		
149		LLVector3 llvect3(12.4f, -5.6f, 0.34f);
150		xform_obj.setScale(llvect3);
151		ensure("setScale did not set SCALED flag: ", xform_obj.isChanged(LLXform::SCALED));
152		xform_obj.setPosition(1.2f, 2.3f, 3.4f);
153		ensure("setScale did not set TRANSLATED flag: ", xform_obj.isChanged(LLXform::TRANSLATED));
154		ensure("TRANSLATED reset SCALED flag: ", xform_obj.isChanged(LLXform::TRANSLATED | LLXform::SCALED));
155		xform_obj.clearChanged(LLXform::SCALED);
156		ensure("reset SCALED failed: ", !xform_obj.isChanged(LLXform::SCALED));
157		xform_obj.setRotation(1, 2, 3, 4);
158		ensure("ROTATION flag not set ", xform_obj.isChanged(LLXform::TRANSLATED | LLXform::ROTATED));
159		xform_obj.setScale(llvect3);
160		ensure("ROTATION flag not set ", xform_obj.isChanged(LLXform::MOVED));
161	}
162
163	//to test init() and getWorldMatrix() fns.
164	template<> template<>
165	void xform_test_object_t::test<5>()	
166	{
167		LLXformMatrix formMatrix_obj;
168		formMatrix_obj.init();
169		LLMatrix4 mat4_obj;
170		
171		ensure("1. The value is not NULL", 1.f == formMatrix_obj.getWorldMatrix().mMatrix[0][0]);
172		ensure("2. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[0][1]);
173		ensure("3. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[0][2]);
174		ensure("4. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[0][3]);
175		ensure("5. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[1][0]);
176		ensure("6. The value is not NULL", 1.f == formMatrix_obj.getWorldMatrix().mMatrix[1][1]);
177		ensure("7. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[1][2]);
178		ensure("8. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[1][3]);
179		ensure("9. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[2][0]);
180		ensure("10. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[2][1]);
181		ensure("11. The value is not NULL", 1.f == formMatrix_obj.getWorldMatrix().mMatrix[2][2]);
182		ensure("12. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[2][3]);
183		ensure("13. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[3][0]);
184		ensure("14. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[3][1]);
185		ensure("15. The value is not NULL", 0.f == formMatrix_obj.getWorldMatrix().mMatrix[3][2]);
186		ensure("16. The value is not NULL", 1.f == formMatrix_obj.getWorldMatrix().mMatrix[3][3]);
187	}
188
189	//to test mMin.clearVec() and mMax.clearVec() fns
190	template<> template<>
191	void xform_test_object_t::test<6>()	
192	{
193		LLXformMatrix formMatrix_obj;
194		formMatrix_obj.init();
195		LLVector3 llmin_vec3;
196		LLVector3 llmax_vec3;
197		formMatrix_obj.getMinMax(llmin_vec3, llmax_vec3);
198		ensure("1. The value is not NULL", 0.f == llmin_vec3.mV[0]);
199		ensure("2. The value is not NULL", 0.f == llmin_vec3.mV[1]);
200		ensure("3. The value is not NULL", 0.f == llmin_vec3.mV[2]);
201		ensure("4. The value is not NULL", 0.f == llmin_vec3.mV[0]);
202		ensure("5. The value is not NULL", 0.f == llmin_vec3.mV[1]);
203		ensure("6. The value is not NULL", 0.f == llmin_vec3.mV[2]);
204	}
205
206	//test case of update() fn.
207	template<> template<>
208	void xform_test_object_t::test<7>()	
209	{
210		LLXformMatrix formMatrix_obj;
211
212		LLXformMatrix parent;
213		LLVector3 llvecpos(1.0, 2.0, 3.0);
214		LLVector3 llvecpospar(10.0, 20.0, 30.0);
215		formMatrix_obj.setPosition(llvecpos);
216		parent.setPosition(llvecpospar);
217
218		LLVector3 llvecparentscale(1.0, 2.0, 0);
219		parent.setScaleChildOffset(TRUE);
220		parent.setScale(llvecparentscale);
221
222		LLQuaternion quat(1, 2, 3, 4);
223		LLQuaternion quatparent(5, 6, 7, 8);
224		formMatrix_obj.setRotation(quat);
225		parent.setRotation(quatparent);
226		formMatrix_obj.setParent(&parent);
227
228		parent.update();
229		formMatrix_obj.update();
230
231		LLVector3 worldPos = llvecpos;
232		worldPos.scaleVec(llvecparentscale);
233		worldPos *= quatparent;
234		worldPos += llvecpospar;
235
236		LLQuaternion worldRot = quat * quatparent; 
237
238		ensure("getWorldPosition failed: ", formMatrix_obj.getWorldPosition() == worldPos);
239		ensure("getWorldRotation failed: ", formMatrix_obj.getWorldRotation() == worldRot);
240
241		ensure("getWorldPosition for parent failed: ", parent.getWorldPosition() == llvecpospar);
242		ensure("getWorldRotation for parent failed: ", parent.getWorldRotation() == quatparent);
243	}
244}	
245