ECCE @ 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 
11 #include <chrono>
12 #include <fstream>
13 #include <iostream>
14 #include <memory>
15 
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(decomposeToSurfaces) {
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.decomposeToSurfaces(nullptr);
77  for (const auto& srf : surfaces) {
78  auto pbounds = dynamic_cast<const PlanarBounds*>(&srf->bounds());
79  for (const auto& vtx : pbounds->vertices()) {
80  Vector3D glob;
81  srf->localToGlobal(gctx, vtx, {}, glob);
82  // check if glob is in actual vertex list
83  BOOST_CHECK(is_in(glob, vertices));
84  }
85  }
86 
87  vertices = {{{0, 0, 0},
88  {2, 0, 0.4},
89  {2, 1, 0.4},
90  {0, 1, 0},
91  {0, 0, 1},
92  {1.8, 0, 1},
93  {1.8, 1, 1},
94  {0, 1, 1}}};
95  cubo = GenericCuboidVolumeBounds(vertices);
96 
97  surfaces = cubo.decomposeToSurfaces(nullptr);
98  for (const auto& srf : surfaces) {
99  auto pbounds = dynamic_cast<const PlanarBounds*>(&srf->bounds());
100  for (const auto& vtx : pbounds->vertices()) {
101  Vector3D glob;
102  srf->localToGlobal(gctx, vtx, {}, glob);
103  // check if glob is in actual vertex list
104  BOOST_CHECK(is_in(glob, vertices));
105  }
106  }
107 
108  Transform3D trf;
109  trf = Translation3D(Vector3D(0, 8, -5)) *
110  AngleAxis3D(M_PI / 3., Vector3D(1, -3, 9).normalized());
111 
112  surfaces = cubo.decomposeToSurfaces(&trf);
113  for (const auto& srf : surfaces) {
114  auto pbounds = dynamic_cast<const PlanarBounds*>(&srf->bounds());
115  for (const auto& vtx : pbounds->vertices()) {
116  Vector3D glob;
117  srf->localToGlobal(gctx, vtx, {}, glob);
118  // check if glob is in actual vertex list
119  BOOST_CHECK(is_in(trf.inverse() * glob, vertices));
120  }
121  }
122 }
123 
125  std::array<Vector3D, 8> vertices;
126  vertices = {{{0, 0, 0},
127  {2, 0, 0},
128  {2, 1, 0},
129  {0, 1, 0},
130  {0, 0, 1},
131  {2, 0, 1},
132  {2, 1, 1},
133  {0, 1, 1}}};
134  GenericCuboidVolumeBounds cubo(vertices);
136  cubo.draw(ply);
137 
138  std::ofstream os("cuboid.ply");
139  os << ply << std::flush;
140  os.close();
141 }
142 
143 BOOST_AUTO_TEST_CASE(bounding_box_creation) {
144  float tol = 1e-4;
145  std::array<Vector3D, 8> vertices;
146  vertices = {{{0, 0, 0},
147  {2, 0, 0.4},
148  {2, 1, 0.4},
149  {0, 1, 0},
150  {0, 0, 1},
151  {1.8, 0, 1},
152  {1.8, 1, 1},
153  {0, 1, 1}}};
154 
155  GenericCuboidVolumeBounds gcvb(vertices);
156  auto bb = gcvb.boundingBox();
157 
158  Transform3D rot;
159  rot = AngleAxis3D(M_PI / 2., Vector3D::UnitX());
160 
161  BOOST_CHECK_EQUAL(bb.entity(), nullptr);
162  BOOST_CHECK_EQUAL(bb.max(), Vector3D(2, 1, 1));
163  BOOST_CHECK_EQUAL(bb.min(), Vector3D(0., 0., 0.));
164 
165  bb = gcvb.boundingBox(&rot);
166 
167  BOOST_CHECK_EQUAL(bb.entity(), nullptr);
168  CHECK_CLOSE_ABS(bb.max(), Vector3D(2, 0, 1), tol);
169  BOOST_CHECK_EQUAL(bb.min(), Vector3D(0, -1, 0));
170 
171  rot = AngleAxis3D(M_PI / 2., Vector3D::UnitZ());
172  bb = gcvb.boundingBox(&rot);
173  BOOST_CHECK_EQUAL(bb.entity(), nullptr);
174  CHECK_CLOSE_ABS(bb.max(), Vector3D(0, 2, 1), tol);
175  CHECK_CLOSE_ABS(bb.min(), Vector3D(-1, 0., 0.), tol);
176 
177  rot = AngleAxis3D(0.542, Vector3D::UnitZ()) *
178  AngleAxis3D(M_PI / 5., Vector3D(1, 3, 6).normalized());
179 
180  bb = gcvb.boundingBox(&rot);
181  BOOST_CHECK_EQUAL(bb.entity(), nullptr);
182  CHECK_CLOSE_ABS(bb.max(), Vector3D(1.00976, 2.26918, 1.11988), tol);
183  CHECK_CLOSE_ABS(bb.min(), Vector3D(-0.871397, 0, -0.0867708), tol);
184 }
185 
186 BOOST_AUTO_TEST_SUITE_END()
187 } // namespace Test
188 } // namespace Acts