10#include <lagrange/raycasting/ClosestPointResult.h>
12#include <lagrange/utils/point_triangle_squared_distance.h>
14#include <embree3/rtcore.h>
15#include <embree3/rtcore_geometry.h>
16#include <embree3/rtcore_ray.h>
17#include <Eigen/Geometry>
22template <
typename Scalar>
23bool embree_closest_point(RTCPointQueryFunctionArguments* args)
25 using Point =
typename ClosestPointResult<Scalar>::Point;
26 using MapType = Eigen::Map<Eigen::Matrix4f, Eigen::Aligned16>;
27 using AffineMat = Eigen::Transform<Scalar, 3, Eigen::Affine>;
29 assert(args->userPtr);
30 auto result =
reinterpret_cast<ClosestPointResult<Scalar>*
>(args->userPtr);
32 const unsigned int geomID = args->geomID;
33 const unsigned int primID = args->primID;
35 RTCPointQueryContext* context = args->context;
36 const unsigned int stack_size = args->context->instStackSize;
37 const unsigned int stack_ptr = stack_size - 1;
39 AffineMat inst2world =
40 (stack_size > 0 ? AffineMat(MapType(context->inst2world[stack_ptr]).template cast<Scalar>())
41 : AffineMat::Identity());
44 Point q(args->query->x, args->query->y, args->query->z);
48 assert(result->populate_triangle);
49 result->populate_triangle(geomID, primID, v0, v1, v2);
52 if (stack_size > 0 && args->similarityScale > 0) {
56 AffineMat world2inst(MapType(context->world2inst[stack_ptr]).template cast<Scalar>());
58 }
else if (stack_size > 0) {
75 float d = std::sqrt(
static_cast<float>(d2));
76 if (args->similarityScale > 0) {
77 d = d / args->similarityScale;
83 if (d < args->query->radius) {
84 args->query->radius = d;
85 result->closest_point = (args->similarityScale > 0 ? (inst2world * p).eval() : p);
86 result->mesh_index = geomID;
87 result->facet_index = primID;
88 result->barycentric_coord = Point(l1, l2, l3);
Main namespace for Lagrange.
Definition: AABBIGL.h:30
auto point_triangle_squared_distance(const Eigen::MatrixBase< PointType > &point, const Eigen::MatrixBase< PointType > &V0, const Eigen::MatrixBase< PointType > &V1, const Eigen::MatrixBase< PointType > &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