Skip to content

Commit d5233dd

Browse files
authored
Merge pull request #139 from orange-cpp/feature/mesh_line_tracer
Feature/mesh line tracer
2 parents 8bccbdb + bd1d437 commit d5233dd

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

include/omath/3d_primitives/mesh.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ namespace omath::primitives
101101

102102
[[nodiscard]]
103103
VectorType vertex_position_to_world_space(const Vector3<float>& vertex_position) const
104-
requires HasPosition<VertexType>
105104
{
106105
auto abs_vec = get_to_world_matrix()
107106
* mat_column_from_vector<typename Mat4X4::ContainedType, Mat4X4::get_store_ordering()>(
@@ -112,11 +111,16 @@ namespace omath::primitives
112111

113112
[[nodiscard]]
114113
Triangle<VectorType> make_face_in_world_space(const Ebo::const_iterator vao_iterator) const
115-
requires HasPosition<VertexType>
116114
{
117-
return {vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->x).position),
118-
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->y).position),
119-
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->z).position)};
115+
if constexpr (HasPosition<VertexType>)
116+
{
117+
return {vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->x).position),
118+
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->y).position),
119+
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->z).position)};
120+
}
121+
return {vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->x)),
122+
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->y)),
123+
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->z))};
120124
}
121125

122126
private:

include/omath/collision/line_tracer.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,25 @@ namespace omath::collision
3333
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
3434
[[nodiscard]]
3535
static Vector3<float> get_ray_hit_point(const Ray& ray, const Triangle<Vector3<float>>& triangle) noexcept;
36+
37+
template<class MeshType>
38+
[[nodiscard]]
39+
static Vector3<float> get_ray_hit_point(const Ray& ray, const MeshType& mesh) noexcept
40+
{
41+
Vector3<float> mesh_hit = ray.end;
42+
43+
auto begin = mesh.m_element_buffer_object.cbegin();
44+
auto end = mesh.m_element_buffer_object.cend();
45+
for (auto current = begin; current < end; current = std::next(current))
46+
{
47+
auto face = mesh.make_face_in_world_space(current);
48+
49+
auto ray_stop_point = get_ray_hit_point(ray, face);
50+
if (ray_stop_point.distance_to(ray.start) < mesh_hit.distance_to(ray.start))
51+
mesh_hit = ray_stop_point;
52+
}
53+
54+
return mesh_hit;
55+
}
3656
};
3757
} // namespace omath::collision

tests/general/unit_test_primitive_box.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
// Created by Vladislav on 11.01.2026.
33
//
44
#include "omath/3d_primitives/box.hpp"
5+
#include "omath/collision/line_tracer.hpp"
56
#include "omath/engines/opengl_engine/primitives.hpp"
67
#include <gtest/gtest.h>
78

89
TEST(test, test)
910
{
10-
auto result = omath::primitives::create_box<omath::opengl_engine::BoxMesh>({0.f, 30.f, 0.f}, {}, omath::opengl_engine::k_abs_forward,
11-
omath::opengl_engine::k_abs_right);
11+
auto result = omath::primitives::create_box<omath::opengl_engine::BoxMesh>(
12+
{0.f, 30.f, 0.f}, {}, omath::opengl_engine::k_abs_forward, omath::opengl_engine::k_abs_right);
13+
14+
omath::collision::Ray ray{.start = {0, 0, 0}, .end = {-100, 0, 0}};
15+
std::ignore = omath::collision::LineTracer::get_ray_hit_point(ray, result);
1216
}

tests/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <gtest/gtest.h>
22

3-
int main(int argc, char** argv) {
3+
int main(int argc, char** argv)
4+
{
45
testing::InitGoogleTest(&argc, argv);
56

67
return RUN_ALL_TESTS();

0 commit comments

Comments
 (0)