Blender V5.3
node_fn_matrix_svd.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_solvers.hh"
6
8#include "node_shader_util.hh"
9
11
13{
14 b.is_function_node();
15 b.add_input<decl::Matrix>("Matrix"_ustr)
16 .description("Matrix to decompose, only the 3x3 part is used");
17 b.add_output<decl::Matrix>("U"_ustr).description("Left singular vectors");
18 b.add_output<decl::Vector>("S"_ustr).description("Singular values");
19 b.add_output<decl::Matrix>("V"_ustr).description("Right singular vectors");
20}
21
22class MatrixSVDFunction : public mf::MultiFunction {
23 public:
25 {
26 static mf::Signature signature_;
27 mf::SignatureBuilder builder{"Matrix SVD", signature_};
28 builder.single_input<float4x4>("Matrix");
29 builder.single_output<float4x4>("U");
30 builder.single_output<float3>("S");
31 builder.single_output<float4x4>("V");
32 this->set_signature(&signature_);
33 }
34
35 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
36 {
37 const VArraySpan<float4x4> matrices = params.readonly_single_input<float4x4>(0, "Matrix");
38 MutableSpan<float4x4> Us = params.uninitialized_single_output<float4x4>(1, "U");
39 MutableSpan<float3> Ss = params.uninitialized_single_output<float3>(2, "S");
40 MutableSpan<float4x4> Vs = params.uninitialized_single_output<float4x4>(3, "V");
41 mask.foreach_index([&](const int64_t i) {
42 const float3x3 matrix = matrices[i].view<3, 3>();
43 float3x3 matrix_U, matrix_V;
44 BLI_svd_m3(matrix.ptr(), matrix_U.ptr(), Ss[i], matrix_V.ptr());
45 Us[i] = float4x4(matrix_U);
46 Vs[i] = float4x4(matrix_V);
47 });
48 }
49};
50
52{
53 static MatrixSVDFunction fn;
54 builder.set_matching_fn(fn);
55}
56
58 bNode *node,
59 bNodeExecData * /*execdata*/,
62{
63 return GPU_stack_link(mat, node, "matrix_svd", in, out);
64}
65
66static void node_register()
67{
68 static bke::bNodeType ntype;
69 fn_cmp_node_type_base(&ntype, "FunctionNodeMatrixSVD"_ustr);
70 ntype.ui_name = "Matrix SVD";
71 ntype.ui_description = "Compute the singular value decomposition of the 3x3 part of a matrix";
73 ntype.declare = node_declare;
77}
79
80} // namespace blender::nodes::node_fn_matrix_svd_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NOD_REGISTER_NODE(REGISTER_FUNC)
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
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static int node_gpu_material(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_declare(NodeDeclarationBuilder &b)
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 BLI_svd_m3(const float m3[3][3], float r_U[3][3], float r_S[3], float r_V[3][3])
Compute the SVD (Singular Values Decomposition) of given 3D matrix (m3 = USV*).
void fn_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
MatBase< float, 4, 4 > float4x4
MatBase< float, 3, 3 > float3x3
VecBase< float, 3 > float3
const c_style_mat & ptr() const
Defines a node type.
Definition BKE_node.hh:265
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
NodeDeclareFunction declare
Definition BKE_node.hh:393