Skip to content

Commit 29377ed

Browse files
authored
Merge pull request #780 from compas-dev/pretty_transformation
pretty print transformations
2 parents 6f58a9b + b6a0402 commit 29377ed

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
### Added
1212

13+
* Added the magic method `__str__` to `compas.geoemetry.Transformation`.
14+
1315
### Changed
1416

1517
* Fixed bug where mimic joints were considered configurable.

src/compas/geometry/transformations/transformation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ def __eq__(self, other, tol=1e-05):
100100
def __repr__(self):
101101
return "Transformation({})".format(self.matrix)
102102

103+
def __str__(self):
104+
s = "[[%s],\n" % ",".join([("%.4f" % n).rjust(10) for n in self.matrix[0]])
105+
s += " [%s],\n" % ",".join([("%.4f" % n).rjust(10) for n in self.matrix[1]])
106+
s += " [%s],\n" % ",".join([("%.4f" % n).rjust(10) for n in self.matrix[2]])
107+
s += " [%s]]\n" % ",".join([("%.4f" % n).rjust(10) for n in self.matrix[3]])
108+
return s
109+
103110
def __len__(self):
104111
return len(self.matrix)
105112

tests/compas/geometry/test_transformations/test_transformation.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,30 @@ def test_list():
108108
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]
109109

110110

111-
def concatenate():
111+
def test_concatenated():
112112
trans1 = [1, 2, 3]
113113
angle1 = [-2.142, 1.141, -0.142]
114114
T1 = Translation.from_vector(trans1)
115115
R1 = Rotation.from_euler_angles(angle1)
116-
M1 = T1.concatenate(R1)
116+
M1 = T1.concatenated(R1)
117117
assert allclose(M1, T1 * R1)
118+
119+
120+
def test___repr__():
121+
trans = [1, 2, 3]
122+
axes = [-2.142, 1.141, -0.142]
123+
angle = 0.7854
124+
R = Rotation.from_axis_and_angle(axes, angle, point=trans)
125+
assert R == eval(repr(R))
126+
127+
128+
def test___str__():
129+
s = '[[ 0.9345, -0.0798, 0.3469, -0.8157],\n' + \
130+
' [ -0.1624, 0.7716, 0.6150, -1.2258],\n' + \
131+
' [ -0.3168, -0.6311, 0.7081, 2.4546],\n' + \
132+
' [ 0.0000, 0.0000, 0.0000, 1.0000]]\n'
133+
trans = [1, 2, 3]
134+
axes = [-2.142, 1.141, -0.142]
135+
angle = 0.7854
136+
R = Rotation.from_axis_and_angle(axes, angle, point=trans)
137+
assert s == str(R)

0 commit comments

Comments
 (0)