-
Notifications
You must be signed in to change notification settings - Fork 15
/
overlap.hpp
2200 lines (1718 loc) · 65.1 KB
/
overlap.hpp
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Exact calculation of the overlap volume of spheres and mesh elements.
* http://dx.doi.org/10.1016/j.jcp.2016.02.003
*
* Copyright (C) 2015-2018 Severin Strobl <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OVERLAP_HPP
#define OVERLAP_HPP
// Eigen
#include <Eigen/Core>
#include <Eigen/Geometry>
// C++
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iterator>
#include <limits>
#include <numeric>
#include <stdexcept>
#include <type_traits>
#include <utility>
// typedefs
typedef double scalar_t;
typedef Eigen::Matrix<scalar_t, 3, 1, Eigen::DontAlign> vector_t;
typedef Eigen::Matrix<scalar_t, 2, 1, Eigen::DontAlign> vector2_t;
// Pretty-printing of Eigen matrices.
static const Eigen::IOFormat pretty(Eigen::StreamPrecision,
Eigen::DontAlignCols, " ", ";\n", "", "", "[", "]");
// constants
const scalar_t pi = scalar_t(4) * std::atan(scalar_t(1.0));
namespace detail {
static const scalar_t tinyEpsilon(2 *
std::numeric_limits<scalar_t>::epsilon());
static const scalar_t mediumEpsilon(1e2 * tinyEpsilon);
static const scalar_t largeEpsilon(1e-10);
// Robust calculation of the normal vector of a polygon using Newell's method
// and a pre-calculated center.
// Ref: Christer Ericson - Real-Time Collision Detection (2005)
template<typename Iterator>
inline vector_t normalNewell(Iterator begin, Iterator end, const vector_t&
center) {
const size_t count = end - begin;
vector_t n(vector_t::Zero());
for(size_t i = 0; i < count; ++i)
n += (*(begin + i) - center).cross(*(begin + ((i + 1) % count)) -
center);
scalar_t length = n.stableNorm();
if(length)
return n / length;
else
return n;
}
// This implementation of double_prec is based on:
// T.J. Dekker, A floating-point technique for extending the available
// precision, http://dx.doi.org/10.1007/BF01397083
template<typename T>
struct double_prec_constant;
template<>
struct double_prec_constant<float> {
// Constant used to split double precision values:
// 2^(24 - 24/2) + 1 = 2^12 + 1 = 4097
static const uint32_t value = 4097;
};
template<>
struct double_prec_constant<double> {
// Constant used to split double precision values:
// 2^(53 - int(53/2)) + 1 = 2^27 + 1 = 134217729
static const uint32_t value = 134217729;
};
// For GCC and Clang an attribute can be used to control the FP precision...
#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && \
!defined(__INTEL_COMPILER)
#define ENFORCE_EXACT_FPMATH_ATTR __attribute__((__target__("ieee-fp")))
#else
#define ENFORCE_EXACT_FPMATH_ATTR
#endif
// ... whereas ICC requires a pragma.
#if defined(__ICC) || defined(__INTEL_COMPILER)
#define ENFORCE_EXACT_FPMATH_ATTR
#define USE_EXACT_FPMATH_PRAGMA 1
#endif
template<typename T>
class double_prec;
template<typename T>
inline double_prec<T> operator+(const double_prec<T>& lhs, const
double_prec<T>& rhs) ENFORCE_EXACT_FPMATH_ATTR;
template<typename T>
inline double_prec<T> operator-(const double_prec<T>& lhs, const
double_prec<T>& rhs) ENFORCE_EXACT_FPMATH_ATTR;
template<typename T>
inline double_prec<T> operator*(const double_prec<T>& lhs, const
double_prec<T>& rhs) ENFORCE_EXACT_FPMATH_ATTR;
template<typename T>
class double_prec {
private:
static const uint32_t c = detail::double_prec_constant<T>::value;
template<typename TF>
friend double_prec<TF> operator+(const double_prec<TF>&, const
double_prec<TF>&);
template<typename TF>
friend double_prec<TF> operator-(const double_prec<TF>&, const
double_prec<TF>&);
template<typename TF>
friend double_prec<TF> operator*(const double_prec<TF>&, const
double_prec<TF>&);
public:
inline double_prec() : h_(0), l_(0) {
}
// This constructor requires floating point operations in accordance
// with IEEE754 to perform the proper splitting. To allow full
// optimization of all other parts of the code, precise floating point
// ops are only requested here. Unfortunately the way to do this is
// extremely compiler dependent.
inline double_prec(const T& val) ENFORCE_EXACT_FPMATH_ATTR : h_(0),
l_(0) {
#ifdef USE_EXACT_FPMATH_PRAGMA
#pragma float_control(precise, on)
#endif
T p = val * T(c);
h_ = (val - p) + p;
l_ = val - h_;
}
private:
inline explicit double_prec(const T& h, const T& l) : h_(h), l_(l) {
}
public:
inline const T& high() const {
return h_;
}
inline const T& low() const {
return l_;
}
inline T value() const {
return h_ + l_;
}
template<typename TOther>
inline TOther convert() const {
return TOther(h_) + TOther(l_);
}
private:
T h_;
T l_;
};
template<typename T>
inline double_prec<T> operator+(const double_prec<T>& lhs, const
double_prec<T>& rhs) {
#ifdef USE_EXACT_FPMATH_PRAGMA
#pragma float_control(precise, on)
#endif
T h = lhs.h_ + rhs.h_;
T l = std::abs(lhs.h_) >= std::abs(rhs.h_) ?
((((lhs.h_ - h) + rhs.h_) + lhs.l_) + rhs.l_) :
((((rhs.h_ - h) + lhs.h_) + rhs.l_) + lhs.l_);
T c = h + l;
return double_prec<T>(c, (h - c) + l);
}
template<typename T>
inline double_prec<T> operator-(const double_prec<T>& lhs, const
double_prec<T>& rhs) {
#ifdef USE_EXACT_FPMATH_PRAGMA
#pragma float_control(precise, on)
#endif
T h = lhs.h_ - rhs.h_;
T l = std::abs(lhs.h_) >= std::abs(rhs.h_) ?
((((lhs.h_ - h) - rhs.h_) - rhs.l_) + lhs.l_) :
((((-rhs.h_ - h) + lhs.h_) + lhs.l_) - rhs.l_);
T c = h + l;
return double_prec<T>(c, (h - c) + l);
}
template<typename T>
inline double_prec<T> operator*(const double_prec<T>& lhs, const
double_prec<T>& rhs) {
#ifdef USE_EXACT_FPMATH_PRAGMA
#pragma float_control(precise, on)
#endif
double_prec<T> l(lhs.h_);
double_prec<T> r(rhs.h_);
T p = l.h_ * r.h_;
T q = l.h_ * r.l_ + l.l_ * r.h_;
T v = p + q;
double_prec<T> c(v, ((p - v) + q) + l.l_ * r.l_);
c.l_ = ((lhs.h_ + lhs.l_) * rhs.l_ + lhs.l_ * rhs.h_) + c.l_;
T z = c.value();
return double_prec<T>(z, (c.h_ - z) + c.l_);
}
// Ref: J.R. Shewchuk - Lecture Notes on Geometric Robustness
// http://www.cs.berkeley.edu/~jrs/meshpapers/robnotes.pdf
inline scalar_t orient2D(const vector2_t& a, const vector2_t& b, const
vector2_t& c) {
typedef double_prec<scalar_t> real_t;
real_t a0(a[0]);
real_t a1(a[1]);
real_t b0(b[0]);
real_t b1(b[1]);
real_t c0(c[0]);
real_t c1(c[1]);
real_t result = (a0 - c0) * (b1 - c1) - (a1 - c1) * (b0 - c0);
return result.convert<scalar_t>();
}
// Numerically robust calculation of the normal of the triangle defined by
// the points a, b, and c.
// Ref: J.R. Shewchuk - Lecture Notes on Geometric Robustness
// http://www.cs.berkeley.edu/~jrs/meshpapers/robnotes.pdf
inline vector_t triangleNormal(const vector_t& a, const vector_t& b, const
vector_t& c) {
scalar_t xy = orient2D(vector2_t(a[0], a[1]), vector2_t(b[0], b[1]),
vector2_t(c[0], c[1]));
scalar_t yz = orient2D(vector2_t(a[1], a[2]), vector2_t(b[1], b[2]),
vector2_t(c[1], c[2]));
scalar_t zx = orient2D(vector2_t(a[2], a[0]), vector2_t(b[2], b[0]),
vector2_t(c[2], c[0]));
return vector_t(yz, zx, xy).normalized();
}
// Numerically robust routine to calculate the angle between normalized
// vectors.
// Ref: http://www.plunk.org/~hatch/rightway.php
inline scalar_t angle(const vector_t& v0, const vector_t& v1) {
if(v0.dot(v1) < scalar_t(0))
return pi - scalar_t(2) * std::asin(scalar_t(0.5) * (v0 +
v1).stableNorm());
else
return scalar_t(2) * std::asin(scalar_t(0.5) * (v0 - v1).stableNorm());
}
template<typename Derived0, typename Derived1>
inline std::array<vector_t, 2> gramSchmidt(const Eigen::MatrixBase<Derived0>&
arg0, const Eigen::MatrixBase<Derived1>& arg1) {
vector_t v0(arg0.normalized());
vector_t v1(arg1);
std::array<vector_t, 2> result;
result[0] = v0;
result[1] = (v1 - v1.dot(v0) * v0).normalized();
return result;
}
inline scalar_t clamp(scalar_t value, scalar_t min, scalar_t max, scalar_t
limit) {
assert(min <= max && limit >= scalar_t(0));
value = (value < min && value > (min - limit)) ? min : value;
value = (value > max && value < (max + limit)) ? max : value;
return value;
}
} // namespace detail
class Transformation {
public:
Transformation(const vector_t& t, const scalar_t& s) : translation(t),
scaling(s) {
}
vector_t translation;
scalar_t scaling;
};
template<size_t VertexCount>
class Polygon {
private:
static_assert(VertexCount >= 3 && VertexCount <= 4,
"Only triangles and quadrilateral are supported.");
public:
static const size_t vertex_count = VertexCount;
protected:
Polygon() : vertices(), center(), normal(), area() {
}
template<typename... Types>
Polygon(const vector_t& v0, Types... verts) : vertices{{v0,
verts...}}, center(), normal(), area() {
center = scalar_t(1.0 / vertex_count) *
std::accumulate(vertices.begin(), vertices.end(),
vector_t::Zero().eval());
// For a quadrilateral, Newell's method can be simplified
// significantly.
// Ref: Christer Ericson - Real-Time Collision Detection (2005)
if(VertexCount == 4) {
normal = ((vertices[2] - vertices[0]).cross(vertices[3] -
vertices[1])).normalized();
} else {
normal = detail::normalNewell(vertices.begin(), vertices.end(),
center);
}
}
void apply(const Transformation& t) {
for(auto& v : vertices)
v = t.scaling * (v + t.translation);
center = t.scaling * (center + t.translation);
}
public:
bool isPlanar(const scalar_t epsilon = detail::largeEpsilon) const {
if(VertexCount == 3)
return true;
for(auto& v : vertices)
if(std::abs(normal.dot(v - center)) > epsilon)
return false;
return true;
}
public:
std::array<vector_t, vertex_count> vertices;
vector_t center;
vector_t normal;
scalar_t area;
};
class Triangle : public Polygon<3> {
public:
Triangle() : Polygon<3>() {
}
template<typename... Types>
Triangle(const vector_t& v0, Types... verts) : Polygon<3>(v0,
verts...) {
init();
}
void apply(const Transformation& t) {
Polygon<3>::apply(t);
init();
}
private:
void init() {
area = scalar_t(0.5) * ((vertices[1] - vertices[0]).cross(
vertices[2] - vertices[0])).stableNorm();
}
};
class Quadrilateral : public Polygon<4> {
public:
Quadrilateral() : Polygon<4>() {
}
template<typename... Types>
Quadrilateral(const vector_t& v0, Types... verts) : Polygon<4>(v0,
verts...) {
init();
}
void apply(const Transformation& t) {
Polygon<4>::apply(t);
init();
}
private:
void init() {
area = scalar_t(0.5) * (((vertices[1] - vertices[0]).cross(
vertices[2] - vertices[0])).stableNorm() +
((vertices[2] - vertices[0]).cross(
vertices[3] - vertices[0])).stableNorm());
}
};
// Forward declarations of the mesh elements.
class Tetrahedron;
class Wedge;
class Hexahedron;
namespace detail {
// Some tricks are required to keep this code header-only.
template<typename T, typename Nil>
struct mappings;
template<typename Nil>
struct mappings<Tetrahedron, Nil> {
// Map edges of a tetrahedron to vertices and faces.
static const uint32_t edge_mapping[6][2][2];
// Map vertices of a tetrahedron to edges and faces.
// 0: local IDs of the edges intersecting at this vertex
// 1: 0 if the edge is pointing away from the vertex, 1 otherwise
// 2: faces joining at the vertex
static const uint32_t vertex_mapping[4][3][3];
// This mapping contains the three sets of the two edges for each of the
// faces joining at a vertex. The indices are mapped to the local edge IDs
// using the first value field of the 'vertex_mapping' table.
static const uint32_t face_mapping[3][2];
};
template<typename Nil>
const uint32_t mappings<Tetrahedron, Nil>::edge_mapping[6][2][2] = {
{ { 0, 1 }, { 0, 1 } }, { { 1, 2 }, { 0, 2 } }, { { 2, 0 }, { 0, 3 } },
{ { 0, 3 }, { 1, 3 } }, { { 1, 3 }, { 1, 2 } }, { { 2, 3 }, { 2, 3 } }
};
template<typename Nil>
const uint32_t mappings<Tetrahedron, Nil>::vertex_mapping[4][3][3] = {
{ { 0, 2, 3 }, { 0, 1, 0 }, { 0, 1, 3 } },
{ { 0, 1, 4 }, { 1, 0, 0 }, { 0, 1, 2 } },
{ { 1, 2, 5 }, { 1, 0, 0 }, { 0, 2, 3 } },
{ { 3, 4, 5 }, { 1, 1, 1 }, { 1, 3, 2 } }
};
template<typename Nil>
const uint32_t mappings<Tetrahedron, Nil>::face_mapping[3][2] = {
{ 0, 1 }, { 0, 2 }, { 1, 2 }
};
typedef mappings<Tetrahedron, void> tet_mappings;
} // namespace detail
class Tetrahedron : public detail::tet_mappings {
public:
template<typename... Types>
Tetrahedron(const vector_t& v0, Types... verts) : vertices{{v0,
verts...}}, faces(), center(), volume() {
#ifndef NDEBUG
// Make sure the ordering of the vertices is correct.
assert((vertices[1] - vertices[0]).cross(vertices[2] -
vertices[0]).dot(vertices[3] - vertices[0]) >= scalar_t(0));
#endif // NDEBUG
init();
}
Tetrahedron(const std::array<vector_t, 4>& verts) : vertices(verts),
faces(), center(), volume() {
init();
}
Tetrahedron() : vertices{{vector_t::Zero(), vector_t::Zero(),
vector_t::Zero(), vector_t::Zero()}}, faces(), center(), volume() {
}
void apply(const Transformation& t) {
for(auto& v : vertices)
v = t.scaling * (v + t.translation);
for(auto& f : faces)
f.apply(t);
center = scalar_t(0.25) * std::accumulate(vertices.begin(),
vertices.end(), vector_t::Zero().eval());
volume = calcVolume();
}
scalar_t surfaceArea() const {
scalar_t area(0);
for(const auto& f : faces)
area += f.area;
return area;
}
private:
void init() {
// 0: v2, v1, v0
faces[0] = Triangle(vertices[2], vertices[1], vertices[0]);
// 1: v0, v1, v3
faces[1] = Triangle(vertices[0], vertices[1], vertices[3]);
// 2: v1, v2, v3
faces[2] = Triangle(vertices[1], vertices[2], vertices[3]);
// 3: v2, v0, v3
faces[3] = Triangle(vertices[2], vertices[0], vertices[3]);
center = scalar_t(0.25) * std::accumulate(vertices.begin(),
vertices.end(), vector_t::Zero().eval());
volume = calcVolume();
}
scalar_t calcVolume() const {
return scalar_t(1.0 / 6.0) * std::abs((vertices[0] -
vertices[3]).dot((vertices[1] - vertices[3]).cross(
vertices[2] - vertices[3])));
}
public:
std::array<vector_t, 4> vertices;
std::array<Triangle, 4> faces;
vector_t center;
scalar_t volume;
};
namespace detail {
template<typename Nil>
struct mappings<Wedge, Nil> {
// Map edges of a wedge to vertices and faces.
static const uint32_t edge_mapping[9][2][2];
// Map vertices of a wedge to edges and faces.
// 0: local IDs of the edges intersecting at this vertex
// 1: 0 if the edge is pointing away from the vertex, 1 otherwise
// 2: faces joining at the vertex
static const uint32_t vertex_mapping[6][3][3];
// This mapping contains the three sets of the two edges for each of the
// faces joining at a vertex. The indices are mapped to the local edge IDs
// using the first value field of the 'vertex_mapping' table.
static const uint32_t face_mapping[3][2];
};
template<typename Nil>
const uint32_t mappings<Wedge, Nil>::edge_mapping[9][2][2] = {
{ { 0, 1 }, { 0, 1 } }, { { 1, 2 }, { 0, 2 } }, { { 2, 0 }, { 0, 3 } },
{ { 0, 3 }, { 1, 3 } }, { { 1, 4 }, { 1, 2 } }, { { 2, 5 }, { 2, 3 } },
{ { 3, 4 }, { 1, 4 } }, { { 4, 5 }, { 2, 4 } }, { { 5, 3 }, { 3, 4 } }
};
template<typename Nil>
const uint32_t mappings<Wedge, Nil>::vertex_mapping[6][3][3] = {
{ { 0, 2, 3 }, { 0, 1, 0 }, { 0, 1, 3 } },
{ { 0, 1, 4 }, { 1, 0, 0 }, { 0, 1, 2 } },
{ { 1, 2, 5 }, { 1, 0, 0 }, { 0, 2, 3 } },
{ { 3, 6, 8 }, { 1, 0, 1 }, { 1, 3, 4 } },
{ { 4, 6, 7 }, { 1, 1, 0 }, { 1, 2, 4 } },
{ { 5, 7, 8 }, { 1, 1, 0 }, { 2, 3, 4 } }
};
template<typename Nil>
const uint32_t mappings<Wedge, Nil>::face_mapping[3][2] = {
{ 0, 1 }, { 0, 2 }, { 1, 2 }
};
typedef mappings<Wedge, void> wedge_mappings;
} // namespace detail
class Wedge : public detail::wedge_mappings {
public:
template<typename... Types>
Wedge(const vector_t& v0, Types... verts) : vertices{{v0,
verts...}}, faces(), center(), volume() {
init();
}
Wedge(const std::array<vector_t, 6>& verts) : vertices(verts),
faces(), center(), volume() {
init();
}
Wedge() : vertices{{vector_t::Zero(), vector_t::Zero(),
vector_t::Zero(), vector_t::Zero(), vector_t::Zero(),
vector_t::Zero()}}, faces(), center(), volume() {
}
void apply(const Transformation& t) {
for(auto& v : vertices)
v = t.scaling * (v + t.translation);
for(auto& f : faces)
f.apply(t);
center = scalar_t(1.0 / 6.0) * std::accumulate(vertices.begin(),
vertices.end(), vector_t::Zero().eval());
volume = calcVolume();
}
scalar_t surfaceArea() const {
scalar_t area(0);
for(const auto& f : faces)
area += f.area;
return area;
}
private:
void init() {
// All faces of the wedge are stored as quadrilaterals, so an
// additional point is inserted between v0 and v1.
// 0: v2, v1, v0, v02
faces[0] = Quadrilateral(vertices[2], vertices[1], vertices[0],
scalar_t(0.5) * (vertices[0] + vertices[2]));
// 1: v0, v1, v4, v3
faces[1] = Quadrilateral(vertices[0], vertices[1], vertices[4],
vertices[3]);
// 2: v1, v2, v5, v4
faces[2] = Quadrilateral(vertices[1], vertices[2], vertices[5],
vertices[4]);
// 3: v2, v0, v3, v5
faces[3] = Quadrilateral(vertices[2], vertices[0], vertices[3],
vertices[5]);
// All faces of the wedge are stored as quadrilaterals, so an
// additional point is inserted between v3 and v5.
// 4: v3, v4, v5, v53
faces[4] = Quadrilateral(vertices[3], vertices[4], vertices[5],
scalar_t(0.5) * (vertices[5] + vertices[3]));
center = scalar_t(1.0 / 6.0) * std::accumulate(vertices.begin(),
vertices.end(), vector_t::Zero().eval());
volume = calcVolume();
}
scalar_t calcVolume() const {
// The wedge is treated as a degenerate hexahedron here by adding
// two fake vertices v02 and v35.
vector_t diagonal(vertices[5] - vertices[0]);
return scalar_t(1.0 / 6.0) * (diagonal.dot(((vertices[1] -
vertices[0]).cross(vertices[2] - vertices[4])) +
((vertices[3] - vertices[0]).cross(
vertices[4] - scalar_t(0.5) * (vertices[3] + vertices[5]))) +
((scalar_t(0.5) * (vertices[0] + vertices[2]) -
vertices[0]).cross(scalar_t(0.5) * (vertices[3] +
vertices[5]) - vertices[2]))));
}
public:
std::array<vector_t, 6> vertices;
std::array<Quadrilateral, 5> faces;
vector_t center;
scalar_t volume;
};
namespace detail {
template<typename Nil>
struct mappings<Hexahedron, Nil> {
// Map edges of a hexahedron to vertices and faces.
static const uint32_t edge_mapping[12][2][2];
// Map vertices of a hexahedron to edges and faces.
// 0: local IDs of the edges intersecting at this vertex
// 1: 0 if the edge is pointing away from the vertex, 1 otherwise
// 2: faces joining at the vertex
static const uint32_t vertex_mapping[8][3][3];
// This mapping contains the three sets of the two edges for each of the
// faces joining at a vertex. The indices are mapped to the local edge IDs
// using the first value field of the 'vertex_mapping' table.
static const uint32_t face_mapping[3][2];
};
template<typename Nil>
const uint32_t mappings<Hexahedron, Nil>::edge_mapping[12][2][2] = {
{ { 0, 1 }, { 0, 1 } }, { { 1, 2 }, { 0, 2 } },
{ { 2, 3 }, { 0, 3 } }, { { 3, 0 }, { 0, 4 } },
{ { 0, 4 }, { 1, 4 } }, { { 1, 5 }, { 1, 2 } },
{ { 2, 6 }, { 2, 3 } }, { { 3, 7 }, { 3, 4 } },
{ { 4, 5 }, { 1, 5 } }, { { 5, 6 }, { 2, 5 } },
{ { 6, 7 }, { 3, 5 } }, { { 7, 4 }, { 4, 5 } }
};
template<typename Nil>
const uint32_t mappings<Hexahedron, Nil>::vertex_mapping[8][3][3] = {
{ { 0, 3, 4 }, { 0, 1, 0 }, { 0, 1, 4 } },
{ { 0, 1, 5 }, { 1, 0, 0 }, { 0, 1, 2 } },
{ { 1, 2, 6 }, { 1, 0, 0 }, { 0, 2, 3 } },
{ { 2, 3, 7 }, { 1, 0, 0 }, { 0, 3, 4 } },
{ { 4, 8, 11 }, { 1, 0, 1 }, { 1, 4, 5 } },
{ { 5, 8, 9 }, { 1, 1, 0 }, { 1, 2, 5 } },
{ { 6, 9, 10 }, { 1, 1, 0 }, { 2, 3, 5 } },
{ { 7, 10, 11 }, { 1, 1, 0 }, { 3, 4, 5 } }
};
template<typename Nil>
const uint32_t mappings<Hexahedron, Nil>::face_mapping[3][2] = {
{ 0, 1 }, { 0, 2 }, { 1, 2 }
};
typedef mappings<Hexahedron, void> hex_mappings;
} // namespace detail
class Hexahedron : public detail::hex_mappings {
public:
template<typename... Types>
Hexahedron(const vector_t& v0, Types... verts) : vertices{{v0,
verts...}}, faces(), center(), volume() {
init();
}
Hexahedron(const std::array<vector_t, 8>& verts) : vertices(verts),
faces(), center(), volume() {
init();
}
void apply(const Transformation& t) {
for(auto& v : vertices)
v = t.scaling * (v + t.translation);
for(auto& f : faces)
f.apply(t);
center = scalar_t(1.0 / 8.0) * std::accumulate(vertices.begin(),
vertices.end(), vector_t::Zero().eval());
volume = calcVolume();
}
scalar_t surfaceArea() const {
scalar_t area(0);
for(const auto& f : faces)
area += f.area;
return area;
}
private:
void init() {
// 0: v3, v2, v1, v0
faces[0] = Quadrilateral(vertices[3], vertices[2], vertices[1],
vertices[0]);
// 1: v0, v1, v5, v4
faces[1] = Quadrilateral(vertices[0], vertices[1], vertices[5],
vertices[4]);
// 2: v1, v2, v6, v5
faces[2] = Quadrilateral(vertices[1], vertices[2], vertices[6],
vertices[5]);
// 3: v2, v3, v7, v6
faces[3] = Quadrilateral(vertices[2], vertices[3], vertices[7],
vertices[6]);
// 4: v3, v0, v4, v7
faces[4] = Quadrilateral(vertices[3], vertices[0], vertices[4],
vertices[7]);
// 5: v4, v5, v6, v7
faces[5] = Quadrilateral(vertices[4], vertices[5], vertices[6],
vertices[7]);
center = scalar_t(1.0 / 8.0) * std::accumulate(vertices.begin(),
vertices.end(), vector_t::Zero().eval());
volume = calcVolume();
}
scalar_t calcVolume() const {
vector_t diagonal(vertices[6] - vertices[0]);
return scalar_t(1.0 / 6.0) * diagonal.dot(((vertices[1] -
vertices[0]).cross(vertices[2] - vertices[5])) +
((vertices[4] - vertices[0]).cross(
vertices[5] - vertices[7])) +
((vertices[3] - vertices[0]).cross(
vertices[7] - vertices[2])));
}
public:
std::array<vector_t, 8> vertices;
std::array<Quadrilateral, 6> faces;
vector_t center;
scalar_t volume;
};
class Sphere {
public:
Sphere(const vector_t& c, scalar_t r) : center(c), radius(r),
volume(scalar_t(4.0 / 3.0 * pi) * r * r * r) {
}
scalar_t capVolume(scalar_t h) const {
if(h <= scalar_t(0))
return scalar_t(0);
else if(h >= scalar_t(2) * radius)
return volume;
else
return scalar_t(pi / 3.0) * h * h * (scalar_t(3) * radius - h);
}
scalar_t capSurfaceArea(scalar_t h) const {
if(h <= scalar_t(0))
return scalar_t(0);
else if(h >= scalar_t(2) * radius)
return surfaceArea();
else
return scalar_t(2 * pi) * radius * h;
}
scalar_t diskArea(scalar_t h) const {
if(h <= scalar_t(0) || h >= scalar_t(2) * radius)
return scalar_t(0);
else
return pi * h * (scalar_t(2) * radius - h);
}
scalar_t surfaceArea() const {
return (scalar_t(4) * pi) * (radius * radius);
}
public:
vector_t center;
scalar_t radius;
scalar_t volume;
};
class Plane {
public:
Plane(const vector_t& c, const vector_t& n) : center(c), normal(n) {
}
public:
vector_t center;
vector_t normal;
};
class AABB {
public:
AABB(const vector_t& minimum = vector_t::Constant(std::numeric_limits<
scalar_t>::infinity()), const vector_t& maximum =
vector_t::Constant(-std::numeric_limits<scalar_t>::infinity())) :
min(minimum), max(maximum) {
}
bool intersects(const AABB& aabb) const {
if((min.array() > aabb.max.array()).any() ||
(max.array() < aabb.min.array()).any())
return false;
return true;
}
AABB overlap(const AABB& aabb) const {
return AABB(min.cwiseMax(aabb.min), max.cwiseMin(aabb.max));
}
bool contains(const vector_t& p) const {
if((p.array() < min.array()).any() ||
(p.array() > max.array()).any())
return false;
return true;
}
void include(const vector_t& point) {
min = min.cwiseMin(point);
max = max.cwiseMax(point);
}
template<size_t N>
void include(const std::array<vector_t, N>& points) {
for(const auto& p : points)
include(p);
}
scalar_t volume() const {
vector_t size(max - min);
return size[0] * size[1] * size[2];
}
public:
vector_t min, max;
};
// Decomposition of a tetrahedron into 4 tetrahedra.
inline void decompose(const Tetrahedron& tet, std::array<Tetrahedron, 4>&
tets) {
tets[0] = Tetrahedron(tet.vertices[0], tet.vertices[1], tet.vertices[2],
tet.center);
tets[1] = Tetrahedron(tet.vertices[0], tet.vertices[1], tet.center,
tet.vertices[3]);
tets[2] = Tetrahedron(tet.vertices[1], tet.vertices[2], tet.center,
tet.vertices[3]);
tets[3] = Tetrahedron(tet.vertices[2], tet.vertices[0], tet.center,
tet.vertices[3]);
}
// Decomposition of a hexahedron into 2 wedges.
inline void decompose(const Hexahedron& hex, std::array<Wedge, 2>& wedges) {
wedges[0] = Wedge(hex.vertices[0], hex.vertices[1], hex.vertices[2],
hex.vertices[4], hex.vertices[5], hex.vertices[6]);
wedges[1] = Wedge(hex.vertices[0], hex.vertices[2], hex.vertices[3],
hex.vertices[4], hex.vertices[6], hex.vertices[7]);
}
// Decomposition of a hexahedron into 5 tetrahedra.
inline void decompose(const Hexahedron& hex, std::array<Tetrahedron, 5>&
tets) {
tets[0] = Tetrahedron(hex.vertices[0], hex.vertices[1], hex.vertices[2],
hex.vertices[5]);
tets[1] = Tetrahedron(hex.vertices[0], hex.vertices[2], hex.vertices[7],
hex.vertices[5]);
tets[2] = Tetrahedron(hex.vertices[0], hex.vertices[2], hex.vertices[3],
hex.vertices[7]);
tets[3] = Tetrahedron(hex.vertices[0], hex.vertices[5], hex.vertices[7],
hex.vertices[4]);