mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 23:13:26 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 31907ceca3 | |||
| afcfed4834 | |||
| 835fd110ba | |||
| 29629a737d | |||
| 6a324e8c0e | |||
| 42c84f2523 | |||
| 9a4fb67289 | |||
| 824b301d40 | |||
| dcf466316c | |||
| dc0bca14c5 | |||
| b2db06a739 |
12
README.md
12
README.md
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
Oranges's Math Library (omath) is a comprehensive, open-source library aimed at providing efficient, reliable, and versatile mathematical functions and algorithms. Developed primarily in C++, this library is designed to cater to a wide range of mathematical operations essential in scientific computing, engineering, and academic research.
|
Oranges's Math Library (omath) is a comprehensive, open-source library aimed at providing efficient, reliable, and versatile mathematical functions and algorithms. Developed primarily in C++, this library is designed to cater to a wide range of mathematical operations essential in scientific computing, engineering, and academic research.
|
||||||
|
|
||||||
## Features
|
## 👁🗨 Features
|
||||||
- **Efficiency**: Optimized for performance, ensuring quick computations.
|
- **Efficiency**: Optimized for performance, ensuring quick computations.
|
||||||
- **Versatility**: Includes a wide array of mathematical functions and algorithms.
|
- **Versatility**: Includes a wide array of mathematical functions and algorithms.
|
||||||
- **Ease of Use**: Simplified interface for convenient integration into various projects.
|
- **Ease of Use**: Simplified interface for convenient integration into various projects.
|
||||||
@@ -20,7 +20,7 @@ Oranges's Math Library (omath) is a comprehensive, open-source library aimed at
|
|||||||
- **Collision Detection**: Production ready code to handle collision detection by using simple interfaces.
|
- **Collision Detection**: Production ready code to handle collision detection by using simple interfaces.
|
||||||
- **No Additional Dependencies**: No additional dependencies need to use OMath except unit test execution
|
- **No Additional Dependencies**: No additional dependencies need to use OMath except unit test execution
|
||||||
|
|
||||||
## Getting Started
|
## ⏬ Getting Started
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
- C++ Compiler
|
- C++ Compiler
|
||||||
- CMake (for building the project)
|
- CMake (for building the project)
|
||||||
@@ -54,7 +54,7 @@ For detailed commands on installing different versions and more information, ple
|
|||||||
cmake --build cmake-build/build/windows-release --target server -j 6
|
cmake --build cmake-build/build/windows-release --target server -j 6
|
||||||
```
|
```
|
||||||
Use **\<platform\>-\<build configuration\>** preset to build siutable version for yourself. Like **windows-release** or **linux-release**.
|
Use **\<platform\>-\<build configuration\>** preset to build siutable version for yourself. Like **windows-release** or **linux-release**.
|
||||||
## Usage
|
## ❔ Usage
|
||||||
Simple world to screen function
|
Simple world to screen function
|
||||||
```c++
|
```c++
|
||||||
TEST(UnitTestProjection, IsPointOnScreen)
|
TEST(UnitTestProjection, IsPointOnScreen)
|
||||||
@@ -76,11 +76,11 @@ With `omath/projection` module you can achieve simple ESP hack for powered by So
|
|||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
## Contributing
|
## 🫵🏻 Contributing
|
||||||
Contributions to `omath` are welcome! Please read `CONTRIBUTING.md` for details on our code of conduct and the process for submitting pull requests.
|
Contributions to `omath` are welcome! Please read `CONTRIBUTING.md` for details on our code of conduct and the process for submitting pull requests.
|
||||||
|
|
||||||
## License
|
## 📜 License
|
||||||
This project is licensed under the MIT - see the `LICENSE` file for details.
|
This project is licensed under the MIT - see the `LICENSE` file for details.
|
||||||
|
|
||||||
## Acknowledgments
|
## 💘 Acknowledgments
|
||||||
- [All contributors](https://github.com/orange-cpp/omath/graphs/contributors)
|
- [All contributors](https://github.com/orange-cpp/omath/graphs/contributors)
|
||||||
|
|||||||
@@ -38,10 +38,7 @@ namespace omath
|
|||||||
|
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
float& operator[](size_t row, size_t column)
|
float& operator[](size_t row, size_t column);
|
||||||
{
|
|
||||||
return At(row, column);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
size_t ColumnsCount() const noexcept;
|
size_t ColumnsCount() const noexcept;
|
||||||
|
|||||||
74
include/omath/Triangle.hpp
Normal file
74
include/omath/Triangle.hpp
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
//
|
||||||
|
// Created by Orange on 11/13/2024.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include "omath/Vector3.hpp"
|
||||||
|
|
||||||
|
namespace omath
|
||||||
|
{
|
||||||
|
template<class Vector>
|
||||||
|
class Triangle final
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
constexpr Triangle() = default;
|
||||||
|
constexpr Triangle(const Vector& vertex1, const Vector& vertex2, const Vector& vertex3)
|
||||||
|
: m_vertex1(vertex1), m_vertex2(vertex2), m_vertex3(vertex3)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3 m_vertex1;
|
||||||
|
Vector3 m_vertex2;
|
||||||
|
Vector3 m_vertex3;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr Vector3 CalculateNormal() const
|
||||||
|
{
|
||||||
|
const auto b = SideBVector();
|
||||||
|
const auto a = SideAVector();
|
||||||
|
return b.Cross(a).Normalized();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
float SideALength() const
|
||||||
|
{
|
||||||
|
return m_vertex1.DistTo(m_vertex2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
float SideBLength() const
|
||||||
|
{
|
||||||
|
return m_vertex3.DistTo(m_vertex2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr Vector3 SideAVector() const
|
||||||
|
{
|
||||||
|
return m_vertex1 - m_vertex2;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr float Hypot() const
|
||||||
|
{
|
||||||
|
return m_vertex1.DistTo(m_vertex3);
|
||||||
|
}
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr bool IsRectangular() const
|
||||||
|
{
|
||||||
|
const auto sideA = SideALength();
|
||||||
|
const auto sideB = SideBLength();
|
||||||
|
const auto hypot = Hypot();
|
||||||
|
|
||||||
|
return std::abs(sideA*sideA + sideB*sideB - hypot*hypot) <= 0.0001f;
|
||||||
|
}
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr Vector3 SideBVector() const
|
||||||
|
{
|
||||||
|
return m_vertex3 - m_vertex2;
|
||||||
|
}
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr Vector3 MidPoint() const
|
||||||
|
{
|
||||||
|
return (m_vertex1 + m_vertex2 + m_vertex3) / 3;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace omath
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by Orange on 11/13/2024.
|
|
||||||
//
|
|
||||||
#pragma once
|
|
||||||
#include "omath/Vector3.hpp"
|
|
||||||
|
|
||||||
namespace omath
|
|
||||||
{
|
|
||||||
class Triangle3d final
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Triangle3d(const Vector3& vertex1, const Vector3& vertex2, const Vector3& vertex3);
|
|
||||||
|
|
||||||
Vector3 m_vertex1;
|
|
||||||
Vector3 m_vertex2;
|
|
||||||
Vector3 m_vertex3;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
Vector3 CalculateNormal() const;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
float SideALength() const;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
float SideBLength() const;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
Vector3 SideAVector() const;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
Vector3 SideBVector() const;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -7,9 +7,18 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "omath/Vector2.hpp"
|
#include "omath/Vector2.hpp"
|
||||||
|
#include "omath/Angle.hpp"
|
||||||
|
#include <expected>
|
||||||
|
|
||||||
|
|
||||||
namespace omath
|
namespace omath
|
||||||
{
|
{
|
||||||
|
|
||||||
|
enum class Vector3Error
|
||||||
|
{
|
||||||
|
IMPOSSIBLE_BETWEEN_ANGLE,
|
||||||
|
};
|
||||||
|
|
||||||
class Vector3 : public Vector2
|
class Vector3 : public Vector2
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -208,6 +217,17 @@ namespace omath
|
|||||||
return Sum2D() + z;
|
return Sum2D() + z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::expected<Angle<float, 0.f, 180.f, AngleFlags::Clamped>, Vector3Error>
|
||||||
|
AngleBetween(const Vector3& other) const
|
||||||
|
{
|
||||||
|
const auto bottom = Length() * other.Length();
|
||||||
|
|
||||||
|
if (bottom == 0.f)
|
||||||
|
return std::unexpected(Vector3Error::IMPOSSIBLE_BETWEEN_ANGLE);
|
||||||
|
|
||||||
|
return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::FromRadians(std::acos(Dot(other) / bottom));
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] constexpr float Sum2D() const
|
[[nodiscard]] constexpr float Sum2D() const
|
||||||
{
|
{
|
||||||
return Vector2::Sum();
|
return Vector2::Sum();
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "omath/Vector3.hpp"
|
#include "omath/Vector3.hpp"
|
||||||
#include "omath/Triangle3d.hpp"
|
#include "omath/Triangle.hpp"
|
||||||
|
|
||||||
namespace omath::collision
|
namespace omath::collision
|
||||||
{
|
{
|
||||||
@@ -27,12 +27,12 @@ namespace omath::collision
|
|||||||
|
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static bool CanTraceLine(const Ray& ray, const Triangle3d& triangle);
|
static bool CanTraceLine(const Ray& ray, const Triangle<Vector3>& triangle);
|
||||||
|
|
||||||
|
|
||||||
// Realization of Möller–Trumbore intersection algorithm
|
// Realization of Möller–Trumbore intersection algorithm
|
||||||
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static Vector3 GetRayHitPoint(const Ray& ray, const Triangle3d& triangle);
|
static Vector3 GetRayHitPoint(const Ray& ray, const Triangle<Vector3>& triangle);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ target_sources(omath PRIVATE
|
|||||||
color.cpp
|
color.cpp
|
||||||
Vector4.cpp
|
Vector4.cpp
|
||||||
Vector2.cpp
|
Vector2.cpp
|
||||||
Triangle3d.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
add_subdirectory(prediction)
|
add_subdirectory(prediction)
|
||||||
|
|||||||
@@ -75,6 +75,11 @@ namespace omath
|
|||||||
return m_rows;
|
return m_rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float& Matrix::operator[](const size_t row, const size_t column)
|
||||||
|
{
|
||||||
|
return At(row, column);
|
||||||
|
}
|
||||||
|
|
||||||
Matrix::Matrix(Matrix&& other) noexcept
|
Matrix::Matrix(Matrix&& other) noexcept
|
||||||
{
|
{
|
||||||
m_rows = other.m_rows;
|
m_rows = other.m_rows;
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
#include "omath/Triangle3d.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
namespace omath
|
|
||||||
{
|
|
||||||
Triangle3d::Triangle3d(const Vector3 &vertex1, const Vector3 &vertex2, const Vector3 &vertex3)
|
|
||||||
: m_vertex1(vertex1), m_vertex2(vertex2), m_vertex3(vertex3)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector3 Triangle3d::CalculateNormal() const
|
|
||||||
{
|
|
||||||
return (m_vertex1 - m_vertex2).Cross(m_vertex3 - m_vertex1).Normalized();
|
|
||||||
}
|
|
||||||
|
|
||||||
float Triangle3d::SideALength() const
|
|
||||||
{
|
|
||||||
return m_vertex1.DistTo(m_vertex2);
|
|
||||||
}
|
|
||||||
|
|
||||||
float Triangle3d::SideBLength() const
|
|
||||||
{
|
|
||||||
return m_vertex3.DistTo(m_vertex2);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector3 Triangle3d::SideAVector() const
|
|
||||||
{
|
|
||||||
return m_vertex1 - m_vertex2;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector3 Triangle3d::SideBVector() const
|
|
||||||
{
|
|
||||||
return m_vertex3 - m_vertex2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
namespace omath::collision
|
namespace omath::collision
|
||||||
{
|
{
|
||||||
bool LineTracer::CanTraceLine(const Ray &ray, const Triangle3d &triangle)
|
bool LineTracer::CanTraceLine(const Ray& ray, const Triangle<Vector3>& triangle)
|
||||||
{
|
{
|
||||||
return GetRayHitPoint(ray, triangle) == ray.end;
|
return GetRayHitPoint(ray, triangle) == ray.end;
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@ namespace omath::collision
|
|||||||
return DirectionVector().Normalized();
|
return DirectionVector().Normalized();
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 LineTracer::GetRayHitPoint(const Ray &ray, const Triangle3d &triangle)
|
Vector3 LineTracer::GetRayHitPoint(const Ray& ray, const Triangle<Vector3>& triangle)
|
||||||
{
|
{
|
||||||
constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
|
constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
|
||||||
|
|
||||||
@@ -59,4 +59,4 @@ namespace omath::collision
|
|||||||
|
|
||||||
return ray.start + rayDir * tHit;
|
return ray.start + rayDir * tHit;
|
||||||
}
|
}
|
||||||
}
|
} // namespace omath::collision
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ add_executable(unit-tests
|
|||||||
general/UnitTestAngles.cpp
|
general/UnitTestAngles.cpp
|
||||||
general/UnitTestViewAngles.cpp
|
general/UnitTestViewAngles.cpp
|
||||||
general/UnitTestAngle.cpp
|
general/UnitTestAngle.cpp
|
||||||
|
general/UnitTestTriangle.cpp
|
||||||
|
|
||||||
engines/UnitTestOpenGL.cpp
|
engines/UnitTestOpenGL.cpp
|
||||||
engines/UnitTestUnityEngine.cpp
|
engines/UnitTestUnityEngine.cpp
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "omath/collision/LineTracer.hpp"
|
#include "omath/collision/LineTracer.hpp"
|
||||||
#include "omath/Triangle3d.hpp"
|
#include "omath/Triangle.hpp"
|
||||||
#include "omath/Vector3.hpp"
|
#include "omath/Vector3.hpp"
|
||||||
|
|
||||||
using namespace omath;
|
using namespace omath;
|
||||||
@@ -13,7 +13,7 @@ protected:
|
|||||||
Vector3 vertex1{0.0f, 0.0f, 0.0f};
|
Vector3 vertex1{0.0f, 0.0f, 0.0f};
|
||||||
Vector3 vertex2{1.0f, 0.0f, 0.0f};
|
Vector3 vertex2{1.0f, 0.0f, 0.0f};
|
||||||
Vector3 vertex3{0.0f, 1.0f, 0.0f};
|
Vector3 vertex3{0.0f, 1.0f, 0.0f};
|
||||||
Triangle3d triangle{vertex1, vertex2, vertex3};
|
Triangle<Vector3> triangle{vertex1, vertex2, vertex3};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Test that a ray intersecting the triangle returns false for CanTraceLine
|
// Test that a ray intersecting the triangle returns false for CanTraceLine
|
||||||
@@ -71,7 +71,7 @@ TEST_F(LineTracerTest, TriangleFarBeyondRayEndPoint)
|
|||||||
constexpr Ray ray{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
|
constexpr Ray ray{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
|
||||||
|
|
||||||
// Define a triangle far beyond the ray's endpoint
|
// Define a triangle far beyond the ray's endpoint
|
||||||
const Triangle3d distantTriangle{
|
constexpr Triangle<Vector3> distantTriangle{
|
||||||
{1000.0f, 1000.0f, 1000.0f}, {1001.0f, 1000.0f, 1000.0f}, {1000.0f, 1001.0f, 1000.0f}
|
{1000.0f, 1000.0f, 1000.0f}, {1001.0f, 1000.0f, 1000.0f}, {1000.0f, 1001.0f, 1000.0f}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
132
tests/general/UnitTestTriangle.cpp
Normal file
132
tests/general/UnitTestTriangle.cpp
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
//
|
||||||
|
// Created by Orange on 1/6/2025.
|
||||||
|
//
|
||||||
|
#include "omath/Triangle.hpp"
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <omath/Vector3.hpp>
|
||||||
|
#include <cmath> // For std::sqrt, std::isinf, std::isnan
|
||||||
|
|
||||||
|
|
||||||
|
using namespace omath;
|
||||||
|
|
||||||
|
class UnitTestTriangle : public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
// Define some Triangles to use in tests
|
||||||
|
Triangle<Vector3> t1;
|
||||||
|
Triangle<Vector3> t2;
|
||||||
|
Triangle<Vector3> t3;
|
||||||
|
|
||||||
|
constexpr void SetUp() override
|
||||||
|
{
|
||||||
|
// Triangle with vertices (0, 0, 0), (1, 0, 0), (0, 1, 0)
|
||||||
|
t1 = Triangle<Vector3>(
|
||||||
|
Vector3(0.0f, 0.0f, 0.0f),
|
||||||
|
Vector3(1.0f, 0.0f, 0.0f),
|
||||||
|
Vector3(0.0f, 1.0f, 0.0f)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Triangle with vertices (1, 2, 3), (4, 5, 6), (7, 8, 9)
|
||||||
|
t2 = Triangle<Vector3>(
|
||||||
|
Vector3(1.0f, 2.0f, 3.0f),
|
||||||
|
Vector3(4.0f, 5.0f, 6.0f),
|
||||||
|
Vector3(7.0f, 8.0f, 9.0f)
|
||||||
|
);
|
||||||
|
|
||||||
|
// An isosceles right triangle
|
||||||
|
t3 = Triangle<Vector3>(
|
||||||
|
Vector3(0.0f, 0.0f, 0.0f),
|
||||||
|
Vector3(2.0f, 0.0f, 0.0f),
|
||||||
|
Vector3(0.0f, 2.0f, 0.0f)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test constructor and vertices
|
||||||
|
TEST_F(UnitTestTriangle, Constructor)
|
||||||
|
{
|
||||||
|
constexpr Triangle<Vector3> t(
|
||||||
|
Vector3(1.0f, 2.0f, 3.0f),
|
||||||
|
Vector3(4.0f, 5.0f, 6.0f),
|
||||||
|
Vector3(7.0f, 8.0f, 9.0f)
|
||||||
|
);
|
||||||
|
|
||||||
|
EXPECT_FLOAT_EQ(t.m_vertex1.x, 1.0f);
|
||||||
|
EXPECT_FLOAT_EQ(t.m_vertex1.y, 2.0f);
|
||||||
|
EXPECT_FLOAT_EQ(t.m_vertex1.z, 3.0f);
|
||||||
|
|
||||||
|
EXPECT_FLOAT_EQ(t.m_vertex2.x, 4.0f);
|
||||||
|
EXPECT_FLOAT_EQ(t.m_vertex2.y, 5.0f);
|
||||||
|
EXPECT_FLOAT_EQ(t.m_vertex2.z, 6.0f);
|
||||||
|
|
||||||
|
EXPECT_FLOAT_EQ(t.m_vertex3.x, 7.0f);
|
||||||
|
EXPECT_FLOAT_EQ(t.m_vertex3.y, 8.0f);
|
||||||
|
EXPECT_FLOAT_EQ(t.m_vertex3.z, 9.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test CalculateNormal
|
||||||
|
TEST_F(UnitTestTriangle, CalculateNormal)
|
||||||
|
{
|
||||||
|
// For t1, the normal should point in the +Z direction (0, 0, 1) or (0, 0, -1)
|
||||||
|
const Vector3 normal_t1 = t1.CalculateNormal();
|
||||||
|
// Check if it's normalized and pointed along Z (sign can differ, so use absolute check)
|
||||||
|
EXPECT_NEAR(std::fabs(normal_t1.z), 1.0f, 1e-5f);
|
||||||
|
EXPECT_NEAR(normal_t1.Length(), 1.0f, 1e-5f);
|
||||||
|
|
||||||
|
|
||||||
|
// For t3, we expect the normal to be along +Z as well
|
||||||
|
const Vector3 normal_t3 = t3.CalculateNormal();
|
||||||
|
EXPECT_NEAR(std::fabs(normal_t3.z), 1.0f, 1e-5f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test side lengths
|
||||||
|
TEST_F(UnitTestTriangle, SideLengths)
|
||||||
|
{
|
||||||
|
// For t1 side lengths
|
||||||
|
EXPECT_FLOAT_EQ(t1.SideALength(), std::sqrt(1.0f)); // distance between (0,0,0) and (1,0,0)
|
||||||
|
EXPECT_FLOAT_EQ(t1.SideBLength(), std::sqrt(1.0f + 1.0f)); // distance between (4,5,6) & (7,8,9)... but we are testing t1, so let's be accurate:
|
||||||
|
// Actually, for t1: vertex2=(1,0,0), vertex3=(0,1,0)
|
||||||
|
// Dist between (0,1,0) and (1,0,0) = sqrt((1-0)^2 + (0-1)^2) = sqrt(1 + 1) = sqrt(2)
|
||||||
|
EXPECT_FLOAT_EQ(t1.SideBLength(), std::sqrt(2.0f));
|
||||||
|
|
||||||
|
// For t3, side a = distance between vertex1=(0,0,0) and vertex2=(2,0,0), which is 2
|
||||||
|
// side b = distance between vertex3=(0,2,0) and vertex2=(2,0,0), which is sqrt(2^2 + (-2)^2)= sqrt(8)= 2.828...
|
||||||
|
// We'll just check side a first:
|
||||||
|
EXPECT_FLOAT_EQ(t3.SideALength(), 2.0f);
|
||||||
|
// Then side b:
|
||||||
|
EXPECT_FLOAT_EQ(t3.SideBLength(), std::sqrt(8.0f));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test side vectors
|
||||||
|
TEST_F(UnitTestTriangle, SideVectors)
|
||||||
|
{
|
||||||
|
const Vector3 sideA_t1 = t1.SideAVector(); // m_vertex1 - m_vertex2
|
||||||
|
EXPECT_FLOAT_EQ(sideA_t1.x, 0.0f - 1.0f);
|
||||||
|
EXPECT_FLOAT_EQ(sideA_t1.y, 0.0f - 0.0f);
|
||||||
|
EXPECT_FLOAT_EQ(sideA_t1.z, 0.0f - 0.0f);
|
||||||
|
|
||||||
|
const Vector3 sideB_t1 = t1.SideBVector(); // m_vertex3 - m_vertex2
|
||||||
|
EXPECT_FLOAT_EQ(sideB_t1.x, 0.0f - 1.0f);
|
||||||
|
EXPECT_FLOAT_EQ(sideB_t1.y, 1.0f - 0.0f);
|
||||||
|
EXPECT_FLOAT_EQ(sideB_t1.z, 0.0f - 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(UnitTestTriangle, IsRectangular)
|
||||||
|
{
|
||||||
|
EXPECT_TRUE(Triangle<Vector3>({2,0,0}, {}, {0,2,0}).IsRectangular());
|
||||||
|
}
|
||||||
|
// Test midpoint
|
||||||
|
TEST_F(UnitTestTriangle, MidPoint)
|
||||||
|
{
|
||||||
|
// For t1, midpoint of (0,0,0), (1,0,0), (0,1,0)
|
||||||
|
const Vector3 mid1 = t1.MidPoint();
|
||||||
|
EXPECT_FLOAT_EQ(mid1.x, (0.0f + 1.0f + 0.0f) / 3.0f);
|
||||||
|
EXPECT_FLOAT_EQ(mid1.y, (0.0f + 0.0f + 1.0f) / 3.0f);
|
||||||
|
EXPECT_FLOAT_EQ(mid1.z, 0.0f);
|
||||||
|
|
||||||
|
// For t2, midpoint of (1,2,3), (4,5,6), (7,8,9)
|
||||||
|
const Vector3 mid2 = t2.MidPoint();
|
||||||
|
EXPECT_FLOAT_EQ(mid2.x, (1.0f + 4.0f + 7.0f) / 3.0f);
|
||||||
|
EXPECT_FLOAT_EQ(mid2.y, (2.0f + 5.0f + 8.0f) / 3.0f);
|
||||||
|
EXPECT_FLOAT_EQ(mid2.z, (3.0f + 6.0f + 9.0f) / 3.0f);
|
||||||
|
}
|
||||||
@@ -387,6 +387,15 @@ TEST_F(UnitTestVector3, AsTuple)
|
|||||||
EXPECT_FLOAT_EQ(std::get<2>(tuple), v1.z);
|
EXPECT_FLOAT_EQ(std::get<2>(tuple), v1.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test AsTuple method
|
||||||
|
TEST_F(UnitTestVector3, AngleBeatween)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(Vector3(0.0f, 0.0f, 1.0f).AngleBetween({1, 0 ,0}).value().AsDegrees(), 90.0f);
|
||||||
|
EXPECT_EQ(Vector3(0.0f, 0.0f, 1.0f).AngleBetween({0.0f, 0.0f, 1.0f}).value().AsDegrees(), 0.0f);
|
||||||
|
EXPECT_FALSE(Vector3(0.0f, 0.0f, 0.0f).AngleBetween({0.0f, 0.0f, 1.0f}).has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Static assertions (compile-time checks)
|
// Static assertions (compile-time checks)
|
||||||
static_assert(Vector3(1.0f, 2.0f, 3.0f).LengthSqr() == 14.0f, "LengthSqr should be 14");
|
static_assert(Vector3(1.0f, 2.0f, 3.0f).LengthSqr() == 14.0f, "LengthSqr should be 14");
|
||||||
static_assert(Vector3(1.0f, 2.0f, 3.0f).Dot(Vector3(4.0f, 5.0f, 6.0f)) == 32.0f, "Dot product should be 32");
|
static_assert(Vector3(1.0f, 2.0f, 3.0f).Dot(Vector3(4.0f, 5.0f, 6.0f)) == 32.0f, "Dot product should be 32");
|
||||||
|
|||||||
Reference in New Issue
Block a user