32 mask.foreach_index([&](
const int i) {
36 const float3 position = positions[i];
40 r_indices[i] = nearest.index;
43 r_distances_sq[i] = nearest.dist_sq;
46 r_positions[i] = nearest.co;
60 b.add_output<
decl::Int>(
"Index").dependent_field({1});
71 node->custom2 =
int(AttrDomain::Point);
85 if (tree_data.
tree ==
nullptr) {
87 r_distances_sq.
fill(0.0f);
91 mask.foreach_index([&](
const int i) {
95 const float3 position = positions[i];
98 r_indices[i] = nearest.index;
100 r_distances_sq[i] = nearest.dist_sq;
161 const Span<int> tri_faces = mesh.corner_tri_faces();
163 mask.foreach_index([&](
const int i) { r_face_indices[i] = tri_faces[tri_indices[i]]; });
174 const Span<float3> vert_positions = mesh.vert_positions();
176 const Span<int> corner_verts = mesh.corner_verts();
182 mask.foreach_index([&](
const int i) {
183 const float3 position = positions[i];
184 const int face_index = face_indices[i];
187 float min_distance_sq =
FLT_MAX;
188 int closest_vert_index = 0;
189 int closest_loop_index = 0;
190 for (
const int loop_index : faces[face_index]) {
191 const int vertex_index = corner_verts[loop_index];
193 if (distance_sq < min_distance_sq) {
194 min_distance_sq = distance_sq;
195 closest_loop_index = loop_index;
196 closest_vert_index = vertex_index;
200 r_corner_indices[i] = closest_loop_index;
203 r_positions[i] = vert_positions[closest_vert_index];
206 r_distances_sq[i] = min_distance_sq;
213 const AttrDomain domain)
215 if (!geometry.has(type)) {
223 const AttrDomain domain)
231 return geometry.get_component(src_type);
248 : source_(std::move(geometry)), domain_(domain)
254 builder.single_input<
float3>(
"Position");
255 builder.single_output<
int>(
"Index");
263 if (!src_component_) {
268 switch (src_component_->
type()) {
271 const Mesh &mesh = *component.
get();
273 case AttrDomain::Point:
276 case AttrDomain::Edge:
279 case AttrDomain::Face:
282 case AttrDomain::Corner:
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();
315 auto fn = std::make_shared<SampleNearestFunction>(std::move(geometry), domain);
328 int(AttrDomain::Point));
void free_bvhtree_from_mesh(BVHTreeFromMesh *data)
BVHTree * BKE_bvhtree_from_mesh_get(BVHTreeFromMesh *data, const Mesh *mesh, BVHCacheType bvh_cache_type, int tree_type)
void BKE_bvhtree_from_pointcloud_get(const PointCloud &pointcloud, const blender::IndexMask &points_mask, BVHTreeFromPointCloud &r_data)
void free_bvhtree_from_pointcloud(BVHTreeFromPointCloud *data)
@ BVHTREE_FROM_CORNER_TRIS
#define NODE_CLASS_GEOMETRY
int BLI_bvhtree_find_nearest(const BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
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
static std::shared_ptr< FieldOperation > Create(std::shared_ptr< const mf::MultiFunction > function, Vector< GField > inputs={})
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
SampleNearestFunction(GeometrySet geometry, AttrDomain domain)
local_group_size(16, 16) .push_constant(Type b
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
void node_register_type(bNodeType *ntype)
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_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)
static void get_closest_pointcloud_points(const PointCloud &pointcloud, const VArray< float3 > &positions, const IndexMask &mask, MutableSpan< int > r_indices, MutableSpan< float > r_distances_sq)
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 get_closest_in_bvhtree(BVHTreeFromMesh &tree_data, const VArray< float3 > &positions, const IndexMask &mask, MutableSpan< int > r_indices, MutableSpan< float > r_distances_sq, MutableSpan< float3 > r_positions)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
const EnumPropertyItem rna_enum_attribute_domain_only_mesh_items[]
BVHTree_NearestPointCallback nearest_callback
BVHTree_NearestPointCallback nearest_callback
void ensure_owns_direct_data()
void(* initfunc)(bNodeTree *ntree, bNode *node)
NodeGeometryExecFunction geometry_node_execute
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
NodeDeclareFunction declare