From 70bc179c45414bd426af49c883fcab1e3bebabf8 Mon Sep 17 00:00:00 2001 From: Gaia Clary Date: Tue, 28 May 2019 09:20:45 +0200 Subject: refactor Collada: rename BCMatrix.* class files to BCMath.* --- source/blender/collada/BCMath.cpp | 208 +++++++++++++++++++++++++++++++++ source/blender/collada/BCMath.h | 109 ++++++++++++++++++ source/blender/collada/BCMatrix.cpp | 209 ---------------------------------- source/blender/collada/BCMatrix.h | 106 ----------------- source/blender/collada/BCSampleData.h | 2 +- source/blender/collada/CMakeLists.txt | 4 +- 6 files changed, 320 insertions(+), 318 deletions(-) create mode 100644 source/blender/collada/BCMath.cpp create mode 100644 source/blender/collada/BCMath.h delete mode 100644 source/blender/collada/BCMatrix.cpp delete mode 100644 source/blender/collada/BCMatrix.h (limited to 'source/blender/collada') diff --git a/source/blender/collada/BCMath.cpp b/source/blender/collada/BCMath.cpp new file mode 100644 index 00000000000..8d4a0778cc1 --- /dev/null +++ b/source/blender/collada/BCMath.cpp @@ -0,0 +1,208 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2008 Blender Foundation. + * All rights reserved. + */ + +#include "BCMath.h" +#include "BlenderContext.h" +#include "collada_utils.h" + +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/BCMath.h b/source/blender/collada/BCMath.h new file mode 100644 index 00000000000..2866e983689 --- /dev/null +++ b/source/blender/collada/BCMath.h @@ -0,0 +1,109 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/** \file + * \ingroup collada + */ + +#ifndef __BCMATRIX_H__ +#define __BCMATRIX_H__ + +#include "BlenderTypes.h" + +extern "C" { +#include "BKE_object.h" +#include "BLI_math.h" +} + +class BCQuat { + private: + mutable Quat q; + + void unit(); + void copy(Quat &r, Quat &a); + + public: + BCQuat(const BCQuat &other) + { + copy_v4_v4(q, other.q); + } + + BCQuat(Quat &other) + { + copy_v4_v4(q, other); + } + + BCQuat() + { + unit_qt(q); + } + + Quat &quat() + { + return q; + } +}; + +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); +}; + +#endif diff --git a/source/blender/collada/BCMatrix.cpp b/source/blender/collada/BCMatrix.cpp deleted file mode 100644 index bcb4d1abab3..00000000000 --- a/source/blender/collada/BCMatrix.cpp +++ /dev/null @@ -1,209 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * The Original Code is Copyright (C) 2008 Blender Foundation. - * All rights reserved. - */ - -#include "BCMatrix.h" -#include "BLI_math.h" -#include "BlenderContext.h" -#include "collada_utils.h" - -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/BCMatrix.h b/source/blender/collada/BCMatrix.h deleted file mode 100644 index 63f84d2fd3d..00000000000 --- a/source/blender/collada/BCMatrix.h +++ /dev/null @@ -1,106 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -/** \file - * \ingroup collada - */ - -#ifndef __BCMATRIX_H__ -#define __BCMATRIX_H__ - -#include "BlenderTypes.h" -#include "BKE_object.h" -#include "BLI_math.h" - -class BCQuat { - private: - mutable Quat q; - - void unit(); - void copy(Quat &r, Quat &a); - - public: - BCQuat(const BCQuat &other) - { - copy_v4_v4(q, other.q); - } - - BCQuat(Quat &other) - { - copy_v4_v4(q, other); - } - - BCQuat() - { - unit_qt(q); - } - - Quat &quat() - { - return q; - } -}; - -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); -}; - -#endif diff --git a/source/blender/collada/BCSampleData.h b/source/blender/collada/BCSampleData.h index 33b460d409c..c4e840ac3b3 100644 --- a/source/blender/collada/BCSampleData.h +++ b/source/blender/collada/BCSampleData.h @@ -26,7 +26,7 @@ #include "ExportSettings.h" #include "BCSampleData.h" -#include "BCMatrix.h" +#include "BCMath.h" extern "C" { #include "BKE_object.h" diff --git a/source/blender/collada/CMakeLists.txt b/source/blender/collada/CMakeLists.txt index 857aea02dc9..5f28ca4ffbb 100644 --- a/source/blender/collada/CMakeLists.txt +++ b/source/blender/collada/CMakeLists.txt @@ -63,7 +63,7 @@ set(SRC ArmatureImporter.cpp BCAnimationCurve.cpp BCAnimationSampler.cpp - BCMatrix.cpp + BCMath.cpp BCSampleData.cpp BlenderContext.cpp CameraExporter.cpp @@ -98,7 +98,7 @@ set(SRC ArmatureImporter.h BCAnimationCurve.h BCAnimationSampler.h - BCMatrix.h + BCMath.h BCSampleData.h BlenderContext.h BlenderTypes.h -- cgit v1.2.3