Line | |
---|
1 | #ifndef _Plane3_H__ |
---|
2 | #define _Plane3_H__ |
---|
3 | |
---|
4 | #include "Vector3.h" |
---|
5 | |
---|
6 | |
---|
7 | /** 3D Plane */ |
---|
8 | class Plane3 { |
---|
9 | public: |
---|
10 | Vector3 mNormal; |
---|
11 | float mD; |
---|
12 | |
---|
13 | Plane3() {} |
---|
14 | |
---|
15 | Plane3(const Vector3 &a, |
---|
16 | const Vector3 &b, |
---|
17 | const Vector3 &c |
---|
18 | ) { |
---|
19 | Vector3 v1=a-b, v2=c-b; |
---|
20 | mNormal = Normalize(CrossProd(v2,v1)); |
---|
21 | mD = -DotProd(b, mNormal); |
---|
22 | } |
---|
23 | |
---|
24 | Plane3(const Vector3 &normal, |
---|
25 | const Vector3 &point |
---|
26 | ):mNormal(normal) |
---|
27 | { |
---|
28 | mD = -DotProd(normal, point); |
---|
29 | } |
---|
30 | |
---|
31 | float Distance(const Vector3 &v) const { |
---|
32 | return DotProd(v, mNormal) + mD; |
---|
33 | } |
---|
34 | |
---|
35 | int Side(const Vector3 &v, const float threshold = 1e-6) const { |
---|
36 | return signum(Distance(v), threshold); |
---|
37 | } |
---|
38 | |
---|
39 | Vector3 FindIntersection(const Vector3 &a, |
---|
40 | const Vector3 &b, |
---|
41 | float *t = NULL, |
---|
42 | bool *coplanar = NULL |
---|
43 | ) const |
---|
44 | { |
---|
45 | const Vector3 v = b - a; // line from A to B |
---|
46 | float dv = DotProd(mNormal, v); |
---|
47 | |
---|
48 | if (signum(dv) == 0) |
---|
49 | { |
---|
50 | if (coplanar) |
---|
51 | (*coplanar) = true; |
---|
52 | Debug << "signum is zero" << endl; |
---|
53 | return a; |
---|
54 | } |
---|
55 | |
---|
56 | float u = - Distance(a) / dv; // TODO: could be done more efficiently |
---|
57 | //Debug << "t: " << u << ", b - a: " << v << ", norm: " << mNormal << ", dist(a): " << - Distance(a) << ", dv: " << dv << endl; |
---|
58 | if (coplanar) |
---|
59 | (*coplanar) = false; |
---|
60 | |
---|
61 | if (t) |
---|
62 | (*t) = u; |
---|
63 | |
---|
64 | //return a - Distance(a) * b / dv + Distance(a) * a / dv; // NOTE: gives better precision than calclulating a + u * v |
---|
65 | return a + (u * v); |
---|
66 | } |
---|
67 | |
---|
68 | friend bool |
---|
69 | PlaneIntersection(const Plane3 &a, const Plane3 &b, const Plane3 &c, Vector3 &result); |
---|
70 | |
---|
71 | friend ostream &operator<<(ostream &s, const Plane3 p) { |
---|
72 | s<<p.mNormal<<" "<<p.mD; |
---|
73 | return s; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | }; |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.