1 #pragma once 2 3 /// \file Plane.h 4 /// \brief Infinite plane definited by its general equation. 5 /// \author Pavel Sevecek (sevecek at sirrah.troja.mff.cuni.cz) 6 /// \date 2016-2021 7 8 #include "objects/geometry/Triangle.h" 9 10 NAMESPACE_SPH_BEGIN 11 12 /// \brief Represents a plane in 3D space. 13 class Plane { 14 private: 15 Vector v; 16 17 public: 18 /// \brief Creates the plane using its normal and a point lying in the plane. 19 /// 20 /// The normal has to be normalized. Plane(const Vector & p,const Vector & n)21 Plane(const Vector& p, const Vector& n) { 22 SPH_ASSERT(almostEqual(getLength(n), 1._f), n, getLength(n)); 23 v = n; 24 v[3] = -dot(p, n); 25 } 26 27 /// \brief Creates the plane from three vertices of a triangle Plane(const Triangle & tri)28 Plane(const Triangle& tri) 29 : Plane(tri[0], getNormalized(tri.normal())) {} 30 31 /// \brief Returns the normal of the plane. normal()32 const Vector& normal() const { 33 return v; 34 } 35 36 /// \brief Returns the signed distance of the point from the plane. signedDistance(const Vector & p)37 Float signedDistance(const Vector& p) const { 38 return dot(v, p) + v[3]; 39 } 40 41 /// \brief Checks if the point lies above the plane. above(const Vector & p)42 bool above(const Vector& p) const { 43 return this->signedDistance(p) > 0._f; 44 } 45 46 /// \brief Returns the projection of the point to the plane. project(const Vector & p)47 Vector project(const Vector& p) const { 48 return p - v * dot(v, p); 49 } 50 51 /// \brief Finds the intersection with a line, given by its origin and direction. intersection(const Vector & origin,const Vector & dir)52 Vector intersection(const Vector& origin, const Vector& dir) const { 53 SPH_ASSERT(dot(dir, normal()) != 0._f); 54 const Float t = -signedDistance(origin) / dot(dir, this->normal()); 55 return origin + t * dir; 56 } 57 }; 58 59 NAMESPACE_SPH_END 60