]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/math/plane.h
ok
[xonotic/netradiant.git] / libs / math / plane.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined(INCLUDED_MATH_PLANE_H)
23 #define INCLUDED_MATH_PLANE_H
24
25 /// \file
26 /// \brief Plane data types and related operations.
27
28 #include "math/matrix.h"
29
30 /// \brief A plane equation stored in double-precision floating-point.
31 class Plane3
32 {
33 public:
34   double a, b, c, d;
35
36   Plane3()
37   {
38   }
39   Plane3(double _a, double _b, double _c, double _d)
40     : a(_a), b(_b), c(_c), d(_d)
41   {
42   }
43   template<typename Element>
44   Plane3(const BasicVector3<Element>& normal, double dist)
45     : a(normal.x()), b(normal.y()), c(normal.z()), d(dist)
46   {
47   }
48
49   BasicVector3<double>& normal()
50   {
51     return reinterpret_cast<BasicVector3<double>&>(*this);
52   }
53   const BasicVector3<double>& normal() const
54   {
55     return reinterpret_cast<const BasicVector3<double>&>(*this);
56   }
57   double& dist()
58   {
59     return d;
60   }
61   const double& dist() const
62   {
63     return d;
64   }
65 };
66
67 inline Plane3 plane3_normalised(const Plane3& plane)
68 {
69   double rmagnitude = 1.0 / sqrt(plane.a * plane.a + plane.b * plane.b + plane.c * plane.c);
70   return Plane3(
71     plane.a * rmagnitude,
72     plane.b * rmagnitude,
73     plane.c * rmagnitude,
74     plane.d * rmagnitude
75   );
76 }
77
78 inline Plane3 plane3_translated(const Plane3& plane, const Vector3& translation)
79 {
80   Plane3 transformed;
81   transformed.a = plane.a;
82   transformed.b = plane.b;
83   transformed.c = plane.c;
84   transformed.d = -((-plane.d * transformed.a + translation.x()) * transformed.a + 
85               (-plane.d * transformed.b + translation.y()) * transformed.b + 
86               (-plane.d * transformed.c + translation.z()) * transformed.c);
87   return transformed;
88 }
89
90 inline Plane3 plane3_transformed(const Plane3& plane, const Matrix4& transform)
91 {
92   Plane3 transformed;
93   transformed.a = transform[0] * plane.a + transform[4] * plane.b + transform[8] * plane.c;
94   transformed.b = transform[1] * plane.a + transform[5] * plane.b + transform[9] * plane.c;
95   transformed.c = transform[2] * plane.a + transform[6] * plane.b + transform[10] * plane.c;
96   transformed.d = -((-plane.d * transformed.a + transform[12]) * transformed.a + 
97               (-plane.d * transformed.b + transform[13]) * transformed.b + 
98               (-plane.d * transformed.c + transform[14]) * transformed.c);
99   return transformed;
100 }
101
102 inline Plane3 plane3_inverse_transformed(const Plane3& plane, const Matrix4& transform)
103 {
104   return Plane3
105   (
106     transform[ 0] * plane.a + transform[ 1] * plane.b + transform[ 2] * plane.c + transform[ 3] * plane.d,
107     transform[ 4] * plane.a + transform[ 5] * plane.b + transform[ 6] * plane.c + transform[ 7] * plane.d,
108     transform[ 8] * plane.a + transform[ 9] * plane.b + transform[10] * plane.c + transform[11] * plane.d,
109     transform[12] * plane.a + transform[13] * plane.b + transform[14] * plane.c + transform[15] * plane.d
110   );
111 }
112
113 inline Plane3 plane3_flipped(const Plane3& plane)
114 {
115   return Plane3(vector3_negated(plane.normal()), -plane.dist());
116 }
117
118 const double c_PLANE_NORMAL_EPSILON = 0.0001f;
119 const double c_PLANE_DIST_EPSILON = 0.02;
120
121 inline bool plane3_equal(const Plane3& self, const Plane3& other)
122 {
123   return vector3_equal_epsilon(self.normal(), other.normal(), c_PLANE_NORMAL_EPSILON)
124           && float_equal_epsilon(self.dist(), other.dist(), c_PLANE_DIST_EPSILON);
125 }
126
127 inline bool plane3_opposing(const Plane3& self, const Plane3& other)
128 {
129   return plane3_equal(self, plane3_flipped(other));
130 }
131
132 inline bool plane3_valid(const Plane3& self)
133 {
134   return float_equal_epsilon(vector3_dot(self.normal(), self.normal()), 1.0, 0.01);
135 }
136
137 template<typename Element>
138 inline Plane3 plane3_for_points(const BasicVector3<Element>& p0, const BasicVector3<Element>& p1, const BasicVector3<Element>& p2)
139 {
140         Plane3 self;
141   self.normal() = vector3_normalised(vector3_cross(vector3_subtracted(p1, p0), vector3_subtracted(p2, p0)));
142         self.dist() = vector3_dot(p0, self.normal());
143         return self;
144 }
145
146 template<typename Element>
147 inline Plane3 plane3_for_points(const BasicVector3<Element> planepts[3])
148 {
149         return plane3_for_points(planepts[2], planepts[1], planepts[0]);
150 }
151
152
153 #endif