Blender V4.3
node_fn_separate_transform.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_math_matrix.hh"
7
10
11#include "node_function_util.hh"
12
14
16{
17 b.is_function_node();
18 b.add_input<decl::Matrix>("Transform");
19 b.add_output<decl::Vector>("Translation").subtype(PROP_TRANSLATION);
20 b.add_output<decl::Rotation>("Rotation");
21 b.add_output<decl::Vector>("Scale").subtype(PROP_XYZ);
22};
23
25 public:
27 {
28 static const mf::Signature signature = []() {
30 mf::SignatureBuilder builder{"Separate Transform", signature};
31 builder.single_input<float4x4>("Transform");
32 builder.single_output<float3>("Translation", mf::ParamFlag::SupportsUnusedOutput);
33 builder.single_output<math::Quaternion>("Rotation", mf::ParamFlag::SupportsUnusedOutput);
34 builder.single_output<float3>("Scale", mf::ParamFlag::SupportsUnusedOutput);
35 return signature;
36 }();
37 this->set_signature(&signature);
38 }
39
40 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
41 {
42 const VArraySpan transforms = params.readonly_single_input<float4x4>(0, "Transform");
43 MutableSpan translation = params.uninitialized_single_output_if_required<float3>(
44 1, "Translation");
45 MutableSpan rotation = params.uninitialized_single_output_if_required<math::Quaternion>(
46 2, "Rotation");
47 MutableSpan scale = params.uninitialized_single_output_if_required<float3>(3, "Scale");
48
49 if (!translation.is_empty()) {
50 mask.foreach_index_optimized<int64_t>(
51 [&](const int64_t i) { translation[i] = transforms[i].location(); });
52 }
53
54 if (rotation.is_empty() && !scale.is_empty()) {
55 mask.foreach_index([&](const int64_t i) { scale[i] = math::to_scale(transforms[i]); });
56 }
57 else if (!rotation.is_empty() && scale.is_empty()) {
58 mask.foreach_index([&](const int64_t i) {
60 math::normalize(float3x3(transforms[i])));
61 });
62 }
63 else if (!rotation.is_empty() && !scale.is_empty()) {
64 mask.foreach_index([&](const int64_t i) {
65 const float3x3 normalized_mat = math::normalize_and_get_size(float3x3(transforms[i]),
66 scale[i]);
67 rotation[i] = math::normalized_to_quaternion_safe(normalized_mat);
68 });
69 }
70 }
71};
72
74{
76 builder.set_matching_fn(fn);
77}
78
80{
81 using namespace value_elem;
82 const MatrixElem matrix_elem = params.get_input_elem<MatrixElem>("Transform");
83 params.set_output_elem("Translation", matrix_elem.translation);
84 params.set_output_elem("Rotation", matrix_elem.rotation);
85 params.set_output_elem("Scale", matrix_elem.scale);
86}
87
89{
90 using namespace value_elem;
91 MatrixElem transform_elem;
92 transform_elem.translation = params.get_output_elem<VectorElem>("Translation");
93 transform_elem.rotation = params.get_output_elem<RotationElem>("Rotation");
94 transform_elem.scale = params.get_output_elem<VectorElem>("Scale");
95 params.set_input_elem("Transform", transform_elem);
96}
97
99{
100 const float3 translation = params.get_output<float3>("Translation");
101 const math::Quaternion rotation = params.get_output<math::Quaternion>("Rotation");
102 const float3 scale = params.get_output<float3>("Scale");
103 params.set_input("Transform", math::from_loc_rot_scale<float4x4>(translation, rotation, scale));
104}
105
106static void node_register()
107{
108 static blender::bke::bNodeType ntype;
110 &ntype, FN_NODE_SEPARATE_TRANSFORM, "Separate Transform", NODE_CLASS_CONVERTER);
111 ntype.declare = node_declare;
117}
119
120} // namespace blender::nodes::node_fn_separate_transform_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:410
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_XYZ
Definition RNA_types.hh:172
@ PROP_TRANSLATION
Definition RNA_types.hh:164
void set_signature(const Signature *signature)
void set_matching_fn(const mf::MultiFunction *fn)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
QuaternionBase< T > normalized_to_quaternion_safe(const MatBase< T, 3, 3 > &mat)
MatBase< T, NumCol, NumRow > normalize_and_get_size(const MatBase< T, NumCol, NumRow > &a, VectorT &r_size)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > to_scale(const MatBase< T, NumCol, NumRow > &mat)
MatT from_loc_rot_scale(const typename MatT::loc_type &location, const RotationT &rotation, const VecBase< typename MatT::base_type, ScaleDim > &scale)
static void node_eval_elem(value_elem::ElemEvalParams &params)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_eval_inverse(inverse_eval::InverseEvalParams &params)
static void node_eval_inverse_elem(value_elem::InverseElemEvalParams &params)
static void node_declare(NodeDeclarationBuilder &b)
MatBase< float, 3, 3 > float3x3
void fn_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
__int64 int64_t
Definition stdint.h:89
Defines a node type.
Definition BKE_node.hh:218
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:378
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:385
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:372
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:336
NodeDeclareFunction declare
Definition BKE_node.hh:347