forked from rai-opensource/spatialmath-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspatialvector.py
More file actions
781 lines (597 loc) · 25.9 KB
/
spatialvector.py
File metadata and controls
781 lines (597 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
"""
A set of cooperating classes to support Featherstone's spatial vector formalism
References:
- "Robot Dynamics Algorithms", R. Featherstone, volume 22,
Springer International Series in Engineering and Computer Science,
Springer, 1987.
- "A beginner's guide to 6-d vectors (part 1)", R. Featherstone,
IEEE Robotics Automation Magazine, 17(3):83-94, Sep. 2010.
- `Online notes <http://users.cecs.anu.edu.au/~roy/spatial>`_
.. inheritance-diagram:: spatialmath.spatialvector
:top-classes: collections.UserList
:parts: 1
"""
from abc import ABC, abstractmethod
import spatialmath.base.argcheck as arg
import spatialmath.base as tr
import numpy as np
from spatialmath.smuserlist import SMUserList
class SpatialVector(SMUserList):
"""
Spatial 6-vector abstract superclass
This class has two abstract subclasses, which each have concrete subclasses.
.. inheritance-diagram:: spatialmath.spatialvector.SpatialVelocity spatialmath.spatialvector.SpatialAcceleration spatialmath.spatialvector.SpatialForce spatialmath.spatialvector.SpatialMomentum
:top-classes: spatialmath.spatialvector.SpatialVector
:parts: 1
Methods:
SpatialV6 constructor invoked by subclasses
double convert to a 6xN double
char convert to string
display display in human readable form
Common operators:
+ add spatial vectors of the same type
- subtract spatial vectors of the same type
- unary minus of spatial vectors
Spatial vectors are a ``UserList`` subclass and can be placed into arrays and indexed.
:seealso: :func:`SpatialM6`, :func:`SpatialF6`, :func:`SpatialVelocity`, :func:`SpatialAcceleration`, :func:`SpatialForce`, :func:`SpatialMomentum`.
"""
def __init__(self, value):
"""
Create a new spatial vector (abstract superclass)
:param value: Value of the
- ``SpatialVector(vec)`` is a spatial vector constructed from the 6-element array-like ``vec``
- ``SpatialVector([V1, V2, ... VN])`` is a spatial vector array with N elements, constructed from the 6-element
array-like values ``Vi``
- ``SpatialVector(A)`` is a spatial vector array with N elements, constructed from the columns of the 6xN
array ``A``.
:seealso: :func:`SpatialVelocity`, :func:`SpatialAcceleration`, :func:`SpatialForce`, :func:`SpatialMomentum`.
"""
print('spatialVec6 init')
super().__init__()
if value is None:
self.data = [np.zeros((6,))]
elif arg.isvector(value, 6):
self.data = [np.array(value)]
elif isinstance(value, list):
assert all(map(lambda x: arg.isvector(x, 6), value)), 'all elements of list must have valid shape and value for the class'
self.data = [np.array(x) for x in value]
elif arg.ismatrix(value, (6, None)):
self.data = [x for x in value.T]
else:
raise ValueError('bad arguments to constructor')
@staticmethod
def _identity():
return np.zeros((6,))
def isvalid(self, x, check):
return True
def shape(self):
return (6,)
# ------------------------------------------------------------------------ #
@property
def V(self):
"""
Spatial vector as an array
:return: Moment vector
:rtype: numpy.ndarray, shape=(3,)
- ``X.v`` is a 3-vector
"""
return self.data[0]
def __repr__(self):
"""
:return:
SpatialVec6.display Display parameters
V.display() displays the spatial vector parameters in compact single line format.
If V is an array of spatial vector objects it displays one per line.
Notes:
- This method is invoked implicitly at the command line when the result
of an expression is a serial vector subclass object and the command has
no trailing semicolon.
"""
return self.__str__()
def __str__(self):
"""
Pretty string representation of spatial vector
:return: readable representation of the spatial vector
:rtype: str
- ``s = str(v)`` is a string showing spatial vector parameters in a
compact single line format.
If V is an array of spatial vector objects return a string with one
line per element.
"""
typ = type(self).__name__
return '\n'.join(["{:s}[{:.5g} {:.5g} {:.5g}; {:.5g} {:.5g} {:.5g}]".format(typ, *list(x)) for x in self.data])
def __neg__(self):
"""
Unary minus for spatial vector
``-V`` is a spatial vector of the same type as ``V`` whose value is
the negative of ``V``. If V is an array V (1xN) then the result
is an array (1xN).
See also SpatialVec6.minus, SpatialVec6.plus.
"""
# for i=1:numel(obj)
# y(i) = obj.new(-obj(i).vw);
return self.__class__([-x for x in self.data])
def __add__(left, right): # pylint: disable=no-self-argument
"""
Addition for spatial vectors
V1 + V2 is a spatial vector of the same type as V1 and V2 whose value is
the sum of V1 and V2. If both are arrays of spatial vectors V1 (1xN) and
V2 (1xN) the result is an array (1xN).
See also SpatialVec6.minus.
:param right:
:return:
"""
assert type(left) == type(right), 'can only add spatial vectors of same type'
assert len(left) == len(right), 'can only add equal length arrays of spatial vectors'
return left.__class__([x + y for x, y in zip(left.data, right.data)])
def __sub__(left, right): # pylint: disable=no-self-argument
"""
Subtraction Addition for spatial vectors
:param right:
:return:
V1 - V2 is a spatial vector of the same type as V1 and V2 whose value is
the difference of V1 and V2. If both are arrays of spatial vectors V1 (1xN) and
V2 (1xN) the result is an array (1xN).
See also SpatialVec6.__minus__, SpatialVec6.__add__
"""
assert type(left) == type(right), 'can only subtract spatial vectors of same type'
assert len(left) == len(right), 'can only subtract equal length arrays of spatial vectors'
return left.__class__([x - y for x, y in zip(left.data, right.data)])
class SpatialM6(SpatialVector):
"""
Create a new spatial motion class (abstract class)
Abstract superclass that represents spatial motion. This class has two
concrete subclasses:
Methods::
SpatialM6 ^constructor invoked by subclasses
char ^convert to string
cross cross product
display ^display in human readable form
double ^convert to a 6xN double
Operators::
+ ^add spatial vectors of the same type
- ^subtract spatial vectors of the same type
- ^unary minus of spatial vectors
Notes:
- ^ is inherited from SpatialVec6.
- Subclass of the MATLAB handle class which means that pass by reference semantics
apply.
- Spatial vectors can be placed into arrays and indexed.
See also SpatialForce, SpatialMomentum, SpatialInertia, SpatialM6.
"""
@abstractmethod
def __init__(self, value):
"""
Create a new spatial motion vector (abstract class)
:param value:
SpatiaVecXXX(V) is a spatial vector of type SpatiaVecXXX with a value
from V (6x1). If V (6xN) then an (Nx1) array of spatial vectors is
returned.
See also SpatialVelocity, SpatialAcceleration, SpatialForce, SpatialMomentum.
"""
super().__init__(value)
def cross(self, other):
"""
:param right:
:return:
SpatialM6.cross Spatial velocity cross product
- ``cross(V1, V2)`` is a SpatialAcceleration object where V1 and V2 are SpatialM6
subclass instances.
cross(V, F) is a SpatialForce object where V1 is a SpatialM6
subclass instances and F is a SpatialForce subclass instance.
Notes:
- The first form is Featherstone's "x" operator.
- The second form is Featherstone's "x*" operator.
"""
pass
# v = obj.vw;
# # vcross = [ skew(w) skew(v); zeros(3,3) skew(w) ]
v = self.V
vcross = np.array([
[0, -v[5], v[5], 0, -v[2], v[1]],
[v[5], 0, -v[3], v[2], 0, -v[0]],
[-v[4], v[3], 0, -v[1], v[0], 0],
[0, 0, 0, 0, -v[5], v[4]],
[0, 0, 0, v[5], 0, -v[3]],
[0, 0, 0, -v[4], v[3], 0]
])
if isinstance(other, SpatialVelocity):
return SpatialAcceleration(vcross * other.V) # * operator
elif isinstance(other, SpatialF6):
return SpatialAcceleration(-vcross * other.V) # x* operator
else:
raise TypeError('type mismatch')
def __mul(left, right): # pylint: disable=no-self-argument
return left.cross(right)
def __rmul(right, left): # pylint: disable=no-self-argument
if isinstance(left, SpatialInertia):
# result is SpatialMomentum
pass # TODO
elif isinstance(left, Twist3):
# result is transformed SpatialVelocity or SpatialAcceleration
# Twist * SpatialVelocity -> SpatialVelocity
# Twist * SpatialAcceleration -> SpatialAcceleration
return right.__class__(left.Ad.T @ right.V)
else:
raise ValueError('SpatialM6 with unknown premultiplication type')
class SpatialF6(SpatialVector):
"""
Abstract spatial force class
Abstract superclass that represents spatial force. This class has two
concrete subclasses:
Operators:
+ ^add spatial vectors of the same type
- ^subtract spatial vectors of the same type
- ^unary minus of spatial vectors
Notes:
- ^ is inherited from SpatialVec6.
- Spatial vectors can be placed into arrays and indexed.
See also SpatialForce, SpatialMomentum, SpatialInertia, SpatialM6.
"""
@abstractmethod
def __init__(self, value):
super().__init__(value)
class SpatialVelocity(SpatialM6):
"""
Spatial velocity class
Concrete subclass of SpatialM6 that represents the
translational and rotational velocity of a rigid-body moving in 3D space.
Operators:
+ ^add spatial vectors of the same type
- ^subtract spatial vectors of the same type
- ^unary minus of spatial vectors
* ^^^premultiplication by SpatialInertia yields SpatialMomentum
* ^^^^premultiplication by Twist yields transformed SpatialVelocity
Notes:
- ^ is inherited from SpatialVec6.
- ^^ is inherited from SpatialM6.
- ^^^ are implemented in SpatialInertia.
- ^^^^ are implemented in Twist.
See also SpatialVec6, SpatialM6, SpatialAcceleration, SpatialInertia, SpatialMomentum.
.. inheritance-diagram:: spatialmath.spatialvector.SpatialVelocity
:top-classes: collections.UserList
:parts: 1
"""
def __init__(self, value=None):
super().__init__(value)
class SpatialAcceleration(SpatialM6):
"""
Spatial acceleration class
Concrete subclass of SpatialM6 that represents the
translational and rotational acceleration of a rigid-body moving in 3D space.
Methods:
SpatialAcceleration ^constructor invoked by subclasses
char ^convert to string
cross ^^cross product
display ^display in human readable form
double ^convert to a 6xN double
new construct new concrete class of same type
Operators::
+ ^add spatial vectors of the same type
- ^subtract spatial vectors of the same type
- ^unary minus of spatial vectors
* ^^^premultiplication by SpatialInertia yields SpatialForce
* ^^^^premultiplication by Twist yields transformed SpatialAcceleration
Notes:
- ^ is inherited from SpatialVec6.
- ^^ is inherited from SpatialM6.
- ^^^ are implemented in SpatialInertia.
- ^^^^ are implemented in Twist.
.. inheritance-diagram:: spatialmath.spatialvector.SpatialAcceleration
:top-classes: collections.UserList
:parts: 1
"""
def __init__(self, value=None):
super().__init__(value)
class SpatialForce(SpatialF6):
"""
Spatial force class
Concrete subclass of SpatialF6 and represents the
translational and rotational forces and torques acting on a rigid-body in 3D space.
Operators::
+ ^add spatial vectors of the same type
- ^subtract spatial vectors of the same type
- ^unary minus of spatial vectors
* ^^^premultiplication by SE3 yields transformed SpatialForce
* ^^^^premultiplication by Twist yields transformed SpatialForce
Notes:
- ^ is inherited from SpatialVec6.
- ^^ is inherited from SpatialM6.
- ^^^ are implemented in RTBPose.
- ^^^^ are implemented in Twist.
See also SpatialVec6, SpatialF6, SpatialMomentum.
"""
def __init__(self, value=None):
super().__init__(value)
# n = SpatialForce(val);
def __rmul(right, left): # pylint: disable=no-self-argument
# Twist * SpatialForce -> SpatialForce
return SpatialForce(left.Ad.T @ right.V)
class SpatialMomentum(SpatialF6):
"""
Spatial momentum class
Operators::
+ ^add spatial vectors of the same type
- ^subtract spatial vectors of the same type
- ^unary minus of spatial vectors
Notes:
- ^ is inherited from SpatialVec6.
- ^^ is inherited from SpatialM6.
See also SpatialVec6, SpatialF6, SpatialForce.
"""
def __init__(self, value=None):
super().__init__(value)
class SpatialInertia(SMUserList):
"""
Spatial inertia class
Concrete class representing spatial inertia.
Methods:
SpatialInertia constructor
char convert to string
display display in human readable form
double convert to a 6xN double
Operators:
+ plus: add spatial inertia of connected bodies
* mtimes: compute force or momentum
Notes:
- Subclass of the MATLAB handle class which means that pass by reference semantics
apply.
- Spatial inertias can be placed into arrays and indexed.
See also SpatialM6, SpatialF6, SpatialVelocity, SpatialAcceleration, SpatialForce,
SpatialMomentum.
"""
def __init__(self, m=None, c=None, I=None):
"""
Create a new spatial inertia
:param m: mass
:type m: float
:param c: centre of mass relative to link frame
:type c: 3-element array_like
:param I: inertia about the centre of mass, axes aligned with link frame
:type I: numpy.array, shape=(6,6)
- ``SpatialInertia(M, C, I)`` is a spatial inertia object for a rigid-body
with mass ``M``, centre of mass at ``C`` relative to the link frame, and an
inertia matrix ``I`` (3x3) about the centre of mass.
- ``SpatialInertia(I)`` is a spatial inertia object with a value equal
to ``I`` (6x6).
"""
if m is not None and c is not None:
assert arg.isvector(c, 3), 'c must be 3-vector'
if I is None:
I = np.zeros((3,3))
else:
assert arg.ismatrix(I, (3,3)), 'I must be 3x3 matrix'
C = tr.skew(c)
self.I = np.array([
[m * np.eye(3), m @ C.T],
[m @ C, I + m * C @ C.T]
])
elif m is None and c is None and I is not None:
assert arg.ismatrix(I, (6, 6)), 'I must be 6x6 matrix'
def __repr__(self):
"""
Convert to string
s = SI.char() is a string showing spatial inertia parameters in a
compact format.
If SI is an array of spatial inertia objects return a string with the
inertia values in a vertical list.
See also SpatialInertia.display.
"""
return self.__str__()
def __str__(self):
return str(self.I)
def __add__(left, right): # pylint: disable=no-self-argument
"""
Spatial inertia addition
:param left:
:param right:
:return:
- ``SI1 + SI2`` is the SpatialInertia of a composite body when bodies with
SpatialInertia ``SI1`` and ``SI2`` are connected.
"""
assert type(left) == type(right), 'spatial inertia can only be added to spatial inertia'
return SpatialInertia(a.I + b.I)
def __mul__(self, right): # pylint: disable=no-self-argument
"""
Spatial inertia product
:param left:
:param right:
:return:
- ``SI * A`` is the SpatialForce required for a body with SpatialInertia ``SI`` to accelerate with
the SpatialAcceleration ``A``.
- ``SI * V`` is the SpatialMomemtum of a body with SpatialInertia ``SI`` and SpatialVelocity ``V``.
"""
left = self
if isinstance(right, SpatialAcceleration):
v = SpatialForce(left.I @ right.V) # F = ma
elif isinstance(right, SpatialVelocity):
# crf(v(i).vw)*model.I(i).I*v(i).vw;
# v = Wrench( a.cross() * I.I * a.vw );
v = SpatialMomentum(left.I * right.V) # M = mv
else:
raise TypeError('bad postmultiply operands for Inertia *')
def __rmul__(self, left): # pylint: disable=no-self-argument
"""
Spatial inertia product
:param left:
:param right:
:return:
- ``A * SI`` is the SpatialForce required for a body with SpatialInertia ``SI`` to accelerate with
the SpatialAcceleration ``A``.
- ``V * SI `` is the SpatialMomemtum of a body with SpatialInertia ``SI`` and SpatialVelocity ``V``.
"""
return self.__mul__(left)
if __name__ == "__main__":
import numpy.testing as nt
import matplotlib.pyplot as plt
import unittest
class TestSpatialVector(unittest.TestCase):
def test_list_powers(self):
x = SpatialVelocity.Empty()
self.assertEqual(len(x), 0)
x.append(SpatialVelocity([1, 2, 3, 4, 5, 6]))
self.assertEqual(len(x), 1)
x.append(SpatialVelocity([7, 8, 9, 10, 11, 12]))
self.assertEqual(len(x), 2)
y = x[0]
self.assertIsInstance(y, SpatialVelocity)
self.assertEqual(len(y), 1)
self.assertTrue(all(y.V == np.r_[1, 2, 3, 4, 5, 6]))
y = x[1]
self.assertIsInstance(y, SpatialVelocity)
self.assertEqual(len(y), 1)
self.assertTrue(all(y.V == np.r_[7, 8, 9, 10, 11, 12]))
x.insert(0, SpatialVelocity([20, 21, 22, 23, 24, 25]))
y = x[0]
self.assertIsInstance(y, SpatialVelocity)
self.assertEqual(len(y), 1)
self.assertTrue(all(y.V == np.r_[20, 21, 22, 23, 24, 25]))
y = x[1]
self.assertIsInstance(y, SpatialVelocity)
self.assertEqual(len(y), 1)
self.assertTrue(all(y.V == np.r_[1, 2, 3, 4, 5, 6]))
def test_velocity(self):
a = SpatialVelocity([1, 2, 3, 4, 5, 6])
self.assertIsInstance(a, SpatialVelocity)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialM6)
self.assertEqual(len(a), 1)
self.assertTrue(all(a.V == np.r_[1, 2, 3, 4, 5, 6]))
a = SpatialVelocity(np.r_[1, 2, 3, 4, 5, 6])
self.assertIsInstance(a, SpatialVelocity)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialM6)
self.assertEqual(len(a), 1)
self.assertTrue(all(a.V == np.r_[1, 2, 3, 4, 5, 6]))
s = str(a)
self.assertIsInstance(s, str)
self.assertEqual(s.count('\n'), 0)
self.assertTrue(s.startswith('SpatialVelocity'))
r = np.random.rand(6, 10)
a = SpatialVelocity(r)
self.assertIsInstance(a, SpatialVelocity)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialM6)
self.assertEqual(len(a), 10)
b = a[3]
self.assertIsInstance(b, SpatialVelocity)
self.assertIsInstance(b, SpatialVector)
self.assertIsInstance(b, SpatialM6)
self.assertEqual(len(b), 1)
self.assertTrue(all(b.V == r[:,3]))
s = str(a)
self.assertIsInstance(s, str)
self.assertEqual(s.count('\n'), 9)
def test_acceleration(self):
a = SpatialAcceleration([1, 2, 3, 4, 5, 6])
self.assertIsInstance(a, SpatialAcceleration)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialM6)
self.assertEqual(len(a), 1)
self.assertTrue(all(a.V == np.r_[1, 2, 3, 4, 5, 6]))
a = SpatialAcceleration(np.r_[1, 2, 3, 4, 5, 6])
self.assertIsInstance(a, SpatialAcceleration)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialM6)
self.assertEqual(len(a), 1)
self.assertTrue(all(a.V == np.r_[1, 2, 3, 4, 5, 6]))
s = str(a)
self.assertIsInstance(s, str)
self.assertEqual(s.count('\n'), 0)
self.assertTrue(s.startswith('SpatialAcceleration'))
r = np.random.rand(6, 10)
a = SpatialAcceleration(r)
self.assertIsInstance(a, SpatialAcceleration)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialM6)
self.assertEqual(len(a), 10)
b = a[3]
self.assertIsInstance(b, SpatialAcceleration)
self.assertIsInstance(b, SpatialVector)
self.assertIsInstance(b, SpatialM6)
self.assertEqual(len(b), 1)
self.assertTrue(all(b.V == r[:,3]))
s = str(a)
self.assertIsInstance(s, str)
def test_force(self):
a = SpatialForce([1, 2, 3, 4, 5, 6])
self.assertIsInstance(a, SpatialForce)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialF6)
self.assertEqual(len(a), 1)
self.assertTrue(all(a.V == np.r_[1, 2, 3, 4, 5, 6]))
a = SpatialForce(np.r_[1, 2, 3, 4, 5, 6])
self.assertIsInstance(a, SpatialForce)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialF6)
self.assertEqual(len(a), 1)
self.assertTrue(all(a.V == np.r_[1, 2, 3, 4, 5, 6]))
s = str(a)
self.assertIsInstance(s, str)
self.assertEqual(s.count('\n'), 0)
self.assertTrue(s.startswith('SpatialForce'))
r = np.random.rand(6, 10)
a = SpatialForce(r)
self.assertIsInstance(a, SpatialForce)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialF6)
self.assertEqual(len(a), 10)
b = a[3]
self.assertIsInstance(b, SpatialForce)
self.assertIsInstance(b, SpatialVector)
self.assertIsInstance(b, SpatialF6)
self.assertEqual(len(b), 1)
self.assertTrue(all(b.V == r[:, 3]))
s = str(a)
self.assertIsInstance(s, str)
def test_momentum(self):
a = SpatialMomentum([1, 2, 3, 4, 5, 6])
self.assertIsInstance(a, SpatialMomentum)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialF6)
self.assertEqual(len(a), 1)
self.assertTrue(all(a.V == np.r_[1, 2, 3, 4, 5, 6]))
a = SpatialMomentum(np.r_[1, 2, 3, 4, 5, 6])
self.assertIsInstance(a, SpatialMomentum)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialF6)
self.assertEqual(len(a), 1)
self.assertTrue(all(a.V == np.r_[1, 2, 3, 4, 5, 6]))
s = str(a)
self.assertIsInstance(s, str)
self.assertEqual(s.count('\n'), 0)
self.assertTrue(s.startswith('SpatialMomentum'))
r = np.random.rand(6, 10)
a = SpatialMomentum(r)
self.assertIsInstance(a, SpatialMomentum)
self.assertIsInstance(a, SpatialVector)
self.assertIsInstance(a, SpatialF6)
self.assertEqual(len(a), 10)
b = a[3]
self.assertIsInstance(b, SpatialMomentum)
self.assertIsInstance(b, SpatialVector)
self.assertIsInstance(b, SpatialF6)
self.assertEqual(len(b), 1)
self.assertTrue(all(b.V == r[:, 3]))
s = str(a)
self.assertIsInstance(s, str)
def test_arith(self):
# just test SpatialVelocity since all types derive from same superclass
r1 = np.r_[1, 2, 3, 4, 5, 6]
r2 = np.r_[7, 8, 9, 10, 11, 12]
a1 = SpatialVelocity(r1)
a2 = SpatialVelocity(r2)
self.assertTrue(all((a1 + a2).V == r1 + r2))
self.assertTrue(all((a1 - a2).V == r1 - r2))
self.assertTrue(all((-a1).V == -r1))
def test_inertia(self):
# constructor
# addition
pass
def test_products(self):
# v x v = a *, v x F6 = a
# a x I, I x a
# v x I, I x v
# twist x v, twist x a, twist x F
pass
unittest.main(buffer=True)