Skip to content

Commit

Permalink
Added the Quad class. #24
Browse files Browse the repository at this point in the history
  • Loading branch information
MStachowicz committed May 13, 2023
1 parent 6b27b57 commit d904f01
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,18 @@ add_library(Geometry
source/Geometry/AABB.cpp
source/Geometry/AABB.hpp
source/Geometry/Cylinder.hpp
source/Geometry/Geometry.hpp
source/Geometry/Geometry.cpp
source/Geometry/Intersect.cpp
source/Geometry/Intersect.hpp
source/Geometry/Plane.cpp
source/Geometry/Plane.hpp
source/Geometry/Quad.cpp
source/Geometry/Quad.hpp
source/Geometry/Ray.hpp
source/Geometry/Sphere.hpp
source/Geometry/Triangle.hpp
source/Geometry/Triangle.cpp
source/Geometry/Geometry.hpp
source/Geometry/Geometry.cpp
)
target_include_directories(Geometry
PUBLIC source/Geometry
Expand Down
48 changes: 48 additions & 0 deletions source/Geometry/Quad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "Quad.hpp"
#include "Plane.hpp"

#include "glm/glm.hpp"

namespace Geometry
{
Quad::Quad(const Plane& p_plane) noexcept
: m_point_1{}
, m_point_2{}
, m_point_3{}
, m_point_4{}
{
glm::vec3 right;
if (p_plane.m_normal.x != 0 || p_plane.m_normal.z != 0)
right = glm::normalize(glm::vec3(p_plane.m_normal.z, 0.f, -p_plane.m_normal.x));
else
right = glm::normalize(glm::vec3(p_plane.m_normal.y, -p_plane.m_normal.x, 0.f));

const glm::vec3 up = glm::normalize(glm::cross(right, p_plane.m_normal));

// Since Plane has no real position, we construct the quad at the arbitrary center-point represented by the closest point on the plane to the origin.
const glm::vec3 p = p_plane.m_normal * p_plane.m_distance;

m_point_1 = glm::vec3(p + right + up);
m_point_2 = glm::vec3(p - right + up);
m_point_3 = glm::vec3(p - right - up);
m_point_4 = glm::vec3(p + right - up);
}

glm::vec3 Quad::center() const
{
return (m_point_1 + m_point_2 + m_point_3 + m_point_4) / 4.0f;
}
void Quad::scale(const float p_scale)
{
const auto c = center();
m_point_1 = m_point_1 + (glm::normalize(m_point_1 - c) * p_scale);
m_point_2 = m_point_2 + (glm::normalize(m_point_2 - c) * p_scale);
m_point_3 = m_point_3 + (glm::normalize(m_point_3 - c) * p_scale);
m_point_4 = m_point_4 + (glm::normalize(m_point_4 - c) * p_scale);
}

std::array<Triangle, 2> Quad::get_triangles() const
{
return std::array<Triangle, 2>({{m_point_1, m_point_2, m_point_3}, {m_point_3, m_point_4, m_point_1}});
}
}
39 changes: 39 additions & 0 deletions source/Geometry/Quad.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "Triangle.hpp"

#include "glm/vec3.hpp"

#include <array>

namespace Geometry
{
class Plane;

// A quadrilateral. Four-sided polygon, having four edges (sides) and four corners (vertices).
// Quad is a 2-dimensional shape.
class Quad
{
public:
glm::vec3 m_point_1;
glm::vec3 m_point_2;
glm::vec3 m_point_3;
glm::vec3 m_point_4;

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
: m_point_1{p_point_1}
, m_point_2{p_point_2}
, m_point_3{p_point_3}
, m_point_4{p_point_4}
{}
// Construct a unit quad inside the plane.
Quad(const Plane& p_plane) noexcept;

// Uniformly scale the Quad by p_scale factor from its center.
void scale(const float p_scale);

glm::vec3 center() const;
// Get a pair of triangles that represent this quad.
std::array<Triangle, 2> get_triangles() const;
};
}

0 comments on commit d904f01

Please sign in to comment.