EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GenericCuboidVolumeBoundsTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GenericCuboidVolumeBoundsTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 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 
18 
19 #include <chrono>
20 #include <fstream>
21 #include <iostream>
22 #include <memory>
23 
24 namespace Acts {
25 namespace Test {
26 
28 
29 BOOST_AUTO_TEST_SUITE(Volumes)
30 
31 BOOST_AUTO_TEST_CASE(construction_test) {
32  std::array<Vector3D, 8> vertices;
33  vertices = {{{0, 0, 0},
34  {2, 0, 0},
35  {2, 1, 0},
36  {0, 1, 0},
37  {0, 0, 1},
38  {2, 0, 1},
39  {2, 1, 1},
40  {0, 1, 1}}};
41  GenericCuboidVolumeBounds cubo(vertices);
42 
43  BOOST_CHECK(cubo.inside({0.5, 0.5, 0.5}));
44  BOOST_CHECK(cubo.inside({1.5, 0.5, 0.5}));
45  BOOST_CHECK(!cubo.inside({2.5, 0.5, 0.5}));
46  BOOST_CHECK(!cubo.inside({0.5, 1.5, 0.5}));
47  BOOST_CHECK(!cubo.inside({0.5, 0.5, 1.5}));
48  BOOST_CHECK(!cubo.inside({-0.5, 0.5, 0.5}));
49 
50  BOOST_CHECK(!cubo.inside({2.2, 1, 1}, 0.1));
51  BOOST_CHECK(cubo.inside({2.2, 1, 1}, 0.21));
52  BOOST_CHECK(cubo.inside({2.2, 1, 1}, 0.3));
53 }
54 
55 BOOST_AUTO_TEST_CASE(GenericCuboidBoundsOrientedSurfaces) {
56  std::array<Vector3D, 8> vertices;
57  vertices = {{{0, 0, 0},
58  {2, 0, 0},
59  {2, 1, 0},
60  {0, 1, 0},
61  {0, 0, 1},
62  {2, 0, 1},
63  {2, 1, 1},
64  {0, 1, 1}}};
65  GenericCuboidVolumeBounds cubo(vertices);
66 
67  auto is_in = [](const auto& tvtx, const auto& vertices_) {
68  for (const auto& vtx : vertices_) {
69  if (checkCloseAbs(vtx, tvtx, 1e-9)) {
70  return true;
71  }
72  }
73  return false;
74  };
75 
76  auto surfaces = cubo.orientedSurfaces(Transform3D::Identity());
77  for (const auto& srf : surfaces) {
78  auto pbounds = dynamic_cast<const PlanarBounds*>(&srf.first->bounds());
79  for (const auto& vtx : pbounds->vertices()) {
80  Vector3D glob = srf.first->localToGlobal(gctx, vtx, {});
81  // check if glob is in actual vertex list
82  BOOST_CHECK(is_in(glob, vertices));
83  }
84  }
85 
86  vertices = {{{0, 0, 0},
87  {2, 0, 0.4},
88  {2, 1, 0.4},
89  {0, 1, 0},
90  {0, 0, 1},
91  {1.8, 0, 1},
92  {1.8, 1, 1},
93  {0, 1, 1}}};
94  cubo = GenericCuboidVolumeBounds(vertices);
95 
96  surfaces = cubo.orientedSurfaces(Transform3D::Identity());
97  for (const auto& srf : surfaces) {
98  auto pbounds = dynamic_cast<const PlanarBounds*>(&srf.first->bounds());
99  for (const auto& vtx : pbounds->vertices()) {
100  Vector3D glob = srf.first->localToGlobal(gctx, vtx, {});
101  // check if glob is in actual vertex list
102  BOOST_CHECK(is_in(glob, vertices));
103  }
104  }
105 
106  Transform3D trf;
107  trf = Translation3D(Vector3D(0, 8, -5)) *
108  AngleAxis3D(M_PI / 3., Vector3D(1, -3, 9).normalized());
109 
110  surfaces = cubo.orientedSurfaces(trf);
111  for (const auto& srf : surfaces) {
112  auto pbounds = dynamic_cast<const PlanarBounds*>(&srf.first->bounds());
113  for (const auto& vtx : pbounds->vertices()) {
114  Vector3D glob = srf.first->localToGlobal(gctx, vtx, {});
115  // check if glob is in actual vertex list
116  BOOST_CHECK(is_in(trf.inverse() * glob, vertices));
117  }
118  }
119 }
120 
122  std::array<Vector3D, 8> vertices;
123  vertices = {{{0, 0, 0},
124  {2, 0, 0},
125  {2, 1, 0},
126  {0, 1, 0},
127  {0, 0, 1},
128  {2, 0, 1},
129  {2, 1, 1},
130  {0, 1, 1}}};
131  GenericCuboidVolumeBounds cubo(vertices);
133  cubo.draw(ply);
134 
135  std::ofstream os("cuboid.ply");
136  os << ply << std::flush;
137  os.close();
138 }
139 
140 BOOST_AUTO_TEST_CASE(bounding_box_creation) {
141  float tol = 1e-4;
142  std::array<Vector3D, 8> vertices;
143  vertices = {{{0, 0, 0},
144  {2, 0, 0.4},
145  {2, 1, 0.4},
146  {0, 1, 0},
147  {0, 0, 1},
148  {1.8, 0, 1},
149  {1.8, 1, 1},
150  {0, 1, 1}}};
151 
152  GenericCuboidVolumeBounds gcvb(vertices);
153  auto bb = gcvb.boundingBox();
154 
155  Transform3D rot;
156  rot = AngleAxis3D(M_PI / 2., Vector3D::UnitX());
157 
158  BOOST_CHECK_EQUAL(bb.entity(), nullptr);
159  BOOST_CHECK_EQUAL(bb.max(), Vector3D(2, 1, 1));
160  BOOST_CHECK_EQUAL(bb.min(), Vector3D(0., 0., 0.));
161 
162  bb = gcvb.boundingBox(&rot);
163 
164  BOOST_CHECK_EQUAL(bb.entity(), nullptr);
165  CHECK_CLOSE_ABS(bb.max(), Vector3D(2, 0, 1), tol);
166  BOOST_CHECK_EQUAL(bb.min(), Vector3D(0, -1, 0));
167 
168  rot = AngleAxis3D(M_PI / 2., Vector3D::UnitZ());
169  bb = gcvb.boundingBox(&rot);
170  BOOST_CHECK_EQUAL(bb.entity(), nullptr);
171  CHECK_CLOSE_ABS(bb.max(), Vector3D(0, 2, 1), tol);
172  CHECK_CLOSE_ABS(bb.min(), Vector3D(-1, 0., 0.), tol);
173 
174  rot = AngleAxis3D(0.542, Vector3D::UnitZ()) *
175  AngleAxis3D(M_PI / 5., Vector3D(1, 3, 6).normalized());
176 
177  bb = gcvb.boundingBox(&rot);
178  BOOST_CHECK_EQUAL(bb.entity(), nullptr);
179  CHECK_CLOSE_ABS(bb.max(), Vector3D(1.00976, 2.26918, 1.11988), tol);
180  CHECK_CLOSE_ABS(bb.min(), Vector3D(-0.871397, 0, -0.0867708), tol);
181 }
182 
183 BOOST_AUTO_TEST_CASE(GenericCuboidVolumeBoundarySurfaces) {
184  std::array<Vector3D, 8> vertices;
185  vertices = {{{0, 0, 0},
186  {4, 0, 0},
187  {4, 2, 0},
188  {0, 2, 0},
189  {0, 0, 2},
190  {4, 0, 2},
191  {4, 2, 2},
192  {0, 2, 2}}};
193 
194  GenericCuboidVolumeBounds cubo(vertices);
195 
196  auto gcvbOrientedSurfaces = cubo.orientedSurfaces(Transform3D::Identity());
197  BOOST_CHECK_EQUAL(gcvbOrientedSurfaces.size(), 6);
198 
199  for (auto& os : gcvbOrientedSurfaces) {
200  auto geoCtx = GeometryContext();
201  auto osCenter = os.first->center(geoCtx);
202  auto osNormal = os.first->normal(geoCtx);
203  double nDir = (double)os.second;
204  // Check if you step inside the volume with the oriented normal
205  auto insideGcvb = osCenter + nDir * osNormal;
206  auto outsideGcvb = osCenter - nDir * osNormal;
207  BOOST_CHECK(cubo.inside(insideGcvb));
208  BOOST_CHECK(!cubo.inside(outsideGcvb));
209  }
210 }
211 
212 BOOST_AUTO_TEST_SUITE_END()
213 } // namespace Test
214 } // namespace Acts