Commit | Line | Data |
---|---|---|
0e44e7d7 JM |
1 | #include "Plane3D.h" |
2 | ||
3 | Plane3D::Plane3D(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3) | |
4 | { | |
5 | // get the normal of the plane | |
6 | Vector3 ab = v1.sub(v2); | |
7 | Vector3 ac = v1.sub(v3); | |
8 | ||
9 | Vector3 cp = ab.cross(ac); | |
10 | normal = cp.unit(); | |
11 | ||
12 | // ax+by+cz+d=0 | |
13 | // solve for d | |
14 | Vector3 dv = normal.mul(v1); | |
15 | d = -dv[0] - dv[1] - dv[2]; | |
16 | } | |
17 | ||
230079d4 JM |
18 | typedef union { float f; uint32_t u; } conv_t; |
19 | // ctor used to restore a saved plane | |
20 | Plane3D::Plane3D(uint32_t a, uint32_t b, uint32_t c, uint32_t d) | |
21 | { | |
22 | conv_t ca, cb, cc, cd; | |
23 | ca.u= a; cb.u= b; cc.u= c; cd.u= d; | |
24 | this->normal.set(ca.f, cb.f, cc.f); | |
25 | this->d= cd.f; | |
26 | } | |
27 | ||
28 | void Plane3D::encode(uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d) | |
29 | { | |
30 | conv_t ca, cb, cc, cd; | |
31 | ca.f= this->normal[0]; | |
32 | cb.f= this->normal[1]; | |
33 | cc.f= this->normal[2]; | |
34 | cd.f= this->d; | |
35 | a= ca.u; b= cb.u; c= cc.u; d= cd.u; | |
36 | } | |
37 | ||
0e44e7d7 JM |
38 | // solve for z given x and y |
39 | // z= (-ax - by - d)/c | |
40 | float Plane3D::getz(float x, float y) | |
41 | { | |
42 | return ((-normal[0] * x) - (normal[1] * y) - d) / normal[2]; | |
43 | } | |
44 | ||
45 | Vector3 Plane3D::getNormal() const | |
46 | { | |
47 | return normal; | |
48 | } |