From 44b8bb8172e06d5a5413dfc7bf3e2d9180c6094a Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 5 Mar 2024 01:05:25 -0500 Subject: [PATCH] Migrate AABB2D implementation to it's cpp file --- CMakeLists.txt | 3 +- include/J3ML/Geometry/AABB2D.h | 83 +++++----------------------- include/J3ML/LinearAlgebra/Vector2.h | 1 + src/J3ML/Geometry/AABB2D.cpp | 80 +++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 69 deletions(-) create mode 100644 src/J3ML/Geometry/AABB2D.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 933b735..51a4ff7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,7 +64,8 @@ add_library(J3ML SHARED ${J3ML_SRC} include/J3ML/Algorithm/DifferentialSolvers.h include/J3ML/Units.h src/J3ML/J3ML.cpp - include/J3ML/LinearAlgebra/Matrix.h) + include/J3ML/LinearAlgebra/Matrix.h + src/J3ML/Geometry/AABB2D.cpp) set_target_properties(J3ML PROPERTIES LINKER_LANGUAGE CXX) install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME}) diff --git a/include/J3ML/Geometry/AABB2D.h b/include/J3ML/Geometry/AABB2D.h index 53c5614..786b988 100644 --- a/include/J3ML/Geometry/AABB2D.h +++ b/include/J3ML/Geometry/AABB2D.h @@ -17,90 +17,37 @@ namespace J3ML::Geometry minPoint(min), maxPoint(max) {} - float Width() const { return maxPoint.x - minPoint.x; } - float Height() const { return maxPoint.y - minPoint.y; } + float Width() const; + float Height() const; - float DistanceSq(const Vector2& pt) const - { - Vector2 cp = pt.Clamp(minPoint, maxPoint); - return cp.DistanceSq(pt); - } + float DistanceSq(const Vector2& pt) const; void SetNegativeInfinity(); - void Enclose(const Vector2& point) - { - minPoint = Vector2::Min(minPoint, point); - maxPoint = Vector2::Max(maxPoint, point); - } + void Enclose(const Vector2& point); - bool Intersects(const AABB2D& rhs) const - { - return maxPoint.x >= rhs.minPoint.x && - maxPoint.y >= rhs.minPoint.y && - rhs.maxPoint.x >= minPoint.x && - rhs.maxPoint.y >= minPoint.y; - } + bool Intersects(const AABB2D& rhs) const; - bool Contains(const AABB2D& rhs) const - { - return rhs.minPoint.x >= minPoint.x && rhs.minPoint.y >= minPoint.y - && rhs.maxPoint.x <= maxPoint.x && rhs.maxPoint.y <= maxPoint.y; - } + bool Contains(const AABB2D& rhs) const; - bool Contains(const Vector2& pt) const - { - return pt.x >= minPoint.x && pt.y >= minPoint.y - && pt.x <= maxPoint.x && pt.y <= maxPoint.y; - } + bool Contains(const Vector2& pt) const; - bool Contains(int x, int y) const - { - return x >= minPoint.x && y >= minPoint.y - && x <= maxPoint.x && y <= maxPoint.y; - } + bool Contains(int x, int y) const; - bool IsDegenerate() const - { - return minPoint.x >= maxPoint.x || minPoint.y >= maxPoint.y; - } + bool IsDegenerate() const; - bool HasNegativeVolume() const - { - return maxPoint.x < minPoint.x || maxPoint.y < minPoint.y; - } + bool HasNegativeVolume() const; - bool IsFinite() const - { - return minPoint.IsFinite() && maxPoint.IsFinite() && minPoint.MinElement() > -1e5f && maxPoint.MaxElement() < 1e5f; - } + bool IsFinite() const; - Vector2 PosInside(const Vector2 &normalizedPos) const - { - return minPoint + normalizedPos.Mul(maxPoint - minPoint); - } + Vector2 PosInside(const Vector2 &normalizedPos) const; - Vector2 ToNormalizedLocalSpace(const Vector2 &pt) const - { - return (pt - minPoint).Div(maxPoint - minPoint); - } + Vector2 ToNormalizedLocalSpace(const Vector2 &pt) const; - AABB2D operator+(const Vector2& pt) const - { - AABB2D a; - a.minPoint = minPoint + pt; - a.maxPoint = maxPoint + pt; - return a; - } + AABB2D operator+(const Vector2& pt) const; - AABB2D operator-(const Vector2& pt) const - { - AABB2D a; - a.minPoint = minPoint - pt; - a.maxPoint = maxPoint - pt; - return a; - } + AABB2D operator-(const Vector2& pt) const; }; } \ No newline at end of file diff --git a/include/J3ML/LinearAlgebra/Vector2.h b/include/J3ML/LinearAlgebra/Vector2.h index 6996c9a..32fdd9e 100644 --- a/include/J3ML/LinearAlgebra/Vector2.h +++ b/include/J3ML/LinearAlgebra/Vector2.h @@ -30,6 +30,7 @@ namespace J3ML::LinearAlgebra { static const Vector2 Down; static const Vector2 Right; static const Vector2 NaN; + static const Vector2 Infinity; float GetX() const; float GetY() const; diff --git a/src/J3ML/Geometry/AABB2D.cpp b/src/J3ML/Geometry/AABB2D.cpp new file mode 100644 index 0000000..d3071dc --- /dev/null +++ b/src/J3ML/Geometry/AABB2D.cpp @@ -0,0 +1,80 @@ +#include + +namespace J3ML::Geometry +{ + + float AABB2D::Width() const { return maxPoint.x - minPoint.x; } + + float AABB2D::Height() const { return maxPoint.y - minPoint.y; } + + float AABB2D::DistanceSq(const Vector2 &pt) const { + Vector2 cp = pt.Clamp(minPoint, maxPoint); + return cp.DistanceSq(pt); + } + + void AABB2D::SetNegativeInfinity() { + minPoint = Vector2::Infinity; + maxPoint = -Vector2::Infinity; + } + + void AABB2D::Enclose(const Vector2 &point) { + minPoint = Vector2::Min(minPoint, point); + maxPoint = Vector2::Max(maxPoint, point); + } + + bool AABB2D::Intersects(const AABB2D &rhs) const { + return maxPoint.x >= rhs.minPoint.x && + maxPoint.y >= rhs.minPoint.y && + rhs.maxPoint.x >= minPoint.x && + rhs.maxPoint.y >= minPoint.y; + } + + bool AABB2D::Contains(const AABB2D &rhs) const { + return rhs.minPoint.x >= minPoint.x && rhs.minPoint.y >= minPoint.y + && rhs.maxPoint.x <= maxPoint.x && rhs.maxPoint.y <= maxPoint.y; + } + + bool AABB2D::Contains(const Vector2 &pt) const { + return pt.x >= minPoint.x && pt.y >= minPoint.y + && pt.x <= maxPoint.x && pt.y <= maxPoint.y; + } + + bool AABB2D::Contains(int x, int y) const { + return x >= minPoint.x && y >= minPoint.y + && x <= maxPoint.x && y <= maxPoint.y; + } + + bool AABB2D::IsDegenerate() const { + return minPoint.x >= maxPoint.x || minPoint.y >= maxPoint.y; + } + + bool AABB2D::HasNegativeVolume() const { + return maxPoint.x < minPoint.x || maxPoint.y < minPoint.y; + } + + bool AABB2D::IsFinite() const { + return minPoint.IsFinite() && maxPoint.IsFinite() && minPoint.MinElement() > -1e5f && maxPoint.MaxElement() < 1e5f; + } + + Vector2 AABB2D::PosInside(const Vector2 &normalizedPos) const { + return minPoint + normalizedPos.Mul(maxPoint - minPoint); + } + + Vector2 AABB2D::ToNormalizedLocalSpace(const Vector2 &pt) const { + return (pt - minPoint).Div(maxPoint - minPoint); + } + + AABB2D AABB2D::operator+(const Vector2 &pt) const { + AABB2D a; + a.minPoint = minPoint + pt; + a.maxPoint = maxPoint + pt; + return a; + } + + AABB2D AABB2D::operator-(const Vector2 &pt) const { + AABB2D a; + a.minPoint = minPoint - pt; + a.maxPoint = maxPoint - pt; + return a; + } +} \ No newline at end of file