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})
61 .description(
"Mesh or point cloud to find the nearest point on");
63 b.add_output<
decl::Int>(
"Index").dependent_field({1});
74 node->
custom2 = int(AttrDomain::Point);
84 if (tree_data.
tree ==
nullptr) {
86 r_distances_sq.
fill(0.0f);
90 mask.foreach_index([&](
const int i) {
94 const float3 position = positions[
i];
100 r_indices[
i] = nearest.
index;
102 r_distances_sq[
i] = nearest.
dist_sq;
155 const Span<int> tri_faces = mesh.corner_tri_faces();
157 mask.foreach_index([&](
const int i) { r_face_indices[
i] = tri_faces[tri_indices[
i]]; });
168 const Span<float3> vert_positions = mesh.vert_positions();
170 const Span<int> corner_verts = mesh.corner_verts();
176 mask.foreach_index([&](
const int i) {
177 const float3 position = positions[
i];
178 const int face_index = face_indices[
i];
181 float min_distance_sq =
FLT_MAX;
182 int closest_vert = 0;
183 int closest_corner = 0;
184 for (
const int corner :
faces[face_index]) {
185 const int vert = corner_verts[corner];
187 if (distance_sq < min_distance_sq) {
188 min_distance_sq = distance_sq;
189 closest_corner = corner;
194 r_corner_indices[
i] = closest_corner;
197 r_positions[
i] = vert_positions[closest_vert];
200 r_distances_sq[
i] = min_distance_sq;
222 GeometryComponent::Type::Mesh, GeometryComponent::Type::PointCloud};
225 return geometry.get_component(src_type);
241 mf::Signature signature_;
245 : source_(std::move(
geometry)), domain_(domain)
247 source_.ensure_owns_direct_data();
256 mf::SignatureBuilder builder{
"Sample Nearest", signature_};
257 builder.single_input<
float3>(
"Position");
258 builder.single_output<
int>(
"Index");
266 if (!src_component_) {
271 switch (src_component_->type()) {
272 case GeometryComponent::Type::Mesh: {
274 const Mesh &mesh = *component.
get();
276 case AttrDomain::Point:
279 case AttrDomain::Edge:
282 case AttrDomain::Face:
285 case AttrDomain::Corner:
293 case GeometryComponent::Type::PointCloud: {
309 TIP_(
"The source geometry must contain a mesh or a point cloud"));
310 params.set_default_remaining_outputs();
316 std::string error_message;
319 std::make_shared<SampleNearestFunction>(std::move(
geometry), domain),
325 params.set_default_remaining_outputs();
330 params.set_output(
"Index", std::move(index));
341 int(AttrDomain::Point));
349 ntype.
ui_name =
"Sample Nearest";
351 "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)
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)
bool execute_multi_function_on_value_variant(const MultiFunction &fn, const std::shared_ptr< MultiFunction > &owned_fn, const Span< SocketValueVariant * > input_values, const Span< SocketValueVariant * > output_values, GeoNodesUserData *user_data, std::string &r_error_message)
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)