ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IntersectionTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file IntersectionTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
11 #include <algorithm>
12 
15 
16 namespace Acts {
17 namespace Test {
18 
20 BOOST_AUTO_TEST_CASE(IntersectionTest) {
21  // a few valid intersections
22  // all positively sortered
23  Intersection fIp(Vector3D(0., 1., 0.), 1., Intersection::Status::reachable);
24  Intersection sIp(Vector3D(0., 2., 0.), 2., Intersection::Status::reachable);
25  Intersection tIp(Vector3D(0., 3., 0.), 3., Intersection::Status::reachable);
26  BOOST_CHECK(bool(fIp));
27  BOOST_CHECK(bool(sIp));
28  BOOST_CHECK(bool(tIp));
29 
30  // a non-valid intersection
31  Intersection nIp(Vector3D(3., 3., 0.), 3., Intersection::Status::unreachable);
32  BOOST_CHECK(!bool(nIp));
33 
34  std::vector<Intersection> fstpIntersections = {fIp, sIp, tIp};
35  std::vector<Intersection> tsfpIntersections = {tIp, sIp, fIp};
36 
37  // let's sort the tsf intersection, it should give fst
38  std::sort(tsfpIntersections.begin(), tsfpIntersections.end());
39  BOOST_CHECK_EQUAL(fstpIntersections[0].pathLength,
40  tsfpIntersections[0].pathLength);
41  BOOST_CHECK_EQUAL(fstpIntersections[1].pathLength,
42  tsfpIntersections[1].pathLength);
43  BOOST_CHECK_EQUAL(fstpIntersections[2].pathLength,
44  tsfpIntersections[2].pathLength);
45 
46  // let's sort them with greater
47  std::sort(tsfpIntersections.begin(), tsfpIntersections.end(),
48  std::greater<>());
49  BOOST_CHECK_EQUAL(fstpIntersections[0].pathLength,
50  tsfpIntersections[2].pathLength);
51  BOOST_CHECK_EQUAL(fstpIntersections[1].pathLength,
52  tsfpIntersections[1].pathLength);
53  BOOST_CHECK_EQUAL(fstpIntersections[2].pathLength,
54  tsfpIntersections[0].pathLength);
55 
56  // now let's create one with a non-valid intersection, it should be shuffled
57  // last
58  std::vector<Intersection> ntfspIntersections = {nIp, tIp, fIp, sIp};
59  std::vector<Intersection> tfnsnpIntersections = {tIp, fIp, nIp, sIp, nIp};
60 
61  // shuffle the intersections
62  std::sort(ntfspIntersections.begin(), ntfspIntersections.end());
63  BOOST_CHECK_EQUAL(fstpIntersections[0].pathLength,
64  ntfspIntersections[0].pathLength);
65  BOOST_CHECK_EQUAL(fstpIntersections[1].pathLength,
66  ntfspIntersections[1].pathLength);
67  BOOST_CHECK_EQUAL(fstpIntersections[2].pathLength,
68  ntfspIntersections[2].pathLength);
69  BOOST_CHECK_EQUAL(bool(ntfspIntersections[3]), false);
70 
71  std::sort(tfnsnpIntersections.begin(), tfnsnpIntersections.end());
72  BOOST_CHECK_EQUAL(fstpIntersections[0].pathLength,
73  tfnsnpIntersections[0].pathLength);
74  BOOST_CHECK_EQUAL(fstpIntersections[1].pathLength,
75  tfnsnpIntersections[1].pathLength);
76  BOOST_CHECK_EQUAL(fstpIntersections[2].pathLength,
77  tfnsnpIntersections[2].pathLength);
78  BOOST_CHECK_EQUAL(bool(tfnsnpIntersections[3]), false);
79  BOOST_CHECK_EQUAL(bool(tfnsnpIntersections[4]), false);
80 
82  Intersection fIn(Vector3D(0., -1., 0.), -1., Intersection::Status::reachable);
83  Intersection sIn(Vector3D(0., -2., 0.), -2., Intersection::Status::reachable);
84  Intersection tIn(Vector3D(0., -3., 0.), -3., Intersection::Status::reachable);
85 
86  std::vector<Intersection> tsfnIntersections = {tIn, sIn, fIn};
87  std::vector<Intersection> fstnIntersections = {fIn, sIn, tIn};
88 
89  // this time around, sort the f-s-t-n to match the t-s-f-n
90  std::sort(fstnIntersections.begin(), fstnIntersections.end());
91  BOOST_CHECK_EQUAL(fstnIntersections[0].pathLength,
92  tsfnIntersections[0].pathLength);
93  BOOST_CHECK_EQUAL(fstnIntersections[1].pathLength,
94  tsfnIntersections[1].pathLength);
95  BOOST_CHECK_EQUAL(fstnIntersections[2].pathLength,
96  tsfnIntersections[2].pathLength);
97 
98  // let's sort them with greater
99  std::sort(fstnIntersections.begin(), fstnIntersections.end(),
100  std::greater<>());
101  BOOST_CHECK_EQUAL(fstnIntersections[0].pathLength,
102  tsfnIntersections[2].pathLength);
103  BOOST_CHECK_EQUAL(fstnIntersections[1].pathLength,
104  tsfnIntersections[1].pathLength);
105  BOOST_CHECK_EQUAL(fstnIntersections[2].pathLength,
106  tsfnIntersections[0].pathLength);
107 
108  // shuffle negative and positive solutions
109  std::vector<Intersection> pnsolutions = {tIp, sIn, sIp, fIn, tIn, fIp};
110  std::sort(pnsolutions.begin(), pnsolutions.end());
111 
112  BOOST_CHECK_EQUAL(pnsolutions[0].pathLength, -3.);
113  BOOST_CHECK_EQUAL(pnsolutions[1].pathLength, -2.);
114  BOOST_CHECK_EQUAL(pnsolutions[2].pathLength, -1.);
115  BOOST_CHECK_EQUAL(pnsolutions[3].pathLength, 1.);
116  BOOST_CHECK_EQUAL(pnsolutions[4].pathLength, 2.);
117  BOOST_CHECK_EQUAL(pnsolutions[5].pathLength, 3.);
118 
119  // sort intersections with zero path length
120  Intersection zI(Vector3D(0., 0., 0.), 0., Intersection::Status::onSurface);
121  std::vector<Intersection> tszfpIntersections = {tIp, sIp, zI, fIp};
122 
123  std::sort(tszfpIntersections.begin(), tszfpIntersections.end());
124  BOOST_CHECK_EQUAL(tszfpIntersections[0].pathLength, 0.);
125  BOOST_CHECK_EQUAL(tszfpIntersections[1].pathLength, 1.);
126  BOOST_CHECK_EQUAL(tszfpIntersections[2].pathLength, 2.);
127  BOOST_CHECK_EQUAL(tszfpIntersections[3].pathLength, 3.);
128 
129  std::vector<Intersection> tfsznIntersections = {tIn, fIn, sIn, zI};
130  std::vector<Intersection> ztfsnIntersections = {zI, tIn, fIn, sIn};
131 
132  std::sort(tfsznIntersections.begin(), tfsznIntersections.end(),
133  std::greater<>());
134  BOOST_CHECK_EQUAL(tfsznIntersections[0].pathLength, 0.);
135  BOOST_CHECK_EQUAL(tfsznIntersections[1].pathLength, -1.);
136  BOOST_CHECK_EQUAL(tfsznIntersections[2].pathLength, -2.);
137  BOOST_CHECK_EQUAL(tfsznIntersections[3].pathLength, -3.);
138 
139  std::sort(ztfsnIntersections.begin(), ztfsnIntersections.end(),
140  std::greater<>());
141  BOOST_CHECK_EQUAL(ztfsnIntersections[0].pathLength, 0.);
142  BOOST_CHECK_EQUAL(ztfsnIntersections[1].pathLength, -1.);
143  BOOST_CHECK_EQUAL(ztfsnIntersections[2].pathLength, -2.);
144  BOOST_CHECK_EQUAL(ztfsnIntersections[3].pathLength, -3.);
145 }
146 
148 BOOST_AUTO_TEST_CASE(ObjectIntersectionTest) {
149  auto psf6 = Surface::makeShared<PlaneSurface>(Vector3D(6., 0., 0.),
150  Vector3D(1., 0., 0.));
151  auto psf7 = Surface::makeShared<PlaneSurface>(Vector3D(7., 0., 0.),
152  Vector3D(1., 0., 0.));
153  auto psf8 = Surface::makeShared<PlaneSurface>(Vector3D(8., 0., 0.),
154  Vector3D(1., 0., 0.));
155  auto psf9 = Surface::makeShared<PlaneSurface>(Vector3D(9., 0., 0.),
156  Vector3D(1., 0., 0.));
157  auto psf10 = Surface::makeShared<PlaneSurface>(Vector3D(10., 0., 0.),
158  Vector3D(1., 0., 0.));
159 
160  using PlaneIntersection = ObjectIntersection<PlaneSurface>;
161 
162  PlaneIntersection int6(
163  Intersection(Vector3D(6., 0., 0.), 6., Intersection::Status::reachable),
164  psf6.get());
165  PlaneIntersection int7(
166  Intersection(Vector3D(7., 0., 0.), 7., Intersection::Status::reachable),
167  psf7.get());
168  PlaneIntersection int8(
169  Intersection(Vector3D(8., 0., 0.), 8., Intersection::Status::reachable),
170  psf8.get());
171  PlaneIntersection int9a(
172  Intersection(Vector3D(9., 0., 0.), 9., Intersection::Status::reachable),
173  psf9.get());
174  PlaneIntersection int9b(
175  Intersection(Vector3D(9., 1., 0.), std::sqrt(9. * 9. + 1.),
176  Intersection::Status::reachable),
177  psf9.get());
178  PlaneIntersection int10(
179  Intersection(Vector3D(10., 0., 0.), 10., Intersection::Status::reachable),
180  psf10.get());
181 
182  std::vector<PlaneIntersection> firstSet = {int6, int7, int9b, int10};
183  std::vector<PlaneIntersection> secondSet = {int8, int9a, int9b, int10};
184  // result of the standard union set
185  std::vector<PlaneIntersection> unionSetStd = {};
186  // result of the custominzed union set
187  std::vector<PlaneIntersection> unionSetCst = {};
188 
189  // This should give 6 different intersections
190  std::set_union(firstSet.begin(), firstSet.end(), secondSet.begin(),
191  secondSet.end(), std::back_inserter(unionSetStd));
192  BOOST_CHECK_EQUAL(unionSetStd.size(), 6u);
193 
194  // This should give 5 different inteseciton attempts (for each surface 1)
195  SameSurfaceIntersection onSameSurface;
196  std::set_union(firstSet.begin(), firstSet.end(), secondSet.begin(),
197  secondSet.end(), std::back_inserter(unionSetCst),
198  onSameSurface);
199  BOOST_CHECK_EQUAL(unionSetCst.size(), 5u);
200 }
201 
202 } // namespace Test
203 } // namespace Acts