Blender V4.5
BCMath.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BCMath.h"
6#include "BlenderContext.h"
7
8#include "BLI_math_matrix.h"
9
11{
12 Quat qd;
13 Matrix matd;
14 Matrix mati;
15 Matrix mat_from;
16
17 quat_to_mat4(mat_from, q);
18
19 /* Calculate the difference matrix matd between mat_from and mat_to */
20 invert_m4_m4(mati, mat_from);
21 mul_m4_m4m4(matd, mati, mat_to);
22
23 mat4_to_quat(qd, matd);
24
25 mul_qt_qtqt(q, qd, q); /* rotate to the final rotation to mat_to */
26}
27
29{
30 set_transform(mat.matrix);
31}
32
37
42
44{
45 unit();
46}
47
49{
50 float mrot[3][3];
51 float mat[4][4];
53 global_forward_axis, global_up_axis, BC_DEFAULT_FORWARD, BC_DEFAULT_UP, mrot);
54 copy_m4_m3(mat, mrot);
55 set_transform(mat);
56}
57
58void BCMatrix::add_transform(const Matrix &mat, bool inverted)
59{
60 add_transform(this->matrix, mat, this->matrix, inverted);
61}
62
63void BCMatrix::add_transform(const BCMatrix &mat, bool inverted)
64{
65 add_transform(this->matrix, mat.matrix, this->matrix, inverted);
66}
67
68void BCMatrix::apply_transform(const BCMatrix &mat, bool inverted)
69{
70 apply_transform(this->matrix, mat.matrix, this->matrix, inverted);
71}
72
74 const Matrix &transform,
75 const Matrix &from,
76 bool inverted)
77{
78 if (inverted) {
79 Matrix globinv;
80 invert_m4_m4(globinv, transform);
81 add_transform(to, globinv, from, /*inverted=*/false);
82 }
83 else {
84 mul_m4_m4m4(to, transform, from);
85 }
86}
87
89 const Matrix &transform,
90 const Matrix &from,
91 bool inverse)
92{
93 Matrix globinv;
94 invert_m4_m4(globinv, transform);
95 if (inverse) {
96 add_transform(to, globinv, from, /*inverted=*/false);
97 }
98 else {
99 mul_m4_m4m4(to, transform, from);
100 mul_m4_m4m4(to, to, globinv);
101 }
102}
103
105{
106 Matrix workmat;
107 invert_m4_m4(workmat, transform);
108 mul_m4_m4m4(to, workmat, from);
109}
110
112{
113 Matrix lmat;
114
116 copy_m4_m4(matrix, lmat);
117
118 mat4_decompose(this->loc, this->q, this->size, lmat);
119 quat_to_compatible_eul(this->rot, ob->rot, this->q);
120}
121
123{
124 copy_m4_m4(matrix, mat);
125 mat4_decompose(this->loc, this->q, this->size, mat);
126 quat_to_eul(this->rot, this->q);
127}
128
129void BCMatrix::copy(Matrix &r, Matrix &a)
130{
131 /* destination comes first: */
132 memcpy(r, a, sizeof(Matrix));
133}
134
136{
137 transpose_m4(mat);
138}
139
140void BCMatrix::sanitize(Matrix &mat, int precision)
141{
142 for (auto &row : mat) {
143 for (float &cell : row) {
144 double val = double(cell);
145 val = double_round(val, precision);
146 cell = float(val);
147 }
148 }
149}
150
151void BCMatrix::sanitize(DMatrix &mat, int precision)
152{
153 for (auto &row : mat) {
154 for (double &cell : row) {
155 cell = double_round(cell, precision);
156 }
157 }
158}
159
160void BCMatrix::unit()
161{
162 unit_m4(this->matrix);
163 mat4_decompose(this->loc, this->q, this->size, this->matrix);
164 quat_to_eul(this->rot, this->q);
165}
166
167void BCMatrix::get_matrix(DMatrix &mat, const bool transposed, const int precision) const
168{
169 for (int i = 0; i < 4; i++) {
170 for (int j = 0; j < 4; j++) {
171 float val = (transposed) ? matrix[j][i] : matrix[i][j];
172 if (precision >= 0) {
173 val = floor(val * pow(10, precision) + 0.5) / pow(10, precision);
174 }
175 mat[i][j] = val;
176 }
177 }
178}
179
181 const bool transposed,
182 const int precision,
183 const bool inverted) const
184{
185 for (int i = 0; i < 4; i++) {
186 for (int j = 0; j < 4; j++) {
187 float val = (transposed) ? matrix[j][i] : matrix[i][j];
188 if (precision >= 0) {
189 val = floor(val * pow(10, precision) + 0.5) / pow(10, precision);
190 }
191 mat[i][j] = val;
192 }
193 }
194
195 if (inverted) {
196 invert_m4(mat);
197 }
198}
199
200bool BCMatrix::in_range(const BCMatrix &other, float distance) const
201{
202 for (int i = 0; i < 4; i++) {
203 for (int j = 0; j < 4; j++) {
204 if (fabs(other.matrix[i][j] - matrix[i][j]) > distance) {
205 return false;
206 }
207 }
208 }
209 return true;
210}
211
212float (&BCMatrix::location() const)[3]
213{
214 return loc;
215}
216
217float (&BCMatrix::rotation() const)[3]
218{
219 return rot;
220}
221
222float (&BCMatrix::scale() const)[3]
223{
224 return size;
225}
226
227float (&BCMatrix::quat() const)[4]
228{
229 return q;
230}
void BKE_object_matrix_local_get(Object *ob, float r_mat[4][4])
double double_round(double x, int ndigits)
Definition math_base.cc:28
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void mat4_decompose(float loc[3], float quat[4], float size[3], const float wmat[4][4])
void copy_m4_m3(float m1[4][4], const float m2[3][3])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
bool invert_m4(float mat[4][4])
void transpose_m4(float R[4][4])
void unit_m4(float m[4][4])
void quat_to_mat4(float m[4][4], const float q[4])
void quat_to_eul(float eul[3], const float quat[4])
void mul_qt_qtqt(float q[4], const float a[4], const float b[4])
void mat4_to_quat(float q[4], const float mat[4][4])
bool mat3_from_axis_conversion(int src_forward, int src_up, int dst_forward, int dst_up, float r_mat[3][3])
void quat_to_compatible_eul(float eul[3], const float oldrot[3], const float quat[4])
static const BC_global_forward_axis BC_DEFAULT_FORWARD
static const BC_global_up_axis BC_DEFAULT_UP
BC_global_up_axis
float[4][4] Matrix
BC_global_forward_axis
double[4][4] DMatrix
float[4] Quat
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
static void transpose(Matrix &matrix)
Definition BCMath.cpp:135
float(& rotation() const)[3]
Definition BCMath.cpp:217
BCMatrix(BC_global_forward_axis global_forward_axis, BC_global_up_axis global_up_axis)
Definition BCMath.cpp:48
void add_transform(Matrix &to, const Matrix &transform, const Matrix &from, bool inverted=false)
Definition BCMath.cpp:73
void add_inverted_transform(Matrix &to, const Matrix &transform, const Matrix &from)
Definition BCMath.cpp:104
void set_transform(Object *ob)
Definition BCMath.cpp:111
float(& scale() const)[3]
Definition BCMath.cpp:222
void get_matrix(DMatrix &matrix, bool transposed=false, int precision=-1) const
Definition BCMath.cpp:167
float(& location() const)[3]
Definition BCMath.cpp:212
float(& quat() const)[4]
Definition BCMath.cpp:227
void apply_transform(Matrix &to, const Matrix &transform, const Matrix &from, bool inverse=false)
Definition BCMath.cpp:88
BCMatrix()
Definition BCMath.cpp:43
static void sanitize(Matrix &matrix, int precision)
Definition BCMath.cpp:140
bool in_range(const BCMatrix &other, float distance) const
Definition BCMath.cpp:200
void rotate_to(Matrix &mat_to)
Definition BCMath.cpp:10
#define rot(x, k)
MatBase< C, R > inverse(MatBase< C, R >) RET
#define pow
#define this
#define floor
float distance(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline float2 fabs(const float2 a)
float rot[3]
i
Definition text_draw.cc:230