Blender V5.3
node_fn_axes_to_rotation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_matrix.hh"
6
8#include "UI_resources.hh"
9
11#include "NOD_rna_define.hh"
12
13#include "node_function_util.hh"
14#include "node_shader_util.hh"
15
17
19{
20 b.is_function_node();
21 b.add_input<decl::Vector>("Primary Axis"_ustr).default_value(float3(0, 0, 1));
22 b.add_input<decl::Vector>("Secondary Axis"_ustr).default_value(float3(1, 0, 0));
23 b.add_output<decl::Rotation>("Rotation"_ustr);
24}
25
26static void node_init(bNodeTree * /*tree*/, bNode *node)
27{
28 node->custom1 = int(math::Axis::Z);
29 node->custom2 = int(math::Axis::X);
30}
31
32static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
33{
34 layout.prop(ptr, "primary_axis", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
35 layout.prop(ptr, "secondary_axis", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
36}
37
39{
41 if (v.x != -v.y) {
42 return float3{-v.y, v.x, 0.0f};
43 }
44 if (v.x != -v.z) {
45 return float3(-v.z, 0.0f, v.x);
46 }
47 return {0.0f, -v.z, v.y};
48}
49
50class AxesToRotationFunction : public mf::MultiFunction {
51 private:
52 math::Axis primary_axis_;
53 math::Axis secondary_axis_;
54 math::Axis tertiary_axis_;
55
56 public:
57 AxesToRotationFunction(const math::Axis primary_axis, const math::Axis secondary_axis)
58 : primary_axis_(primary_axis), secondary_axis_(secondary_axis)
59 {
60 BLI_assert(primary_axis_ != secondary_axis_);
61
62 /* Through cancellation this will set the last axis to be the one that's neither the primary
63 * nor secondary axis. */
64 tertiary_axis_ = math::Axis::from_int((0 + 1 + 2) - primary_axis.as_int() -
65 secondary_axis.as_int());
66
67 static const mf::Signature signature = []() {
68 mf::Signature signature;
69 mf::SignatureBuilder builder{"Axes to Rotation", signature};
70 builder.single_input<float3>("Primary");
71 builder.single_input<float3>("Secondary");
72 builder.single_output<math::Quaternion>("Rotation");
73 return signature;
74 }();
76 }
77
78 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
79 {
80 const VArray<float3> primaries = params.readonly_single_input<float3>(0, "Primary");
81 const VArray<float3> secondaries = params.readonly_single_input<float3>(1, "Secondary");
82 MutableSpan r_rotations = params.uninitialized_single_output<math::Quaternion>(2, "Rotation");
83
84 /* Might have to invert the axis to make sure that the created matrix has determinant 1. */
85 const bool invert_tertiary = (secondary_axis_.as_int() + 1) % 3 == primary_axis_.as_int();
86 const float tertiary_factor = invert_tertiary ? -1.0f : 1.0f;
87
88 mask.foreach_index([&](const int64_t i) {
89 float3 primary = math::normalize(primaries[i]);
90 float3 secondary = secondaries[i];
91 float3 tertiary;
92
93 const bool primary_is_non_zero = !math::is_zero(primary);
94 const bool secondary_is_non_zero = !math::is_zero(secondary);
95 if (primary_is_non_zero && secondary_is_non_zero) {
96 tertiary = math::cross(primary, secondary);
97 if (math::is_zero(tertiary)) {
98 tertiary = get_orthogonal_of_non_zero_vector(primary);
99 }
100 tertiary = math::normalize(tertiary);
101 secondary = math::cross(tertiary, primary);
102 }
103 else if (primary_is_non_zero) {
104 secondary = get_orthogonal_of_non_zero_vector(primary);
105 secondary = math::normalize(secondary);
106 tertiary = math::cross(primary, secondary);
107 }
108 else if (secondary_is_non_zero) {
109 secondary = math::normalize(secondary);
110 primary = get_orthogonal_of_non_zero_vector(secondary);
111 primary = math::normalize(primary);
112 tertiary = math::cross(primary, secondary);
113 }
114 else {
115 r_rotations[i] = math::Quaternion::identity();
116 return;
117 }
118
119 float3x3 mat;
120 mat[primary_axis_.as_int()] = primary;
121 mat[secondary_axis_.as_int()] = secondary;
122 mat[tertiary_axis_.as_int()] = tertiary_factor * tertiary;
124 BLI_assert(std::abs(math::determinant(mat) - 1.0f) < 0.0001f);
125
126 r_rotations[i] = math::to_quaternion(mat);
127 });
128 };
129
130 void hash_unique(UniqueHashBytes &hash) const override
131 {
132 static constexpr int8_t id = 0;
133 hash.add(&id);
134 hash.add(primary_axis_);
135 hash.add(secondary_axis_);
136 hash.add(tertiary_axis_);
137 }
138};
139
141{
142 const bNode &node = builder.node();
143 if (node.custom1 == node.custom2) {
144 static auto fallback_fn = mf::build::SI2_SO<float3, float3, math::Quaternion>(
145 "Axes to Rotation fallback",
146 [](const float3 & /*a*/, const float3 & /*b*/) { return math::Quaternion::identity(); });
147 builder.set_matching_fn(fallback_fn);
148 return;
149 }
152}
153
155 bNode *node,
156 bNodeExecData * /*execdata*/,
159{
160 if (node->custom1 == node->custom2) {
161 return GPU_stack_link(mat, node, "axes_to_rotation_identity", in, out);
162 }
163
164 const float primary = float(node->custom1);
165 const float secondary = float(node->custom2);
166 /* Through cancellation this will set the last axis to be the one that's neither the primary
167 * nor secondary axis. */
168 const int tertiary_axis = (0 + 1 + 2) - node->custom1 - node->custom2;
169 const float tertiary = float(tertiary_axis);
170
171 /* Might have to invert the axis to make sure that the created matrix has determinant 1. */
172 const bool invert_tertiary = (node->custom2 + 1) % 3 == node->custom1;
173 const float tertiary_factor = invert_tertiary ? -1.0f : 1.0f;
174
175 return GPU_stack_link(mat,
176 node,
177 "axes_to_rotation",
178 in,
179 out,
180 GPU_constant(&primary),
181 GPU_constant(&secondary),
182 GPU_constant(&tertiary),
183 GPU_constant(&tertiary_factor));
184}
185
187{
188 if (params.node.custom1 == params.node.custom2) {
190 row.text = RPT_("Equal Axes");
191 row.tooltip = TIP_("The primary and secondary axis have to be different");
192 row.icon = ICON_STATUS_ERROR;
193 params.rows.append(std::move(row));
194 }
195}
196
197static void node_rna(StructRNA *srna)
198{
199 static const EnumPropertyItem axis_items[] = {
200 {int(math::Axis::X), "X", ICON_NONE, "X", ""},
201 {int(math::Axis::Y), "Y", ICON_NONE, "Y", ""},
202 {int(math::Axis::Z), "Z", ICON_NONE, "Z", ""},
203 {0, nullptr, 0, nullptr, nullptr},
204 };
205
207 "primary_axis",
208 "Primary Axis",
209 "Axis that is aligned exactly to the provided primary direction",
210 axis_items,
212 int(math::Axis::Z));
214 srna,
215 "secondary_axis",
216 "Secondary Axis",
217 "Axis that is aligned as well as possible given the alignment of the primary axis",
218 axis_items,
220}
221
222static void node_register()
223{
224 static bke::bNodeType ntype;
225 fn_cmp_node_type_base(&ntype, "FunctionNodeAxesToRotation"_ustr, FN_NODE_AXES_TO_ROTATION);
226 ntype.ui_name = "Axes to Rotation";
227 ntype.ui_description =
228 "Create a rotation from a primary and (ideally orthogonal) secondary axis";
229 ntype.enum_name_legacy = "AXES_TO_ROTATION";
231 ntype.declare = node_declare;
232 ntype.initfunc = node_init;
237 node_rna(ntype.rna_ext.srna);
239}
241
242} // namespace blender::nodes::node_fn_axes_to_rotation_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_AXES_TO_ROTATION
#define RPT_(msgid)
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
void set_signature(const Signature *signature)
constexpr int as_int() const
static constexpr Value Z
static constexpr Axis from_int(const int axis_int)
static constexpr Value X
static constexpr Value Y
void set_matching_fn(const mf::MultiFunction *fn)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
AxesToRotationFunction(const math::Axis primary_axis, const math::Axis secondary_axis)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
QuaternionBase< float > Quaternion
QuaternionBase< T > to_quaternion(const AxisAngleBase< T, AngleT > &axis_angle)
bool is_orthonormal(const MatT &mat)
bool is_zero(const T &a)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T determinant(const MatBase< T, Size, Size > &mat)
static float3 get_orthogonal_of_non_zero_vector(const float3 &v)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_init(bNodeTree *, bNode *node)
static void node_extra_info(NodeExtraInfoParams &params)
static int node_gpu_material(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
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)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
__int64 int64_t
Definition BKE_text.h:90
void fn_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
ATTR_WARN_UNUSED_RESULT const BMVert * v
GPUNodeLink * GPU_constant(const float *num)
MatBase< float, 3, 3 > float3x3
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
#define hash
Definition noise_c.cc:156
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
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
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
NodeExtraInfoFunction get_extra_info
Definition BKE_node.hh:412
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
Vector< NodeExtraInfoRow > & rows
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)