Euphoria
collisionmesh.cc
Go to the documentation of this file.
1 #include "core/collisionmesh.h"
2 
3 #include "core/mesh.h"
4 #include "core/intersection.h"
5 
6 
7 namespace eu::core
8 {
9 
10 
11 void
13 {
14  for(const auto& part: mesh.parts)
15  {
16  const auto point_base = points.size();
17 
18  points.reserve(points.size() + part.points.size());
19  for(const auto& p: part.points)
20  {
21  points.emplace_back(p.vertex);
22  }
23 
24  faces.reserve(faces.size() + part.faces.size());
25  for(const auto& f: part.faces)
26  {
27  faces.emplace_back
28  (
29  c_sizet_to_int(point_base) + f.a,
30  c_sizet_to_int(point_base) + f.b,
31  c_sizet_to_int(point_base) + f.c
32  );
33  }
34  }
35 }
36 
37 
38 std::optional<float>
39 get_intersection(const UnitRay3f& ray, const CollisionMesh& mesh)
40 {
41  std::optional<float> closest_intersection = std::nullopt;
42 
43  for(const auto& face: mesh.faces)
44  {
45  const auto face_intersection = get_intersection_ray_triangle
46  (
47  ray,
48  mesh.points[face.x], mesh.points[face.y], mesh.points[face.z]
49  );
50 
51  if(closest_intersection.has_value() && face_intersection.has_value())
52  {
53  if(*face_intersection < *closest_intersection)
54  {
55  closest_intersection = face_intersection;
56  }
57  }
58  else if (closest_intersection.has_value() == false)
59  {
60  closest_intersection = face_intersection;
61  }
62  }
63 
64  return closest_intersection;
65 }
66 
67 
68 }
std::optional< float > get_intersection_ray_triangle(const UnitRay3f &ray, const vec3f &v0, const vec3f &v1, const vec3f &v2)
This implements the Möller–Trumbore intersection algorithm.
std::optional< float > get_intersection(const UnitRay3f &ray, const CollisionMesh &mesh)
int c_sizet_to_int(size_t t)
Definition: cint.cc:11
std::vector< vec3f > points
Definition: collisionmesh.h:34
void add(const Mesh &mesh)
std::vector< CollisionFace > faces
Definition: collisionmesh.h:35
std::vector< MeshPart > parts
Definition: mesh.h:103