OpenWalnut  1.5.0dev
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 <memory>
29 #include <vector>
30 
31 #include <boost/random.hpp>
32 #include <cxxtest/TestSuite.h>
33 
34 #include "../../common/WLogger.h"
35 #include "../WDataSetScalar.h"
36 
37 /**
38  * Tests for the data set type containing only scalars.
39  */
40 class WDataSetScalarTest : public CxxTest::TestSuite
41 {
42 public:
43  /**
44  * Setup logger and other stuff for each test.
45  */
46  void setUp()
47  {
49  }
50 
51  /**
52  * Test if the interpolate function works reasonable.
53  */
54  void testInterpolate( void )
55  {
56  // create dummies, since they are needed in almost every test
57  std::shared_ptr< WGrid > grid( new WGridRegular3D( 5, 3, 3 ) );
58  std::shared_ptr< std::vector< double > > data( new std::vector< double >( grid->size() ) );
59  for( size_t i = 0; i < grid->size(); ++i )
60  {
61  ( *data )[i] = i;
62  }
63  std::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 0, 1, data, W_DT_DOUBLE ) );
64  WDataSetScalar ds( valueSet, grid );
65 
66  bool success = false;
67 
68  TS_ASSERT_EQUALS( ds.interpolate( WPosition::zero(), &success ), ( *data )[0] );
69  TS_ASSERT( success );
70  TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 0 ), &success ), ( *data )[1], 1e-9 );
71  TS_ASSERT( success );
72  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 0 ), &success ), ( *data )[5], 1e-9 );
73  TS_ASSERT( success );
74  TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 0 ), &success ), ( *data )[6], 1e-9 );
75  TS_ASSERT( success );
76  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 0, 1 ), &success ), ( *data )[15], 1e-9 );
77  TS_ASSERT( success );
78  TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 0, 1 ), &success ), ( *data )[16], 1e-9 );
79  TS_ASSERT( success );
80  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0, 1, 1 ), &success ), ( *data )[20], 1e-9 );
81  TS_ASSERT( success );
82  TS_ASSERT_DELTA( ds.interpolate( WPosition( 1, 1, 1 ), &success ), ( *data )[21], 1e-9 );
83  TS_ASSERT( success );
84 
85  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.3, 0.4, 0.5 ), &success ), 9.8, 1e-9 );
86  TS_ASSERT( success );
87  TS_ASSERT_DELTA( ds.interpolate( WPosition( 0.5, 0.5, 0.5 ), &success ), 10.5, 1e-9 );
88  TS_ASSERT( success );
89  }
90 
91  /**
92  * Test if the interpolate function works also for rotated grids reasonable.
93  */
95  {
96  // rotation around z with 45 degrees
97  WMatrix< double > mat( 4, 4 );
98  mat.makeIdentity();
99  mat( 0, 0 ) = 1.0 / sqrt( 2.0 );
100  mat( 0, 1 ) = 1.0 / sqrt( 2.0 );
101  mat( 1, 0 ) = -1.0 / sqrt( 2.0 );
102  mat( 1, 1 ) = 1.0 / sqrt( 2.0 );
103 
104  WGridTransformOrtho v( mat );
105 
106  std::shared_ptr< WGridRegular3D > grid( new WGridRegular3D( 5, 3, 3, v ) );
107  std::shared_ptr< std::vector< double > > data( new std::vector< double >( grid->size() ) );
108  for( size_t i = 0; i < grid->size(); ++i )
109  {
110  ( *data )[i] = i;
111  }
112  std::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 0, 1, data, W_DT_DOUBLE ) );
113  WDataSetScalar ds( valueSet, grid );
114 
115  bool success = false;
116 
117  TS_ASSERT_EQUALS( ds.interpolate( WPosition::zero(), &success ), ( *data )[0] );
118  TS_ASSERT( success );
119  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 0, 0 ) ), &success ), ( *data )[1], 1e-9 );
120  TS_ASSERT( success );
121  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0, 1, 0 ) ), &success ), ( *data )[5], 1e-9 );
122  TS_ASSERT( success );
123  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 1, 0 ) ), &success ), ( *data )[6], 1e-9 );
124  TS_ASSERT( success );
125  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0, 0, 1 ) ), &success ), ( *data )[15], 1e-9 );
126  TS_ASSERT( success );
127  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 0, 1 ) ), &success ), ( *data )[16], 1e-9 );
128  TS_ASSERT( success );
129  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0, 1, 1 ) ), &success ), ( *data )[20], 1e-9 );
130  TS_ASSERT( success );
131  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 1, 1, 1 ) ), &success ), ( *data )[21], 1e-9 );
132  TS_ASSERT( success );
133 
134  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0.3, 0.4, 0.5 ) ), &success ), 9.8, 1e-9 );
135  TS_ASSERT( success );
136  TS_ASSERT_DELTA( ds.interpolate( grid->getTransform().positionToWorldSpace( WPosition( 0.5, 0.5, 0.5 ) ), &success ), 10.5, 1e-9 );
137  TS_ASSERT( success );
138  }
139 
140  /**
141  * Check whether interpolation works for a translated dataset.
142  */
144  {
145  boost::random::mt19937 rng;
146  boost::random::uniform_int_distribution<> urnd( 3, 20 );
147  boost::random::uniform_real_distribution<> drnd( -1000.0, +1000.0 );
148 
149  WMatrix< double > mat( 4, 4 );
150  mat.makeIdentity();
151  mat( 0, 0 ) = 1.7;
152  mat( 1, 1 ) = 1.7;
153  mat( 2, 2 ) = 1.7;
154 
155  std::size_t sx = urnd( rng );
156  std::size_t sy = urnd( rng );
157  std::size_t sz = urnd( rng );
158 
159  boost::random::uniform_real_distribution<> prndx( 0.0, ( sx - 1.000000001 ) * mat( 0, 0 ) );
160  boost::random::uniform_real_distribution<> prndy( 0.0, ( sy - 1.000000001 ) * mat( 1, 1 ) );
161  boost::random::uniform_real_distribution<> prndz( 0.0, ( sz - 1.000000001 ) * mat( 2, 2 ) );
162 
163  WGridTransformOrtho v( mat );
164  std::shared_ptr< WGridRegular3D > grid( new WGridRegular3D( sx, sy, sz, v ) );
165  std::shared_ptr< std::vector< double > > data( new std::vector< double >( grid->size() ) );
166 
167  for( std::size_t k = 0; k < grid->size(); ++k )
168  {
169  data->at( k ) = drnd( rng );
170  }
171 
172  std::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 0, 1, data, W_DT_DOUBLE ) );
173  WDataSetScalar ds( valueSet, grid );
174 
175  for( std::size_t k = 0; k < 1000; ++k )
176  {
177  WMatrix< double > m = mat;
178 
179  m( 0, 3 ) = drnd( rng );
180  m( 1, 3 ) = drnd( rng );
181  m( 2, 3 ) = drnd( rng );
182 
183  WGridTransformOrtho t( m );
184  std::shared_ptr< WGridRegular3D > tGrid( new WGridRegular3D( sx, sy, sz, t ) );
185  std::shared_ptr< WValueSet< double > > tValueSet( new WValueSet< double >( 0, 1, data, W_DT_DOUBLE ) );
186  WDataSetScalar tds( tValueSet, tGrid );
187 
188  // test random positions in the dataset
189  for( std::size_t i = 0; i < 100; ++i )
190  {
191  WVector3d p( prndx( rng ), prndy( rng ), prndz( rng ) );
192  WVector3d q( p[ 0 ] + m( 0, 3 ), p[ 1 ] + m( 1, 3 ), p[ 2 ] + m( 2, 3 ) );
193 
194  bool s1, s2;
195  TS_ASSERT_DELTA( ds.interpolate( p, &s1 ), tds.interpolate( q, &s2 ), 1e-9 );
196  TS_ASSERT( s1 );
197  TS_ASSERT( s2 );
198  }
199  }
200  }
201 };
202 
203 #endif // WDATASETSCALAR_TEST_H
Tests for the data set type containing only scalars.
void testInterpolateInRotatedGrid(void)
Test if the interpolate function works also for rotated grids reasonable.
void setUp()
Setup logger and other stuff for each test.
void testInterpolate(void)
Test if the interpolate function works reasonable.
void testTranslatedGridInterpolation()
Check whether interpolation works for a translated dataset.
This data set type contains scalars as values.
double interpolate(const WPosition &pos, bool *success) const
Interpolate the value for the valueset at the given position.
A grid that has parallelepiped cells which all have the same proportion.
Implements an orthogonal grid transformation.
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
WMatrix & makeIdentity()
Makes the matrix contain the identity matrix, i.e.
Definition: WMatrix.h:352
This only is a 3d double vector.
Base Class for all value set types.
Definition: WValueSet.h:47