Skip to content

Commit

Permalink
Added Point v Line, LineSegment, Ray, Point funcs. Intersection funct…
Browse files Browse the repository at this point in the history
…ions #24

Added Unit tests.
  • Loading branch information
MStachowicz committed Nov 20, 2023
1 parent ca113d4 commit a9ffc90
Show file tree
Hide file tree
Showing 3 changed files with 368 additions and 83 deletions.
71 changes: 65 additions & 6 deletions source/Geometry/Intersect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,28 @@ namespace Geometry
}
std::optional<Point> get_intersection(const Cone& cone, const Point& point)
{
if (intersecting(cone, point))
return point;
else
return std::nullopt;
if (intersecting(cone, point)) return point;
else return std::nullopt;
}
std::optional<Point> get_intersection(const Cylinder& cylinder, const Point& point)
{
if (intersecting(cylinder, point)) return point;
else return std::nullopt;
if (intersecting(cylinder, point)) return point;
else return std::nullopt;
}
std::optional<Point> get_intersection(const Line& line, const Point& point)
{
if (intersecting(line, point)) return point;
else return std::nullopt;
}
std::optional<Point> get_intersection(const LineSegment& lineSegment, const Point& point)
{
if (intersecting(lineSegment, point)) return point;
else return std::nullopt;
}
std::optional<Point> get_intersection(const Point& point, const Ray& ray)
{
if (intersecting(point, ray)) return point;
else return std::nullopt;
}
std::optional<Geometry::Point> get_intersection(const Line& line, const Triangle& triangle)
{
Expand Down Expand Up @@ -467,6 +480,52 @@ namespace Geometry
else
return false; // Outside the cylinder
}
bool intersecting(const Line& line, const Point& point)
{
// Calculate vectors along the line and to the point
glm::vec3 AB = line.m_point_2 - line.m_point_1;
glm::vec3 AP = point.m_position - line.m_point_1;

// Calculate the cross product of the two vectors (area of parallelogram with AB and AP as sides)
glm::vec3 cross_AB_AP = glm::cross(AB, AP);

// If the cross product is (0, 0, 0), the point is on the line
return cross_AB_AP == glm::vec3(0.f);
}
bool intersecting(const LineSegment& lineSegment, const Point& point)
{
// Calculate vectors along the lineSegment and to the point
glm::vec3 AB = lineSegment.m_end - lineSegment.m_start;
glm::vec3 AP = point.m_position - lineSegment.m_start;

// Calculate the cross product of the two vectors (area of parallelogram with AB and AP as sides)
glm::vec3 cross_AB_AP = glm::cross(AB, AP);

// If the cross product is (0, 0, 0), the point is on the line
if (cross_AB_AP != glm::vec3(0.0f))
return false;

// Check if the point is within the line segment
float dot_AB_AP = glm::dot(AB, AP);
return dot_AB_AP >= 0.0f && dot_AB_AP <= glm::dot(AB, AB);
}
bool intersecting(const Point& point, const Ray& ray)
{
// Calculate vectors along the ray and to the point
glm::vec3 AB = ray.m_direction;
glm::vec3 AP = point.m_position - ray.m_start;

// Calculate the cross product of the two vectors (area of parallelogram with AB and AP as sides)
glm::vec3 cross_AB_AP = glm::cross(AB, AP);

// If the cross product is (0, 0, 0), the point is along the ray direction
if (cross_AB_AP != glm::vec3(0.0f))
return false;

// Check if the point is ahead of or on the ray start
float dot_AB_AP = glm::dot(AB, AP);
return dot_AB_AP >= 0.0f;
}
bool intersecting(const Line& line, const Triangle& triangle)
{
// Below works for a double-sided triangle (both CW or CCW depending on which side it is viewed),
Expand Down
58 changes: 29 additions & 29 deletions source/Geometry/Intersect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ namespace Geometry
inline std::optional<Point> get_intersection(const Line& line_1, const Line& line_2) { LOG_WARN("[INTERSECT] Not implemented Line v Line"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Line& line, const LineSegment& lineSegment) { LOG_WARN("[INTERSECT] Not implemented Line v LineSegment"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Line& line, const Plane& plane) { LOG_WARN("[INTERSECT] Not implemented Line v Plane"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Line& line, const Point& point) { LOG_WARN("[INTERSECT] Not implemented Line v Point"); return std::nullopt; } // #TODO
std::optional<Point> get_intersection(const Line& line, const Point& point); // IMPLEMENTED
inline std::optional<Point> get_intersection(const Line& line, const Quad& quad) { LOG_WARN("[INTERSECT] Not implemented Line v Quad"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Line& line, const Ray& ray) { LOG_WARN("[INTERSECT] Not implemented Line v Ray"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Line& line, const Sphere& sphere) { LOG_WARN("[INTERSECT] Not implemented Line v Sphere"); return std::nullopt; } // #TODO
std::optional<Point> get_intersection(const Line& line, const Triangle& triangle); // IMPLEMENTED
std::optional<Point> get_intersection(const Line& line, const Triangle& triangle); // IMPLEMENTED

// LineSegment functions
//==============================================================================================================================
Expand All @@ -164,7 +164,7 @@ namespace Geometry
inline std::optional<Point> get_intersection(const LineSegment& lineSegment, const Line& line) { return get_intersection(line, lineSegment); }
inline std::optional<Point> get_intersection(const LineSegment& lineSegment_1, const LineSegment& lineSegment_2) { LOG_WARN("[INTERSECT] Not implemented LineSegment v LineSegment"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const LineSegment& lineSegment, const Plane& plane) { LOG_WARN("[INTERSECT] Not implemented LineSegment v Plane"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const LineSegment& lineSegment, const Point& point) { LOG_WARN("[INTERSECT] Not implemented LineSegment v Point"); return std::nullopt; } // #TODO
std::optional<Point> get_intersection(const LineSegment& lineSegment, const Point& point); // IMPLEMENTED
inline std::optional<Point> get_intersection(const LineSegment& lineSegment, const Quad& quad) { LOG_WARN("[INTERSECT] Not implemented LineSegment v Quad"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const LineSegment& lineSegment, const Ray& ray) { LOG_WARN("[INTERSECT] Not implemented LineSegment v Ray"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const LineSegment& lineSegment, const Sphere& sphere) { LOG_WARN("[INTERSECT] Not implemented LineSegment v Sphere"); return std::nullopt; } // #TODO
Expand All @@ -188,18 +188,18 @@ namespace Geometry

// Point functions - Is the point inside or on the surface of the shape
//==============================================================================================================================
inline std::optional<Point> get_intersection(const Point& point, const AABB& AABB) { return get_intersection(AABB, point); }
inline std::optional<Point> get_intersection(const Point& point, const Cone& cone) { return get_intersection(cone, point); }
inline std::optional<Point> get_intersection(const Point& point, const Cuboid& cuboid) { return get_intersection(cuboid, point); }
inline std::optional<Point> get_intersection(const Point& point, const Cylinder& cylinder) { return get_intersection(cylinder, point); }
inline std::optional<Point> get_intersection(const Point& point, const Line& line) { return get_intersection(line, point); }
inline std::optional<Point> get_intersection(const Point& point, const LineSegment& lineSegment) { return get_intersection(lineSegment, point); }
inline std::optional<Point> get_intersection(const Point& point, const Plane& plane) { return get_intersection(plane, point); }
inline std::optional<Point> get_intersection(const Point& point_1, const Point& point_2) { LOG_WARN("[INTERSECT] Not implemented Point v Point"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Point& point, const Quad& quad) { LOG_WARN("[INTERSECT] Not implemented Point v Quad"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Point& point, const Ray& ray) { LOG_WARN("[INTERSECT] Not implemented Point v Ray"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Point& point, const Sphere& sphere) { LOG_WARN("[INTERSECT] Not implemented Point v Sphere"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Point& point, const Triangle& triangle) { LOG_WARN("[INTERSECT] Not implemented Point v Triangle"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Point& point, const AABB& AABB) { return get_intersection(AABB, point); }
inline std::optional<Point> get_intersection(const Point& point, const Cone& cone) { return get_intersection(cone, point); }
inline std::optional<Point> get_intersection(const Point& point, const Cuboid& cuboid) { return get_intersection(cuboid, point); }
inline std::optional<Point> get_intersection(const Point& point, const Cylinder& cylinder) { return get_intersection(cylinder, point); }
inline std::optional<Point> get_intersection(const Point& point, const Line& line) { return get_intersection(line, point); }
inline std::optional<Point> get_intersection(const Point& point, const LineSegment& lineSegment) { return get_intersection(lineSegment, point); }
inline std::optional<Point> get_intersection(const Point& point, const Plane& plane) { return get_intersection(plane, point); }
inline std::optional<Point> get_intersection(const Point& point_1, const Point& point_2) { LOG_WARN("[INTERSECT] Not implemented Point v Point"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Point& point, const Quad& quad) { LOG_WARN("[INTERSECT] Not implemented Point v Quad"); return std::nullopt; } // #TODO
std::optional<Point> get_intersection(const Point& point, const Ray& ray); // IMPLEMENTED
inline std::optional<Point> get_intersection(const Point& point, const Sphere& sphere) { LOG_WARN("[INTERSECT] Not implemented Point v Sphere"); return std::nullopt; } // #TODO
inline std::optional<Point> get_intersection(const Point& point, const Triangle& triangle) { LOG_WARN("[INTERSECT] Not implemented Point v Triangle"); return std::nullopt; } // #TODO

// Quad functions
//==============================================================================================================================
Expand Down Expand Up @@ -339,7 +339,7 @@ namespace Geometry
inline bool intersecting(const Line& line_1, const Line& line_2) { return get_intersection(line_1, line_2).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Line& line, const LineSegment& lineSegment) { return get_intersection(line, lineSegment).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Line& line, const Plane& plane) { return get_intersection(line, plane).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Line& line, const Point& point) { return get_intersection(line, point).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
bool intersecting(const Line& line, const Point& point); // IMPLEMENTED
inline bool intersecting(const Line& line, const Quad& quad) { return get_intersection(line, quad).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Line& line, const Ray& ray) { return get_intersection(line, ray).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Line& line, const Sphere& sphere) { return get_intersection(line, sphere).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
Expand All @@ -354,7 +354,7 @@ namespace Geometry
inline bool intersecting(const LineSegment& lineSegment, const Line& line) { return intersecting(line, lineSegment); }
inline bool intersecting(const LineSegment& lineSegment_1, const LineSegment& lineSegment_2) { return get_intersection(lineSegment_1, lineSegment_2).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const LineSegment& lineSegment, const Plane& plane) { return get_intersection(lineSegment, plane).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const LineSegment& lineSegment, const Point& point) { return get_intersection(lineSegment, point).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
bool intersecting(const LineSegment& lineSegment, const Point& point); // IMPLEMENTED
inline bool intersecting(const LineSegment& lineSegment, const Quad& quad) { return get_intersection(lineSegment, quad).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const LineSegment& lineSegment, const Ray& ray) { return get_intersection(lineSegment, ray).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const LineSegment& lineSegment, const Sphere& sphere) { return get_intersection(lineSegment, sphere).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
Expand All @@ -378,18 +378,18 @@ namespace Geometry

// Point functions
//==============================================================================================================================
inline bool intersecting(const Point& point, const AABB& AABB) { return intersecting(AABB, point); }
inline bool intersecting(const Point& point, const Cone& cone) { return intersecting(cone, point); }
inline bool intersecting(const Point& point, const Cuboid& cuboid) { return intersecting(cuboid, point); }
inline bool intersecting(const Point& point, const Cylinder& cylinder) { return intersecting(cylinder, point); }
inline bool intersecting(const Point& point, const Line& line) { return intersecting(line, point); }
inline bool intersecting(const Point& point, const LineSegment& lineSegment) { return intersecting(lineSegment, point); }
inline bool intersecting(const Point& point, const Plane& plane) { return intersecting(plane, point); }
inline bool intersecting(const Point& point_1, const Point& point_2) { return get_intersection(point_1, point_2).has_value(); }
inline bool intersecting(const Point& point, const Quad& quad) { return get_intersection(point, quad).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Point& point, const Ray& ray) { return get_intersection(point, ray).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Point& point, const Sphere& sphere) { return get_intersection(point, sphere).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Point& point, const Triangle& triangle) { return get_intersection(point, triangle).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Point& point, const AABB& AABB) { return intersecting(AABB, point); }
inline bool intersecting(const Point& point, const Cone& cone) { return intersecting(cone, point); }
inline bool intersecting(const Point& point, const Cuboid& cuboid) { return intersecting(cuboid, point); }
inline bool intersecting(const Point& point, const Cylinder& cylinder) { return intersecting(cylinder, point); }
inline bool intersecting(const Point& point, const Line& line) { return intersecting(line, point); }
inline bool intersecting(const Point& point, const LineSegment& lineSegment) { return intersecting(lineSegment, point); }
inline bool intersecting(const Point& point, const Plane& plane) { return intersecting(plane, point); }
inline bool intersecting(const Point& point_1, const Point& point_2) { return get_intersection(point_1, point_2).has_value(); }
inline bool intersecting(const Point& point, const Quad& quad) { return get_intersection(point, quad).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
bool intersecting(const Point& point, const Ray& ray); // IMPLEMENTED
inline bool intersecting(const Point& point, const Sphere& sphere) { return get_intersection(point, sphere).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO
inline bool intersecting(const Point& point, const Triangle& triangle) { return get_intersection(point, triangle).has_value(); } // Expensive get_intersection call for lack of bespoke intersection function #TODO

// Quad functions
//==============================================================================================================================
Expand Down
Loading

0 comments on commit a9ffc90

Please sign in to comment.