From 4f4abe3350c5e9192532d0ece69fe43838a07c40 Mon Sep 17 00:00:00 2001 From: Michael Kirsch Date: Sat, 29 Jun 2019 00:37:47 +0200 Subject: [PATCH] rewrite functionality of inverse function --- xs/src/libslic3r/TransformationMatrix.cpp | 9 ++++++--- xs/src/libslic3r/TransformationMatrix.hpp | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/xs/src/libslic3r/TransformationMatrix.cpp b/xs/src/libslic3r/TransformationMatrix.cpp index ec0884ff7..93f6d7a42 100644 --- a/xs/src/libslic3r/TransformationMatrix.cpp +++ b/xs/src/libslic3r/TransformationMatrix.cpp @@ -83,17 +83,20 @@ double TransformationMatrix::determinante() const return m11*(m22*m33 - m23*m32) - m12*(m21*m33 - m23*m31) + m13*(m21*m32 - m31*m22); } -bool TransformationMatrix::inverse(TransformationMatrix &inverse) const +TransformationMatrix TransformationMatrix::inverse() const { // from http://mathworld.wolfram.com/MatrixInverse.html // and https://math.stackexchange.com/questions/152462/inverse-of-transformation-matrix double det = this->determinante(); if (abs(det) < 1e-9) { - return false; + printf("Matrix (very close to) singular. Inverse cannot be computed"); + return TransformationMatrix(); } double fac = 1.0 / det; + TransformationMatrix inverse; + inverse.m11 = fac * (this->m22 * this->m33 - this->m23 * this->m32); inverse.m12 = fac * (this->m13 * this->m32 - this->m12 * this->m33); inverse.m13 = fac * (this->m12 * this->m23 - this->m13 * this->m22); @@ -108,7 +111,7 @@ bool TransformationMatrix::inverse(TransformationMatrix &inverse) const inverse.m24 = -(inverse.m21 * this->m14 + inverse.m22 * this->m24 + inverse.m23 * this->m34); inverse.m34 = -(inverse.m31 * this->m14 + inverse.m32 * this->m24 + inverse.m33 * this->m34); - return true; + return inverse; } void TransformationMatrix::applyLeft(const TransformationMatrix &left) diff --git a/xs/src/libslic3r/TransformationMatrix.hpp b/xs/src/libslic3r/TransformationMatrix.hpp index c553eb0ba..f5e87fd46 100644 --- a/xs/src/libslic3r/TransformationMatrix.hpp +++ b/xs/src/libslic3r/TransformationMatrix.hpp @@ -33,7 +33,7 @@ public: double determinante() const; /// returns the inverse of the matrix - bool inverse(TransformationMatrix &inverse) const; + TransformationMatrix inverse() const; /// multiplies the parameter-matrix from the left (this=left*this) void applyLeft(const TransformationMatrix &left);