Blender V5.3
node_fn_align_euler_to_vector.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
8
10#include "UI_resources.hh"
11
12#include "NOD_rna_define.hh"
13
14#include "node_function_util.hh"
15
17
19{
20 b.is_function_node();
21 b.add_input<decl::Vector>("Rotation"_ustr).subtype(PROP_EULER).hide_value();
22 b.add_input<decl::Float>("Factor"_ustr)
23 .default_value(1.0f)
24 .min(0.0f)
25 .max(1.0f)
27 b.add_input<decl::Vector>("Vector"_ustr).default_value({0.0, 0.0, 1.0});
28 b.add_output<decl::Vector>("Rotation"_ustr).subtype(PROP_EULER);
29}
30
31static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
32{
33 layout.prop(ptr, "axis", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
34 layout.use_property_split_set(true);
35 layout.use_property_decorate_set(false);
36 layout.prop(ptr, "pivot_axis", UI_ITEM_NONE, IFACE_("Pivot"), ICON_NONE);
37}
38
40 const VArray<float3> &input_rotations,
41 const VArray<float3> &vectors,
42 const VArray<float> &factors,
43 const float3 local_main_axis,
44 MutableSpan<float3> output_rotations)
45{
46 mask.foreach_index([&](const int64_t i) {
47 const float3 vector = vectors[i];
48 if (math::is_zero(vector)) {
49 output_rotations[i] = input_rotations[i];
50 return;
51 }
52
53 float old_rotation[3][3];
54 eul_to_mat3(old_rotation, input_rotations[i]);
55 float3 old_axis;
56 mul_v3_m3v3(old_axis, old_rotation, local_main_axis);
57
58 const float3 new_axis = math::normalize(vector);
59 float3 rotation_axis = math::cross_high_precision(old_axis, new_axis);
60 if (math::is_zero(rotation_axis)) {
61 /* The vectors are linearly dependent, so we fall back to another axis. */
62 rotation_axis = math::cross_high_precision(old_axis, float3(1, 0, 0));
63 if (math::is_zero(rotation_axis)) {
64 /* This is now guaranteed to not be zero. */
65 rotation_axis = math::cross_high_precision(old_axis, float3(0, 1, 0));
66 }
67 }
68
69 const float full_angle = angle_normalized_v3v3(old_axis, new_axis);
70 const float angle = factors[i] * full_angle;
71
72 float rotation[3][3];
73 axis_angle_to_mat3(rotation, rotation_axis, angle);
74
75 float new_rotation_matrix[3][3];
76 mul_m3_m3m3(new_rotation_matrix, rotation, old_rotation);
77
78 float3 new_rotation;
79 mat3_to_eul(new_rotation, new_rotation_matrix);
80
81 output_rotations[i] = new_rotation;
82 });
83}
84
86 const VArray<float3> &input_rotations,
87 const VArray<float3> &vectors,
88 const VArray<float> &factors,
89 const float3 local_main_axis,
90 const float3 local_pivot_axis,
91 MutableSpan<float3> output_rotations)
92{
93 mask.foreach_index([&](const int64_t i) {
94 if (local_main_axis == local_pivot_axis) {
95 /* Can't compute any meaningful rotation angle in this case. */
96 output_rotations[i] = input_rotations[i];
97 return;
98 }
99
100 const float3 vector = vectors[i];
101 if (math::is_zero(vector)) {
102 output_rotations[i] = input_rotations[i];
103 return;
104 }
105
106 float old_rotation[3][3];
107 eul_to_mat3(old_rotation, input_rotations[i]);
108 float3 old_axis;
109 mul_v3_m3v3(old_axis, old_rotation, local_main_axis);
110 float3 pivot_axis;
111 mul_v3_m3v3(pivot_axis, old_rotation, local_pivot_axis);
112
113 float full_angle = angle_signed_on_axis_v3v3_v3(vector, old_axis, pivot_axis);
114 if (full_angle > M_PI) {
115 /* Make sure the point is rotated as little as possible. */
116 full_angle -= 2.0f * M_PI;
117 }
118 const float angle = factors[i] * full_angle;
119
120 float rotation[3][3];
121 axis_angle_to_mat3(rotation, pivot_axis, angle);
122
123 float new_rotation_matrix[3][3];
124 mul_m3_m3m3(new_rotation_matrix, rotation, old_rotation);
125
126 float3 new_rotation;
127 mat3_to_eul(new_rotation, new_rotation_matrix);
128
129 output_rotations[i] = new_rotation;
130 });
131}
132
133class MF_AlignEulerToVector : public mf::MultiFunction {
134 private:
135 int main_axis_mode_;
136 int pivot_axis_mode_;
137
138 public:
139 MF_AlignEulerToVector(int main_axis_mode, int pivot_axis_mode)
140 : main_axis_mode_(main_axis_mode), pivot_axis_mode_(pivot_axis_mode)
141 {
142 static const mf::Signature signature = []() {
143 mf::Signature signature;
144 mf::SignatureBuilder builder{"Align Euler to Vector", signature};
145 builder.single_input<float3>("Rotation");
146 builder.single_input<float>("Factor");
147 builder.single_input<float3>("Vector");
148 builder.single_output<float3>("Rotation");
149 return signature;
150 }();
151 this->set_signature(&signature);
152 }
153
154 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
155 {
156 const VArray<float3> &input_rotations = params.readonly_single_input<float3>(0, "Rotation");
157 const VArray<float> &factors = params.readonly_single_input<float>(1, "Factor");
158 const VArray<float3> &vectors = params.readonly_single_input<float3>(2, "Vector");
159
160 auto output_rotations = params.uninitialized_single_output<float3>(3, "Rotation");
161
162 float3 local_main_axis = {0.0f, 0.0f, 0.0f};
163 local_main_axis[main_axis_mode_] = 1;
164
165 if (pivot_axis_mode_ == FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_AUTO) {
167 mask, input_rotations, vectors, factors, local_main_axis, output_rotations);
168 }
169 else {
170 float3 local_pivot_axis = {0.0f, 0.0f, 0.0f};
171 local_pivot_axis[pivot_axis_mode_ - 1] = 1;
173 input_rotations,
174 vectors,
175 factors,
176 local_main_axis,
177 local_pivot_axis,
178 output_rotations);
179 }
180 }
181
183 {
184 ExecutionHints hints;
185 hints.min_grain_size = 512;
186 return hints;
187 }
188};
189
191{
192 const bNode &node = builder.node();
194}
195
196static void node_rna(StructRNA *srna)
197{
198 static const EnumPropertyItem axis_items[] = {
200 "X",
201 ICON_NONE,
202 "X",
203 "Align the X axis with the vector"},
205 "Y",
206 ICON_NONE,
207 "Y",
208 "Align the Y axis with the vector"},
210 "Z",
211 ICON_NONE,
212 "Z",
213 "Align the Z axis with the vector"},
214 {0, nullptr, 0, nullptr, nullptr},
215 };
216
217 static const EnumPropertyItem pivot_axis_items[] = {
219 "AUTO",
220 ICON_NONE,
221 "Auto",
222 "Automatically detect the best rotation axis to rotate towards the vector"},
224 "X",
225 ICON_NONE,
226 "X",
227 "Rotate around the local X axis"},
229 "Y",
230 ICON_NONE,
231 "Y",
232 "Rotate around the local Y axis"},
234 "Z",
235 ICON_NONE,
236 "Z",
237 "Rotate around the local Z axis"},
238 {0, nullptr, 0, nullptr, nullptr},
239 };
240
242 "axis",
243 "Axis",
244 "Axis to align to the vector",
245 axis_items,
247 std::nullopt,
248 nullptr,
249 true);
250
252 "pivot_axis",
253 "Pivot Axis",
254 "Axis to rotate around",
255 pivot_axis_items,
257 std::nullopt,
258 nullptr,
259 true);
260}
261
262static void node_register()
263{
264 static bke::bNodeType ntype;
265
266 fn_node_type_base(&ntype, "FunctionNodeAlignEulerToVector"_ustr, FN_NODE_ALIGN_EULER_TO_VECTOR);
267 ntype.ui_name = "Align Euler to Vector";
268 ntype.ui_description = "Orient an Euler rotation along the given direction";
269 ntype.enum_name_legacy = "ALIGN_EULER_TO_VECTOR";
271 ntype.declare = node_declare;
274 ntype.deprecation_notice = N_("Use the \"Align Rotation to Vector\" node instead");
276
277 node_rna(ntype.rna_ext.srna);
278}
280
281} // namespace blender::nodes::node_fn_align_euler_to_vector_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_ALIGN_EULER_TO_VECTOR
#define M_PI
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
VecBase< float, 3 > cross_high_precision(const VecBase< float, 3 > &a, const VecBase< float, 3 > &b)
bool is_zero(const T &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void align_rotations_fixed_pivot(const IndexMask &mask, const VArray< float3 > &input_rotations, const VArray< float3 > &vectors, const VArray< float > &factors, const float3 local_main_axis, const float3 local_pivot_axis, MutableSpan< float3 > output_rotations)
static void align_rotations_auto_pivot(const IndexMask &mask, const VArray< float3 > &input_rotations, const VArray< float3 > &vectors, const VArray< float > &factors, const float3 local_main_axis, MutableSpan< float3 > output_rotations)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
__int64 int64_t
Definition BKE_text.h:90
void eul_to_mat3(float mat[3][3], const float eul[3])
void axis_angle_to_mat3(float R[3][3], const float axis[3], float angle)
float angle_signed_on_axis_v3v3_v3(const float v1[3], const float v2[3], const float axis[3]) ATTR_WARN_UNUSED_RESULT
void mat3_to_eul(float eul[3], const float mat[3][3])
@ FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_X
@ FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Y
@ FN_NODE_ALIGN_EULER_TO_VECTOR_AXIS_Z
void fn_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
VecBase< float, 3 > float3
@ PROP_FACTOR
Definition RNA_types.hh:308
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
@ FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_Y
@ FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_Z
@ FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_X
@ FN_NODE_ALIGN_EULER_TO_VECTOR_PIVOT_AXIS_AUTO
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
const char * deprecation_notice
Definition BKE_node.hh:455
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
#define N_(msgid)