File: TestEllipsoid.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 (719 lines) | stat: -rw-r--r-- 28,139 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// 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 the Ellipsoid shape
/// @file test/unit_teststest/TestEllipsoid.cpp
/// @author Evgueni Tcherniaev

// ensure asserts are compiled in
#undef NDEBUG

#include <iomanip>
#include "VecGeom/base/Global.h"
#include "VecGeom/base/Vector3D.h"
#include "VecGeom/base/Stopwatch.h"
#include "VecGeom/volumes/EllipticUtilities.h"
#include "VecGeom/volumes/Ellipsoid.h"
#include "ApproxEqual.h"

bool testvecgeom = false;

using namespace vecgeom;

///////////////////////////////////////////////////////////////////////////////
//
// Estimate normal to the surface at given z and phi
//
Vector3D<Precision> EstimateNormal(Precision a, Precision b, Precision c, Precision z, Precision phi)
{
  Precision delta = 0.001;
  Precision z1    = z - delta;
  Precision z2    = z + delta;
  Precision phi1  = phi - delta;
  Precision phi2  = phi + delta;
  Precision rho1  = std::sqrt((1. + z1 / c) * (1. - z1 / c));
  Precision rho2  = std::sqrt((1. + z2 / c) * (1. - z2 / c));

  Vector3D<Precision> p1(rho1 * std::cos(phi1) * a, rho1 * std::sin(phi1) * b, z1);
  Vector3D<Precision> p2(rho1 * std::cos(phi2) * a, rho1 * std::sin(phi2) * b, z1);
  Vector3D<Precision> p3(rho2 * std::cos(phi1) * a, rho2 * std::sin(phi1) * b, z2);
  Vector3D<Precision> p4(rho2 * std::cos(phi2) * a, rho2 * std::sin(phi2) * b, z2);

  return ((p4 - p1).Cross(p3 - p2)).Unit();
}

///////////////////////////////////////////////////////////////////////////////
//
// Unit test for Ellipsoid
//
template <class Ellipsoid_t, class Vec_t = Vector3D<Precision>>
bool TestEllipsoid()
{
  ///////////////////////////////////////////////////////////////////////////////
  //
  // Check surface area, volume and other basic methods
  //
  std::cout << "=== Check Set()/Get()" << std::endl;

  Precision a, b, c, zbottom, ztop;

  Ellipsoid_t solid("Test_Ellipsoid", a = 3., b = 4., c = 5., zbottom = -4.5, ztop = 3.5);
  assert(solid.GetDx() == a);
  assert(solid.GetDy() == b);
  assert(solid.GetDz() == c);
  assert(solid.GetZBottomCut() == zbottom);
  assert(solid.GetZTopCut() == ztop);

  solid.SetZCuts(-c - 1., c + 1);
  assert(solid.GetZBottomCut() == -c);
  assert(solid.GetZTopCut() == c);

  solid.SetZCuts(-4., 2);
  assert(solid.GetZBottomCut() == -4.);
  assert(solid.GetZTopCut() == 2.);

  solid.SetZCuts(0., 0.);
  assert(solid.GetZBottomCut() == -c);
  assert(solid.GetZTopCut() == c);

  solid.SetZCuts(zbottom, ztop);
  assert(solid.GetZBottomCut() == zbottom);
  assert(solid.GetZTopCut() == ztop);

  solid.SetSemiAxes(2., 3., 4.);
  assert(solid.GetDx() == 2.);
  assert(solid.GetDy() == 3.);
  assert(solid.GetDz() == 4.);
  assert(solid.GetZBottomCut() == -4.);
  assert(solid.GetZTopCut() == 3.5);

  solid.SetSemiAxes(a, b, c);
  solid.SetZCuts(zbottom, ztop);
  assert(solid.GetDx() == a);
  assert(solid.GetDy() == b);
  assert(solid.GetDz() == c);
  assert(solid.GetZBottomCut() == zbottom);
  assert(solid.GetZTopCut() == ztop);

  std::cout << "=== Check Print()" << std::endl;
  solid.Print();
  std::cout << "Ellipsoid (" << a << ", " << b << ", " << c << ", " << zbottom << ", " << ztop << ")" << std::endl;

  // Check Surface area
  int Npoints = 1000000;
  std::cout << "=== Check SurfaceArea()" << std::endl;

  // check sphere
  solid.SetSemiAxes(5., 5., 5.);
  solid.SetZCuts(0., 0.);
  Precision area      = solid.SurfaceArea();
  Precision areaMath  = 4. * vecgeom::kPi * 25.;
  Precision areaCheck = solid.GetUnplacedVolume()->EstimateSurfaceArea(Npoints);
  std::cout << " sphere(5) = " << area << "   exact = " << areaMath << "   mc_estimated = " << areaCheck << " ("
            << Npoints / 1000000. << " million points)" << std::endl;
  assert(std::abs(area - areaMath) < 0.01 * area);

  // check prolate spheroid
  solid.SetSemiAxes(3., 3., 5.);
  solid.SetZCuts(0., 0.);
  area        = solid.SurfaceArea();
  Precision e = 4. / 5.;
  areaMath    = vecgeom::kTwoPi * 3. * (3. + 5. * std::asin(e) / e);
  areaCheck   = solid.GetUnplacedVolume()->EstimateSurfaceArea(Npoints);
  std::cout << " spheroid(3,3,5) = " << area << "   exact = " << areaMath << "   mc_estimated = " << areaCheck << " ("
            << Npoints / 1000000. << " million points)" << std::endl;
  assert(std::abs(area - areaMath) < 0.01 * area);

  // check oblate spheroid
  solid.SetSemiAxes(5., 5., 3.);
  solid.SetZCuts(0., 0.);
  area      = solid.SurfaceArea();
  areaMath  = vecgeom::kTwoPi * 25. + vecgeom::kPi * 9. * std::log((1. + e) / (1 - e)) / e;
  areaCheck = solid.GetUnplacedVolume()->EstimateSurfaceArea(Npoints);
  std::cout << " spheroid(5,5,3) = " << area << "   exact = " << areaMath << "   mc_estimated = " << areaCheck << " ("
            << Npoints / 1000000. << " million points)" << std::endl;
  assert(std::abs(area - areaMath) < 0.01 * area);

  // check ellipsoid under test
  solid.SetSemiAxes(a, b, c);
  solid.SetZCuts(zbottom, ztop);
  area      = solid.SurfaceArea();
  areaCheck = solid.GetUnplacedVolume()->EstimateSurfaceArea(Npoints);
  std::cout << " ellipsoid(3,4,5, -4.5,3.5) = " << area << "   mc_estimated = " << areaCheck << " ("
            << Npoints / 1000000. << " million points)" << std::endl;
  assert(std::abs(area - areaCheck) < 0.01 * area);

  // Check Cubic volume
  std::cout << "=== Check Capacity()" << std::endl;
  solid.SetSemiAxes(a, b, c);
  solid.SetZCuts(zbottom, ztop);
  Precision vol      = solid.Capacity();
  Precision volCheck = solid.GetUnplacedVolume()->EstimateCapacity(Npoints);
  std::cout << " volume = " << vol << "   mc_estimated = " << volCheck << " (" << Npoints / 1000000.
            << " million points)" << std::endl;
  assert(std::abs(vol - volCheck) < 0.01 * vol);

  // Check Extent
  std::cout << "=== Check Extent()" << std::endl;
  Vec_t minCheck(kInfLength, kInfLength, kInfLength);
  Vec_t maxCheck(-kInfLength, -kInfLength, -kInfLength);
  for (int i = 0; i < Npoints; ++i) {
    Vec_t p = solid.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()));
  }
  Vec_t minExtent, maxExtent;
  solid.Extent(minExtent, maxExtent);
  std::cout << " calculated:    min = " << minExtent << " max = " << maxExtent << std::endl;
  std::cout << " mc_estimated:  min = " << minCheck << " max = " << maxCheck << " (" << Npoints / 1000000.
            << " million points)" << 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());

  ///////////////////////////////////////////////////////////////////////////////
  //
  // Check Inside()
  //
  std::cout << "=== Check Inside()" << std::endl;
  solid.SetSemiAxes(a, b, c);
  solid.SetZCuts(zbottom, ztop);
  int NZ         = 30;
  int NPHI       = 30;
  Precision DZ   = 2. * c / NZ;
  Precision DPHI = kTwoPi / NPHI;
  for (int iz = 0; iz < NZ; ++iz) {
    Precision z = -c + iz * DZ;
    for (int iphi = 0; iphi < NPHI; ++iphi) {
      Precision eps = 0.5 * kHalfTolerance * (2. * RNG::Instance().uniform() - 1.);
      Precision phi = iphi * DPHI;
      Precision rho = std::sqrt((1. + z / c) * (1. - z / c));
      Precision px  = rho * std::cos(phi) * a + eps;
      Precision py  = rho * std::sin(phi) * b + eps;
      Precision pz  = z + eps;
      if (z < zbottom) pz = zbottom;
      if (z > ztop) pz = ztop;
      Vec_t p(px, py, pz);
      assert(solid.Inside(p) == vecgeom::kSurface);
      assert(solid.Inside(p * 0.999) == vecgeom::kInside);
      assert(solid.Inside(p * 1.001) == vecgeom::kOutside);
    }
  }
  assert(solid.Inside(Vec_t(0., 0., zbottom)) == vecgeom::kSurface);
  assert(solid.Inside(Vec_t(0., 0., zbottom - kTolerance)) == vecgeom::kOutside);
  assert(solid.Inside(Vec_t(0., 0., zbottom + kTolerance)) == vecgeom::kInside);
  assert(solid.Inside(Vec_t(0., 0., ztop)) == vecgeom::kSurface);
  assert(solid.Inside(Vec_t(0., 0., ztop - kTolerance)) == vecgeom::kInside);
  assert(solid.Inside(Vec_t(0., 0., ztop + kTolerance)) == vecgeom::kOutside);
  assert(solid.Inside(Vec_t(0., 0., 0.)) == vecgeom::kInside);
  assert(solid.Inside(Vec_t(0., 0., -c)) == vecgeom::kOutside);
  assert(solid.Inside(Vec_t(0., 0., +c)) == vecgeom::kOutside);
  assert(solid.Inside(Vec_t(-a, 0., 0.)) == vecgeom::kSurface);
  assert(solid.Inside(Vec_t(-a - kTolerance, 0., 0.)) == vecgeom::kOutside);
  assert(solid.Inside(Vec_t(-a + kTolerance, 0., 0.)) == vecgeom::kInside);
  assert(solid.Inside(Vec_t(0., b, 0.)) == vecgeom::kSurface);
  assert(solid.Inside(Vec_t(0., b - kTolerance, 0.)) == vecgeom::kInside);
  assert(solid.Inside(Vec_t(0., b + kTolerance, 0.)) == vecgeom::kOutside);

  ///////////////////////////////////////////////////////////////////////////////
  //
  // Check Normal()
  //
  std::cout << "=== Check Normal()" << std::endl;
  solid.SetSemiAxes(a, b, c);
  solid.SetZCuts(zbottom, ztop);
  Vec_t normal(0.);
  bool valid;

  // Check normals on lateral surface
  NZ   = 30;
  NPHI = 30;
  DZ   = (ztop - zbottom) / NZ;
  DPHI = kTwoPi / NPHI;
  for (int iz = 1; iz < NZ - 1; ++iz) {
    Precision z = zbottom + iz * DZ;
    for (int iphi = 0; iphi < NPHI; ++iphi) {
      Precision eps = 0.5 * kHalfTolerance * (2. * RNG::Instance().uniform() - 1.);
      Precision phi = iphi * DPHI;
      Precision rho = std::sqrt((1. + z / c) * (1. - z / c));
      Precision px  = rho * std::cos(phi) * a + eps;
      Precision py  = rho * std::sin(phi) * b + eps;
      Precision pz  = z + eps;
      valid         = solid.Normal(Vec_t(px, py, pz), normal);
      assert(valid);
      assert(ApproxEqual(normal, EstimateNormal(a, b, c, pz, phi)));
    }
  }

  // Check normals at zbottom edge
  for (int iphi = 0; iphi < NPHI; ++iphi) {
    Precision eps = 0.5 * kHalfTolerance * (2. * RNG::Instance().uniform() - 1.);
    Precision phi = iphi * DPHI;
    Precision rho = std::sqrt((1. + zbottom / c) * (1. - zbottom / c));
    Precision px  = rho * std::cos(phi) * a + eps;
    Precision py  = rho * std::sin(phi) * b + eps;
    Precision pz  = zbottom + eps;
    valid         = solid.Normal(Vec_t(px, py, pz), normal);
    assert(valid);
    assert(ApproxEqual(normal, (EstimateNormal(a, b, c, pz, phi) + Vec_t(0., 0., -1.)).Unit()));
  }

  // Check normals at ztop edge
  for (int iphi = 0; iphi < NPHI; ++iphi) {
    Precision eps = 0.5 * kHalfTolerance * (2. * RNG::Instance().uniform() - 1.);
    Precision phi = iphi * DPHI;
    Precision rho = std::sqrt((1. + ztop / c) * (1. - ztop / c));
    Precision px  = rho * std::cos(phi) * a + eps;
    Precision py  = rho * std::sin(phi) * b + eps;
    Precision pz  = ztop + eps;
    valid         = solid.Normal(Vec_t(px, py, pz), normal);
    assert(valid);
    assert(ApproxEqual(normal, (EstimateNormal(a, b, c, pz, phi) + Vec_t(0., 0., 1.)).Unit()));
  }

  // Check normals on zbottom cut
  assert(solid.Normal(Vec_t(0., 0., zbottom), normal));
  assert(normal == Vec_t(0., 0., -1.));
  assert(solid.Normal(Vec_t(0.5, 0., zbottom), normal));
  assert(normal == Vec_t(0., 0., -1.));
  assert(solid.Normal(Vec_t(0, -0.5, zbottom), normal));
  assert(normal == Vec_t(0., 0., -1.));
  assert(solid.Normal(Vec_t(-0.5, 0.5, zbottom), normal));
  assert(normal == Vec_t(0., 0., -1.));
  assert(solid.Normal(Vec_t(-0.6, -0.6, zbottom), normal));
  assert(normal == Vec_t(0., 0., -1.));

  // Check normals on ztop cut
  assert(solid.Normal(Vec_t(0., 0., ztop), normal));
  assert(normal == Vec_t(0., 0., 1.));
  assert(solid.Normal(Vec_t(0.5, 0., ztop), normal));
  assert(normal == Vec_t(0., 0., 1.));
  assert(solid.Normal(Vec_t(0, -0.5, ztop), normal));
  assert(normal == Vec_t(0., 0., 1.));
  assert(solid.Normal(Vec_t(-0.5, 0.5, ztop), normal));
  assert(normal == Vec_t(0., 0., 1.));
  assert(solid.Normal(Vec_t(-0.6, -0.6, ztop), normal));
  assert(normal == Vec_t(0., 0., 1.));

  // Check points not on surface
  assert(!solid.Normal(Vec_t(0., 0., 0.), normal));
  assert(normal.Mag() == 1.);

  assert(!solid.Normal(Vec_t(0., 0., zbottom + 1.), normal));
  assert(normal == Vec_t(0., 0., -1.));
  assert(!solid.Normal(Vec_t(0.1, 0.1, zbottom + 1.), normal));
  assert(normal == Vec_t(0., 0., -1.));

  assert(!solid.Normal(Vec_t(0., 0., ztop - 1.), normal));
  assert(normal == Vec_t(0., 0., 1.));
  assert(!solid.Normal(Vec_t(-0.2, -0.3, ztop - 1.), normal));
  assert(normal == Vec_t(0., 0., 1.));

  for (int iz = 1; iz < NZ - 1; ++iz) {
    Precision z = zbottom + iz * DZ;
    for (int iphi = 0; iphi < NPHI; ++iphi) {
      Precision eps = 0.5 * kHalfTolerance * (2. * RNG::Instance().uniform() - 1.);
      Precision phi = iphi * DPHI;
      Precision rho = std::sqrt((1. + z / c) * (1. - z / c));
      Precision px  = rho * std::cos(phi) * a + eps;
      Precision py  = rho * std::sin(phi) * b + eps;
      Precision pz  = z + eps;
      valid         = solid.Normal(Vec_t(px, py, pz) * 1.2, normal);
      assert(!valid);
      assert(ApproxEqual(normal, EstimateNormal(a, b, c, pz, phi)));
      valid = solid.Normal(Vec_t(px, py, pz) * 0.8, normal);
      assert(!valid);
      assert(ApproxEqual(normal, EstimateNormal(a, b, c, pz, phi)));
    }
  }

  ///////////////////////////////////////////////////////////////////////////////
  //
  // Check SafetyToIn()
  //
  std::cout << "=== Check SafetyToIn()" << std::endl;
  solid.SetSemiAxes(a = 3, b = 4, c = 5);
  solid.SetZCuts(zbottom = -4.5, ztop = 3.5);

  // Check consistence with the convention
  NZ   = 30;
  NPHI = 30;
  DZ   = 2. * c / NZ;
  DPHI = kTwoPi / NPHI;
  for (int iz = 0; iz < NZ; ++iz) {
    Precision z = -c + iz * DZ;
    for (int iphi = 0; iphi < NPHI; ++iphi) {
      Precision eps = 0.5 * kHalfTolerance * (2. * RNG::Instance().uniform() - 1.);
      Precision phi = iphi * DPHI;
      Precision rho = std::sqrt((1. + z / c) * (1. - z / c));
      Precision px  = rho * std::cos(phi) * a + eps;
      Precision py  = rho * std::sin(phi) * b + eps;
      Precision pz  = z + eps;
      if (z < zbottom) pz = zbottom;
      if (z > ztop) pz = ztop;
      Vec_t p(px, py, pz);
      assert(solid.Inside(p) == vecgeom::kSurface);
      assert(solid.SafetyToIn(p) == 0.);
      p = Vec_t(px, py, pz) * 2.;
      assert(solid.Inside(p) == vecgeom::kOutside);
      assert(solid.SafetyToIn(p) > 0.);
      p = Vec_t(px, py, pz) * 0.5;
      assert(solid.Inside(p) == vecgeom::kInside);
      assert(solid.SafetyToIn(p) < 0.);
    }
  }

  // Check particular points to verify that the algorithm works as expected
  assert(solid.SafetyToIn(Vec_t(+10, 0, 0)) == 10. - a);
  assert(solid.SafetyToIn(Vec_t(-10, 0, 0)) == 10. - a);
  assert(solid.SafetyToIn(Vec_t(0, +10, 0)) == 10. - b);
  assert(solid.SafetyToIn(Vec_t(0, -10, 0)) == 10. - b);
  assert(solid.SafetyToIn(Vec_t(0, 0, +10)) == 10. - ztop);
  assert(solid.SafetyToIn(Vec_t(0, 0, -10)) == 10. + zbottom);
  assert(solid.SafetyToIn(Vec_t(a, b, ztop)) > 0.);
  assert(ApproxEqual<Precision>(solid.SafetyToIn(Vec_t(a, b, c)), (std::sqrt(3.) - 1.) * a));

  ///////////////////////////////////////////////////////////////////////////////
  //
  // Check SafetyToOut()
  //
  std::cout << "=== Check SafetyToOut()" << std::endl;
  solid.SetSemiAxes(a = 3, b = 4, c = 5);
  solid.SetZCuts(zbottom = -4.5, ztop = 3.5);

  // Check consistence with the convention
  NZ   = 30;
  NPHI = 30;
  DZ   = 2. * c / NZ;
  DPHI = kTwoPi / NPHI;
  for (int iz = 0; iz < NZ; ++iz) {
    Precision z = -c + iz * DZ;
    for (int iphi = 0; iphi < NPHI; ++iphi) {
      Precision eps = 0.5 * kHalfTolerance * (2. * RNG::Instance().uniform() - 1.);
      Precision phi = iphi * DPHI;
      Precision rho = std::sqrt((1. + z / c) * (1. - z / c));
      Precision px  = rho * std::cos(phi) * a + eps;
      Precision py  = rho * std::sin(phi) * b + eps;
      Precision pz  = z + eps;
      if (z < zbottom) pz = zbottom;
      if (z > ztop) pz = ztop;
      Vec_t p(px, py, pz);
      assert(solid.Inside(p) == vecgeom::kSurface);
      assert(solid.SafetyToOut(p) == 0.);
      p = Vec_t(px, py, pz) * 2.;
      assert(solid.Inside(p) == vecgeom::kOutside);
      assert(solid.SafetyToOut(p) < 0.);
      p = Vec_t(px, py, pz) * 0.5;
      assert(solid.Inside(p) == vecgeom::kInside);
      assert(solid.SafetyToOut(p) > 0.);
    }
  }

  // Check particular points to verify that the algorithm works as expected
  assert(solid.SafetyToOut(Vec_t(0, 0, 0)) == a);
  assert(solid.SafetyToOut(Vec_t(0, 0, 2.5)) == ztop - 2.5);

  ///////////////////////////////////////////////////////////////////////////////
  //
  // Check DistanceToIn()
  //
  std::cout << "=== Check DistanceToIn()" << std::endl;
  solid.SetSemiAxes(a = 3, b = 4, c = 5);
  solid.SetZCuts(zbottom = -4.5, ztop = 3.5);
  Precision sctop = std::sqrt((c - ztop) * (2. - c + ztop));
  Precision scbot = std::sqrt((c + zbottom) * (2. - c - zbottom));
  Precision del   = kTolerance / 3.;

  // set coordinates for points in grid
  static const int np = 11;

  Precision xxx[np] = {-a - 1, -a - del, -a, -a + del, -1, 0, 1, a - del, a, a + del, a + 1};
  Precision yyy[np] = {-b - 1, -b - del, -b, -b + del, -1, 0, 1, b - del, b, b + del, b + 1};
  Precision zzz[np] = {-4.5 - 1, -4.5f - del, -4.5, -4.5f + del, -1, 0, 1, 3.5f - del, 3.5, 3.5f + del, 3.5 + 1};

  // check directions parallel to +Z
  for (int ix = 0; ix < np; ++ix) {
    for (int iy = 0; iy < np; ++iy) {
      for (int iz = 0; iz < np; ++iz) {
        Vec_t p(xxx[ix], yyy[iy], zzz[iz]);
        Precision dist = solid.DistanceToIn(p, Vec_t(0, 0, 1));
        // Check inside points ("wrong" side)
        if (solid.Inside(p) == vecgeom::kInside) assert(dist < 0);
        // Check points on surface
        if (solid.Inside(p) == vecgeom::kSurface) {
          if (p.z() > zbottom + 1) {
            assert(dist == kInfLength);
          } else {
            assert(ApproxEqual<Precision>(dist, zbottom - p.z()));
          }
        }
        // Check outside points
        if (solid.Inside(p) == vecgeom::kOutside) {
          if (p.z() >= 0 || (p.x() * p.x() / (a * a) + p.y() * p.y() / (b * b)) >= 1.) {
            assert(dist == kInfLength);
          } else {
            if (p.x() * p.x() / (a * a * scbot * scbot) + p.y() * p.y() / (b * b * scbot * scbot) <= 1.) {
              assert(ApproxEqual<Precision>(dist, zbottom - p.z()));
            } else {
              Precision z = c * std::sqrt(1. - p.x() * p.x() / (a * a) - p.y() * p.y() / (b * b));
              assert(ApproxEqual<Precision>(dist, -z - p.z()));
            }
          }
        }
      }
    }
  }

  // check directions parallel to -Z
  for (int ix = 0; ix < np; ++ix) {
    for (int iy = 0; iy < np; ++iy) {
      for (int iz = 0; iz < np; ++iz) {
        Vec_t p(xxx[ix], yyy[iy], zzz[iz]);
        Precision dist = solid.DistanceToIn(p, Vec_t(0, 0, -1));
        // Check inside points ("wrong" side)
        if (solid.Inside(p) == vecgeom::kInside) assert(dist < 0);
        // Check points on surface
        if (solid.Inside(p) == vecgeom::kSurface) {
          if (p.z() < ztop - 1) {
            assert(dist == kInfLength);
          } else {
            assert(ApproxEqual<Precision>(dist, p.z() - ztop));
          }
        }
        // Check outside points
        if (solid.Inside(p) == vecgeom::kOutside) {
          if (p.z() <= 0 || (p.x() * p.x() / (a * a) + p.y() * p.y() / (b * b)) >= 1.) {
            assert(dist == kInfLength);
          } else {
            if (p.x() * p.x() / (a * a * sctop * sctop) + p.y() * p.y() / (b * b * sctop * sctop) <= 1.) {
              assert(ApproxEqual<Precision>(dist, p.z() - ztop));
            } else {
              Precision z = c * std::sqrt(1. - p.x() * p.x() / (a * a) - p.y() * p.y() / (b * b));
              assert(ApproxEqual<Precision>(dist, p.z() - z));
            }
          }
        }
      }
    }
  }

  // check directions parallel to +Y
  for (int ix = 0; ix < np; ++ix) {
    for (int iy = 0; iy < np; ++iy) {
      for (int iz = 0; iz < np; ++iz) {
        Vec_t p(xxx[ix], yyy[iy], zzz[iz]);
        Precision dist = solid.DistanceToIn(p, Vec_t(0, 1, 0));
        if (solid.Inside(p) == vecgeom::kInside) {
          assert(dist < 0);
        } else {
          if (p.y() >= 0 || p.z() < zbottom + kHalfTolerance || p.z() > ztop - kHalfTolerance ||
              (p.x() * p.x() / (a * a) + p.z() * p.z() / (c * c)) >= 1.) {
            assert(dist == kInfLength);
          } else {
            Precision y = b * std::sqrt(1. - p.x() * p.x() / (a * a) - p.z() * p.z() / (c * c));
            assert(ApproxEqual<Precision>(dist, -y - p.y()));
          }
        }
      }
    }
  }

  // check directions parallel to -X
  for (int ix = 0; ix < np; ++ix) {
    for (int iy = 0; iy < np; ++iy) {
      for (int iz = 0; iz < np; ++iz) {
        Vec_t p(xxx[ix], yyy[iy], zzz[iz]);
        Precision dist = solid.DistanceToIn(p, Vec_t(-1, 0, 0));
        if (solid.Inside(p) == vecgeom::kInside) {
          assert(dist < 0);
        } else {
          if (p.x() <= 0 || p.z() < zbottom + kHalfTolerance || p.z() > ztop - kHalfTolerance ||
              (p.y() * p.y() / (b * b) + p.z() * p.z() / (c * c)) >= 1.) {
            assert(dist == kInfLength);
          } else {
            Precision x = a * std::sqrt(1. - p.y() * p.y() / (b * b) - p.z() * p.z() / (c * c));
            assert(ApproxEqual<Precision>(dist, p.x() - x));
          }
        }
      }
    }
  }

  // check far points
  Precision Kfar = 1.e+5;
  int nz         = 40;
  int nphi       = 36;
  for (int iz = 0; iz < nz + 1; ++iz) {
    for (int iphi = 0; iphi < nphi; ++iphi) {
      Precision z   = -1. + iz * (2. / nz);
      Precision phi = iphi * (kTwoPi / nphi);
      Precision rho = std::sqrt(1. - z * z);
      Precision x   = rho * std::cos(phi);
      Precision y   = rho * std::sin(phi);
      if (z < zbottom / c) {
        x = a * x * zbottom / (c * z);
        y = b * y * zbottom / (c * z);
        z = zbottom;
      } else if (z > ztop / c) {
        x = a * x * ztop / (c * z);
        y = b * y * ztop / (c * z);
        z = ztop;
      } else {
        x *= a;
        y *= b;
        z *= c;
      }
      Vec_t p(x, y, z);
      Vec_t v        = -p.Unit();
      Precision dist = solid.DistanceToIn(Kfar * p, v);
      assert(std::abs(dist - (Kfar - 1.) * p.Mag()) < kHalfTolerance);
    }
  }

  ///////////////////////////////////////////////////////////////////////////////
  //
  // Check DistanceToOut()
  //
  std::cout << "=== Check DistanceToOut()" << std::endl;
  solid.SetSemiAxes(a = 3, b = 4, c = 5);
  solid.SetZCuts(zbottom = -4.5, ztop = 3.5);

  // check directions parallel to +Z
  for (int ix = 0; ix < np; ++ix) {
    for (int iy = 0; iy < np; ++iy) {
      for (int iz = 0; iz < np; ++iz) {
        Vec_t p(xxx[ix], yyy[iy], zzz[iz]);
        Precision dist = solid.DistanceToOut(p, Vec_t(0, 0, 1));
        // Check if point is outside ("wrong" side)
        if (solid.Inside(p) == vecgeom::kOutside) {
          assert(dist < 0);
        } else {
          if ((p.x() * p.x() / (a * a) + p.y() * p.y() / (b * b)) >= 1.) {
            assert(dist == 0.);
          } else if (solid.Inside(Vec_t(p.x(), p.y(), ztop)) == vecgeom::kSurface) {
            assert(std::abs(dist - (ztop - p.z())) < 0.01 * kHalfTolerance);
          } else {
            Precision z = c * std::sqrt(1. - p.x() * p.x() / (a * a) - p.y() * p.y() / (b * b));
            assert(std::abs(dist - (z - p.z())) < 0.01 * kHalfTolerance);
          }
        }
      }
    }
  }

  // check directions parallel to -Z
  for (int ix = 0; ix < np; ++ix) {
    for (int iy = 0; iy < np; ++iy) {
      for (int iz = 0; iz < np; ++iz) {
        Vec_t p(xxx[ix], yyy[iy], zzz[iz]);
        Precision dist = solid.DistanceToOut(p, Vec_t(0, 0, -1));
        // Check if point is outside ("wrong" side)
        if (solid.Inside(p) == vecgeom::kOutside) {
          assert(dist < 0);
        } else {
          if ((p.x() * p.x() / (a * a) + p.y() * p.y() / (b * b)) >= 1.) {
            assert(dist == 0.);
          } else if (solid.Inside(Vec_t(p.x(), p.y(), zbottom)) == vecgeom::kSurface) {
            assert(std::abs(dist - (p.z() - zbottom)) < 0.01 * kHalfTolerance);
          } else {
            Precision z = c * std::sqrt(1. - p.x() * p.x() / (a * a) - p.y() * p.y() / (b * b));
            assert(std::abs(dist - (p.z() + z)) < 0.01 * kHalfTolerance);
          }
        }
      }
    }
  }

  // check directions parallel to -Y
  for (int ix = 0; ix < np; ++ix) {
    for (int iy = 0; iy < np; ++iy) {
      for (int iz = 0; iz < np; ++iz) {
        Vec_t p(xxx[ix], yyy[iy], zzz[iz]);
        Precision dist = solid.DistanceToOut(p, Vec_t(0, -1, 0));
        // Check if point is outside ("wrong" side)
        if (solid.Inside(p) == vecgeom::kOutside) {
          assert(dist < 0);
        } else {
          if ((p.x() * p.x() / (a * a) + p.z() * p.z() / (c * c)) >= 1.) {
            assert(dist == 0.);
          } else {
            Precision y = b * std::sqrt(1. - p.x() * p.x() / (a * a) - p.z() * p.z() / (c * c));
            assert(std::abs(dist - (p.y() + y)) < 0.01 * kHalfTolerance);
          }
        }
      }
    }
  }

  // check directions parallel to +X
  for (int ix = 0; ix < np; ++ix) {
    for (int iy = 0; iy < np; ++iy) {
      for (int iz = 0; iz < np; ++iz) {
        Vec_t p(xxx[ix], yyy[iy], zzz[iz]);
        Precision dist = solid.DistanceToOut(p, Vec_t(1, 0, 0));
        // Check if point is outside ("wrong" side)
        if (solid.Inside(p) == vecgeom::kOutside) {
          assert(dist < 0);
        } else {
          if ((p.y() * p.y() / (b * b) + p.z() * p.z() / (c * c)) >= 1.) {
            assert(dist == 0.);
          } else {
            Precision x = a * std::sqrt(1. - p.y() * p.y() / (b * b) - p.z() * p.z() / (c * c));
            assert(std::abs(dist - (x - p.x())) < 0.01 * kHalfTolerance);
          }
        }
      }
    }
  }

  ///////////////////////////////////////////////////////////////////////////////
  //
  // Check SamplePointOnSurface()
  //
  std::cout << "=== Check SamplePointOnSurface()" << std::endl;
  solid.SetSemiAxes(a = 3, b = 4, c = 5);
  solid.SetZCuts(zbottom = -4.5, ztop = 3.5);
  area            = solid.SurfaceArea();
  Precision hbot  = 1. + zbottom / c;
  Precision htop  = 1. - ztop / c;
  Precision szneg = kPi * a * b * hbot * (2. - hbot);
  Precision szpos = kPi * a * b * htop * (2. - htop);
  Precision sside = area - szneg - szpos;

  Stopwatch timer;
  timer.Start();
  int nzneg = 0, nzpos = 0, nside = 0, nfactor = 100000, ntot = area * nfactor;
  for (int i = 0; i < ntot; i++) {
    Vec_t rndPoint = solid.GetUnplacedVolume()->SamplePointOnSurface();
    assert(solid.Inside(rndPoint) == vecgeom::kSurface);
    if (rndPoint.z() == zbottom)
      ++nzneg;
    else if (rndPoint.z() == ztop)
      ++nzpos;
    else
      ++nside;
  }
  timer.Stop();
  std::cout << "szneg,sside,szpos = " << szneg << ", \t" << sside << ", \t" << szpos << std::endl;
  std::cout << "nzneg,nside,nzpos = " << nzneg << ", \t" << nside << ", \t" << nzpos << std::endl;
  assert(std::abs(nzneg - szneg * nfactor) < 2. * std::sqrt(ntot));
  assert(std::abs(nside - sside * nfactor) < 2. * std::sqrt(ntot));
  assert(std::abs(nzpos - szpos * nfactor) < 2. * std::sqrt(ntot));
  std::cout << "Time : " << timer.Elapsed() << " sec   " << ntot / 1000000. << " million points" << std::endl;
  std::cout << "Time per million points : " << timer.Elapsed() * 1000000. / ntot << " sec" << std::endl;

  return true;
}

int main(int argc, char *argv[])
{
  assert(TestEllipsoid<vecgeom::SimpleEllipsoid>());
  std::cout << "VecGeom Ellipsoid passed\n";

  return 0;
}