Blender V5.3
node_geo_input_curve_handles.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::Bool>("Relative"_ustr)
14 .default_value(false)
15 .structure_type(StructureType::Field)
16 .description(
17 "Output the handle positions relative to the corresponding control point "
18 "instead of in the local space of the geometry");
19 b.add_output<decl::Vector>("Left"_ustr)
20 .structure_type(StructureType::Field)
21 .propagate_references();
22 b.add_output<decl::Vector>("Right"_ustr)
23 .structure_type(StructureType::Field)
24 .propagate_references();
25}
26
28 Field<bool> relative_;
29 bool left_;
30
31 public:
33 : bke::GeometryFieldInput(CPPType::get<float3>(), "Handle"), relative_(relative), left_(left)
34 {
35 }
36
38 const IndexMask &mask) const final
39 {
40 const bke::CurvesGeometry *curves_ptr = context.curves_or_strokes();
41 if (!curves_ptr) {
42 return {};
43 }
44 const bke::CurvesGeometry &curves = *curves_ptr;
45 const bke::AttrDomain domain = context.domain();
46
47 const bke::GeometryFieldContext field_context{context, AttrDomain::Point};
48 fn::FieldEvaluator evaluator(field_context, &mask);
49 evaluator.add(relative_);
50 evaluator.evaluate();
51 const VArray<bool> relative = evaluator.get_evaluated<bool>(0);
52
53 const Span<float3> positions = curves.positions();
54
55 const AttributeAccessor attributes = curves.attributes();
56 StringRef side = left_ ? "handle_left" : "handle_right";
57 VArray<float3> handles = *attributes.lookup_or_default<float3>(
58 side, AttrDomain::Point, {0, 0, 0});
59
60 if (relative.is_single()) {
61 if (relative.get_internal_single()) {
62 Array<float3> output(positions.size());
63 for (const int i : positions.index_range()) {
64 output[i] = handles[i] - positions[i];
65 }
66 return attributes.adapt_domain<float3>(
67 VArray<float3>::from_container(std::move(output)), AttrDomain::Point, domain);
68 }
69 return attributes.adapt_domain<float3>(handles, AttrDomain::Point, domain);
70 }
71
72 Array<float3> output(positions.size());
73 for (const int i : positions.index_range()) {
74 if (relative[i]) {
75 output[i] = handles[i] - positions[i];
76 }
77 else {
78 output[i] = handles[i];
79 }
80 }
81 return attributes.adapt_domain<float3>(
82 VArray<float3>::from_container(std::move(output)), AttrDomain::Point, domain);
83 }
84
85 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const final
86 {
87 fn(relative_);
88 }
89
90 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const final
91 {
92 static constexpr int8_t id = 0;
93 hash.add(&id);
94 hash.add(deep_hash_cache.ensure(relative_));
95 hash.add(left_);
96 }
97
98 std::optional<AttrDomain> preferred_domain(
99 const bke::GeometryComponent & /*component*/) const final
100 {
101 return AttrDomain::Point;
102 }
103};
104
106{
107 Field<bool> relative = params.extract_input<Field<bool>>("Relative"_ustr);
110
111 params.set_output("Left"_ustr, std::move(left_field));
112 params.set_output("Right"_ustr, std::move(right_field));
113}
114
115static void node_register()
116{
117 static bke::bNodeType ntype;
119 &ntype, "GeometryNodeInputCurveHandlePositions"_ustr, GEO_NODE_INPUT_CURVE_HANDLES);
120 ntype.ui_name = "Curve Handle Positions";
121 ntype.ui_description = "Retrieve the position of each Bézier control point's handles";
122 ntype.enum_name_legacy = "INPUT_CURVE_HANDLES";
123 ntype.nclass = NODE_CLASS_INPUT;
126 ntype.declare = node_declare;
128}
130
131} // namespace blender::nodes::node_geo_input_curve_handles_cc
Low-level operations for curves.
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_INPUT_CURVE_HANDLES
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
static VArray from_container(ContainerT container)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GVArray adapt_domain(const GVArray &varray, const AttrDomain from_domain, const AttrDomain to_domain) const
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)
void foreach_recursive_field(FunctionRef< void(const GField &)> fn) const final
std::optional< AttrDomain > preferred_domain(const bke::GeometryComponent &) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const final
GVArray get_varray_for_context(const bke::GeometryFieldContext &context, const IndexMask &mask) const final
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
VecBase< float, 3 > float3
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