10#include <lagrange/legacy/inline.h>
11#include <lagrange/raycasting/ClosestPointResult.h>
13#include <lagrange/utils/point_triangle_squared_distance.h>
15#ifdef LAGRANGE_WITH_EMBREE_3
16 #include <embree3/rtcore.h>
17 #include <embree3/rtcore_geometry.h>
18 #include <embree3/rtcore_ray.h>
20 #include <embree4/rtcore.h>
21 #include <embree4/rtcore_geometry.h>
22 #include <embree4/rtcore_ray.h>
25#include <Eigen/Geometry>
34template <
typename Scalar>
35bool embree_closest_point(RTCPointQueryFunctionArguments* args)
37 using Point =
typename ClosestPointResult<Scalar>::Point;
38 using MapType = Eigen::Map<Eigen::Matrix4f, Eigen::Aligned16>;
39 using AffineMat = Eigen::Transform<Scalar, 3, Eigen::Affine>;
41 assert(args->userPtr);
44 const unsigned int geomID = args->geomID;
45 const unsigned int primID = args->primID;
47 RTCPointQueryContext* context = args->context;
48 const unsigned int stack_size = args->context->instStackSize;
49 const unsigned int stack_ptr = stack_size - 1;
51 AffineMat inst2world =
52 (stack_size > 0 ? AffineMat(MapType(context->inst2world[stack_ptr]).template
cast<Scalar>())
53 : AffineMat::Identity());
56 Point q(args->query->x, args->query->y, args->query->z);
60 assert(result->populate_triangle);
61 result->populate_triangle(geomID, primID, v0, v1, v2);
64 if (stack_size > 0 && args->similarityScale > 0) {
68 AffineMat world2inst(MapType(context->world2inst[stack_ptr]).template
cast<Scalar>());
70 }
else if (stack_size > 0) {
87 float d = std::sqrt(
static_cast<float>(d2));
88 if (args->similarityScale > 0) {
89 d = d / args->similarityScale;
95 if (d < args->query->radius) {
96 args->query->radius = d;
97 result->closest_point = (args->similarityScale > 0 ? (inst2world * p).eval() : p);
98 result->mesh_index = geomID;
99 result->facet_index = primID;
100 result->barycentric_coord = Point(l1, l2, l3);
@ Scalar
Mesh attribute must have exactly 1 channel.
Definition AttributeFwd.h:56
SurfaceMesh< ToScalar, ToIndex > cast(const SurfaceMesh< FromScalar, FromIndex > &source_mesh, const AttributeFilter &convertible_attributes={}, std::vector< std::string > *converted_attributes_names=nullptr)
Cast a mesh to a mesh of different scalar and/or index type.
Raycasting operations.
Definition ClosestPointResult.h:22
Main namespace for Lagrange.
auto point_triangle_squared_distance(const Eigen::MatrixBase< PointType > &point, const Eigen::MatrixBase< PointType2 > &V0, const Eigen::MatrixBase< PointType2 > &V1, const Eigen::MatrixBase< PointType2 > &V2, Eigen::PlainObjectBase< PointType > &closest_point, ScalarOf< PointType > &lambda0, ScalarOf< PointType > &lambda1, ScalarOf< PointType > &lambda2) -> ScalarOf< PointType >
Computes the point closest to a given point in a nd triangle.
Definition point_triangle_squared_distance.h:77
Definition ClosestPointResult.h:28