Skip to content

Commit 5e8c846

Browse files
committed
Cleanup.
1 parent 51c3264 commit 5e8c846

File tree

3 files changed

+138
-76
lines changed

3 files changed

+138
-76
lines changed

include/bx/bounds.h

+80-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010

1111
namespace bx
1212
{
13+
///
14+
struct Line
15+
{
16+
Vec3 pos = init::None;
17+
Vec3 dir = init::None;
18+
};
19+
20+
///
21+
struct LineSegment
22+
{
23+
Vec3 pos = init::None;
24+
Vec3 end = init::None;
25+
};
26+
1327
///
1428
struct Aabb
1529
{
@@ -20,33 +34,33 @@ namespace bx
2034
///
2135
struct Capsule
2236
{
23-
Vec3 pos = init::None;
24-
Vec3 end = init::None;
25-
float radius;
37+
Vec3 pos = init::None;
38+
Vec3 end = init::None;
39+
float radius;
2640
};
2741

2842
///
2943
struct Cone
3044
{
31-
Vec3 pos = init::None;
32-
Vec3 end = init::None;
33-
float radius;
45+
Vec3 pos = init::None;
46+
Vec3 end = init::None;
47+
float radius;
3448
};
3549

3650
///
3751
struct Cylinder
3852
{
39-
Vec3 pos = init::None;
40-
Vec3 end = init::None;
41-
float radius;
53+
Vec3 pos = init::None;
54+
Vec3 end = init::None;
55+
float radius;
4256
};
4357

4458
///
4559
struct Disk
4660
{
47-
Vec3 center = init::None;
48-
Vec3 normal = init::None;
49-
float radius;
61+
Vec3 center = init::None;
62+
Vec3 normal = init::None;
63+
float radius;
5064
};
5165

5266
///
@@ -58,8 +72,8 @@ namespace bx
5872
///
5973
struct Sphere
6074
{
61-
Vec3 center = init::None;
62-
float radius;
75+
Vec3 center = init::None;
76+
float radius;
6377
};
6478

6579
///
@@ -84,9 +98,37 @@ namespace bx
8498
Plane plane = init::None;
8599
};
86100

101+
///
102+
struct Interval
103+
{
104+
///
105+
Interval(float _val);
106+
107+
///
108+
Interval(float _min, float _max);
109+
110+
///
111+
void set(float _val);
112+
113+
///
114+
void setCenter(float _val);
115+
116+
///
117+
void expand(float _val);
118+
119+
float min;
120+
float max;
121+
};
122+
87123
///
88124
Vec3 getPointAt(const Ray& _ray, float _t);
89125

126+
///
127+
Vec3 getPointAt(const Line& _line, float _t);
128+
129+
///
130+
Vec3 getPointAt(const LineSegment& _line, float _t);
131+
90132
///
91133
Vec3 getCenter(const Aabb& _aabb);
92134

@@ -183,6 +225,30 @@ namespace bx
183225
/// Intersect ray / triangle.
184226
bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit = NULL);
185227

228+
///
229+
Vec3 closestPoint(const Line& _line, const Vec3& _point);
230+
231+
///
232+
Vec3 closestPoint(const LineSegment& _line, const Vec3& _point);
233+
234+
///
235+
Vec3 closestPoint(const Plane& _plane, const Vec3& _point);
236+
237+
///
238+
Vec3 closestPoint(const Aabb& _aabb, const Vec3& _point);
239+
240+
///
241+
Vec3 closestPoint(const Obb& _obb, const Vec3& _point);
242+
243+
///
244+
Vec3 closestPoint(const Triangle& _triangle, const Vec3& _point);
245+
246+
///
247+
bool overlap(const Interval& _interval, float _pos);
248+
249+
///
250+
bool overlap(const Interval& _intervalA, const Interval& _intervalB);
251+
186252
///
187253
bool overlap(const Aabb& _aabb, const Vec3& _pos);
188254

include/bx/inline/bounds.inl

+55
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,71 @@
99

1010
namespace bx
1111
{
12+
inline Interval::Interval(float _val)
13+
: min(_val)
14+
, max(_val)
15+
{
16+
}
17+
18+
inline Interval::Interval(float _min, float _max)
19+
: min(_min)
20+
, max(_max)
21+
{
22+
}
23+
24+
inline void Interval::set(float _val)
25+
{
26+
min = _val;
27+
max = _val;
28+
}
29+
30+
inline void Interval::setCenter(float _val)
31+
{
32+
const float extents = (max - min) * 0.5f;
33+
min = _val - extents;
34+
max = _val + extents;
35+
}
36+
37+
inline void Interval::expand(float _val)
38+
{
39+
min = bx::min(min, _val);
40+
max = bx::max(max, _val);
41+
}
42+
1243
inline Vec3 getPointAt(const Ray& _ray, float _t)
1344
{
1445
return mad(_ray.dir, _t, _ray.pos);
1546
}
1647

48+
inline Vec3 getPointAt(const Line& _line, float _t)
49+
{
50+
return mad(_line.dir, _t, _line.pos);
51+
}
52+
53+
inline Vec3 getPointAt(const LineSegment& _line, float _t)
54+
{
55+
return lerp(_line.pos, _line.end, _t);
56+
}
57+
1758
inline bool intersect(const Ray& _ray, const Plane& _plane, Hit* _hit)
1859
{
1960
return intersect(_ray, _plane, false, _hit);
2061
}
2162

63+
inline bool overlap(const Interval& _interval, float _t)
64+
{
65+
return _t > _interval.min
66+
&& _t < _interval.max
67+
;
68+
}
69+
70+
inline bool overlap(const Interval& _intervalA, const Interval& _intervalB)
71+
{
72+
return _intervalA.max > _intervalB.min
73+
&& _intervalB.max > _intervalA.min
74+
;
75+
}
76+
2277
inline bool overlap(const Aabb& _aabb, const Sphere& _sphere)
2378
{
2479
return overlap(_sphere, _aabb);

src/bounds.cpp

+3-62
Original file line numberDiff line numberDiff line change
@@ -856,43 +856,6 @@ namespace bx
856856
calcPlane(_outPlane, _triangle.v0, _triangle.v1, _triangle.v2);
857857
}
858858

859-
struct Interval
860-
{
861-
Interval(float _val)
862-
: start(_val)
863-
, end(_val)
864-
{
865-
}
866-
867-
Interval(float _start, float _end)
868-
: start(_start)
869-
, end(_end)
870-
{
871-
}
872-
873-
void set(float _val)
874-
{
875-
start = _val;
876-
end = _val;
877-
}
878-
879-
void expand(float _val)
880-
{
881-
start = min(_val, start);
882-
end = max(_val, end);
883-
}
884-
885-
float start;
886-
float end;
887-
};
888-
889-
bool overlap(const Interval& _a, const Interval& _b)
890-
{
891-
return _a.end > _b.start
892-
&& _b.end > _a.start
893-
;
894-
}
895-
896859
float projectToAxis(const Vec3& _axis, const Vec3& _point)
897860
{
898861
return dot(_axis, _point);
@@ -913,7 +876,7 @@ namespace bx
913876
Interval projectToAxis(const Vec3& _axis, const Aabb& _aabb)
914877
{
915878
const float extent = abs(projectToAxis(abs(_axis), getExtents(_aabb) ) );
916-
const float center = projectToAxis( _axis , getCenter (_aabb) );
879+
const float center = projectToAxis( _axis , getCenter (_aabb) );
917880
return
918881
{
919882
center - extent,
@@ -1057,17 +1020,6 @@ namespace bx
10571020
return isNearZero(dot(_v, _v) );
10581021
}
10591022

1060-
struct Line
1061-
{
1062-
Vec3 pos = init::None;
1063-
Vec3 dir = init::None;
1064-
};
1065-
1066-
inline Vec3 getPointAt(const Line& _line, float _t)
1067-
{
1068-
return mad(_line.dir, _t, _line.pos);
1069-
}
1070-
10711023
bool intersect(Line& _outLine, const Plane& _planeA, const Plane& _planeB)
10721024
{
10731025
const Vec3 axb = cross(_planeA.normal, _planeB.normal);
@@ -1107,17 +1059,6 @@ namespace bx
11071059
return result;
11081060
}
11091061

1110-
struct LineSegment
1111-
{
1112-
Vec3 pos;
1113-
Vec3 end;
1114-
};
1115-
1116-
inline Vec3 getPointAt(const LineSegment& _line, float _t)
1117-
{
1118-
return lerp(_line.pos, _line.end, _t);
1119-
}
1120-
11211062
bool intersect(float& _outTa, float& _outTb, const LineSegment& _a, const LineSegment& _b)
11221063
{
11231064
// Reference(s):
@@ -1380,7 +1321,7 @@ namespace bx
13801321
}
13811322

13821323
static void calcObbVertices(
1383-
Vec3* _outVertices
1324+
Vec3* _outVertices
13841325
, const Vec3& _axisX
13851326
, const Vec3& _axisY
13861327
, const Vec3& _axisZ
@@ -1485,7 +1426,7 @@ namespace bx
14851426
const Vec3 bd = sub(_capsuleB.end, _capsuleB.pos);
14861427

14871428
return overlap(
1488-
Sphere{mad(ad, ta, _capsuleA.pos), _capsuleA.radius}
1429+
Sphere{mad(ad, ta, _capsuleA.pos), _capsuleA.radius}
14891430
, Sphere{mad(bd, tb, _capsuleB.pos), _capsuleB.radius}
14901431
);
14911432
}

0 commit comments

Comments
 (0)