1 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
2 | // PLANE.cpp
|
---|
3 | // function definitions for RGBA color class
|
---|
4 | // You may use this code however you wish, but if you do, please credit me and
|
---|
5 | // provide a link to my website in a readme file or similar
|
---|
6 | // Downloaded from: www.paulsprojects.net
|
---|
7 | // Created: 20th July 2002
|
---|
8 | // Modified: 6th August 2002 - Added "Normalize"
|
---|
9 | // 7th November 2002 - Altered constructor layout
|
---|
10 | // - Corrected "lerp"
|
---|
11 | //////////////////////////////////////////////////////////////////////////////////////////
|
---|
12 |
|
---|
13 | #include "Maths.h"
|
---|
14 |
|
---|
15 | void PLANE::SetFromPoints(const VECTOR3D & p0, const VECTOR3D & p1, const VECTOR3D & p2)
|
---|
16 | {
|
---|
17 | normal=(p1-p0).CrossProduct(p2-p0);
|
---|
18 |
|
---|
19 | normal.Normalize();
|
---|
20 |
|
---|
21 | CalculateIntercept(p0);
|
---|
22 | }
|
---|
23 |
|
---|
24 | void PLANE::Normalize()
|
---|
25 | {
|
---|
26 | float normalLength=normal.GetLength();
|
---|
27 | normal/=normalLength;
|
---|
28 | intercept/=normalLength;
|
---|
29 | }
|
---|
30 |
|
---|
31 | bool PLANE::Intersect3(const PLANE & p2, const PLANE & p3, VECTOR3D & result)//find point of intersection of 3 planes
|
---|
32 | {
|
---|
33 | float denominator=normal.DotProduct((p2.normal).CrossProduct(p3.normal));
|
---|
34 | //scalar triple product of normals
|
---|
35 | if(denominator==0.0f) //if zero
|
---|
36 | return false; //no intersection
|
---|
37 |
|
---|
38 | VECTOR3D temp1, temp2, temp3;
|
---|
39 | temp1=(p2.normal.CrossProduct(p3.normal))*intercept;
|
---|
40 | temp2=(p3.normal.CrossProduct(normal))*p2.intercept;
|
---|
41 | temp3=(normal.CrossProduct(p2.normal))*p3.intercept;
|
---|
42 |
|
---|
43 | result=(temp1+temp2+temp3)/(-denominator);
|
---|
44 |
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 |
|
---|
48 | float PLANE::GetDistance(const VECTOR3D & point) const
|
---|
49 | {
|
---|
50 | return point.x*normal.x + point.y*normal.y + point.z*normal.z + intercept;
|
---|
51 | }
|
---|
52 |
|
---|
53 | int PLANE::ClassifyPoint(const VECTOR3D & point) const
|
---|
54 | {
|
---|
55 | float distance=point.x*normal.x + point.y*normal.y + point.z*normal.z + intercept;
|
---|
56 |
|
---|
57 | if(distance>EPSILON) //==0.0f is too exact, give a bit of room
|
---|
58 | return POINT_IN_FRONT_OF_PLANE;
|
---|
59 |
|
---|
60 | if(distance<-EPSILON)
|
---|
61 | return POINT_BEHIND_PLANE;
|
---|
62 |
|
---|
63 | return POINT_ON_PLANE; //otherwise
|
---|
64 | }
|
---|
65 |
|
---|
66 | PLANE PLANE::lerp(const PLANE & p2, float factor)
|
---|
67 | {
|
---|
68 | PLANE result;
|
---|
69 | result.normal=normal*(1.0f-factor) + p2.normal*factor;
|
---|
70 | result.normal.Normalize();
|
---|
71 |
|
---|
72 | result.intercept=intercept*(1.0f-factor) + p2.intercept*factor;
|
---|
73 |
|
---|
74 | return result;
|
---|
75 | }
|
---|
76 |
|
---|
77 | bool PLANE::operator ==(const PLANE & rhs) const
|
---|
78 | {
|
---|
79 | if(normal==rhs.normal && intercept==rhs.intercept)
|
---|
80 | return true;
|
---|
81 |
|
---|
82 | return false;
|
---|
83 | }
|
---|