Blender V5.0
node_geo_sample_nearest.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "DNA_mesh_types.h"
7
8#include "BLI_math_vector.hh"
9
10#include "BKE_bvhutils.hh"
11
12#include "NOD_rna_define.hh"
13
15#include "UI_resources.hh"
16
17#include "RNA_enum_types.hh"
18
19#include "node_geometry_util.hh"
20
21namespace blender::nodes {
22
24 const VArray<float3> &positions,
25 const IndexMask &mask,
26 const MutableSpan<int> r_indices,
27 const MutableSpan<float> r_distances_sq,
28 const MutableSpan<float3> r_positions)
29{
30 BLI_assert(positions.size() >= r_indices.size());
31 BLI_assert(positions.size() >= r_distances_sq.size());
32 BLI_assert(positions.size() >= r_positions.size());
33
34 mask.foreach_index([&](const int i) {
35 BVHTreeNearest nearest;
36 nearest.index = -1;
37 nearest.dist_sq = FLT_MAX;
38 const float3 position = positions[i];
40 tree_data.tree, position, &nearest, tree_data.nearest_callback, &tree_data);
41 if (!r_indices.is_empty()) {
42 r_indices[i] = nearest.index;
43 }
44 if (!r_distances_sq.is_empty()) {
45 r_distances_sq[i] = nearest.dist_sq;
46 }
47 if (!r_positions.is_empty()) {
48 r_positions[i] = nearest.co;
49 }
50 });
51}
52
53} // namespace blender::nodes
54
56
58{
59 b.add_input<decl::Geometry>("Geometry")
60 .supported_type({GeometryComponent::Type::Mesh, GeometryComponent::Type::PointCloud})
61 .description("Mesh or point cloud to find the nearest point on");
62 b.add_input<decl::Vector>("Sample Position").implicit_field(NODE_DEFAULT_INPUT_POSITION_FIELD);
63 b.add_output<decl::Int>("Index").dependent_field({1});
64}
65
66static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
67{
68 layout->prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
69}
70
71static void node_init(bNodeTree * /*tree*/, bNode *node)
72{
73 node->custom1 = CD_PROP_FLOAT;
74 node->custom2 = int(AttrDomain::Point);
75}
76
78 const VArray<float3> &positions,
79 const IndexMask &mask,
80 MutableSpan<int> r_indices,
81 MutableSpan<float> r_distances_sq)
82{
83 BLI_assert(positions.size() >= r_indices.size());
84 if (tree_data.tree == nullptr) {
85 r_indices.fill(0);
86 r_distances_sq.fill(0.0f);
87 return;
88 }
89
90 mask.foreach_index([&](const int i) {
91 BVHTreeNearest nearest;
92 nearest.index = -1;
93 nearest.dist_sq = FLT_MAX;
94 const float3 position = positions[i];
96 position,
97 &nearest,
98 tree_data.nearest_callback,
99 &const_cast<bke::BVHTreeFromPointCloud &>(tree_data));
100 r_indices[i] = nearest.index;
101 if (!r_distances_sq.is_empty()) {
102 r_distances_sq[i] = nearest.dist_sq;
103 }
104 });
105}
106
107static void get_closest_mesh_points(const Mesh &mesh,
108 const VArray<float3> &positions,
109 const IndexMask &mask,
110 const MutableSpan<int> r_point_indices,
111 const MutableSpan<float> r_distances_sq,
112 const MutableSpan<float3> r_positions)
113{
114 BLI_assert(mesh.verts_num > 0);
115 bke::BVHTreeFromMesh tree_data = mesh.bvh_verts();
116 get_closest_in_bvhtree(tree_data, positions, mask, r_point_indices, r_distances_sq, r_positions);
117}
118
119static void get_closest_mesh_edges(const Mesh &mesh,
120 const VArray<float3> &positions,
121 const IndexMask &mask,
122 const MutableSpan<int> r_edge_indices,
123 const MutableSpan<float> r_distances_sq,
124 const MutableSpan<float3> r_positions)
125{
126 BLI_assert(mesh.edges_num > 0);
127 bke::BVHTreeFromMesh tree_data = mesh.bvh_edges();
128 get_closest_in_bvhtree(tree_data, positions, mask, r_edge_indices, r_distances_sq, r_positions);
129}
130
131static void get_closest_mesh_tris(const Mesh &mesh,
132 const VArray<float3> &positions,
133 const IndexMask &mask,
134 const MutableSpan<int> r_tri_indices,
135 const MutableSpan<float> r_distances_sq,
136 const MutableSpan<float3> r_positions)
137{
138 BLI_assert(mesh.faces_num > 0);
139 bke::BVHTreeFromMesh tree_data = mesh.bvh_corner_tris();
140 get_closest_in_bvhtree(tree_data, positions, mask, r_tri_indices, r_distances_sq, r_positions);
141}
142
143static void get_closest_mesh_faces(const Mesh &mesh,
144 const VArray<float3> &positions,
145 const IndexMask &mask,
146 const MutableSpan<int> r_face_indices,
147 const MutableSpan<float> r_distances_sq,
148 const MutableSpan<float3> r_positions)
149{
150 BLI_assert(mesh.faces_num > 0);
151
152 Array<int> tri_indices(positions.size());
153 get_closest_mesh_tris(mesh, positions, mask, tri_indices, r_distances_sq, r_positions);
154
155 const Span<int> tri_faces = mesh.corner_tri_faces();
156
157 mask.foreach_index([&](const int i) { r_face_indices[i] = tri_faces[tri_indices[i]]; });
158}
159
160/* The closest corner is defined to be the closest corner on the closest face. */
161static void get_closest_mesh_corners(const Mesh &mesh,
162 const VArray<float3> &positions,
163 const IndexMask &mask,
164 const MutableSpan<int> r_corner_indices,
165 const MutableSpan<float> r_distances_sq,
166 const MutableSpan<float3> r_positions)
167{
168 const Span<float3> vert_positions = mesh.vert_positions();
169 const OffsetIndices faces = mesh.faces();
170 const Span<int> corner_verts = mesh.corner_verts();
171
172 BLI_assert(mesh.corners_num > 0);
173 Array<int> face_indices(positions.size());
174 get_closest_mesh_faces(mesh, positions, mask, face_indices, {}, {});
175
176 mask.foreach_index([&](const int i) {
177 const float3 position = positions[i];
178 const int face_index = face_indices[i];
179
180 /* Find the closest vertex in the face. */
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];
186 const float distance_sq = math::distance_squared(position, vert_positions[vert]);
187 if (distance_sq < min_distance_sq) {
188 min_distance_sq = distance_sq;
189 closest_corner = corner;
190 closest_vert = vert;
191 }
192 }
193 if (!r_corner_indices.is_empty()) {
194 r_corner_indices[i] = closest_corner;
195 }
196 if (!r_positions.is_empty()) {
197 r_positions[i] = vert_positions[closest_vert];
198 }
199 if (!r_distances_sq.is_empty()) {
200 r_distances_sq[i] = min_distance_sq;
201 }
202 });
203}
204
206 const GeometryComponent::Type type,
207 const AttrDomain domain)
208{
209 if (!geometry.has(type)) {
210 return false;
211 }
212 const GeometryComponent &component = *geometry.get_component(type);
213 return component.attribute_domain_size(domain) != 0;
214}
215
217 const AttrDomain domain)
218{
219 /* Choose the other component based on a consistent order, rather than some more complicated
220 * heuristic. This is the same order visible in the spreadsheet and used in the ray-cast node. */
221 static const Array<GeometryComponent::Type> supported_types = {
222 GeometryComponent::Type::Mesh, GeometryComponent::Type::PointCloud};
223 for (const GeometryComponent::Type src_type : supported_types) {
224 if (component_is_available(geometry, src_type, domain)) {
225 return geometry.get_component(src_type);
226 }
227 }
228
229 return nullptr;
230}
231
232class SampleNearestFunction : public mf::MultiFunction {
233 GeometrySet source_;
234 AttrDomain domain_;
235
236 const GeometryComponent *src_component_;
237
238 /* Point clouds do not cache BVH trees currently; avoid rebuilding it on every call. */
239 bke::BVHTreeFromPointCloud pointcloud_bvh = {};
240
241 mf::Signature signature_;
242
243 public:
245 : source_(std::move(geometry)), domain_(domain)
246 {
247 source_.ensure_owns_direct_data();
248 this->src_component_ = find_source_component(source_, domain_);
249 if (src_component_ && src_component_->type() == bke::GeometryComponent::Type::PointCloud) {
250 const PointCloudComponent &component = *static_cast<const PointCloudComponent *>(
251 src_component_);
252 const PointCloud &points = *component.get();
253 pointcloud_bvh = bke::bvhtree_from_pointcloud_get(points, IndexMask(points.totpoint));
254 }
255
256 mf::SignatureBuilder builder{"Sample Nearest", signature_};
257 builder.single_input<float3>("Position");
258 builder.single_output<int>("Index");
259 this->set_signature(&signature_);
260 }
261
262 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
263 {
264 const VArray<float3> &positions = params.readonly_single_input<float3>(0, "Position");
265 MutableSpan<int> indices = params.uninitialized_single_output<int>(1, "Index");
266 if (!src_component_) {
268 return;
269 }
270
271 switch (src_component_->type()) {
272 case GeometryComponent::Type::Mesh: {
273 const MeshComponent &component = *static_cast<const MeshComponent *>(src_component_);
274 const Mesh &mesh = *component.get();
275 switch (domain_) {
276 case AttrDomain::Point:
277 get_closest_mesh_points(mesh, positions, mask, indices, {}, {});
278 break;
279 case AttrDomain::Edge:
280 get_closest_mesh_edges(mesh, positions, mask, indices, {}, {});
281 break;
282 case AttrDomain::Face:
283 get_closest_mesh_faces(mesh, positions, mask, indices, {}, {});
284 break;
285 case AttrDomain::Corner:
286 get_closest_mesh_corners(mesh, positions, mask, indices, {}, {});
287 break;
288 default:
289 break;
290 }
291 break;
292 }
293 case GeometryComponent::Type::PointCloud: {
294 get_closest_pointcloud_points(pointcloud_bvh, positions, mask, indices, {});
295 break;
296 }
297 default:
298 break;
299 }
300 }
301};
302
304{
305 GeometrySet geometry = params.extract_input<GeometrySet>("Geometry");
306 const AttrDomain domain = AttrDomain(params.node().custom2);
307 if (geometry.has_curves() && !geometry.has_mesh() && !geometry.has_pointcloud()) {
308 params.error_message_add(NodeWarningType::Error,
309 TIP_("The source geometry must contain a mesh or a point cloud"));
310 params.set_default_remaining_outputs();
311 return;
312 }
313
314 auto sample_position = params.extract_input<bke::SocketValueVariant>("Sample Position");
315
316 std::string error_message;
319 std::make_shared<SampleNearestFunction>(std::move(geometry), domain),
320 {&sample_position},
321 {&index},
322 params.user_data(),
323 error_message))
324 {
325 params.set_default_remaining_outputs();
326 params.error_message_add(NodeWarningType::Error, std::move(error_message));
327 return;
328 }
329
330 params.set_output("Index", std::move(index));
331}
332
333static void node_rna(StructRNA *srna)
334{
336 "domain",
337 "Domain",
338 "",
341 int(AttrDomain::Point));
342}
343
344static void node_register()
345{
346 static blender::bke::bNodeType ntype;
347
348 geo_node_type_base(&ntype, "GeometryNodeSampleNearest", GEO_NODE_SAMPLE_NEAREST);
349 ntype.ui_name = "Sample Nearest";
350 ntype.ui_description =
351 "Find the element of a geometry closest to a position. Similar to the \"Index of Nearest\" "
352 "node";
353 ntype.enum_name_legacy = "SAMPLE_NEAREST";
355 ntype.initfunc = node_init;
356 ntype.declare = node_declare;
360
361 node_rna(ntype.rna_ext.srna);
362}
364
365} // namespace blender::nodes::node_geo_sample_nearest_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_SAMPLE_NEAREST
#define BLI_assert(a)
Definition BLI_assert.h:46
int BLI_bvhtree_find_nearest(const BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
#define TIP_(msgid)
@ CD_PROP_FLOAT
@ NODE_DEFAULT_INPUT_POSITION_FIELD
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr bool is_empty() const
Definition BLI_span.hh:509
constexpr void fill(const T &value) const
Definition BLI_span.hh:517
int attribute_domain_size(AttrDomain domain) const
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
static ushort indices[]
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
static char faces[256]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
BVHTreeFromPointCloud bvhtree_from_pointcloud_get(const PointCloud &pointcloud, const IndexMask &points_mask)
Definition bvhutils.cc:842
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_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[]
#define FLT_MAX
Definition stdcycles.h:14
StructRNA * srna
int corners_num
int edges_num
int faces_num
int verts_num
int16_t custom1
int16_t custom2
BVHTree_NearestPointCallback nearest_callback
BVHTree_NearestPointCallback nearest_callback
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
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)
i
Definition text_draw.cc:230
PointerRNA * ptr
Definition wm_files.cc:4238