From e44c49d89d67038657cdcbd373e64b9a17b6c993 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Sat, 6 Sep 2014 14:54:08 +0200 Subject: Py Mathutils: add `invert_safe()` and `inverted_safe()` to `Matrix`. Those two mimic our BLI invert_m4_m4_safe - they add a small offset to diagonal values, in case org matrix is degenerated, and if still non-invertible, return identity matrix. Org patch by me, final enhanced version by ideasman42, many thanks! --- tests/python/bl_pyapi_mathutils.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests') diff --git a/tests/python/bl_pyapi_mathutils.py b/tests/python/bl_pyapi_mathutils.py index 7354f559b95..85232e465d7 100644 --- a/tests/python/bl_pyapi_mathutils.py +++ b/tests/python/bl_pyapi_mathutils.py @@ -162,6 +162,29 @@ class MatrixTesting(unittest.TestCase): self.assertEqual(mat.inverted(), inv_mat) + def test_matrix_inverse_safe(self): + mat = Matrix(((1, 4, 0, -1), + (2, -1, 0, -2), + (0, 3, 0, 3), + (-2, 9, 0, 0))) + + # Warning, if we change epsilon in py api we have to update this!!! + epsilon = 1e-8 + inv_mat_safe = mat.copy() + inv_mat_safe[0][0] += epsilon + inv_mat_safe[1][1] += epsilon + inv_mat_safe[2][2] += epsilon + inv_mat_safe[3][3] += epsilon + inv_mat_safe.invert() + ''' + inv_mat_safe = Matrix(((1.0, -0.5, 0.0, -0.5), + (0.222222, -0.111111, -0.0, 0.0), + (-333333344.0, 316666656.0, 100000000.0, 150000000.0), + (0.888888, -0.9444444, 0.0, -0.5))) + ''' + + self.assertEqual(mat.inverted_safe(), inv_mat_safe) + def test_matrix_mult(self): mat = Matrix(((1, 4, 0, -1), (2, -1, 2, -2), -- cgit v1.2.3