Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrapezoidBoundsTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrapezoidBoundsTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-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/data/test_case.hpp>
10 #include <boost/test/tools/output_test_stream.hpp>
11 #include <boost/test/unit_test.hpp>
12 
18 
19 #include <algorithm>
20 #include <array>
21 #include <ostream>
22 #include <random>
23 #include <stdexcept>
24 #include <utility>
25 #include <vector>
26 
27 namespace bdata = boost::unit_test::data;
28 
29 namespace Acts {
30 
31 namespace Test {
32 BOOST_AUTO_TEST_SUITE(Surfaces)
33 
34 
35 BOOST_AUTO_TEST_CASE(TrapezoidBoundsConstruction) {
36  double minHalfX(1.), maxHalfX(6.), halfY(2.);
37  //
38  // default construction deleted
39  // TrapezoidBounds defaultConstructedTrapezoidBounds;
40  //
42  BOOST_CHECK_EQUAL(TrapezoidBounds(minHalfX, maxHalfX, halfY).type(),
45  TrapezoidBounds original(minHalfX, maxHalfX, halfY);
46  TrapezoidBounds copied(original);
47  BOOST_CHECK_EQUAL(copied, original);
48 }
49 
51 BOOST_AUTO_TEST_CASE(TrapezoidBoundsRecreated) {
52  double minHalfX(1.), maxHalfX(6.), halfY(2.);
54  TrapezoidBounds original(minHalfX, maxHalfX, halfY);
55  // const bool symmetric(false);
56  auto valvector = original.values();
57  std::array<double, TrapezoidBounds::eSize> values{};
58  std::copy_n(valvector.begin(), TrapezoidBounds::eSize, values.begin());
59  TrapezoidBounds recreated(values);
60  BOOST_CHECK_EQUAL(original, recreated);
61 }
62 
63 // Exception tests
64 BOOST_AUTO_TEST_CASE(TrapezoidBoundsException) {
65  double minHalfX(1.), maxHalfX(6.), halfY(2.);
66 
67  // Negative x at min y
68  BOOST_CHECK_THROW(TrapezoidBounds(-minHalfX, maxHalfX, halfY),
69  std::logic_error);
70 
71  // Negative x at max y
72  BOOST_CHECK_THROW(TrapezoidBounds(minHalfX, -maxHalfX, halfY),
73  std::logic_error);
74 
75  // Negative x at miny and max y
76  BOOST_CHECK_THROW(TrapezoidBounds(-minHalfX, -maxHalfX, halfY),
77  std::logic_error);
78 
79  // Negative y
80  BOOST_CHECK_THROW(TrapezoidBounds(minHalfX, maxHalfX, -halfY),
81  std::logic_error);
82 }
83 
85 BOOST_AUTO_TEST_CASE(TrapezoidBoundsProperties) {
86  double minHalfX(1.), maxHalfX(6.), halfY(2.);
87  //
88  TrapezoidBounds trapezoidBoundsObject(minHalfX, maxHalfX, halfY);
89  //
91  BOOST_CHECK_EQUAL(trapezoidBoundsObject.type(), SurfaceBounds::eTrapezoid);
92  //
94  BOOST_CHECK_EQUAL(
95  trapezoidBoundsObject.get(TrapezoidBounds::eHalfLengthXnegY), minHalfX);
96  //
98  BOOST_CHECK_EQUAL(
99  trapezoidBoundsObject.get(TrapezoidBounds::eHalfLengthXposY), maxHalfX);
100  //
102  BOOST_CHECK_EQUAL(trapezoidBoundsObject.get(TrapezoidBounds::eHalfLengthY),
103  halfY);
104  //
106  Vector2 outside(30., 0.);
107  Vector2 inRectangle(2., 0.5);
108 
110  std::vector<Vector2> expectedVertices{
111  {-1., -2.}, {1., -2.}, {6., 2.}, {-6., 2.}};
112  const auto& actualVertices = trapezoidBoundsObject.vertices();
113  BOOST_CHECK_EQUAL_COLLECTIONS(actualVertices.cbegin(), actualVertices.cend(),
114  expectedVertices.cbegin(),
115  expectedVertices.cend());
120  //
122  BOOST_CHECK_EQUAL(trapezoidBoundsObject.boundingBox(),
123  RectangleBounds(6., 2.));
124  //
125 
126  //
128  boost::test_tools::output_test_stream dumpOuput;
129  trapezoidBoundsObject.toStream(dumpOuput);
130  BOOST_CHECK(
131  dumpOuput.is_equal("Acts::TrapezoidBounds: (halfXnegY, halfXposY, "
132  "halfY) = (1.0000000, 6.0000000, 2.0000000)"));
133  //
135  BOOST_CHECK(trapezoidBoundsObject.inside(inRectangle, BoundaryCheck(true)));
136  BOOST_CHECK(!trapezoidBoundsObject.inside(outside, BoundaryCheck(true)));
137 
138  const auto vertices = trapezoidBoundsObject.vertices();
139  BoundaryCheck bc{true};
140 
141  std::vector<Vector2> testPoints = {
142  // inside
143  {0, 1},
144  {0, -1},
145  {2, 0.5},
146  {2, 0},
147  {-2, 0},
148  {-2, 0.5},
149  {3, 1},
150  {-3, 1},
151  {4, 1},
152  {-4, 1},
153  {-6, 2},
154 
155  // outside
156  {0, 2.5},
157  {0, -2.5},
158  {2, 2.5},
159  {-2, 2.5},
160  {2, -2.5},
161  {2, -2.5},
162  {4, -1},
163  {-4, -1},
164  {-7, 0},
165  {7, 0},
166  {5, -3},
167  {5, 3},
168  {-5, -3},
169  {-5, 3},
170  {6, 2},
171 
172  };
173 
174  for (const auto& p : testPoints) {
175  BOOST_TEST_CONTEXT("p=" << p.transpose()) {
176  BOOST_CHECK_EQUAL(bc.isInside(p, vertices),
177  trapezoidBoundsObject.inside(p, bc));
178  }
179  }
180 }
181 
183  TrapezoidInsideCheck,
184  bdata::random((bdata::seed = 1,
185  bdata::distribution = std::uniform_real_distribution<>(-7,
186  7))) ^
187  bdata::random(
188  (bdata::seed = 2,
189  bdata::distribution = std::uniform_real_distribution<>(-3, 3))) ^
190  bdata::xrange(1000) * bdata::make({0.0, 0.1, 0.2, 0.3}),
191  x, y, index, tol) {
192  (void)index;
193  double minHalfX(1.), maxHalfX(6.), halfY(2.);
194  static const TrapezoidBounds trapezoidBoundsObject(minHalfX, maxHalfX, halfY);
195  static const auto vertices = trapezoidBoundsObject.vertices();
196 
197  BoundaryCheck bc{true};
198 
199  if (tol != 0.0) {
200  bc = BoundaryCheck{true, true, tol, tol};
201  }
202 
203  BOOST_CHECK_EQUAL(bc.isInside({x, y}, vertices),
204  trapezoidBoundsObject.inside({x, y}, bc));
205 }
206 
208 BOOST_AUTO_TEST_CASE(TrapezoidBoundsAssignment) {
209  double minHalfX(1.), maxHalfX(6.), halfY(2.);
210  TrapezoidBounds trapezoidBoundsObject(minHalfX, maxHalfX, halfY);
211  // operator == not implemented in this class
212  //
214  TrapezoidBounds assignedTrapezoidBoundsObject(10., 20., 14.2);
215  assignedTrapezoidBoundsObject = trapezoidBoundsObject;
216  BOOST_CHECK_EQUAL(assignedTrapezoidBoundsObject, trapezoidBoundsObject);
217 }
218 
219 BOOST_AUTO_TEST_SUITE_END()
220 
221 } // namespace Test
222 
223 } // namespace Acts