-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b27b57
commit d904f01
Showing
3 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |