File: TestParallelepiped.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 (404 lines) | stat: -rw-r--r-- 14,800 bytes parent folder | download | duplicates (2)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// This file is part of VecGeom and is distributed under the
// conditions in the file LICENSE.txt in the top directory.
// For the full list of authors see CONTRIBUTORS.txt and `git log`.

/// Unit test for Paralleliped.
/// @file test/unit_tests/TestParallelepiped.cpp
/// @author Created by Mihaela Gheata
/// @author Revised by Evgueni Tcherniaev

// ensure asserts are compiled in
#undef NDEBUG
#include "VecGeom/base/FpeEnable.h"

#include "VecGeom/base/Vector3D.h"
#include "VecGeom/volumes/Parallelepiped.h"
#include "ApproxEqual.h"
#include <cmath>

template <class Parallelepiped_t>
bool TestParallelepiped()
{
  using namespace vecgeom::VECGEOM_IMPL_NAMESPACE;
  using Vec_t = vecgeom::Vector3D<vecgeom::Precision>;
  EnumInside inside;
  const Precision dx    = 20;
  const Precision dy    = 30;
  const Precision dz    = 40;
  const Precision alpha = kDegToRad * 30;
  const Precision theta = kDegToRad * 30;
  const Precision phi   = kDegToRad * 45;

  Parallelepiped_t para("Test Parallelepiped", dx, dy, dz, alpha, theta, phi);

  // Points on faces
  Vec_t pzero(0, 0, 0);
  Vec_t ponxside(dx, 0, 0), ponyside(0, dy, 0),
      ponzside(dz * para.GetTanThetaCosPhi(), dz * para.GetTanThetaSinPhi(), dz);
  Vec_t ponmxside(-dx, 0, 0), ponmyside(0, -dy, 0),
      ponmzside(-dz * para.GetTanThetaCosPhi(), -dz * para.GetTanThetaSinPhi(), -dz);

  Vec_t pbigx(100, 0, 0), pbigy(0, 100, 0), pbigz(0, 0, 100);
  Vec_t pbigmx(-100, 0, 0), pbigmy(0, -100, 0), pbigmz(0, 0, -100);

  Vec_t vx(1, 0, 0), vy(0, 1, 0), vz(0, 0, 1);
  Vec_t vmx(-1, 0, 0), vmy(0, -1, 0), vmz(0, 0, -1);
  Vec_t vxy(1 / std::sqrt(2.0), 1 / std::sqrt(2.0), 0);
  Vec_t vmxy(-1 / std::sqrt(2.0), 1 / std::sqrt(2.0), 0);
  Vec_t vmxmy(-1 / std::sqrt(2.0), -1 / std::sqrt(2.0), 0);
  Vec_t vxmy(1 / std::sqrt(2.0), -1 / std::sqrt(2.0), 0);

  Precision Dist;
  Vec_t normal, norm;
  bool valid;

  // check Cubic volume

  int Npoints = 10000000;
  std::cout << "=== Check Capacity()" << std::endl;
  Precision vol      = para.Capacity();
  Precision volCheck = para.GetUnplacedVolume()->EstimateCapacity(Npoints);
  std::cout << " vol = " << vol << "   mc_estimated = " << volCheck << std::endl;
  assert(std::abs(vol - volCheck) < 0.01 * vol);

  // Check Surface area

  std::cout << "=== Check SurfaceArea()" << std::endl;
  Precision surf      = para.SurfaceArea();
  Precision surfCheck = para.GetUnplacedVolume()->EstimateSurfaceArea(Npoints);
  std::cout << " surf = " << surf << "   mc_estimated = " << surfCheck << std::endl;
  assert(std::abs(surf - surfCheck) < 0.01 * surf);

  // Check Extent and cached BBox

  std::cout << "=== Check Extent() and cached BBox" << std::endl;
  Vec_t minExtent, maxExtent;
  Vec_t minBBox, maxBBox;
  Vec_t minCheck(kInfLength, kInfLength, kInfLength);
  Vec_t maxCheck(-kInfLength, -kInfLength, -kInfLength);
  para.Extent(minExtent, maxExtent);
  para.GetUnplacedVolume()->GetBBox(minBBox, maxBBox);
  for (int i = 0; i < Npoints; ++i) {
    Vec_t p = para.GetUnplacedVolume()->SamplePointOnSurface();
    minCheck.Set(std::min(p.x(), minCheck.x()), std::min(p.y(), minCheck.y()), std::min(p.z(), minCheck.z()));
    maxCheck.Set(std::max(p.x(), maxCheck.x()), std::max(p.y(), maxCheck.y()), std::max(p.z(), maxCheck.z()));
  }
  std::cout << " calculated: min = " << minExtent << " max = " << maxExtent << std::endl;
  std::cout << " estimated:  min = " << minCheck << " max = " << maxCheck << std::endl;

  assert(std::abs(minExtent.x() - minCheck.x()) < 0.001 * std::abs(minExtent.x()));
  assert(std::abs(minExtent.y() - minCheck.y()) < 0.001 * std::abs(minExtent.y()));
  assert(minExtent.z() == minCheck.z());
  assert(std::abs(maxExtent.x() - maxCheck.x()) < 0.001 * std::abs(maxExtent.x()));
  assert(std::abs(maxExtent.y() - maxCheck.y()) < 0.001 * std::abs(maxExtent.y()));
  assert(maxExtent.z() == maxCheck.z());
  assert(ApproxEqual<Precision>(minExtent, minBBox));
  assert(ApproxEqual<Precision>(maxExtent, maxBBox));

  // Check Inside

  std::cout << "=== Check Inside()" << std::endl;
  assert(para.Inside(pzero) == vecgeom::EInside::kInside);
  assert(para.Inside(pbigz) == vecgeom::EInside::kOutside);
  assert(para.Inside(ponxside) == vecgeom::EInside::kSurface);
  assert(para.Inside(ponyside) == vecgeom::EInside::kSurface);
  assert(para.Inside(ponzside) == vecgeom::EInside::kSurface);

  inside = para.Inside(ponzside + ponxside);
  assert(inside == vecgeom::EInside::kSurface);

  inside = para.Inside(ponzside + ponmxside);
  assert(inside == vecgeom::EInside::kSurface);

  inside = para.Inside(ponzside + ponyside);
  assert(inside == vecgeom::EInside::kSurface);

  inside = para.Inside(ponzside + ponmyside);
  assert(inside == vecgeom::EInside::kSurface);

  inside = para.Inside(ponmzside + ponxside);
  assert(inside == vecgeom::EInside::kSurface);

  inside = para.Inside(ponmzside + ponmxside);
  assert(inside == vecgeom::EInside::kSurface);

  inside = para.Inside(ponmzside + ponyside);
  assert(inside == vecgeom::EInside::kSurface);

  inside = para.Inside(ponmzside + ponmyside);
  assert(inside == vecgeom::EInside::kSurface);

  // Check Surface Normal

  std::cout << "=== Check Normal()" << std::endl;
  Vec_t pp[8], nn[3], ptest;
  pp[0].Set(-dx, -dy, -dz);
  pp[1].Set(dx, -dy, -dz);
  pp[2].Set(dx, dy, -dz);
  pp[3].Set(-dx, dy, -dz);
  pp[4].Set(-dx, -dy, dz);
  pp[5].Set(dx, -dy, dz);
  pp[6].Set(dx, dy, dz);
  pp[7].Set(-dx, dy, dz);
  for (int i = 0; i < 8; ++i) {
    pp[i].x() += para.GetTanAlpha() * pp[i].y() + para.GetTanThetaCosPhi() * pp[i].z();
    pp[i].y() += para.GetTanThetaSinPhi() * pp[i].z();
  }
  nn[0] = para.GetUnplacedVolume()->GetNormal(0);
  nn[1] = para.GetUnplacedVolume()->GetNormal(1);
  nn[2] = Vec_t(0, 0, 1);

  // check facets
  ptest = Vec_t(dx, 0, 0);
  valid = para.Normal(ptest, normal);
  assert(valid && normal == nn[0]);
  valid = para.Normal(1.1 * ptest, normal);
  assert(!valid && normal == nn[0]);
  valid = para.Normal(0.9 * ptest, normal);
  assert(!valid && normal == nn[0]);

  ptest = Vec_t(-dx, 0, 0);
  valid = para.Normal(ptest, normal);
  assert(valid && normal == -nn[0]);
  valid = para.Normal(1.1 * ptest, normal);
  assert(!valid && normal == -nn[0]);
  valid = para.Normal(0.9 * ptest, normal);
  assert(!valid && normal == -nn[0]);

  ptest = Vec_t(0, dy, 0);
  valid = para.Normal(ptest, normal);
  assert(valid && normal == nn[1]);
  valid = para.Normal(1.1 * ptest, normal);
  assert(!valid && normal == nn[1]);
  valid = para.Normal(0.9 * ptest, normal);
  assert(!valid && normal == nn[1]);

  ptest = Vec_t(0, -dy, 0);
  valid = para.Normal(ptest, normal);
  assert(valid && normal == -nn[1]);
  valid = para.Normal(1.1 * ptest, normal);
  assert(!valid && normal == -nn[1]);
  valid = para.Normal(0.9 * ptest, normal);
  assert(!valid && normal == -nn[1]);

  ptest = Vec_t(0, 0, dz);
  valid = para.Normal(ptest, normal);
  assert(valid && normal == nn[2]);
  valid = para.Normal(1.1 * ptest, normal);
  assert(!valid && normal == nn[2]);
  valid = para.Normal(0.9 * ptest, normal);
  assert(!valid && normal == nn[2]);

  ptest = Vec_t(0, 0, -dz);
  valid = para.Normal(ptest, normal);
  assert(valid && normal == -nn[2]);
  valid = para.Normal(1.1 * ptest, normal);
  assert(!valid && normal == -nn[2]);
  valid = para.Normal(0.9 * ptest, normal);
  assert(!valid && normal == -nn[2]);

  // check edges
  valid = para.Normal((pp[0] + pp[1]) / 2, normal);
  assert(valid && normal == (-nn[2] - nn[1]).Unit());
  valid = para.Normal((pp[1] + pp[2]) / 2, normal);
  assert(valid && normal == (-nn[2] + nn[0]).Unit());
  valid = para.Normal((pp[2] + pp[3]) / 2, normal);
  assert(valid && normal == (-nn[2] + nn[1]).Unit());
  valid = para.Normal((pp[3] + pp[0]) / 2, normal);
  assert(valid && normal == (-nn[2] - nn[0]).Unit());

  valid = para.Normal((pp[4] + pp[5]) / 2, normal);
  assert(valid && normal == (nn[2] - nn[1]).Unit());
  valid = para.Normal((pp[5] + pp[6]) / 2, normal);
  assert(valid && normal == (nn[2] + nn[0]).Unit());
  valid = para.Normal((pp[6] + pp[7]) / 2, normal);
  assert(valid && normal == (nn[2] + nn[1]).Unit());
  valid = para.Normal((pp[7] + pp[4]) / 2, normal);
  assert(valid && normal == (nn[2] - nn[0]).Unit());

  valid = para.Normal((pp[0] + pp[4]) / 2, normal);
  assert(valid && normal == (-nn[0] - nn[1]).Unit());
  valid = para.Normal((pp[1] + pp[5]) / 2, normal);
  assert(valid && normal == (nn[0] - nn[1]).Unit());
  valid = para.Normal((pp[2] + pp[6]) / 2, normal);
  assert(valid && normal == (nn[0] + nn[1]).Unit());
  valid = para.Normal((pp[3] + pp[7]) / 2, normal);
  assert(valid && normal == (-nn[0] + nn[1]).Unit());

  // check nodes
  valid = para.Normal(pp[0], normal);
  assert(valid && normal == (-nn[2] - nn[1] - nn[0]).Unit());
  valid = para.Normal(pp[1], normal);
  assert(valid && normal == (-nn[2] - nn[1] + nn[0]).Unit());
  valid = para.Normal(pp[2], normal);
  assert(valid && normal == (-nn[2] + nn[1] + nn[0]).Unit());
  valid = para.Normal(pp[3], normal);
  assert(valid && normal == (-nn[2] + nn[1] - nn[0]).Unit());

  valid = para.Normal(pp[4], normal);
  assert(valid && normal == (nn[2] - nn[1] - nn[0]).Unit());
  valid = para.Normal(pp[5], normal);
  assert(valid && normal == (nn[2] - nn[1] + nn[0]).Unit());
  valid = para.Normal(pp[6], normal);
  assert(valid && normal == (nn[2] + nn[1] + nn[0]).Unit());
  valid = para.Normal(pp[7], normal);
  assert(valid && normal == (nn[2] + nn[1] - nn[0]).Unit());

  // Check SafetyToOut

  std::cout << "=== Check SafetyToOut()" << std::endl;
  Dist = para.SafetyToOut(pzero);
  assert(Dist < dx);

  Dist = para.SafetyToOut(ponxside);
  assert(Dist == 0.);

  Dist = para.SafetyToOut(ponyside);
  assert(Dist == 0.);

  Dist = para.SafetyToOut(ponzside);
  assert(Dist == 0.);

  Dist = para.SafetyToOut(ponmxside);
  assert(Dist == 0.);

  Dist = para.SafetyToOut(ponmyside);
  assert(Dist == 0.);

  Dist = para.SafetyToOut(ponmzside);
  assert(Dist == 0.);

  // Check SafetyToIn

  std::cout << "=== Check SafetyToIn()" << std::endl;
  Dist = para.SafetyToIn(pzero);
  assert(Dist < dx);

  Dist = para.SafetyToIn(ponxside);
  assert(Dist == 0.);

  Dist = para.SafetyToIn(ponyside);
  assert(Dist == 0.);

  Dist = para.SafetyToIn(ponzside);
  assert(Dist == 0.);

  Dist = para.SafetyToIn(ponmxside);
  assert(Dist == 0.);

  Dist = para.SafetyToIn(ponmyside);
  assert(Dist == 0.);

  Dist = para.SafetyToIn(ponmzside);
  assert(Dist == 0.);

  assert(ApproxEqual<Precision>(para.SafetyToIn(pbigx), para.SafetyToIn(pbigmx)));
  assert(ApproxEqual<Precision>(para.SafetyToIn(pbigy), para.SafetyToIn(pbigmy)));
  assert(ApproxEqual<Precision>(para.SafetyToIn(pbigz), para.SafetyToIn(pbigmz)));

  // DistanceToOut(P,V)

  std::cout << "=== Check DistanceToOut()" << std::endl;
  Dist = para.DistanceToOut(pzero, vx);
  assert(ApproxEqual<Precision>(Dist, dx));
  Dist = para.DistanceToOut(pzero, vmx);
  assert(ApproxEqual<Precision>(Dist, dx));
  Dist = para.DistanceToOut(pzero, vy);
  assert(ApproxEqual<Precision>(Dist, dy));
  Dist = para.DistanceToOut(pzero, vmy);
  assert(ApproxEqual<Precision>(Dist, dy));
  Dist = para.DistanceToOut(pzero, vz);
  assert(ApproxEqual<Precision>(Dist, dz));
  Dist = para.DistanceToOut(pzero, vmz);
  assert(ApproxEqual<Precision>(Dist, dz));

  Dist = para.DistanceToOut(ponxside, vx);
  assert(ApproxEqual<Precision>(Dist, 0));
  Dist = para.DistanceToOut(ponmxside, vmx);
  assert(ApproxEqual<Precision>(Dist, 0));
  Dist = para.DistanceToOut(ponyside, vy);
  assert(ApproxEqual<Precision>(Dist, 0));
  Dist = para.DistanceToOut(ponmyside, vmy);
  assert(ApproxEqual<Precision>(Dist, 0));
  Dist = para.DistanceToOut(ponzside, vz);
  assert(ApproxEqual<Precision>(Dist, 0));
  Dist = para.DistanceToOut(ponmzside, vmz);
  assert(ApproxEqual<Precision>(Dist, 0));

  // DistanceToIn(P,V)

  std::cout << "=== Check DistanceToIn()" << std::endl;
  Dist = para.DistanceToIn(pbigx, vmx);
  assert(ApproxEqual<Precision>(Dist, 100 - dx));
  Dist = para.DistanceToIn(pbigmx, vx);
  assert(ApproxEqual<Precision>(Dist, 100 - dx));
  Dist = para.DistanceToIn(pbigy, vmy);
  assert(ApproxEqual<Precision>(Dist, 100 - dy));
  Dist = para.DistanceToIn(pbigmy, vy);
  assert(ApproxEqual<Precision>(Dist, 100 - dy));
  Dist = para.DistanceToIn(pbigz, vmz);
  assert(ApproxEqual<Precision>(Dist, 100 - dz));
  Dist = para.DistanceToIn(pbigmz, vz);
  assert(ApproxEqual<Precision>(Dist, 100 - dz));
  Dist = para.DistanceToIn(pbigx, vxy);
  assert(ApproxEqual<Precision>(Dist, kInfLength));
  Dist = para.DistanceToIn(pbigmx, vxy);
  assert(ApproxEqual<Precision>(Dist, kInfLength));

  // Check SamplePointOnSurface()

  std::cout << "=== Check SamplePointOnSurface()" << std::endl;
  Vec_t Vx(1., 0., 0.);
  Vec_t Vy(para.GetTanAlpha(), 1., 0.);
  Vec_t Vz(para.GetTanThetaCosPhi(), para.GetTanThetaSinPhi(), 1.);
  Vec_t Nx = Vy.Cross(Vz);
  Vec_t Ny = Vz.Cross(Vx);
  Vec_t Nz(0., 0., 1.);
  Precision sx = 4. * para.GetY() * para.GetZ() * Nx.Mag();
  Precision sy = 4. * para.GetZ() * para.GetX() * Ny.Mag();
  Precision sz = 4. * para.GetX() * para.GetY();
  Nx.Normalize();
  Ny.Normalize();
  Precision Dx = -Nx.x() * para.GetX();
  Precision Dy = -Ny.y() * para.GetY();
  int nxneg = 0, nxpos = 0, nyneg = 0, nypos = 0, nzneg = 0, nzpos = 0;
  int nfactor = 100, ntot = 2. * (sx + sy + sz) * nfactor;
  for (int i = 0; i < ntot; i++) {
    Vec_t p = para.GetUnplacedVolume()->SamplePointOnSurface();
    assert(para.Inside(p) == vecgeom::kSurface);
    if (std::abs(Nx.Dot(p) - Dx) < kHalfTolerance) {
      ++nxneg;
    } else if (std::abs(Nx.Dot(p) + Dx) < kHalfTolerance) {
      ++nxpos;
    } else if (std::abs(Ny.Dot(p) - Dy) < kHalfTolerance) {
      ++nyneg;
    } else if (std::abs(Ny.Dot(p) + Dy) < kHalfTolerance) {
      ++nypos;
    } else if (p.z() == -para.GetZ()) {
      ++nzneg;
    } else {
      ++nzpos;
    }
  }
  std::cout << "facet surface -/+x, -/+y, -/+z: "
            << "\t" << sx << ", \t" << sx << ", \t" << sy << ", \t" << sy << ", \t" << sz << ", \t" << sz << std::endl;
  std::cout << "n. of samples -/+x, -/+y, -/+z: "
            << "\t" << nxneg << ", \t" << nxpos << ", \t" << nyneg << ", \t" << nypos << ", \t" << nzneg << ", \t"
            << nzpos << std::endl;
  assert(std::abs(nxneg - sx * nfactor) < 0.01 * sx * nfactor);
  assert(std::abs(nxpos - sx * nfactor) < 0.01 * sx * nfactor);
  assert(std::abs(nyneg - sy * nfactor) < 0.01 * sy * nfactor);
  assert(std::abs(nypos - sy * nfactor) < 0.01 * sy * nfactor);
  assert(std::abs(nzneg - sz * nfactor) < 0.01 * sz * nfactor);
  assert(std::abs(nzpos - sz * nfactor) < 0.01 * sz * nfactor);

  return true;
}

int main(int argc, char *argv[])
{

  TestParallelepiped<vecgeom::SimpleParallelepiped>();
  std::cout << "\nVecGeom Parallelepiped passed\n" << std::endl;
  return 0;
}