All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LineClosestPoint_test.cc
Go to the documentation of this file.
1 /**
2  * @file LineClosestPoint_test.cc
3  * @brief Test of `LineClosestPoint.h` utilities.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date June 29, 2021
6  * @see `larcorealg/Geometry/LineClosestPoint.h`
7  */
8 
9 
10 // Boost libraries
11 #define BOOST_TEST_MODULE LineClosestPoint_test
12 #include <boost/test/unit_test.hpp> // BOOST_AUTO_TEST_CASE(), BOOST_TEST()
13 
14 // LArSoft libraries
17 
18 // C++ standard library
19 #include <utility> // std::pair<>
20 #include <type_traits> // std::is_same_v<>
21 #include <cmath> // std::sqrt()
22 
23 
24 // =============================================================================
26 
27  auto const tol = boost::test_tools::tolerance(0.001);
28 
30  geo::origin() - geo::Zaxis() - 3.0 * geo::Xaxis(), geo::Xaxis(),
31  geo::origin() + geo::Zaxis() + 2.0 * geo::Yaxis(), geo::Yaxis()
32  );
33 
34  BOOST_TEST(p.X() == 0.0, tol);
35  BOOST_TEST(p.Y() == 0.0, tol);
36  BOOST_TEST(p.Z() == -1.0, tol);
37 
38 } // LineClosestPointSimple_test()
39 
40 
41 // -----------------------------------------------------------------------------
43 
44  auto const tol = boost::test_tools::tolerance(0.001);
45 
48  geo::origin() + geo::Yaxis(), (geo::Xaxis() + geo::Zaxis()) / std::sqrt(2.0)
49  );
50 
51  BOOST_TEST(p.X() == 0.0, tol);
52  BOOST_TEST(p.Y() == 0.0, tol);
53  BOOST_TEST(p.Z() == 0.0, tol);
54 
55 } // LineClosestPointSimple45_test()
56 
57 
58 // -----------------------------------------------------------------------------
60 
61  auto const tol = boost::test_tools::tolerance(0.001);
62 
63  auto const [ p, ofsA, ofsB ] = geo::LineClosestPointAndOffsets(
64  geo::origin() - 3.0 * geo::Xaxis(), geo::Xaxis(),
65  geo::origin() + geo::Zaxis() + 2.0 * geo::Yaxis(), geo::Yaxis()
66  );
67 
68  BOOST_TEST(p.X() == 0.0, tol);
69  BOOST_TEST(p.Y() == 0.0, tol);
70  BOOST_TEST(p.Z() == 0.0, tol);
71  BOOST_TEST(ofsA == +3.0, tol);
72  BOOST_TEST(ofsB == -2.0, tol);
73 
74 } // LineClosestPointAndOffsets_test()
75 
76 
77 // -----------------------------------------------------------------------------
79 
80  auto const tol = boost::test_tools::tolerance(0.001);
81 
82  auto const [ p, ofsA, ofsB ] = geo::LineClosestPointAndOffsets(
83  geo::origin() - 3.0 * geo::Xaxis(), 2.0 * geo::Xaxis(),
84  geo::origin() + geo::Zaxis() + 2.0 * geo::Yaxis(), -2.0 * geo::Yaxis()
85  );
86 
87  BOOST_TEST(p.X() == 0.0, tol);
88  BOOST_TEST(p.Y() == 0.0, tol);
89  BOOST_TEST(p.Z() == 0.0, tol);
90  BOOST_TEST(ofsA == (+3.0 / 2.0), tol);
91  BOOST_TEST(ofsB == (-2.0 / -2.0), tol);
92 
93 } // LineClosestPointWithScaledDirs_test()
94 
95 
96 // -----------------------------------------------------------------------------
98 
99  auto const tol = boost::test_tools::tolerance(0.001);
100 
101  auto const [ p, ofsA, ofsB ] = geo::LineClosestPointAndOffsets(
102  geo::origin() - 3.0 * geo::Xaxis(), 1.5 * geo::Xaxis(),
103  geo::origin() + geo::Zaxis() + 2.0 * geo::Yaxis(), -2.0 * geo::Yaxis()
104  );
105 
106  BOOST_TEST(p.X() == 0.0, tol);
107  BOOST_TEST(p.Y() == 0.0, tol);
108  BOOST_TEST(p.Z() == 0.0, tol);
109  BOOST_TEST(ofsA == (+3.0 / 1.5), tol);
110  BOOST_TEST(ofsB == (-2.0 / -2.0), tol);
111 
112 } // LineClosestPointWithNonHomogeneousDirs_test()
113 
114 
115 // -----------------------------------------------------------------------------
117 
118  auto const tol = boost::test_tools::tolerance(0.001);
119 
120  /*
121  * The promise:
122  *
123  * --- 8< --------------------------------------------------------------------
124  * The return value is a triplet, which is most easily unpacked immediately:
125  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
126  * auto [ point, offsetA, offsetB ] = geo::LineClosestPointAndOffsets(
127  * geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0.0, 0.5, 0.0 },
128  * geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 0.866, 0.0, 0.0 }
129  * );
130  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131  * will set `point` to `geo::Point{ 2, 1, 1 }`, `offsetA` to `2` and `offsetB`
132  * to `2.309...`.
133  * To reassign the variables after they have been defined, instead:
134  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
135  * auto const xsectAndOfs = geo::LineClosestPointAndOffsets(
136  * geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 0.866, 0.0, 0.0 },
137  * geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0.0, 0.5, 0.0 }
138  * );
139  * point = xsectAndOfs.point;
140  * offsetA = xsectAndOfs.offset1;
141  * offsetB = xsectAndOfs.offset2;
142  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143  * (`point` to `geo::Point{ 2, 1, 0 }`, `offsetA` to `2.039...` and `offsetB`
144  * to `2`, because the intersection point is always on the first line).
145  * --- 8< --------------------------------------------------------------------
146  */
147 
148  auto [ point, offsetA, offsetB ] = geo::LineClosestPointAndOffsets(
149  geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0.0, 0.5, 0.0 },
150  geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 0.866, 0.0, 0.0 }
151  );
152 
153  // a way to check we did not mess with the assignment above too much
154  static_assert
155  (std::is_same_v<decltype(point), geo::Point_t>, "Unexpected point type");
156  static_assert
157  (std::is_same_v<decltype(offsetA), double>, "Unexpected first offset type");
158  static_assert
159  (std::is_same_v<decltype(offsetB), double>, "Unexpected second offset type");
160 
161  BOOST_TEST(point.X() == 2.0, tol);
162  BOOST_TEST(point.Y() == 1.0, tol);
163  BOOST_TEST(point.Z() == 1.0, tol);
164  BOOST_TEST(offsetA == 2.0, tol);
165  BOOST_TEST(offsetB == (2.0/0.866), tol);
166 
167  auto const xsectAndOfs = geo::LineClosestPointAndOffsets(
168  geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 0.866, 0.0, 0.0 },
169  geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0.0, 0.5, 0.0 }
170  );
171  point = xsectAndOfs.point;
172  offsetA = xsectAndOfs.offset1;
173  offsetB = xsectAndOfs.offset2;
174 
175  BOOST_TEST(point.X() == 2.0, tol);
176  BOOST_TEST(point.Y() == 1.0, tol);
177  BOOST_TEST(point.Z() == 0.0, tol);
178  BOOST_TEST(offsetA == 2.0/0.866, tol);
179  BOOST_TEST(offsetB == 2.0, tol);
180 
181  // actually we _can_ assign with `std::tie()`:
182  std::tie(point, offsetA, offsetB) = geo::LineClosestPointAndOffsets(
183  geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0.0, 0.5, 0.0 },
184  geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 0.866, 0.0, 0.0 }
185  );
186 
187  BOOST_TEST(point.X() == 2.0, tol);
188  BOOST_TEST(point.Y() == 1.0, tol);
189  BOOST_TEST(point.Z() == 1.0, tol);
190  BOOST_TEST(offsetA == 2.0, tol);
191  BOOST_TEST(offsetB == (2.0/0.866), tol);
192 
193 } // LineClosestPointAndOffsetsDocumentation_test()
194 
195 
196 // =============================================================================
198 
199  auto const tol = boost::test_tools::tolerance(0.001);
200 
202  geo::origin() - geo::Zaxis() - 3.0 * geo::Xaxis(), geo::Xaxis(),
203  geo::origin() + geo::Zaxis() + 2.0 * geo::Yaxis(), geo::Yaxis()
204  );
205 
206  BOOST_TEST(p.X() == 0.0, tol);
207  BOOST_TEST(p.Y() == 0.0, tol);
208  BOOST_TEST(p.Z() == -1.0, tol);
209 
210 } // LineClosestPointWithUnitVectorsSimple_test()
211 
212 
213 // -----------------------------------------------------------------------------
215 
216  auto const tol = boost::test_tools::tolerance(0.001);
217 
219  geo::origin(), geo::Xaxis(),
220  geo::origin() + geo::Yaxis(), (geo::Xaxis() + geo::Zaxis()) / std::sqrt(2.0)
221  );
222 
223  BOOST_TEST(p.X() == 0.0, tol);
224  BOOST_TEST(p.Y() == 0.0, tol);
225  BOOST_TEST(p.Z() == 0.0, tol);
226 
227 } // LineClosestPointWithUnitVectorsSimple45_test()
228 
229 
230 // -----------------------------------------------------------------------------
232 
233  auto const tol = boost::test_tools::tolerance(0.001);
234 
235  auto const [ p, ofsA, ofsB ] = geo::LineClosestPointAndOffsetsWithUnitVectors(
236  geo::origin() - 3.0 * geo::Xaxis(), geo::Xaxis(),
237  geo::origin() + geo::Zaxis() + 2.0 * geo::Yaxis(), geo::Yaxis()
238  );
239 
240  BOOST_TEST(p.X() == 0.0, tol);
241  BOOST_TEST(p.Y() == 0.0, tol);
242  BOOST_TEST(p.Z() == 0.0, tol);
243  BOOST_TEST(ofsA == +3.0, tol);
244  BOOST_TEST(ofsB == -2.0, tol);
245 
246 } // LineClosestPointWithUnitVectorsAndOffsets_test()
247 
248 
249 // -----------------------------------------------------------------------------
251 
252  auto const tol = boost::test_tools::tolerance(0.001);
253 
254  /*
255  * The promise:
256  *
257  * --- 8< --------------------------------------------------------------------
258  * The return value is a triplet, which is most easily unpacked immediately:
259  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
260  * auto [ point, offsetA, offsetB ] = geo::LineClosestPointAndOffsetsWithUnitVectors(
261  * geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0, 1, 0 },
262  * geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 1, 0, 0 }
263  * );
264  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
265  * will set `point` to `geo::Point{ 2, 1, 1 }`, `offsetA` to `1` and `offsetB`
266  * to `2`.
267  * To reassign the variables after they have been defined, instead:
268  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
269  * auto const xsectAndOfs = geo::LineClosestPointAndOffsetsWithUnitVectors(
270  * geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 1, 0, 0 },
271  * geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0, 1, 0 }
272  * );
273  * point = xsectAndOfs.point;
274  * offsetA = xsectAndOfs.offset1;
275  * offsetB = xsectAndOfs.offset2;
276  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277  * (`point` to `geo::Point{ 2, 1, 0 }`, `offsetA` to `2` and `offsetB` to `1`,
278  * because the intersection point is always on the first line).
279  * --- 8< --------------------------------------------------------------------
280  */
281 
282  auto [ point, offsetA, offsetB ] = geo::LineClosestPointAndOffsetsWithUnitVectors(
283  geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0, 1, 0 },
284  geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 1, 0, 0 }
285  );
286 
287  // a way to check we did not mess with the assignment above too much
288  static_assert
289  (std::is_same_v<decltype(point), geo::Point_t>, "Unexpected point type");
290  static_assert
291  (std::is_same_v<decltype(offsetA), double>, "Unexpected first offset type");
292  static_assert
293  (std::is_same_v<decltype(offsetB), double>, "Unexpected second offset type");
294 
295  BOOST_TEST(point.X() == 2.0, tol);
296  BOOST_TEST(point.Y() == 1.0, tol);
297  BOOST_TEST(point.Z() == 1.0, tol);
298  BOOST_TEST(offsetA == 1.0, tol);
299  BOOST_TEST(offsetB == 2.0, tol);
300 
301  auto const xsectAndOfs = geo::LineClosestPointAndOffsetsWithUnitVectors(
302  geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 1, 0, 0 },
303  geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0, 1, 0 }
304  );
305  point = xsectAndOfs.point;
306  offsetA = xsectAndOfs.offset1;
307  offsetB = xsectAndOfs.offset2;
308 
309  BOOST_TEST(point.X() == 2.0, tol);
310  BOOST_TEST(point.Y() == 1.0, tol);
311  BOOST_TEST(point.Z() == 0.0, tol);
312  BOOST_TEST(offsetA == 2.0, tol);
313  BOOST_TEST(offsetB == 1.0, tol);
314 
315  // actually we _can_ assign with `std::tie()`:
316  std::tie(point, offsetA, offsetB) = geo::LineClosestPointAndOffsetsWithUnitVectors(
317  geo::Point_t{ 2, 0, 1 }, geo::Vector_t{ 0, 1, 0 },
318  geo::Point_t{ 0, 1, 0 }, geo::Vector_t{ 1, 0, 0 }
319  );
320 
321  BOOST_TEST(point.X() == 2.0, tol);
322  BOOST_TEST(point.Y() == 1.0, tol);
323  BOOST_TEST(point.Z() == 1.0, tol);
324  BOOST_TEST(offsetA == 1.0, tol);
325  BOOST_TEST(offsetB == 2.0, tol);
326 
327 } // LineClosestPointAndOffsetsWithUnitVectorsDocumentation_test()
328 
329 
330 // =============================================================================
331 BOOST_AUTO_TEST_CASE(LineClosestPointTestCase) {
332 
339 
340 } // BOOST_AUTO_TEST_CASE(LineClosestPointTestCase)
341 
342 
343 // -----------------------------------------------------------------------------
344 BOOST_AUTO_TEST_CASE(LineClosestPointWithUnitVectorsTestCase) {
345 
350 
351 } // BOOST_AUTO_TEST_CASE(LineClosestPointWithUnitVectorsTestCase)
352 
353 
354 // -----------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(AllTests)
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space.
Definition: geo_vectors.h:164
void LineClosestPointWithScaledDirs_test()
void LineClosestPointWithUnitVectorsAndOffsets_test()
auto const tol
Definition: SurfXYZTest.cc:16
void LineClosestPointAndOffsetsDocumentation_test()
auto const tolerance
pdgs p
Definition: selectors.fcl:22
constexpr Vector Yaxis()
Returns a y axis vector of the specified type.
Definition: geo_vectors.h:219
void LineClosestPointSimple45_test()
IntersectionPointAndOffsets< Point > LineClosestPointAndOffsets(Point const &startA, Vector const &dirA, Point const &startB, Vector const &dirB)
Returns the point of a line that is closest to a second line.
void LineClosestPointWithNonHomogeneousDirs_test()
IntersectionPointAndOffsets< Point > LineClosestPointAndOffsetsWithUnitVectors(Point const &startA, UnitVector const &dirA, Point const &startB, UnitVector const &dirB)
Returns the point of a line that is closest to a second line.
Definitions of geometry vector data types.
Utility for intersection of two 3D lines.
void LineClosestPointAndOffsetsWithUnitVectorsDocumentation_test()
constexpr Vector Xaxis()
Returns a x axis vector of the specified type.
Definition: geo_vectors.h:215
void LineClosestPointWithUnitVectorsSimple45_test()
Point LineClosestPointWithUnitVectors(Point const &startA, UnitVector const &dirA, Point const &startB, UnitVector const &dirB)
Returns the point of a line that is closest to a second line.
constexpr Vector Zaxis()
Returns a z axis vector of the specified type.
Definition: geo_vectors.h:223
void LineClosestPointSimple_test()
void LineClosestPointWithUnitVectorsSimple_test()
Point LineClosestPoint(Point const &startA, Vector const &dirA, Point const &startB, Vector const &dirB)
Returns the point of a line that is closest to a second line.
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:184
void LineClosestPointAndOffsets_test()
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:227