Skip to content

Commit e5d8e1c

Browse files
committed
add mesh class
1 parent e2378bf commit e5d8e1c

File tree

4 files changed

+112
-11
lines changed

4 files changed

+112
-11
lines changed

.idea/editor.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//
2+
// Created by Vladislav on 09.11.2025.
3+
//
4+
#pragma once
5+
#include "omath/linear_algebra/triangle.hpp"
6+
#include <omath/linear_algebra/mat.hpp>
7+
#include <omath/linear_algebra/vector3.hpp>
8+
#include <utility>
9+
#include <vector>
10+
11+
namespace omath::primitives
12+
{
13+
template<class Mat4X4, class RotationAngles, class MeshTypeTrait, class NumericType = float>
14+
class Mesh final
15+
{
16+
using Vbo = std::vector<Vector3<NumericType>>;
17+
using Vao = std::vector<Vector3<std::size_t>>;
18+
19+
public:
20+
Vbo m_vertex_buffer;
21+
Vao m_vertex_array_object;
22+
23+
Mesh(Vbo vbo, Vao vao): m_vertex_buffer(std::move(vbo)), m_vertex_array_object(std::move(vao))
24+
{
25+
26+
}
27+
void set_origin(const Vector3<NumericType>& new_origin)
28+
{
29+
m_origin = new_origin;
30+
m_to_world_matrix = std::nullopt;
31+
}
32+
33+
void set_scale(const Vector3<NumericType>& new_scale)
34+
{
35+
m_scale = new_scale;
36+
m_to_world_matrix = std::nullopt;
37+
}
38+
39+
void set_rotation(const RotationAngles& new_rotation_angles)
40+
{
41+
m_rotation_angles = new_rotation_angles;
42+
m_to_world_matrix = std::nullopt;
43+
}
44+
45+
[[nodiscard]]
46+
const Vector3<NumericType>& get_origin() const
47+
{
48+
return m_origin;
49+
}
50+
51+
[[nodiscard]]
52+
const Vector3<NumericType>& get_scale() const
53+
{
54+
return m_scale;
55+
}
56+
57+
[[nodiscard]]
58+
const RotationAngles& get_rotation_angles() const
59+
{
60+
return m_rotation_angles;
61+
}
62+
63+
[[nodiscard]]
64+
const Mat4X4& get_to_world_matrix() const
65+
{
66+
if (m_to_world_matrix)
67+
return m_to_world_matrix.value();
68+
m_to_world_matrix =
69+
mat_translation(m_origin) * mat_scale(m_scale) * MeshTypeTrait::rotation_matrix(m_rotation_angles);
70+
71+
return m_to_world_matrix.value();
72+
}
73+
74+
[[nodiscard]]
75+
Vector3<float> vertex_to_world_space(const Vector3<float>& vertex) const
76+
{
77+
auto abs_vec = get_to_world_matrix() * mat_column_from_vector(vertex);
78+
79+
return {abs_vec.at(0, 0), abs_vec.at(1, 0), abs_vec.at(2, 0)};
80+
}
81+
[[nodiscard]]
82+
Triangle<Vector3<float>> make_face_in_world_space(const Vao::const_iterator index) const
83+
{
84+
return {vertex_to_world_space(m_vertex_buffer.at(index->x)),
85+
vertex_to_world_space(m_vertex_buffer.at(index->y)),
86+
vertex_to_world_space(m_vertex_buffer.at(index->z))};
87+
}
88+
89+
private:
90+
Vector3<NumericType> m_origin;
91+
Vector3<NumericType> m_scale;
92+
93+
RotationAngles m_rotation_angles;
94+
95+
mutable std::optional<Mat4X4> m_to_world_matrix;
96+
};
97+
} // namespace omath::primitives

include/omath/collision/gjk_algorithm.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@
99

1010
namespace omath::collision
1111
{
12-
template<class ColliderType = MeshCollider>
12+
template<class ColliderType = MeshCollider<>>
1313
class GjkAlgorithm final
1414
{
1515
public:
1616
[[nodiscard]]
17-
static MeshCollider::VertexType find_support_vertex(const ColliderType& collider_a,
17+
static ColliderType::VertexType find_support_vertex(const ColliderType& collider_a,
1818
const ColliderType& collider_b,
19-
const MeshCollider::VertexType& direction)
19+
const ColliderType::VertexType& direction)
2020
{
2121
return collider_a.find_abs_furthest_vertex(direction) - collider_b.find_abs_furthest_vertex(-direction);
2222
}
2323

2424
[[nodiscard]]
25-
static bool is_collide(const MeshCollider& collider_a, const MeshCollider& collider_b)
25+
static bool is_collide(const ColliderType& collider_a, const ColliderType& collider_b)
2626
{
2727
// Get initial support point in any direction
2828
auto support = find_support_vertex(collider_a, collider_b, {1, 0, 0});
2929

30-
Simplex<MeshCollider::VertexType> simplex;
30+
Simplex<typename ColliderType::VertexType> simplex;
3131
simplex.push_front(support);
3232

3333
auto direction = -support;

include/omath/collision/mesh_collider.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
namespace omath::collision
1111
{
12+
template<class NumericType = float>
1213
class MeshCollider
1314
{
1415
public:
15-
using VertexType = Vector3<float>;
16-
MeshCollider(const std::vector<VertexType>& vertexes, const VertexType& origin, const VertexType& scale = {1.f, 1.f, 1.f})
17-
: m_vertexes(vertexes),m_scale(scale), m_origin(origin)
16+
using VertexType = Vector3<NumericType>;
17+
MeshCollider(const std::vector<VertexType>& vertexes, const VertexType& origin,
18+
const VertexType& scale = {1.f, 1.f, 1.f})
19+
: m_vertexes(vertexes), m_scale(scale), m_origin(origin)
1820
{
1921
if (m_vertexes.empty())
2022
throw std::runtime_error("Collider cannot have 0 vertexes");
@@ -37,12 +39,14 @@ namespace omath::collision
3739
return *std::ranges::max_element(m_vertexes, [&direction](const auto& first, const auto& second)
3840
{ return first.dot(direction) < second.dot(direction); });
3941
}
42+
4043
[[nodiscard]]
4144
Vector3<float> find_abs_furthest_vertex(const Vector3<float>& direction) const
4245
{
4346
return vertex_to_world_space(find_furthest_vertex(direction));
4447
}
45-
[[nodiscard]] Vector3<float> vertex_to_world_space( const Vector3<float>& local_vertex) const
48+
49+
[[nodiscard]] Vector3<float> vertex_to_world_space(const Vector3<float>& local_vertex) const
4650
{
4751
auto abs_vec = to_world() * mat_column_from_vector(local_vertex);
4852

0 commit comments

Comments
 (0)