00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _PLANE_H_
00011 #define _PLANE_H_
00012
00013 #include <celmath/mathlib.h>
00014 #include <celmath/vecmath.h>
00015
00016
00017 template<class T> class Plane
00018 {
00019 public:
00020 inline Plane();
00021 inline Plane(const Plane<T>&);
00022 inline Plane(const Vector3<T>&, T);
00023 inline Plane(const Vector3<T>&, const Point3<T>&);
00024
00025 T distanceTo(const Point3<T>&) const;
00026
00027 static Point3<T> intersection(const Plane<T>&,
00028 const Plane<T>&,
00029 const Plane<T>&);
00030
00031 public:
00032 Vector3<T> normal;
00033 T d;
00034 };
00035
00036
00037 typedef Plane<float> Planef;
00038 typedef Plane<double> Planed;
00039
00040
00041 template<class T> Plane<T>::Plane() : normal(0, 0, 1), d(0)
00042 {
00043 }
00044
00045 template<class T> Plane<T>::Plane(const Plane<T>& p) :
00046 normal(p.normal), d(p.d)
00047 {
00048 }
00049
00050 template<class T> Plane<T>::Plane(const Vector3<T>& _normal, T _d) :
00051 normal(_normal), d(_d)
00052 {
00053 }
00054
00055 template<class T> Plane<T>::Plane(const Vector3<T>& _normal, const Point3<T>& _point) :
00056 normal(_normal)
00057 {
00058 d = _normal.x * _point.x + _normal.y * _point.y + _normal.z * _point.z;
00059 }
00060
00061 template<class T> T Plane<T>::distanceTo(const Point3<T>& p) const
00062 {
00063 return normal.x * p.x + normal.y * p.y + normal.z * p.z + d;
00064 }
00065
00066 template<class T> Plane<T> operator*(const Matrix4<T>& m, const Plane<T>& p)
00067 {
00068 Vector4<T> v = m * Vector4<T>(p.normal.x, p.normal.y, p.normal.z, p.d);
00069 return Plane<T>(Vector3<T>(v.x, v.y, v.z), v.w);
00070 }
00071
00072 template<class T> Plane<T> operator*(const Plane<T>& p, const Matrix4<T>& m)
00073 {
00074 Vector4<T> v = Vector4<T>(p.normal.x, p.normal.y, p.normal.z, p.d) * m;
00075 return Plane<T>(Vector3<T>(v.x, v.y, v.z), v.w);
00076 }
00077
00078 template<class T> Point3<T> Plane<T>::intersection(const Plane<T>& p0,
00079 const Plane<T>& p1,
00080 const Plane<T>& p2)
00081 {
00082 T d = Matrix3<T>(p0.normal, p1.normal, p2.normal).determinant();
00083
00084 Vector3<T> v = (p0.d * cross(p1.normal, p2.normal) +
00085 p1.d * cross(p2.normal, p0.normal) +
00086 p2.d * cross(p0.normal, p1.normal)) * (1.0f / d);
00087 return Point3<T>(v.x, v.y, v.z);
00088 }
00089
00090 #endif // _PLANE_H_