diff options
author | Gaia Clary <gaia.clary@machinimatrix.org> | 2019-05-28 10:23:05 +0300 |
---|---|---|
committer | Gaia Clary <gaia.clary@machinimatrix.org> | 2019-06-03 00:00:56 +0300 |
commit | 122b9478c66e13fa3c14da606cfa92528acd0b87 (patch) | |
tree | d2105c15b7b67aac115b3e1271dbe8996433d1ec /source/blender/collada | |
parent | 70bc179c45414bd426af49c883fcab1e3bebabf8 (diff) |
refactor collada: replace bc_sanitize_mat() by static class method in BCMatrix
Diffstat (limited to 'source/blender/collada')
-rw-r--r-- | source/blender/collada/ArmatureExporter.cpp | 2 | ||||
-rw-r--r-- | source/blender/collada/BCMath.cpp | 15 | ||||
-rw-r--r-- | source/blender/collada/BCMath.h | 5 | ||||
-rw-r--r-- | source/blender/collada/BCSampleData.cpp | 190 | ||||
-rw-r--r-- | source/blender/collada/BlenderContext.h | 49 | ||||
-rw-r--r-- | source/blender/collada/ControllerExporter.cpp | 4 | ||||
-rw-r--r-- | source/blender/collada/ExportSettings.h | 1 | ||||
-rw-r--r-- | source/blender/collada/TransformWriter.cpp | 3 | ||||
-rw-r--r-- | source/blender/collada/collada_utils.cpp | 23 | ||||
-rw-r--r-- | source/blender/collada/collada_utils.h | 2 |
10 files changed, 21 insertions, 273 deletions
diff --git a/source/blender/collada/ArmatureExporter.cpp b/source/blender/collada/ArmatureExporter.cpp index ed645ee5001..84979cc4ca4 100644 --- a/source/blender/collada/ArmatureExporter.cpp +++ b/source/blender/collada/ArmatureExporter.cpp @@ -315,7 +315,7 @@ void ArmatureExporter::add_bone_transform(Object *ob_arm, Bone *bone, COLLADASW: } if (this->export_settings.get_limit_precision()) { - bc_sanitize_mat(mat, LIMITTED_PRECISION); + BCMatrix::sanitize(mat, LIMITTED_PRECISION); } TransformWriter::add_joint_transform(node, mat, NULL, this->export_settings, has_restmat); diff --git a/source/blender/collada/BCMath.cpp b/source/blender/collada/BCMath.cpp index 8d4a0778cc1..a07d1e735ff 100644 --- a/source/blender/collada/BCMath.cpp +++ b/source/blender/collada/BCMath.cpp @@ -19,7 +19,6 @@ #include "BCMath.h" #include "BlenderContext.h" -#include "collada_utils.h" BCMatrix::BCMatrix(const BCMatrix &mat) { @@ -134,7 +133,19 @@ void BCMatrix::transpose(Matrix &mat) void BCMatrix::sanitize(Matrix &mat, int precision) { - bc_sanitize_mat(mat, precision); + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) { + double val = (double)mat[i][j]; + val = double_round(val, precision); + mat[i][j] = (float)val; + } +} + +void BCMatrix::sanitize(DMatrix &mat, int precision) +{ + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + mat[i][j] = double_round(mat[i][j], precision); } void BCMatrix::unit() diff --git a/source/blender/collada/BCMath.h b/source/blender/collada/BCMath.h index 2866e983689..00e8f24c970 100644 --- a/source/blender/collada/BCMath.h +++ b/source/blender/collada/BCMath.h @@ -32,9 +32,6 @@ class BCQuat { private: mutable Quat q; - void unit(); - void copy(Quat &r, Quat &a); - public: BCQuat(const BCQuat &other) { @@ -102,7 +99,9 @@ class BCMatrix { void apply_transform(const BCMatrix &matrix, const bool inverted = false); const bool in_range(const BCMatrix &other, float distance) const; + static void sanitize(Matrix &matrix, int precision); + static void sanitize(DMatrix &matrix, int precision); static void transpose(Matrix &matrix); }; diff --git a/source/blender/collada/BCSampleData.cpp b/source/blender/collada/BCSampleData.cpp index 6258c9c9bdb..fff3c393a1d 100644 --- a/source/blender/collada/BCSampleData.cpp +++ b/source/blender/collada/BCSampleData.cpp @@ -75,193 +75,3 @@ const BCMatrix &BCSample::get_matrix() const { return obmat; } - -BCMatrix::BCMatrix(const BCMatrix &mat) -{ - set_transform(mat.matrix); -} - -BCMatrix::BCMatrix(Matrix &mat) -{ - set_transform(mat); -} - -BCMatrix::BCMatrix(Object *ob) -{ - set_transform(ob); -} - -BCMatrix::BCMatrix() -{ - unit(); -} - -BCMatrix::BCMatrix(BC_global_forward_axis global_forward_axis, BC_global_up_axis global_up_axis) -{ - float mrot[3][3]; - float mat[4][4]; - mat3_from_axis_conversion( - BC_DEFAULT_FORWARD, BC_DEFAULT_UP, global_forward_axis, global_up_axis, mrot); - - transpose_m3(mrot); // TODO: Verify that mat3_from_axis_conversion() returns a transposed matrix - copy_m4_m3(mat, mrot); - set_transform(mat); -} - -void BCMatrix::add_transform(const Matrix &mat, bool inverse) -{ - add_transform(this->matrix, mat, this->matrix, inverse); -} - -void BCMatrix::add_transform(const BCMatrix &mat, bool inverse) -{ - add_transform(this->matrix, mat.matrix, this->matrix, inverse); -} - -void BCMatrix::apply_transform(const BCMatrix &mat, bool inverse) -{ - apply_transform(this->matrix, mat.matrix, this->matrix, inverse); -} - -void BCMatrix::add_transform(Matrix &to, const Matrix &transform, const Matrix &from, bool inverse) -{ - if (inverse) { - Matrix globinv; - invert_m4_m4(globinv, transform); - add_transform(to, globinv, from, /*inverse=*/false); - } - else { - mul_m4_m4m4(to, transform, from); - } -} - -void BCMatrix::apply_transform(Matrix &to, - const Matrix &transform, - const Matrix &from, - bool inverse) -{ - Matrix globinv; - invert_m4_m4(globinv, transform); - if (inverse) { - add_transform(to, globinv, from, /*inverse=*/false); - } - else { - mul_m4_m4m4(to, transform, from); - mul_m4_m4m4(to, to, globinv); - } -} - -void BCMatrix::add_inverted_transform(Matrix &to, const Matrix &transform, const Matrix &from) -{ - Matrix workmat; - invert_m4_m4(workmat, transform); - mul_m4_m4m4(to, workmat, from); -} - -void BCMatrix::set_transform(Object *ob) -{ - Matrix lmat; - - BKE_object_matrix_local_get(ob, lmat); - copy_m4_m4(matrix, lmat); - - mat4_decompose(this->loc, this->q, this->size, lmat); - quat_to_compatible_eul(this->rot, ob->rot, this->q); -} - -void BCMatrix::set_transform(Matrix &mat) -{ - copy_m4_m4(matrix, mat); - mat4_decompose(this->loc, this->q, this->size, mat); - quat_to_eul(this->rot, this->q); -} - -void BCMatrix::copy(Matrix &out, Matrix &in) -{ - /* destination comes first: */ - memcpy(out, in, sizeof(Matrix)); -} - -void BCMatrix::transpose(Matrix &mat) -{ - transpose_m4(mat); -} - -void BCMatrix::sanitize(Matrix &mat, int precision) -{ - bc_sanitize_mat(mat, precision); -} - -void BCMatrix::unit() -{ - unit_m4(this->matrix); - mat4_decompose(this->loc, this->q, this->size, this->matrix); - quat_to_eul(this->rot, this->q); -} - -/* We need double here because the OpenCollada API needs it. - * precision = -1 indicates to not limit the precision. */ -void BCMatrix::get_matrix(DMatrix &mat, const bool transposed, const int precision) const -{ - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - float val = (transposed) ? matrix[j][i] : matrix[i][j]; - if (precision >= 0) { - val = floor((val * pow(10, precision) + 0.5)) / pow(10, precision); - } - mat[i][j] = val; - } - } -} - -void BCMatrix::get_matrix(Matrix &mat, - const bool transposed, - const int precision, - const bool inverted) const -{ - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - float val = (transposed) ? matrix[j][i] : matrix[i][j]; - if (precision >= 0) { - val = floor((val * pow(10, precision) + 0.5)) / pow(10, precision); - } - mat[i][j] = val; - } - } - - if (inverted) { - invert_m4(mat); - } -} - -const bool BCMatrix::in_range(const BCMatrix &other, float distance) const -{ - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - if (fabs(other.matrix[i][j] - matrix[i][j]) > distance) { - return false; - } - } - } - return true; -} - -float (&BCMatrix::location() const)[3] -{ - return loc; -} - -float (&BCMatrix::rotation() const)[3] -{ - return rot; -} - -float (&BCMatrix::scale() const)[3] -{ - return size; -} - -float (&BCMatrix::quat() const)[4] -{ - return q; -} diff --git a/source/blender/collada/BlenderContext.h b/source/blender/collada/BlenderContext.h index bf5c0e85280..50781e8eede 100644 --- a/source/blender/collada/BlenderContext.h +++ b/source/blender/collada/BlenderContext.h @@ -50,55 +50,6 @@ void bc_set_mark(Object *ob); #ifdef __cplusplus } -class BCMatrix { - - private: - mutable float matrix[4][4]; - mutable float size[3]; - mutable float rot[3]; - mutable float loc[3]; - mutable float q[4]; - - void unit(); - void copy(Matrix &r, Matrix &a); - - public: - float (&location() const)[3]; - float (&rotation() const)[3]; - float (&scale() const)[3]; - float (&quat() const)[4]; - - BCMatrix(BC_global_forward_axis global_forward_axis, BC_global_up_axis global_up_axis); - BCMatrix(const BCMatrix &mat); - BCMatrix(Matrix &mat); - BCMatrix(Object *ob); - BCMatrix(); - - void get_matrix(DMatrix &matrix, const bool transposed = false, const int precision = -1) const; - void get_matrix(Matrix &matrix, - const bool transposed = false, - const int precision = -1, - const bool inverted = false) const; - void set_transform(Object *ob); - void set_transform(Matrix &mat); - void add_transform(Matrix &to, - const Matrix &transform, - const Matrix &from, - const bool inverted = false); - void apply_transform(Matrix &to, - const Matrix &transform, - const Matrix &from, - const bool inverted = false); - void add_inverted_transform(Matrix &to, const Matrix &transform, const Matrix &from); - void add_transform(const Matrix &matrix, const bool inverted = false); - void add_transform(const BCMatrix &matrix, const bool inverted = false); - void apply_transform(const BCMatrix &matrix, const bool inverted = false); - - const bool in_range(const BCMatrix &other, float distance) const; - static void sanitize(Matrix &matrix, int precision); - static void transpose(Matrix &matrix); -}; - class BlenderContext { private: bContext *context; diff --git a/source/blender/collada/ControllerExporter.cpp b/source/blender/collada/ControllerExporter.cpp index 1e1da13fc5f..b574635217c 100644 --- a/source/blender/collada/ControllerExporter.cpp +++ b/source/blender/collada/ControllerExporter.cpp @@ -444,7 +444,7 @@ void ControllerExporter::add_bind_shape_mat(Object *ob) // UnitConverter::mat4_to_dae_double(bind_mat, ob->obmat); UnitConverter::mat4_to_dae_double(bind_mat, f_obmat); if (this->export_settings.get_limit_precision()) { - bc_sanitize_mat(bind_mat, LIMITTED_PRECISION); + BCMatrix::sanitize(bind_mat, LIMITTED_PRECISION); } addBindShapeTransform(bind_mat); @@ -569,7 +569,7 @@ std::string ControllerExporter::add_inv_bind_mats_source(Object *ob_arm, invert_m4_m4(mat, world); UnitConverter::mat4_to_dae(inv_bind_mat, mat); if (this->export_settings.get_limit_precision()) { - bc_sanitize_mat(inv_bind_mat, LIMITTED_PRECISION); + BCMatrix::sanitize(inv_bind_mat, LIMITTED_PRECISION); } source.appendValues(inv_bind_mat); } diff --git a/source/blender/collada/ExportSettings.h b/source/blender/collada/ExportSettings.h index 36f3ea40da9..1e158418120 100644 --- a/source/blender/collada/ExportSettings.h +++ b/source/blender/collada/ExportSettings.h @@ -23,6 +23,7 @@ #ifdef __cplusplus # include <vector> +# include "BCMath.h" extern "C" { #endif diff --git a/source/blender/collada/TransformWriter.cpp b/source/blender/collada/TransformWriter.cpp index acf7d3c78b4..0b1f867ba99 100644 --- a/source/blender/collada/TransformWriter.cpp +++ b/source/blender/collada/TransformWriter.cpp @@ -86,8 +86,9 @@ void TransformWriter::add_node_transform_ob(COLLADASW::Node &node, UnitConverter converter; double d_obmat[4][4]; converter.mat4_to_dae_double(d_obmat, f_obmat); + if (limit_precision) { - bc_sanitize_mat(d_obmat, LIMITTED_PRECISION); + BCMatrix::sanitize(d_obmat, LIMITTED_PRECISION); } node.addMatrix("transform", d_obmat); break; diff --git a/source/blender/collada/collada_utils.cpp b/source/blender/collada/collada_utils.cpp index 9e480e7faf0..e42ceda7da8 100644 --- a/source/blender/collada/collada_utils.cpp +++ b/source/blender/collada/collada_utils.cpp @@ -1085,20 +1085,6 @@ void bc_create_restpose_mat(BCExportSettings &export_settings, loc_eulO_size_to_mat4(to_mat, loc, rot, scale, 6); } -/* - * Make 4*4 matrices better readable - */ -void bc_sanitize_mat(float mat[4][4], int precision) -{ - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - double val = (double)mat[i][j]; - val = double_round(val, precision); - mat[i][j] = (float)val; - } - } -} - void bc_sanitize_v3(float v[3], int precision) { for (int i = 0; i < 3; i++) { @@ -1108,15 +1094,6 @@ void bc_sanitize_v3(float v[3], int precision) } } -void bc_sanitize_mat(double mat[4][4], int precision) -{ - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - mat[i][j] = double_round(mat[i][j], precision); - } - } -} - void bc_sanitize_v3(double v[3], int precision) { for (int i = 0; i < 3; i++) { diff --git a/source/blender/collada/collada_utils.h b/source/blender/collada/collada_utils.h index 5c90e762373..2b74d8ee3ad 100644 --- a/source/blender/collada/collada_utils.h +++ b/source/blender/collada/collada_utils.h @@ -217,8 +217,6 @@ void bc_copy_darray_m4d(double *r, double a[4][4]); void bc_copy_m4d_v44(double (&r)[4][4], std::vector<std::vector<double>> &a); void bc_copy_v44_m4d(std::vector<std::vector<double>> &a, double (&r)[4][4]); -void bc_sanitize_mat(float mat[4][4], int precision); -void bc_sanitize_mat(double mat[4][4], int precision); void bc_sanitize_v3(double v[3], int precision); void bc_sanitize_v3(float v[3], int precision); |