Blender V5.3
node_geo_edge_paths_to_selection.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 "BLI_task.hh"
8
10
12
14{
15 b.add_input<decl::Bool>("Start Vertices"_ustr)
16 .default_value(true)
17 .hide_value()
18 .structure_type(StructureType::Field);
19 b.add_input<decl::Int>("Next Vertex Index"_ustr)
20 .default_value(-1)
21 .hide_value()
22 .structure_type(StructureType::Field);
23 b.add_output<decl::Bool>("Selection"_ustr)
24 .structure_type(StructureType::Field)
25 .propagate_references();
26}
27
28static void edge_paths_to_selection(const Mesh &src_mesh,
29 const IndexMask &start_selection,
30 const Span<int> next_indices,
31 MutableSpan<bool> r_edge_selection)
32{
33 Array<bool> vert_selection(src_mesh.verts_num, false);
34
35 const IndexRange vert_range(src_mesh.verts_num);
36 start_selection.foreach_index(
37 [&](const int start_vert) {
38 /* If vertex is selected, all next is already selected too. */
39 for (int current_vert = start_vert; !vert_selection[current_vert];
40 current_vert = next_indices[current_vert])
41 {
42 if (!vert_range.contains(current_vert)) [[unlikely]] {
43 break;
44 }
45 vert_selection[current_vert] = true;
46 }
47 },
49
50 const Span<int2> edges = src_mesh.edges();
51 threading::parallel_for(edges.index_range(), 4096, [&](const IndexRange range) {
52 for (const int i : range) {
53 const int2 edge = edges[i];
54 if (!(vert_selection[edge[0]] && vert_selection[edge[1]])) {
55 continue;
56 }
57 if (edge[0] == next_indices[edge[1]] || edge[1] == next_indices[edge[0]]) {
58 r_edge_selection[i] = true;
59 }
60 }
61 });
62}
63
65 private:
66 Field<bool> start_vertices_;
67 Field<int> next_vertex_;
68
69 public:
71 : bke::MeshFieldInput(CPPType::get<bool>(), "Edge Selection"),
72 start_vertices_(start_verts),
73 next_vertex_(next_vertex)
74 {
75 }
76
78 const AttrDomain domain,
79 const IndexMask & /*mask*/) const final
80 {
81 const bke::MeshFieldContext context{mesh, AttrDomain::Point};
82 fn::FieldEvaluator evaluator{context, mesh.verts_num};
83 evaluator.add(next_vertex_);
84 evaluator.add(start_vertices_);
85 evaluator.evaluate();
86 const VArraySpan<int> next_vert = evaluator.get_evaluated<int>(0);
87 const IndexMask start_verts = evaluator.get_evaluated_as_mask(1);
88 if (start_verts.is_empty()) {
89 return {};
90 }
91
92 Array<bool> selection(mesh.edges_num, false);
93 edge_paths_to_selection(mesh, start_verts, next_vert, selection);
94
95 return mesh.attributes().adapt_domain<bool>(
96 VArray<bool>::from_container(std::move(selection)), AttrDomain::Edge, domain);
97 }
98
99 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
100 {
101 fn(start_vertices_);
102 fn(next_vertex_);
103 }
104
105 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
106 {
107 static constexpr int8_t id = 0;
108 hash.add(&id);
109 hash.add(deep_hash_cache.ensure(start_vertices_));
110 hash.add(deep_hash_cache.ensure(next_vertex_));
111 }
112
113 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const override
114 {
115 return AttrDomain::Edge;
116 }
117};
118
120{
121 Field<bool> start_verts = params.extract_input<Field<bool>>("Start Vertices"_ustr);
122 Field<int> next_vertex = params.extract_input<Field<int>>("Next Vertex Index"_ustr);
123 params.set_output(
124 "Selection"_ustr,
126}
127
128static void node_register()
129{
130 static bke::bNodeType ntype;
131
133 &ntype, "GeometryNodeEdgePathsToSelection"_ustr, GEO_NODE_EDGE_PATHS_TO_SELECTION);
134 ntype.ui_name = "Edge Paths to Selection";
135 ntype.ui_description = "Output a selection of edges by following paths across mesh edges";
136 ntype.enum_name_legacy = "EDGE_PATHS_TO_SELECTION";
137 ntype.nclass = NODE_CLASS_INPUT;
138 ntype.declare = node_declare;
142}
143NOD_REGISTER_NODE(node_register)
144
145} // namespace blender::nodes::node_geo_edge_paths_to_selection_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_EDGE_PATHS_TO_SELECTION
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr bool contains(int64_t value) const
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
static VArray from_container(ContainerT container)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
const GVArray & get_evaluated(const int field_index) const
uint64_t hash() const
Definition field.cc:185
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
static Field from_input(Args &&...args)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
constexpr ParallelGrainSize grain_size(int grain_size)
static void edge_paths_to_selection(const Mesh &src_mesh, const IndexMask &start_selection, const Span< int > next_indices, MutableSpan< bool > r_edge_selection)
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
static constexpr int _160
Definition BKE_node.hh:246
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
UniqueHash ensure(const GFieldRef &field)
Definition field.cc:113