33 "Splits the elements of the input geometry into groups which can be sampled "
35 b.add_input<
decl::Vector>(
"Sample Position",
"Source Position")
37 b.add_input<
decl::Int>(
"Sample Group ID").hide_value().supports_field();
38 b.add_output<
decl::Vector>(
"Position").dependent_field({2, 3}).reference_pass_all();
39 b.add_output<
decl::Float>(
"Distance").dependent_field({2, 3}).reference_pass_all();
41 .dependent_field({2, 3})
43 "Whether the sampling was successful. It can fail when the sampled group is empty");
55 node->storage = node_storage;
74 : target_(std::move(target)), type_(type)
79 builder.single_input<
float3>(
"Source Position");
80 builder.single_input<
int>(
"Sample ID");
81 builder.single_output<
float3>(
"Position", mf::ParamFlag::SupportsUnusedOutput);
82 builder.single_output<
float>(
"Distance", mf::ParamFlag::SupportsUnusedOutput);
83 builder.single_output<
bool>(
"Is Valid", mf::ParamFlag::SupportsUnusedOutput);
105 field_evaluator.add(group_id_field);
106 field_evaluator.evaluate();
107 const VArray<int> group_ids = field_evaluator.get_evaluated<
int>(0);
111 const int groups_num = group_masks.
size();
114 bvh_trees_.
resize(groups_num);
119 for (
const int group_i :
range) {
120 const IndexMask &group_mask = group_masks[group_i];
129 [&](
const int group_i) {
return group_masks[group_i].
size(); }, pointcloud.
totpoint));
139 field_evaluator.
add(group_id_field);
140 field_evaluator.evaluate();
141 const VArray<int> group_ids = field_evaluator.get_evaluated<
int>(0);
145 const int groups_num = group_masks.
size();
148 bvh_trees_.
resize(groups_num);
153 for (
const int group_i :
range) {
154 const IndexMask &group_mask = group_masks[group_i];
172 [&](
const int group_i) {
return group_masks[group_i].
size(); }, domain_size));
192 0,
"Source Position");
193 const VArray<int> &sample_ids =
params.readonly_single_input<
int>(1,
"Sample ID");
201 mask.foreach_index([&](
const int i) {
202 const float3 sample_position = sample_positions[i];
203 const int sample_id = sample_ids[i];
204 const int group_index = group_indices_.
index_of_try(sample_id);
205 if (group_index == -1) {
206 if (!positions.is_empty()) {
207 positions[i] =
float3(0, 0, 0);
209 if (!is_valid_span.is_empty()) {
210 is_valid_span[i] =
false;
212 if (!distances.is_empty()) {
217 const BVHTrees &trees = bvh_trees_[group_index];
223 if (trees.mesh_bvh.
tree !=
nullptr) {
230 if (trees.pointcloud_bvh.
tree !=
nullptr) {
238 if (!positions.is_empty()) {
239 positions[i] = nearest.
co;
241 if (!is_valid_span.is_empty()) {
242 is_valid_span[i] =
true;
244 if (!distances.is_empty()) {
245 distances[i] = std::sqrt(nearest.
dist_sq);
257 params.set_default_remaining_outputs();
266 auto proximity_fn = std::make_unique<ProximityFunction>(
269 std::move(proximity_fn), {std::move(position_field), std::move(sample_id_field)});
283 "Calculate the proximity to the target's points (faster than the other modes)"},
288 "Calculate the proximity to the target's edges"},
293 "Calculate the proximity to the target's faces"},
294 {0,
nullptr, 0,
nullptr,
nullptr},
300 "Element of the target geometry to calculate the distance from",
301 target_element_items,
311 ntype.
ui_name =
"Geometry Proximity";
312 ntype.
ui_description =
"Compute the closest location on the target geometry";
#define NODE_STORAGE_FUNCS(StorageT)
#define NODE_CLASS_GEOMETRY
#define GEO_NODE_PROXIMITY
#define BLI_assert_unreachable()
int BLI_bvhtree_find_nearest(const BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
@ NODE_DEFAULT_INPUT_POSITION_FIELD
GeometryNodeProximityTargetType
@ GEO_NODE_PROX_TARGET_EDGES
@ GEO_NODE_PROX_TARGET_POINTS
@ GEO_NODE_PROX_TARGET_FACES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
int64_t index_of_try(const Key &key) const
void resize(const int64_t new_size)
int add(GField field, GVArray *varray_ptr)
static std::shared_ptr< FieldOperation > Create(std::shared_ptr< const mf::MultiFunction > function, Vector< GField > inputs={})
const Signature & signature() const
void set_signature(const Signature *signature)
static Vector< IndexMask, 4 > from_group_ids(const VArray< int > &group_ids, IndexMaskMemory &memory, VectorSet< int > &r_index_by_group_id)
void init_for_pointcloud(const PointCloud &pointcloud, const Field< int > &group_id_field)
~ProximityFunction() override=default
bke::AttrDomain get_domain_on_mesh() const
ProximityFunction(GeometrySet target, GeometryNodeProximityTargetType type, const Field< int > &group_id_field)
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)
void * MEM_callocN(size_t len, const char *str)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void node_register_type(bNodeType &ntype)
BVHTreeFromMesh bvhtree_from_mesh_edges_init(const Mesh &mesh, const IndexMask &edges_mask)
BVHTreeFromMesh bvhtree_from_mesh_tris_init(const Mesh &mesh, const IndexMask &faces_mask)
BVHTreeFromMesh bvhtree_from_mesh_verts_init(const Mesh &mesh, const IndexMask &verts_mask)
BVHTreeFromPointCloud bvhtree_from_pointcloud_get(const PointCloud &pointcloud, const IndexMask &points_mask)
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))
static void node_geo_exec(GeoNodeExecParams params)
static void node_register()
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
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)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
BVHTree_NearestPointCallback nearest_callback
BVHTree_NearestPointCallback nearest_callback
bool has_pointcloud() const
void ensure_owns_direct_data()
const PointCloud * get_pointcloud() const
const Mesh * get_mesh() const
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)