OpenWalnut  1.3.1
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  TS_ASSERT_EQUALS( v.getUnitDirectionX(), WVector3d( 1.0, 0.0, 0.0 ) );
60  TS_ASSERT_EQUALS( v.getUnitDirectionY(), WVector3d( 0.0, 1.0, 0.0 ) );
61  TS_ASSERT_EQUALS( v.getUnitDirectionZ(), WVector3d( 0.0, 0.0, 1.0 ) );
62  TS_ASSERT_EQUALS( v.getDirectionX(), WVector3d( 1.0, 0.0, 0.0 ) );
63  TS_ASSERT_EQUALS( v.getDirectionY(), WVector3d( 0.0, 1.0, 0.0 ) );
64  TS_ASSERT_EQUALS( v.getDirectionZ(), WVector3d( 0.0, 0.0, 1.0 ) );
65  TS_ASSERT_EQUALS( v.getOrigin(), WVector3d( 0.0, 0.0, 0.0 ) );
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  TS_ASSERT_EQUALS( v.getUnitDirectionX(), WVector3d( 1.0, 0.0, 0.0 ) );
79  TS_ASSERT_EQUALS( v.getUnitDirectionY(), WVector3d( 0.0, 1.0, 0.0 ) );
80  TS_ASSERT_EQUALS( v.getUnitDirectionZ(), WVector3d( 0.0, 0.0, -1.0 ) );
81  TS_ASSERT_EQUALS( v.getDirectionX(), WVector3d( 2.2, 0.0, 0.0 ) );
82  TS_ASSERT_EQUALS( v.getDirectionY(), WVector3d( 0.0, 3.3, 0.0 ) );
83  TS_ASSERT_EQUALS( v.getDirectionZ(), WVector3d( 0.0, 0.0, -1.0 ) );
84  TS_ASSERT_EQUALS( v.getOrigin(), WVector3d( 0.0, 0.0, 0.0 ) );
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  TS_ASSERT_EQUALS( v.getUnitDirectionZ(), WVector3d( 0.0, 0.0, 1.0 ) );
124  TS_ASSERT_EQUALS( v.getDirectionX(), WVector3d( 2.0, 2.0, 0.0 ) );
125  TS_ASSERT_EQUALS( v.getDirectionY(), WVector3d( -3.0, 3.0, 0.0 ) );
126  TS_ASSERT_EQUALS( v.getDirectionZ(), WVector3d( 0.0, 0.0, 4.4 ) );
127  TS_ASSERT_EQUALS( v.getOrigin(), WVector3d( 1.0, 2.0, 0.5 ) );
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  TS_ASSERT_EQUALS( t1.getDirectionX(), t2.getDirectionX() );
151  TS_ASSERT_EQUALS( t1.getDirectionY(), t2.getDirectionY() );
152  TS_ASSERT_EQUALS( t1.getDirectionZ(), t2.getDirectionZ() );
153 
154  TS_ASSERT_EQUALS( t1.getOrigin(), t2.getOrigin() );
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  TS_ASSERT_EQUALS( v, t.positionToWorldSpace( v ) );
168  TS_ASSERT_EQUALS( v, t.directionToWorldSpace( v ) );
169  }
170 
171  {
172  WGridTransformOrtho t( 2.2, 3.3, 4.4 );
173  WVector3d v( 1.0, 1.0, 1.0 );
174 
175  TS_ASSERT_EQUALS( WVector3d( 2.2, 3.3, 4.4 ), t.positionToWorldSpace( v ) );
176  TS_ASSERT_EQUALS( WVector3d( 2.2, 3.3, 4.4 ), t.directionToWorldSpace( v ) );
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  TS_ASSERT_EQUALS( WVector3d( 3.2, 5.3, 4.9 ), t.positionToWorldSpace( v ) );
193  TS_ASSERT_EQUALS( WVector3d( 2.2, 3.3, 4.4 ), t.directionToWorldSpace( v ) );
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  TS_ASSERT_EQUALS( WVector3d( -1.0, 5.0, 4.4 ), t.directionToWorldSpace( v ) );
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  TS_ASSERT_EQUALS( v, t.positionToGridSpace( v ) );
229  TS_ASSERT_EQUALS( v, t.directionToGridSpace( v ) );
230  }
231 
232  {
233  WGridTransformOrtho t( 2.2, 3.3, 4.4 );
234  WVector3d v( 2.2, 3.3, 4.4 );
235 
236  TS_ASSERT_EQUALS( WVector3d( 1.0, 1.0, 1.0 ), t.positionToGridSpace( v ) );
237  TS_ASSERT_EQUALS( WVector3d( 1.0, 1.0, 1.0 ), t.directionToGridSpace( v ) );
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  TS_ASSERT_EQUALS( WVector3d( 1.0, 1.0, 1.0 ), t.positionToGridSpace( WVector3d( 3.2, 5.3, 4.9 ) ) );
253  TS_ASSERT_EQUALS( WVector3d( 1.0, 1.0, 1.0 ), t.directionToGridSpace( WVector3d( 2.2, 3.3, 4.4 ) ) );
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 
277 #endif // WGRIDTRANSFORMORTHO_TEST_H