Move AABB class and base it on top of Range
This commit is contained in:
96
openVulkanoCpp/Math/AABB.hpp
Normal file
96
openVulkanoCpp/Math/AABB.hpp
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Math.hpp"
|
||||||
|
#include "Range.hpp"
|
||||||
|
|
||||||
|
namespace openVulkanoCpp::Math
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* \brief A class that represents an axis aligned bounding box
|
||||||
|
*/
|
||||||
|
class AABB final : Range<Math::Vector3f>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AABB() : Range(Math::Vector3f(INFINITY), Math::Vector3f(-INFINITY)) {}
|
||||||
|
|
||||||
|
AABB(const Math::Vector3f& min, const Math::Vector3f& max) : Range(min, max) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Init(const Math::Vector3f& min, const Math::Vector3f& max)
|
||||||
|
{
|
||||||
|
this->min = min;
|
||||||
|
this->max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Initiates the AABB from some other AABB
|
||||||
|
* \param other The other AABB that should be copied
|
||||||
|
*/
|
||||||
|
void Init(const AABB& other)
|
||||||
|
{
|
||||||
|
min = other.GetMin();
|
||||||
|
max = other.GetMax();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Grow(const Math::Vector3f& point)
|
||||||
|
{
|
||||||
|
min = Math::Utils::min(min, point);
|
||||||
|
max = Math::Utils::max(max, point);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Grow(const AABB& otherAABB)
|
||||||
|
{
|
||||||
|
min = Math::Utils::min(min, otherAABB.GetMin());
|
||||||
|
max = Math::Utils::max(max, otherAABB.GetMax());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Grow(const AABB& otherAABB, Math::Matrix4f transformation)
|
||||||
|
{
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Math::Vector3f GetDiagonal() const
|
||||||
|
{
|
||||||
|
return GetSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Math::Vector3f GetCenter() const
|
||||||
|
{
|
||||||
|
return min + (GetDiagonal() * 0.5f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Checks if the AABB overlaps with an other AABB
|
||||||
|
* \param other The other AABB that should be checked
|
||||||
|
* \return true if the AABB overlaps with the other, false if not
|
||||||
|
*/
|
||||||
|
[[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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Resets the AABB to min=Inf, max=-Inf, same as Init()
|
||||||
|
*/
|
||||||
|
void Reset()
|
||||||
|
{
|
||||||
|
min = Math::Vector3f(INFINITY);
|
||||||
|
max = Math::Vector3f(-INFINITY);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
36
openVulkanoCpp/Math/Range.hpp
Normal file
36
openVulkanoCpp/Math/Range.hpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#undef min
|
||||||
|
#undef max
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace openVulkanoCpp::Math
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
class Range
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
T min, max;
|
||||||
|
|
||||||
|
Range() = default;
|
||||||
|
|
||||||
|
Range(const T& min, const T& max) : min(min), max(max) {}
|
||||||
|
|
||||||
|
[[nodiscard]] const T& GetMin() const { return min; }
|
||||||
|
|
||||||
|
[[nodiscard]] const T& GetMax() const { return max; }
|
||||||
|
|
||||||
|
[[nodiscard]] T& GetMin() { return min; }
|
||||||
|
|
||||||
|
[[nodiscard]] T& GetMax() { return max; }
|
||||||
|
|
||||||
|
[[nodiscard]] T GetSize() const { return max - min; }
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,111 +0,0 @@
|
|||||||
/*
|
|
||||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "Math/Math.hpp"
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#undef min
|
|
||||||
#undef max
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace openVulkanoCpp
|
|
||||||
{
|
|
||||||
namespace Scene
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* \brief A class that represents an axis aligned bounding box
|
|
||||||
*/
|
|
||||||
class AABB final
|
|
||||||
{
|
|
||||||
Math::Vector3f m_min, m_max;
|
|
||||||
|
|
||||||
public:
|
|
||||||
AABB() : m_min(INFINITY), m_max(-INFINITY) {}
|
|
||||||
~AABB() = default;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \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)
|
|
||||||
{
|
|
||||||
m_min = m_max = point;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Init(const Math::Vector3f& min, const Math::Vector3f& max)
|
|
||||||
{
|
|
||||||
m_min = min;
|
|
||||||
m_max = max;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Initiates the AABB from some other AABB
|
|
||||||
* \param other The other AABB that should be copied
|
|
||||||
*/
|
|
||||||
void Init(const AABB& other)
|
|
||||||
{
|
|
||||||
m_min = other.GetMin();
|
|
||||||
m_max = other.GetMax();
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] inline const Math::Vector3f& GetMin() const { return m_min; }
|
|
||||||
|
|
||||||
[[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)
|
|
||||||
{
|
|
||||||
m_min = Math::Utils::min(m_min, point);
|
|
||||||
m_max = Math::Utils::max(m_max, point);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Grow(const AABB& otherAABB)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] Math::Vector3f GetDiagonal() const
|
|
||||||
{
|
|
||||||
return m_max - m_min;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] Math::Vector3f GetCenter() const
|
|
||||||
{
|
|
||||||
return m_min + (GetDiagonal() * 0.5f);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Checks if the AABB overlaps with an other AABB
|
|
||||||
* \param other The other AABB that should be checked
|
|
||||||
* \return true if the AABB overlaps with the other, false if not
|
|
||||||
*/
|
|
||||||
[[nodiscard]] bool IsOverlapping(const AABB& other) const
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Resets the AABB to min=Inf, max=-Inf, same as Init()
|
|
||||||
*/
|
|
||||||
void Reset()
|
|
||||||
{
|
|
||||||
m_min = Math::Vector3f(INFINITY);
|
|
||||||
m_max = Math::Vector3f(-INFINITY);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Base/ICloseable.hpp"
|
#include "Base/ICloseable.hpp"
|
||||||
#include "AABB.hpp"
|
#include "Math/AABB.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class aiMesh;
|
class aiMesh;
|
||||||
@@ -29,7 +29,7 @@ namespace openVulkanoCpp
|
|||||||
Vertex* vertices;
|
Vertex* vertices;
|
||||||
void* indices;
|
void* indices;
|
||||||
VertexIndexType indexType;
|
VertexIndexType indexType;
|
||||||
AABB aabb;
|
Math::AABB aabb;
|
||||||
ICloseable* renderGeo = nullptr;
|
ICloseable* renderGeo = nullptr;
|
||||||
|
|
||||||
Vertex* GetVertices() const { return vertices; }
|
Vertex* GetVertices() const { return vertices; }
|
||||||
|
|||||||
Reference in New Issue
Block a user