mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
improvement
This commit is contained in:
59
include/omath/Triangle.hpp
Normal file
59
include/omath/Triangle.hpp
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
//
|
||||||
|
// Created by Orange on 11/13/2024.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include "omath/Vector3.hpp"
|
||||||
|
|
||||||
|
namespace omath
|
||||||
|
{
|
||||||
|
template<class Vector>
|
||||||
|
class Triangle final
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
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
|
||||||
|
{
|
||||||
|
return (m_vertex1 - m_vertex2).Cross(m_vertex3 - m_vertex1).Normalized();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr float SideALength() const
|
||||||
|
{
|
||||||
|
return m_vertex1.DistTo(m_vertex2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr float SideBLength() const
|
||||||
|
{
|
||||||
|
return m_vertex3.DistTo(m_vertex2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr Vector3 SideAVector() const
|
||||||
|
{
|
||||||
|
return m_vertex1 - m_vertex2;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[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,36 +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;
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
Vector3 MidPoint() const;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
|||||||
@@ -1,40 +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;
|
|
||||||
}
|
|
||||||
Vector3 Triangle3d::MidPoint() const
|
|
||||||
{
|
|
||||||
return (m_vertex1 + m_vertex2 + m_vertex3) / 3;
|
|
||||||
}
|
|
||||||
} // namespace omath
|
|
||||||
@@ -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
|
||||||
|
|||||||
4
tests/general/UnitTestTriangle.cpp
Normal file
4
tests/general/UnitTestTriangle.cpp
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
//
|
||||||
|
// Created by Orange on 1/6/2025.
|
||||||
|
//
|
||||||
|
#include "omath/Triangle.hpp"
|
||||||
Reference in New Issue
Block a user