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 
13 
14 #include <algorithm>
15 
16 namespace Acts {
17 namespace Test {
18 
20 BOOST_AUTO_TEST_CASE(IntersectionTest) {
21  // a few valid intersections
22  // all positively sortered
23  Intersection3D fIp(Vector3D(0., 1., 0.), 1.,
24  Intersection3D::Status::reachable);
25  Intersection3D sIp(Vector3D(0., 2., 0.), 2.,
26  Intersection3D::Status::reachable);
27  Intersection3D tIp(Vector3D(0., 3., 0.), 3.,
28  Intersection3D::Status::reachable);
29  BOOST_CHECK(bool(fIp));
30  BOOST_CHECK(bool(sIp));
31  BOOST_CHECK(bool(tIp));
32 
33  // a non-valid intersection
34  Intersection3D nIp(Vector3D(3., 3., 0.), 3.,
35  Intersection3D::Status::unreachable);
36  BOOST_CHECK(!bool(nIp));
37 
38  std::vector<Intersection3D> fstpIntersections = {fIp, sIp, tIp};
39  std::vector<Intersection3D> tsfpIntersections = {tIp, sIp, fIp};
40 
41  // let's sort the tsf intersection, it should give fst
42  std::sort(tsfpIntersections.begin(), tsfpIntersections.end());
43  BOOST_CHECK_EQUAL(fstpIntersections[0].pathLength,
44  tsfpIntersections[0].pathLength);
45  BOOST_CHECK_EQUAL(fstpIntersections[1].pathLength,
46  tsfpIntersections[1].pathLength);
47  BOOST_CHECK_EQUAL(fstpIntersections[2].pathLength,
48  tsfpIntersections[2].pathLength);
49 
50  // let's sort them with greater
51  std::sort(tsfpIntersections.begin(), tsfpIntersections.end(),
52  std::greater<>());
53  BOOST_CHECK_EQUAL(fstpIntersections[0].pathLength,
54  tsfpIntersections[2].pathLength);
55  BOOST_CHECK_EQUAL(fstpIntersections[1].pathLength,
56  tsfpIntersections[1].pathLength);
57  BOOST_CHECK_EQUAL(fstpIntersections[2].pathLength,
58  tsfpIntersections[0].pathLength);
59 
60  // now let's create one with a non-valid intersection, it should be shuffled
61  // last
62  std::vector<Intersection3D> ntfspIntersections = {nIp, tIp, fIp, sIp};
63  std::vector<Intersection3D> tfnsnpIntersections = {tIp, fIp, nIp, sIp, nIp};
64 
65  // shuffle the intersections
66  std::sort(ntfspIntersections.begin(), ntfspIntersections.end());
67  BOOST_CHECK_EQUAL(fstpIntersections[0].pathLength,
68  ntfspIntersections[0].pathLength);
69  BOOST_CHECK_EQUAL(fstpIntersections[1].pathLength,
70  ntfspIntersections[1].pathLength);
71  BOOST_CHECK_EQUAL(fstpIntersections[2].pathLength,
72  ntfspIntersections[2].pathLength);
73  BOOST_CHECK_EQUAL(bool(ntfspIntersections[3]), false);
74 
75  std::sort(tfnsnpIntersections.begin(), tfnsnpIntersections.end());
76  BOOST_CHECK_EQUAL(fstpIntersections[0].pathLength,
77  tfnsnpIntersections[0].pathLength);
78  BOOST_CHECK_EQUAL(fstpIntersections[1].pathLength,
79  tfnsnpIntersections[1].pathLength);
80  BOOST_CHECK_EQUAL(fstpIntersections[2].pathLength,
81  tfnsnpIntersections[2].pathLength);
82  BOOST_CHECK_EQUAL(bool(tfnsnpIntersections[3]), false);
83  BOOST_CHECK_EQUAL(bool(tfnsnpIntersections[4]), false);
84 
86  Intersection3D fIn(Vector3D(0., -1., 0.), -1.,
87  Intersection3D::Status::reachable);
88  Intersection3D sIn(Vector3D(0., -2., 0.), -2.,
89  Intersection3D::Status::reachable);
90  Intersection3D tIn(Vector3D(0., -3., 0.), -3.,
91  Intersection3D::Status::reachable);
92 
93  std::vector<Intersection3D> tsfnIntersections = {tIn, sIn, fIn};
94  std::vector<Intersection3D> fstnIntersections = {fIn, sIn, tIn};
95 
96  // this time around, sort the f-s-t-n to match the t-s-f-n
97  std::sort(fstnIntersections.begin(), fstnIntersections.end());
98  BOOST_CHECK_EQUAL(fstnIntersections[0].pathLength,
99  tsfnIntersections[0].pathLength);
100  BOOST_CHECK_EQUAL(fstnIntersections[1].pathLength,
101  tsfnIntersections[1].pathLength);
102  BOOST_CHECK_EQUAL(fstnIntersections[2].pathLength,
103  tsfnIntersections[2].pathLength);
104 
105  // let's sort them with greater
106  std::sort(fstnIntersections.begin(), fstnIntersections.end(),
107  std::greater<>());
108  BOOST_CHECK_EQUAL(fstnIntersections[0].pathLength,
109  tsfnIntersections[2].pathLength);
110  BOOST_CHECK_EQUAL(fstnIntersections[1].pathLength,
111  tsfnIntersections[1].pathLength);
112  BOOST_CHECK_EQUAL(fstnIntersections[2].pathLength,
113  tsfnIntersections[0].pathLength);
114 
115  // shuffle negative and positive solutions
116  std::vector<Intersection3D> pnsolutions = {tIp, sIn, sIp, fIn, tIn, fIp};
117  std::sort(pnsolutions.begin(), pnsolutions.end());
118 
119  BOOST_CHECK_EQUAL(pnsolutions[0].pathLength, -3.);
120  BOOST_CHECK_EQUAL(pnsolutions[1].pathLength, -2.);
121  BOOST_CHECK_EQUAL(pnsolutions[2].pathLength, -1.);
122  BOOST_CHECK_EQUAL(pnsolutions[3].pathLength, 1.);
123  BOOST_CHECK_EQUAL(pnsolutions[4].pathLength, 2.);
124  BOOST_CHECK_EQUAL(pnsolutions[5].pathLength, 3.);
125 
126  // sort intersections with zero path length
127  Intersection3D zI(Vector3D(0., 0., 0.), 0.,
128  Intersection3D::Status::onSurface);
129  std::vector<Intersection3D> tszfpIntersections = {tIp, sIp, zI, fIp};
130 
131  std::sort(tszfpIntersections.begin(), tszfpIntersections.end());
132  BOOST_CHECK_EQUAL(tszfpIntersections[0].pathLength, 0.);
133  BOOST_CHECK_EQUAL(tszfpIntersections[1].pathLength, 1.);
134  BOOST_CHECK_EQUAL(tszfpIntersections[2].pathLength, 2.);
135  BOOST_CHECK_EQUAL(tszfpIntersections[3].pathLength, 3.);
136 
137  std::vector<Intersection3D> tfsznIntersections = {tIn, fIn, sIn, zI};
138  std::vector<Intersection3D> ztfsnIntersections = {zI, tIn, fIn, sIn};
139 
140  std::sort(tfsznIntersections.begin(), tfsznIntersections.end(),
141  std::greater<>());
142  BOOST_CHECK_EQUAL(tfsznIntersections[0].pathLength, 0.);
143  BOOST_CHECK_EQUAL(tfsznIntersections[1].pathLength, -1.);
144  BOOST_CHECK_EQUAL(tfsznIntersections[2].pathLength, -2.);
145  BOOST_CHECK_EQUAL(tfsznIntersections[3].pathLength, -3.);
146 
147  std::sort(ztfsnIntersections.begin(), ztfsnIntersections.end(),
148  std::greater<>());
149  BOOST_CHECK_EQUAL(ztfsnIntersections[0].pathLength, 0.);
150  BOOST_CHECK_EQUAL(ztfsnIntersections[1].pathLength, -1.);
151  BOOST_CHECK_EQUAL(ztfsnIntersections[2].pathLength, -2.);
152  BOOST_CHECK_EQUAL(ztfsnIntersections[3].pathLength, -3.);
153 }
154 
156 BOOST_AUTO_TEST_CASE(ObjectIntersectionTest) {
157  auto psf6 = Surface::makeShared<PlaneSurface>(Vector3D(6., 0., 0.),
158  Vector3D(1., 0., 0.));
159  auto psf7 = Surface::makeShared<PlaneSurface>(Vector3D(7., 0., 0.),
160  Vector3D(1., 0., 0.));
161  auto psf8 = Surface::makeShared<PlaneSurface>(Vector3D(8., 0., 0.),
162  Vector3D(1., 0., 0.));
163  auto psf9 = Surface::makeShared<PlaneSurface>(Vector3D(9., 0., 0.),
164  Vector3D(1., 0., 0.));
165  auto psf10 = Surface::makeShared<PlaneSurface>(Vector3D(10., 0., 0.),
166  Vector3D(1., 0., 0.));
167 
168  using PlaneIntersection = ObjectIntersection<PlaneSurface>;
169 
170  PlaneIntersection int6(Intersection3D(Vector3D(6., 0., 0.), 6.,
171  Intersection3D::Status::reachable),
172  psf6.get());
173  PlaneIntersection int7(Intersection3D(Vector3D(7., 0., 0.), 7.,
174  Intersection3D::Status::reachable),
175  psf7.get());
176  PlaneIntersection int8(Intersection3D(Vector3D(8., 0., 0.), 8.,
177  Intersection3D::Status::reachable),
178  psf8.get());
179  PlaneIntersection int9a(Intersection3D(Vector3D(9., 0., 0.), 9.,
180  Intersection3D::Status::reachable),
181  psf9.get());
182  PlaneIntersection int9b(
183  Intersection3D(Vector3D(9., 1., 0.), std::sqrt(9. * 9. + 1.),
184  Intersection3D::Status::reachable),
185  psf9.get());
186  PlaneIntersection int10(Intersection3D(Vector3D(10., 0., 0.), 10.,
187  Intersection3D::Status::reachable),
188  psf10.get());
189 
190  std::vector<PlaneIntersection> firstSet = {int6, int7, int9b, int10};
191  std::vector<PlaneIntersection> secondSet = {int8, int9a, int9b, int10};
192  // result of the standard union set
193  std::vector<PlaneIntersection> unionSetStd = {};
194  // result of the custominzed union set
195  std::vector<PlaneIntersection> unionSetCst = {};
196 
197  // This should give 6 different intersections
198  std::set_union(firstSet.begin(), firstSet.end(), secondSet.begin(),
199  secondSet.end(), std::back_inserter(unionSetStd));
200  BOOST_CHECK_EQUAL(unionSetStd.size(), 6u);
201 
202  // This should give 5 different inteseciton attempts (for each surface 1)
203  SameSurfaceIntersection onSameSurface;
204  std::set_union(firstSet.begin(), firstSet.end(), secondSet.begin(),
205  secondSet.end(), std::back_inserter(unionSetCst),
206  onSameSurface);
207  BOOST_CHECK_EQUAL(unionSetCst.size(), 5u);
208 }
209 
210 } // namespace Test
211 } // namespace Acts