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