diff --git a/CHANGELOG.md b/CHANGELOG.md index 053d5454dfed..f694cc549246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +* Added the magic method `__str__` to `compas.geoemetry.Transformation`. + ### Changed * Fixed bug where mimic joints were considered configurable. diff --git a/src/compas/geometry/transformations/transformation.py b/src/compas/geometry/transformations/transformation.py index f1dbc4eed299..821046fd3ace 100644 --- a/src/compas/geometry/transformations/transformation.py +++ b/src/compas/geometry/transformations/transformation.py @@ -100,6 +100,13 @@ def __eq__(self, other, tol=1e-05): def __repr__(self): return "Transformation({})".format(self.matrix) + def __str__(self): + s = "[[%s],\n" % ",".join([("%.4f" % n).rjust(10) for n in self.matrix[0]]) + s += " [%s],\n" % ",".join([("%.4f" % n).rjust(10) for n in self.matrix[1]]) + s += " [%s],\n" % ",".join([("%.4f" % n).rjust(10) for n in self.matrix[2]]) + s += " [%s]]\n" % ",".join([("%.4f" % n).rjust(10) for n in self.matrix[3]]) + return s + def __len__(self): return len(self.matrix) diff --git a/tests/compas/geometry/test_transformations/test_transformation.py b/tests/compas/geometry/test_transformations/test_transformation.py index 703ae6597701..c2f76b4fa662 100644 --- a/tests/compas/geometry/test_transformations/test_transformation.py +++ b/tests/compas/geometry/test_transformations/test_transformation.py @@ -108,10 +108,30 @@ def test_list(): assert T.list == [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0] -def concatenate(): +def test_concatenated(): trans1 = [1, 2, 3] angle1 = [-2.142, 1.141, -0.142] T1 = Translation.from_vector(trans1) R1 = Rotation.from_euler_angles(angle1) - M1 = T1.concatenate(R1) + M1 = T1.concatenated(R1) assert allclose(M1, T1 * R1) + + +def test___repr__(): + trans = [1, 2, 3] + axes = [-2.142, 1.141, -0.142] + angle = 0.7854 + R = Rotation.from_axis_and_angle(axes, angle, point=trans) + assert R == eval(repr(R)) + + +def test___str__(): + s = '[[ 0.9345, -0.0798, 0.3469, -0.8157],\n' + \ + ' [ -0.1624, 0.7716, 0.6150, -1.2258],\n' + \ + ' [ -0.3168, -0.6311, 0.7081, 2.4546],\n' + \ + ' [ 0.0000, 0.0000, 0.0000, 1.0000]]\n' + trans = [1, 2, 3] + axes = [-2.142, 1.141, -0.142] + angle = 0.7854 + R = Rotation.from_axis_and_angle(axes, angle, point=trans) + assert s == str(R)