-
Notifications
You must be signed in to change notification settings - Fork 8
/
m_plane.h
88 lines (69 loc) · 1.79 KB
/
m_plane.h
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
#ifndef M_PLANE_HDR
#define M_PLANE_HDR
#include "m_vec.h"
namespace m {
struct mat4;
struct bbox;
struct plane {
enum point {
kBack,
kOn,
kFront
};
constexpr plane();
plane(const m::vec3 &p1, const m::vec3 &p2, const m::vec3 &p3);
plane(const m::vec3 &pp, const m::vec3 &nn);
plane(const m::vec3 &pp, float distance);
bool intersect(float &f, const m::vec3 &p, const m::vec3 &v) const;
float distance(const m::vec3 &p) const;
point classify(const m::vec3 &p, float epsilon = kEpsilon) const;
vec3 n;
float d;
};
inline constexpr plane::plane()
: d(0.0f)
{
}
inline plane::plane(const m::vec3 &p1, const m::vec3 &p2, const m::vec3 &p3)
: n(((p2 - p1) ^ (p3 - p1)).normalized())
, d(-n * p1)
{
}
inline plane::plane(const m::vec3 &point, const m::vec3 &normal)
: n(normal.normalized())
, d(-n * point)
{
}
inline plane::plane(const m::vec3 &point, float distance)
: n(point)
, d(distance)
{
const float magnitude = 1.0f / n.abs();
n *= magnitude;
d *= magnitude;
}
inline bool plane::intersect(float &f, const m::vec3 &p, const m::vec3 &v) const {
const float q = n * v;
// Plane and line are parallel?
if (m::abs(q) < kEpsilon)
return false;
f = -(n * p + d) / q;
return true;
}
inline float plane::distance(const m::vec3 &p) const {
return p * n + d;
}
inline plane::point plane::classify(const m::vec3 &p, float epsilon) const {
const float dist = distance(p);
return dist > epsilon ? kFront : dist < -epsilon ? kBack : kOn;
}
struct frustum {
void update(const m::mat4 &wvp);
bool testSphere(const m::vec3 &position, float radius);
bool testBox(const m::bbox& box);
bool testPoint(const m::vec3 &point);
private:
plane m_planes[6];
};
}
#endif