Skip to content

Commit 88379a6

Browse files
committed
make trprint and printline default format to be compact
1 parent 21f7a4f commit 88379a6

5 files changed

Lines changed: 41 additions & 38 deletions

File tree

‎spatialmath/base/transforms3d.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,7 +1809,7 @@ def tr2adjoint(T):
18091809
raise ValueError('bad argument')
18101810

18111811

1812-
def trprint(T, orient='rpy/zyx', label=None, file=sys.stdout, fmt='{:8.2g}', degsym=True, unit='deg'):
1812+
def trprint(T, orient='rpy/zyx', label=None, file=sys.stdout, fmt='{:.3g}', degsym=True, unit='deg'):
18131813
"""
18141814
Compact display of SO(3) or SE(3) matrices
18151815
@@ -1821,7 +1821,7 @@ def trprint(T, orient='rpy/zyx', label=None, file=sys.stdout, fmt='{:8.2g}', deg
18211821
:type orient: str
18221822
:param file: file to write formatted string to. [default, stdout]
18231823
:type file: file object
1824-
:param fmt: conversion format for each number
1824+
:param fmt: conversion format for each number in the format used with ``format``
18251825
:type fmt: str
18261826
:param unit: angular units: 'rad' [default], or 'deg'
18271827
:type unit: str
@@ -1832,16 +1832,19 @@ def trprint(T, orient='rpy/zyx', label=None, file=sys.stdout, fmt='{:8.2g}', deg
18321832
The matrix is formatted and written to ``file`` and the
18331833
string is returned. To suppress writing to a file, set ``file=None``.
18341834
1835-
- ``trprint(R)`` displays the SO(3) rotation matrix in a compact
1835+
- ``trprint(R)`` prints the SO(3) rotation matrix to stdout in a compact
18361836
single-line format:
18371837
18381838
[LABEL:] ORIENTATION UNIT
18391839
1840-
- ``trprint(T)`` displays the SE(3) homogoneous transform in a compact
1841-
single-line format:
1840+
- ``trprint(T)`` prints the SE(3) homogoneous transform to stdout in a
1841+
compact single-line format:
18421842
18431843
[LABEL:] [t=X, Y, Z;] ORIENTATION UNIT
18441844
1845+
- ``trprint(X, file=None)`` as above but returns the string rather than
1846+
printing to a file
1847+
18451848
Orientation is expressed in one of several formats:
18461849
18471850
- 'rpy/zyx' roll-pitch-yaw angles in ZYX axis order [default]
@@ -1864,7 +1867,9 @@ def trprint(T, orient='rpy/zyx', label=None, file=sys.stdout, fmt='{:8.2g}', deg
18641867
- If the 'rpy' option is selected, then the particular angle sequence can be
18651868
specified with the options 'xyz' or 'yxz' which are passed through to ``tr2rpy``.
18661869
'zyx' is the default.
1867-
- Default formatting is for readable columns of data
1870+
- Default formatting is for compact display of data
1871+
- For tabular data set ``fmt`` to a fixed width format such as
1872+
``fmt='{:.3g}'``
18681873
18691874
:seealso: :func:`~spatialmath.base.transforms2d.trprint2`, :func:`~tr2eul`, :func:`~tr2rpy`, :func:`~tr2angvec`
18701875
:SymPy: not supported

‎spatialmath/baseposematrix.py‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ def simplify(self):
562562

563563
# ----------------------- i/o stuff
564564

565-
def printline(self, **kwargs):
565+
def printline(self, *args, **kwargs):
566566
"""
567-
Stringify pose as a single line (superclass method)
567+
Print pose in compact single line format (superclass method)
568568
569569
:param label: text label to put at start of line
570570
:type label: str
@@ -578,40 +578,38 @@ def printline(self, **kwargs):
578578
:param unit: angular units: 'rad' [default], or 'deg'
579579
:type unit: str
580580
:param file: file to write formatted string to. [default, stdout]
581-
:type file:
582-
:return: formatted string
583-
:rtype: str
581+
:type file: file object
584582
585583
- ``X.printline()`` print ``X`` in single-line format
586584
- ``X.printline(file=None)`` is a string representing the pose ``X`` in single-line format
587585
588586
If ``X`` has multiple values, print one per line.
589587
590-
Example::
588+
Example:
589+
590+
.. runblock:: pycon
591591
592-
>>> x=SE3.Rx(0.3)
592+
>>> x = SE3.Rx(0.3)
593593
>>> x.printline()
594-
t = 0, 0, 0; rpy/zyx = 17°, 0°, 0°
595-
>>> x = SE3.Rx([0.2, 0.3])
594+
>>> x = SE3.Rx([0.2, 0.3], 'rpy/xyz')
596595
>>> x.printline()
597-
t = 0, 0, 0; rpy/zyx = 11°, 0°, 0°
598-
t = 0, 0, 0; rpy/zyx = 17°, 0°, 0°
599-
>> x = SE2(1, 2, 0.3)
596+
>>> x = SE2(1, 2, 0.3)
600597
>>> x.printline()
601-
t = 1, 2; 17 deg
598+
>>> SE3.Rand(N=3).printline(fmt='{:8.3g}')
602599
603-
.. note:: The formatted string is always returned.
600+
.. note::
601+
- Default formatting is for compact display of data
602+
- For tabular data set ``fmt`` to a fixed width format such as
603+
``fmt='{:.3g}'``
604604
605+
:seealso: :func:`trprint`, :func:`trprint2`
605606
"""
606-
s = []
607607
if self.N == 2:
608608
for x in self.data:
609-
s.append(base.trprint2(x, **kwargs))
609+
base.trprint2(x, *args, **kwargs)
610610
else:
611611
for x in self.data:
612-
s.append(base.trprint(x, **kwargs))
613-
614-
return '\n'.join(s)
612+
base.trprint(x, *args, **kwargs)
615613

616614
def __repr__(self):
617615
"""
@@ -1476,4 +1474,4 @@ def _op2(left, right, op): # lgtm[py/not-named-self] pylint: disable=no-self-ar
14761474
from spatialmath import SE3
14771475
x = SE3.Rand(N=6)
14781476

1479-
print(x)
1477+
x.printline('rpy/xyz', fmt='{:8.3g}')

‎tests/base/test_transforms3d.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,24 +371,24 @@ def test_print(self):
371371
R = rotx(0.3) @ roty(0.4)
372372
s = trprint(R, file=None)
373373
self.assertIsInstance(s, str)
374-
self.assertEqual(len(s), 42)
374+
self.assertEqual(len(s), 30)
375375

376376
T = transl(1, 2, 3) @ trotx(0.3) @ troty(0.4)
377377
s = trprint(T, file=None)
378378
self.assertIsInstance(s, str)
379-
self.assertEqual(len(s), 75)
379+
self.assertEqual(len(s), 42)
380380
self.assertTrue('rpy' in s)
381381
self.assertTrue('zyx' in s)
382382

383383
s = trprint(T, file=None, orient='rpy/xyz')
384384
self.assertIsInstance(s, str)
385-
self.assertEqual(len(s), 75)
385+
self.assertEqual(len(s), 39)
386386
self.assertTrue('rpy' in s)
387387
self.assertTrue('xyz' in s)
388388

389389
s = trprint(T, file=None, orient='eul')
390390
self.assertIsInstance(s, str)
391-
self.assertEqual(len(s), 71)
391+
self.assertEqual(len(s), 37)
392392
self.assertTrue('eul' in s)
393393
self.assertFalse('zyx' in s)
394394

‎tests/test_pose2d.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ def test_printline(self):
219219
R = SO2( 0.3)
220220

221221
R.printline()
222-
s = R.printline(file=None)
223-
self.assertIsInstance(s, str)
222+
# s = R.printline(file=None)
223+
# self.assertIsInstance(s, str)
224224

225225
R = SO2([0.3, 0.4, 0.5])
226226
s = R.printline(file=None)
227-
self.assertIsInstance(s, str)
228-
self.assertEqual(s.count('\n'), 2)
227+
# self.assertIsInstance(s, str)
228+
# self.assertEqual(s.count('\n'), 2)
229229

230230
def test_plot(self):
231231
plt.close('all')

‎tests/test_pose3d.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,13 @@ def test_printline(self):
242242
R = SO3.Rx( 0.3)
243243

244244
R.printline()
245-
s = R.printline(file=None)
246-
self.assertIsInstance(s, str)
245+
# s = R.printline(file=None)
246+
# self.assertIsInstance(s, str)
247247

248248
R = SO3.Rx([0.3, 0.4, 0.5])
249249
s = R.printline(file=None)
250-
self.assertIsInstance(s, str)
251-
self.assertEqual(s.count('\n'), 2)
250+
# self.assertIsInstance(s, str)
251+
# self.assertEqual(s.count('\n'), 2)
252252

253253
def test_plot(self):
254254
plt.close('all')

0 commit comments

Comments
 (0)