|
2 | 2 | Provide quaternion and unit-quaternion abstractions as classes
|
3 | 3 | """
|
4 | 4 |
|
5 |
| -from collections import UserList |
6 | 5 | import math
|
7 | 6 | import numpy as np
|
8 | 7 | from typing import Any
|
9 | 8 | from spatialmath import base as tr
|
10 | 9 | from spatialmath.base import quaternions as quat
|
11 | 10 | from spatialmath.base import argcheck
|
12 | 11 | from spatialmath import pose3d as p3d
|
| 12 | +from spatialmath.smuserlist import SMUserList |
13 | 13 |
|
14 |
| - |
15 |
| -class Quaternion(UserList): |
| 14 | +class Quaternion(SMUserList): |
16 | 15 | r"""
|
17 | 16 | A quaternion is a compact method of representing a 3D rotation that has
|
18 | 17 | computational advantages including speed and numerical robustness.
|
@@ -98,182 +97,6 @@ def __init__(self, s: Any =None, v=None, check=True):
|
98 | 97 | else:
|
99 | 98 | raise ValueError('bad argument to Quaternion constructor')
|
100 | 99 |
|
101 |
| - @classmethod |
102 |
| - def Empty(cls): |
103 |
| - """ |
104 |
| - Construct an empty quaternion object |
105 |
| - |
106 |
| - :return: a quaternion instance with zero values |
107 |
| - :rtype: Quaternion |
108 |
| -
|
109 |
| - Example:: |
110 |
| - |
111 |
| - >>> q = Quaternion.Empty() |
112 |
| - >>> len(q) |
113 |
| - 0 |
114 |
| - """ |
115 |
| - q = cls() |
116 |
| - q.data = [] |
117 |
| - return q |
118 |
| - |
119 |
| - @property |
120 |
| - def _A(self): |
121 |
| - # get the underlying numpy array |
122 |
| - if len(self.data) == 1: |
123 |
| - return self.data[0] |
124 |
| - else: |
125 |
| - return self.data |
126 |
| - |
127 |
| - # ------------------------------------------------------------------------ # |
128 |
| - |
129 |
| - def __getitem__(self, i): |
130 |
| - """ |
131 |
| - Access value of a quaternion instance |
132 |
| -
|
133 |
| - :param i: index of element to return |
134 |
| - :type i: int |
135 |
| - :return: the specific element of the pose |
136 |
| - :rtype: Quaternion or UnitQuaternion instance |
137 |
| - :raises IndexError: if the element is out of bounds |
138 |
| -
|
139 |
| - Note that only a single index is supported, slices are not. |
140 |
| - |
141 |
| - Example:: |
142 |
| - |
143 |
| - >>> q = UnitQuaternion.Rx([0, 0.3, 0.6]) |
144 |
| - >>> len(q) |
145 |
| - 3 |
146 |
| - >>> q[1] |
147 |
| - 0.988771 << 0.149438, 0.000000, 0.000000 >> |
148 |
| - """ |
149 |
| - |
150 |
| - if isinstance(i, slice): |
151 |
| - return self.__class__([self.data[k] for k in range(i.start or 0, i.stop or len(self), i.step or 1)]) |
152 |
| - else: |
153 |
| - return self.__class__(self.data[i]) |
154 |
| - |
155 |
| - def __setitem__(self, i, value): |
156 |
| - """ |
157 |
| - Assign a value to a quaternion instance |
158 |
| - |
159 |
| - :param i: index of element to assign to |
160 |
| - :type i: int |
161 |
| - :param value: the value to insert |
162 |
| - :type value: Quaternion or UnitQuaternion instance |
163 |
| - :raises ValueError: incorrect type of assigned value |
164 |
| -
|
165 |
| - Assign the argument to an element of the object's internal list of values. |
166 |
| - This supports the assignement operator, for example:: |
167 |
| - |
168 |
| - >>> q = Quaternion([Quaternion() for i in range(10)]) # sequence of ten identity values |
169 |
| - >>> len(q) |
170 |
| - 10 |
171 |
| - >>> q[3] = Quaternion([1,2,3,4]) # assign to position 3 in the list |
172 |
| - """ |
173 |
| - if not type(self) == type(value): |
174 |
| - raise ValueError("can't insert different type of object") |
175 |
| - if len(value) > 1: |
176 |
| - raise ValueError("can't insert a multivalued element - must have len() == 1") |
177 |
| - self.data[i] = value.A |
178 |
| - |
179 |
| - def append(self, q): |
180 |
| - """ |
181 |
| - Append a value to a quaternion instance |
182 |
| - |
183 |
| - :param x: the value to append |
184 |
| - :type x: Quaternion or UnitQuaternion instance |
185 |
| - :raises ValueError: incorrect type of appended object |
186 |
| -
|
187 |
| - Appends the argument to the object's internal list of values. |
188 |
| - |
189 |
| - Examples:: |
190 |
| - |
191 |
| - >>> q = Quaternion() |
192 |
| - >>> len(q) |
193 |
| - 1 |
194 |
| - >>> q.append(Quaternion([1,2,3,4])) |
195 |
| - >>> len(q) |
196 |
| - 2 |
197 |
| - """ |
198 |
| - #print('in append method') |
199 |
| - if not type(self) == type(q): |
200 |
| - raise ValueError("can't append different type of object") |
201 |
| - if len(q) > 1: |
202 |
| - raise ValueError("can't append a multivalued instance - use extend") |
203 |
| - super().append(q.A) |
204 |
| - |
205 |
| - |
206 |
| - def extend(self, q): |
207 |
| - """ |
208 |
| - Extend sequence of values of a quaternion instance |
209 |
| - |
210 |
| - :param x: the value to extend |
211 |
| - :type x: Quaternion or UnitQuaternion instance |
212 |
| - :raises ValueError: incorrect type of appended object |
213 |
| -
|
214 |
| - Appends the argument to the object's internal list of values. |
215 |
| - |
216 |
| - Examples:: |
217 |
| - |
218 |
| - >>> q = UnitQuaternion() |
219 |
| - >>> len(q) |
220 |
| - 1 |
221 |
| - >>> q.extend(UnitQuaternion.Rx([0.1, 0.2])) |
222 |
| - >>> len(3) |
223 |
| - 3 |
224 |
| - """ |
225 |
| - #print('in extend method') |
226 |
| - if not type(self) == type(q): |
227 |
| - raise ValueError("can't append different type of object") |
228 |
| - super().extend(q._A) |
229 |
| - |
230 |
| - def insert(self, i, value): |
231 |
| - """ |
232 |
| - Insert a value to a quaternion instance |
233 |
| -
|
234 |
| - :param i: element to insert value before |
235 |
| - :type i: int |
236 |
| - :param value: the value to insert |
237 |
| - :type value: Quaternion or UnitQuaternion instance |
238 |
| - :raises ValueError: incorrect type of inserted value |
239 |
| -
|
240 |
| - Inserts the argument into the object's internal list of values. |
241 |
| - |
242 |
| - Examples:: |
243 |
| - |
244 |
| - >>> q = UnitQuaternion() |
245 |
| - >>> q.insert(0, UnitQuaternion.Rx(0.1)) # insert at position 0 in the list |
246 |
| - >>> len(q) |
247 |
| - 2 |
248 |
| - """ |
249 |
| - if not type(self) == type(value): |
250 |
| - raise ValueError("can't insert different type of object") |
251 |
| - if len(value) > 1: |
252 |
| - raise ValueError("can't insert a multivalued instance - must have len() == 1") |
253 |
| - super().insert(i, value._A) |
254 |
| - |
255 |
| - def pop(self): |
256 |
| - """ |
257 |
| - Pop value of a quaternion instance |
258 |
| -
|
259 |
| - :return: the first quaternion value |
260 |
| - :rtype: Quaternion or UnitQuaternion instance |
261 |
| - :raises IndexError: if there are no values to pop |
262 |
| -
|
263 |
| - Removes the first quaternion value from the instancet. |
264 |
| - |
265 |
| - Example:: |
266 |
| - |
267 |
| - >>> q = UnitQuaternion.Rx([0, 0.3, 0.6]) |
268 |
| - >>> len(q) |
269 |
| - 3 |
270 |
| - >>> q.pop() |
271 |
| - 1.000000 << 0.000000, 0.000000, 0.000000 >> |
272 |
| - >>> len(q) |
273 |
| - 2 |
274 |
| - """ |
275 |
| - return self.__class__(super().pop()) |
276 |
| - |
277 | 100 | @property
|
278 | 101 | def s(self):
|
279 | 102 | """
|
|
0 commit comments