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