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