34 mask.foreach_index([&](
const int i) {
38 const float3 position = positions[
i];
42 r_indices[
i] = nearest.
index;
48 r_positions[
i] = nearest.
co;
60 .supported_type({GeometryComponent::Type::Mesh, GeometryComponent::Type::PointCloud});
62 b.add_output<
decl::Int>(
"Index").dependent_field({1});
73 node->
custom2 = int(AttrDomain::Point);
83 if (tree_data.
tree ==
nullptr) {
85 r_distances_sq.
fill(0.0f);
89 mask.foreach_index([&](
const int i) {
93 const float3 position = positions[
i];
99 r_indices[
i] = nearest.
index;
101 r_distances_sq[
i] = nearest.
dist_sq;
154 const Span<int> tri_faces = mesh.corner_tri_faces();
156 mask.foreach_index([&](
const int i) { r_face_indices[
i] = tri_faces[tri_indices[
i]]; });
167 const Span<float3> vert_positions = mesh.vert_positions();
169 const Span<int> corner_verts = mesh.corner_verts();
175 mask.foreach_index([&](
const int i) {
176 const float3 position = positions[
i];
177 const int face_index = face_indices[
i];
180 float min_distance_sq =
FLT_MAX;
181 int closest_vert = 0;
182 int closest_corner = 0;
183 for (
const int corner :
faces[face_index]) {
184 const int vert = corner_verts[corner];
186 if (distance_sq < min_distance_sq) {
187 min_distance_sq = distance_sq;
188 closest_corner = corner;
193 r_corner_indices[
i] = closest_corner;
196 r_positions[
i] = vert_positions[closest_vert];
199 r_distances_sq[
i] = min_distance_sq;
221 GeometryComponent::Type::Mesh, GeometryComponent::Type::PointCloud};
224 return geometry.get_component(src_type);
240 mf::Signature signature_;
244 : source_(std::move(
geometry)), domain_(domain)
246 source_.ensure_owns_direct_data();
255 mf::SignatureBuilder builder{
"Sample Nearest", signature_};
256 builder.single_input<
float3>(
"Position");
257 builder.single_output<
int>(
"Index");
265 if (!src_component_) {
270 switch (src_component_->type()) {
271 case GeometryComponent::Type::Mesh: {
273 const Mesh &mesh = *component.
get();
275 case AttrDomain::Point:
278 case AttrDomain::Edge:
281 case AttrDomain::Face:
284 case AttrDomain::Corner:
292 case GeometryComponent::Type::PointCloud: {
308 TIP_(
"The source geometry must contain a mesh or a point cloud"));
309 params.set_default_remaining_outputs();
314 auto fn = std::make_shared<SampleNearestFunction>(std::move(
geometry), domain);
327 int(AttrDomain::Point));
335 ntype.
ui_name =
"Sample Nearest";
337 "Find the element of a geometry closest to a position. Similar to the \"Index of Nearest\" "
#define NODE_CLASS_GEOMETRY
#define GEO_NODE_SAMPLE_NEAREST
int BLI_bvhtree_find_nearest(const BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
@ NODE_DEFAULT_INPUT_POSITION_FIELD
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
constexpr int64_t size() const
constexpr bool is_empty() const
constexpr void fill(const T &value) const
int attribute_domain_size(AttrDomain domain) const
const PointCloud * get() const
void set_signature(const Signature *signature)
static std::shared_ptr< FieldOperation > Create(std::shared_ptr< const mf::MultiFunction > function, Vector< GField > inputs={})
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
SampleNearestFunction(GeometrySet geometry, AttrDomain domain)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void node_register_type(bNodeType &ntype)
BVHTreeFromPointCloud bvhtree_from_pointcloud_get(const PointCloud &pointcloud, const IndexMask &points_mask)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
static void get_closest_mesh_faces(const Mesh &mesh, const VArray< float3 > &positions, const IndexMask &mask, const MutableSpan< int > r_face_indices, const MutableSpan< float > r_distances_sq, const MutableSpan< float3 > r_positions)
static void get_closest_mesh_edges(const Mesh &mesh, const VArray< float3 > &positions, const IndexMask &mask, const MutableSpan< int > r_edge_indices, const MutableSpan< float > r_distances_sq, const MutableSpan< float3 > r_positions)
static void get_closest_mesh_points(const Mesh &mesh, const VArray< float3 > &positions, const IndexMask &mask, const MutableSpan< int > r_point_indices, const MutableSpan< float > r_distances_sq, const MutableSpan< float3 > r_positions)
static void get_closest_pointcloud_points(const bke::BVHTreeFromPointCloud &tree_data, const VArray< float3 > &positions, const IndexMask &mask, MutableSpan< int > r_indices, MutableSpan< float > r_distances_sq)
static void get_closest_mesh_corners(const Mesh &mesh, const VArray< float3 > &positions, const IndexMask &mask, const MutableSpan< int > r_corner_indices, const MutableSpan< float > r_distances_sq, const MutableSpan< float3 > r_positions)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static bool component_is_available(const GeometrySet &geometry, const GeometryComponent::Type type, const AttrDomain domain)
static const GeometryComponent * find_source_component(const GeometrySet &geometry, const AttrDomain domain)
static void get_closest_mesh_tris(const Mesh &mesh, const VArray< float3 > &positions, const IndexMask &mask, const MutableSpan< int > r_tri_indices, const MutableSpan< float > r_distances_sq, const MutableSpan< float3 > r_positions)
static void node_rna(StructRNA *srna)
static void node_register()
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
void get_closest_in_bvhtree(bke::BVHTreeFromMesh &tree_data, const VArray< float3 > &positions, const IndexMask &mask, MutableSpan< int > r_indices, MutableSpan< float > r_distances_sq, MutableSpan< float3 > r_positions)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_attribute_domain_only_mesh_items[]
BVHTree_NearestPointCallback nearest_callback
BVHTree_NearestPointCallback nearest_callback
std::string ui_description
void(* initfunc)(bNodeTree *ntree, bNode *node)
NodeGeometryExecFunction geometry_node_execute
const char * enum_name_legacy
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
NodeDeclareFunction declare
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)