123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- #pragma once
- #include "irrMath.h"
- #include "vector3d.h"
- namespace irr
- {
- namespace core
- {
- enum EIntersectionRelation3D
- {
- ISREL3D_FRONT = 0,
- ISREL3D_BACK,
- ISREL3D_PLANAR,
- ISREL3D_SPANNING,
- ISREL3D_CLIPPED
- };
- template <class T>
- class plane3d
- {
- public:
-
- plane3d() :
- Normal(0, 1, 0) { recalculateD(vector3d<T>(0, 0, 0)); }
- plane3d(const vector3d<T> &MPoint, const vector3d<T> &Normal) :
- Normal(Normal) { recalculateD(MPoint); }
- plane3d(T px, T py, T pz, T nx, T ny, T nz) :
- Normal(nx, ny, nz) { recalculateD(vector3d<T>(px, py, pz)); }
- plane3d(const vector3d<T> &point1, const vector3d<T> &point2, const vector3d<T> &point3)
- {
- setPlane(point1, point2, point3);
- }
- plane3d(const vector3d<T> &normal, const T d) :
- Normal(normal), D(d) {}
-
- inline bool operator==(const plane3d<T> &other) const { return (equals(D, other.D) && Normal == other.Normal); }
- inline bool operator!=(const plane3d<T> &other) const { return !(*this == other); }
-
- void setPlane(const vector3d<T> &point, const vector3d<T> &nvector)
- {
- Normal = nvector;
- recalculateD(point);
- }
- void setPlane(const vector3d<T> &nvect, T d)
- {
- Normal = nvect;
- D = d;
- }
- void setPlane(const vector3d<T> &point1, const vector3d<T> &point2, const vector3d<T> &point3)
- {
-
- Normal = (point2 - point1).crossProduct(point3 - point1);
- Normal.normalize();
- recalculateD(point1);
- }
-
-
- bool getIntersectionWithLine(const vector3d<T> &linePoint,
- const vector3d<T> &lineVect,
- vector3d<T> &outIntersection) const
- {
- T t2 = Normal.dotProduct(lineVect);
- if (t2 == 0)
- return false;
- T t = -(Normal.dotProduct(linePoint) + D) / t2;
- outIntersection = linePoint + (lineVect * t);
- return true;
- }
-
-
- f32 getKnownIntersectionWithLine(const vector3d<T> &linePoint1,
- const vector3d<T> &linePoint2) const
- {
- vector3d<T> vect = linePoint2 - linePoint1;
- T t2 = (f32)Normal.dotProduct(vect);
- return (f32) - ((Normal.dotProduct(linePoint1) + D) / t2);
- }
-
-
- bool getIntersectionWithLimitedLine(
- const vector3d<T> &linePoint1,
- const vector3d<T> &linePoint2,
- vector3d<T> &outIntersection) const
- {
- return (getIntersectionWithLine(linePoint1, linePoint2 - linePoint1, outIntersection) &&
- outIntersection.isBetweenPoints(linePoint1, linePoint2));
- }
-
-
- EIntersectionRelation3D classifyPointRelation(const vector3d<T> &point) const
- {
- const T d = Normal.dotProduct(point) + D;
- if (d < -ROUNDING_ERROR_f32)
- return ISREL3D_BACK;
- if (d > ROUNDING_ERROR_f32)
- return ISREL3D_FRONT;
- return ISREL3D_PLANAR;
- }
-
- void recalculateD(const vector3d<T> &MPoint)
- {
- D = -MPoint.dotProduct(Normal);
- }
-
- vector3d<T> getMemberPoint() const
- {
- return Normal * -D;
- }
-
-
- bool existsIntersection(const plane3d<T> &other) const
- {
- vector3d<T> cross = other.Normal.crossProduct(Normal);
- return cross.getLength() > core::ROUNDING_ERROR_f32;
- }
-
-
- bool getIntersectionWithPlane(const plane3d<T> &other,
- vector3d<T> &outLinePoint,
- vector3d<T> &outLineVect) const
- {
- const T fn00 = Normal.getLength();
- const T fn01 = Normal.dotProduct(other.Normal);
- const T fn11 = other.Normal.getLength();
- const f64 det = fn00 * fn11 - fn01 * fn01;
- if (fabs(det) < ROUNDING_ERROR_f64)
- return false;
- const f64 invdet = 1.0 / det;
- const f64 fc0 = (fn11 * -D + fn01 * other.D) * invdet;
- const f64 fc1 = (fn00 * -other.D + fn01 * D) * invdet;
- outLineVect = Normal.crossProduct(other.Normal);
- outLinePoint = Normal * (T)fc0 + other.Normal * (T)fc1;
- return true;
- }
-
- bool getIntersectionWithPlanes(const plane3d<T> &o1,
- const plane3d<T> &o2, vector3d<T> &outPoint) const
- {
- vector3d<T> linePoint, lineVect;
- if (getIntersectionWithPlane(o1, linePoint, lineVect))
- return o2.getIntersectionWithLine(linePoint, lineVect, outPoint);
- return false;
- }
-
-
- bool isFrontFacing(const vector3d<T> &lookDirection) const
- {
- const f32 d = Normal.dotProduct(lookDirection);
- return F32_LOWER_EQUAL_0(d);
- }
-
-
- T getDistanceTo(const vector3d<T> &point) const
- {
- return point.dotProduct(Normal) + D;
- }
-
- vector3d<T> Normal;
-
- T D;
- };
- typedef plane3d<f32> plane3df;
- typedef plane3d<s32> plane3di;
- }
- }
|