1 | #include "Plane3.h"
|
---|
2 | #include "Matrix4x4.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | namespace CHCDemoEngine
|
---|
6 | {
|
---|
7 |
|
---|
8 | Plane3::Plane3(const Vector3 &a, const Vector3 &b, const Vector3 &c)
|
---|
9 | {
|
---|
10 | Vector3 v1 = a - b, v2 = c - b;
|
---|
11 | mNormal = Normalize(CrossProd(v2, v1));
|
---|
12 | mD = -DotProd(b, mNormal);
|
---|
13 | }
|
---|
14 |
|
---|
15 |
|
---|
16 | /*Plane3::Plane3(const Vector3 &normal, float dist):
|
---|
17 | mNormal(normal), mD(dist)
|
---|
18 | {
|
---|
19 | }*/
|
---|
20 |
|
---|
21 |
|
---|
22 | Plane3::Plane3(const Vector3 &normal, const Vector3 &point):
|
---|
23 | mNormal(normal)
|
---|
24 | {
|
---|
25 | mD = -DotProd(normal, point);
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | bool PlaneIntersection(const Plane3 &a,
|
---|
30 | const Plane3 &b,
|
---|
31 | const Plane3 &c,
|
---|
32 | Vector3 &result)
|
---|
33 | {
|
---|
34 | Vector3 sx(a.mNormal.x, b.mNormal.x, c.mNormal.x),
|
---|
35 | sy(a.mNormal.y, b.mNormal.y, c.mNormal.y),
|
---|
36 | sz(a.mNormal.z, b.mNormal.z, c.mNormal.z),
|
---|
37 | sd(a.mD, b.mD, c.mD);
|
---|
38 |
|
---|
39 | Matrix4x4 md(a.mNormal, b.mNormal, c.mNormal), mx, my, mz;
|
---|
40 |
|
---|
41 | mx.SetColumns(sd, sy, sz);
|
---|
42 | my.SetColumns(sx, sd, sz);
|
---|
43 | mz.SetColumns(sx, sy, sd);
|
---|
44 |
|
---|
45 | const float det = md.Det3x3();
|
---|
46 |
|
---|
47 | if (fabs(det) < TRASH)
|
---|
48 | return false;
|
---|
49 |
|
---|
50 | result.SetValue(mx.Det3x3()/det,
|
---|
51 | my.Det3x3()/det,
|
---|
52 | mz.Det3x3()/det);
|
---|
53 |
|
---|
54 | return true;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | bool PlaneIntersection(const Plane3 &p1, const Plane3 &p2)
|
---|
59 | {
|
---|
60 | return
|
---|
61 | p1.mNormal.x != p2.mNormal.x ||
|
---|
62 | p1.mNormal.y != p2.mNormal.y ||
|
---|
63 | p1.mNormal.z != p2.mNormal.z ||
|
---|
64 | p1.mD == p2.mD;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | Vector3 Plane3::FindIntersection(const Vector3 &a,
|
---|
69 | const Vector3 &b,
|
---|
70 | float *t,
|
---|
71 | bool *coplanar) const
|
---|
72 | {
|
---|
73 | // line from A to B
|
---|
74 | const Vector3 v = b - a;
|
---|
75 | float dv = DotProd(v, mNormal);
|
---|
76 |
|
---|
77 | if (signum(dv) == 0)
|
---|
78 | {
|
---|
79 | if (coplanar) (*coplanar) = true;
|
---|
80 |
|
---|
81 | if (t) (*t) = 0;
|
---|
82 |
|
---|
83 | return a;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (coplanar) (*coplanar) = false;
|
---|
87 |
|
---|
88 | float u = - Distance(a) / dv;
|
---|
89 |
|
---|
90 | if (t) (*t) = u;
|
---|
91 |
|
---|
92 | return a + u * v;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | int Plane3::Side(const Vector3 &v, const float threshold) const
|
---|
97 | {
|
---|
98 | return signum(Distance(v), threshold);
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | float Plane3::FindT(const Vector3 &a, const Vector3 &b) const
|
---|
103 | {
|
---|
104 | const Vector3 v = b - a; // line from A to B
|
---|
105 | const float dv = DotProd(v, mNormal);
|
---|
106 |
|
---|
107 | // does not intersect or coincident
|
---|
108 | if (signum(dv) == 0)
|
---|
109 | return 0;
|
---|
110 |
|
---|
111 | return - Distance(a) / dv;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | }
|
---|