OpenWalnut  1.4.0
WDataSetScalar_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 WDATASETSCALAR_TEST_H
00026 #define WDATASETSCALAR_TEST_H
00027 
00028 #include <vector>
00029 
00030 #include <cxxtest/TestSuite.h>
00031 
00032 #include "../../common/WLogger.h"
00033 
00034 #include "../WDataSetScalar.h"
00035 
00036 /**
00037  * Tests for the data set type containing only scalars.
00038  */
00039 class WDataSetScalarTest : public CxxTest::TestSuite
00040 {
00041 public:
00042     /**
00043      * Setup logger and other stuff for each test.
00044      */
00045     void setUp()
00046     {
00047         WLogger::startup();
00048     }
00049 
00050     /**
00051      * Test if the interpolate function works reasonable.
00052      */
00053     void testInterpolate( void )
00054     {
00055         // create dummies, since they are needed in almost every test
00056         boost::shared_ptr< WGrid > grid( new WGridRegular3D( 5, 3, 3 ) );
00057         boost::shared_ptr< std::vector< double > > data( new std::vector< double >( grid->size() ) );
00058         for( size_t i = 0; i < grid->size(); ++i )
00059         {
00060             ( *data )[i] = i;
00061         }
00062         boost::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 0, 1, data, W_DT_DOUBLE ) );
00063         WDataSetScalar ds( valueSet, grid );
00064 
00065         bool success = false;
00066 
00067         TS_ASSERT_EQUALS( ds.interpolate( WPosition::zero(), &success ), ( *data )[0] );
00068         TS_ASSERT( success );
00069         TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 0 ), &success ), ( *data )[1], 1e-9 );
00070         TS_ASSERT( success );
00071         TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 0 ), &success ), ( *data )[5], 1e-9 );
00072         TS_ASSERT( success );
00073         TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 0 ), &success ), ( *data )[6], 1e-9 );
00074         TS_ASSERT( success );
00075         TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 0, 1 ), &success ), ( *data )[15], 1e-9 );
00076         TS_ASSERT( success );
00077         TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 1 ), &success ), ( *data )[16], 1e-9 );
00078         TS_ASSERT( success );
00079         TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 1 ), &success ), ( *data )[20], 1e-9 );
00080         TS_ASSERT( success );
00081         TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 1 ), &success ), ( *data )[21], 1e-9 );
00082         TS_ASSERT( success );
00083 
00084         TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.3, 0.4, 0.5 ), &success ), 9.8, 1e-9 );
00085         TS_ASSERT( success );
00086         TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.5, 0.5, 0.5 ), &success ), 10.5, 1e-9 );
00087         TS_ASSERT( success );
00088     }
00089 
00090     /**
00091      * Test if the interpolate function works also for rotated grids reasonable.
00092      */
00093     void testInterpolateInRotatedGrid( void )
00094     {
00095         // rotation around z with 45 degrees
00096         WMatrix< double > mat( 4, 4 );
00097         mat.makeIdentity();
00098         mat( 0, 0 ) =  1.0 / sqrt( 2.0 );
00099         mat( 0, 1 ) =  1.0 / sqrt( 2.0 );
00100         mat( 1, 0 ) = -1.0 / sqrt( 2.0 );
00101         mat( 1, 1 ) =  1.0 / sqrt( 2.0 );
00102 
00103         WGridTransformOrtho v( mat );
00104 
00105         boost::shared_ptr< WGridRegular3D > grid( new WGridRegular3D( 5, 3, 3, v ) );
00106         boost::shared_ptr< std::vector< double > > data( new std::vector< double >( grid->size() ) );
00107         for( size_t i = 0; i < grid->size(); ++i )
00108         {
00109             ( *data )[i] = i;
00110         }
00111         boost::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 0, 1, data, W_DT_DOUBLE ) );
00112         WDataSetScalar ds( valueSet, grid );
00113 
00114         bool success = false;
00115 
00116         TS_ASSERT_EQUALS( ds.interpolate( WPosition::zero(), &success ), ( *data )[0] );
00117         TS_ASSERT( success );
00118         TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 0, 0 ) ), &success ), ( *data )[1], 1e-9 );
00119         TS_ASSERT( success );
00120         TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0, 1, 0 ) ), &success ), ( *data )[5], 1e-9 );
00121         TS_ASSERT( success );
00122         TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 1, 0 ) ), &success ), ( *data )[6], 1e-9 );
00123         TS_ASSERT( success );
00124         TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0, 0, 1 ) ), &success ), ( *data )[15], 1e-9 );
00125         TS_ASSERT( success );
00126         TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 0, 1 ) ), &success ), ( *data )[16], 1e-9 );
00127         TS_ASSERT( success );
00128         TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0, 1, 1 ) ), &success ), ( *data )[20], 1e-9 );
00129         TS_ASSERT( success );
00130         TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 1, 1 ) ), &success ), ( *data )[21], 1e-9 );
00131         TS_ASSERT( success );
00132 
00133         TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0.3, 0.4, 0.5 ) ), &success ), 9.8, 1e-9 );
00134         TS_ASSERT( success );
00135         TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0.5, 0.5, 0.5 ) ), &success ), 10.5, 1e-9 );
00136         TS_ASSERT( success );
00137     }
00138 };
00139 
00140 #endif  // WDATASETSCALAR_TEST_H