Blender V5.3
node_geo_curve_endpoint_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 "BLI_task.hh"
6
7#include "BKE_curves.hh"
8
10
12
14{
15 b.add_input<decl::Int>("Start Size"_ustr)
16 .min(0)
17 .default_value(1)
18 .structure_type(StructureType::Field)
19 .description("The amount of points to select from the start of each spline");
20 b.add_input<decl::Int>("End Size"_ustr)
21 .min(0)
22 .default_value(1)
23 .structure_type(StructureType::Field)
24 .description("The amount of points to select from the end of each spline");
25 b.add_output<decl::Bool>("Selection"_ustr)
26 .structure_type(StructureType::Field)
27 .propagate_references()
28 .description("The selection from the start and end of the splines based on the input sizes");
29}
30
32 Field<int> start_size_;
33 Field<int> end_size_;
34
35 public:
37 : bke::GeometryFieldInput(CPPType::get<bool>(), "Endpoint Selection node"),
38 start_size_(start_size),
39 end_size_(end_size)
40 {
41 }
42
44 const IndexMask & /*mask*/) const final
45 {
46 if (context.domain() != AttrDomain::Point) {
47 return {};
48 }
49 const bke::CurvesGeometry *curves_ptr = context.curves_or_strokes();
50 if (!curves_ptr) {
51 return {};
52 }
53 const bke::CurvesGeometry &curves = *curves_ptr;
54 if (curves.is_empty()) {
55 return {};
56 }
57
58 const bke::GeometryFieldContext sub_context{context, AttrDomain::Curve};
59 fn::FieldEvaluator evaluator{sub_context, curves.curves_num()};
60 evaluator.add(start_size_);
61 evaluator.add(end_size_);
62 evaluator.evaluate();
63 const VArray<int> start_size = evaluator.get_evaluated<int>(0);
64 const VArray<int> end_size = evaluator.get_evaluated<int>(1);
65
66 Array<bool> selection(curves.points_num(), false);
67 MutableSpan<bool> selection_span = selection.as_mutable_span();
68 const OffsetIndices points_by_curve = curves.points_by_curve();
69 devirtualize_varray2(start_size, end_size, [&](const auto start_size, const auto end_size) {
70 threading::parallel_for(curves.curves_range(), 1024, [&](IndexRange curves_range) {
71 for (const int i : curves_range) {
72 const IndexRange points = points_by_curve[i];
73 const int start = std::max(start_size[i], 0);
74 const int end = std::max(end_size[i], 0);
75
76 selection_span.slice(points.take_front(start)).fill(true);
77 selection_span.slice(points.take_back(end)).fill(true);
78 }
79 });
80 });
81
82 return VArray<bool>::from_container(std::move(selection));
83 };
84
85 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const final
86 {
87 fn(start_size_);
88 fn(end_size_);
89 }
90
91 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const final
92 {
93 static constexpr int8_t id = 0;
94 hash.add(&id);
95 hash.add(deep_hash_cache.ensure(start_size_));
96 hash.add(deep_hash_cache.ensure(end_size_));
97 }
98
99 std::optional<AttrDomain> preferred_domain(const GeometryComponent & /*component*/) const final
100 {
101 return AttrDomain::Point;
102 }
103};
104
106{
107 Field<int> start_size = params.extract_input<Field<int>>("Start Size"_ustr);
108 Field<int> end_size = params.extract_input<Field<int>>("End Size"_ustr);
109 Field<bool> selection_field = Field<bool>::from_input<EndpointFieldInput>(start_size, end_size);
110 params.set_output("Selection"_ustr, std::move(selection_field));
111}
112
113static void node_register()
114{
115 static bke::bNodeType ntype;
117 &ntype, "GeometryNodeCurveEndpointSelection"_ustr, GEO_NODE_CURVE_ENDPOINT_SELECTION);
118 ntype.ui_name = "Endpoint Selection";
119 ntype.ui_description = "Provide a selection for an arbitrary number of endpoints in each spline";
120 ntype.enum_name_legacy = "CURVE_ENDPOINT_SELECTION";
121 ntype.nclass = NODE_CLASS_INPUT;
122 ntype.declare = node_declare;
124
126}
127NOD_REGISTER_NODE(node_register)
128
129} // namespace blender::nodes::node_geo_curve_endpoint_selection_cc
Low-level operations for curves.
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_CURVE_ENDPOINT_SELECTION
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
static VArray from_container(ContainerT container)
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
uint64_t hash() const
Definition field.cc:185
static Field from_input(Args &&...args)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
std::optional< AttrDomain > preferred_domain(const GeometryComponent &) const final
GVArray get_varray_for_context(const bke::GeometryFieldContext &context, const IndexMask &) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const final
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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 devirtualize_varray2(const VArray< T1 > &varray1, const VArray< T2 > &varray2, const Func &func, bool enable=true)
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
#define min(a, b)
Definition sort.cc:38
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