#ifndef _Plane3_H__ #define _Plane3_H__ #include "Vector3.h" /** 3D Plane */ class Plane3 { public: Vector3 mNormal; float mD; Plane3() {} Plane3(const Vector3 &a, const Vector3 &b, const Vector3 &c ) { Vector3 v1=a-b, v2=c-b; mNormal = Normalize(CrossProd(v2,v1)); mD = -DotProd(b, mNormal); } Plane3(const Vector3 &normal, const Vector3 &point ):mNormal(normal) { mD = -DotProd(normal, point); } void ReverseOrientation() { mNormal *= -1; mD *= -1; } float Distance(const Vector3 &v) const { return DotProd(v, mNormal) + mD; } int Side(const Vector3 &v, const float threshold = 1e-6) const { return signum(Distance(v), threshold); } /** Finds intersection of line segment between points a and b with plane. @param a start point @param b end point @param t if not NULL, returns parameter value of intersections @param coplanar if not NULL, returns true if plane and line segments are coplanar. */ Vector3 FindIntersection(const Vector3 &a, const Vector3 &b, float *t = NULL, bool *coplanar = NULL ) const { const Vector3 v = b - a; // line from A to B float dv = DotProd(v, mNormal); if (signum(dv) == 0) { if (coplanar) (*coplanar) = true; if (t) (*t) = 0; return a; } if (coplanar) (*coplanar) = false; float u = - Distance(a) / dv; // TODO: could be done more efficiently if (t) (*t) = u; return a + u * v; } /** Finds value of intersection parameter t on line segment from a to b. @returns -1 if coplanar, else parameter t */ float FindT(const Vector3 &a, const Vector3 &b) const { const Vector3 v = b - a; // line from A to B const float dv = DotProd(v, mNormal); // does not intersect if (signum(dv) == 0) return -1; return - Distance(a) / dv; // TODO: could be done more efficiently } friend bool PlaneIntersection(const Plane3 &a, const Plane3 &b, const Plane3 &c, Vector3 &result); friend ostream &operator<<(ostream &s, const Plane3 p) { s<