PageRenderTime 37ms CodeModel.GetById 2ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/tests/llbbox_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 367 lines | 213 code | 79 blank | 75 comment | 10 complexity | 36b19f289330b9ddec2551978e53f09d MD5 | raw file
  1/**
  2 * @file   llbbox_test.cpp
  3 * @author Martin Reddy
  4 * @date   2009-06-25
  5 * @brief  Test for llbbox.cpp.
  6 * 
  7 * $LicenseInfo:firstyear=2009&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 "../test/lltut.h"
 32
 33#include "../llbbox.h"
 34
 35
 36#define ANGLE                (3.14159265f / 2.0f)
 37#define APPROX_EQUAL(a, b)   (dist_vec_squared((a),(b)) < 1e-10)
 38
 39namespace tut
 40{
 41	struct LLBBoxData
 42	{
 43	};
 44
 45	typedef test_group<LLBBoxData> factory;
 46	typedef factory::object object;
 47}
 48
 49namespace
 50{
 51	tut::factory llbbox_test_factory("LLBBox");
 52}
 53
 54namespace tut
 55{
 56	template<> template<>
 57	void object::test<1>()
 58	{	
 59		//
 60		// test the default constructor
 61		//
 62		
 63		LLBBox bbox1;
 64		
 65		ensure_equals("Default bbox min", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, 0.0f));
 66		ensure_equals("Default bbox max", bbox1.getMaxLocal(), LLVector3(0.0f, 0.0f, 0.0f));
 67		ensure_equals("Default bbox pos agent", bbox1.getPositionAgent(), LLVector3(0.0f, 0.0f, 0.0f));
 68		ensure_equals("Default bbox rotation", bbox1.getRotation(), LLQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
 69	}
 70	
 71	template<> template<>
 72	void object::test<2>()
 73	{	
 74		//
 75		// test the non-default constructor
 76		//
 77		
 78		LLBBox bbox2(LLVector3(1.0f, 2.0f, 3.0f), LLQuaternion(),
 79					 LLVector3(2.0f, 3.0f, 4.0f), LLVector3(4.0f, 5.0f, 6.0f));
 80		
 81		ensure_equals("Custom bbox min", bbox2.getMinLocal(), LLVector3(2.0f, 3.0f, 4.0f));
 82		ensure_equals("Custom bbox max", bbox2.getMaxLocal(), LLVector3(4.0f, 5.0f, 6.0f));
 83		ensure_equals("Custom bbox pos agent", bbox2.getPositionAgent(), LLVector3(1.0f, 2.0f, 3.0f));
 84		ensure_equals("Custom bbox rotation", bbox2.getRotation(), LLQuaternion(0.0f, 0.0f, 0.0f, 1.0f));		
 85	}
 86		
 87	template<> template<>
 88	void object::test<3>()
 89	{	
 90		//
 91		// test the setMinLocal() method
 92		//
 93		LLBBox bbox2;
 94		bbox2.setMinLocal(LLVector3(3.0f, 3.0f, 3.0f));
 95		ensure_equals("Custom bbox min (2)", bbox2.getMinLocal(), LLVector3(3.0f, 3.0f, 3.0f));
 96	}
 97
 98	template<> template<>
 99	void object::test<4>()
100	{	
101		//
102		// test the setMaxLocal() method
103		//
104		LLBBox bbox2;
105		bbox2.setMaxLocal(LLVector3(5.0f, 5.0f, 5.0f));
106		ensure_equals("Custom bbox max (2)", bbox2.getMaxLocal(), LLVector3(5.0f, 5.0f, 5.0f));
107	}
108
109	template<> template<>
110	void object::test<5>()
111	{	
112		//
113		// test the getCenterLocal() method
114		//
115		
116		ensure_equals("Default bbox local center", LLBBox().getCenterLocal(), LLVector3(0.0f, 0.0f, 0.0f));
117		
118		LLBBox bbox1(LLVector3(1.0f, 2.0f, 3.0f), LLQuaternion(),
119					 LLVector3(2.0f, 4.0f, 6.0f), LLVector3(4.0f, 6.0f, 8.0f));
120
121		ensure_equals("Custom bbox center local", bbox1.getCenterLocal(), LLVector3(3.0f, 5.0f, 7.0f));
122
123		LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(0.0f, 0.0f, 1.0f)),
124					 LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
125
126		ensure_equals("Custom bbox center local with rot", bbox2.getCenterLocal(), LLVector3(3.0f, 3.0f, 3.0f));
127	}
128
129	template<> template<>
130	void object::test<6>()
131	{	
132		//
133		// test the getCenterAgent()
134		//
135		
136		ensure_equals("Default bbox agent center", LLBBox().getCenterAgent(), LLVector3(0.0f, 0.0f, 0.0f));
137		
138		LLBBox bbox1(LLVector3(1.0f, 2.0f, 3.0f), LLQuaternion(),
139					 LLVector3(2.0f, 4.0f, 6.0f), LLVector3(4.0f, 6.0f, 8.0f));
140		
141		ensure_equals("Custom bbox center agent", bbox1.getCenterAgent(), LLVector3(4.0f, 7.0f, 10.0f));
142		
143		LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(0.0f, 0.0f, 1.0f)),
144					 LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
145		
146		ensure("Custom bbox center agent with rot", APPROX_EQUAL(bbox2.getCenterAgent(), LLVector3(-2.0f, 4.0f, 4.0f)));
147	}
148	
149	template<> template<>
150	void object::test<7>()
151	{	
152		//
153		// test the getExtentLocal() method
154		//
155		
156		ensure_equals("Default bbox local extent", LLBBox().getExtentLocal(), LLVector3(0.0f, 0.0f, 0.0f));
157		
158		LLBBox bbox1(LLVector3(1.0f, 2.0f, 3.0f), LLQuaternion(),
159					 LLVector3(2.0f, 4.0f, 6.0f), LLVector3(4.0f, 6.0f, 8.0f));
160		
161		ensure_equals("Custom bbox extent local", bbox1.getExtentLocal(), LLVector3(2.0f, 2.0f, 2.0f));
162		
163		LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(0.0f, 0.0f, 1.0f)),
164					 LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
165		
166		ensure_equals("Custom bbox extent local with rot", bbox1.getExtentLocal(), LLVector3(2.0f, 2.0f, 2.0f));		
167	}
168	
169	template<> template<>
170	void object::test<8>()
171	{
172		//
173		// test the addPointLocal() method
174		//
175		
176		LLBBox bbox1;
177		bbox1.addPointLocal(LLVector3(1.0f, 1.0f, 1.0f));
178		bbox1.addPointLocal(LLVector3(3.0f, 3.0f, 3.0f));
179		
180		ensure_equals("addPointLocal center local (1)", bbox1.getCenterLocal(), LLVector3(2.0f, 2.0f, 2.0f));
181		ensure_equals("addPointLocal center agent (1)", bbox1.getCenterAgent(), LLVector3(2.0f, 2.0f, 2.0f));
182		ensure_equals("addPointLocal min (1)", bbox1.getMinLocal(), LLVector3(1.0f, 1.0f, 1.0f));
183		ensure_equals("addPointLocal max (1)", bbox1.getMaxLocal(), LLVector3(3.0f, 3.0f, 3.0f));
184		
185		bbox1.addPointLocal(LLVector3(0.0f, 0.0f, 0.0f));
186		bbox1.addPointLocal(LLVector3(1.0f, 1.0f, 1.0f));
187		bbox1.addPointLocal(LLVector3(2.0f, 2.0f, 2.0f));
188		
189		ensure_equals("addPointLocal center local (2)", bbox1.getCenterLocal(), LLVector3(1.5f, 1.5f, 1.5f));
190		ensure_equals("addPointLocal min (2)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, 0.0f));
191		ensure_equals("addPointLocal max (2)", bbox1.getMaxLocal(), LLVector3(3.0f, 3.0f, 3.0f));		
192	}
193
194	template<> template<>
195	void object::test<9>()
196	{
197		//
198		// test the addBBoxLocal() method
199		//
200		
201		LLBBox bbox1;
202		bbox1.addBBoxLocal(LLBBox(LLVector3(), LLQuaternion(),
203								  LLVector3(0.0f, 0.0f, 0.0f), LLVector3(3.0f, 3.0f, 3.0f)));
204		
205		ensure_equals("addPointLocal center local (3)", bbox1.getCenterLocal(), LLVector3(1.5f, 1.5f, 1.5f));
206		ensure_equals("addPointLocal min (3)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, 0.0f));
207		ensure_equals("addPointLocal max (3)", bbox1.getMaxLocal(), LLVector3(3.0f, 3.0f, 3.0f));
208		
209		bbox1.addBBoxLocal(LLBBox(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
210								  LLVector3(5.0f, 5.0f, 5.0f), LLVector3(10.0f, 10.0f, 10.0f)));
211		
212		ensure_equals("addPointLocal center local (4)", bbox1.getCenterLocal(), LLVector3(5.0f, 5.0f, 5.0f));
213		ensure_equals("addPointLocal center agent (4)", bbox1.getCenterAgent(), LLVector3(5.0f, 5.0f, 5.0f));
214		ensure_equals("addPointLocal min (4)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, 0.0f));
215		ensure_equals("addPointLocal max (4)", bbox1.getMaxLocal(), LLVector3(10.0f, 10.0f, 10.0f));		
216	}
217	
218	template<> template<>
219	void object::test<10>()
220	{
221		//
222		// test the addPointAgent() method
223		//
224		
225		LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(1.0, 0.0, 0.0, 1.0),
226					 LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
227
228		bbox1.addPointAgent(LLVector3(1.0f, 1.0f, 1.0f));
229		bbox1.addPointAgent(LLVector3(3.0f, 3.0f, 3.0f));
230		
231		ensure_equals("addPointAgent center local (1)", bbox1.getCenterLocal(), LLVector3(2.0f, 2.0f, -2.0f));
232		ensure_equals("addPointAgent center agent (1)", bbox1.getCenterAgent(), LLVector3(3.0f, 3.0f, 7.0f));
233		ensure_equals("addPointAgent min (1)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, -4.0f));
234		ensure_equals("addPointAgent max (1)", bbox1.getMaxLocal(), LLVector3(4.0f, 4.0f, 0.0f));
235	}
236
237	template<> template<>
238	void object::test<11>()
239	{
240		//
241		// test the addBBoxAgent() method
242		//
243		
244		LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(1.0, 0.0, 0.0, 1.0),
245					 LLVector3(2.0f, 2.0f, 2.0f), LLVector3(4.0f, 4.0f, 4.0f));
246		
247		bbox1.addPointAgent(LLVector3(1.0f, 1.0f, 1.0f));
248		bbox1.addPointAgent(LLVector3(3.0f, 3.0f, 3.0f));
249		
250		bbox1.addBBoxLocal(LLBBox(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
251								  LLVector3(5.0f, 5.0f, 5.0f), LLVector3(10.0f, 10.0f, 10.0f)));
252		
253		ensure_equals("addPointAgent center local (2)", bbox1.getCenterLocal(), LLVector3(5.0f, 5.0f, 3.0f));
254		ensure_equals("addPointAgent center agent (2)", bbox1.getCenterAgent(), LLVector3(6.0f, -10.0f, 8.0f));
255		ensure_equals("addPointAgent min (2)", bbox1.getMinLocal(), LLVector3(0.0f, 0.0f, -4.0f));
256		ensure_equals("addPointAgent max (2)", bbox1.getMaxLocal(), LLVector3(10.0f, 10.0f, 10.0f));		
257	}
258	
259	template<> template<>
260	void object::test<12>()
261	{
262		//
263		// test the expand() method
264		//
265		
266		LLBBox bbox1;
267		bbox1.expand(0.0);
268
269		ensure_equals("Zero-expanded Default BBox center", bbox1.getCenterLocal(), LLVector3(0.0f, 0.0f, 0.0f));
270
271		LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
272					 LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
273		bbox2.expand(0.0);
274		
275		ensure_equals("Zero-expanded center local", bbox2.getCenterLocal(), LLVector3(2.0f, 2.0f, 2.0f));
276		ensure_equals("Zero-expanded center agent", bbox2.getCenterAgent(), LLVector3(3.0f, 3.0f, 3.0f));
277		ensure_equals("Zero-expanded min", bbox2.getMinLocal(), LLVector3(1.0f, 1.0f, 1.0f));
278		ensure_equals("Zero-expanded max", bbox2.getMaxLocal(), LLVector3(3.0f, 3.0f, 3.0f));
279
280		bbox2.expand(0.5);
281
282		ensure_equals("Positive-expanded center", bbox2.getCenterLocal(), LLVector3(2.0f, 2.0f, 2.0f));
283		ensure_equals("Positive-expanded min", bbox2.getMinLocal(), LLVector3(0.5f, 0.5f, 0.5f));
284		ensure_equals("Positive-expanded max", bbox2.getMaxLocal(), LLVector3(3.5f, 3.5f, 3.5f));
285
286		bbox2.expand(-1.0);
287		
288		ensure_equals("Negative-expanded center", bbox2.getCenterLocal(), LLVector3(2.0f, 2.0f, 2.0f));
289		ensure_equals("Negative-expanded min", bbox2.getMinLocal(), LLVector3(1.5f, 1.5f, 1.5f));
290		ensure_equals("Negative-expanded max", bbox2.getMaxLocal(), LLVector3(2.5f, 2.5f, 2.5f));		
291	}
292	
293	template<> template<>
294	void object::test<13>()
295	{
296		//
297		// test the localToAgent() method
298		//
299		
300		LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
301					 LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
302		
303		ensure_equals("localToAgent(1,2,3)", bbox1.localToAgent(LLVector3(1.0f, 2.0f, 3.0f)), LLVector3(2.0f, 3.0f, 4.0f));
304		
305		LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(1.0f, 0.0f, 0.0f)),
306					 LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
307		
308		ensure("localToAgent(1,2,3) rot", APPROX_EQUAL(bbox2.localToAgent(LLVector3(1.0f, 2.0f, 3.0f)), LLVector3(2.0f, -2.0f, 3.0f)));
309	}
310	
311	template<> template<>
312	void object::test<14>()
313	{
314		//
315		// test the agentToLocal() method
316		//
317		
318		LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
319					 LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
320		
321		ensure_equals("agentToLocal(1,2,3)", bbox1.agentToLocal(LLVector3(1.0f, 2.0f, 3.0f)), LLVector3(0.0f, 1.0f, 2.0f));
322		ensure_equals("agentToLocal(localToAgent)", bbox1.agentToLocal(bbox1.localToAgent(LLVector3(1.0f, 2.0f, 3.0f))),
323					  LLVector3(1.0f, 2.0f, 3.0f));
324		
325		LLBBox bbox2(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(ANGLE, LLVector3(1.0f, 0.0f, 0.0f)),
326					 LLVector3(1.0f, 1.0f, 1.0f), LLVector3(3.0f, 3.0f, 3.0f));
327		
328		ensure("agentToLocal(1,2,3) rot", APPROX_EQUAL(bbox2.agentToLocal(LLVector3(1.0f, 2.0f, 3.0f)), LLVector3(0.0f, 2.0f, -1.0f)));
329		ensure("agentToLocal(localToAgent) rot", APPROX_EQUAL(bbox2.agentToLocal(bbox2.localToAgent(LLVector3(1.0f, 2.0f, 3.0f))),
330															  LLVector3(1.0f, 2.0f, 3.0f)));
331	}
332	
333	template<> template<>
334	void object::test<15>()
335	{
336		//
337		// test the containsPointLocal() method
338		//
339		
340		LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
341					 LLVector3(1.0f, 2.0f, 3.0f), LLVector3(3.0f, 4.0f, 5.0f));
342
343		ensure("containsPointLocal(0,0,0)", bbox1.containsPointLocal(LLVector3(0.0f, 0.0f, 0.0f)) == FALSE);
344		ensure("containsPointLocal(1,2,3)", bbox1.containsPointLocal(LLVector3(1.0f, 2.0f, 3.0f)) == TRUE);
345		ensure("containsPointLocal(0.999,2,3)", bbox1.containsPointLocal(LLVector3(0.999f, 2.0f, 3.0f)) == FALSE);
346		ensure("containsPointLocal(3,4,5)", bbox1.containsPointLocal(LLVector3(3.0f, 4.0f, 5.0f)) == TRUE);
347		ensure("containsPointLocal(3,4,5.001)", bbox1.containsPointLocal(LLVector3(3.0f, 4.0f, 5.001f)) == FALSE);
348	}
349
350	template<> template<>
351	void object::test<16>()
352	{
353		//
354		// test the containsPointAgent() method
355		//
356		
357		LLBBox bbox1(LLVector3(1.0f, 1.0f, 1.0f), LLQuaternion(),
358					 LLVector3(1.0f, 2.0f, 3.0f), LLVector3(3.0f, 4.0f, 5.0f));
359		
360		ensure("containsPointAgent(0,0,0)", bbox1.containsPointAgent(LLVector3(0.0f, 0.0f, 0.0f)) == FALSE);
361		ensure("containsPointAgent(2,3,4)", bbox1.containsPointAgent(LLVector3(2.0f, 3.0f, 4.0f)) == TRUE);
362		ensure("containsPointAgent(2,2.999,4)", bbox1.containsPointAgent(LLVector3(2.0f, 2.999f, 4.0f)) == FALSE);
363		ensure("containsPointAgent(4,5,6)", bbox1.containsPointAgent(LLVector3(4.0f, 5.0f, 6.0f)) == TRUE);
364		ensure("containsPointAgent(4,5.001,6)", bbox1.containsPointAgent(LLVector3(4.0f, 5.001f, 6.0f)) == FALSE);
365	}				
366}
367