Skip to content

Commit b421e60

Browse files
committed
Fixed bugs in Plane and Quad mesh generation.
Renamed Quad point members to match their positons in space for easier debugging. Fixed get_intersection(Plane, Plane, Plane) function with missing brackets. Ouch. Added Constants file for Geometry lib to use as the Tolerance source across Intersect and individual primitive classes.
1 parent 2ed465e commit b421e60

File tree

9 files changed

+74
-49
lines changed

9 files changed

+74
-49
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ source/Geometry/Cylinder.hpp
188188
source/Geometry/Cylinder.cpp
189189
source/Geometry/Cone.hpp
190190
source/Geometry/Cone.cpp
191+
source/Geometry/Constants.hpp
191192
source/Geometry/Cuboid.hpp
192193
source/Geometry/Cuboid.cpp
193194
source/Geometry/Geometry.hpp

source/Geometry/Constants.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
namespace Geometry
4+
{
5+
// Constants
6+
constexpr float TOLERANCE = 0.0001f; // The tolerance to use when comparing floating point numbers.
7+
constexpr float INF_SCALER = 1000.f; // Scale the geometry that extends infinitely by this to give it an infinite appearance.
8+
}

source/Geometry/Intersect.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ namespace Geometry
433433
if (std::abs(denom) < Epsilon)
434434
return std::nullopt;
435435
else
436-
return plane_1.m_distance * u + glm::cross(plane_1.m_normal, plane_3.m_distance * plane_2.m_normal - plane_2.m_distance * plane_3.m_normal) / denom;
436+
return (plane_1.m_distance * u + glm::cross(plane_1.m_normal, plane_3.m_distance * plane_2.m_normal - plane_2.m_distance * plane_3.m_normal)) / denom;
437437
}
438438
std::optional<glm::vec3> get_intersection(const Sphere& sphere_1, const Sphere& sphere_2)
439439
{
@@ -584,6 +584,13 @@ namespace Geometry
584584
// If sphere center within +/-radius from plane, plane intersects sphere
585585
return std::abs(dist) <= sphere.m_radius;
586586
}
587+
bool intersecting(const Plane& plane, const glm::vec3& point, float tolerance)
588+
{
589+
// The point is on the plane if the dot product of the normal and the point is equal to the distance of the plane.
590+
// The dot product of the normal and the point is the projection of the point onto the normal.
591+
// If the projection is equal to the distance of the plane, then the point is on the plane.
592+
return glm::abs(glm::dot(plane.m_normal, point) - plane.m_distance) < tolerance;
593+
}
587594
bool intersecting(const Sphere& sphere_1, const Sphere& sphere_2)
588595
{
589596
// Returns true if the spheres are intersecting.

source/Geometry/Intersect.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "Geometry/Line.hpp"
44
#include "Geometry/LineSegment.hpp"
5+
#include "Geometry/Constants.hpp"
56
#include "Utility/Logger.hpp"
67

78
#include "glm/vec3.hpp"
@@ -100,12 +101,13 @@ namespace Geometry
100101
//==============================================================================================================================
101102
// intersecting functions: Return if the geometries are intersecting.
102103
//==============================================================================================================================
103-
bool intersecting(const AABB& AABB_1, const AABB& AABB_2);
104-
bool intersecting(const AABB& AABB, const Ray& ray);
105-
bool intersecting(const Line& line, const Triangle& triangle);
106-
bool intersecting(const Plane& plane_1, const Plane& plane_2);
107-
bool intersecting(const Plane& plane, const Sphere& sphere);
108-
bool intersecting(const Sphere& sphere_1, const Sphere& sphere_2);
104+
bool intersecting(const AABB& AABB_1, const AABB& AABB_2);
105+
bool intersecting(const AABB& AABB, const Ray& ray);
106+
bool intersecting(const Line& line, const Triangle& triangle);
107+
bool intersecting(const Plane& plane_1, const Plane& plane_2);
108+
bool intersecting(const Plane& plane, const Sphere& sphere);
109+
bool intersecting(const Plane& plane, const glm::vec3& point, float tolerance = TOLERANCE);
110+
bool intersecting(const Sphere& sphere_1, const Sphere& sphere_2);
109111
bool intersecting(const Triangle& triangle_1, const Triangle& triangle_2);
110112

111113
} // namespace Geometry

source/Geometry/Plane.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Plane.hpp"
2+
#include "Intersect.hpp"
23

34
#include "glm/glm.hpp"
45

@@ -14,6 +15,8 @@ namespace Geometry
1415
, m_distance{p_equation.w}
1516
{}
1617

18+
bool Plane::point_on_plane(const glm::vec3 & p_point, float tolerance) const { return intersecting(*this, p_point, tolerance); }
19+
1720
void Plane::normalise()
1821
{
1922
// Normalise the plane p_equation

source/Geometry/Plane.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "Constants.hpp"
4+
35
#include "glm/vec3.hpp"
46
#include "glm/vec4.hpp"
57

@@ -20,6 +22,10 @@ namespace Geometry
2022
// Construct a plane from a plane equation.
2123
//@param p_equation Equation of a plane in the form 'x + y + z + w = 0'
2224
Plane(const glm::vec4& p_equation) noexcept;
25+
// @param p_point The point to check if it lies on the plane.
26+
// @param tolerance The tolerance to use when comparing the point to the plane.
27+
// @return True if the point lies on the plane, false otherwise.
28+
bool point_on_plane(const glm::vec3& p_point, const float tolerance = TOLERANCE) const;
2329

2430
void normalise();
2531
};

source/Geometry/Quad.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,38 @@ namespace Geometry
1515
else
1616
right = glm::normalize(glm::vec3(p_normal.y, -p_normal.x, 0.f));
1717

18-
const glm::vec3 up = glm::normalize(glm::cross(right, p_normal));
18+
glm::vec3 up = -glm::cross(right, p_normal);
1919

20-
m_point_1 = glm::vec3(p_point + right + up);
21-
m_point_2 = glm::vec3(p_point - right + up);
22-
m_point_3 = glm::vec3(p_point - right - up);
23-
m_point_4 = glm::vec3(p_point + right - up);
20+
m_top_left = glm::vec3(p_point - right + up);
21+
m_top_right = glm::vec3(p_point + right + up);
22+
m_bottom_left = glm::vec3(p_point - right - up);
23+
m_bottom_right = glm::vec3(p_point + right - up);
2424
}
2525
Quad::Quad(const Plane& p_plane) noexcept
2626
{
2727
// Since Plane has no real position, we construct the quad at the arbitrary center-point represented by the closest point
2828
// on the plane to the origin.
2929
const glm::vec3 p = p_plane.m_normal * p_plane.m_distance;
30-
auto quad = Quad(p, p_plane.m_normal);
31-
m_point_1 = quad.m_point_1;
32-
m_point_2 = quad.m_point_2;
33-
m_point_3 = quad.m_point_3;
34-
m_point_4 = quad.m_point_4;
30+
auto quad = Quad(p, p_plane.m_normal);
31+
m_top_left = quad.m_top_left;
32+
m_top_right = quad.m_top_right;
33+
m_bottom_left = quad.m_bottom_left;
34+
m_bottom_right = quad.m_bottom_right;
3535
}
3636
void Quad::transform(const glm::mat4& p_transform)
3737
{
38-
m_point_1 = glm::vec3(p_transform * glm::vec4(m_point_1, 1.f));
39-
m_point_2 = glm::vec3(p_transform * glm::vec4(m_point_2, 1.f));
40-
m_point_3 = glm::vec3(p_transform * glm::vec4(m_point_3, 1.f));
41-
m_point_4 = glm::vec3(p_transform * glm::vec4(m_point_4, 1.f));
38+
m_top_left = glm::vec3(p_transform * glm::vec4(m_top_left, 1.f));
39+
m_top_right = glm::vec3(p_transform * glm::vec4(m_top_right, 1.f));
40+
m_bottom_left = glm::vec3(p_transform * glm::vec4(m_bottom_left, 1.f));
41+
m_bottom_right = glm::vec3(p_transform * glm::vec4(m_bottom_right, 1.f));
4242
}
4343
void Quad::draw_UI() const
4444
{
4545
ImGui::SeparatorText("Quad");
46-
ImGui::Text("Point 1", m_point_1);
47-
ImGui::Text("Point 2", m_point_2);
48-
ImGui::Text("Point 3", m_point_3);
49-
ImGui::Text("Point 4", m_point_4);
46+
ImGui::Text("Point 1", m_top_left);
47+
ImGui::Text("Point 2", m_top_right);
48+
ImGui::Text("Point 3", m_bottom_left);
49+
ImGui::Text("Point 4", m_bottom_right);
5050
ImGui::Text("Center", center());
5151
}
5252
Quad::Quad(const Triangle& p_triangle) noexcept
@@ -85,27 +85,27 @@ namespace Geometry
8585
most_left_value = dot_left;
8686
}
8787

88-
m_point_1 = glm::vec3(triangle_center + (right * most_right_value) + (up * most_up_value));
89-
m_point_2 = glm::vec3(triangle_center + (-right * most_left_value) + (up * most_up_value));
90-
m_point_3 = glm::vec3(triangle_center + (-right * most_left_value) + (-up * most_down_value));
91-
m_point_4 = glm::vec3(triangle_center + (right * most_right_value) + (-up * most_down_value));
88+
m_top_left = glm::vec3(triangle_center + (right * most_right_value) + (up * most_up_value));
89+
m_top_right = glm::vec3(triangle_center + (-right * most_left_value) + (up * most_up_value));
90+
m_bottom_left = glm::vec3(triangle_center + (-right * most_left_value) + (-up * most_down_value));
91+
m_bottom_right = glm::vec3(triangle_center + (right * most_right_value) + (-up * most_down_value));
9292
}
9393

9494
glm::vec3 Quad::center() const
9595
{
96-
return (m_point_1 + m_point_2 + m_point_3 + m_point_4) / 4.0f;
96+
return (m_top_left + m_top_right + m_bottom_left + m_bottom_right) / 4.0f;
9797
}
9898
void Quad::scale(const float p_scale)
9999
{
100100
const auto c = center();
101-
m_point_1 = m_point_1 + (glm::normalize(m_point_1 - c) * p_scale);
102-
m_point_2 = m_point_2 + (glm::normalize(m_point_2 - c) * p_scale);
103-
m_point_3 = m_point_3 + (glm::normalize(m_point_3 - c) * p_scale);
104-
m_point_4 = m_point_4 + (glm::normalize(m_point_4 - c) * p_scale);
101+
m_top_left = m_top_left + (glm::normalize(m_top_left - c) * p_scale);
102+
m_top_right = m_top_right + (glm::normalize(m_top_right - c) * p_scale);
103+
m_bottom_left = m_bottom_left + (glm::normalize(m_bottom_left - c) * p_scale);
104+
m_bottom_right = m_bottom_right + (glm::normalize(m_bottom_right - c) * p_scale);
105105
}
106106

107107
std::array<Triangle, 2> Quad::get_triangles() const
108108
{
109-
return std::array<Triangle, 2>({Triangle{m_point_1, m_point_2, m_point_3}, Triangle{m_point_3, m_point_4, m_point_1}});
109+
return std::array<Triangle, 2>({Triangle{m_top_left, m_top_right, m_bottom_left}, Triangle{m_bottom_left, m_bottom_right, m_top_left}});
110110
}
111111
}

source/Geometry/Quad.hpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@ namespace Geometry
1414

1515
// A quadrilateral. Four-sided polygon, having four edges (sides) and four corners (vertices).
1616
// Quad is a 2-dimensional shape.
17-
// Stored quad points are in CCW winding order.
1817
class Quad
1918
{
2019
public:
21-
glm::vec3 m_point_1;
22-
glm::vec3 m_point_2;
23-
glm::vec3 m_point_3;
24-
glm::vec3 m_point_4;
25-
26-
// Construct a quad from its 4 corner points. Points expected in CCW order.
27-
constexpr Quad(const glm::vec3& p_point_1, const glm::vec3& p_point_2, const glm::vec3& p_point_3, const glm::vec3& p_point_4) noexcept
28-
: m_point_1{p_point_1}
29-
, m_point_2{p_point_2}
30-
, m_point_3{p_point_3}
31-
, m_point_4{p_point_4}
20+
glm::vec3 m_top_left;
21+
glm::vec3 m_top_right;
22+
glm::vec3 m_bottom_left;
23+
glm::vec3 m_bottom_right;
24+
25+
// Construct a quad from its 4 corner points.
26+
constexpr Quad(const glm::vec3& p_top_left, const glm::vec3& p_top_right, const glm::vec3& p_bottom_left, const glm::vec3& p_bottom_right) noexcept
27+
: m_top_left{p_top_left}
28+
, m_top_right{p_top_right}
29+
, m_bottom_left{p_bottom_left}
30+
, m_bottom_right{p_bottom_right}
3231
{}
3332
// Construct a unit quad at p_point facing p_normal (counter-clockwise winding).
3433
Quad(const glm::vec3& p_point, const glm::vec3& p_normal) noexcept;

source/Utility/MeshBuilder.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,10 @@ namespace Utility
296296
void add_quad(const glm::vec3& top_left, const glm::vec3& top_right, const glm::vec3& bottom_left, const glm::vec3& bottom_right)
297297
{
298298
add_quad_impl(top_left, top_right, bottom_left, bottom_right);
299-
300299
}
301300
void add_quad(const Geometry::Quad& quad)
302301
{
303-
add_quad(quad.m_point_1, quad.m_point_2, quad.m_point_3, quad.m_point_4);
302+
add_quad(quad.m_top_left, quad.m_top_right, quad.m_bottom_left, quad.m_bottom_right);
304303
}
305304
void add_cone(const glm::vec3& base, const glm::vec3& top, float radius, size_t segments = 16)
306305
{

0 commit comments

Comments
 (0)