Blender V5.3
node_geo_input_mesh_face_neighbors.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
7#include "BKE_mesh_mapping.hh"
8
9#include "BLI_task.hh"
10
11#include "node_geometry_util.hh"
12
14
16{
17 b.add_output<decl::Int>("Vertex Count"_ustr)
18 .structure_type(StructureType::Field)
19 .description("Number of edges or points in the face");
20 b.add_output<decl::Int>("Face Count"_ustr)
21 .structure_type(StructureType::Field)
22 .description("Number of faces which share an edge with the face");
23}
24
26 const Span<int> indices,
27 const int max)
28{
29 int num = 0;
30 for (const int i : indices) {
31 num += values[i].size();
32 if (max <= num) {
33 return true;
34 }
35 }
36 return false;
37}
38
39static int unique_num(const GroupedSpan<int> values, const Span<int> indices)
40{
41 if (large_enough_total_size(values, indices, 100)) {
42 Set<int, 16> unique_values;
43 for (const int i : indices) {
44 unique_values.add_multiple(values[i]);
45 }
46 return unique_values.size();
47 }
48 Vector<int, 16> unique_values;
49 for (const int i : indices) {
50 unique_values.extend_non_duplicates(values[i]);
51 }
52 return unique_values.size();
53}
54
56{
57 const GroupedSpan<int> face_edges(mesh.faces(), mesh.corner_edges());
58
59 Array<int> offsets;
60 Array<int> indices;
62 face_edges.offsets, face_edges.data, mesh.edges_num, offsets, indices);
63
64 Array<int> face_count(face_edges.size());
65 threading::parallel_for(face_edges.index_range(), 2048, [&](const IndexRange range) {
66 for (const int64_t face_i : range) {
67 face_count[face_i] = unique_num(edge_to_faces_map, face_edges[face_i]) - 1;
68 }
69 });
70 return mesh.attributes().adapt_domain<int>(
71 VArray<int>::from_container(std::move(face_count)), AttrDomain::Face, domain);
72}
73
75 public:
77 : bke::MeshFieldInput(CPPType::get<int>(), "Face Neighbor Count Field")
78 {
79 }
80
82 const AttrDomain domain,
83 const IndexMask & /*mask*/) const final
84 {
86 }
87
88 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
89 {
90 static constexpr int8_t id = 0;
91 hash.add(&id);
92 }
93
94 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
95 {
96 return AttrDomain::Face;
97 }
98};
99
101{
102 const OffsetIndices faces = mesh.faces();
103 return mesh.attributes().adapt_domain<int>(
105 [faces](const int i) -> float { return faces[i].size(); }),
106 AttrDomain::Face,
107 domain);
108}
109
111 public:
112 FaceVertexCountFieldInput() : bke::MeshFieldInput(CPPType::get<int>(), "Vertex Count Field") {}
113
115 const AttrDomain domain,
116 const IndexMask & /*mask*/) const final
117 {
118 return construct_vertex_count_varray(mesh, domain);
119 }
120
121 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
122 {
123 static constexpr int8_t id = 0;
124 hash.add(&id);
125 }
126
127 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
128 {
129 return AttrDomain::Face;
130 }
131};
132
134{
137}
138
139static void node_register()
140{
141 static bke::bNodeType ntype;
143 &ntype, "GeometryNodeInputMeshFaceNeighbors"_ustr, GEO_NODE_INPUT_MESH_FACE_NEIGHBORS);
144 ntype.ui_name = "Face Neighbors";
145 ntype.ui_description = "Retrieve topology information relating to each face of a mesh";
146 ntype.enum_name_legacy = "MESH_FACE_NEIGHBORS";
147 ntype.nclass = NODE_CLASS_INPUT;
148 ntype.declare = node_declare;
151}
152NOD_REGISTER_NODE(node_register)
153
154} // namespace blender::nodes::node_geo_input_mesh_face_neighbors_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_INPUT_MESH_FACE_NEIGHBORS
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
btScalar max
int64_t size() const
Definition BLI_set.hh:606
void add_multiple(Span< Key > keys)
Definition BLI_set.hh:287
static VArray from_func(const int64_t size, GetFunc get_func)
static VArray from_container(ContainerT container)
int64_t size() const
void extend_non_duplicates(Span< T > array)
uint64_t hash() const
Definition field.cc:185
static Field from_input(Args &&...args)
void set_output(const UString identifier, T &&value)
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
GroupedSpan< int > build_edge_to_face_map(OffsetIndices< int > faces, Span< int > corner_edges, int edges_num, Array< int > &r_offsets, Array< int > &r_indices)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static VArray< int > construct_vertex_count_varray(const Mesh &mesh, const AttrDomain domain)
static bool large_enough_total_size(const GroupedSpan< int > values, const Span< int > indices, const int max)
static VArray< int > construct_neighbor_count_varray(const Mesh &mesh, const AttrDomain domain)
static int unique_num(const GroupedSpan< int > values, const Span< int > indices)
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
ATTR_WARN_UNUSED_RESULT const size_t num
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