MOAB: Mesh Oriented datABase  (version 5.5.0)
homxform_test.cpp
Go to the documentation of this file.
1 /**
2  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
3  * storing and accessing finite element mesh data.
4  *
5  * Copyright 2004 Sandia Corporation. Under the terms of Contract
6  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
7  * retains certain rights in this software.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  */
15 
16 #include "moab/HomXform.hpp"
17 #include <cassert>
18 #include <iostream>
19 
20 using namespace moab;
21 
22 // unit test for the HomCoord and HomXform classes
23 //
24 int test_get_set();
29 
30 int main( int /*argc*/, char** /*argv*/ )
31 {
32  // first test HomCoord
33 
34  // constructors
35  // following shouldn't compile, since bare constructor is private
36 
37  int errors = 0;
38 
39  errors += test_get_set();
40  errors += test_coord_operators();
41  errors += test_xform_operators();
42  errors += test_xform_functions();
43  errors += test_coord_xform_operators();
44 
45  if( errors > 0 )
46  std::cout << errors << " errors found." << std::endl;
47  else
48  std::cout << "All tests passed." << std::endl;
49 
50  return errors;
51 }
52 
54 {
55  int errors = 0;
56 
57  // other constructors
58  int coordsa[4] = { 1, 2, 3, 1 };
59  int coordsb[4] = { 4, 3, 2, 1 };
60 
61  HomCoord coords1( coordsa );
62  HomCoord coords2( 4, 3, 2, 1 );
63  HomCoord coords3( 4, 3, 2 );
64  HomCoord coords4( 1, 1, 1, 1 );
65 
66  // set
67  coords2.set( coordsb );
68 
69  // hom_coord()
70  for( int i = 0; i < 4; i++ )
71  {
72  if( coords2.hom_coord()[i] != coordsb[i] )
73  {
74  std::cout << "Get test failed." << std::endl;
75  errors++;
76  }
77  }
78 
79  // ijkh
80  if( coords2.i() != coordsb[0] || coords2.j() != coordsb[1] || coords2.k() != coordsb[2] ||
81  coords2.h() != coordsb[3] )
82  {
83  std::cout << "ijkh test failed." << std::endl;
84  errors++;
85  }
86 
87  // set
88  coords2.set( 3, 3, 3 );
89 
90  // hom_coord()
91  if( coords2.hom_coord()[0] != 3 || coords2.hom_coord()[1] != 3 || coords2.hom_coord()[2] != 3 ||
92  coords2.hom_coord()[3] != 1 )
93  {
94  std::cout << "Set (int) test failed." << std::endl;
95  errors++;
96  }
97 
98  return errors;
99 }
100 
102 {
103  int errors = 0;
104 
105  HomCoord coords1( 1, 2, 3, 1 );
106  HomCoord coords2( 4, 3, 2, 1 );
107  HomCoord coords3( 4, 3, 2 );
108  HomCoord coords4( 1, 1, 1, 1 );
109 
110  // operator>=
111  bool optest = ( coords2 >= coords4 && coords2 >= coords3 && coords3 >= coords2 );
112  if( !optest )
113  {
114  std::cout << "Test failed for operator >=." << std::endl;
115  errors++;
116  }
117 
118  optest = ( coords4 <= coords2 && coords2 <= coords3 && coords3 <= coords2 );
119  if( !optest )
120  {
121  std::cout << "Test failed for operator <=." << std::endl;
122  errors++;
123  }
124 
125  // operator>
126  optest = ( coords2 > coords4 && !( coords2 > coords3 ) && !( coords3 > coords2 ) );
127  if( !optest )
128  {
129  std::cout << "Test failed for operator >." << std::endl;
130  errors++;
131  }
132 
133  optest = ( coords4 < coords2 && !( coords2 < coords3 ) && !( coords3 < coords2 ) );
134  if( !optest )
135  {
136  std::cout << "Test failed for operator <." << std::endl;
137  errors++;
138  }
139 
140  // operator[]
141  for( int i = 0; i < 3; i++ )
142  {
143  if( coords1[i] != coords2[3 - i] )
144  {
145  std::cout << "Test failed for operator[]." << std::endl;
146  errors++;
147  }
148  }
149 
150  // operator+
151  HomCoord coords5( 2 * coords1[0], 2 * coords1[1], 2 * coords1[2] );
152  HomCoord coords6 = coords1 + coords1;
153  if( coords5 != coords6 )
154  {
155  std::cout << "Test failed for operator+." << std::endl;
156  errors++;
157  }
158 
159  // operator-
160  if( coords5 - coords1 != coords1 )
161  {
162  std::cout << "Test failed for operator-." << std::endl;
163  errors++;
164  }
165 
166  return errors;
167 }
168 
170 {
171  int errors = 0;
172 
173  // integer constructor
174  int test_int[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
175  HomXform xform( test_int );
176  for( int i = 0; i < 16; i++ )
177  {
178  if( xform[i] != i )
179  {
180  std::cout << "HomXform integer array constructor failed." << std::endl;
181  errors++;
182  }
183  }
184 
185  HomXform xform3( test_int[0], test_int[1], test_int[2], test_int[3], test_int[4], test_int[5], test_int[6],
186  test_int[7], test_int[8], test_int[9], test_int[10], test_int[11], test_int[12], test_int[13],
187  test_int[14], test_int[15] );
188  for( int i = 0; i < 16; i++ )
189  {
190  if( xform3[i] != i )
191  {
192  std::cout << "HomXform integer constructor failed." << std::endl;
193  errors++;
194  }
195  }
196 
197  // sample rotation, translation, and scaling matrices/vectors
198  int rotate[] = { 0, 1, 0, -1, 0, 0, 0, 0, 1 };
199  int translate[] = { 4, 5, 6 };
200  int scale[] = { 1, 1, 1 };
201 
202  // construct an xform matrix based on those
203  HomXform xform2( rotate, scale, translate );
204 
205  // test where those went in the xform
206  if( !( xform2[XFORM_INDEX( 0, 0 )] == rotate[0] && xform2[XFORM_INDEX( 0, 1 )] == rotate[1] &&
207  xform2[XFORM_INDEX( 0, 2 )] == rotate[2] && xform2[XFORM_INDEX( 1, 0 )] == rotate[3] &&
208  xform2[XFORM_INDEX( 1, 1 )] == rotate[4] && xform2[XFORM_INDEX( 1, 2 )] == rotate[5] &&
209  xform2[XFORM_INDEX( 2, 0 )] == rotate[6] && xform2[XFORM_INDEX( 2, 1 )] == rotate[7] &&
210  xform2[XFORM_INDEX( 2, 2 )] == rotate[8] && xform2[XFORM_INDEX( 3, 0 )] == translate[0] &&
211  xform2[XFORM_INDEX( 3, 1 )] == translate[1] && xform2[XFORM_INDEX( 3, 2 )] == translate[2] &&
212  xform2[XFORM_INDEX( 0, 3 )] == 0 && xform2[XFORM_INDEX( 1, 3 )] == 0 && xform2[XFORM_INDEX( 2, 3 )] == 0 &&
213  xform2[XFORM_INDEX( 3, 3 )] == 1 ) )
214  {
215  std::cout << "HomXform rotate, scale, translate constructor failed." << std::endl;
216  errors++;
217  }
218 
219  return errors;
220 }
221 
223 {
224  int errors = 0;
225 
226  // sample rotation, translation, and scaling matrices/vectors
227  int rotate[] = { 0, 1, 0, -1, 0, 0, 0, 0, 1 };
228  int translate[] = { 4, 5, 6 };
229  int scale[] = { 1, 1, 1 };
230 
231  // construct an xform matrix based on those
232  HomXform xform1( rotate, scale, translate );
233  HomXform xform1a( rotate, scale, translate );
234 
235  // test operator==
236  if( !( xform1 == xform1a ) )
237  {
238  std::cout << "HomXform operator== failed." << std::endl;
239  errors++;
240  }
241 
242  // test operator!=
243  xform1a[1] = 0;
244  if( !( xform1 != xform1a ) )
245  {
246  std::cout << "HomXform operator!= failed." << std::endl;
247  errors++;
248  }
249 
250  // test operator=
251  HomXform xform1c = xform1;
252  if( !( xform1c == xform1 ) )
253  {
254  std::cout << "HomXform operator= failed." << std::endl;
255  errors++;
256  }
257 
258  HomXform xform3 = xform1 * HomXform::IDENTITY;
259  if( xform3 != xform1 )
260  {
261  std::cout << "HomXform operator * failed." << std::endl;
262  errors++;
263  }
264 
265  // test operator*=
266  xform3 *= HomXform::IDENTITY;
267  if( xform3 != xform1 )
268  {
269  std::cout << "HomXform operator *= failed." << std::endl;
270  errors++;
271  }
272 
273  return errors;
274 }
275 
277 {
278  int errors = 0;
279 
280  // HomCoord functions
281  // length() and length_squared()
282  HomCoord coord1( 3, 4, 5 );
283  if( coord1.length_squared() != 50 || coord1.length() != 7 )
284  {
285  std::cout << "HomCoord length() or length_squared() failed." << std::endl;
286  errors++;
287  }
288 
289  // normalize()
290  coord1.normalize();
291  HomCoord coord2( 3, 0, 0 );
292  coord2.normalize();
293  if( coord1.length_squared() != 0 || coord2.length_squared() != 1 )
294  {
295  std::cout << "HomCoord normalize failed." << std::endl;
296  errors++;
297  }
298 
299  // sample rotation, translation, and scaling matrices/vectors
300  int inv_int[] = { 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 7, 5, 0, 1 };
301 
302  // construct an xform matrix based on those
303  HomXform xform1( inv_int );
304 
305  HomXform xform1_inv = xform1.inverse();
306 
307  HomXform xform2 = xform1 * xform1_inv;
308  if( xform2 != HomXform::IDENTITY )
309  {
310  std::cout << "HomXform inverse failed." << std::endl;
311  errors++;
312  }
313 
314  return errors;
315 }
316 
318 {
319  int errors = 0;
320 
321  // sample pt
322  HomCoord test_pt( 1, 2, 3 );
323 
324  HomCoord test_pt2 = test_pt * HomXform::IDENTITY;
325  if( test_pt2 != test_pt )
326  {
327  std::cout << "Coord-xform operator* failed." << std::endl;
328  errors++;
329  }
330 
331  // get an inverse transform quickly
332  int rotate[] = { 0, 1, 0, -1, 0, 0, 0, 0, 1 };
333  int translate[] = { 4, 5, 6 };
334  int scale[] = { 1, 1, 1 };
335  HomXform xform2( rotate, scale, translate );
336 
337  // operator*
338  HomCoord ident( 1, 1, 1, 1 );
339  HomCoord test_pt3 = ident * HomXform::IDENTITY;
340  if( test_pt3 != ident )
341  {
342  std::cout << "Coord-xform operator* failed." << std::endl;
343  errors++;
344  }
345 
346  // operator/
347  test_pt2 = ( test_pt * xform2 ) / xform2;
348  if( test_pt2 != test_pt )
349  {
350  std::cout << "Coord-xform operator/ failed." << std::endl;
351  errors++;
352  }
353 
354  // test three_pt_xform; use known transforms in most cases
355  HomXform xform;
356 
357  // first test: i = j, j = -i, k = k, t = (7,5,0)
358  xform.three_pt_xform( HomCoord( 0, 0, 0, 1 ), HomCoord( 7, 5, 0, 1 ), HomCoord( 0, 3, 0, 1 ),
359  HomCoord( 4, 5, 0, 1 ), HomCoord( 0, 0, 3, 1 ), HomCoord( 7, 5, 3, 1 ) );
360 
361  HomXform solution( 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 7, 5, 0, 1 );
362  if( xform != solution )
363  {
364  std::cout << "Three-pt transform (general) test failed." << std::endl;
365  errors++;
366  }
367 
368  // now test 1d
369  xform.three_pt_xform( HomCoord( 0, 0, 0, 1 ), HomCoord( 7, 5, 0, 1 ), HomCoord( 6, 0, 0, 1 ),
370  HomCoord( 7, 11, 0, 1 ), HomCoord( 0, 0, 0, 1 ), HomCoord( 7, 5, 0, 1 ) );
371 
372  solution = HomXform( 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 7, 5, 0, 1 );
373  if( xform != solution )
374  {
375  std::cout << "Three-pt transform (1d) test failed." << std::endl;
376  errors++;
377  }
378 
379  return errors;
380 }