Blender V4.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 "BKE_mesh.hh"
6
8#include "BLI_task.hh"
9
10#include "node_geometry_util.hh"
11
13
15{
16 b.add_output<decl::Int>("Island Index")
17 .field_source()
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")
22 .field_source()
23 .description("The total number of mesh islands");
24}
25
27 public:
28 IslandFieldInput() : bke::MeshFieldInput(CPPType::get<int>(), "Island Index")
29 {
31 }
32
34 const AttrDomain domain,
35 const IndexMask & /*mask*/) const final
36 {
37 const Span<int2> edges = mesh.edges();
38
39 AtomicDisjointSet islands(mesh.verts_num);
40 threading::parallel_for(edges.index_range(), 1024, [&](const IndexRange range) {
41 for (const int2 &edge : edges.slice(range)) {
42 islands.join(edge[0], edge[1]);
43 }
44 });
45
46 Array<int> output(mesh.verts_num);
47 islands.calc_reduced_ids(output);
48
49 return mesh.attributes().adapt_domain<int>(
50 VArray<int>::ForContainer(std::move(output)), AttrDomain::Point, domain);
51 }
52
53 uint64_t hash() const override
54 {
55 /* Some random constant hash. */
56 return 635467354;
57 }
58
59 bool is_equal_to(const fn::FieldNode &other) const override
60 {
61 return dynamic_cast<const IslandFieldInput *>(&other) != nullptr;
62 }
63
64 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
65 {
66 return AttrDomain::Point;
67 }
68};
69
71 public:
72 IslandCountFieldInput() : bke::MeshFieldInput(CPPType::get<int>(), "Island Count")
73 {
74 category_ = Category::Generated;
75 }
76
78 const AttrDomain domain,
79 const IndexMask & /*mask*/) const final
80 {
81 const Span<int2> edges = mesh.edges();
82
83 AtomicDisjointSet islands(mesh.verts_num);
84 threading::parallel_for(edges.index_range(), 1024, [&](const IndexRange range) {
85 for (const int2 &edge : edges.slice(range)) {
86 islands.join(edge[0], edge[1]);
87 }
88 });
89
90 const int islands_num = islands.count_sets();
91 return VArray<int>::ForSingle(islands_num, mesh.attributes().domain_size(domain));
92 }
93
94 uint64_t hash() const override
95 {
96 /* Some random hash. */
97 return 45634572457;
98 }
99
100 bool is_equal_to(const fn::FieldNode &other) const override
101 {
102 return dynamic_cast<const IslandCountFieldInput *>(&other) != nullptr;
103 }
104
105 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
106 {
107 return AttrDomain::Point;
108 }
109};
110
112{
113 if (params.output_is_required("Island Index")) {
114 Field<int> field{std::make_shared<IslandFieldInput>()};
115 params.set_output("Island Index", std::move(field));
116 }
117 if (params.output_is_required("Island Count")) {
118 Field<int> field{std::make_shared<IslandCountFieldInput>()};
119 params.set_output("Island Count", std::move(field));
120 }
121}
122
123static void node_register()
124{
125 static blender::bke::bNodeType ntype;
126 geo_node_type_base(&ntype, GEO_NODE_INPUT_MESH_ISLAND, "Mesh Island", NODE_CLASS_INPUT);
127 ntype.declare = node_declare;
130}
131NOD_REGISTER_NODE(node_register)
132
133} // namespace blender::nodes::node_geo_input_mesh_island_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:404
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
static VArray ForContainer(ContainerT container)
std::optional< AttrDomain > preferred_domain(const Mesh &) 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
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
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
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
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:95
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
unsigned __int64 uint64_t
Definition stdint.h:90
Defines a node type.
Definition BKE_node.hh:218
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
NodeDeclareFunction declare
Definition BKE_node.hh:347