Blender V5.3
node_geo_offset_point_in_curve.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_curves.hh"
6
8
10
12{
13 b.add_input<decl::Int>("Point Index"_ustr)
14 .default_input_type(NODE_DEFAULT_INPUT_INDEX_FIELD)
15 .description("The index of the control point to evaluate. Defaults to the current index")
16 .structure_type(StructureType::Field);
17 b.add_input<decl::Int>("Offset"_ustr)
18 .structure_type(StructureType::Field)
19 .description("The number of control points along the curve to traverse");
20 b.add_output<decl::Bool>("Is Valid Offset"_ustr)
21 .structure_type(StructureType::Field)
22 .propagate_references()
24 "Whether the input control point plus the offset is a valid index of the "
25 "original curve");
26 b.add_output<decl::Int>("Point Index"_ustr)
27 .structure_type(StructureType::Field)
28 .propagate_references()
30 "The index of the control point plus the offset within the entire "
31 "curves data-block");
32}
33
35 private:
36 const Field<int> index_;
37 const Field<int> offset_;
38
39 public:
41 : GeometryFieldInput(CPPType::get<int>(), "Offset Point in Curve"),
42 index_(std::move(index)),
43 offset_(std::move(offset))
44 {
45 }
46
48 const IndexMask &mask) const final
49 {
50 const bke::CurvesGeometry *curves_ptr = context.curves_or_strokes();
51 if (!curves_ptr) {
52 return {};
53 }
54 const bke::CurvesGeometry &curves = *curves_ptr;
55
56 const OffsetIndices points_by_curve = curves.points_by_curve();
57 const VArray<bool> cyclic = curves.cyclic();
58 const Array<int> parent_curves = curves.point_to_curve_map();
59
60 fn::FieldEvaluator evaluator{context, &mask};
61 evaluator.add(index_);
62 evaluator.add(offset_);
63 evaluator.evaluate();
64 const VArray<int> indices = evaluator.get_evaluated<int>(0);
65 const VArray<int> offsets = evaluator.get_evaluated<int>(1);
66
67 Array<int> output(mask.min_array_size());
68 mask.foreach_index(
69 [&](const int i_selection) {
70 const int point = std::clamp(indices[i_selection], 0, curves.points_num() - 1);
71 const int curve = parent_curves[point];
72 const IndexRange curve_points = points_by_curve[curve];
73 const int shifted_point = point + offsets[i_selection];
74
75 if (cyclic[curve]) {
76 const int point_index_in_curve = shifted_point - curve_points.start();
77 output[i_selection] = curve_points.start() +
78 math::mod_periodic<int>(point_index_in_curve,
79 curve_points.size());
80 return;
81 }
82 output[i_selection] = std::clamp(shifted_point, 0, curves.points_num() - 1);
83 },
85
86 return VArray<int>::from_container(std::move(output));
87 }
88
89 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
90 {
91 fn(index_);
92 fn(offset_);
93 }
94};
95
97 private:
98 const Field<int> index_;
99 const Field<int> offset_;
100
101 public:
103 : GeometryFieldInput(CPPType::get<bool>(), "Offset Valid"),
104 index_(std::move(index)),
105 offset_(std::move(offset))
106 {
107 }
108
110 const IndexMask &mask) const final
111 {
112 const bke::CurvesGeometry *curves_ptr = context.curves_or_strokes();
113 if (!curves_ptr) {
114 return {};
115 }
116 const bke::CurvesGeometry &curves = *curves_ptr;
117
118 const VArray<bool> cyclic = curves.cyclic();
119 const OffsetIndices points_by_curve = curves.points_by_curve();
120 const Array<int> parent_curves = curves.point_to_curve_map();
121
122 fn::FieldEvaluator evaluator{context, &mask};
123 evaluator.add(index_);
124 evaluator.add(offset_);
125 evaluator.evaluate();
126 const VArray<int> indices = evaluator.get_evaluated<int>(0);
127 const VArray<int> offsets = evaluator.get_evaluated<int>(1);
128
129 Array<bool> output(mask.min_array_size());
130 mask.foreach_index(
131 [&](const int i_selection) {
132 const int i_point = indices[i_selection];
133 if (!curves.points_range().contains(i_point)) {
134 output[i_selection] = false;
135 return;
136 }
137
138 const int i_curve = parent_curves[i_point];
139 const IndexRange curve_points = points_by_curve[i_curve];
140 if (cyclic[i_curve]) {
141 output[i_selection] = true;
142 return;
143 }
144 output[i_selection] = curve_points.contains(i_point + offsets[i_selection]);
145 },
147 return VArray<bool>::from_container(std::move(output));
148 }
149
150 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
151 {
152 fn(index_);
153 fn(offset_);
154 }
155};
156
158{
159 Field<int> index = params.extract_input<Field<int>>("Point Index"_ustr);
160 Field<int> offset = params.extract_input<Field<int>>("Offset"_ustr);
161
162 if (params.output_is_required("Point Index"_ustr)) {
163 params.set_output("Point Index"_ustr,
165 }
166 if (params.output_is_required("Is Valid Offset"_ustr)) {
167 params.set_output("Is Valid Offset"_ustr,
169 }
170}
171
172static void node_register()
173{
174 static bke::bNodeType ntype;
176 &ntype, "GeometryNodeOffsetPointInCurve"_ustr, GEO_NODE_OFFSET_POINT_IN_CURVE);
177 ntype.ui_name = "Offset Point in Curve";
178 ntype.ui_description = "Offset a control point index within its curve";
179 ntype.enum_name_legacy = "OFFSET_POINT_IN_CURVE";
180 ntype.nclass = NODE_CLASS_INPUT;
182 ntype.declare = node_declare;
185}
187
188} // namespace blender::nodes::node_geo_offset_point_in_curve_cc
Low-level operations for curves.
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_OFFSET_POINT_IN_CURVE
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
constexpr int64_t size() const
constexpr int64_t start() const
constexpr bool contains(int64_t value) const
static VArray from_container(ContainerT container)
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
static Field from_input(Args &&...args)
T extract_input(const UString identifier)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
GVArray get_varray_for_context(const bke::GeometryFieldContext &context, const IndexMask &mask) const final
void foreach_recursive_field(FunctionRef< void(const GField &)> fn) const override
GVArray get_varray_for_context(const bke::GeometryFieldContext &context, const IndexMask &mask) const final
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
constexpr ParallelGrainSize grain_size(int grain_size)
T mod_periodic(const T &a, const T &b)
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
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