improved mesh class

This commit is contained in:
2025-11-29 16:28:06 +03:00
parent b98093b244
commit ba267cbcb8
10 changed files with 120 additions and 63 deletions

4
.idea/editor.xml generated
View File

@@ -201,7 +201,7 @@
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStaticDataMemberInUnnamedStruct/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStaticDataMemberInUnnamedStruct/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStaticSpecifierOnAnonymousNamespaceMember/@EntryIndexedValue" value="SUGGESTION" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStaticSpecifierOnAnonymousNamespaceMember/@EntryIndexedValue" value="SUGGESTION" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStringLiteralToCharPointerConversion/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStringLiteralToCharPointerConversion/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTabsAreDisallowed/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTabsAreDisallowed/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateArgumentsCanBeDeduced/@EntryIndexedValue" value="HINT" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateArgumentsCanBeDeduced/@EntryIndexedValue" value="HINT" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateParameterNeverUsed/@EntryIndexedValue" value="HINT" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateParameterNeverUsed/@EntryIndexedValue" value="HINT" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateParameterShadowing/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateParameterShadowing/@EntryIndexedValue" value="WARNING" type="string" />
@@ -215,7 +215,7 @@
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnmatchedPragmaEndRegionDirective/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnmatchedPragmaEndRegionDirective/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnmatchedPragmaRegionDirective/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnmatchedPragmaRegionDirective/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnnamedNamespaceInHeaderFile/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnnamedNamespaceInHeaderFile/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnnecessaryWhitespace/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnnecessaryWhitespace/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnsignedZeroComparison/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnsignedZeroComparison/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnusedIncludeDirective/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnusedIncludeDirective/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUseAlgorithmWithCount/@EntryIndexedValue" value="SUGGESTION" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUseAlgorithmWithCount/@EntryIndexedValue" value="SUGGESTION" type="string" />

View File

@@ -6,25 +6,44 @@
#include <omath/linear_algebra/mat.hpp> #include <omath/linear_algebra/mat.hpp>
#include <omath/linear_algebra/vector3.hpp> #include <omath/linear_algebra/vector3.hpp>
#include <utility> #include <utility>
#include <variant>
#include <vector> #include <vector>
namespace omath::primitives namespace omath::primitives
{ {
template<class Mat4X4, class RotationAngles, class MeshTypeTrait, class Type = float> struct Vertex
{
using VectorType = Vector3<float>;
Vector3<float> position;
Vector3<float> normal;
Vector3<float> uv;
};
template<typename T> concept HasPosition = requires(T vertex) { vertex.position; };
template<typename T> concept HasNormal = requires(T vertex) { vertex.normal; };
template<typename T> concept HasUv = requires(T vertex) { vertex.uv; };
template<class Mat4X4, class RotationAngles, class MeshTypeTrait, class VertType = Vertex>
class Mesh final class Mesh final
{ {
public: public:
using NumericType = Type; using NumericType = float;
using VertexType = Vertex;
private: private:
using Vbo = std::vector<Vector3<NumericType>>; using Vbo = std::vector<VertexType>;
using Vao = std::vector<Vector3<std::size_t>>; using Vao = std::vector<Vector3<std::size_t>>;
public: public:
Vbo m_vertex_buffer; Vbo m_vertex_buffer;
Vao m_vertex_array_object; Vao m_vertex_array_object;
Mesh(Vbo vbo, Vao vao, const Vector3<NumericType> scale = {1, 1, 1,}) Mesh(Vbo vbo, Vao vao,
const Vector3<NumericType> scale =
{
1,
1,
1,
})
: m_vertex_buffer(std::move(vbo)), m_vertex_array_object(std::move(vao)), m_scale(std::move(scale)) : m_vertex_buffer(std::move(vbo)), m_vertex_array_object(std::move(vao)), m_scale(std::move(scale))
{ {
} }
@@ -76,19 +95,20 @@ namespace omath::primitives
} }
[[nodiscard]] [[nodiscard]]
Vector3<float> vertex_to_world_space(const Vector3<float>& vertex) const Vector3<float> vertex_to_world_space(const Vector3<float>& vertex_position) const
{ {
auto abs_vec = get_to_world_matrix() * mat_column_from_vector(vertex); auto abs_vec = get_to_world_matrix() * mat_column_from_vector(vertex_position);
return {abs_vec.at(0, 0), abs_vec.at(1, 0), abs_vec.at(2, 0)}; return {abs_vec.at(0, 0), abs_vec.at(1, 0), abs_vec.at(2, 0)};
} }
[[nodiscard]] [[nodiscard]]
Triangle<Vector3<float>> make_face_in_world_space(const Vao::const_iterator vao_iterator) const Triangle<Vector3<float>> make_face_in_world_space(const Vao::const_iterator vao_iterator) const
requires HasPosition<VertexType>
{ {
return {vertex_to_world_space(m_vertex_buffer.at(vao_iterator->x)), return {vertex_to_world_space(m_vertex_buffer.at(vao_iterator->x).position),
vertex_to_world_space(m_vertex_buffer.at(vao_iterator->y)), vertex_to_world_space(m_vertex_buffer.at(vao_iterator->y).position),
vertex_to_world_space(m_vertex_buffer.at(vao_iterator->z))}; vertex_to_world_space(m_vertex_buffer.at(vao_iterator->z).position)};
} }
private: private:

View File

@@ -24,13 +24,13 @@ namespace omath::collision
class Epa final class Epa final
{ {
public: public:
using Vertex = typename ColliderType::VertexType; using VectorType = typename ColliderType::VectorType;
static_assert(EpaVector<Vertex>, "VertexType must satisfy EpaVector concept"); static_assert(EpaVector<VectorType>, "VertexType must satisfy EpaVector concept");
struct Result final struct Result final
{ {
Vertex normal{}; // outward normal (from B to A) VectorType normal{}; // outward normal (from B to A)
Vertex penetration_vector; VectorType penetration_vector;
float depth{0.0f}; float depth{0.0f};
int iterations{0}; int iterations{0};
int num_vertices{0}; int num_vertices{0};
@@ -45,11 +45,11 @@ namespace omath::collision
// Precondition: simplex.size()==4 and contains the origin. // Precondition: simplex.size()==4 and contains the origin.
[[nodiscard]] [[nodiscard]]
static std::optional<Result> solve(const ColliderType& a, const ColliderType& b, const Simplex<Vertex>& simplex, static std::optional<Result> solve(const ColliderType& a, const ColliderType& b, const Simplex<VectorType>& simplex,
const Params params = {}) const Params params = {})
{ {
// --- Build initial polytope from simplex (4 points) --- // --- Build initial polytope from simplex (4 points) ---
std::vector<Vertex> vertexes; std::vector<VectorType> vertexes;
vertexes.reserve(64); vertexes.reserve(64);
for (std::size_t i = 0; i < simplex.size(); ++i) for (std::size_t i = 0; i < simplex.size(); ++i)
vertexes.push_back(simplex[i]); vertexes.push_back(simplex[i]);
@@ -84,7 +84,7 @@ namespace omath::collision
const Face f = faces[fidx]; const Face f = faces[fidx];
// Get farthest point in face normal direction // Get farthest point in face normal direction
const Vertex p = support_point(a, b, f.n); const VectorType p = support_point(a, b, f.n);
const float p_dist = f.n.dot(p); const float p_dist = f.n.dot(p);
// Converged if we cant push the face closer than tolerance // Converged if we cant push the face closer than tolerance
@@ -172,7 +172,7 @@ namespace omath::collision
struct Face final struct Face final
{ {
int i0, i1, i2; int i0, i1, i2;
Vertex n; // unit outward normal VectorType n; // unit outward normal
float d; // n · v0 (>=0 ideally because origin is inside) float d; // n · v0 (>=0 ideally because origin is inside)
}; };
@@ -205,7 +205,7 @@ namespace omath::collision
} }
[[nodiscard]] [[nodiscard]]
static bool visible_from(const Face& f, const Vertex& p) static bool visible_from(const Face& f, const VectorType& p)
{ {
// positive if p is in front of the face // positive if p is in front of the face
return (f.n.dot(p) - f.d) > 1e-7f; return (f.n.dot(p) - f.d) > 1e-7f;
@@ -223,12 +223,12 @@ namespace omath::collision
} }
[[nodiscard]] [[nodiscard]]
static Face make_face(const std::vector<Vertex>& vertexes, int i0, int i1, int i2) static Face make_face(const std::vector<VectorType>& vertexes, int i0, int i1, int i2)
{ {
const Vertex& a0 = vertexes[i0]; const VectorType& a0 = vertexes[i0];
const Vertex& a1 = vertexes[i1]; const VectorType& a1 = vertexes[i1];
const Vertex& a2 = vertexes[i2]; const VectorType& a2 = vertexes[i2];
Vertex n = (a1 - a0).cross(a2 - a0); VectorType n = (a1 - a0).cross(a2 - a0);
if (n.dot(n) <= 1e-30f) if (n.dot(n) <= 1e-30f)
{ {
n = any_perp_vec(a1 - a0); // degenerate guard n = any_perp_vec(a1 - a0); // degenerate guard
@@ -246,9 +246,9 @@ namespace omath::collision
} }
[[nodiscard]] [[nodiscard]]
static Vertex support_point(const ColliderType& a, const ColliderType& b, const Vertex& dir) static VectorType support_point(const ColliderType& a, const ColliderType& b, const VectorType& dir)
{ {
return a.find_abs_furthest_vertex(dir) - b.find_abs_furthest_vertex(-dir); return a.find_abs_furthest_vertex(dir).position - b.find_abs_furthest_vertex(-dir).position;
} }
template<class V> template<class V>

View File

@@ -17,14 +17,14 @@ namespace omath::collision
template<class ColliderType> template<class ColliderType>
class GjkAlgorithm final class GjkAlgorithm final
{ {
using VertexType = typename ColliderType::VertexType; using VertexType = ColliderType::VertexType;
using VectorType = VertexType::VectorType;
public: public:
[[nodiscard]] [[nodiscard]]
static VertexType find_support_vertex(const ColliderType& collider_a, const ColliderType& collider_b, static VectorType find_support_vertex(const ColliderType& collider_a, const ColliderType& collider_b,
const VertexType& direction) const VectorType& direction)
{ {
return collider_a.find_abs_furthest_vertex(direction) - collider_b.find_abs_furthest_vertex(-direction); return collider_a.find_abs_furthest_vertex(direction).position - collider_b.find_abs_furthest_vertex(-direction).position;
} }
[[nodiscard]] [[nodiscard]]
@@ -34,12 +34,12 @@ namespace omath::collision
} }
[[nodiscard]] [[nodiscard]]
static GjkHitInfo<VertexType> is_collide_with_simplex_info(const ColliderType& collider_a, static GjkHitInfo<VectorType> is_collide_with_simplex_info(const ColliderType& collider_a,
const ColliderType& collider_b) const ColliderType& collider_b)
{ {
auto support = find_support_vertex(collider_a, collider_b, {1, 0, 0}); auto support = find_support_vertex(collider_a, collider_b, VectorType{1, 0, 0});
Simplex<VertexType> simplex; Simplex<VectorType> simplex;
simplex.push_front(support); simplex.push_front(support);
auto direction = -support; auto direction = -support;

View File

@@ -11,31 +11,37 @@ namespace omath::collision
class MeshCollider class MeshCollider
{ {
public: public:
using NumericType = typename MeshType::NumericType; using NumericType = MeshType::NumericType;
using VertexType = Vector3<NumericType>; using VertexType = MeshType::VertexType;
using VectorType = VertexType::VectorType;
explicit MeshCollider(MeshType mesh): m_mesh(std::move(mesh)) explicit MeshCollider(MeshType mesh): m_mesh(std::move(mesh))
{ {
} }
[[nodiscard]] [[nodiscard]]
const VertexType& find_furthest_vertex(const VertexType& direction) const const VertexType& find_furthest_vertex(const VectorType& direction) const
{ {
return *std::ranges::max_element(m_mesh.m_vertex_buffer, [&direction](const auto& first, const auto& second) return *std::ranges::max_element(
{ return first.dot(direction) < second.dot(direction); }); m_mesh.m_vertex_buffer, [&direction](const auto& first, const auto& second)
{ return first.position.dot(direction) < second.position.dot(direction); });
} }
[[nodiscard]] [[nodiscard]]
VertexType find_abs_furthest_vertex(const VertexType& direction) const VertexType find_abs_furthest_vertex(const VectorType& direction) const
{ {
return m_mesh.vertex_to_world_space(find_furthest_vertex(direction)); const auto& vertex = find_furthest_vertex(direction);
auto new_vertex = vertex;
new_vertex.position = m_mesh.vertex_to_world_space(find_furthest_vertex(direction).position);
return new_vertex;
} }
[[nodiscard]] [[nodiscard]]
const VertexType& get_origin() const const VectorType& get_origin() const
{ {
return m_mesh.get_origin(); return m_mesh.get_origin();
} }
private: private:
MeshType m_mesh; MeshType m_mesh;
}; };

View File

@@ -8,5 +8,5 @@
namespace omath::source_engine namespace omath::source_engine
{ {
using Mesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait, float>; using Mesh = primitives::Mesh<Mat4X4, ViewAngles, MeshTrait>;
} }

View File

@@ -320,7 +320,7 @@ namespace omath
return std::visit( return std::visit(
[base_address, &pattern](const auto& nt_header) -> std::optional<std::uintptr_t> [base_address, &pattern](const auto& nt_header) -> std::optional<std::uintptr_t>
{ {
// Define .code segment as scan area // Define .text segment as scan area
const auto start = nt_header.optional_header.base_of_code; const auto start = nt_header.optional_header.base_of_code;
const auto scan_size = nt_header.optional_header.size_code; const auto scan_size = nt_header.optional_header.size_code;

View File

@@ -7,20 +7,32 @@
TEST(UnitTestColider, CheckToWorld) TEST(UnitTestColider, CheckToWorld)
{ {
omath::source_engine::Mesh mesh = {std::vector<omath::Vector3<float>>{{1.f, 1.f, 1.f}, {-1.f, -1.f, -1.f}}, {}}; omath::source_engine::Mesh mesh = {
std::vector<omath::primitives::Vertex>{
{ { 1.f, 1.f, 1.f }, {}, {} },
{ {-1.f, -1.f, -1.f }, {}, {} }
},
{}
};
mesh.set_origin({0, 2, 0}); mesh.set_origin({0, 2, 0});
const omath::source_engine::MeshCollider collider(mesh); const omath::source_engine::MeshCollider collider(mesh);
const auto vertex = collider.find_abs_furthest_vertex({1.f, 0.f, 0.f}); const auto vertex = collider.find_abs_furthest_vertex({1.f, 0.f, 0.f}).position;
EXPECT_EQ(vertex, omath::Vector3<float>(1.f, 3.f, 1.f)); EXPECT_EQ(vertex, omath::Vector3<float>(1.f, 3.f, 1.f));
} }
TEST(UnitTestColider, FindFurthestVertex) TEST(UnitTestColider, FindFurthestVertex)
{ {
const omath::source_engine::Mesh mesh = {{{1.f, 1.f, 1.f}, {-1.f, -1.f, -1.f}}, {}}; const omath::source_engine::Mesh mesh = {
{
{ { 1.f, 1.f, 1.f }, {}, {} }, // position, normal, uv
{ {-1.f, -1.f, -1.f }, {}, {} }
},
{}
};
const omath::source_engine::MeshCollider collider(mesh); const omath::source_engine::MeshCollider collider(mesh);
const auto vertex = collider.find_furthest_vertex({1.f, 0.f, 0.f}); const auto vertex = collider.find_furthest_vertex({1.f, 0.f, 0.f}).position;
EXPECT_EQ(vertex, omath::Vector3<float>(1.f, 1.f, 1.f)); EXPECT_EQ(vertex, omath::Vector3<float>(1.f, 1.f, 1.f));
} }

View File

@@ -14,8 +14,16 @@ using EPA = omath::collision::Epa<Collider>;
TEST(UnitTestEpa, TestCollisionTrue) TEST(UnitTestEpa, TestCollisionTrue)
{ {
// Unit cube [-1,1]^3 // Unit cube [-1,1]^3
std::vector<omath::Vector3<float>> vbo = {{-1, -1, -1}, {-1, -1, 1}, {-1, 1, -1}, {-1, 1, 1}, std::vector<omath::primitives::Vertex> vbo = {
{1, 1, 1}, {1, 1, -1}, {1, -1, 1}, {1, -1, -1}}; { {-1.f, -1.f, -1.f}, {}, {} },
{ {-1.f, -1.f, 1.f}, {}, {} },
{ {-1.f, 1.f, -1.f}, {}, {} },
{ {-1.f, 1.f, 1.f}, {}, {} },
{ { 1.f, 1.f, 1.f}, {}, {} },
{ { 1.f, 1.f, -1.f}, {}, {} },
{ { 1.f, -1.f, 1.f}, {}, {} },
{ { 1.f, -1.f, -1.f}, {}, {} }
};
std::vector<omath::Vector3<std::size_t>> vao; // not needed std::vector<omath::Vector3<std::size_t>> vao; // not needed
Mesh a(vbo, vao, {1, 1, 1}); Mesh a(vbo, vao, {1, 1, 1});
@@ -80,8 +88,16 @@ TEST(UnitTestEpa, TestCollisionTrue)
} }
TEST(UnitTestEpa, TestCollisionTrue2) TEST(UnitTestEpa, TestCollisionTrue2)
{ {
std::vector<omath::Vector3<float>> vbo = {{-1, -1, -1}, {-1, -1, 1}, {-1, 1, -1}, {-1, 1, 1}, std::vector<omath::primitives::Vertex> vbo = {
{1, 1, 1}, {1, 1, -1}, {1, -1, 1}, {1, -1, -1}}; { { -1.f, -1.f, -1.f }, {}, {} },
{ { -1.f, -1.f, 1.f }, {}, {} },
{ { -1.f, 1.f, -1.f }, {}, {} },
{ { -1.f, 1.f, 1.f }, {}, {} },
{ { 1.f, 1.f, 1.f }, {}, {} },
{ { 1.f, 1.f, -1.f }, {}, {} },
{ { 1.f, -1.f, 1.f }, {}, {} },
{ { 1.f, -1.f, -1.f }, {}, {} }
};
std::vector<omath::Vector3<std::size_t>> vao; // not needed std::vector<omath::Vector3<std::size_t>> vao; // not needed
Mesh a(vbo, vao, {1, 1, 1}); Mesh a(vbo, vao, {1, 1, 1});

View File

@@ -8,15 +8,18 @@
namespace namespace
{ {
const omath::source_engine::Mesh mesh = { const omath::source_engine::Mesh mesh = {
{{-1.f, -1.f, -1.f}, {
{-1.f, -1.f, 1.f}, { {-1.f, -1.f, -1.f}, {}, {} },
{-1.f, 1.f, -1.f}, { {-1.f, -1.f, 1.f}, {}, {} },
{-1.f, 1.f, 1.f}, { {-1.f, 1.f, -1.f}, {}, {} },
{1.f, 1.f, 1.f}, { {-1.f, 1.f, 1.f}, {}, {} },
{1.f, 1.f, -1.f}, { { 1.f, 1.f, 1.f}, {}, {} },
{1.f, -1.f, 1.f}, { { 1.f, 1.f, -1.f}, {}, {} },
{1.f, -1.f, -1.f}}, { { 1.f, -1.f, 1.f}, {}, {} },
{}}; { { 1.f, -1.f, -1.f}, {}, {} }
},
{}
};
} }
TEST(UnitTestGjk, TestCollisionTrue) TEST(UnitTestGjk, TestCollisionTrue)
{ {