Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Plane.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * A Plane in R3 and several utility methods.
20  */
21 
22 // Note that the format used for the plane equation is
23 // Ax + By + Cz + D = 0, where <A,B,C> is the normal vector.
24 
25 #include "precompiled.h"
26 
27 #include "Plane.h"
28 #include "MathUtil.h"
29 
31 {
32  m_Dist = 0.0f;
33 }
34 
35 //sets the plane equation from 3 points on that plane
36 void CPlane::Set (const CVector3D &p1, const CVector3D &p2, const CVector3D &p3)
37 {
38  CVector3D D1, D2;
39  CVector3D Norm;
40 
41  //calculate two vectors on the surface of the plane
42  D1 = p2-p1;
43  D2 = p3-p1;
44 
45  //cross multiply gives normal
46  Norm = D2.Cross(D1);
47 
48  Set (Norm, p1);
49 }
50 
51 //sets the plane equation from a normal and a point on
52 //that plane
53 void CPlane::Set (const CVector3D &norm, const CVector3D &point)
54 {
55  m_Norm = norm;
56 
57  m_Dist = - (norm.X * point.X +
58  norm.Y * point.Y +
59  norm.Z * point.Z);
60 
61 // Normalize ();
62 }
63 
64 //normalizes the plane equation
66 {
67  float Scale;
68 
69  Scale = 1.0f/m_Norm.Length ();
70 
71  m_Norm.X *= Scale;
72  m_Norm.Y *= Scale;
73  m_Norm.Z *= Scale;
74  m_Dist *= Scale;
75 }
76 
77 //returns the side of the plane on which this point
78 //lies.
80 {
81  float Dist;
82 
83  Dist = m_Norm.X * point.X +
84  m_Norm.Y * point.Y +
85  m_Norm.Z * point.Z +
86  m_Dist;
87 
88  const float EPS = 0.001f;
89 
90  if (Dist > EPS)
91  return PS_FRONT;
92  else if (Dist < -EPS)
93  return PS_BACK;
94 
95  return PS_ON;
96 }
97 
98 //solves the plane equation for a particular point
99 float CPlane::DistanceToPlane (const CVector3D &point) const
100 {
101  float Dist;
102 
103  Dist = m_Norm.X * point.X +
104  m_Norm.Y * point.Y +
105  m_Norm.Z * point.Z +
106  m_Dist;
107 
108  return Dist;
109 }
110 
111 //calculates the intersection point of a line with this
112 //plane. Returns false if there is no intersection
113 bool CPlane::FindLineSegIntersection (const CVector3D &start, const CVector3D &end, CVector3D *intsect)
114 {
115  float dist1 = DistanceToPlane( start );
116  float dist2 = DistanceToPlane( end );
117 
118  if( (dist1 < 0 && dist2 < 0) || (dist1 >= 0 && dist2 >= 0) )
119  return false;
120 
121  float t = (-dist1) / (dist2-dist1);
122  *intsect = Interpolate( start, end, t );
123 
124  return true;
125 }
126 
127 bool CPlane::FindRayIntersection (const CVector3D &start, const CVector3D &direction, CVector3D *intsect)
128 {
129  float dot = m_Norm.Dot (direction);
130  if (dot == 0.0f)
131  return false;
132 
133  *intsect = start - (direction * (DistanceToPlane (start)/dot));
134  return true;
135 }
PLANESIDE
Definition: Plane.h:31
T Interpolate(const T &a, const T &b, float l)
Definition: MathUtil.h:26
bool FindRayIntersection(const CVector3D &start, const CVector3D &direction, CVector3D *intsect)
Definition: Plane.cpp:127
float Dot(const CVector3D &vector) const
Definition: Vector3D.cpp:48
CVector3D Cross(const CVector3D &vector) const
Definition: Vector3D.cpp:55
void Set(const CVector3D &p1, const CVector3D &p2, const CVector3D &p3)
Definition: Plane.cpp:36
Definition: Plane.h:35
void Normalize()
Definition: Plane.cpp:65
CPlane()
Definition: Plane.cpp:30
float X
Definition: Vector3D.h:31
float DistanceToPlane(const CVector3D &point) const
Definition: Plane.cpp:99
float Length() const
Definition: Vector3D.cpp:72
float Y
Definition: Vector3D.h:31
PLANESIDE ClassifyPoint(const CVector3D &point) const
Definition: Plane.cpp:79
Definition: Plane.h:33
bool FindLineSegIntersection(const CVector3D &start, const CVector3D &end, CVector3D *intsect)
Definition: Plane.cpp:113
CVector3D m_Norm
Definition: Plane.h:67
float Z
Definition: Vector3D.h:31
Definition: Plane.h:34
float m_Dist
Definition: Plane.h:68