Blender V5.0
node_fn_invert_matrix.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"
6
8
10
12{
13 b.use_custom_socket_order();
14 b.allow_any_socket_order();
15 b.is_function_node();
16 b.add_input<decl::Matrix>("Matrix");
17 b.add_output<decl::Matrix>("Matrix")
18 .description("The inverted matrix or the identity matrix if the input is not invertible")
19 .align_with_previous();
20 b.add_output<decl::Bool>("Invertible").description("True if the input matrix is invertible");
21}
22
23class InvertMatrixFunction : public mf::MultiFunction {
24 public:
26 {
27 static mf::Signature signature = []() {
28 mf::Signature signature;
29 mf::SignatureBuilder builder{"Invert Matrix", signature};
30 builder.single_input<float4x4>("Matrix");
31 builder.single_output<float4x4>("Matrix", mf::ParamFlag::SupportsUnusedOutput);
32 builder.single_output<bool>("Invertible", mf::ParamFlag::SupportsUnusedOutput);
33 return signature;
34 }();
35 this->set_signature(&signature);
36 }
37
38 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
39 {
40 const VArraySpan<float4x4> in_matrices = params.readonly_single_input<float4x4>(0, "Matrix");
41 MutableSpan<float4x4> out_matrices = params.uninitialized_single_output_if_required<float4x4>(
42 1, "Matrix");
43 MutableSpan<bool> out_invertible = params.uninitialized_single_output_if_required<bool>(
44 2, "Invertible");
45 mask.foreach_index([&](const int64_t i) {
46 const float4x4 &matrix = in_matrices[i];
47 bool success;
48 float4x4 inverted_matrix = math::invert(matrix, success);
49 if (!out_matrices.is_empty()) {
50 out_matrices[i] = success ? inverted_matrix : float4x4::identity();
51 }
52 if (!out_invertible.is_empty()) {
53 out_invertible[i] = success;
54 }
55 });
56 }
57};
58
64
65static void node_register()
66{
67 static blender::bke::bNodeType ntype;
68 fn_node_type_base(&ntype, "FunctionNodeInvertMatrix", FN_NODE_INVERT_MATRIX);
69 ntype.ui_name = "Invert Matrix";
70 ntype.ui_description = "Compute the inverse of the given matrix, if one exists";
71 ntype.enum_name_legacy = "INVERT_MATRIX";
73 ntype.declare = node_declare;
76}
78
79} // namespace blender::nodes::node_fn_invert_matrix_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define FN_NODE_INVERT_MATRIX
#define NOD_REGISTER_NODE(REGISTER_FUNC)
long long int int64_t
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
CartesianBasis invert(const CartesianBasis &basis)
static void node_declare(NodeDeclarationBuilder &b)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
MatBase< float, 4, 4 > float4x4
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
std::string ui_description
Definition BKE_node.hh:244
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