// plane.h // // Copyright (C) 2000, Chris Laurel // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. #ifndef _PLANE_H_ #define _PLANE_H_ #include #include template class Plane { public: inline Plane(); inline Plane(const Plane&); inline Plane(const Vector3&, T); inline Plane(const Vector3&, const Point3&); T distanceTo(const Point3&) const; static Point3 intersection(const Plane&, const Plane&, const Plane&); public: Vector3 normal; T d; }; typedef Plane Planef; typedef Plane Planed; template Plane::Plane() : normal(0, 0, 1), d(0) { } template Plane::Plane(const Plane& p) : normal(p.normal), d(p.d) { } template Plane::Plane(const Vector3& _normal, T _d) : normal(_normal), d(_d) { } template Plane::Plane(const Vector3& _normal, const Point3& _point) : normal(_normal) { d = _normal.x * _point.x + _normal.y * _point.y + _normal.z * _point.z; } template T Plane::distanceTo(const Point3& p) const { return normal.x * p.x + normal.y * p.y + normal.z * p.z + d; } template Plane operator*(const Matrix4& m, const Plane& p) { Vector4 v = m * Vector4(p.normal.x, p.normal.y, p.normal.z, p.d); return Plane(Vector3(v.x, v.y, v.z), v.w); } template Plane operator*(const Plane& p, const Matrix4& m) { Vector4 v = Vector4(p.normal.x, p.normal.y, p.normal.z, p.d) * m; return Plane(Vector3(v.x, v.y, v.z), v.w); } template Point3 Plane::intersection(const Plane& p0, const Plane& p1, const Plane& p2) { T d = Matrix3(p0.normal, p1.normal, p2.normal).determinant(); Vector3 v = (p0.d * cross(p1.normal, p2.normal) + p1.d * cross(p2.normal, p0.normal) + p2.d * cross(p0.normal, p1.normal)) * (1.0f / d); return Point3(v.x, v.y, v.z); } #endif // _PLANE_H_