Blender  V3.3
BCMath.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2008 Blender Foundation. All rights reserved. */
3 
4 #include "BLI_utildefines.h"
5 
6 #include "BCMath.h"
7 #include "BlenderContext.h"
8 
9 void BCQuat::rotate_to(Matrix &mat_to)
10 {
11  Quat qd;
12  Matrix matd;
13  Matrix mati;
14  Matrix mat_from;
15 
16  quat_to_mat4(mat_from, q);
17 
18  /* Calculate the difference matrix matd between mat_from and mat_to */
19  invert_m4_m4(mati, mat_from);
20  mul_m4_m4m4(matd, mati, mat_to);
21 
22  mat4_to_quat(qd, matd);
23 
24  mul_qt_qtqt(q, qd, q); /* rotate to the final rotation to mat_to */
25 }
26 
28 {
29  set_transform(mat.matrix);
30 }
31 
32 BCMatrix::BCMatrix(Matrix &mat)
33 {
34  set_transform(mat);
35 }
36 
38 {
39  set_transform(ob);
40 }
41 
43 {
44  unit();
45 }
46 
47 BCMatrix::BCMatrix(BC_global_forward_axis global_forward_axis, BC_global_up_axis global_up_axis)
48 {
49  float mrot[3][3];
50  float mat[4][4];
52  global_forward_axis, global_up_axis, BC_DEFAULT_FORWARD, BC_DEFAULT_UP, mrot);
53  copy_m4_m3(mat, mrot);
54  set_transform(mat);
55 }
56 
57 void BCMatrix::add_transform(const Matrix &mat, bool inverted)
58 {
59  add_transform(this->matrix, mat, this->matrix, inverted);
60 }
61 
62 void BCMatrix::add_transform(const BCMatrix &mat, bool inverted)
63 {
64  add_transform(this->matrix, mat.matrix, this->matrix, inverted);
65 }
66 
67 void BCMatrix::apply_transform(const BCMatrix &mat, bool inverted)
68 {
69  apply_transform(this->matrix, mat.matrix, this->matrix, inverted);
70 }
71 
72 void BCMatrix::add_transform(Matrix &to,
73  const Matrix &transform,
74  const Matrix &from,
75  bool inverted)
76 {
77  if (inverted) {
78  Matrix globinv;
79  invert_m4_m4(globinv, transform);
80  add_transform(to, globinv, from, /*inverted=*/false);
81  }
82  else {
84  }
85 }
86 
87 void BCMatrix::apply_transform(Matrix &to,
88  const Matrix &transform,
89  const Matrix &from,
90  bool inverse)
91 {
92  Matrix globinv;
93  invert_m4_m4(globinv, transform);
94  if (inverse) {
95  add_transform(to, globinv, from, /*inverted=*/false);
96  }
97  else {
99  mul_m4_m4m4(to, to, globinv);
100  }
101 }
102 
103 void BCMatrix::add_inverted_transform(Matrix &to, const Matrix &transform, const Matrix &from)
104 {
105  Matrix workmat;
106  invert_m4_m4(workmat, transform);
107  mul_m4_m4m4(to, workmat, from);
108 }
109 
111 {
112  Matrix lmat;
113 
114  BKE_object_matrix_local_get(ob, lmat);
115  copy_m4_m4(matrix, lmat);
116 
117  mat4_decompose(this->loc, this->q, this->size, lmat);
118  quat_to_compatible_eul(this->rot, ob->rot, this->q);
119 }
120 
121 void BCMatrix::set_transform(Matrix &mat)
122 {
123  copy_m4_m4(matrix, mat);
124  mat4_decompose(this->loc, this->q, this->size, mat);
125  quat_to_eul(this->rot, this->q);
126 }
127 
128 void BCMatrix::copy(Matrix &r, Matrix &a)
129 {
130  /* destination comes first: */
131  memcpy(r, a, sizeof(Matrix));
132 }
133 
134 void BCMatrix::transpose(Matrix &mat)
135 {
136  transpose_m4(mat);
137 }
138 
139 void BCMatrix::sanitize(Matrix &mat, int precision)
140 {
141  for (auto &row : mat) {
142  for (float &cell : row) {
143  double val = (double)cell;
144  val = double_round(val, precision);
145  cell = (float)val;
146  }
147  }
148 }
149 
150 void BCMatrix::sanitize(DMatrix &mat, int precision)
151 {
152  for (auto &row : mat) {
153  for (double &cell : row) {
154  cell = double_round(cell, precision);
155  }
156  }
157 }
158 
159 void BCMatrix::unit()
160 {
161  unit_m4(this->matrix);
162  mat4_decompose(this->loc, this->q, this->size, this->matrix);
163  quat_to_eul(this->rot, this->q);
164 }
165 
166 void BCMatrix::get_matrix(DMatrix &mat, const bool transposed, const int precision) const
167 {
168  for (int i = 0; i < 4; i++) {
169  for (int j = 0; j < 4; j++) {
170  float val = (transposed) ? matrix[j][i] : matrix[i][j];
171  if (precision >= 0) {
172  val = floor((val * pow(10, precision) + 0.5)) / pow(10, precision);
173  }
174  mat[i][j] = val;
175  }
176  }
177 }
178 
179 void BCMatrix::get_matrix(Matrix &mat,
180  const bool transposed,
181  const int precision,
182  const bool inverted) const
183 {
184  for (int i = 0; i < 4; i++) {
185  for (int j = 0; j < 4; j++) {
186  float val = (transposed) ? matrix[j][i] : matrix[i][j];
187  if (precision >= 0) {
188  val = floor((val * pow(10, precision) + 0.5)) / pow(10, precision);
189  }
190  mat[i][j] = val;
191  }
192  }
193 
194  if (inverted) {
195  invert_m4(mat);
196  }
197 }
198 
199 bool BCMatrix::in_range(const BCMatrix &other, float distance) const
200 {
201  for (int i = 0; i < 4; i++) {
202  for (int j = 0; j < 4; j++) {
203  if (fabs(other.matrix[i][j] - matrix[i][j]) > distance) {
204  return false;
205  }
206  }
207  }
208  return true;
209 }
210 
212 {
213  return loc;
214 }
215 
217 {
218  return rot;
219 }
220 
221 float (&BCMatrix::scale() const)[3]
222 {
223  return size;
224 }
225 
226 float (&BCMatrix::quat() const)[4]
227 {
228  return q;
229 }
typedef float(TangentPoint)[2]
void BKE_object_matrix_local_get(struct Object *ob, float r_mat[4][4])
Definition: object.cc:3093
double double_round(double x, int ndigits)
Definition: math_base.c:27
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:259
bool invert_m4(float R[4][4])
Definition: math_matrix.c:1206
void mat4_decompose(float loc[3], float quat[4], float size[3], const float wmat[4][4])
Definition: math_matrix.c:2253
void unit_m4(float m[4][4])
Definition: rct.c:1090
void copy_m4_m3(float m1[4][4], const float m2[3][3])
Definition: math_matrix.c:102
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1287
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:77
void transpose_m4(float R[4][4])
Definition: math_matrix.c:1377
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])
Definition: math_rotation.c:46
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])
void quat_to_mat4(float mat[4][4], const float q[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
Definition: BlenderTypes.h:24
BC_global_forward_axis
Definition: BlenderTypes.h:15
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
btMatrix3x3 inverse() const
Return the inverse of the matrix.
Definition: btTransform.h:182
static void transpose(Matrix &matrix)
Definition: BCMath.cpp:134
float(& rotation() const)[3]
Definition: BCMath.cpp:216
void add_transform(Matrix &to, const Matrix &transform, const Matrix &from, bool inverted=false)
Definition: BCMath.cpp:72
void add_inverted_transform(Matrix &to, const Matrix &transform, const Matrix &from)
Definition: BCMath.cpp:103
void set_transform(Object *ob)
Definition: BCMath.cpp:110
float(& scale() const)[3]
Definition: BCMath.cpp:221
void get_matrix(DMatrix &matrix, bool transposed=false, int precision=-1) const
Definition: BCMath.cpp:166
float(& location() const)[3]
Definition: BCMath.cpp:211
float(& quat() const)[4]
Definition: BCMath.cpp:226
void apply_transform(Matrix &to, const Matrix &transform, const Matrix &from, bool inverse=false)
Definition: BCMath.cpp:87
BCMatrix()
Definition: BCMath.cpp:42
static void sanitize(Matrix &matrix, int precision)
Definition: BCMath.cpp:139
bool in_range(const BCMatrix &other, float distance) const
Definition: BCMath.cpp:199
void rotate_to(Matrix &mat_to)
Definition: BCMath.cpp:9
StackEntry * from
#define rot(x, k)
ccl_device_inline float2 fabs(const float2 &a)
Definition: math_float2.h:222
ccl_device_inline float3 pow(float3 v, float e)
Definition: math_float3.h:533
static unsigned a[3]
Definition: RandGen.cpp:78
T distance(const T &a, const T &b)
T floor(const T &a)
float rot[3]