OpenWalnut  1.4.0
WDataSetScalar_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WDATASETSCALAR_TEST_H
26 #define WDATASETSCALAR_TEST_H
27 
28 #include <vector>
29 
30 #include <cxxtest/TestSuite.h>
31 
32 #include "../../common/WLogger.h"
33 
34 #include "../WDataSetScalar.h"
35 
36 /**
37  * Tests for the data set type containing only scalars.
38  */
39 class WDataSetScalarTest : public CxxTest::TestSuite
40 {
41 public:
42  /**
43  * Setup logger and other stuff for each test.
44  */
45  void setUp()
46  {
48  }
49 
50  /**
51  * Test if the interpolate function works reasonable.
52  */
53  void testInterpolate( void )
54  {
55  // create dummies, since they are needed in almost every test
56  boost::shared_ptr< WGrid > grid( new WGridRegular3D( 5, 3, 3 ) );
57  boost::shared_ptr< std::vector< double > > data( new std::vector< double >( grid->size() ) );
58  for( size_t i = 0; i < grid->size(); ++i )
59  {
60  ( *data )[i] = i;
61  }
62  boost::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 0, 1, data, W_DT_DOUBLE ) );
63  WDataSetScalar ds( valueSet, grid );
64 
65  bool success = false;
66 
67  TS_ASSERT_EQUALS( ds.interpolate( WPosition::zero(), &success ), ( *data )[0] );
68  TS_ASSERT( success );
69  TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 0 ), &success ), ( *data )[1], 1e-9 );
70  TS_ASSERT( success );
71  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 0 ), &success ), ( *data )[5], 1e-9 );
72  TS_ASSERT( success );
73  TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 0 ), &success ), ( *data )[6], 1e-9 );
74  TS_ASSERT( success );
75  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 0, 1 ), &success ), ( *data )[15], 1e-9 );
76  TS_ASSERT( success );
77  TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 1 ), &success ), ( *data )[16], 1e-9 );
78  TS_ASSERT( success );
79  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 1 ), &success ), ( *data )[20], 1e-9 );
80  TS_ASSERT( success );
81  TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 1 ), &success ), ( *data )[21], 1e-9 );
82  TS_ASSERT( success );
83 
84  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.3, 0.4, 0.5 ), &success ), 9.8, 1e-9 );
85  TS_ASSERT( success );
86  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.5, 0.5, 0.5 ), &success ), 10.5, 1e-9 );
87  TS_ASSERT( success );
88  }
89 
90  /**
91  * Test if the interpolate function works also for rotated grids reasonable.
92  */
94  {
95  // rotation around z with 45 degrees
96  WMatrix< double > mat( 4, 4 );
97  mat.makeIdentity();
98  mat( 0, 0 ) = 1.0 / sqrt( 2.0 );
99  mat( 0, 1 ) = 1.0 / sqrt( 2.0 );
100  mat( 1, 0 ) = -1.0 / sqrt( 2.0 );
101  mat( 1, 1 ) = 1.0 / sqrt( 2.0 );
102 
103  WGridTransformOrtho v( mat );
104 
105  boost::shared_ptr< WGridRegular3D > grid( new WGridRegular3D( 5, 3, 3, v ) );
106  boost::shared_ptr< std::vector< double > > data( new std::vector< double >( grid->size() ) );
107  for( size_t i = 0; i < grid->size(); ++i )
108  {
109  ( *data )[i] = i;
110  }
111  boost::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 0, 1, data, W_DT_DOUBLE ) );
112  WDataSetScalar ds( valueSet, grid );
113 
114  bool success = false;
115 
116  TS_ASSERT_EQUALS( ds.interpolate( WPosition::zero(), &success ), ( *data )[0] );
117  TS_ASSERT( success );
118  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 0, 0 ) ), &success ), ( *data )[1], 1e-9 );
119  TS_ASSERT( success );
120  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0, 1, 0 ) ), &success ), ( *data )[5], 1e-9 );
121  TS_ASSERT( success );
122  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 1, 0 ) ), &success ), ( *data )[6], 1e-9 );
123  TS_ASSERT( success );
124  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0, 0, 1 ) ), &success ), ( *data )[15], 1e-9 );
125  TS_ASSERT( success );
126  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 0, 1 ) ), &success ), ( *data )[16], 1e-9 );
127  TS_ASSERT( success );
128  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0, 1, 1 ) ), &success ), ( *data )[20], 1e-9 );
129  TS_ASSERT( success );
130  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 1, 1 ) ), &success ), ( *data )[21], 1e-9 );
131  TS_ASSERT( success );
132 
133  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0.3, 0.4, 0.5 ) ), &success ), 9.8, 1e-9 );
134  TS_ASSERT( success );
135  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0.5, 0.5, 0.5 ) ), &success ), 10.5, 1e-9 );
136  TS_ASSERT( success );
137  }
138 };
139 
140 #endif // WDATASETSCALAR_TEST_H
Tests for the data set type containing only scalars.
WMatrix & makeIdentity()
Makes the matrix contain the identity matrix, i.e.
Definition: WMatrix.h:352
void setUp()
Setup logger and other stuff for each test.
A grid that has parallelepiped cells which all have the same proportion.
double interpolate(const WPosition &pos, bool *success) const
Interpolate the value fo the valueset at the given position.
void testInterpolate(void)
Test if the interpolate function works reasonable.
This only is a 3d double vector.
void testInterpolateInRotatedGrid(void)
Test if the interpolate function works also for rotated grids reasonable.
static void startup(std::ostream &output=std::cout, LogLevel level=LL_DEBUG)
Create the first and only instance of the logger as it is a singleton.
Definition: WLogger.cpp:41
Base Class for all value set types.
Definition: WValueSet.h:46
This data set type contains scalars as values.
Implements an orthogonal grid transformation.