Blender V5.3
node_geo_input_mesh_island.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"
6
8#include "BLI_task.hh"
9
10#include "node_geometry_util.hh"
11
13
15{
16 b.add_output<decl::Int>("Island Index"_ustr)
17 .structure_type(StructureType::Field)
19 "The index of the each vertex's island. Indices are based on the "
20 "lowest vertex index contained in each island");
21 b.add_output<decl::Int>("Island Count"_ustr)
22 .structure_type(StructureType::Field)
23 .description("The total number of mesh islands");
24}
25
27 public:
28 IslandFieldInput() : bke::MeshFieldInput(CPPType::get<int>(), "Island Index") {}
29
31 const AttrDomain domain,
32 const IndexMask & /*mask*/) const final
33 {
34 const Span<int2> edges = mesh.edges();
35
37 threading::parallel_for(edges.index_range(), 1024, [&](const IndexRange range) {
38 for (const int2 &edge : edges.slice(range)) {
39 islands.join(edge[0], edge[1]);
40 }
41 });
42
43 Array<int> output(mesh.verts_num);
44 islands.calc_reduced_ids(output);
45
46 return mesh.attributes().adapt_domain<int>(
47 VArray<int>::from_container(std::move(output)), AttrDomain::Point, domain);
48 }
49
50 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
51 {
52 static constexpr int8_t id = 0;
53 hash.add(&id);
54 }
55
56 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
57 {
58 return AttrDomain::Point;
59 }
60
61 bke::NativeFieldDomain native_domain_info(const Mesh & /*mesh*/) const override
62 {
63 /* Domain interpolation will not mix values for separate islands. */
65 }
66};
67
69 public:
70 IslandCountFieldInput() : bke::MeshFieldInput(CPPType::get<int>(), "Island Count") {}
71
73 const AttrDomain domain,
74 const IndexMask & /*mask*/) const final
75 {
76 const Span<int2> edges = mesh.edges();
77
79 threading::parallel_for(edges.index_range(), 1024, [&](const IndexRange range) {
80 for (const int2 &edge : edges.slice(range)) {
81 islands.join(edge[0], edge[1]);
82 }
83 });
84
85 const int islands_num = islands.count_sets();
86 return VArray<int>::from_single(islands_num, mesh.attributes().domain_size(domain));
87 }
88
89 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
90 {
91 static constexpr int8_t id = 0;
92 hash.add(&id);
93 }
94
95 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
96 {
97 return AttrDomain::Point;
98 }
99
100 bke::NativeFieldDomain native_domain_info(const Mesh & /*mesh*/) const override
101 {
103 }
104};
105
107{
108 if (params.output_is_required("Island Index"_ustr)) {
109 params.set_output("Island Index"_ustr, Field<int>::from_input<IslandFieldInput>());
110 }
111 if (params.output_is_required("Island Count"_ustr)) {
112 params.set_output("Island Count"_ustr, Field<int>::from_input<IslandCountFieldInput>());
113 }
114}
115
116static void node_register()
117{
118 static bke::bNodeType ntype;
119 geo_node_type_base(&ntype, "GeometryNodeInputMeshIsland"_ustr, GEO_NODE_INPUT_MESH_ISLAND);
120 ntype.ui_name = "Mesh Island";
121 ntype.ui_description = "Retrieve information about separate connected regions in a mesh";
122 ntype.enum_name_legacy = "MESH_ISLAND";
123 ntype.nclass = NODE_CLASS_INPUT;
124 ntype.declare = node_declare;
127}
128NOD_REGISTER_NODE(node_register)
129
130} // namespace blender::nodes::node_geo_input_mesh_island_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_INPUT_MESH_ISLAND
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
static VArray from_single(T value, const int64_t size)
static VArray from_container(ContainerT container)
uint64_t hash() const
Definition field.cc:185
static Field from_input(Args &&...args)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
bke::NativeFieldDomain native_domain_info(const Mesh &) const override
std::optional< AttrDomain > preferred_domain(const Mesh &) const override
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &) const override
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
std::optional< AttrDomain > preferred_domain(const Mesh &) const override
bke::NativeFieldDomain native_domain_info(const Mesh &) const override
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &) const override
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed char int8_t
Definition BKE_text.h:76
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393