19
19
import scipy .interpolate as interpolate
20
20
from typing import Optional
21
21
from functools import lru_cache
22
+ import warnings
22
23
23
24
_eps = np .finfo (np .float64 ).eps
24
25
26
+
25
27
def qeye () -> QuaternionArray :
26
28
"""
27
29
Create an identity quaternion
@@ -56,7 +58,7 @@ def qpure(v: ArrayLike3) -> QuaternionArray:
56
58
57
59
.. runblock:: pycon
58
60
59
- >>> from spatialmath.base import pure , qprint
61
+ >>> from spatialmath.base import qpure , qprint
60
62
>>> q = qpure([1, 2, 3])
61
63
>>> qprint(q)
62
64
"""
@@ -1088,14 +1090,53 @@ def qangle(q1: ArrayLike4, q2: ArrayLike4) -> float:
1088
1090
return 4.0 * math .atan2 (smb .norm (q1 - q2 ), smb .norm (q1 + q2 ))
1089
1091
1090
1092
1093
+ def q2str (
1094
+ q : Union [ArrayLike4 , ArrayLike4 ],
1095
+ delim : Optional [Tuple [str , str ]] = ("<" , ">" ),
1096
+ fmt : Optional [str ] = "{: .4f}" ,
1097
+ ) -> str :
1098
+ """
1099
+ Format a quaternion as a string
1100
+
1101
+ :arg q: unit-quaternion
1102
+ :type q: array_like(4)
1103
+ :arg delim: 2-list of delimeters [default ('<', '>')]
1104
+ :type delim: list or tuple of strings
1105
+ :arg fmt: printf-style format soecifier [default '{: .4f}']
1106
+ :type fmt: str
1107
+ :return: formatted string
1108
+ :rtype: str
1109
+
1110
+ Format the quaternion in a human-readable form as::
1111
+
1112
+ S D1 VX VY VZ D2
1113
+
1114
+ where S, VX, VY, VZ are the quaternion elements, and D1 and D2 are a pair
1115
+ of delimeters given by `delim`.
1116
+
1117
+ .. runblock:: pycon
1118
+
1119
+ >>> from spatialmath.base import q2str, qrand
1120
+ >>> q = [1, 2, 3, 4]
1121
+ >>> q2str(q)
1122
+ >>> q = qrand() # a unit quaternion
1123
+ >>> q2str(q, delim=('<<', '>>'))
1124
+
1125
+ :seealso: :meth:`qprint`
1126
+ """
1127
+ q = smb .getvector (q , 4 )
1128
+ template = "# {} #, #, # {}" .replace ("#" , fmt )
1129
+ return template .format (q [0 ], delim [0 ], q [1 ], q [2 ], q [3 ], delim [1 ])
1130
+
1131
+
1091
1132
def qprint (
1092
1133
q : Union [ArrayLike4 , ArrayLike4 ],
1093
1134
delim : Optional [Tuple [str , str ]] = ("<" , ">" ),
1094
1135
fmt : Optional [str ] = "{: .4f}" ,
1095
1136
file : Optional [TextIO ] = sys .stdout ,
1096
- ) -> str :
1137
+ ) -> None :
1097
1138
"""
1098
- Format a quaternion
1139
+ Format a quaternion to a file
1099
1140
1100
1141
:arg q: unit-quaternion
1101
1142
:type q: array_like(4)
@@ -1105,8 +1146,6 @@ def qprint(
1105
1146
:type fmt: str
1106
1147
:arg file: destination for formatted string [default sys.stdout]
1107
1148
:type file: file object
1108
- :return: formatted string
1109
- :rtype: str
1110
1149
1111
1150
Format the quaternion in a human-readable form as::
1112
1151
@@ -1117,23 +1156,23 @@ def qprint(
1117
1156
1118
1157
By default the string is written to `sys.stdout`.
1119
1158
1120
- If `file=None` then a string is returned.
1121
-
1122
1159
.. runblock:: pycon
1123
1160
1124
1161
>>> from spatialmath.base import qprint, qrand
1125
1162
>>> q = [1, 2, 3, 4]
1126
1163
>>> qprint(q)
1127
1164
>>> q = qrand() # a unit quaternion
1128
1165
>>> qprint(q, delim=('<<', '>>'))
1166
+
1167
+ :seealso: :meth:`q2str`
1129
1168
"""
1130
1169
q = smb .getvector (q , 4 )
1131
- template = "# {} #, #, # {}" . replace ( "#" , fmt )
1132
- s = template . format ( q [ 0 ], delim [ 0 ], q [ 1 ], q [ 2 ], q [ 3 ], delim [ 1 ])
1133
- if file :
1134
- file . write ( s + " \n " )
1135
- else :
1136
- return s
1170
+ if file is None :
1171
+ warnings . warn (
1172
+ "Usage: qprint(..., file=None) -> str is deprecated, use q2str() instead" ,
1173
+ DeprecationWarning ,
1174
+ )
1175
+ print ( q2str ( q , delim = delim , fmt = fmt ), file = file )
1137
1176
1138
1177
1139
1178
if __name__ == "__main__" : # pragma: no cover
0 commit comments