39 m_vertex_positions = &m_mesh->vertices().attributes().at<
math::fvec3>(
"position");
42 if (!m_mesh->faces().attributes().contains(
"normal"))
50 if (!m_mesh->vertices().attributes().contains(
"normal"))
57 m_face_normals = &m_mesh->faces().attributes().at<
math::fvec3>(
"normal");
61 m_vertex_positions =
nullptr;
62 m_face_normals =
nullptr;
87 bool triangle_hit =
false;
88 float nearest_face_distance = std::numeric_limits<float>::infinity();
89 std::uint32_t nearest_face_index;
95 [&](std::uint32_t index)
108 auto loop = face->loops().begin();
109 const auto& a = (*m_vertex_positions)[loop->vertex()->index()];
110 const auto& b = (*m_vertex_positions)[(++loop)->vertex()->index()];
111 const auto& c = (*m_vertex_positions)[(++loop)->vertex()->index()];
118 if (t < nearest_face_distance)
121 nearest_face_distance = t;
122 nearest_face_index = index;
135 return std::make_tuple(nearest_face_distance, nearest_face_index, (*m_face_normals)[nearest_face_index]);
Portion of a shell bounded by loops.
void clear()
Clears the BVH.
void build(std::span< const bvh_primitive > primitives)
Constructs a BVH from a set of primitives.
void visit(const geom::ray< float, 3 > &ray, const visitor_type &f) const
Visits the primitive indices of all BVH nodes that intersect a ray.
void set_mesh(std::shared_ptr< mesh_type > mesh)
Sets the collider's mesh.
constexpr mesh_collider() noexcept=default
Constructs an empty mesh collider.
std::optional< std::tuple< float, std::uint32_t, math::fvec3 > > intersection(const geom::ray< float, 3 > &ray) const
Finds the nearest point of intersection between a ray and this collision mesh.
void rebuild_bvh()
Rebuilds the BVH of the collision mesh faces.
constexpr std::optional< T > intersection(const ray< T, N > &ray, const hyperplane< T, N > &hyperplane) noexcept
Ray-hyperplane intersection test.
void generate_face_normals(brep_mesh &mesh)
Generates the math::fvec3 face attribute "normal" for a B-rep mesh.
void generate_vertex_normals(brep_mesh &mesh)
Generates the math::fvec3 vertex attribute "normal" for a B-rep mesh.
constexpr T dot(const quaternion< T > &a, const quaternion< T > &b) noexcept
Calculates the dot product of two quaternions.
Half of a line proceeding from an initial point.
vector_type direction
Ray direction vector.