31 .
description(
"Mesh to find the closest surface point on");
32 if (
node !=
nullptr) {
34 b.add_input(data_type,
"Value"_ustr).hide_value().evaluated_geometry_field();
38 .evaluated_geometry_field()
40 "Splits the faces of the input mesh into groups which can be sampled individually");
41 auto &sample_position =
b.add_input<
decl::Vector>(
"Sample Position"_ustr)
44 auto &sample_group_id =
b.add_input<
decl::Int>(
"Sample Group ID"_ustr)
48 std::array<int, 2> dynamic_inputs = {sample_position.index(), sample_group_id.index()};
49 if (
node !=
nullptr) {
51 b.add_output(data_type,
"Value"_ustr)
52 .inferred_structure_type(dynamic_inputs)
53 .propagate_references(dynamic_inputs);
56 .inferred_structure_type(dynamic_inputs)
57 .propagate_references(dynamic_inputs)
59 "Whether the sampling was successful. It can fail when the sampled group is empty");
101 : source_(std::move(
geometry)), group_id_field_(std::move(group_id_field))
103 source_.ensure_owns_direct_data();
104 static const mf::Signature
signature = []() {
106 mf::SignatureBuilder builder{
"Sample Nearest Surface",
signature};
107 builder.single_input<
float3>(
"Position");
108 builder.single_input<
int>(
"Sample ID");
109 builder.single_output<
int>(
"Triangle Index");
110 builder.single_output<
float3>(
"Barycentric Weight");
111 builder.single_output<
bool>(
"Is Valid", mf::ParamFlag::SupportsUnusedOutput);
119 mutex_.ensure([&]() {
120 const Mesh &
mesh = *source_.get_mesh();
125 field_evaluator.
add(group_id_field_);
132 const int groups_num = group_masks.
size();
133 group_indices_.reserve(groups_num);
134 for (
const IndexMask &group_mask : group_masks) {
135 group_indices_.add_new(group_ids[group_mask.first()]);
138 if (groups_num == 1) {
139 single_tree_ = &
mesh.bvh_tris();
142 bvh_trees_.reinitialize(groups_num);
147 for (
const int group_i : range) {
148 const IndexMask &group_mask = group_masks[group_i];
153 [&](
const int group_i) {
return group_masks[group_i].
size(); },
mesh.faces_num));
163 const VArray<int> &sample_ids = params.readonly_single_input<
int>(1,
"Sample ID");
164 MutableSpan<int> triangle_index = params.uninitialized_single_output<
int>(2,
"Triangle Index");
166 3,
"Barycentric Weight");
167 MutableSpan<bool> is_valid_span = params.uninitialized_single_output_if_required<
bool>(
170 mask.foreach_index([&](
const int i) {
172 const int sample_id = sample_ids[i];
173 const int group_index = group_indices_.index_of_try(sample_id);
174 if (group_index == -1) {
175 triangle_index[i] = -1;
176 bary_weights[i] =
float3(0, 0, 0);
177 if (!is_valid_span.is_empty()) {
178 is_valid_span[i] =
false;
182 const bke::bvh::Tree &
bvh = single_tree_ ? *single_tree_ : bvh_trees_[group_index];
183 const std::optional<bke::bvh::ClosestPointResult>
result =
bvh.closest_point(
position);
185 triangle_index[i] = -1;
186 bary_weights[i] =
float3(0, 0, 0);
187 if (!is_valid_span.is_empty()) {
188 is_valid_span[i] =
false;
192 triangle_index[i] =
result->index;
193 bary_weights[i] =
result->bary_coord;
194 if (!is_valid_span.is_empty()) {
195 is_valid_span[i] =
true;
209 static constexpr int8_t id = 0;
211 hash.add(source_.get_mesh());
221 if (
mesh ==
nullptr) {
225 if (
mesh->verts_num == 0) {
229 if (
mesh->faces_num == 0) {
240 std::string error_message;
246 std::make_shared<SampleNearestSurfaceFunction>(
geometry, group_id_field),
247 {&sample_position, &sample_group_id},
248 {&triangle_index, &bary_weights, &is_valid},
259 std::make_shared<bke::mesh_surface_sample::BaryWeightSampleFn>(
geometry,
261 {&triangle_index, &bary_weights},
271 params.
set_output(
"Value"_ustr, std::move(sample_value));
272 params.
set_output(
"Is Valid"_ustr, std::move(is_valid));
293 ntype.
ui_name =
"Sample Nearest Surface";
295 "Calculate the interpolated value of a mesh attribute on the closest point of its surface";
#define NODE_CLASS_GEOMETRY
#define GEO_NODE_SAMPLE_NEAREST_SURFACE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
static Vector< IndexMask, 4 > from_group_ids(const VArray< int > &group_ids, LinearAllocator<> &memory)
constexpr IndexRange()=default
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)
const bke::bNodeType & node_type() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
const bNodeSocket & other_socket() const
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
GeoNodesUserData * user_data() const
void set_default_remaining_outputs()
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
bNode & add_node(UString idname)
Vector< SocketDeclaration * > inputs
StructureType structure_type
ExecutionHints get_execution_hints() const override
SampleNearestSurfaceFunction(GeometrySet geometry, Field< int > group_id_field)
void hash_unique(UniqueHashBytes &hash) const override
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
void prepare_for_execution() const override
~SampleNearestSurfaceFunction() override=default
void node_register_type(bNodeType &ntype)
std::optional< eCustomDataType > socket_type_to_custom_data_type(eNodeSocketDatatype type)
const EnumPropertyItem * attribute_type_type_with_socket_fn(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
static void node_register()
static void node_gather_link_searches(GatherLinkSearchOpParams ¶ms)
static void node_rna(StructRNA *srna)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_init(bNodeTree *, bNode *node)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
void search_link_ops_for_declarations(GatherLinkSearchOpParams ¶ms, Span< SocketDeclaration * > declarations)
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)
const EnumPropertyItem rna_enum_attribute_type_items[]
@ NODE_DEFAULT_INPUT_POSITION_FIELD
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static constexpr int _160
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
NodeDeclareFunction declare
nodes::NodeDeclaration * static_declaration
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)