File: TestUtils3D.cpp

package info (click to toggle)
vecgeom 1.2.8%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,016 kB
  • sloc: cpp: 88,803; ansic: 6,888; python: 1,035; sh: 582; sql: 538; makefile: 23
file content (217 lines) | stat: -rw-r--r-- 6,840 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
///
/// file:    TestUtils3D.cpp
/// purpose: Unit tests for the 3D geometry utilities
///

//-- ensure asserts are compiled in
#ifdef NDEBUG
#undef NDEBUG
#endif

#include "VecGeom/base/Utils3D.h"
#include "ApproxEqual.h"
#include "VecGeom/volumes/Box.h"
#include "test/benchmark/ArgParser.h"

#ifdef VECGEOM_ROOT
#include "VecGeomTest/Visualizer.h"
#include "TPolyMarker3D.h"
#include "TPolyLine3D.h"
#endif

using vecgeom::Precision;

bool ValidXing(vecgeom::Vector3D<Precision> const &point, vecgeom::Vector3D<Precision> const &dir,
               vecgeom::Vector3D<Precision> const &n1, Precision p1, vecgeom::Vector3D<Precision> const &n2,
               Precision p2)
{
  bool valid_dir   = ApproxEqual<Precision>(dir.Dot(n1), 0.) && ApproxEqual<Precision>(dir.Dot(n2), 0.);
  bool valid_point = ApproxEqual<Precision>(point.Dot(n1 - n2) + (p1 - p2), 0.);
  return valid_point & valid_dir;
}

#ifdef VECGEOM_ROOT
void DrawPolygon(vecgeom::Utils3D::Polygon const &poly, vecgeom::Visualizer &visualizer, size_t color)
{
  using namespace vecgeom;
  using Vec_t = Vector3D<Precision>;
  TPolyLine3D pl(poly.fN + 1);
  pl.SetLineColor(color);
  for (size_t i = 0; i < poly.fN; ++i)
    pl.SetNextPoint(poly.GetVertex(i).x(), poly.GetVertex(i).y(), poly.GetVertex(i).z());
  pl.SetNextPoint(poly.GetVertex(0).x(), poly.GetVertex(0).y(), poly.GetVertex(0).z());
  visualizer.AddLine(pl);

  // Compute center of polygon
  Vec_t center;
  for (size_t i = 0; i < poly.fN; ++i)
    center += poly.GetVertex(i);
  center *= 1. / poly.fN;
  TPolyLine3D plnorm(2);
  plnorm.SetLineColor(color);
  plnorm.SetNextPoint(center[0], center[1], center[2]);
  plnorm.SetNextPoint(center[0] + poly.fNorm[0], center[1] + poly.fNorm[1], center[2] + poly.fNorm[2]);
  visualizer.AddLine(plnorm);
}

void DrawPolyhedron(vecgeom::Utils3D::Polyhedron &polyh, vecgeom::Visualizer &visualizer, size_t color)
{
  using namespace vecgeom;

  for (size_t i = 0; i < polyh.GetNpolygons(); ++i)
    DrawPolygon(polyh.GetPolygon(i), visualizer, color);
}
#endif

int main(int argc, char *argv[])
{
  using namespace vecgeom;
  using namespace vecCore::math;
  using Vec_t = Vector3D<Precision>;

  using vecgeom::Utils3D::Line;
  using vecgeom::Utils3D::Plane;
  using vecgeom::Utils3D::Polygon;
  using vecgeom::Utils3D::Polyhedron;

  const Vec_t dirx(1., 0., 0.);
  const Vec_t diry(0., 1., 0.);
  const Vec_t dirz(0., 0., 1.);

#ifdef VECGEOM_ROOT
  OPTION_INT(vis, 0);
#endif

  ///* Plane transformations */
  Plane pl1(Vec_t(1., 0., 0.), -10.);
  Transformation3D transf1(10., 0., 0., 0., 0., 180.);
  pl1.Transform(transf1);
  assert(ApproxEqual<Precision>(pl1.fNorm[0], -1.) && ApproxEqual<Precision>(pl1.fDist, 0.));

  // Polygon intersection

  Utils3D::vector_t<Vec_t> pvec1 = {{10., 4., 6.}, {10., -4., 6.}, {10., -4., -6.}, {10., 4., -6.}};
  Polygon poly1(4, pvec1, Vec_t(1., 0., 0.));
  poly1.fInd = {0, 1, 2, 3};
  poly1.Init();
  Utils3D::vector_t<Vec_t> pvec2 = {{7.75736, -3.17423, 10.5854},
                                    {7.75736, -7.17423, 3.65722},
                                    {16.2426, 0.174235, -0.585422},
                                    {16.2426, 4.17423, 6.34278}};
  Polygon poly2(4, pvec2, true);
  poly2.fInd = {0, 1, 2, 3};
  poly2.Init();

  Line line1;
  assert(Utils3D::PolygonXing(poly1, poly2, &line1) == Utils3D::kOverlapping);

  ///* Test plane crossings */
  Vector3D<Precision> point, direction;
  Vector3D<Precision> n1, n2;
  Precision p1, p2;

  // identical planes
  n1.Set(0., 0., 1.);
  n2.Set(0., 0., 1.);
  p1 = -3.;
  p2 = -3.;
  assert(Utils3D::PlaneXing(Plane(n1, p1), Plane(n2, p2), point, direction) == Utils3D::kIdentical);

  // identical planes with opposite normals
  n1.Set(0., 0., 1.);
  n2.Set(0., 0., -1.);
  p1 = -3.;
  p2 = 3.;
  assert(Utils3D::PlaneXing(Plane(n1, p1), Plane(n2, p2), point, direction) == Utils3D::kIdentical);

  // opposite planes with opposite normals
  n1.Set(0., 0., 1.);
  n2.Set(0., 0., -1.);
  p1 = -3;
  p2 = -3;
  assert(Utils3D::PlaneXing(Plane(n1, p1), Plane(n2, p2), point, direction) == Utils3D::kParallel);

  // opposite planes with identical normal
  n1.Set(0., 0., 1.);
  n2.Set(0., 0., 1.);
  p1 = -3;
  p2 = 3;
  assert(Utils3D::PlaneXing(Plane(n1, p1), Plane(n2, p2), point, direction) == Utils3D::kParallel);

  // arbitrary parallel planes
  n1.Set(1., 2., 3.);
  n1.Normalize();
  n2 = -n1;
  p1 = 1;
  p2 = -2;
  assert(Utils3D::PlaneXing(Plane(n1, p1), Plane(n2, p2), point, direction) == Utils3D::kParallel);

  // +z face of a box with +x face of the same box
  n1.Set(0., 0., 1.);
  n2.Set(1., 0., 0.);
  p1 = -3;
  p2 = -2;
  assert(Utils3D::PlaneXing(Plane(n1, p1), Plane(n2, p2), point, direction) == Utils3D::kIntersecting);
  assert(ValidXing(point, direction, n1, p1, n2, p2));

  // same as above but 1 face has opposite normal
  n2 = -n2;
  p2 = -p2;
  assert(Utils3D::PlaneXing(Plane(n1, p1), Plane(n2, p2), point, direction) == Utils3D::kIntersecting);
  assert(ValidXing(point, direction, n1, p1, n2, p2));

  ///* Test box crossings */
  Vec_t box1(1., 2., 3.);
  Vec_t box2(2., 3., 4.);
  Polyhedron polyh1, polyh2;
  Utils3D::FillBoxPolyhedron(box1, polyh1);
  Utils3D::FillBoxPolyhedron(box2, polyh2);
  Transformation3D tr1, tr2, tr3;

  // Touching boxes
  tr1 = Transformation3D(0., 0., 0.);
  tr2 = Transformation3D(3., 5., 0.);
  assert(Utils3D::BoxCollision(box1, tr1, box2, tr2) == Utils3D::kTouching);

  // Disjoint boxes
  tr1 = Transformation3D(0., 0., 0.);
  tr2 = Transformation3D(2.5, 4.5, 10.2);
  assert(Utils3D::BoxCollision(box1, tr1, box2, tr2) == Utils3D::kDisjoint);

  // Overlapping boxes
  tr1 = Transformation3D(0., 0., 0.);
  tr2 = Transformation3D(2.5, 4.5, 6.5);
  assert(Utils3D::BoxCollision(box1, tr1, box2, tr2) == Utils3D::kOverlapping);

  tr1 = Transformation3D(-1, -0.5, 0.5);
  tr2 = Transformation3D(-3., -0.5, 0.5);
  tr3 = Transformation3D(1., 2., 3., 0., 45., 45.);
  polyh1.Transform(tr1);
  polyh2.Transform(tr3);
  assert(Utils3D::BoxCollision(box1, tr1, box2, tr3) == Utils3D::kOverlapping &&
         Utils3D::BoxCollision(box1, tr2, box2, tr3) == Utils3D::kDisjoint);

  std::cout << "TestUtils3D passed\n";

#ifdef VECGEOM_ROOT
  if (vis == 0) return 0;
  Visualizer visualizer;
  SimpleBox boxshape("box", 7, 7, 7);
  visualizer.AddVolume(boxshape);
  Utils3D::vector_t<Utils3D::Line> lines;
  DrawPolyhedron(polyh1, visualizer, kBlue);
  DrawPolyhedron(polyh2, visualizer, kGreen);
  if (PolyhedronXing(polyh1, polyh2, lines) == Utils3D::kOverlapping) {
    TPolyLine3D pl(2);
    pl.SetLineColor(kRed);
    for (auto line : lines) {
      pl.SetNextPoint(line.fPts[0].x(), line.fPts[0].y(), line.fPts[0].z());
      pl.SetNextPoint(line.fPts[1].x(), line.fPts[1].y(), line.fPts[1].z());
      visualizer.AddLine(pl);
    }
  }
  visualizer.Show();
#endif

  return 0;
}