Decomposer_test.cxx
Go to the documentation of this file.
1 /**
2  * @file Decomposer_test.cxx
3  * @brief Unit test for Decomposer class
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date December 8th, 2016
6  *
7  * Linking information: currently, both Decomposer.h and pow.h are
8  * header-only libraries; the former does require ROOT vectors. Therefore the
9  * only needed external library (beside Boost) is ROOT for the vector data type.
10  *
11  * Run the test without arguments.
12  *
13  */
14 
15 // Boost libraries
16 #define BOOST_TEST_MODULE ( decomposer_test )
17 #include <boost/test/unit_test.hpp>
18 
19 // LArSoft libraries
22 
23 // ROOT libraries
24 #include "TVector3.h"
25 #include "TVector2.h"
26 #include "Math/GenVector/Cartesian2D.h"
27 #include "Math/GenVector/Cartesian3D.h"
28 #include "Math/GenVector/PositionVector3D.h"
29 #include "Math/GenVector/DisplacementVector3D.h"
30 #include "Math/GenVector/DisplacementVector2D.h"
31 
32 // C/C++ standard libraries
33 #include <utility> // std::move()
34 #include <ostream>
35 
37 
38 //------------------------------------------------------------------------------
39 template <typename Vector, typename Point, typename Proj>
41 
42  //
43  // Test including all methods of geo::Decomposer<>
44  //
45  using Decomposer_t = geo::Decomposer<Vector, Point, Proj>;
46 
47  using Vector3D_t = typename Decomposer_t::Vector_t;
48  using Point3D_t = typename Decomposer_t::Point_t;
49  using Projection_t = typename Decomposer_t::Projection_t;
50  using Distance_t [[gnu::unused]] = typename Decomposer_t::Distance_t;
51  using DecomposedVector_t = typename Decomposer_t::DecomposedVector_t;
52  using AffinePlaneBase_t = typename Decomposer_t::AffinePlaneBase_t;
53 
54  //
55  // preparation
56  //
57  static const Point3D_t Origin{ 0.0, 0.0, 0.0 };
58  static const Point3D_t ReferencePoint{ -5.0, 10.0, 15.0 };
59  static const Vector3D_t NullVector{ 0.0, 0.0, 0.0 };
60  static const Vector3D_t Xaxis{ 1.0, 0.0, 0.0 };
61  static const Vector3D_t Yaxis{ 0.0, 1.0, 0.0 };
62  static const Vector3D_t Zaxis{ 0.0, 0.0, 1.0 };
63 
64 
65  //
66  // constructors
67  //
68 
69  //
70  // Default constructor: projection on (x,y) with origin (0, 0, 0)
71  //
72  // Decomposer()
73  //
74  // Constructor: specifies a base (an origin and two direction vectors)
75  //
76  // Decomposer(AffinePlaneBase_t&& base)
77  // Decomposer(AffinePlaneBase_t const& base)
78  //
79 
80  Decomposer_t defaultBase;
81  BOOST_TEST(defaultBase.MainDir() == Xaxis);
82  BOOST_TEST(defaultBase.SecondaryDir() == Yaxis);
83  BOOST_TEST(defaultBase.NormalDir() == Zaxis);
84  BOOST_TEST(defaultBase.ReferencePoint() == Origin);
85 
86  AffinePlaneBase_t rotatedBase(ReferencePoint, Yaxis, Zaxis);
87 
88  Decomposer_t rotatedBase1_1(rotatedBase);
89  BOOST_TEST(rotatedBase1_1.MainDir() == Yaxis);
90  BOOST_TEST(rotatedBase1_1.SecondaryDir() == Zaxis);
91  BOOST_TEST(rotatedBase1_1.NormalDir() == Xaxis);
92  BOOST_TEST(rotatedBase1_1.ReferencePoint() == ReferencePoint);
93 
94  Decomposer_t rotatedBase1_2(std::move(rotatedBase));
95  BOOST_TEST(rotatedBase1_2.MainDir() == Yaxis);
96  BOOST_TEST(rotatedBase1_2.SecondaryDir() == Zaxis);
97  BOOST_TEST(rotatedBase1_2.NormalDir() == Xaxis);
98  BOOST_TEST(rotatedBase1_2.ReferencePoint() == ReferencePoint);
99 
100 
101  //
102  // setters
103  //
104  AffinePlaneBase_t negativeBase(ReferencePoint, Yaxis, Xaxis);
105 
106  //
107  // Change projection base
108  //
109  // void SetBase(AffinePlaneBase_t&& base)
110  //
111  // Change projection base
112  //
113  // void SetBase(AffinePlaneBase_t const& base)
114  //
115  // Change the 3D point of the reference frame origin
116  //
117  // void SetOrigin(Point_t const& point)
118  //
119  // Change the main direction of the projection base
120  //
121  // void SetMainDir(Vector_t const& dir)
122  //
123  // Change the secondary direction of the projection base
124  //
125  // void SetSecondaryDir(Vector_t const& dir)
126  //
127  //
128  defaultBase.SetBase(negativeBase);
129  BOOST_TEST(defaultBase.MainDir() == Yaxis);
130  BOOST_TEST(defaultBase.SecondaryDir() == Xaxis);
131  BOOST_TEST(defaultBase.NormalDir() == -Zaxis);
132  BOOST_TEST(defaultBase.ReferencePoint() == ReferencePoint);
133 
134  defaultBase.SetOrigin(Origin);
135  BOOST_TEST(defaultBase.MainDir() == Yaxis);
136  BOOST_TEST(defaultBase.SecondaryDir() == Xaxis);
137  BOOST_TEST(defaultBase.NormalDir() == -Zaxis);
138  BOOST_TEST(defaultBase.ReferencePoint() == Origin);
139 
140  defaultBase.SetMainDir(Zaxis);
141  BOOST_TEST(defaultBase.MainDir() == Zaxis);
142  BOOST_TEST(defaultBase.SecondaryDir() == Xaxis);
143  BOOST_TEST(defaultBase.NormalDir() == Yaxis);
144  BOOST_TEST(defaultBase.ReferencePoint() == Origin);
145 
146  defaultBase.SetSecondaryDir(Yaxis);
147  BOOST_TEST(defaultBase.MainDir() == Zaxis);
148  BOOST_TEST(defaultBase.SecondaryDir() == Yaxis);
149  BOOST_TEST(defaultBase.NormalDir() == -Xaxis);
150  BOOST_TEST(defaultBase.ReferencePoint() == Origin);
151 
152  defaultBase.SetMainDir(Xaxis);
153  BOOST_TEST(defaultBase.MainDir() == Xaxis);
154  BOOST_TEST(defaultBase.SecondaryDir() == Yaxis);
155  BOOST_TEST(defaultBase.NormalDir() == Zaxis);
156  BOOST_TEST(defaultBase.ReferencePoint() == Origin);
157 
158 
159  //
160  // getters
161  //
162 
163  AffinePlaneBase_t decompBase(ReferencePoint, Yaxis, Zaxis);
164  Decomposer_t decomp(decompBase);
165 
166  // Returns the reference point for the plane coordinate, as a 3D point
167  //
168  // Point_t ReferencePoint() const
169  //
170  // Returns the base of the decomposition
171  //
172  // AffinePlaneBase_t const& Base() const
173  //
174  // Returns the plane main axis direction
175  //
176  // Vector_t const& MainDir() const
177  //
178  // Returns the plane secondary axis direction
179  //
180  // Vector_t const& SecondaryDir() const
181  //
182  // Returns the plane normal axis direction
183  //
184  // Vector_t const& NormalDir() const
185  //
186 
187  BOOST_TEST(decomp.ReferencePoint() == ReferencePoint);
188  BOOST_TEST(decomp.MainDir() == Yaxis);
189  BOOST_TEST(decomp.SecondaryDir() == Zaxis);
190  BOOST_TEST(decomp.NormalDir() == Xaxis);
191  BOOST_TEST(decomp.Base().Origin() == ReferencePoint);
192  BOOST_TEST(decomp.Base().MainDir() == Yaxis);
193  BOOST_TEST(decomp.Base().SecondaryDir() == Zaxis);
194  BOOST_TEST(decomp.Base().NormalDir() == Xaxis);
195 
196 
197  //
198  // projections: of 3D points
199  //
200 
201  // decompBase: ( y, z, x ); ReferencePoint: { -5.0, 10.0, 15.0 }
202  Point3D_t point(ReferencePoint + Vector3D_t(3.0, 4.0, 5.0)); // { -2, 14, 20 }
203 
204  //
205  // Returns the main component of a point
206  //
207  // auto PointMainComponent(Point_t const& point) const
208  //
209  // Returns the secondary component of a point
210  //
211  // auto PointSecondaryComponent(Point_t const& point) const
212  //
213  // Returns the secondary component of a point
214  //
215  // auto PointNormalComponent(Point_t const& point) const
216  //
217  // Returns the projection of the specified point on the plane
218  //
219  // Projection_t ProjectPointOnPlane(Point_t const& point) const
220  //
221  // Decomposes a 3D point in two components
222  //
223  // DecomposedVector_t DecomposePoint(Point_t const& point) const
224  //
225 
226  auto const tol = 0.01% tolerance();
227  BOOST_TEST(decomp.PointMainComponent (point) == 4.0, tol);
228  BOOST_TEST(decomp.PointSecondaryComponent(point) == 5.0, tol);
229  BOOST_TEST(decomp.PointNormalComponent (point) == 3.0, tol);
230 
231  {
232  auto proj = decomp.ProjectPointOnPlane(point);
233  BOOST_TEST(proj.X() == 4.0, tol);
234  BOOST_TEST(proj.Y() == 5.0, tol);
235  }
236 
237  {
238  auto decomposed = decomp.DecomposePoint(point);
239  BOOST_TEST(decomposed.distance == 3.0, tol);
240  BOOST_TEST(decomposed.projection.X() == 4.0, tol);
241  BOOST_TEST(decomposed.projection.Y() == 5.0, tol);
242  }
243 
244 
245  //
246  // projections: of 3D vectors
247  //
248 
249  // decompBase: ( y, z, x ); ReferencePoint: { -5.0, 10.0, 15.0 }
250  Vector3D_t vector(2.0, 3.0, 4.0);
251 
252  //
253  // Returns the main component of a vector
254  //
255  // auto VectorMainComponent(Vector_t const& v) const
256  //
257  // Returns the secondary component of a vector
258  //
259  // auto VectorSecondaryComponent(Vector_t const& v) const
260  //
261  // Returns the secondary component of a vector
262  //
263  // auto VectorNormalComponent(Vector_t const& v) const
264  //
265  // Returns the projection of the specified vector on the plane
266  //
267  // Projection_t ProjectVectorOnPlane(Point_t const& v) const
268  //
269  // Decomposes a 3D vector in two components
270  //
271  // DecomposedVector_t DecomposeVector(Vector_t const& v) const
272  //
273  // Returns the main component of a projection vector
274  //
275  // auto MainComponent(Projection_t const& v) const
276  //
277  // Returns the secondary component of a projection vector
278  //
279  // auto SecondaryComponent(Projection_t const& v) const
280  //
281 
282  BOOST_TEST(decomp.VectorMainComponent (vector) == 3.0, tol);
283  BOOST_TEST(decomp.VectorSecondaryComponent(vector) == 4.0, tol);
284  BOOST_TEST(decomp.VectorNormalComponent (vector) == 2.0, tol);
285 
286  {
287  auto proj = decomp.ProjectVectorOnPlane(vector);
288  BOOST_TEST(proj.X() == 3.0, tol);
289  BOOST_TEST(proj.Y() == 4.0, tol);
290  }
291 
292  {
293  auto decomposed = decomp.DecomposeVector(vector);
294  BOOST_TEST(decomposed.distance == 2.0, tol);
295  BOOST_TEST(decomposed.projection.X() == 3.0, tol);
296  BOOST_TEST(decomposed.projection.Y() == 4.0, tol);
297  }
298 
299  //
300  // projection: projected vector on the plane
301  //
302  Projection_t proj(3.0, 4.0);
303 
304  BOOST_TEST(decomp.MainComponent (proj) == 3.0, tol);
305  BOOST_TEST(decomp.SecondaryComponent(proj) == 4.0, tol);
306 
307 
308  //
309  // composition: point
310  //
311  // decompBase: ( y, z, x ); ReferencePoint: { -5.0, 10.0, 15.0 }
312  DecomposedVector_t decomposed(
313  3.0, // distance from plane
314  { 4.0, 5.0 } // projection on plane in local plane coordinates
315  );
316 
317  // Returns the 3D point from composition of projection and distance
318  //
319  // Point_t ComposePoint(DecomposedVector_t const& decomp) const
320  //
321  // Returns the 3D point from composition of projection and distance
322  //
323  // Point_t ComposePoint(double distance, Projection_t const& proj) const
324  //
325  // Returns the 3D vector from composition of projection and distance
326  //
327  // Vector_t ComposeVector(DecomposedVector_t const& decomp) const
328  //
329  // Returns the 3D vector from composition of projection and distance
330  //
331  // Vector_t ComposeVector(double distance, Projection_t const& proj) const
332  //
333 
334  {
335  auto comp = decomp.ComposePoint(decomposed.distance, decomposed.projection);
336  BOOST_TEST(comp.X() == -2.0, tol);
337  BOOST_TEST(comp.Y() == 14.0, tol);
338  BOOST_TEST(comp.Z() == 20.0, tol);
339  }
340 
341  {
342  auto comp = decomp.ComposePoint(decomposed);
343  BOOST_TEST(comp.X() == -2.0, tol);
344  BOOST_TEST(comp.Y() == 14.0, tol);
345  BOOST_TEST(comp.Z() == 20.0, tol);
346  }
347 
348 
349  //
350  // composition: vector
351  //
352 
353  {
354  auto comp = decomp.ComposeVector
355  (decomposed.distance, decomposed.projection);
356  BOOST_TEST(comp.X() == 3.0, tol);
357  BOOST_TEST(comp.Y() == 4.0, tol);
358  BOOST_TEST(comp.Z() == 5.0, tol);
359  }
360 
361  {
362  auto comp = decomp.ComposeVector(decomposed);
363  BOOST_TEST(comp.X() == 3.0, tol);
364  BOOST_TEST(comp.Y() == 4.0, tol);
365  BOOST_TEST(comp.Z() == 5.0, tol);
366  }
367 
368 
369 } // StandardDecomposerTest<>()
370 
371 
372 //------------------------------------------------------------------------------
373 BOOST_AUTO_TEST_CASE(TVectorDecomposerTestCase) {
374 
375  StandardDecomposerTest<TVector3, TVector3, TVector2>();
376 
377 } // BOOST_AUTO_TEST_CASE(TVectorDecomposerTestCase)
378 
379 
380 BOOST_AUTO_TEST_CASE(GenVectorDecomposerTestCase) {
381 
382  using Point_t = ROOT::Math::PositionVector3D<ROOT::Math::Cartesian3D<double>>;
383  using Vector_t
384  = ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double>>;
385  using Projection_t
386  = ROOT::Math::DisplacementVector2D<ROOT::Math::Cartesian2D<double>>;
387 
388  StandardDecomposerTest<Vector_t, Point_t, Projection_t>();
389 
390 } // BOOST_AUTO_TEST_CASE(GenVectorDecomposerTestCase)
auto const tol
Definition: SurfXYZTest.cc:16
auto const tolerance
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >> Point_t
constexpr Vector Yaxis()
Returns a y axis vector of the specified type.
Definition: geo_vectors.h:219
struct vector vector
Class with methods to decompose and compose back vectors.
Definition: Decomposer.h:391
Classes to project and compose a vector on a plane.
BOOST_AUTO_TEST_CASE(TVectorDecomposerTestCase)
def move(depos, offset)
Definition: depos.py:107
constexpr Vector Xaxis()
Returns a x axis vector of the specified type.
Definition: geo_vectors.h:215
Specializations of geo_vectors_utils.h for ROOT old vector types.
constexpr Vector Zaxis()
Returns a z axis vector of the specified type.
Definition: geo_vectors.h:223
void StandardDecomposerTest()
recob::tracking::Vector_t Vector_t