1
+ #include < array>
1
2
#include < vector>
2
3
3
4
#include < cage-core/geometry.h>
@@ -25,6 +26,19 @@ namespace cage
25
26
Vec3 a = A - O;
26
27
Vec3 b = B - O;
27
28
Real denom = 2 * dot (cross (a, b), cross (a, b));
29
+ if (abs (denom) < 1e-5 )
30
+ {
31
+ Sphere sph[3 ] = {
32
+ MbSphere (O, A),
33
+ MbSphere (O, B),
34
+ MbSphere (A, B),
35
+ };
36
+ Sphere b = sph[0 ];
37
+ for (Sphere s : sph)
38
+ if (s.radius > b.radius )
39
+ b = s;
40
+ return b;
41
+ }
28
42
Vec3 o = (dot (b, b) * cross (cross (a, b), a) + dot (a, a) * cross (b, cross (a, b))) / denom;
29
43
return Sphere (O + o, length (o) + 1e-5 );
30
44
}
@@ -35,6 +49,20 @@ namespace cage
35
49
Vec3 b = B - O;
36
50
Vec3 c = C - O;
37
51
Real denom = 2 * determinant (Mat3 (a[0 ], a[1 ], a[2 ], b[0 ], b[1 ], b[2 ], c[0 ], c[1 ], c[2 ]));
52
+ if (abs (denom) < 1e-5 )
53
+ {
54
+ Sphere sph[4 ] = {
55
+ MbSphere (O, A, B),
56
+ MbSphere (O, A, C),
57
+ MbSphere (O, B, C),
58
+ MbSphere (A, B, C),
59
+ };
60
+ Sphere b = sph[0 ];
61
+ for (Sphere s : sph)
62
+ if (s.radius > b.radius )
63
+ b = s;
64
+ return b;
65
+ }
38
66
Vec3 o = (dot (c, c) * cross (a, b) + dot (b, b) * cross (c, a) + dot (a, a) * cross (b, c)) / denom;
39
67
return Sphere (O + o, length (o) + 1e-5 );
40
68
}
@@ -59,18 +87,15 @@ namespace cage
59
87
MB = MbSphere (*P[-1 ], *P[-2 ], *P[-3 ]);
60
88
break ;
61
89
case 4 :
62
- return MbSphere (*P[-1 ], *P[-2 ], *P[-3 ], *P[-4 ]);
90
+ MB = MbSphere (*P[-1 ], *P[-2 ], *P[-3 ], *P[-4 ]);
91
+ return MB;
63
92
}
64
93
for (uint32 i = 0 ; i < p; i++)
65
94
{
66
95
if (d2 (MB, *P[i]) > 0 )
67
96
{
68
97
for (uint32 j = i; j > 0 ; j--)
69
- {
70
- const Vec3 *T = P[j];
71
- P[j] = P[j - 1 ];
72
- P[j - 1 ] = T;
73
- }
98
+ std::swap (P[j], P[j - 1 ]);
74
99
MB = recurseMini (P + 1 , i, b + 1 );
75
100
}
76
101
}
@@ -81,19 +106,19 @@ namespace cage
81
106
Sphere::Sphere (const Frustum &other)
82
107
{
83
108
const Frustum::Corners corners = other.corners ();
84
- const Vec3 *L[ 8 ] ;
109
+ std::array< const Vec3 *, 8 > L ;
85
110
int i = 0 ;
86
111
for (const Vec3 &v : corners.data )
87
112
L[i++] = &v;
88
- *this = recurseMini (L, 8 , 0 );
113
+ *this = recurseMini (L. data () , 8 , 0 );
89
114
}
90
115
91
116
Sphere makeSphere (PointerRange<const Vec3> points)
92
117
{
93
118
switch (points.size ())
94
119
{
95
120
case 0 :
96
- return Sphere ();
121
+ return Sphere (Vec3 (), 0 );
97
122
case 1 :
98
123
return Sphere (points[0 ], 0 );
99
124
case 2 :
0 commit comments