Add Plane::ProjectToNegativeHalf ProjectToPositiveHalf

This commit is contained in:
2024-07-10 14:21:34 -04:00
parent c5e5958066
commit c24a352350
2 changed files with 34 additions and 5 deletions

View File

@@ -8,14 +8,17 @@ namespace J3ML::Geometry
{
using J3ML::LinearAlgebra::Vector3;
class Plane : public Shape
class Plane
{
public:
Vector3 Position;
Vector3 Normal;
float distance = 0.f;
public:
Plane() : Shape() {}
Plane() {}
/// Constructs a plane by directly specifying the normal and distance parameters.
Plane(const Vector3& normal, float d);
Plane(const Vector3& v1, const Vector3 &v2, const Vector3& v3);
Plane(const Vector3& pos, const Vector3& norm);
Plane(const Line &line, const Vector3 &normal);
@@ -82,5 +85,22 @@ namespace J3ML::Geometry
LineSegment Project(const LineSegment &segment);
Vector3 Project(const Vector3 &point) const;
/// Projects the given point to the negative half-space of this plane.
Vector3 ProjectToNegativeHalf(const Vector3& point) const;
/// Projects the given point to the positive half-space of this plane.
Vector3 ProjectToPositiveHalf(const Vector3& point) const;
bool IsParallel(const Plane& plane, float epsilon = 1e-3f) const {
return Normal.Equals(plane.Normal, epsilon);
}
/// Returns true if the two planes are equal, and their normals are oriented to the same direction.
bool Equals(const Plane& other, float epsilon = 1e-3f) const {
return IsParallel(other, epsilon) && Math::EqualAbs(distance, other.distance, epsilon);
}
};
}

View File

@@ -1,5 +1,5 @@
#include <J3ML/Geometry/Plane.h>
#include <J3ML/Geometry/AABB.h>
#include <J3ML/Geometry/AABB.hpp>
#include <J3ML/Geometry/OBB.h>
#include <J3ML/Geometry/Capsule.h>
#include <J3ML/Geometry/Polygon.h>
@@ -130,12 +130,13 @@ namespace J3ML::Geometry
#endif
}
Plane::Plane(const Vector3 &normal, float d): Normal(normal), distance(d) {}
Plane::Plane(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3) {
Set(v1, v2, v3);
}
Plane::Plane(const Vector3 &pos, const Vector3 &norm)
: Shape(), Position(pos), Normal(norm) {}
Plane::Plane(const Vector3 &pos, const Vector3 &norm) : Position(pos), Normal(norm) {}
bool Plane::Intersects(J3ML::Geometry::Ray ray, float *dist) const {
float t;
@@ -231,6 +232,14 @@ namespace J3ML::Geometry
return projected;
}
Vector3 Plane::ProjectToNegativeHalf(const Vector3 &point) const {
return point - Math::Max(0.f, (Normal.Dot(point) - distance)) * Normal;
}
Vector3 Plane::ProjectToPositiveHalf(const Vector3 &point) const {
return point - Math::Min(0.f, (Vector3::Dot(Normal, point) - distance)) * Normal;
}
LineSegment Plane::Project(const LineSegment &lineSegment) {
return LineSegment(Project(lineSegment.A), Project(lineSegment.B));
}