plane.h
Go to the documentation of this file.00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #if !defined(INCLUDED_MATH_PLANE_H)
00028 #define INCLUDED_MATH_PLANE_H
00029
00030 #include "math/matrix.h"
00031
00033 class Plane3
00034 {
00035 public:
00036 double a, b, c, d;
00037
00038 Plane3 ()
00039 {
00040 }
00041 Plane3 (double _a, double _b, double _c, double _d) :
00042 a(_a), b(_b), c(_c), d(_d)
00043 {
00044 }
00045 template<typename Element>
00046 Plane3 (const BasicVector3<Element>& normal, double dist) :
00047 a(normal.x()), b(normal.y()), c(normal.z()), d(dist)
00048 {
00049 }
00050
00051 BasicVector3<double>& normal ()
00052 {
00053 return reinterpret_cast<BasicVector3<double>&> (*this);
00054 }
00055 const BasicVector3<double>& normal () const
00056 {
00057 return reinterpret_cast<const BasicVector3<double>&> (*this);
00058 }
00059 double& dist ()
00060 {
00061 return d;
00062 }
00063 const double& dist () const
00064 {
00065 return d;
00066 }
00067 };
00068
00069 inline Plane3 plane3_normalised (const Plane3& plane)
00070 {
00071 double rmagnitude = 1.0 / sqrt(plane.a * plane.a + plane.b * plane.b + plane.c * plane.c);
00072 return Plane3(plane.a * rmagnitude, plane.b * rmagnitude, plane.c * rmagnitude, plane.d * rmagnitude);
00073 }
00074
00075 inline Plane3 plane3_translated (const Plane3& plane, const Vector3& translation)
00076 {
00077 Plane3 transformed;
00078 transformed.a = plane.a;
00079 transformed.b = plane.b;
00080 transformed.c = plane.c;
00081 transformed.d = -((-plane.d * transformed.a + translation.x()) * transformed.a + (-plane.d * transformed.b
00082 + translation.y()) * transformed.b + (-plane.d * transformed.c + translation.z()) * transformed.c);
00083 return transformed;
00084 }
00085
00086 inline Plane3 plane3_transformed (const Plane3& plane, const Matrix4& transform)
00087 {
00088 Plane3 transformed;
00089 transformed.a = transform[0] * plane.a + transform[4] * plane.b + transform[8] * plane.c;
00090 transformed.b = transform[1] * plane.a + transform[5] * plane.b + transform[9] * plane.c;
00091 transformed.c = transform[2] * plane.a + transform[6] * plane.b + transform[10] * plane.c;
00092 transformed.d = -((-plane.d * transformed.a + transform[12]) * transformed.a + (-plane.d * transformed.b
00093 + transform[13]) * transformed.b + (-plane.d * transformed.c + transform[14]) * transformed.c);
00094 return transformed;
00095 }
00096
00097 inline Plane3 plane3_inverse_transformed (const Plane3& plane, const Matrix4& transform)
00098 {
00099 return Plane3(transform[0] * plane.a + transform[1] * plane.b + transform[2] * plane.c + transform[3] * plane.d,
00100 transform[4] * plane.a + transform[5] * plane.b + transform[6] * plane.c + transform[7] * plane.d,
00101 transform[8] * plane.a + transform[9] * plane.b + transform[10] * plane.c + transform[11] * plane.d,
00102 transform[12] * plane.a + transform[13] * plane.b + transform[14] * plane.c + transform[15] * plane.d);
00103 }
00104
00105 inline Plane3 plane3_flipped (const Plane3& plane)
00106 {
00107 return Plane3(-plane.normal(), -plane.dist());
00108 }
00109
00110 const double c_PLANE_NORMAL_EPSILON = 0.0001f;
00111 const double c_PLANE_DIST_EPSILON = 0.02;
00112
00113 inline bool plane3_equal (const Plane3& self, const Plane3& other)
00114 {
00115 return vector3_equal_epsilon(self.normal(), other.normal(), c_PLANE_NORMAL_EPSILON) && float_equal_epsilon(
00116 self.dist(), other.dist(), c_PLANE_DIST_EPSILON);
00117 }
00118
00119 inline bool plane3_opposing (const Plane3& self, const Plane3& other)
00120 {
00121 return plane3_equal(self, plane3_flipped(other));
00122 }
00123
00124 inline bool plane3_valid (const Plane3& self)
00125 {
00126 return float_equal_epsilon(self.normal().dot(self.normal()), 1.0, 0.01);
00127 }
00128
00129 template<typename Element>
00130 inline Plane3 plane3_for_points (const BasicVector3<Element>& p0, const BasicVector3<Element>& p1, const BasicVector3<
00131 Element>& p2)
00132 {
00133 Plane3 self;
00134 self.normal() = (p1 - p0).crossProduct(p2 - p0).getNormalised();
00135 self.dist() = p0.dot(self.normal());
00136 return self;
00137 }
00138
00139 template<typename Element>
00140 inline Plane3 plane3_for_points (const BasicVector3<Element> planepts[3])
00141 {
00142 return plane3_for_points(planepts[2], planepts[1], planepts[0]);
00143 }
00144
00145 #endif