Blender V5.3
gpu_shader_math_matrix_adjoint.bsl.hh
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#pragma once
6
8
9/* -------------------------------------------------------------------- */
12
17{
18 float2x2 adj = float2x2(0.0f);
19 for (int c = 0; c < 2; c++) {
20 for (int r = 0; r < 2; r++) {
21 /* Copy other cells except the "cross" to compute the determinant. */
22 float tmp = 0.0f;
23 for (int m_c = 0; m_c < 2; m_c++) {
24 for (int m_r = 0; m_r < 2; m_r++) {
25 if (m_c != c && m_r != r) {
26 tmp = mat[m_c][m_r];
27 }
28 }
29 }
30 float minor = tmp;
31 /* Transpose directly to get the adjugate. Swap destination row and col. */
32 adj[r][c] = (((c + r) & 1) != 0) ? -minor : minor;
33 }
34 }
35 return adj;
36}
37
42{
43 float3x3 adj = float3x3(0.0f);
44 for (int c = 0; c < 3; c++) {
45 for (int r = 0; r < 3; r++) {
46 /* Copy other cells except the "cross" to compute the determinant. */
47 float2x2 tmp = float2x2(0.0f);
48 for (int m_c = 0; m_c < 3; m_c++) {
49 for (int m_r = 0; m_r < 3; m_r++) {
50 if (m_c != c && m_r != r) {
51 int d_c = (m_c < c) ? m_c : (m_c - 1);
52 int d_r = (m_r < r) ? m_r : (m_r - 1);
53 tmp[d_c][d_r] = mat[m_c][m_r];
54 }
55 }
56 }
57 float minor = determinant(tmp);
58 /* Transpose directly to get the adjugate. Swap destination row and col. */
59 adj[r][c] = (((c + r) & 1) != 0) ? -minor : minor;
60 }
61 }
62 return adj;
63}
64
69{
70 float4x4 adj = float4x4(0.0f);
71 for (int c = 0; c < 4; c++) {
72 for (int r = 0; r < 4; r++) {
73 /* Copy other cells except the "cross" to compute the determinant. */
74 float3x3 tmp = float3x3(0.0f);
75 for (int m_c = 0; m_c < 4; m_c++) {
76 for (int m_r = 0; m_r < 4; m_r++) {
77 if (m_c != c && m_r != r) {
78 int d_c = (m_c < c) ? m_c : (m_c - 1);
79 int d_r = (m_r < r) ? m_r : (m_r - 1);
80 tmp[d_c][d_r] = mat[m_c][m_r];
81 }
82 }
83 }
84 float minor = determinant(tmp);
85 /* Transpose directly to get the adjugate. Swap destination row and col. */
86 adj[r][c] = (((c + r) & 1) != 0) ? -minor : minor;
87 }
88 }
89 return adj;
90}
91
b[1] b &&[1] b &&[1] b &&[1] b &&[1] b[3] b &&[1] b[3] float determinant(MatBase< C, R >) RET
float2x2 adjoint(float2x2 mat)
MatBase< float, 2, 2 > float2x2
MatBase< float, 4, 4 > float4x4