OpenWalnut  1.4.0
WGridTransformOrtho_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 WGRIDTRANSFORMORTHO_TEST_H
26 #define WGRIDTRANSFORMORTHO_TEST_H
27 
28 #include <cstdio>
29 #include <sstream>
30 #include <string>
31 #include <vector>
32 
33 #include <boost/shared_ptr.hpp>
34 
35 #include <cxxtest/TestSuite.h>
36 
37 #include "../../common/exceptions/WPreconditionNotMet.h"
38 
39 #include "../WGridTransformOrtho.h"
40 
41 /**
42  * Tests the WGridTransform class.
43  */
44 class WGridTransformTest : public CxxTest::TestSuite
45 {
46 public:
47  /**
48  * Test if all data fields get initialized correctly. Constructors should throw
49  * a WPreconditionNotMet exception if any input values are invalid.
50  */
52  {
53  {
54  TS_ASSERT_THROWS_NOTHING( WGridTransformOrtho v() );
56  TS_ASSERT_EQUALS( v.getOffsetX(), 1.0 );
57  TS_ASSERT_EQUALS( v.getOffsetY(), 1.0 );
58  TS_ASSERT_EQUALS( v.getOffsetZ(), 1.0 );
59  compareVectors( v.getUnitDirectionX(), WVector3d( 1.0, 0.0, 0.0 ), 0.0001 );
60  compareVectors( v.getUnitDirectionY(), WVector3d( 0.0, 1.0, 0.0 ), 0.0001 );
61  compareVectors( v.getUnitDirectionZ(), WVector3d( 0.0, 0.0, 1.0 ), 0.0001 );
62  compareVectors( v.getDirectionX(), WVector3d( 1.0, 0.0, 0.0 ), 0.0001 );
63  compareVectors( v.getDirectionY(), WVector3d( 0.0, 1.0, 0.0 ), 0.0001 );
64  compareVectors( v.getDirectionZ(), WVector3d( 0.0, 0.0, 1.0 ), 0.0001 );
65  compareVectors( v.getOrigin(), WVector3d( 0.0, 0.0, 0.0 ), 0.0001 );
66  }
67  {
68  TS_ASSERT_THROWS_NOTHING( WGridTransformOrtho v( 2.2, 3.3, -1.0 ) );
69  TS_ASSERT_THROWS( WGridTransformOrtho v( 0.0, 0.0, 1.0 ), WPreconditionNotMet );
70  TS_ASSERT_THROWS( WGridTransformOrtho v( 0.0, 2.0, 1.0 ), WPreconditionNotMet );
71  TS_ASSERT_THROWS( WGridTransformOrtho v( 1.0, 1.0, 0.0 ), WPreconditionNotMet );
72  }
73  {
74  WGridTransformOrtho v( 2.2, 3.3, -1.0 );
75  TS_ASSERT_EQUALS( v.getOffsetX(), 2.2 );
76  TS_ASSERT_EQUALS( v.getOffsetY(), 3.3 );
77  TS_ASSERT_EQUALS( v.getOffsetZ(), 1.0 );
78  compareVectors( v.getUnitDirectionX(), WVector3d( 1.0, 0.0, 0.0 ), 0.0001 );
79  compareVectors( v.getUnitDirectionY(), WVector3d( 0.0, 1.0, 0.0 ), 0.0001 );
80  compareVectors( v.getUnitDirectionZ(), WVector3d( 0.0, 0.0, -1.0 ), 0.0001 );
81  compareVectors( v.getDirectionX(), WVector3d( 2.2, 0.0, 0.0 ), 0.0001 );
82  compareVectors( v.getDirectionY(), WVector3d( 0.0, 3.3, 0.0 ), 0.0001 );
83  compareVectors( v.getDirectionZ(), WVector3d( 0.0, 0.0, -1.0 ), 0.0001 );
84  compareVectors( v.getOrigin(), WVector3d( 0.0, 0.0, 0.0 ), 0.0001 );
85  }
86  {
87  WMatrix< double > mat( 4, 4 );
88  mat.makeIdentity();
89  mat( 0, 0 ) = 2.2;
90  mat( 1, 1 ) = 3.3;
91  mat( 2, 2 ) = 0.0;
92 
93  TS_ASSERT_THROWS( WGridTransformOrtho v( mat ), WPreconditionNotMet );
94  }
95  {
96  WMatrix< double > mat( 4, 4 );
97  mat.makeIdentity();
98  mat( 0, 0 ) = 2.2;
99  mat( 1, 0 ) = 0.1;
100  mat( 1, 1 ) = 3.3;
101  mat( 2, 2 ) = 1.0;
102 
103  TS_ASSERT_THROWS( WGridTransformOrtho v( mat ), WPreconditionNotMet );
104  }
105  {
106  WMatrix< double > mat( 4, 4 );
107  mat.makeIdentity();
108  mat( 0, 0 ) = 2.0;
109  mat( 1, 0 ) = 2.0;
110  mat( 1, 1 ) = 3.0;
111  mat( 0, 1 ) = -3.0;
112  mat( 2, 2 ) = 4.4;
113  mat( 0, 3 ) = 1.0;
114  mat( 1, 3 ) = 2.0;
115  mat( 2, 3 ) = 0.5;
116 
117  WGridTransformOrtho v( mat );
118  TS_ASSERT_EQUALS( v.getOffsetX(), sqrt( 8.0 ) );
119  TS_ASSERT_EQUALS( v.getOffsetY(), sqrt( 18.0 ) );
120  TS_ASSERT_EQUALS( v.getOffsetZ(), 4.4 );
121  TS_ASSERT_DELTA( length( v.getUnitDirectionX() - WVector3d( 0.5 * sqrt( 2.0 ), 0.5 * sqrt( 2.0 ), 0.0 ) ), 0.0, 1e-13 );
122  TS_ASSERT_DELTA( length( v.getUnitDirectionY() - WVector3d( -0.5 * sqrt( 2.0 ), 0.5 * sqrt( 2.0 ), 0.0 ) ), 0.0, 1e-13 );
123  compareVectors( v.getUnitDirectionZ(), WVector3d( 0.0, 0.0, 1.0 ), 0.0001 );
124  compareVectors( v.getDirectionX(), WVector3d( 2.0, 2.0, 0.0 ), 0.0001 );
125  compareVectors( v.getDirectionY(), WVector3d( -3.0, 3.0, 0.0 ), 0.0001 );
126  compareVectors( v.getDirectionZ(), WVector3d( 0.0, 0.0, 4.4 ), 0.0001 );
127  compareVectors( v.getOrigin(), WVector3d( 1.0, 2.0, 0.5 ), 0.0001 );
128  }
129  }
130 
131  /**
132  * Different constructors should not yield differently initialized
133  * data fields.
134  */
136  {
137  WMatrix< double > mat( 4, 4 );
138  mat.makeIdentity();
139  mat( 0, 0 ) = 2.2;
140  mat( 1, 1 ) = 3.3;
141  mat( 2, 2 ) = 4.4;
142 
143  WGridTransformOrtho t1( mat );
144  WGridTransformOrtho t2( 2.2, 3.3, 4.4 );
145 
146  TS_ASSERT_EQUALS( t1.getOffsetX(), t2.getOffsetX() );
147  TS_ASSERT_EQUALS( t1.getOffsetY(), t2.getOffsetY() );
148  TS_ASSERT_EQUALS( t1.getOffsetZ(), t2.getOffsetZ() );
149 
150  compareVectors( t1.getDirectionX(), t2.getDirectionX(), 0.0001 );
151  compareVectors( t1.getDirectionY(), t2.getDirectionY(), 0.0001 );
152  compareVectors( t1.getDirectionZ(), t2.getDirectionZ(), 0.0001 );
153 
154  compareVectors( t1.getOrigin(), t2.getOrigin(), 0.0001 );
155  }
156 
157  /**
158  * Test transformation from grid space to world space.
159  */
161  {
162  {
163  // test identity transform
164  WVector3d v( -7.64, 8.73, -0.0063 );
166 
167  compareVectors( v, t.positionToWorldSpace( v ), 0.0001 );
168  compareVectors( v, t.directionToWorldSpace( v ), 0.0001 );
169  }
170 
171  {
172  WGridTransformOrtho t( 2.2, 3.3, 4.4 );
173  WVector3d v( 1.0, 1.0, 1.0 );
174 
175  compareVectors( WVector3d( 2.2, 3.3, 4.4 ), t.positionToWorldSpace( v ), 0.0001 );
176  compareVectors( WVector3d( 2.2, 3.3, 4.4 ), t.directionToWorldSpace( v ), 0.0001 );
177  }
178 
179  {
180  WMatrix< double > mat( 4, 4 );
181  mat.makeIdentity();
182  mat( 0, 0 ) = 2.2;
183  mat( 1, 1 ) = 3.3;
184  mat( 2, 2 ) = 4.4;
185  mat( 0, 3 ) = 1.0;
186  mat( 1, 3 ) = 2.0;
187  mat( 2, 3 ) = 0.5;
188 
189  WGridTransformOrtho t( mat );
190  WVector3d v( 1.0, 1.0, 1.0 );
191 
192  compareVectors( WVector3d( 3.2, 5.3, 4.9 ), t.positionToWorldSpace( v ), 0.0001 );
193  compareVectors( WVector3d( 2.2, 3.3, 4.4 ), t.directionToWorldSpace( v ), 0.0001 );
194  }
195  {
196  WMatrix< double > mat( 4, 4 );
197  mat.makeIdentity();
198  mat( 0, 0 ) = 2.0;
199  mat( 1, 0 ) = 2.0;
200  mat( 1, 1 ) = 3.0;
201  mat( 0, 1 ) = -3.0;
202  mat( 2, 2 ) = 4.4;
203  mat( 0, 3 ) = 1.0;
204  mat( 1, 3 ) = 2.0;
205  mat( 2, 3 ) = 0.5;
206 
207  WGridTransformOrtho t( mat );
208  WVector3d v( 1.0, 1.0, 1.0 );
209 
210  WVector3d w = t.positionToWorldSpace( v );
211  TS_ASSERT_DELTA( 0.0, w[ 0 ], 0.0001 );
212  TS_ASSERT_DELTA( 7.0, w[ 1 ], 0.0001 );
213  TS_ASSERT_DELTA( 4.9, w[ 2 ], 0.0001 );
214  compareVectors( WVector3d( -1.0, 5.0, 4.4 ), t.directionToWorldSpace( v ), 0.0001 );
215  }
216  }
217 
218  /**
219  * Test transformation from world space to grid space.
220  */
222  {
223  {
224  // test identity transform
225  WVector3d v( -7.64, 8.73, -0.0063 );
227 
228  compareVectors( v, t.positionToGridSpace( v ), 0.0001 );
229  compareVectors( v, t.directionToGridSpace( v ), 0.0001 );
230  }
231 
232  {
233  WGridTransformOrtho t( 2.2, 3.3, 4.4 );
234  WVector3d v( 2.2, 3.3, 4.4 );
235 
236  compareVectors( WVector3d( 1.0, 1.0, 1.0 ), t.positionToGridSpace( v ), 0.0001 );
237  compareVectors( WVector3d( 1.0, 1.0, 1.0 ), t.directionToGridSpace( v ), 0.0001 );
238  }
239 
240  {
241  WMatrix< double > mat( 4, 4 );
242  mat.makeIdentity();
243  mat( 0, 0 ) = 2.2;
244  mat( 1, 1 ) = 3.3;
245  mat( 2, 2 ) = 4.4;
246  mat( 0, 3 ) = 1.0;
247  mat( 1, 3 ) = 2.0;
248  mat( 2, 3 ) = 0.5;
249 
250  WGridTransformOrtho t( mat );
251 
252  compareVectors( WVector3d( 1.0, 1.0, 1.0 ), t.positionToGridSpace( WVector3d( 3.2, 5.3, 4.9 ) ), 0.0001 );
253  compareVectors( WVector3d( 1.0, 1.0, 1.0 ), t.directionToGridSpace( WVector3d( 2.2, 3.3, 4.4 ) ), 0.0001 );
254  }
255  {
256  WMatrix< double > mat( 4, 4 );
257  mat.makeIdentity();
258  mat( 0, 0 ) = 2.0;
259  mat( 1, 0 ) = 2.0;
260  mat( 1, 1 ) = 3.0;
261  mat( 0, 1 ) = -3.0;
262  mat( 2, 2 ) = 4.4;
263  mat( 0, 3 ) = 1.0;
264  mat( 1, 3 ) = 2.0;
265  mat( 2, 3 ) = 0.5;
266 
267  WGridTransformOrtho t( mat );
268 
269  TS_ASSERT_DELTA( length( WVector3d( 1.0, 1.0, 1.0 ) // NOLINT
270  - t.positionToGridSpace( WVector3d( 0.0, 7.0, 4.9 ) ) ), 0.0, 1e-13 );
271  TS_ASSERT_DELTA( length( WVector3d( 1.0, 1.0, 1.0 ) // NOLINT
272  - t.directionToGridSpace( WVector3d( -1.0, 5.0, 4.4 ) ) ), 0.0, 1e-13 );
273  }
274  }
275 
276 private:
277  /**
278  * Compares two vectors, element by element.
279  *
280  * \param v1 The first vector.
281  * \param v2 The second vector.
282  * \param delta The maximum absolute difference between the elements of the vectors.
283  */
284  void compareVectors( WVector3d const& v1, WVector3d const& v2, double delta ) const
285  {
286  TS_ASSERT_DELTA( v1[ 0 ], v2[ 0 ], delta );
287  TS_ASSERT_DELTA( v1[ 1 ], v2[ 1 ], delta );
288  TS_ASSERT_DELTA( v1[ 2 ], v2[ 2 ], delta );
289  }
290 };
291 
292 #endif // WGRIDTRANSFORMORTHO_TEST_H
Vector3Type getDirectionX() const
Returns the vector determining the direction of samples in x direction.
T getOffsetY() const
Returns the distance between samples in y direction.
WMatrix & makeIdentity()
Makes the matrix contain the identity matrix, i.e.
Definition: WMatrix.h:352
Vector3Type getOrigin() const
Returns the position of the origin of the grid.
void testCompareConstructors()
Different constructors should not yield differently initialized data fields.
Vector3Type getDirectionY() const
Returns the vector determining the direction of samples in y direction.
Vector3Type positionToGridSpace(Vector3Type const &position) const
Transforms a position from world space to grid space.
void testTransformationToWorldSpace()
Test transformation from grid space to world space.
void testInstantiation()
Test if all data fields get initialized correctly.
T getOffsetX() const
Returns the distance between samples in x direction.
Vector3Type directionToWorldSpace(Vector3Type const &direction) const
Transforms a direction from grid space to world space.
Vector3Type getDirectionZ() const
Returns the vector determining the direction of samples in z direction.
Vector3Type positionToWorldSpace(Vector3Type const &position) const
Transforms a position from grid space to world space.
Vector3Type getUnitDirectionZ() const
Returns the vector determining the unit (normalized) direction of samples in z direction.
Vector3Type getUnitDirectionY() const
Returns the vector determining the unit (normalized) direction of samples in y direction.
void compareVectors(WVector3d const &v1, WVector3d const &v2, double delta) const
Compares two vectors, element by element.
An exception that gets thrown when preconditions of a function are not met.
Tests the WGridTransform class.
void testTransformationToGridSpace()
Test transformation from world space to grid space.
Implements an orthogonal grid transformation.
Vector3Type directionToGridSpace(Vector3Type const &direction) const
Transforms a direction from world space to grid space.
Vector3Type getUnitDirectionX() const
Returns the vector determining the unit (normalized) direction of samples in x direction.
T getOffsetZ() const
Returns the distance between samples in z direction.