Blender V5.0
node_fn_combine_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::Vector>("Translation").subtype(PROP_TRANSLATION);
19 b.add_input<decl::Rotation>("Rotation");
20 b.add_input<decl::Vector>("Scale").default_value(float3(1)).subtype(PROP_XYZ);
21 b.add_output<decl::Matrix>("Transform");
22}
23
24class CombineTransformFunction : public mf::MultiFunction {
25 public:
27 {
28 static const mf::Signature signature = []() {
29 mf::Signature signature;
30 mf::SignatureBuilder builder{"Combine Transform", signature};
31 builder.single_input<float3>("Translation");
32 builder.single_input<math::Quaternion>("Rotation");
33 builder.single_input<float3>("Scale");
34 builder.single_output<float4x4>("Transform");
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 VArray translation = params.readonly_single_input<float3>(0, "Translation");
43 const VArray rotation = params.readonly_single_input<math::Quaternion>(1, "Rotation");
44 const VArray scale = params.readonly_single_input<float3>(2, "Scale");
45 MutableSpan transforms = params.uninitialized_single_output<float4x4>(3, "Transform");
46
47 const std::optional<float3> translation_single = translation.get_if_single();
48 const std::optional<math::Quaternion> rotation_single = rotation.get_if_single();
49 const std::optional<float3> scale_single = scale.get_if_single();
50
51 const bool no_translation = translation_single && math::is_zero(*translation_single);
52 const bool no_rotation = rotation_single && math::angle_of(*rotation_single).radian() < 1e-7f;
53 const bool no_scale = scale_single && math::is_equal(*scale_single, float3(1), 1e-7f);
54
55 if (no_rotation && no_scale) {
56 mask.foreach_index(
57 [&](const int64_t i) { transforms[i] = math::from_location<float4x4>(translation[i]); });
58 }
59 else if (no_translation && no_scale) {
60 mask.foreach_index(
61 [&](const int64_t i) { transforms[i] = math::from_rotation<float4x4>(rotation[i]); });
62 }
63 else if (no_translation && no_rotation) {
64 mask.foreach_index(
65 [&](const int64_t i) { transforms[i] = math::from_scale<float4x4>(scale[i]); });
66 }
67 else {
68 mask.foreach_index([&](const int64_t i) {
69 transforms[i] = math::from_loc_rot_scale<float4x4>(translation[i], rotation[i], scale[i]);
70 });
71 }
72 }
73};
74
80
82{
83 using namespace value_elem;
84 MatrixElem matrix_elem;
85 matrix_elem.translation = params.get_input_elem<VectorElem>("Translation");
86 matrix_elem.rotation = params.get_input_elem<RotationElem>("Rotation");
87 matrix_elem.scale = params.get_input_elem<VectorElem>("Scale");
88 params.set_output_elem("Transform", matrix_elem);
89}
90
92{
93 using namespace value_elem;
94 const MatrixElem matrix_elem = params.get_output_elem<MatrixElem>("Transform");
95 params.set_input_elem("Translation", matrix_elem.translation);
96 params.set_input_elem("Rotation", matrix_elem.rotation);
97 params.set_input_elem("Scale", matrix_elem.scale);
98}
99
101{
102 const float4x4 transform = params.get_output<float4x4>("Transform");
103 float3 translation;
104 math::Quaternion rotation;
105 float3 scale;
106 math::to_loc_rot_scale_safe<true>(transform, translation, rotation, scale);
107 params.set_input("Translation", translation);
108 params.set_input("Rotation", rotation);
109 params.set_input("Scale", scale);
110}
111
112static void node_register()
113{
114 static blender::bke::bNodeType ntype;
115 fn_node_type_base(&ntype, "FunctionNodeCombineTransform", FN_NODE_COMBINE_TRANSFORM);
116 ntype.ui_name = "Combine Transform";
117 ntype.ui_description =
118 "Combine a translation vector, a rotation, and a scale vector into a transformation matrix";
119 ntype.enum_name_legacy = "COMBINE_TRANSFORM";
121 ntype.declare = node_declare;
127}
129
130} // namespace blender::nodes::node_fn_combine_transform_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define FN_NODE_COMBINE_TRANSFORM
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_XYZ
Definition RNA_types.hh:269
@ PROP_TRANSLATION
Definition RNA_types.hh:261
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
long long int int64_t
std::optional< T > get_if_single() const
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
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
QuaternionBase< float > Quaternion
bool is_zero(const T &a)
AngleRadianBase< T > angle_of(const QuaternionBase< T > &q)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
bool is_equal(const MatBase< T, NumCol, NumRow > &a, const MatBase< T, NumCol, NumRow > &b, const T epsilon=T(0))
void to_loc_rot_scale_safe(const MatBase< T, 4, 4 > &mat, VecBase< T, 3 > &r_location, RotationT &r_rotation, VecBase< T, 3 > &r_scale)
MatT from_location(const typename MatT::loc_type &location)
MatT from_rotation(const RotationT &rotation)
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_eval_inverse(inverse_eval::InverseEvalParams &params)
static void node_eval_inverse_elem(value_elem::InverseElemEvalParams &params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
MatBase< float, 4, 4 > float4x4
VecBase< float, 3 > float3
void fn_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:238
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:403
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:410
std::string ui_description
Definition BKE_node.hh:244
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:397
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
i
Definition text_draw.cc:230