31 .description(
"Geometry to find the closest point on");
34 .evaluated_geometry_field()
36 "Splits the elements of the input geometry into groups which can be sampled "
38 auto &sample_position =
b.add_input<
decl::Vector>(
"Sample Position"_ustr,
"Source Position"_ustr)
40 .structure_type(StructureType::Dynamic);
41 auto &sample_group_id =
b.add_input<
decl::Int>(
"Sample Group ID"_ustr)
43 .structure_type(StructureType::Dynamic);
45 const std::array<int, 2> dynamic_inputs = {sample_position.index(), sample_group_id.index()};
47 .inferred_structure_type(dynamic_inputs)
48 .propagate_references(dynamic_inputs);
50 .inferred_structure_type(dynamic_inputs)
51 .propagate_references(dynamic_inputs);
53 .inferred_structure_type(dynamic_inputs)
54 .propagate_references(dynamic_inputs)
56 "Whether the sampling was successful. It can fail when the sampled group is empty");
68 node->storage = node_storage;
85 return group == -1 ?
nullptr : this->trees[group];
94 mutable GroupTrees mesh_trees_;
95 mutable GroupTrees pointcloud_trees_;
101 : target_(std::move(target)), group_id_field_(std::move(group_id_field)), type_(type)
103 static const mf::Signature
signature = []() {
105 mf::SignatureBuilder builder{
"Geometry Proximity",
signature};
106 builder.single_input<
float3>(
"Source Position");
107 builder.single_input<
int>(
"Sample ID");
108 builder.single_output<
float3>(
"Position", mf::ParamFlag::SupportsUnusedOutput);
109 builder.single_output<
float>(
"Distance", mf::ParamFlag::SupportsUnusedOutput);
110 builder.single_output<
bool>(
"Is Valid", mf::ParamFlag::SupportsUnusedOutput);
129 const int groups_num = group_masks.
size();
131 r_trees.ids.
reserve(groups_num);
132 for (
const IndexMask &group_mask : group_masks) {
133 r_trees.ids.
add_new(group_ids[group_mask.first()]);
135 r_trees.owned_trees.reinitialize(groups_num);
136 r_trees.trees.reinitialize(groups_num);
141 for (
const int group_i : range) {
142 const IndexMask &group_mask = group_masks[group_i];
143 if (group_mask.
size() == group_ids.
size()) {
144 r_trees.trees[group_i] = &cached_tree();
147 r_trees.owned_trees[group_i] = build_tree(group_mask);
148 r_trees.trees[group_i] = &*r_trees.owned_trees[group_i];
153 [&](
const int group_i) {
return group_masks[group_i].
size(); }, group_ids.
size()));
160 field_evaluator.
add(group_id_field);
166 [&]() ->
const bke::bvh::Tree & {
return pointcloud.bvh_tree(); },
176 const int domain_size =
mesh.attributes().domain_size(domain);
179 field_evaluator.
add(group_id_field);
229 0,
"Source Position");
230 const VArray<int> &sample_ids = params.readonly_single_input<
int>(1,
"Sample ID");
235 MutableSpan<bool> is_valid_span = params.uninitialized_single_output_if_required<
bool>(
238 mask.foreach_index([&](
const int i) {
239 const float3 sample_position = sample_positions[i];
240 const int sample_id = sample_ids[i];
242 const bke::bvh::Tree *pointcloud_bvh = pointcloud_trees_.find(sample_id);
243 if (!mesh_bvh && !pointcloud_bvh) {
244 if (!positions.is_empty()) {
245 positions[i] =
float3(0, 0, 0);
247 if (!is_valid_span.is_empty()) {
248 is_valid_span[i] =
false;
250 if (!distances.is_empty()) {
258 float3 nearest_position(0.0f);
259 float nearest_distance =
FLT_MAX;
264 nearest_position =
result->position;
268 if (pointcloud_bvh) {
269 if (
const std::optional<bke::bvh::ClosestPointResult>
result =
270 pointcloud_bvh->
closest_point(sample_position, nearest_distance))
272 nearest_position =
result->position;
277 if (!positions.is_empty()) {
278 positions[i] = nearest_position;
280 if (!is_valid_span.is_empty()) {
281 is_valid_span[i] =
true;
283 if (!distances.is_empty()) {
284 distances[i] = nearest_distance;
298 static constexpr int8_t id = 0;
300 hash.add(target_.get_mesh());
301 hash.add(target_.get_pointcloud());
309 mutex_.ensure([&]() {
311 const PointCloud &pointcloud = *target_.get_pointcloud();
314 if (target_.has_mesh()) {
315 const Mesh &
mesh = *target_.get_mesh();
339 std::string error_message;
344 std::make_shared<ProximityFunction>(
345 std::move(target), target_type, std::move(group_id_field)),
346 {&sample_position, &sample_group_id},
358 params.
set_output(
"Is Valid"_ustr, std::move(is_valid));
368 "Calculate the proximity to the target's points (faster than the other modes)"},
373 "Calculate the proximity to the target's edges"},
378 "Calculate the proximity to the target's faces"},
379 {0,
nullptr, 0,
nullptr,
nullptr},
385 "Element of the target geometry to calculate the distance from",
386 target_element_items,
396 ntype.
ui_name =
"Geometry Proximity";
397 ntype.
ui_description =
"Compute the closest location on the target geometry";
#define NODE_STORAGE_FUNCS(StorageT)
#define NODE_CLASS_GEOMETRY
#define GEO_NODE_PROXIMITY
General operations for point clouds.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define BLI_assert_unreachable()
static Vector< IndexMask, 4 > from_group_ids(const VArray< int > &group_ids, LinearAllocator<> &memory)
constexpr IndexRange()=default
int64_t index_of_try(const Key &key) const
void reserve(const int64_t n)
void add_new(const Key &key)
std::optional< ClosestPointResult > closest_point(const float3 &point, float radius=std::numeric_limits< float >::max()) const
static Tree from_edges(Span< float3 > positions, Span< int2 > edges, const IndexMask &mask)
static Tree from_points(Span< float3 > positions, const IndexMask &mask)
static Tree from_tris(const Mesh &mesh, const IndexMask &mask, bool map_global_indices)
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
const Signature & signature() const
void set_signature(const Signature *signature)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
GeoNodesUserData * user_data() const
const bNode & node() const
void set_default_remaining_outputs()
void set_output(const UString identifier, T &&value)
NodeDefaultInputType default_input_type
bool only_realized_data() const
static void build_group_trees(const VArray< int > &group_ids, const FunctionRef< const bke::bvh::Tree &()> cached_tree, const FunctionRef< bke::bvh::Tree(const IndexMask &)> build_tree, GroupTrees &r_trees)
~ProximityFunction() override=default
void hash_unique(UniqueHashBytes &hash) const override
void prepare_for_execution() const override
ExecutionHints get_execution_hints() const override
bke::AttrDomain get_domain_on_mesh() const
ProximityFunction(GeometrySet target, GeometryNodeProximityTargetType type, const Field< int > &group_id_field)
void init_for_pointcloud(const PointCloud &pointcloud, const Field< int > &group_id_field) const
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
void init_for_mesh(const Mesh &mesh, const Field< int > &group_id_field) const
#define storage(slot, qualifiers)
float distance(float, float) RET
void node_register_type(bNodeType &ntype)
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
T distance(const T &a, const T &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_register()
static void node_declare(NodeDeclarationBuilder &b)
static void node_rna(StructRNA *srna)
static void geo_proximity_init(bNodeTree *, bNode *node)
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 parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
auto individual_task_sizes(Fn &&fn, const std::optional< int64_t > full_size=std::nullopt)
GeometryNodeProximityTargetType
@ GEO_NODE_PROX_TARGET_EDGES
@ GEO_NODE_PROX_TARGET_FACES
@ GEO_NODE_PROX_TARGET_POINTS
@ NODE_DEFAULT_INPUT_POSITION_FIELD
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
void node_free_standard_storage(bNode *node)
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
GeometryNodeProximityTargetType target_element
bool has_pointcloud() const
void ensure_owns_direct_data()
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
std::string ui_description
NodeGeometryExecFunction geometry_node_execute
void(*) initfunc(bNodeTree *ntree, bNode *node)
const char * enum_name_legacy
NodeDeclareFunction declare
UniqueHash ensure(const GFieldRef &field)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)