MOAB: Mesh Oriented datABase  (version 5.4.1)
homxform_test.cpp
Go to the documentation of this file.
00001 /**
00002  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
00003  * storing and accessing finite element mesh data.
00004  *
00005  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
00006  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00007  * retains certain rights in this software.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  */
00015 
00016 #include "moab/HomXform.hpp"
00017 #include <cassert>
00018 #include <iostream>
00019 
00020 using namespace moab;
00021 
00022 // unit test for the HomCoord and HomXform classes
00023 //
00024 int test_get_set();
00025 int test_coord_operators();
00026 int test_xform_operators();
00027 int test_xform_functions();
00028 int test_coord_xform_operators();
00029 
00030 int main( int /*argc*/, char** /*argv*/ )
00031 {
00032     // first test HomCoord
00033 
00034     // constructors
00035     // following shouldn't compile, since bare constructor is private
00036 
00037     int errors = 0;
00038 
00039     errors += test_get_set();
00040     errors += test_coord_operators();
00041     errors += test_xform_operators();
00042     errors += test_xform_functions();
00043     errors += test_coord_xform_operators();
00044 
00045     if( errors > 0 )
00046         std::cout << errors << " errors found." << std::endl;
00047     else
00048         std::cout << "All tests passed." << std::endl;
00049 
00050     return errors;
00051 }
00052 
00053 int test_get_set()
00054 {
00055     int errors = 0;
00056 
00057     // other constructors
00058     int coordsa[4] = { 1, 2, 3, 1 };
00059     int coordsb[4] = { 4, 3, 2, 1 };
00060 
00061     HomCoord coords1( coordsa );
00062     HomCoord coords2( 4, 3, 2, 1 );
00063     HomCoord coords3( 4, 3, 2 );
00064     HomCoord coords4( 1, 1, 1, 1 );
00065 
00066     // set
00067     coords2.set( coordsb );
00068 
00069     // hom_coord()
00070     for( int i = 0; i < 4; i++ )
00071     {
00072         if( coords2.hom_coord()[i] != coordsb[i] )
00073         {
00074             std::cout << "Get test failed." << std::endl;
00075             errors++;
00076         }
00077     }
00078 
00079     // ijkh
00080     if( coords2.i() != coordsb[0] || coords2.j() != coordsb[1] || coords2.k() != coordsb[2] ||
00081         coords2.h() != coordsb[3] )
00082     {
00083         std::cout << "ijkh test failed." << std::endl;
00084         errors++;
00085     }
00086 
00087     // set
00088     coords2.set( 3, 3, 3 );
00089 
00090     // hom_coord()
00091     if( coords2.hom_coord()[0] != 3 || coords2.hom_coord()[1] != 3 || coords2.hom_coord()[2] != 3 ||
00092         coords2.hom_coord()[3] != 1 )
00093     {
00094         std::cout << "Set (int) test failed." << std::endl;
00095         errors++;
00096     }
00097 
00098     return errors;
00099 }
00100 
00101 int test_coord_operators()
00102 {
00103     int errors = 0;
00104 
00105     HomCoord coords1( 1, 2, 3, 1 );
00106     HomCoord coords2( 4, 3, 2, 1 );
00107     HomCoord coords3( 4, 3, 2 );
00108     HomCoord coords4( 1, 1, 1, 1 );
00109 
00110     // operator>=
00111     bool optest = ( coords2 >= coords4 && coords2 >= coords3 && coords3 >= coords2 );
00112     if( !optest )
00113     {
00114         std::cout << "Test failed for operator >=." << std::endl;
00115         errors++;
00116     }
00117 
00118     optest = ( coords4 <= coords2 && coords2 <= coords3 && coords3 <= coords2 );
00119     if( !optest )
00120     {
00121         std::cout << "Test failed for operator <=." << std::endl;
00122         errors++;
00123     }
00124 
00125     // operator>
00126     optest = ( coords2 > coords4 && !( coords2 > coords3 ) && !( coords3 > coords2 ) );
00127     if( !optest )
00128     {
00129         std::cout << "Test failed for operator >." << std::endl;
00130         errors++;
00131     }
00132 
00133     optest = ( coords4 < coords2 && !( coords2 < coords3 ) && !( coords3 < coords2 ) );
00134     if( !optest )
00135     {
00136         std::cout << "Test failed for operator <." << std::endl;
00137         errors++;
00138     }
00139 
00140     // operator[]
00141     for( int i = 0; i < 3; i++ )
00142     {
00143         if( coords1[i] != coords2[3 - i] )
00144         {
00145             std::cout << "Test failed for operator[]." << std::endl;
00146             errors++;
00147         }
00148     }
00149 
00150     // operator+
00151     HomCoord coords5( 2 * coords1[0], 2 * coords1[1], 2 * coords1[2] );
00152     HomCoord coords6 = coords1 + coords1;
00153     if( coords5 != coords6 )
00154     {
00155         std::cout << "Test failed for operator+." << std::endl;
00156         errors++;
00157     }
00158 
00159     // operator-
00160     if( coords5 - coords1 != coords1 )
00161     {
00162         std::cout << "Test failed for operator-." << std::endl;
00163         errors++;
00164     }
00165 
00166     return errors;
00167 }
00168 
00169 int test_xform_constructors()
00170 {
00171     int errors = 0;
00172 
00173     // integer constructor
00174     int test_int[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
00175     HomXform xform( test_int );
00176     for( int i = 0; i < 16; i++ )
00177     {
00178         if( xform[i] != i )
00179         {
00180             std::cout << "HomXform integer array constructor failed." << std::endl;
00181             errors++;
00182         }
00183     }
00184 
00185     HomXform xform3( test_int[0], test_int[1], test_int[2], test_int[3], test_int[4], test_int[5], test_int[6],
00186                      test_int[7], test_int[8], test_int[9], test_int[10], test_int[11], test_int[12], test_int[13],
00187                      test_int[14], test_int[15] );
00188     for( int i = 0; i < 16; i++ )
00189     {
00190         if( xform3[i] != i )
00191         {
00192             std::cout << "HomXform integer constructor failed." << std::endl;
00193             errors++;
00194         }
00195     }
00196 
00197     // sample rotation, translation, and scaling matrices/vectors
00198     int rotate[]    = { 0, 1, 0, -1, 0, 0, 0, 0, 1 };
00199     int translate[] = { 4, 5, 6 };
00200     int scale[]     = { 1, 1, 1 };
00201 
00202     // construct an xform matrix based on those
00203     HomXform xform2( rotate, scale, translate );
00204 
00205     // test where those went in the xform
00206     if( !( xform2[XFORM_INDEX( 0, 0 )] == rotate[0] && xform2[XFORM_INDEX( 0, 1 )] == rotate[1] &&
00207            xform2[XFORM_INDEX( 0, 2 )] == rotate[2] && xform2[XFORM_INDEX( 1, 0 )] == rotate[3] &&
00208            xform2[XFORM_INDEX( 1, 1 )] == rotate[4] && xform2[XFORM_INDEX( 1, 2 )] == rotate[5] &&
00209            xform2[XFORM_INDEX( 2, 0 )] == rotate[6] && xform2[XFORM_INDEX( 2, 1 )] == rotate[7] &&
00210            xform2[XFORM_INDEX( 2, 2 )] == rotate[8] && xform2[XFORM_INDEX( 3, 0 )] == translate[0] &&
00211            xform2[XFORM_INDEX( 3, 1 )] == translate[1] && xform2[XFORM_INDEX( 3, 2 )] == translate[2] &&
00212            xform2[XFORM_INDEX( 0, 3 )] == 0 && xform2[XFORM_INDEX( 1, 3 )] == 0 && xform2[XFORM_INDEX( 2, 3 )] == 0 &&
00213            xform2[XFORM_INDEX( 3, 3 )] == 1 ) )
00214     {
00215         std::cout << "HomXform rotate, scale, translate constructor failed." << std::endl;
00216         errors++;
00217     }
00218 
00219     return errors;
00220 }
00221 
00222 int test_xform_operators()
00223 {
00224     int errors = 0;
00225 
00226     // sample rotation, translation, and scaling matrices/vectors
00227     int rotate[]    = { 0, 1, 0, -1, 0, 0, 0, 0, 1 };
00228     int translate[] = { 4, 5, 6 };
00229     int scale[]     = { 1, 1, 1 };
00230 
00231     // construct an xform matrix based on those
00232     HomXform xform1( rotate, scale, translate );
00233     HomXform xform1a( rotate, scale, translate );
00234 
00235     // test operator==
00236     if( !( xform1 == xform1a ) )
00237     {
00238         std::cout << "HomXform operator== failed." << std::endl;
00239         errors++;
00240     }
00241 
00242     // test operator!=
00243     xform1a[1] = 0;
00244     if( !( xform1 != xform1a ) )
00245     {
00246         std::cout << "HomXform operator!= failed." << std::endl;
00247         errors++;
00248     }
00249 
00250     // test operator=
00251     HomXform xform1c = xform1;
00252     if( !( xform1c == xform1 ) )
00253     {
00254         std::cout << "HomXform operator= failed." << std::endl;
00255         errors++;
00256     }
00257 
00258     HomXform xform3 = xform1 * HomXform::IDENTITY;
00259     if( xform3 != xform1 )
00260     {
00261         std::cout << "HomXform operator * failed." << std::endl;
00262         errors++;
00263     }
00264 
00265     // test operator*=
00266     xform3 *= HomXform::IDENTITY;
00267     if( xform3 != xform1 )
00268     {
00269         std::cout << "HomXform operator *= failed." << std::endl;
00270         errors++;
00271     }
00272 
00273     return errors;
00274 }
00275 
00276 int test_xform_functions()
00277 {
00278     int errors = 0;
00279 
00280     // HomCoord functions
00281     // length() and length_squared()
00282     HomCoord coord1( 3, 4, 5 );
00283     if( coord1.length_squared() != 50 || coord1.length() != 7 )
00284     {
00285         std::cout << "HomCoord length() or length_squared() failed." << std::endl;
00286         errors++;
00287     }
00288 
00289     // normalize()
00290     coord1.normalize();
00291     HomCoord coord2( 3, 0, 0 );
00292     coord2.normalize();
00293     if( coord1.length_squared() != 0 || coord2.length_squared() != 1 )
00294     {
00295         std::cout << "HomCoord normalize failed." << std::endl;
00296         errors++;
00297     }
00298 
00299     // sample rotation, translation, and scaling matrices/vectors
00300     int inv_int[] = { 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 7, 5, 0, 1 };
00301 
00302     // construct an xform matrix based on those
00303     HomXform xform1( inv_int );
00304 
00305     HomXform xform1_inv = xform1.inverse();
00306 
00307     HomXform xform2 = xform1 * xform1_inv;
00308     if( xform2 != HomXform::IDENTITY )
00309     {
00310         std::cout << "HomXform inverse failed." << std::endl;
00311         errors++;
00312     }
00313 
00314     return errors;
00315 }
00316 
00317 int test_coord_xform_operators()
00318 {
00319     int errors = 0;
00320 
00321     // sample pt
00322     HomCoord test_pt( 1, 2, 3 );
00323 
00324     HomCoord test_pt2 = test_pt * HomXform::IDENTITY;
00325     if( test_pt2 != test_pt )
00326     {
00327         std::cout << "Coord-xform operator* failed." << std::endl;
00328         errors++;
00329     }
00330 
00331     // get an inverse transform quickly
00332     int rotate[]    = { 0, 1, 0, -1, 0, 0, 0, 0, 1 };
00333     int translate[] = { 4, 5, 6 };
00334     int scale[]     = { 1, 1, 1 };
00335     HomXform xform2( rotate, scale, translate );
00336 
00337     // operator*
00338     HomCoord ident( 1, 1, 1, 1 );
00339     HomCoord test_pt3 = ident * HomXform::IDENTITY;
00340     if( test_pt3 != ident )
00341     {
00342         std::cout << "Coord-xform operator* failed." << std::endl;
00343         errors++;
00344     }
00345 
00346     // operator/
00347     test_pt2 = ( test_pt * xform2 ) / xform2;
00348     if( test_pt2 != test_pt )
00349     {
00350         std::cout << "Coord-xform operator/ failed." << std::endl;
00351         errors++;
00352     }
00353 
00354     // test three_pt_xform; use known transforms in most cases
00355     HomXform xform;
00356 
00357     // first test: i = j, j = -i, k = k, t = (7,5,0)
00358     xform.three_pt_xform( HomCoord( 0, 0, 0, 1 ), HomCoord( 7, 5, 0, 1 ), HomCoord( 0, 3, 0, 1 ),
00359                           HomCoord( 4, 5, 0, 1 ), HomCoord( 0, 0, 3, 1 ), HomCoord( 7, 5, 3, 1 ) );
00360 
00361     HomXform solution( 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 7, 5, 0, 1 );
00362     if( xform != solution )
00363     {
00364         std::cout << "Three-pt transform (general) test failed." << std::endl;
00365         errors++;
00366     }
00367 
00368     // now test 1d
00369     xform.three_pt_xform( HomCoord( 0, 0, 0, 1 ), HomCoord( 7, 5, 0, 1 ), HomCoord( 6, 0, 0, 1 ),
00370                           HomCoord( 7, 11, 0, 1 ), HomCoord( 0, 0, 0, 1 ), HomCoord( 7, 5, 0, 1 ) );
00371 
00372     solution = HomXform( 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 7, 5, 0, 1 );
00373     if( xform != solution )
00374     {
00375         std::cout << "Three-pt transform (1d) test failed." << std::endl;
00376         errors++;
00377     }
00378 
00379     return errors;
00380 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines