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