OpenWalnut  1.4.0
WGridTransformOrtho_test.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGRIDTRANSFORMORTHO_TEST_H
00026 #define WGRIDTRANSFORMORTHO_TEST_H
00027 
00028 #include <cstdio>
00029 #include <sstream>
00030 #include <string>
00031 #include <vector>
00032 
00033 #include <boost/shared_ptr.hpp>
00034 
00035 #include <cxxtest/TestSuite.h>
00036 
00037 #include "../../common/exceptions/WPreconditionNotMet.h"
00038 
00039 #include "../WGridTransformOrtho.h"
00040 
00041 /**
00042  * Tests the WGridTransform class.
00043  */
00044 class WGridTransformTest : public CxxTest::TestSuite
00045 {
00046 public:
00047     /**
00048      * Test if all data fields get initialized correctly. Constructors should throw
00049      * a WPreconditionNotMet exception if any input values are invalid.
00050      */
00051     void testInstantiation()
00052     {
00053         {
00054             TS_ASSERT_THROWS_NOTHING( WGridTransformOrtho v() );
00055             WGridTransformOrtho v;
00056             TS_ASSERT_EQUALS( v.getOffsetX(), 1.0 );
00057             TS_ASSERT_EQUALS( v.getOffsetY(), 1.0 );
00058             TS_ASSERT_EQUALS( v.getOffsetZ(), 1.0 );
00059             compareVectors( v.getUnitDirectionX(), WVector3d( 1.0, 0.0, 0.0 ), 0.0001 );
00060             compareVectors( v.getUnitDirectionY(), WVector3d( 0.0, 1.0, 0.0 ), 0.0001 );
00061             compareVectors( v.getUnitDirectionZ(), WVector3d( 0.0, 0.0, 1.0 ), 0.0001 );
00062             compareVectors( v.getDirectionX(), WVector3d( 1.0, 0.0, 0.0 ), 0.0001 );
00063             compareVectors( v.getDirectionY(), WVector3d( 0.0, 1.0, 0.0 ), 0.0001 );
00064             compareVectors( v.getDirectionZ(), WVector3d( 0.0, 0.0, 1.0 ), 0.0001 );
00065             compareVectors( v.getOrigin(), WVector3d( 0.0, 0.0, 0.0 ), 0.0001 );
00066         }
00067         {
00068             TS_ASSERT_THROWS_NOTHING( WGridTransformOrtho v( 2.2, 3.3, -1.0 ) );
00069             TS_ASSERT_THROWS( WGridTransformOrtho v( 0.0, 0.0, 1.0 ), WPreconditionNotMet );
00070             TS_ASSERT_THROWS( WGridTransformOrtho v( 0.0, 2.0, 1.0 ), WPreconditionNotMet );
00071             TS_ASSERT_THROWS( WGridTransformOrtho v( 1.0, 1.0, 0.0 ), WPreconditionNotMet );
00072         }
00073         {
00074             WGridTransformOrtho v( 2.2, 3.3, -1.0 );
00075             TS_ASSERT_EQUALS( v.getOffsetX(), 2.2 );
00076             TS_ASSERT_EQUALS( v.getOffsetY(), 3.3 );
00077             TS_ASSERT_EQUALS( v.getOffsetZ(), 1.0 );
00078             compareVectors( v.getUnitDirectionX(), WVector3d( 1.0, 0.0, 0.0 ), 0.0001 );
00079             compareVectors( v.getUnitDirectionY(), WVector3d( 0.0, 1.0, 0.0 ), 0.0001 );
00080             compareVectors( v.getUnitDirectionZ(), WVector3d( 0.0, 0.0, -1.0 ), 0.0001 );
00081             compareVectors( v.getDirectionX(), WVector3d( 2.2, 0.0, 0.0 ), 0.0001 );
00082             compareVectors( v.getDirectionY(), WVector3d( 0.0, 3.3, 0.0 ), 0.0001 );
00083             compareVectors( v.getDirectionZ(), WVector3d( 0.0, 0.0, -1.0 ), 0.0001 );
00084             compareVectors( v.getOrigin(), WVector3d( 0.0, 0.0, 0.0 ), 0.0001 );
00085         }
00086         {
00087             WMatrix< double > mat( 4, 4 );
00088             mat.makeIdentity();
00089             mat( 0, 0 ) = 2.2;
00090             mat( 1, 1 ) = 3.3;
00091             mat( 2, 2 ) = 0.0;
00092 
00093             TS_ASSERT_THROWS( WGridTransformOrtho v( mat ), WPreconditionNotMet );
00094         }
00095         {
00096             WMatrix< double > mat( 4, 4 );
00097             mat.makeIdentity();
00098             mat( 0, 0 ) = 2.2;
00099             mat( 1, 0 ) = 0.1;
00100             mat( 1, 1 ) = 3.3;
00101             mat( 2, 2 ) = 1.0;
00102 
00103             TS_ASSERT_THROWS( WGridTransformOrtho v( mat ), WPreconditionNotMet );
00104         }
00105         {
00106             WMatrix< double > mat( 4, 4 );
00107             mat.makeIdentity();
00108             mat( 0, 0 ) = 2.0;
00109             mat( 1, 0 ) = 2.0;
00110             mat( 1, 1 ) = 3.0;
00111             mat( 0, 1 ) = -3.0;
00112             mat( 2, 2 ) = 4.4;
00113             mat( 0, 3 ) = 1.0;
00114             mat( 1, 3 ) = 2.0;
00115             mat( 2, 3 ) = 0.5;
00116 
00117             WGridTransformOrtho v( mat );
00118             TS_ASSERT_EQUALS( v.getOffsetX(), sqrt( 8.0 ) );
00119             TS_ASSERT_EQUALS( v.getOffsetY(), sqrt( 18.0 ) );
00120             TS_ASSERT_EQUALS( v.getOffsetZ(), 4.4 );
00121             TS_ASSERT_DELTA( length( v.getUnitDirectionX() - WVector3d( 0.5 * sqrt( 2.0 ), 0.5 * sqrt( 2.0 ), 0.0 ) ), 0.0, 1e-13 );
00122             TS_ASSERT_DELTA( length( v.getUnitDirectionY() - WVector3d( -0.5 * sqrt( 2.0 ), 0.5 * sqrt( 2.0 ), 0.0 ) ), 0.0, 1e-13 );
00123             compareVectors( v.getUnitDirectionZ(), WVector3d( 0.0, 0.0, 1.0 ), 0.0001 );
00124             compareVectors( v.getDirectionX(), WVector3d( 2.0, 2.0, 0.0 ), 0.0001 );
00125             compareVectors( v.getDirectionY(), WVector3d( -3.0, 3.0, 0.0 ), 0.0001 );
00126             compareVectors( v.getDirectionZ(), WVector3d( 0.0, 0.0, 4.4 ), 0.0001 );
00127             compareVectors( v.getOrigin(), WVector3d( 1.0, 2.0, 0.5 ), 0.0001 );
00128         }
00129     }
00130 
00131     /**
00132      * Different constructors should not yield differently initialized
00133      * data fields.
00134      */
00135     void testCompareConstructors()
00136     {
00137         WMatrix< double > mat( 4, 4 );
00138         mat.makeIdentity();
00139         mat( 0, 0 ) = 2.2;
00140         mat( 1, 1 ) = 3.3;
00141         mat( 2, 2 ) = 4.4;
00142 
00143         WGridTransformOrtho t1( mat );
00144         WGridTransformOrtho t2( 2.2, 3.3, 4.4 );
00145 
00146         TS_ASSERT_EQUALS( t1.getOffsetX(), t2.getOffsetX() );
00147         TS_ASSERT_EQUALS( t1.getOffsetY(), t2.getOffsetY() );
00148         TS_ASSERT_EQUALS( t1.getOffsetZ(), t2.getOffsetZ() );
00149 
00150         compareVectors( t1.getDirectionX(), t2.getDirectionX(), 0.0001 );
00151         compareVectors( t1.getDirectionY(), t2.getDirectionY(), 0.0001 );
00152         compareVectors( t1.getDirectionZ(), t2.getDirectionZ(), 0.0001 );
00153 
00154         compareVectors( t1.getOrigin(), t2.getOrigin(), 0.0001 );
00155     }
00156 
00157     /**
00158      * Test transformation from grid space to world space.
00159      */
00160     void testTransformationToWorldSpace()
00161     {
00162         {
00163             // test identity transform
00164             WVector3d v( -7.64, 8.73, -0.0063 );
00165             WGridTransformOrtho t;
00166 
00167             compareVectors( v, t.positionToWorldSpace( v ), 0.0001 );
00168             compareVectors( v, t.directionToWorldSpace( v ), 0.0001 );
00169         }
00170 
00171         {
00172             WGridTransformOrtho t( 2.2, 3.3, 4.4 );
00173             WVector3d v( 1.0, 1.0, 1.0 );
00174 
00175             compareVectors( WVector3d( 2.2, 3.3, 4.4 ), t.positionToWorldSpace( v ), 0.0001 );
00176             compareVectors( WVector3d( 2.2, 3.3, 4.4 ), t.directionToWorldSpace( v ), 0.0001 );
00177         }
00178 
00179         {
00180             WMatrix< double > mat( 4, 4 );
00181             mat.makeIdentity();
00182             mat( 0, 0 ) = 2.2;
00183             mat( 1, 1 ) = 3.3;
00184             mat( 2, 2 ) = 4.4;
00185             mat( 0, 3 ) = 1.0;
00186             mat( 1, 3 ) = 2.0;
00187             mat( 2, 3 ) = 0.5;
00188 
00189             WGridTransformOrtho t( mat );
00190             WVector3d v( 1.0, 1.0, 1.0 );
00191 
00192             compareVectors( WVector3d( 3.2, 5.3, 4.9 ), t.positionToWorldSpace( v ), 0.0001 );
00193             compareVectors( WVector3d( 2.2, 3.3, 4.4 ), t.directionToWorldSpace( v ), 0.0001 );
00194         }
00195         {
00196             WMatrix< double > mat( 4, 4 );
00197             mat.makeIdentity();
00198             mat( 0, 0 ) = 2.0;
00199             mat( 1, 0 ) = 2.0;
00200             mat( 1, 1 ) = 3.0;
00201             mat( 0, 1 ) = -3.0;
00202             mat( 2, 2 ) = 4.4;
00203             mat( 0, 3 ) = 1.0;
00204             mat( 1, 3 ) = 2.0;
00205             mat( 2, 3 ) = 0.5;
00206 
00207             WGridTransformOrtho t( mat );
00208             WVector3d v( 1.0, 1.0, 1.0 );
00209 
00210             WVector3d w = t.positionToWorldSpace( v );
00211             TS_ASSERT_DELTA( 0.0, w[ 0 ], 0.0001 );
00212             TS_ASSERT_DELTA( 7.0, w[ 1 ], 0.0001 );
00213             TS_ASSERT_DELTA( 4.9, w[ 2 ], 0.0001 );
00214             compareVectors( WVector3d( -1.0, 5.0, 4.4 ), t.directionToWorldSpace( v ), 0.0001 );
00215         }
00216     }
00217 
00218     /**
00219      * Test transformation from world space to grid space.
00220      */
00221     void testTransformationToGridSpace()
00222     {
00223         {
00224             // test identity transform
00225             WVector3d v( -7.64, 8.73, -0.0063 );
00226             WGridTransformOrtho t;
00227 
00228             compareVectors( v, t.positionToGridSpace( v ), 0.0001 );
00229             compareVectors( v, t.directionToGridSpace( v ), 0.0001 );
00230         }
00231 
00232         {
00233             WGridTransformOrtho t( 2.2, 3.3, 4.4 );
00234             WVector3d v( 2.2, 3.3, 4.4 );
00235 
00236             compareVectors( WVector3d( 1.0, 1.0, 1.0 ), t.positionToGridSpace( v ), 0.0001 );
00237             compareVectors( WVector3d( 1.0, 1.0, 1.0 ), t.directionToGridSpace( v ), 0.0001 );
00238         }
00239 
00240         {
00241             WMatrix< double > mat( 4, 4 );
00242             mat.makeIdentity();
00243             mat( 0, 0 ) = 2.2;
00244             mat( 1, 1 ) = 3.3;
00245             mat( 2, 2 ) = 4.4;
00246             mat( 0, 3 ) = 1.0;
00247             mat( 1, 3 ) = 2.0;
00248             mat( 2, 3 ) = 0.5;
00249 
00250             WGridTransformOrtho t( mat );
00251 
00252             compareVectors( WVector3d( 1.0, 1.0, 1.0 ), t.positionToGridSpace( WVector3d( 3.2, 5.3, 4.9 ) ), 0.0001 );
00253             compareVectors( WVector3d( 1.0, 1.0, 1.0 ), t.directionToGridSpace( WVector3d( 2.2, 3.3, 4.4 ) ), 0.0001 );
00254         }
00255         {
00256             WMatrix< double > mat( 4, 4 );
00257             mat.makeIdentity();
00258             mat( 0, 0 ) = 2.0;
00259             mat( 1, 0 ) = 2.0;
00260             mat( 1, 1 ) = 3.0;
00261             mat( 0, 1 ) = -3.0;
00262             mat( 2, 2 ) = 4.4;
00263             mat( 0, 3 ) = 1.0;
00264             mat( 1, 3 ) = 2.0;
00265             mat( 2, 3 ) = 0.5;
00266 
00267             WGridTransformOrtho t( mat );
00268 
00269             TS_ASSERT_DELTA( length( WVector3d( 1.0, 1.0, 1.0 )  // NOLINT
00270                                - t.positionToGridSpace( WVector3d( 0.0, 7.0, 4.9 ) ) ), 0.0, 1e-13 );
00271             TS_ASSERT_DELTA( length( WVector3d( 1.0, 1.0, 1.0 )  // NOLINT
00272                                - t.directionToGridSpace( WVector3d( -1.0, 5.0, 4.4 ) ) ), 0.0, 1e-13 );
00273         }
00274     }
00275 
00276 private:
00277     /**
00278      * Compares two vectors, element by element.
00279      *
00280      * \param v1 The first vector.
00281      * \param v2 The second vector.
00282      * \param delta The maximum absolute difference between the elements of the vectors.
00283      */
00284     void compareVectors( WVector3d const& v1, WVector3d const& v2, double delta ) const
00285     {
00286         TS_ASSERT_DELTA( v1[ 0 ], v2[ 0 ], delta );
00287         TS_ASSERT_DELTA( v1[ 1 ], v2[ 1 ], delta );
00288         TS_ASSERT_DELTA( v1[ 2 ], v2[ 2 ], delta );
00289     }
00290 };
00291 
00292 #endif  // WGRIDTRANSFORMORTHO_TEST_H