Skip to content

Commit 73f579e

Browse files
committed
Added LineSegment::Intersects(Plane) and Plane::ClosestPoint(Ray) and Plane::ClosestPoint(LineSegment).
1 parent 7263cb3 commit 73f579e

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/Geometry/LineSegment.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ bool LineSegment::Intersects(const Plane &plane) const
221221
return d * d2 <= 0.f;
222222
}
223223

224+
bool LineSegment::Intersects(const Plane &plane, float *d) const
225+
{
226+
return plane.Intersects(*this, d);
227+
}
228+
224229
bool LineSegment::Intersects(const Triangle &triangle, float *d, float3 *intersectionPoint) const
225230
{
226231
return triangle.Intersects(*this, d, intersectionPoint);

src/Geometry/LineSegment.h

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class LineSegment
8787

8888
///\todo Output intersection point.
8989
bool Intersects(const Plane &plane) const;
90+
bool Intersects(const Plane &plane, float *d = 0) const;
9091
bool Intersects(const Triangle &triangle, float *d, float3 *intersectionPoint) const;
9192
bool Intersects(const Sphere &s, float3 *intersectionPoint = 0, float3 *intersectionNormal = 0, float *d = 0) const;
9293
bool Intersects(const AABB &aabb, float *dNear = 0, float *dFar = 0) const;

src/Geometry/Plane.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,29 @@ float3 Plane::Project(const float3 &point) const
190190
return projected;
191191
}
192192

193+
float3 Plane::ClosestPoint(const Ray &ray) const
194+
{
195+
///\todo Output parametric d as well.
196+
float d;
197+
if (ray.Intersects(*this, &d))
198+
return ray.GetPoint(d);
199+
else
200+
return Project(ray.pos);
201+
}
202+
203+
float3 Plane::ClosestPoint(const LineSegment &lineSegment) const
204+
{
205+
///\todo Output parametric d as well.
206+
float d;
207+
if (lineSegment.Intersects(*this, &d))
208+
return lineSegment.GetPoint(d);
209+
else
210+
if (Distance(lineSegment.a) < Distance(lineSegment.b))
211+
return Project(lineSegment.a);
212+
else
213+
return Project(lineSegment.b);
214+
}
215+
193216
float3 Plane::ObliqueProject(const float3 &point, const float3 &obliqueProjectionDir) const
194217
{
195218
assume(false && "Not implemented!"); ///\todo

src/Geometry/Plane.h

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ class Plane
115115

116116
/// Returns the closest point on this plane to the given point. This is an alias to Plane::Project(const float3 &point).
117117
float3 ClosestPoint(const float3 &point) const { return Project(point); }
118+
/// Returns the closest point on this plane to the given object.
119+
float3 ClosestPoint(const Ray &ray) const;
120+
float3 ClosestPoint(const LineSegment &lineSegment) const;
118121

119122
/// Projects the given point onto this plane in the given oblique projection direction.
120123
float3 ObliqueProject(const float3 &point, const float3 &obliqueProjectionDir) const;

0 commit comments

Comments
 (0)