mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-14 15:33:26 +00:00
Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 244d01c313 | |||
| e31ffac103 | |||
| ae87257adf | |||
| 906f5099d1 | |||
| 96e4e1c9d6 | |||
| 872dbe146f | |||
| e0dcb65e3f | |||
| d0c532df39 | |||
| 31907ceca3 | |||
| afcfed4834 | |||
| 835fd110ba | |||
| 29629a737d | |||
| 6a324e8c0e | |||
| 42c84f2523 | |||
| 9a4fb67289 | |||
| 824b301d40 | |||
| dcf466316c | |||
| dc0bca14c5 | |||
| b2db06a739 | |||
| cdce07f2a5 | |||
| 931937d010 | |||
| af880be056 | |||
| c3bfd12971 | |||
| 9ad8ec755b | |||
| 409cd18de6 | |||
| 001c6a8a47 | |||
| 30bc3f2c6d | |||
| 232f456a4d | |||
| 2b9575311e | |||
| e497308349 | |||
| 390e584aee | |||
| 93082d6a8d | |||
| c8c3c4e5fc | |||
|
|
24ad4ac698 | ||
| 221fd54f5a | |||
| 5f8fc4058c | |||
|
|
fc164b339b | ||
|
|
58fb6a0304 | ||
|
|
193e87847a | ||
|
|
075c553521 |
1
.idea/vcs.xml
generated
1
.idea/vcs.xml
generated
@@ -2,7 +2,6 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="" vcs="Git" />
|
<mapping directory="" vcs="Git" />
|
||||||
<mapping directory="$PROJECT_DIR$/extlibs/glm" vcs="Git" />
|
|
||||||
<mapping directory="$PROJECT_DIR$/extlibs/googletest" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$/extlibs/googletest" vcs="Git" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -1,28 +1,41 @@
|
|||||||
cmake_minimum_required(VERSION 3.26)
|
cmake_minimum_required(VERSION 3.26)
|
||||||
|
|
||||||
project(omath VERSION 1.0.0)
|
project(omath VERSION 1.0.1)
|
||||||
|
|
||||||
|
include(CMakePackageConfigHelpers)
|
||||||
|
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 26)
|
|
||||||
|
|
||||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
|
||||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
|
||||||
|
|
||||||
option(OMATH_BUILD_TESTS "Build unit tests" ON)
|
option(OMATH_BUILD_TESTS "Build unit tests" ON)
|
||||||
option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON)
|
option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON)
|
||||||
option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF)
|
option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF)
|
||||||
|
|
||||||
|
|
||||||
if (OMATH_BUILD_AS_SHARED_LIBRARY)
|
if (OMATH_BUILD_AS_SHARED_LIBRARY)
|
||||||
add_library(omath SHARED source/Vector3.cpp)
|
add_library(omath SHARED source/Vector3.cpp)
|
||||||
else()
|
else()
|
||||||
add_library(omath STATIC source/Vector3.cpp)
|
add_library(omath STATIC source/Vector3.cpp
|
||||||
|
include/omath/engines/OpenGL/Constants.hpp
|
||||||
|
include/omath/engines/OpenGL/Formulas.hpp
|
||||||
|
include/omath/engines/OpenGL/Camera.hpp)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
set_target_properties(omath PROPERTIES
|
||||||
|
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
|
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
|
UNITY_BUILD ON
|
||||||
|
UNITY_BUILD_BATCH_SIZE 20
|
||||||
|
CXX_STANDARD 23
|
||||||
|
CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
|
||||||
|
target_compile_features(omath PUBLIC cxx_std_23)
|
||||||
|
|
||||||
|
target_compile_definitions(omath PUBLIC OMATH_EXPORT)
|
||||||
|
|
||||||
add_subdirectory(source)
|
add_subdirectory(source)
|
||||||
add_subdirectory(extlibs)
|
|
||||||
|
|
||||||
if(OMATH_BUILD_TESTS)
|
if(OMATH_BUILD_TESTS)
|
||||||
|
add_subdirectory(extlibs)
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|||||||
28
README.md
28
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,12 +20,26 @@ 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)
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
### vcpkg
|
||||||
|
**Note**: Support vcpkg for package management
|
||||||
|
1. Install vcpkg (https://github.com/microsoft/vcpkg)
|
||||||
|
2. Run the following command to install the orange-math package:
|
||||||
|
```
|
||||||
|
vcpkg install orange-math
|
||||||
|
```
|
||||||
|
CMakeLists.txt
|
||||||
|
```cmake
|
||||||
|
find_package(omath CONFIG REQUIRED)
|
||||||
|
target_link_libraries(main PRIVATE omath::omath)
|
||||||
|
```
|
||||||
|
For detailed commands on installing different versions and more information, please refer to Microsoft's official instructions (https://learn.microsoft.com/en-us/vcpkg/get_started/overview)
|
||||||
|
### Build from source
|
||||||
1. Clone the repository:
|
1. Clone the repository:
|
||||||
```
|
```
|
||||||
git clone https://github.com/orange-cpp/omath.git
|
git clone https://github.com/orange-cpp/omath.git
|
||||||
@@ -40,7 +54,7 @@ Oranges's Math Library (omath) is a comprehensive, open-source library aimed at
|
|||||||
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)
|
||||||
@@ -62,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
|
||||||
- Orange | [Telegram](https://t.me/orange_cpp)
|
- [All contributors](https://github.com/orange-cpp/omath/graphs/contributors)
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ include(CMakeFindDependencyMacro)
|
|||||||
|
|
||||||
# Load the targets for the omath library
|
# Load the targets for the omath library
|
||||||
include("${CMAKE_CURRENT_LIST_DIR}/omathTargets.cmake")
|
include("${CMAKE_CURRENT_LIST_DIR}/omathTargets.cmake")
|
||||||
|
check_required_components(omath)
|
||||||
|
|||||||
@@ -4,10 +4,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "omath/Angles.hpp"
|
#include "omath/Angles.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
namespace omath
|
namespace omath
|
||||||
{
|
{
|
||||||
enum class AngleFlags
|
enum class AngleFlags
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
#include <numbers>
|
#include <numbers>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
|
|
||||||
namespace omath::angles
|
namespace omath::angles
|
||||||
{
|
{
|
||||||
template<class Type>
|
template<class Type>
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include "omath/Vector4.hpp"
|
#include "omath/Vector4.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace omath
|
namespace omath
|
||||||
{
|
{
|
||||||
struct HSV
|
struct HSV
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include "Vector3.hpp"
|
#include "Vector3.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace omath
|
namespace omath
|
||||||
{
|
{
|
||||||
struct MatSize
|
struct MatSize
|
||||||
@@ -23,6 +22,13 @@ namespace omath
|
|||||||
COLUMN_MAJOR
|
COLUMN_MAJOR
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename M1, typename M2>
|
||||||
|
concept MatTemplateEqual =
|
||||||
|
(M1::rows == M2::rows) && (M1::columns == M2::columns) &&
|
||||||
|
std::is_same_v<typename M1::value_type, typename M2::value_type> &&
|
||||||
|
(M1::store_type == M2::store_type);
|
||||||
|
|
||||||
template<size_t Rows = 0, size_t Columns = 0, class Type = float, MatStoreType StoreType = MatStoreType::ROW_MAJOR>
|
template<size_t Rows = 0, size_t Columns = 0, class Type = float, MatStoreType StoreType = MatStoreType::ROW_MAJOR>
|
||||||
requires std::is_arithmetic_v<Type>
|
requires std::is_arithmetic_v<Type>
|
||||||
class Mat final
|
class Mat final
|
||||||
@@ -66,6 +72,11 @@ namespace omath
|
|||||||
m_data = other.m_data;
|
m_data = other.m_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr Type& operator[](const size_t row, const size_t col)
|
||||||
|
{
|
||||||
|
return At(row, col);
|
||||||
|
}
|
||||||
|
|
||||||
constexpr Mat(Mat&& other) noexcept
|
constexpr Mat(Mat&& other) noexcept
|
||||||
{
|
{
|
||||||
m_data = std::move(other.m_data);
|
m_data = std::move(other.m_data);
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ namespace omath
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
size_t RowCount() const noexcept;
|
size_t RowCount() const noexcept;
|
||||||
|
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
float& operator[](size_t row, size_t 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;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,6 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
|
|
||||||
namespace omath
|
namespace omath
|
||||||
{
|
{
|
||||||
class Vector2
|
class Vector2
|
||||||
|
|||||||
@@ -7,10 +7,19 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "omath/Vector2.hpp"
|
#include "omath/Vector2.hpp"
|
||||||
|
#include "omath/Angle.hpp"
|
||||||
|
#include <expected>
|
||||||
|
#include <immintrin.h>
|
||||||
|
|
||||||
|
|
||||||
namespace omath
|
namespace omath
|
||||||
{
|
{
|
||||||
|
|
||||||
|
enum class Vector3Error
|
||||||
|
{
|
||||||
|
IMPOSSIBLE_BETWEEN_ANGLE,
|
||||||
|
};
|
||||||
|
|
||||||
class Vector3 : public Vector2
|
class Vector3 : public Vector2
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -209,6 +218,25 @@ 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]] bool IsPerpendicular(const Vector3& other) const
|
||||||
|
{
|
||||||
|
if (const auto angle = AngleBetween(other))
|
||||||
|
return angle->AsDegrees() == 90.f;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] constexpr float Sum2D() const
|
[[nodiscard]] constexpr float Sum2D() const
|
||||||
{
|
{
|
||||||
return Vector2::Sum();
|
return Vector2::Sum();
|
||||||
@@ -216,7 +244,7 @@ namespace omath
|
|||||||
|
|
||||||
[[nodiscard]] Vector3 ViewAngleTo(const Vector3& other) const;
|
[[nodiscard]] Vector3 ViewAngleTo(const Vector3& other) const;
|
||||||
|
|
||||||
[[nodiscard]] std::tuple<float, float, float> AsTuple() const
|
[[nodiscard]] constexpr std::tuple<float, float, float> AsTuple() const
|
||||||
{
|
{
|
||||||
return std::make_tuple(x, y, z);
|
return std::make_tuple(x, y, z);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
#include <omath/Vector3.hpp>
|
#include <omath/Vector3.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
namespace omath
|
namespace omath
|
||||||
{
|
{
|
||||||
class Vector4 : public Vector3
|
class Vector4 : public Vector3
|
||||||
|
|||||||
@@ -4,8 +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
|
||||||
{
|
{
|
||||||
@@ -28,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);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
19
include/omath/engines/OpenGL/Camera.hpp
Normal file
19
include/omath/engines/OpenGL/Camera.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Created by Orange on 12/23/2024.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include "Constants.hpp"
|
||||||
|
#include "omath/projection/Camera.hpp"
|
||||||
|
|
||||||
|
namespace omath::opengl
|
||||||
|
{
|
||||||
|
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||||
|
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||||
|
void LookAt(const Vector3& target) override;
|
||||||
|
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||||
|
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||||
|
};
|
||||||
|
}
|
||||||
25
include/omath/engines/OpenGL/Constants.hpp
Normal file
25
include/omath/engines/OpenGL/Constants.hpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
//
|
||||||
|
// Created by Orange on 12/23/2024.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <omath/Vector3.hpp>
|
||||||
|
#include <omath/Mat.hpp>
|
||||||
|
#include <omath/Angle.hpp>
|
||||||
|
#include <omath/ViewAngles.hpp>
|
||||||
|
|
||||||
|
namespace omath::opengl
|
||||||
|
{
|
||||||
|
constexpr Vector3 kAbsUp = {0, 1, 0};
|
||||||
|
constexpr Vector3 kAbsRight = {1, 0, 0};
|
||||||
|
constexpr Vector3 kAbsForward = {0, 0, -1};
|
||||||
|
|
||||||
|
using Mat4x4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||||
|
using Mat3x3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||||
|
using Mat1x3 = Mat<1, 3, float, MatStoreType::COLUMN_MAJOR>;
|
||||||
|
using PitchAngle = Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
|
||||||
|
using YawAngle = Angle<float, 0.f, 360.f, AngleFlags::Normalized>;
|
||||||
|
using RollAngle = Angle<float, 0.f, 360.f, AngleFlags::Normalized>;
|
||||||
|
|
||||||
|
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
|
||||||
|
}
|
||||||
54
include/omath/engines/OpenGL/Formulas.hpp
Normal file
54
include/omath/engines/OpenGL/Formulas.hpp
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
//
|
||||||
|
// Created by Orange on 12/23/2024.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include "Constants.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace omath::opengl
|
||||||
|
{
|
||||||
|
[[nodiscard]]
|
||||||
|
inline Vector3 ForwardVector(const ViewAngles& angles)
|
||||||
|
{
|
||||||
|
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward);
|
||||||
|
|
||||||
|
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
inline Vector3 RightVector(const ViewAngles& angles)
|
||||||
|
{
|
||||||
|
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight);
|
||||||
|
|
||||||
|
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
inline Vector3 UpVector(const ViewAngles& angles)
|
||||||
|
{
|
||||||
|
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp);
|
||||||
|
|
||||||
|
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin)
|
||||||
|
{
|
||||||
|
return MatCameraView<float, MatStoreType::COLUMN_MAJOR>(-ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
inline Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||||
|
const float far)
|
||||||
|
{
|
||||||
|
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||||
|
|
||||||
|
return {
|
||||||
|
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||||
|
{0, 1.f / (fovHalfTan), 0, 0},
|
||||||
|
{0, 0, -(far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||||
|
{0, 0, -1, 0},
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
// Created by Orange on 12/4/2024.
|
// Created by Orange on 12/4/2024.
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "Constants.h"
|
#include "Constants.hpp"
|
||||||
#include "omath/projection/Camera.hpp"
|
#include "omath/projection/Camera.hpp"
|
||||||
|
|
||||||
namespace omath::source
|
namespace omath::source
|
||||||
@@ -11,9 +11,9 @@ namespace omath::source
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||||
const Angle<float, 0, 180, AngleFlags::Clamped>& fov, float near, float far);
|
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||||
void LookAt(const Vector3& target) override;
|
void LookAt(const Vector3& target) override;
|
||||||
[[nodiscard]] Mat4x4 GetViewMatrix() const override;
|
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||||
[[nodiscard]] Mat4x4 GetProjectionMatrix() const override;
|
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
// Created by Orange on 12/4/2024.
|
// Created by Orange on 12/4/2024.
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "Constants.h"
|
#include "Constants.hpp"
|
||||||
|
|
||||||
namespace omath::source
|
namespace omath::source
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,14 +7,19 @@
|
|||||||
#include "NavigationMesh.hpp"
|
#include "NavigationMesh.hpp"
|
||||||
#include "omath/Vector3.hpp"
|
#include "omath/Vector3.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace omath::pathfinding
|
namespace omath::pathfinding
|
||||||
{
|
{
|
||||||
|
struct PathNode;
|
||||||
class Astar final
|
class Astar final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static std::vector<Vector3> FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh);
|
static std::vector<Vector3> FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh);
|
||||||
|
private:
|
||||||
|
[[nodiscard]]
|
||||||
|
static std::vector<Vector3> ReconstructFinalPath(const std::unordered_map<Vector3, PathNode>& closedList, const Vector3& current);
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
static auto GetPerfectNode(const std::unordered_map<Vector3, PathNode>& openList, const Vector3& endVertex);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
namespace omath::pathfinding
|
namespace omath::pathfinding
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
#include "omath/prediction/Projectile.hpp"
|
#include "omath/prediction/Projectile.hpp"
|
||||||
#include "omath/prediction/Target.hpp"
|
#include "omath/prediction/Target.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace omath::prediction
|
namespace omath::prediction
|
||||||
{
|
{
|
||||||
class Engine final
|
class Engine final
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "omath/Vector3.hpp"
|
#include "omath/Vector3.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace omath::prediction
|
namespace omath::prediction
|
||||||
{
|
{
|
||||||
class Projectile final
|
class Projectile final
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "omath/Vector3.hpp"
|
#include "omath/Vector3.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace omath::prediction
|
namespace omath::prediction
|
||||||
{
|
{
|
||||||
class Target final
|
class Target final
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
#include <omath/Angle.hpp>
|
#include <omath/Angle.hpp>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
|
|
||||||
namespace omath::projection
|
namespace omath::projection
|
||||||
{
|
{
|
||||||
class ViewPort final
|
class ViewPort final
|
||||||
@@ -25,12 +24,11 @@ namespace omath::projection
|
|||||||
return m_width / m_height;
|
return m_width / m_height;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
using FieldOfView = const Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
|
using FieldOfView = Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
|
||||||
|
|
||||||
template<class Mat4x4Type, class ViewAnglesType>
|
template<class Mat4x4Type, class ViewAnglesType>
|
||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~Camera() = default;
|
virtual ~Camera() = default;
|
||||||
Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort,
|
Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort,
|
||||||
@@ -43,18 +41,82 @@ namespace omath::projection
|
|||||||
|
|
||||||
virtual void LookAt(const Vector3& target) = 0;
|
virtual void LookAt(const Vector3& target) = 0;
|
||||||
|
|
||||||
[[nodiscard]] virtual Mat4x4Type GetViewMatrix() const = 0;
|
[[nodiscard]] virtual Mat4x4Type CalcViewMatrix() const = 0;
|
||||||
|
|
||||||
[[nodiscard]] virtual Mat4x4Type GetProjectionMatrix() const = 0;
|
[[nodiscard]] virtual Mat4x4Type CalcProjectionMatrix() const = 0;
|
||||||
|
|
||||||
[[nodiscard]] Mat4x4Type GetViewProjectionMatrix()
|
[[nodiscard]] Mat4x4Type CalcViewProjectionMatrix() const
|
||||||
{
|
{
|
||||||
return GetProjectionMatrix() * GetViewMatrix();
|
return CalcProjectionMatrix() * CalcViewMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Mat4x4Type& viewProj, const Vector3& worldPosition) const
|
void SetFieldOfView(const FieldOfView& fov)
|
||||||
{
|
{
|
||||||
auto projected = viewProj * MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
|
m_fieldOfView = fov;
|
||||||
|
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetNearPlane(const float near)
|
||||||
|
{
|
||||||
|
m_nearPlaneDistance = near;
|
||||||
|
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetFarPlane(const float far)
|
||||||
|
{
|
||||||
|
m_farPlaneDistance = far;
|
||||||
|
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetViewAngles(const ViewAnglesType& viewAngles)
|
||||||
|
{
|
||||||
|
m_viewAngles = viewAngles;
|
||||||
|
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetOrigin(const Vector3& origin)
|
||||||
|
{
|
||||||
|
m_origin = origin;
|
||||||
|
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetViewPort(const ViewPort& viewPort)
|
||||||
|
{
|
||||||
|
m_viewPort = viewPort;
|
||||||
|
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] const FieldOfView& GetFieldOfView() const
|
||||||
|
{
|
||||||
|
return m_fieldOfView;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] const float& GetNearPlane() const
|
||||||
|
{
|
||||||
|
return m_nearPlaneDistance;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] const float& GetFarPlane() const
|
||||||
|
{
|
||||||
|
return m_farPlaneDistance;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] const ViewAnglesType& GetViewAngles() const
|
||||||
|
{
|
||||||
|
return m_viewAngles;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] const Vector3& GetOrigin() const
|
||||||
|
{
|
||||||
|
return m_origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Vector3& worldPosition) const
|
||||||
|
{
|
||||||
|
if (!m_viewProjectionMatrix.has_value())
|
||||||
|
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||||
|
|
||||||
|
auto projected = m_viewProjectionMatrix.value() * MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
|
||||||
|
|
||||||
if (projected.At(3, 0) == 0.0f)
|
if (projected.At(3, 0) == 0.0f)
|
||||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||||
@@ -64,13 +126,18 @@ namespace omath::projection
|
|||||||
if (IsNdcOutOfBounds(projected))
|
if (IsNdcOutOfBounds(projected))
|
||||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||||
|
|
||||||
return Vector3{(projected.At(0,0)+1) / 2 * m_viewPort.m_width , (-projected.At(1,0)+1) / 2 * m_viewPort.m_height, projected.At(2,0)};
|
const auto screenPositionX = (projected.At(0,0)+1.f) / 2.f * m_viewPort.m_width;
|
||||||
|
const auto screenPositionY = (-projected.At(1,0)+1) / 2.f * m_viewPort.m_height;
|
||||||
|
|
||||||
|
return Vector3{screenPositionX, screenPositionY, projected.At(2,0)};
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ViewPort m_viewPort{};
|
ViewPort m_viewPort{};
|
||||||
Angle<float, 0.f, 180.f, AngleFlags::Clamped> m_fieldOfView;
|
Angle<float, 0.f, 180.f, AngleFlags::Clamped> m_fieldOfView;
|
||||||
|
|
||||||
|
mutable std::optional<Mat4x4Type> m_viewProjectionMatrix;
|
||||||
|
|
||||||
float m_farPlaneDistance;
|
float m_farPlaneDistance;
|
||||||
float m_nearPlaneDistance;
|
float m_nearPlaneDistance;
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ namespace omath::collision
|
|||||||
const auto u = t.Dot(p) * invDet;
|
const auto u = t.Dot(p) * invDet;
|
||||||
|
|
||||||
|
|
||||||
if ((u < 0 && std::abs(u) > kEpsilon) || (u > 1 && std::abs(u-1) > kEpsilon))
|
if ((u < 0 && std::abs(u) > kEpsilon) || (u > 1 && std::abs(u - 1) > kEpsilon))
|
||||||
return ray.end;
|
return ray.end;
|
||||||
|
|
||||||
const auto q = t.Cross(sideA);
|
const auto q = t.Cross(sideA);
|
||||||
@@ -59,4 +59,4 @@ namespace omath::collision
|
|||||||
|
|
||||||
return ray.start + rayDir * tHit;
|
return ray.start + rayDir * tHit;
|
||||||
}
|
}
|
||||||
}
|
} // namespace omath::collision
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
add_subdirectory(Source)
|
add_subdirectory(Source)
|
||||||
|
add_subdirectory(OpenGL)
|
||||||
1
source/engines/OpenGL/CMakeLists.txt
Normal file
1
source/engines/OpenGL/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target_sources(omath PRIVATE Camera.cpp)
|
||||||
35
source/engines/OpenGL/Camera.cpp
Normal file
35
source/engines/OpenGL/Camera.cpp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
//
|
||||||
|
// Created by Orange on 12/23/2024.
|
||||||
|
//
|
||||||
|
#include "omath/engines/OpenGL/Camera.hpp"
|
||||||
|
#include "omath/engines/OpenGL/Formulas.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace omath::opengl
|
||||||
|
{
|
||||||
|
|
||||||
|
Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||||
|
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far) :
|
||||||
|
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void Camera::LookAt([[maybe_unused]] const Vector3& target)
|
||||||
|
{
|
||||||
|
const float distance = m_origin.DistTo(target);
|
||||||
|
const auto delta = target - m_origin;
|
||||||
|
|
||||||
|
|
||||||
|
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
|
||||||
|
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
|
||||||
|
m_viewAngles.roll = RollAngle::FromRadians(0.f);
|
||||||
|
}
|
||||||
|
Mat4x4 Camera::CalcViewMatrix() const
|
||||||
|
{
|
||||||
|
return opengl::CalcViewMatrix(m_viewAngles, m_origin);
|
||||||
|
}
|
||||||
|
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||||
|
{
|
||||||
|
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||||
|
m_farPlaneDistance);
|
||||||
|
}
|
||||||
|
} // namespace omath::opengl
|
||||||
@@ -24,13 +24,14 @@ namespace omath::source
|
|||||||
m_viewAngles.roll = RollAngle::FromRadians(0.f);
|
m_viewAngles.roll = RollAngle::FromRadians(0.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat4x4 Camera::GetViewMatrix() const
|
Mat4x4 Camera::CalcViewMatrix() const
|
||||||
{
|
{
|
||||||
return CalcViewMatrix(m_viewAngles, m_origin);
|
return source::CalcViewMatrix(m_viewAngles, m_origin);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat4x4 Camera::GetProjectionMatrix() const
|
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||||
{
|
{
|
||||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance, m_farPlaneDistance);
|
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||||
|
m_farPlaneDistance);
|
||||||
}
|
}
|
||||||
} // namespace omath::source
|
} // namespace omath::source
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
//
|
//
|
||||||
#include "omath/pathfinding/Astar.hpp"
|
#include "omath/pathfinding/Astar.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
|
|
||||||
namespace omath::pathfinding
|
namespace omath::pathfinding
|
||||||
@@ -18,45 +18,83 @@ namespace omath::pathfinding
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
std::vector<Vector3> Astar::FindPath(const Vector3 &start, const Vector3 &end, const NavigationMesh &navMesh)
|
std::vector<Vector3> Astar::ReconstructFinalPath(const std::unordered_map<Vector3, PathNode>& closedList,
|
||||||
|
const Vector3& current)
|
||||||
|
{
|
||||||
|
std::vector<Vector3> path;
|
||||||
|
std::optional currentOpt = current;
|
||||||
|
|
||||||
|
while (currentOpt)
|
||||||
|
{
|
||||||
|
path.push_back(*currentOpt);
|
||||||
|
|
||||||
|
auto it = closedList.find(*currentOpt);
|
||||||
|
|
||||||
|
if (it == closedList.end())
|
||||||
|
break;
|
||||||
|
|
||||||
|
currentOpt = it->second.cameFrom;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ranges::reverse(path);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
auto Astar::GetPerfectNode(const std::unordered_map<Vector3, PathNode>& openList, const Vector3& endVertex)
|
||||||
|
{
|
||||||
|
return std::ranges::min_element(openList,
|
||||||
|
[&endVertex](const auto& a, const auto& b)
|
||||||
|
{
|
||||||
|
const float fA = a.second.gCost + a.first.DistTo(endVertex);
|
||||||
|
const float fB = b.second.gCost + b.first.DistTo(endVertex);
|
||||||
|
return fA < fB;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Vector3> Astar::FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh)
|
||||||
{
|
{
|
||||||
std::unordered_map<Vector3, PathNode> closedList;
|
std::unordered_map<Vector3, PathNode> closedList;
|
||||||
std::unordered_map<Vector3, PathNode> openList;
|
std::unordered_map<Vector3, PathNode> openList;
|
||||||
|
|
||||||
const auto startVertex = navMesh.GetClosestVertex(start).value();
|
auto maybeStartVertex = navMesh.GetClosestVertex(start);
|
||||||
const auto endVertex = navMesh.GetClosestVertex(end).value();
|
auto maybeEndVertex = navMesh.GetClosestVertex(end);
|
||||||
|
|
||||||
|
if (!maybeStartVertex || !maybeEndVertex)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
const auto startVertex = maybeStartVertex.value();
|
||||||
|
const auto endVertex = maybeEndVertex.value();
|
||||||
|
|
||||||
|
|
||||||
openList.emplace(startVertex, PathNode{std::nullopt, 0.f});
|
openList.emplace(startVertex, PathNode{std::nullopt, 0.f});
|
||||||
|
|
||||||
while (!openList.empty())
|
while (!openList.empty())
|
||||||
{
|
{
|
||||||
const auto perfectVertex = *std::ranges::min_element(openList,
|
auto currentIt = GetPerfectNode(openList, endVertex);
|
||||||
[&endVertex](const auto& a, const auto& b) -> bool
|
|
||||||
|
const auto current = currentIt->first;
|
||||||
|
const auto currentNode = currentIt->second;
|
||||||
|
|
||||||
|
if (current == endVertex)
|
||||||
|
return ReconstructFinalPath(closedList, current);
|
||||||
|
|
||||||
|
|
||||||
|
closedList.emplace(current, currentNode);
|
||||||
|
openList.erase(currentIt);
|
||||||
|
|
||||||
|
for (const auto& neighbor: navMesh.GetNeighbors(current))
|
||||||
{
|
{
|
||||||
const auto aCost = a.second.gCost + a.first.DistTo(endVertex);
|
if (closedList.contains(neighbor))
|
||||||
const auto bCost = b.second.gCost + b.first.DistTo(endVertex);
|
|
||||||
return aCost < bCost;
|
|
||||||
});
|
|
||||||
|
|
||||||
closedList.emplace(perfectVertex);
|
|
||||||
openList.erase(perfectVertex.first);
|
|
||||||
|
|
||||||
for (const auto& neighbor : navMesh.GetNeighbors(perfectVertex.first))
|
|
||||||
if (!closedList.contains(neighbor))
|
|
||||||
openList.emplace(neighbor, PathNode{perfectVertex.first, neighbor.DistTo(perfectVertex.first) + perfectVertex.second.gCost});
|
|
||||||
|
|
||||||
|
|
||||||
if (perfectVertex.first != endVertex)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
std::vector<Vector3> path = {};
|
const float tentativeGCost = currentNode.gCost + neighbor.DistTo(current);
|
||||||
|
|
||||||
for (std::optional current = perfectVertex.first; current; current = closedList.at(*current).cameFrom )
|
const auto openIt = openList.find(neighbor);
|
||||||
path.push_back(current.value());
|
|
||||||
|
|
||||||
return path;
|
if (openIt == openList.end() || tentativeGCost < openIt->second.gCost)
|
||||||
|
openList[neighbor] = PathNode{current, tentativeGCost};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
} // namespace omath::pathfinding
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
enable_testing()
|
enable_testing()
|
||||||
|
|
||||||
project(unit-tests)
|
project(unit-tests)
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
|
||||||
|
|
||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
add_executable(unit-tests
|
add_executable(unit-tests
|
||||||
@@ -18,6 +17,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
|
||||||
@@ -25,6 +25,15 @@ add_executable(unit-tests
|
|||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set_target_properties(unit-tests PROPERTIES
|
||||||
|
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
|
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
|
UNITY_BUILD ON
|
||||||
|
UNITY_BUILD_BATCH_SIZE 20
|
||||||
|
CXX_STANDARD 23
|
||||||
|
CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
|
||||||
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath)
|
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath)
|
||||||
|
|
||||||
gtest_discover_tests(unit-tests)
|
gtest_discover_tests(unit-tests)
|
||||||
@@ -1,37 +1,69 @@
|
|||||||
//
|
//
|
||||||
// Created by Orange on 11/23/2024.
|
// Created by Orange on 11/23/2024.
|
||||||
//
|
//
|
||||||
#include <complex>
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <omath/Matrix.hpp>
|
#include <omath/engines/OpenGL/Camera.hpp>
|
||||||
#include <print>
|
#include <omath/engines/OpenGL/Constants.hpp>
|
||||||
|
#include <omath/engines/OpenGL/Formulas.hpp>
|
||||||
// #include <glm/glm.hpp>
|
|
||||||
|
|
||||||
// #include "glm/ext/matrix_clip_space.hpp"
|
|
||||||
// #include "glm/ext/matrix_transform.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
TEST(UnitTestOpenGL, Projection)
|
TEST(UnitTestOpenGL, ForwardVector)
|
||||||
{
|
{
|
||||||
|
const auto forward = omath::opengl::ForwardVector({});
|
||||||
|
|
||||||
/*const auto proj_glm = glm::perspective(glm::radians(90.f), 16.f / 9.f, 0.1f, 1000.f);
|
EXPECT_EQ(forward, omath::opengl::kAbsForward);
|
||||||
// const auto proj_glm2 = glm::perspectiveLH_NO(glm::radians(90.f), 16.f / 9.f, 0.1f, 1000.f);
|
}
|
||||||
// const auto proj_omath = omath::Mat<4, 4, float, omath::MatStoreType::COLUMN_MAJOR>((const float*)&proj_glm);
|
|
||||||
// EXPECT_EQ(omath::opengl::PerspectiveProjectionMatrix(90, 16.f / 9.f, 0.1f, 1000.f), proj_omath);
|
TEST(UnitTestOpenGL, RightVector)
|
||||||
|
{
|
||||||
|
const auto right = omath::opengl::RightVector({});
|
||||||
glm::vec4 ndc_glm2 = proj_glm * glm::vec4(300.f, 0.f, -1000.f, 1.f);
|
|
||||||
ndc_glm2 /= ndc_glm2.w;
|
EXPECT_EQ(right, omath::opengl::kAbsRight);
|
||||||
const omath::Mat<4, 1, float, omath::MatStoreType::COLUMN_MAJOR> cords_omath =
|
}
|
||||||
{
|
|
||||||
{0},
|
TEST(UnitTestOpenGL, UpVector)
|
||||||
{0},
|
{
|
||||||
{-0.2f},
|
const auto up = omath::opengl::UpVector({});
|
||||||
{1}
|
EXPECT_EQ(up, omath::opengl::kAbsUp);
|
||||||
};
|
}
|
||||||
|
|
||||||
//auto ndc_omath = proj_omath * cords_omath;
|
TEST(UnitTestOpenGL, ProjectTargetMovedFromCamera)
|
||||||
// ndc_omath /= ndc_omath.At(3, 0);
|
{
|
||||||
*/
|
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||||
|
auto cam = omath::opengl::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||||
|
|
||||||
|
|
||||||
|
for (float distance = -10.f; distance > -1000.f; distance -= 0.01f)
|
||||||
|
{
|
||||||
|
const auto projected = cam.WorldToScreen({0, 0, distance});
|
||||||
|
|
||||||
|
EXPECT_TRUE(projected.has_value());
|
||||||
|
|
||||||
|
if (!projected.has_value())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
EXPECT_NEAR(projected->x, 960, 0.00001f);
|
||||||
|
EXPECT_NEAR(projected->y, 540, 0.00001f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(UnitTestOpenGL, CameraSetAndGetFov)
|
||||||
|
{
|
||||||
|
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||||
|
auto cam = omath::opengl::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||||
|
|
||||||
|
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 90.f);
|
||||||
|
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||||
|
|
||||||
|
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(UnitTestOpenGL, CameraSetAndGetOrigin)
|
||||||
|
{
|
||||||
|
auto cam = omath::opengl::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, {}, 0.01f, 1000.f);
|
||||||
|
|
||||||
|
EXPECT_EQ(cam.GetOrigin(), omath::Vector3{});
|
||||||
|
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||||
|
|
||||||
|
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
//
|
//
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <omath/engines/Source/Camera.hpp>
|
#include <omath/engines/Source/Camera.hpp>
|
||||||
#include <omath/engines/Source/Constants.h>
|
#include <omath/engines/Source/Constants.hpp>
|
||||||
#include <omath/engines/Source/Formulas.hpp>
|
#include <omath/engines/Source/Formulas.hpp>
|
||||||
|
|
||||||
|
|
||||||
@@ -27,17 +27,15 @@ TEST(UnitTestSourceEngine, UpVector)
|
|||||||
EXPECT_EQ(up, omath::source::kAbsUp);
|
EXPECT_EQ(up, omath::source::kAbsUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(UnitTestSourceEngine, PerpectiveProjectionAtCenter)
|
TEST(UnitTestSourceEngine, ProjectTargetMovedFromCamera)
|
||||||
{
|
{
|
||||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||||
auto cam = omath::source::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
auto cam = omath::source::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||||
|
|
||||||
|
|
||||||
const auto viewProjMatrix = cam.GetViewProjectionMatrix();
|
|
||||||
|
|
||||||
for (float distance = 0.02f; distance < 1000.f; distance += 0.01f)
|
for (float distance = 0.02f; distance < 1000.f; distance += 0.01f)
|
||||||
{
|
{
|
||||||
const auto projected = cam.WorldToScreen(viewProjMatrix, {distance, 0, 0});
|
const auto projected = cam.WorldToScreen({distance, 0, 0});
|
||||||
|
|
||||||
EXPECT_TRUE(projected.has_value());
|
EXPECT_TRUE(projected.has_value());
|
||||||
|
|
||||||
@@ -48,3 +46,24 @@ TEST(UnitTestSourceEngine, PerpectiveProjectionAtCenter)
|
|||||||
EXPECT_NEAR(projected->y, 540, 0.00001f);
|
EXPECT_NEAR(projected->y, 540, 0.00001f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(UnitTestSourceEngine, CameraSetAndGetFov)
|
||||||
|
{
|
||||||
|
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||||
|
auto cam = omath::source::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||||
|
|
||||||
|
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 90.f);
|
||||||
|
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||||
|
|
||||||
|
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(UnitTestSourceEngine, CameraSetAndGetOrigin)
|
||||||
|
{
|
||||||
|
auto cam = omath::source::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, {}, 0.01f, 1000.f);
|
||||||
|
|
||||||
|
EXPECT_EQ(cam.GetOrigin(), omath::Vector3{});
|
||||||
|
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||||
|
|
||||||
|
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||||
|
}
|
||||||
@@ -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}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,14 @@ TEST_F(UnitTestMat, Constructor_InitializerList)
|
|||||||
EXPECT_FLOAT_EQ(m.At(1, 1), 4.0f);
|
EXPECT_FLOAT_EQ(m.At(1, 1), 4.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(UnitTestMat, Operator_SquareBrackets)
|
||||||
|
{
|
||||||
|
EXPECT_EQ((m2[0, 0]), 1.0f);
|
||||||
|
EXPECT_EQ((m2[0, 1]), 2.0f);
|
||||||
|
EXPECT_EQ((m2[1, 0]), 3.0f);
|
||||||
|
EXPECT_EQ((m2[1, 1]), 4.0f);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(UnitTestMat, Constructor_Copy)
|
TEST_F(UnitTestMat, Constructor_Copy)
|
||||||
{
|
{
|
||||||
Mat<2, 2> m3 = m2;
|
Mat<2, 2> m3 = m2;
|
||||||
|
|||||||
@@ -29,6 +29,15 @@ TEST_F(UnitTestMatrix, Constructor_Size)
|
|||||||
EXPECT_EQ(m.ColumnsCount(), 3);
|
EXPECT_EQ(m.ColumnsCount(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(UnitTestMatrix, Operator_SquareBrackets)
|
||||||
|
{
|
||||||
|
EXPECT_EQ((m2[0, 0]), 1.0f);
|
||||||
|
EXPECT_EQ((m2[0, 1]), 2.0f);
|
||||||
|
EXPECT_EQ((m2[1, 0]), 3.0f);
|
||||||
|
EXPECT_EQ((m2[1, 1]), 4.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_F(UnitTestMatrix, Constructor_InitializerList)
|
TEST_F(UnitTestMatrix, Constructor_InitializerList)
|
||||||
{
|
{
|
||||||
Matrix m{{1.0f, 2.0f}, {3.0f, 4.0f}};
|
Matrix m{{1.0f, 2.0f}, {3.0f, 4.0f}};
|
||||||
|
|||||||
@@ -10,9 +10,9 @@
|
|||||||
|
|
||||||
TEST(UnitTestProjection, Projection)
|
TEST(UnitTestProjection, Projection)
|
||||||
{
|
{
|
||||||
auto x = omath::Angle<float, 0.f, 180.f, omath::AngleFlags::Clamped>::FromDegrees(90.f);
|
const auto x = omath::Angle<float, 0.f, 180.f, omath::AngleFlags::Clamped>::FromDegrees(90.f);
|
||||||
auto cam = omath::source::Camera({0, 0, 0}, omath::source::ViewAngles{}, {1920.f, 1080.f}, x, 0.01f, 1000.f);
|
auto cam = omath::source::Camera({0, 0, 0}, omath::source::ViewAngles{}, {1920.f, 1080.f}, x, 0.01f, 1000.f);
|
||||||
|
|
||||||
const auto projected = cam.WorldToScreen(cam.GetViewProjectionMatrix(), {1000, 0, 50});
|
const auto projected = cam.WorldToScreen({1000, 0, 50});
|
||||||
std::print("{} {} {}", projected->x, projected->y, projected->z);
|
std::print("{} {} {}", projected->x, projected->y, projected->z);
|
||||||
}
|
}
|
||||||
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,21 @@ 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(UnitTestVector3, IsPerpendicular)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(Vector3(0.0f, 0.0f, 1.0f).IsPerpendicular({1, 0 ,0}), true);
|
||||||
|
EXPECT_EQ(Vector3(0.0f, 0.0f, 1.0f).IsPerpendicular({0.0f, 0.0f, 1.0f}), false);
|
||||||
|
EXPECT_FALSE(Vector3(0.0f, 0.0f, 0.0f).IsPerpendicular({0.0f, 0.0f, 1.0f}));
|
||||||
|
}
|
||||||
|
|
||||||
// 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