From 9f25d0a4768d60964c7411887a54caca2d868eea Mon Sep 17 00:00:00 2001 From: Dawid Zapolski Date: Sun, 10 Nov 2024 23:42:50 +0100 Subject: [PATCH 1/2] Removed MathLibraryInterface --- .../CheckCollisionVisitorInterface.h | 26 +++++++------------ .../AABB_AABBCheckCollisionVisitor.h | 9 ++----- .../CircleAABBCheckCollisionVisitor.h | 9 ++----- .../CircleCircleCheckCollisionVisitor.h | 9 ++----- .../OBB_AABBCheckCollisionVisitor.h | 9 ++----- .../DynamicCollisionDetectionStrategy.h | 25 ++++++++---------- .../StaticCollisionDetectionStrategy.h | 24 +++++++---------- include/Geometrics/Math/BartaMathLibrary.h | 12 --------- .../Geometrics/Math/MathLibraryInterface.h | 15 ----------- include/Utilities/MathUtilities.h | 14 ++++++++++ .../AABB_AABBCheckCollisionVisitor.cpp | 4 +-- .../CircleAABBCheckCollisionVisitor.cpp | 4 +-- .../CircleCircleCheckCollisionVisitor.cpp | 7 +++-- .../OBB_AABBCheckCollisionVisitor.cpp | 2 -- .../DynamicCollisionDetectionStrategy.cpp | 15 +++++------ .../StaticCollisionDetectionStrategy.cpp | 12 ++++----- lib/Geometrics/Math/BartaMathLibrary.cpp | 6 ----- lib/Geometrics/Math/CMakeLists.txt | 7 +++-- 18 files changed, 72 insertions(+), 137 deletions(-) delete mode 100644 include/Geometrics/Math/BartaMathLibrary.h delete mode 100644 include/Geometrics/Math/MathLibraryInterface.h create mode 100644 include/Utilities/MathUtilities.h delete mode 100644 lib/Geometrics/Math/BartaMathLibrary.cpp diff --git a/include/Collisions/CheckCollisionVisitorInterface.h b/include/Collisions/CheckCollisionVisitorInterface.h index a4a5027..b4a8ff8 100644 --- a/include/Collisions/CheckCollisionVisitorInterface.h +++ b/include/Collisions/CheckCollisionVisitorInterface.h @@ -1,25 +1,17 @@ #pragma once -#include "../pch.h" #include "../Dynamics/TimerInterface.h" -#include "../Geometrics/Math/MathLibraryInterface.h" +#include "../pch.h" #include #include -namespace Barta{ - class CheckCollisionVisitorInterface{ - public: - CheckCollisionVisitorInterface() = default; - virtual ~CheckCollisionVisitorInterface() = default; +namespace Barta { +class CheckCollisionVisitorInterface { +public: + CheckCollisionVisitorInterface() = default; + virtual ~CheckCollisionVisitorInterface() = default; - virtual CollisionTestResult checkStaticCollision( - const MathLibraryInterface& mathLib, - CollisionTestResultBuilder& collisionTestResultBuilder - ) const = 0; + virtual CollisionTestResult checkStaticCollision(CollisionTestResultBuilder& collisionTestResultBuilder) const = 0; - virtual CollisionTestResult checkDynamicCollision( - const MathLibraryInterface& mathLib, - const float delta_time, - CollisionTestResultBuilder& collisionTestResultBuilder - ) const = 0; - }; + virtual CollisionTestResult checkDynamicCollision(const float delta_time, CollisionTestResultBuilder& collisionTestResultBuilder) const = 0; +}; } diff --git a/include/Collisions/CheckCollisionVisitors/AABB_AABBCheckCollisionVisitor.h b/include/Collisions/CheckCollisionVisitors/AABB_AABBCheckCollisionVisitor.h index 953c49a..af566fe 100644 --- a/include/Collisions/CheckCollisionVisitors/AABB_AABBCheckCollisionVisitor.h +++ b/include/Collisions/CheckCollisionVisitors/AABB_AABBCheckCollisionVisitor.h @@ -13,14 +13,9 @@ class AABB_AABBCheckCollisionVisitor: public CheckCollisionVisitorInterface { public: AABB_AABBCheckCollisionVisitor(const AABB& aabb1, const AABB& aabb2, const DynamicsDifference& dynamicsDifference) noexcept; - CollisionTestResult checkStaticCollision(const MathLibraryInterface& mathLib, CollisionTestResultBuilder& collisionTestResultBuilder) - const override; + CollisionTestResult checkStaticCollision(CollisionTestResultBuilder& collisionTestResultBuilder) const override; - CollisionTestResult checkDynamicCollision( - const MathLibraryInterface& mathLib, - float delta_time, - CollisionTestResultBuilder& collisionTestResultBuilder - ) const override; + CollisionTestResult checkDynamicCollision(float delta_time, CollisionTestResultBuilder& collisionTestResultBuilder) const override; private: const AABB aabb1; diff --git a/include/Collisions/CheckCollisionVisitors/CircleAABBCheckCollisionVisitor.h b/include/Collisions/CheckCollisionVisitors/CircleAABBCheckCollisionVisitor.h index f24a2a9..5ccde0c 100644 --- a/include/Collisions/CheckCollisionVisitors/CircleAABBCheckCollisionVisitor.h +++ b/include/Collisions/CheckCollisionVisitors/CircleAABBCheckCollisionVisitor.h @@ -12,14 +12,9 @@ class CircleAABBCheckCollisionVisitor: public CheckCollisionVisitorInterface { CircleAABBCheckCollisionVisitor(const Circle& circle, const AABB& aabb, const DynamicsDifference& dynamicsDifference); virtual ~CircleAABBCheckCollisionVisitor(); - CollisionTestResult checkStaticCollision(const MathLibraryInterface& mathLib, CollisionTestResultBuilder& collisionTestResultBuilder) - const override; + CollisionTestResult checkStaticCollision(CollisionTestResultBuilder& collisionTestResultBuilder) const override; - CollisionTestResult checkDynamicCollision( - const MathLibraryInterface& mathLib, - float delta_time, - CollisionTestResultBuilder& collisionTestResultBuilder - ) const override; + CollisionTestResult checkDynamicCollision(float delta_time, CollisionTestResultBuilder& collisionTestResultBuilder) const override; private: const Circle circle; diff --git a/include/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.h b/include/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.h index 2501706..9616b73 100644 --- a/include/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.h +++ b/include/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.h @@ -10,14 +10,9 @@ class CircleCircleCheckCollisionVisitor: public CheckCollisionVisitorInterface { CircleCircleCheckCollisionVisitor(const Circle& circle1, const Circle& circle2, const DynamicsDifference& dynamicsDifference); virtual ~CircleCircleCheckCollisionVisitor(); - CollisionTestResult checkStaticCollision(const MathLibraryInterface& mathLib, CollisionTestResultBuilder& collisionTestResultBuilder) - const override; + CollisionTestResult checkStaticCollision(CollisionTestResultBuilder& collisionTestResultBuilder) const override; - CollisionTestResult checkDynamicCollision( - const MathLibraryInterface& mathLib, - float delta_time, - CollisionTestResultBuilder& collisionTestResultBuilder - ) const override; + CollisionTestResult checkDynamicCollision(float delta_time, CollisionTestResultBuilder& collisionTestResultBuilder) const override; private: const Circle circle1; diff --git a/include/Collisions/CheckCollisionVisitors/OBB_AABBCheckCollisionVisitor.h b/include/Collisions/CheckCollisionVisitors/OBB_AABBCheckCollisionVisitor.h index 55bee03..e5e68a2 100644 --- a/include/Collisions/CheckCollisionVisitors/OBB_AABBCheckCollisionVisitor.h +++ b/include/Collisions/CheckCollisionVisitors/OBB_AABBCheckCollisionVisitor.h @@ -12,14 +12,9 @@ class OBB_AABBCheckCollisionVisitor: public CheckCollisionVisitorInterface { public: OBB_AABBCheckCollisionVisitor(const OBB& obb, const AABB& aabb, const DynamicsDifference& dynamicsDifference) noexcept; - CollisionTestResult checkStaticCollision(const MathLibraryInterface& mathLib, CollisionTestResultBuilder& collisionTestResultBuilder) - const override; + CollisionTestResult checkStaticCollision(CollisionTestResultBuilder& collisionTestResultBuilder) const override; - CollisionTestResult checkDynamicCollision( - const MathLibraryInterface& mathLib, - float deltaTime, - CollisionTestResultBuilder& collisionTestResultBuilder - ) const override; + CollisionTestResult checkDynamicCollision(float deltaTime, CollisionTestResultBuilder& collisionTestResultBuilder) const override; Vector2f calculateCollisionPoint() const; private: diff --git a/include/Collisions/CollisionDetectionStrategies/DynamicCollisionDetectionStrategy.h b/include/Collisions/CollisionDetectionStrategies/DynamicCollisionDetectionStrategy.h index 795660d..27df10d 100644 --- a/include/Collisions/CollisionDetectionStrategies/DynamicCollisionDetectionStrategy.h +++ b/include/Collisions/CollisionDetectionStrategies/DynamicCollisionDetectionStrategy.h @@ -1,20 +1,17 @@ #pragma once +#include "../../Dynamics/TimerInterface.h" #include "../../pch.h" -#include"../CollisionDetectionStrategyInterface.h" -#include"../../Dynamics/TimerInterface.h" -#include "../../Geometrics/Math/MathLibraryInterface.h" +#include "../CollisionDetectionStrategyInterface.h" -namespace Barta{ - class DynamicCollisionDetectionStrategy : public CollisionDetectionStrategyInterface{ - public: - DynamicCollisionDetectionStrategy(std::unique_ptr mathLibrary, const TimerInterface& timer); - virtual ~DynamicCollisionDetectionStrategy() noexcept = default; +namespace Barta { +class DynamicCollisionDetectionStrategy: public CollisionDetectionStrategyInterface { +public: + DynamicCollisionDetectionStrategy(const TimerInterface& timer); + virtual ~DynamicCollisionDetectionStrategy() noexcept = default; - virtual CollisionTestResult acceptCheckCollisionVisitor( const CheckCollisionVisitorInterface& checkCollisionVisitor ) const override; - private: - const TimerInterface& timer; + virtual CollisionTestResult acceptCheckCollisionVisitor(const CheckCollisionVisitorInterface& checkCollisionVisitor) const override; - std::unique_ptr mathLibrary; - }; +private: + const TimerInterface& timer; +}; } - diff --git a/include/Collisions/CollisionDetectionStrategies/StaticCollisionDetectionStrategy.h b/include/Collisions/CollisionDetectionStrategies/StaticCollisionDetectionStrategy.h index 4fa80ae..6660fa2 100644 --- a/include/Collisions/CollisionDetectionStrategies/StaticCollisionDetectionStrategy.h +++ b/include/Collisions/CollisionDetectionStrategies/StaticCollisionDetectionStrategy.h @@ -1,21 +1,15 @@ #pragma once -#include #include "../../pch.h" -#include"../CollisionDetectionStrategyInterface.h" -#include "../../Geometrics/Math/MathLibraryInterface.h" - -namespace Barta{ - class StaticCollisionDetectionStrategy : public CollisionDetectionStrategyInterface{ - - public: - StaticCollisionDetectionStrategy(std::unique_ptr); - virtual ~StaticCollisionDetectionStrategy() noexcept = default; +#include "../CollisionDetectionStrategyInterface.h" +#include - virtual CollisionTestResult acceptCheckCollisionVisitor( const CheckCollisionVisitorInterface& checkCollisionVisitor ) const override; +namespace Barta { +class StaticCollisionDetectionStrategy: public CollisionDetectionStrategyInterface { +public: + StaticCollisionDetectionStrategy(); + virtual ~StaticCollisionDetectionStrategy() noexcept = default; - private: - std::unique_ptr mathLibrary; - }; + virtual CollisionTestResult acceptCheckCollisionVisitor(const CheckCollisionVisitorInterface& checkCollisionVisitor) const override; +}; } - diff --git a/include/Geometrics/Math/BartaMathLibrary.h b/include/Geometrics/Math/BartaMathLibrary.h deleted file mode 100644 index 07ef0f2..0000000 --- a/include/Geometrics/Math/BartaMathLibrary.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "../../pch.h" -#include"MathLibraryInterface.h" - -namespace Barta{ - - class BartaMathLibrary : public MathLibraryInterface{ - public: - - std::unique_ptr createQuadraticEquation( float A, float B, float C ) const override; - }; -} \ No newline at end of file diff --git a/include/Geometrics/Math/MathLibraryInterface.h b/include/Geometrics/Math/MathLibraryInterface.h deleted file mode 100644 index 088f0be..0000000 --- a/include/Geometrics/Math/MathLibraryInterface.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "../../pch.h" -#include"QuadraticEquation.h" - -namespace Barta{ - - class MathLibraryInterface{ - public: - virtual ~MathLibraryInterface() noexcept = default; - /* - Ax^2 + Bx + C = 0 - */ - virtual std::unique_ptr createQuadraticEquation( float A, float B, float C ) const = 0; - }; -} \ No newline at end of file diff --git a/include/Utilities/MathUtilities.h b/include/Utilities/MathUtilities.h new file mode 100644 index 0000000..c74b0ec --- /dev/null +++ b/include/Utilities/MathUtilities.h @@ -0,0 +1,14 @@ +#pragma once +#include "../Geometrics/Math/EquationInterface.h" +#include "../Geometrics/Math/QuadraticEquation.h" +#include "../pch.h" + +namespace Barta::Utils { +std::unique_ptr createQuadraticEquation( + float A, + float B, + float C +) { + return std::unique_ptr(new QuadraticEquation(A, B, C)); +} +} diff --git a/lib/Collisions/CheckCollisionVisitors/AABB_AABBCheckCollisionVisitor.cpp b/lib/Collisions/CheckCollisionVisitors/AABB_AABBCheckCollisionVisitor.cpp index 4f97bbc..2aa91f1 100644 --- a/lib/Collisions/CheckCollisionVisitors/AABB_AABBCheckCollisionVisitor.cpp +++ b/lib/Collisions/CheckCollisionVisitors/AABB_AABBCheckCollisionVisitor.cpp @@ -10,7 +10,6 @@ Barta::AABB_AABBCheckCollisionVisitor::AABB_AABBCheckCollisionVisitor( dynamicsDifference(dynamicsDifference) {} Barta::CollisionTestResult Barta::AABB_AABBCheckCollisionVisitor::checkStaticCollision( - const MathLibraryInterface& mathLib, CollisionTestResultBuilder& collisionTestResultBuilder ) const { std::stringstream ss; @@ -30,11 +29,10 @@ Barta::CollisionTestResult Barta::AABB_AABBCheckCollisionVisitor::checkStaticCol } Barta::CollisionTestResult Barta::AABB_AABBCheckCollisionVisitor::checkDynamicCollision( - const MathLibraryInterface& mathLib, const float delta_time, CollisionTestResultBuilder& collisionTestResultBuilder ) const { - auto staticCollisionResult = this->checkStaticCollision(mathLib, collisionTestResultBuilder); + auto staticCollisionResult = this->checkStaticCollision(collisionTestResultBuilder); if (staticCollisionResult.collisionDetected) { return staticCollisionResult; } diff --git a/lib/Collisions/CheckCollisionVisitors/CircleAABBCheckCollisionVisitor.cpp b/lib/Collisions/CheckCollisionVisitors/CircleAABBCheckCollisionVisitor.cpp index 1d3c46a..2cf80b4 100644 --- a/lib/Collisions/CheckCollisionVisitors/CircleAABBCheckCollisionVisitor.cpp +++ b/lib/Collisions/CheckCollisionVisitors/CircleAABBCheckCollisionVisitor.cpp @@ -37,7 +37,6 @@ Barta::Vector2f Barta::CircleAABBCheckCollisionVisitor::calculateCollisionPoint( } Barta::CollisionTestResult Barta::CircleAABBCheckCollisionVisitor::checkStaticCollision( - const MathLibraryInterface& mathLib, CollisionTestResultBuilder& collisionTestResultBuilder ) const { std::stringstream ss; @@ -73,11 +72,10 @@ Barta::CollisionTestResult Barta::CircleAABBCheckCollisionVisitor::checkStaticCo #pragma GCC diagnostic pop Barta::CollisionTestResult Barta::CircleAABBCheckCollisionVisitor::checkDynamicCollision( - const MathLibraryInterface& mathLib, const float delta_time, CollisionTestResultBuilder& collisionTestResultBuilder ) const { - auto staticResult = this->checkStaticCollision(mathLib, collisionTestResultBuilder); + auto staticResult = this->checkStaticCollision(collisionTestResultBuilder); if (staticResult.collisionDetected) { return staticResult; } diff --git a/lib/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.cpp b/lib/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.cpp index babbe0e..43ffb89 100644 --- a/lib/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.cpp +++ b/lib/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.cpp @@ -1,5 +1,6 @@ #include #include "pch.h" +#include Barta::CircleCircleCheckCollisionVisitor::CircleCircleCheckCollisionVisitor( const Circle& circle1, @@ -16,7 +17,6 @@ Barta::CircleCircleCheckCollisionVisitor::~CircleCircleCheckCollisionVisitor() { #pragma GCC diagnostic ignored "-Wunused-parameter" Barta::CollisionTestResult Barta::CircleCircleCheckCollisionVisitor::checkStaticCollision( - const MathLibraryInterface& mathLib, CollisionTestResultBuilder& collisionTestResultBuilder ) const { std::stringstream ss; @@ -38,11 +38,10 @@ Barta::CollisionTestResult Barta::CircleCircleCheckCollisionVisitor::checkStatic #pragma GCC diagnostic pop Barta::CollisionTestResult Barta::CircleCircleCheckCollisionVisitor::checkDynamicCollision( - const MathLibraryInterface& mathLib, const float delta_time, CollisionTestResultBuilder& collisionTestResultBuilder ) const { - auto staticResult = this->checkStaticCollision(mathLib, collisionTestResultBuilder); + auto staticResult = this->checkStaticCollision(collisionTestResultBuilder); if (staticResult.collisionDetected) { return staticResult; } @@ -52,7 +51,7 @@ Barta::CollisionTestResult Barta::CircleCircleCheckCollisionVisitor::checkDynami auto v = dynamicsDifference.velocity + 0.5f * dynamicsDifference.acceleration * delta_time; auto r = circle1.getRadius() + circle2.getRadius(); - auto eq = mathLib.createQuadraticEquation(v * v, v * s * 2.f, s * s - r * r); + auto eq = Barta::Utils::createQuadraticEquation(v * v, v * s * 2.f, s * s - r * r); eq->solve(); if (eq->getState() != EquationInterface::State::FINITE_NO_SOLTIONS) { diff --git a/lib/Collisions/CheckCollisionVisitors/OBB_AABBCheckCollisionVisitor.cpp b/lib/Collisions/CheckCollisionVisitors/OBB_AABBCheckCollisionVisitor.cpp index 1b78cd8..62f0dfc 100644 --- a/lib/Collisions/CheckCollisionVisitors/OBB_AABBCheckCollisionVisitor.cpp +++ b/lib/Collisions/CheckCollisionVisitors/OBB_AABBCheckCollisionVisitor.cpp @@ -55,7 +55,6 @@ bool Barta::OBB_AABBCheckCollisionVisitor::checkStaticOneWay( } Barta::CollisionTestResult Barta::OBB_AABBCheckCollisionVisitor::checkStaticCollision( - const MathLibraryInterface& mathLib, CollisionTestResultBuilder& collisionTestResultBuilder ) const { std::stringstream ss; @@ -80,7 +79,6 @@ Barta::CollisionTestResult Barta::OBB_AABBCheckCollisionVisitor::checkStaticColl } Barta::CollisionTestResult Barta::OBB_AABBCheckCollisionVisitor::checkDynamicCollision( - const MathLibraryInterface& mathLib, float deltaTime, CollisionTestResultBuilder& collisionTestResultBuilder ) const { diff --git a/lib/Collisions/CollisionDetectionStrategies/DynamicCollisionDetectionStrategy.cpp b/lib/Collisions/CollisionDetectionStrategies/DynamicCollisionDetectionStrategy.cpp index 7434742..fc48df4 100644 --- a/lib/Collisions/CollisionDetectionStrategies/DynamicCollisionDetectionStrategy.cpp +++ b/lib/Collisions/CollisionDetectionStrategies/DynamicCollisionDetectionStrategy.cpp @@ -1,16 +1,15 @@ -#include "pch.h" #include +#include "pch.h" Barta::DynamicCollisionDetectionStrategy::DynamicCollisionDetectionStrategy( - std::unique_ptr mathLibrary, const TimerInterface& timer -) : - timer( timer ), - mathLibrary(std::move(mathLibrary)) - {} +): + timer(timer) {} -Barta::CollisionTestResult Barta::DynamicCollisionDetectionStrategy::acceptCheckCollisionVisitor( const CheckCollisionVisitorInterface& checkCollisionVisitor ) const{ +Barta::CollisionTestResult Barta::DynamicCollisionDetectionStrategy::acceptCheckCollisionVisitor( + const CheckCollisionVisitorInterface& checkCollisionVisitor +) const { auto builder = CollisionTestResultBuilder(); - return checkCollisionVisitor.checkDynamicCollision(*this->mathLibrary, this->timer.getCurrentDeltaTime(), builder); + return checkCollisionVisitor.checkDynamicCollision(this->timer.getCurrentDeltaTime(), builder); } diff --git a/lib/Collisions/CollisionDetectionStrategies/StaticCollisionDetectionStrategy.cpp b/lib/Collisions/CollisionDetectionStrategies/StaticCollisionDetectionStrategy.cpp index 907d41a..8c922ec 100644 --- a/lib/Collisions/CollisionDetectionStrategies/StaticCollisionDetectionStrategy.cpp +++ b/lib/Collisions/CollisionDetectionStrategies/StaticCollisionDetectionStrategy.cpp @@ -1,13 +1,13 @@ -#include "pch.h" #include +#include "pch.h" #include -Barta::StaticCollisionDetectionStrategy::StaticCollisionDetectionStrategy( std::unique_ptr mathLibrary ) : - mathLibrary(std::move(mathLibrary)) -{} +Barta::StaticCollisionDetectionStrategy::StaticCollisionDetectionStrategy() {} -Barta::CollisionTestResult Barta::StaticCollisionDetectionStrategy::acceptCheckCollisionVisitor( const CheckCollisionVisitorInterface& checkCollisionVisitor ) const{ +Barta::CollisionTestResult Barta::StaticCollisionDetectionStrategy::acceptCheckCollisionVisitor( + const CheckCollisionVisitorInterface& checkCollisionVisitor +) const { auto builder = CollisionTestResultBuilder(); - return checkCollisionVisitor.checkStaticCollision(*this->mathLibrary, builder); + return checkCollisionVisitor.checkStaticCollision(builder); } diff --git a/lib/Geometrics/Math/BartaMathLibrary.cpp b/lib/Geometrics/Math/BartaMathLibrary.cpp deleted file mode 100644 index e606ca9..0000000 --- a/lib/Geometrics/Math/BartaMathLibrary.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include - -std::unique_ptr Barta::BartaMathLibrary::createQuadraticEquation( float A, float B, float C ) const{ - return std::unique_ptr( new QuadraticEquation( A, B, C ) ); -} diff --git a/lib/Geometrics/Math/CMakeLists.txt b/lib/Geometrics/Math/CMakeLists.txt index d5e84aa..b0a7bba 100644 --- a/lib/Geometrics/Math/CMakeLists.txt +++ b/lib/Geometrics/Math/CMakeLists.txt @@ -1,4 +1,3 @@ -target_sources(${LIBRARY_NAME} PUBLIC - BartaMathLibrary.cpp - QuadraticEquation.cpp -) \ No newline at end of file +target_sources( + ${LIBRARY_NAME} PUBLIC QuadraticEquation.cpp +) From f00b3237373a7a4001b641cbc15ddd71e75ab2b4 Mon Sep 17 00:00:00 2001 From: Dawid Zapolski Date: Mon, 11 Nov 2024 12:26:53 +0100 Subject: [PATCH 2/2] Moved QuadraticEquation and EquationInterface to Utilities --- include/Geometrics/Intersections.h | 111 ++++++++++-------- include/Geometrics/Math/EquationInterface.h | 32 ----- include/Geometrics/Math/QuadraticEquation.h | 31 ----- include/Utilities/EquationInterface.h | 31 +++++ include/Utilities/MathUtilities.h | 7 +- include/Utilities/QuadraticEquation.h | 30 +++++ lib/CMakeLists.txt | 1 + .../CircleCircleCheckCollisionVisitor.cpp | 8 +- lib/Geometrics/CMakeLists.txt | 1 - lib/Geometrics/Math/CMakeLists.txt | 3 - lib/Geometrics/Math/QuadraticEquation.cpp | 103 ---------------- lib/Utilities/CMakeLists.txt | 3 + lib/Utilities/QuadraticEquation.cpp | 108 +++++++++++++++++ 13 files changed, 239 insertions(+), 230 deletions(-) delete mode 100644 include/Geometrics/Math/EquationInterface.h delete mode 100644 include/Geometrics/Math/QuadraticEquation.h create mode 100644 include/Utilities/EquationInterface.h create mode 100644 include/Utilities/QuadraticEquation.h delete mode 100644 lib/Geometrics/Math/CMakeLists.txt delete mode 100644 lib/Geometrics/Math/QuadraticEquation.cpp create mode 100644 lib/Utilities/CMakeLists.txt create mode 100644 lib/Utilities/QuadraticEquation.cpp diff --git a/include/Geometrics/Intersections.h b/include/Geometrics/Intersections.h index d9b2d96..db13084 100644 --- a/include/Geometrics/Intersections.h +++ b/include/Geometrics/Intersections.h @@ -1,69 +1,76 @@ #pragma once -#include "Segment.h" +#include "../Utilities/QuadraticEquation.h" #include "BartaShapes/AABB.h" #include "BartaShapes/Circle.h" -#include "Math/QuadraticEquation.h" +#include "Segment.h" namespace Barta { - namespace Intersections { - auto segmentAndCircle(const Segment& I, const Circle& c) noexcept { - auto d = I.getEnd() - I.getBeginning(); - auto m = I.getBeginning() - c.getCenter(); - auto eq = QuadraticEquation(d*d, m*d*2.f, m*m - c.getRadius()*c.getRadius()); - eq.solve(); - auto sol = eq.getSolutions(); +namespace Intersections { + auto segmentAndCircle( + const Segment& I, + const Circle& c + ) noexcept { + auto d = I.getEnd() - I.getBeginning(); + auto m = I.getBeginning() - c.getCenter(); + auto eq = Utils::QuadraticEquation(d * d, m * d * 2.f, m * m - c.getRadius() * c.getRadius()); + eq.solve(); + auto sol = eq.getSolutions(); - sol.erase(std::remove_if(sol.begin(), sol.end(), [] (float t) { - return t < 0.f || 1.f < t; - }), sol.end()); + sol.erase(std::remove_if(sol.begin(), sol.end(), [](float t) { return t < 0.f || 1.f < t; }), sol.end()); - return sol; - } + return sol; + } - float lineAndLineRespectToFirst(const Segment& I1, const Segment& I2) noexcept { - auto perpendicular = (I2.getEnd() - I2.getBeginning()).perpendicular(); - auto denom = (perpendicular * (I1.getEnd() - I1.getBeginning())); - if (denom == 0.f) { - return std::numeric_limits::infinity(); - } + float lineAndLineRespectToFirst( + const Segment& I1, + const Segment& I2 + ) noexcept { + auto perpendicular = (I2.getEnd() - I2.getBeginning()).perpendicular(); + auto denom = (perpendicular * (I1.getEnd() - I1.getBeginning())); + if (denom == 0.f) { + return std::numeric_limits::infinity(); + } - return (perpendicular * (I2.getBeginning() - I1.getBeginning())) / denom; - } + return (perpendicular * (I2.getBeginning() - I1.getBeginning())) / denom; + } - auto segmentAndSegment(const Segment& I1, const Segment& I2) noexcept { - std::vector> result; - auto barCoords= std::tuple( - lineAndLineRespectToFirst(I1, I2), - lineAndLineRespectToFirst(I2, I1) - ); + auto segmentAndSegment( + const Segment& I1, + const Segment& I2 + ) noexcept { + std::vector> result; + auto barCoords = std::tuple(lineAndLineRespectToFirst(I1, I2), lineAndLineRespectToFirst(I2, I1)); - if (0.f <= std::get<0>(barCoords) && std::get<0>(barCoords) <= 1.f && 0.f <= std::get<1>(barCoords) && std::get<1>(barCoords) <= 1.f) { - result.push_back(barCoords); - } + if (0.f <= std::get<0>(barCoords) && std::get<0>(barCoords) <= 1.f && 0.f <= std::get<1>(barCoords) && std::get<1>(barCoords) <= 1.f) { + result.push_back(barCoords); + } - return result; - } + return result; + } - auto segmentAndAABB(const Segment& I, const AABB& aabb) { - decltype(segmentAndSegment(I, I)) intersections; - for (const auto& side : aabb.getSides()) { - auto sideIntersections = segmentAndSegment(I, side); - intersections.insert(intersections.end(), sideIntersections.begin(), sideIntersections.end()); - } + auto segmentAndAABB( + const Segment& I, + const AABB& aabb + ) { + decltype(segmentAndSegment(I, I)) intersections; + for (const auto& side: aabb.getSides()) { + auto sideIntersections = segmentAndSegment(I, side); + intersections.insert(intersections.end(), sideIntersections.begin(), sideIntersections.end()); + } - if (intersections.size() == 2) { - if (std::get<0>(intersections[0]) > std::get<0>(intersections[1])) { - auto temp = intersections[0]; - intersections[0] = intersections[1]; - intersections[1] = temp; - } + if (intersections.size() == 2) { + if (std::get<0>(intersections[0]) > std::get<0>(intersections[1])) { + auto temp = intersections[0]; + intersections[0] = intersections[1]; + intersections[1] = temp; } + } - if (intersections.size() > 2) { - throw std::runtime_error("size too long"); - } + if (intersections.size() > 2) { + throw std::runtime_error("size too long"); + } - return intersections; - } - } -} \ No newline at end of file + return intersections; + } +} +} diff --git a/include/Geometrics/Math/EquationInterface.h b/include/Geometrics/Math/EquationInterface.h deleted file mode 100644 index 8614df2..0000000 --- a/include/Geometrics/Math/EquationInterface.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "../../pch.h" - -namespace Barta{ - - class EquationInterface{ - public: - - enum class Method{ - DEFAULT = 0 - }; - - enum class State{ - UNSOLVED = 0, - NO_SOLUTIONS, - FINITE_NO_SOLTIONS, - INFINITE_NO_SOLUTIONS - }; - - EquationInterface() noexcept = default; - EquationInterface( const EquationInterface& ) noexcept = default; - EquationInterface( EquationInterface&& ) noexcept = default; - EquationInterface& operator=( EquationInterface&& ) noexcept = default; - virtual ~EquationInterface() noexcept = default; - - virtual void solve( const Method& method = Method::DEFAULT ) = 0; - - virtual State getState() const noexcept = 0; - - virtual std::vector getSolutions() const = 0; - }; -} \ No newline at end of file diff --git a/include/Geometrics/Math/QuadraticEquation.h b/include/Geometrics/Math/QuadraticEquation.h deleted file mode 100644 index 597a95a..0000000 --- a/include/Geometrics/Math/QuadraticEquation.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include "../../pch.h" -#include"EquationInterface.h" - -namespace Barta{ - - class QuadraticEquation : public EquationInterface{ - public: - - QuadraticEquation( float A, float B, float C ) noexcept; - QuadraticEquation( const QuadraticEquation& ) noexcept; - QuadraticEquation( QuadraticEquation&& ) noexcept; - QuadraticEquation& operator=( QuadraticEquation&& ) noexcept; - ~QuadraticEquation() noexcept; - - void solve( const Method & method = Method::DEFAULT ); - - State getState() const noexcept; - - std::vector getSolutions() const; - - private: - void solveLinear(); - - EquationInterface::State state; - - std::vector solutions; - - float A, B, C; - }; -} \ No newline at end of file diff --git a/include/Utilities/EquationInterface.h b/include/Utilities/EquationInterface.h new file mode 100644 index 0000000..531989a --- /dev/null +++ b/include/Utilities/EquationInterface.h @@ -0,0 +1,31 @@ +#pragma once +#include "../pch.h" + +namespace Barta::Utils { + +class EquationInterface { +public: + enum class Method { + DEFAULT = 0 + }; + + enum class State { + UNSOLVED = 0, + NO_SOLUTIONS, + FINITE_NO_SOLTIONS, + INFINITE_NO_SOLUTIONS + }; + + EquationInterface() noexcept = default; + EquationInterface(const EquationInterface&) noexcept = default; + EquationInterface(EquationInterface&&) noexcept = default; + EquationInterface& operator=(EquationInterface&&) noexcept = default; + virtual ~EquationInterface() noexcept = default; + + virtual void solve(const Method& method = Method::DEFAULT) = 0; + + virtual State getState() const noexcept = 0; + + virtual std::vector getSolutions() const = 0; +}; +} diff --git a/include/Utilities/MathUtilities.h b/include/Utilities/MathUtilities.h index c74b0ec..e8ef04e 100644 --- a/include/Utilities/MathUtilities.h +++ b/include/Utilities/MathUtilities.h @@ -1,14 +1,13 @@ #pragma once -#include "../Geometrics/Math/EquationInterface.h" -#include "../Geometrics/Math/QuadraticEquation.h" #include "../pch.h" +#include "QuadraticEquation.h" namespace Barta::Utils { -std::unique_ptr createQuadraticEquation( +QuadraticEquation createQuadraticEquation( float A, float B, float C ) { - return std::unique_ptr(new QuadraticEquation(A, B, C)); + return QuadraticEquation(A, B, C); } } diff --git a/include/Utilities/QuadraticEquation.h b/include/Utilities/QuadraticEquation.h new file mode 100644 index 0000000..a36c26c --- /dev/null +++ b/include/Utilities/QuadraticEquation.h @@ -0,0 +1,30 @@ +#pragma once +#include "../pch.h" +#include "EquationInterface.h" + +namespace Barta::Utils { + +class QuadraticEquation: public virtual EquationInterface { +public: + QuadraticEquation(float A, float B, float C) noexcept; + QuadraticEquation(const QuadraticEquation&) noexcept; + QuadraticEquation(QuadraticEquation&&) noexcept; + QuadraticEquation& operator=(QuadraticEquation&&) noexcept; + ~QuadraticEquation() noexcept; + + void solve(const Method& method = Method::DEFAULT); + + State getState() const noexcept; + + std::vector getSolutions() const; + +private: + void solveLinear(); + + EquationInterface::State state; + + std::vector solutions; + + float A, B, C; +}; +} diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index c7aedf0..6f03df0 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -24,6 +24,7 @@ add_subdirectory(Geometrics) add_subdirectory(Graphics) add_subdirectory(Hitbox) add_subdirectory(Objects) +add_subdirectory(Utilities) target_include_directories(${LIBRARY_NAME} PUBLIC "${PROJECT_SOURCE_DIR}/include") diff --git a/lib/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.cpp b/lib/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.cpp index 43ffb89..bcf7a79 100644 --- a/lib/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.cpp +++ b/lib/Collisions/CheckCollisionVisitors/CircleCircleCheckCollisionVisitor.cpp @@ -51,14 +51,14 @@ Barta::CollisionTestResult Barta::CircleCircleCheckCollisionVisitor::checkDynami auto v = dynamicsDifference.velocity + 0.5f * dynamicsDifference.acceleration * delta_time; auto r = circle1.getRadius() + circle2.getRadius(); - auto eq = Barta::Utils::createQuadraticEquation(v * v, v * s * 2.f, s * s - r * r); - eq->solve(); + auto eq = Utils::createQuadraticEquation(v * v, v * s * 2.f, s * s - r * r); + eq.solve(); - if (eq->getState() != EquationInterface::State::FINITE_NO_SOLTIONS) { + if (eq.getState() != Utils::EquationInterface::State::FINITE_NO_SOLTIONS) { return collisionTestResultBuilder.setCollisionDetected(false)->setDebugInfo("Circle - Circle dynamic, moving parallelly")->build(); } - auto solution = eq->getSolutions()[0]; + auto solution = eq.getSolutions()[0]; return collisionTestResultBuilder.setNormVector(this->getNormalVector()) ->setCollisionDetected(0.f <= solution && solution <= delta_time) diff --git a/lib/Geometrics/CMakeLists.txt b/lib/Geometrics/CMakeLists.txt index cb3e724..78c0fe7 100644 --- a/lib/Geometrics/CMakeLists.txt +++ b/lib/Geometrics/CMakeLists.txt @@ -1,5 +1,4 @@ add_subdirectory(BartaShapes) -add_subdirectory(Math) target_sources(${LIBRARY_NAME} PUBLIC Segment.cpp diff --git a/lib/Geometrics/Math/CMakeLists.txt b/lib/Geometrics/Math/CMakeLists.txt deleted file mode 100644 index b0a7bba..0000000 --- a/lib/Geometrics/Math/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -target_sources( - ${LIBRARY_NAME} PUBLIC QuadraticEquation.cpp -) diff --git a/lib/Geometrics/Math/QuadraticEquation.cpp b/lib/Geometrics/Math/QuadraticEquation.cpp deleted file mode 100644 index f4ca0d2..0000000 --- a/lib/Geometrics/Math/QuadraticEquation.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include -#include - -Barta::QuadraticEquation::QuadraticEquation( float A, float B, float C ) noexcept : - state( EquationInterface::State::UNSOLVED ), - solutions({}), - A( A ), - B( B ), - C( C ) -{} - -Barta::QuadraticEquation::QuadraticEquation( const QuadraticEquation& second ) noexcept : - state( second.state ), - solutions( second.solutions ), - A( second.A ), - B( second.B ), - C( second.C ) -{} - -Barta::QuadraticEquation::QuadraticEquation( QuadraticEquation&& second ) noexcept : - state( second.state ), - solutions( std::move( second.solutions ) ), - A( second.A ), - B( second.B ), - C( second.C ) -{} - -Barta::QuadraticEquation& Barta::QuadraticEquation::operator=( QuadraticEquation&& second ) noexcept{ - this->solutions = std::move( second.solutions ); - this->state = second.state; - this->A = second.A; - this->B = second.B; - this->C = second.C; - - return *this; -} - -Barta::QuadraticEquation::~QuadraticEquation() noexcept{} - -void Barta::QuadraticEquation::solve( const Method& method ){ - if (method != Method::DEFAULT) { - return; - } - - if( this->state != EquationInterface::State::UNSOLVED ){ - return; - } - - if( this->A == 0.f ){ - this->solveLinear(); - - return; - } - - auto p = - this->B / this->A * 0.5f; - auto q = this->C / this->A; - auto delta = p * p - q; - if( delta < 0.f ){ - this->state = EquationInterface::State::NO_SOLUTIONS; - - return; - } - - this->solutions.resize( 2 ); - this->state = EquationInterface::State::FINITE_NO_SOLTIONS; - delta = sqrt( delta ); - if( p >= 0.f ){ - this->solutions[ 1 ] = p + delta; - this->solutions[ 0 ] = q / this->solutions[ 1 ]; - } - else{ - this->solutions[ 0 ] = p - delta; - this->solutions[ 1 ] = q / this->solutions[ 0 ]; - } -} - -Barta::EquationInterface::State Barta::QuadraticEquation::getState() const noexcept{ - return this->state; -} - -std::vector Barta::QuadraticEquation::getSolutions() const{ - return this->solutions; -} - -void Barta::QuadraticEquation::solveLinear(){ - if( this->B == 0.f ){ - if( this->C != 0.f ){ - this->state = EquationInterface::State::NO_SOLUTIONS; - } - else{ - this->state = EquationInterface::State::INFINITE_NO_SOLUTIONS; - } - } - else{ - this->solutions.resize( 1 ); - this->solutions[ 0 ] = - this->C / this->B; - this->state = EquationInterface::State::FINITE_NO_SOLTIONS; - } -} - - - - diff --git a/lib/Utilities/CMakeLists.txt b/lib/Utilities/CMakeLists.txt new file mode 100644 index 0000000..e5f0492 --- /dev/null +++ b/lib/Utilities/CMakeLists.txt @@ -0,0 +1,3 @@ +target_sources(${LIBRARY_NAME} PUBLIC + QuadraticEquation.cpp +) diff --git a/lib/Utilities/QuadraticEquation.cpp b/lib/Utilities/QuadraticEquation.cpp new file mode 100644 index 0000000..be09ded --- /dev/null +++ b/lib/Utilities/QuadraticEquation.cpp @@ -0,0 +1,108 @@ +#include +#include + +namespace Barta::Utils { + +QuadraticEquation::QuadraticEquation( + float A, + float B, + float C +) noexcept: + state(EquationInterface::State::UNSOLVED), + solutions({}), + A(A), + B(B), + C(C) {} + +QuadraticEquation::QuadraticEquation( + const QuadraticEquation& second +) noexcept: + state(second.state), + solutions(second.solutions), + A(second.A), + B(second.B), + C(second.C) {} + +QuadraticEquation::QuadraticEquation( + QuadraticEquation&& second +) noexcept: + state(second.state), + solutions(std::move(second.solutions)), + A(second.A), + B(second.B), + C(second.C) {} + +QuadraticEquation& QuadraticEquation::operator=( + QuadraticEquation&& second +) noexcept { + this->solutions = std::move(second.solutions); + this->state = second.state; + this->A = second.A; + this->B = second.B; + this->C = second.C; + + return *this; +} + +QuadraticEquation::~QuadraticEquation() noexcept {} + +void QuadraticEquation::solve( + const Method& method +) { + if (method != Method::DEFAULT) { + return; + } + + if (this->state != EquationInterface::State::UNSOLVED) { + return; + } + + if (this->A == 0.f) { + this->solveLinear(); + + return; + } + + auto p = -this->B / this->A * 0.5f; + auto q = this->C / this->A; + auto delta = p * p - q; + if (delta < 0.f) { + this->state = EquationInterface::State::NO_SOLUTIONS; + + return; + } + + this->solutions.resize(2); + this->state = EquationInterface::State::FINITE_NO_SOLTIONS; + delta = sqrt(delta); + if (p >= 0.f) { + this->solutions[1] = p + delta; + this->solutions[0] = q / this->solutions[1]; + } else { + this->solutions[0] = p - delta; + this->solutions[1] = q / this->solutions[0]; + } +} + +EquationInterface::State QuadraticEquation::getState() const noexcept { + return this->state; +} + +std::vector QuadraticEquation::getSolutions() const { + return this->solutions; +} + +void QuadraticEquation::solveLinear() { + if (this->B == 0.f) { + if (this->C != 0.f) { + this->state = EquationInterface::State::NO_SOLUTIONS; + } else { + this->state = EquationInterface::State::INFINITE_NO_SOLUTIONS; + } + } else { + this->solutions.resize(1); + this->solutions[0] = -this->C / this->B; + this->state = EquationInterface::State::FINITE_NO_SOLTIONS; + } +} +}