Refactor AABB class

This commit is contained in:
2021-02-08 00:14:23 +01:00
parent 5a12607b61
commit e871a989b4
5 changed files with 38 additions and 28 deletions

View File

@@ -7,7 +7,10 @@
#pragma once
#include "Math/Math.hpp"
#include "Base/IInitable.hpp"
#ifdef _MSC_VER
#undef min
#undef max
#endif
namespace openVulkanoCpp
{
@@ -16,30 +19,27 @@ namespace openVulkanoCpp
/**
* \brief A class that represents an axis aligned bounding box
*/
class AABB final : public virtual IInitable
class AABB final
{
Math::Vector3f min, max;
Math::Vector3f m_min, m_max;
public:
AABB() : min(INFINITY), max(-INFINITY) {}
AABB() : m_min(INFINITY), m_max(-INFINITY) {}
~AABB() = default;
/**
* \brief Initiates the AABB to min=Inf, max=-Inf
*/
void Init() override
{
min = Math::Vector3f(INFINITY);
max = Math::Vector3f(-INFINITY);
}
/**
* \brief Initiates the AABB to a single point (min=max=point)
* \param point The point that should be used as min and max of the AABB
*/
void Init(const Math::Vector3f& point)
{
min = max = point;
m_min = m_max = point;
}
void Init(const Math::Vector3f& min, const Math::Vector3f& max)
{
m_min = min;
m_max = max;
}
/**
@@ -48,24 +48,28 @@ namespace openVulkanoCpp
*/
void Init(const AABB& other)
{
min = other.GetMin();
max = other.GetMax();
m_min = other.GetMin();
m_max = other.GetMax();
}
[[nodiscard]] inline const Math::Vector3f& GetMin() const { return min; }
[[nodiscard]] inline const Math::Vector3f& GetMin() const { return m_min; }
[[nodiscard]] inline const Math::Vector3f& GetMax() const { return max; }
[[nodiscard]] inline const Math::Vector3f& GetMax() const { return m_max; }
[[nodiscard]] inline Math::Vector3f& GetMin() { return m_min; }
[[nodiscard]] inline Math::Vector3f& GetMax() { return m_max; }
void Grow(const Math::Vector3f& point)
{
min = Math::Utils::min(min, point);
max = Math::Utils::max(max, point);
m_min = Math::Utils::min(m_min, point);
m_max = Math::Utils::max(m_max, point);
}
void Grow(const AABB& otherAABB)
{
min = Math::Utils::min(min, otherAABB.GetMin());
max = Math::Utils::max(max, otherAABB.GetMax());
m_min = Math::Utils::min(m_min, otherAABB.GetMin());
m_max = Math::Utils::max(m_max, otherAABB.GetMax());
}
void Grow(const AABB& otherAABB, Math::Matrix4f transformation)
@@ -75,12 +79,12 @@ namespace openVulkanoCpp
[[nodiscard]] Math::Vector3f GetDiagonal() const
{
return max - min;
return m_max - m_min;
}
[[nodiscard]] Math::Vector3f GetCenter() const
{
return min + (GetDiagonal() * 0.5f);
return m_min + (GetDiagonal() * 0.5f);
}
/**
@@ -90,7 +94,8 @@ namespace openVulkanoCpp
*/
[[nodiscard]] bool IsOverlapping(const AABB& other) const
{
return !(other.min.x > max.x || other.max.x < min.x || other.min.y > max.y || other.max.y < min.y || other.min.z > max.z || other.max.z < min.z);
return !(other.m_min.x > m_max.x || other.m_max.x < m_min.x || other.m_min.y > m_max.y ||
other.m_max.y < m_min.y || other.m_min.z > m_max.z || other.m_max.z < m_min.z);
}
/**
@@ -98,7 +103,8 @@ namespace openVulkanoCpp
*/
void Reset()
{
Init();
m_min = Math::Vector3f(INFINITY);
m_max = Math::Vector3f(-INFINITY);
}
};
}

View File

@@ -53,7 +53,7 @@ namespace openVulkanoCpp::Scene
void Geometry::Init(aiMesh* mesh)
{
#ifdef ASSIMP_AVAILABLE
aabb.Init();
aabb.Reset();
Init(mesh->mNumVertices, mesh->mNumFaces * 3); // Reserve the space for the data
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{

View File

@@ -6,6 +6,7 @@
#pragma once
#include "Base/IInitable.hpp"
#include "Base/ICloseable.hpp"
#include "Math/Math.hpp"
#include "Drawable.hpp"

View File

@@ -14,8 +14,9 @@ namespace openVulkanoCpp
{
namespace Scene
{
struct Scene : virtual public IInitable, virtual public ICloseable
class Scene : virtual public IInitable, virtual public ICloseable
{
public:
Node* root;
std::vector<Drawable*> shapeList;
Shader* shader;

View File

@@ -7,6 +7,8 @@
#pragma once
#include "DataFormat.hpp"
#include <stdexcept>
#include <vector>
namespace openVulkanoCpp
{