-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoboticsLib.py
More file actions
1593 lines (1312 loc) · 48.6 KB
/
RoboticsLib.py
File metadata and controls
1593 lines (1312 loc) · 48.6 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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
from scipy import linalg as la
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
##################################################################################
# Robotics Library
# v00.7
#
# (quick & dirty) library for the robotics course
# written by Andreas Birk
#
# do not distribute!!!
#
##################################################################################
##################################################################################
#
# 3D Homogeneous Matrix Class
#
class Hmat:
def __init__(self, H = np.array([[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]])):
self.H = np.copy(H)
self.type = 'Hmat'
def __str__(self):
return '{}'.format(self.H)
##############################################################################
# mathematical operators for homogeneous matrices
#
def __matmul__(self, B): # matrix, repsectively matrix/vector multiplication with '@'
if B.type == 'Hmat':
HA = self.H
HB = B.H
HC = HA @ HB
C = Hmat(HC)
if B.type == 'Hvec':
HA = self.H
Hb = B.v
Hc = HA @ Hb
C = Hvec(Hc)
return C
def __add__(self, B):
HA = self.H
HB = B.H
HC = HA + HB
HC[3][3] = 1
C = Hmat(HC)
return C
def __sub__(self, B):
HA = self.H
HB = B.H
HC = HA - HB
HC[3][3] = 1
C = Hmat(HC)
return C
def __neg__(self):
HA = self.H
HB = -HA
HB[3][3] = 1
B = Hmat(HB)
return B
def __mul__(self, c): # scalar multiplication for convenience only!!! Rot of H will not be orthoNORMAL anymore
HA = self.H
HB = c*HA
HB[3][3] = 1
B = Hmat(HB)
return B
def __rmul__(self, c): # scalar multiplication for convenience only!!! Rot of H will not be orthoNORMAL anymore
HA = self.H
HB = c*HA
HB[3][3] = 1
B = Hmat(HB)
return B
def __eq__(self, B): # equal?
HA = self.H
HB = B.H
b = np.array_equal(HA, HB)
return b
def __ne__(self, B): # nonequal?
HA = self.H
HB = B.H
b = not np.array_equal(HA, HB)
return b
##############################################################################
# compute inverse of a homogeneous matrix via rotation transpose
#
def inv(self):
R = self.get_rot()
HRinv = (R.H).T
Hinv = Hmat(HRinv)
t = self.get_transl()
t = -Hinv @ t
Hinv.set_transl_v(t)
return Hinv
##############################################################################
# compute transpose of a homogeneous matrix
# (NOTE: this is only meaningful for pure homogeneous rotation matrices, i.e., translation is Zero)
def T(self):
HT = Hmat()
HT.H = self.H.T
return HT
##############################################################################
# (re-)set a homogeneous matrix to the Identy matrix
#
def set_I(self):
self.H = np.array([[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]])
##############################################################################
# set translation component of a homogeneous matrix
# (via homogeneous vector v, respectively x,y,z coordinates)
#
def set_transl_v(self, v):
self.H[0,3] = v.v[0,0]
self.H[1,3] = v.v[1,0]
self.H[2,3] = v.v[2,0]
def set_transl_xyz(self, x,y,z):
self.H[0,3] = x
self.H[1,3] = y
self.H[2,3] = z
##############################################################################
# set rotation component of a homogeneous matrix
# via homogeneous rotation matrices for Roll, Pitch, Yaw
#
def set_rot_X(self, alpha):
self.set_I()
self.H[1][1] = np.cos(alpha)
self.H[1][2] = -np.sin(alpha)
self.H[2][1] = np.sin(alpha)
self.H[2][2] = np.cos(alpha)
def set_rot_Y(self, beta):
self.set_I()
self.H[0][0] = np.cos(beta)
self.H[0][2] = np.sin(beta)
self.H[2][0] = -np.sin(beta)
self.H[2][2] = np.cos(beta)
def set_rot_Z(self, gamma):
self.set_I()
self.H[0][0] = np.cos(gamma)
self.H[0][1] = -np.sin(gamma)
self.H[1][0] = np.sin(gamma)
self.H[1][1] = np.cos(gamma)
###################################################
# generate homogeneous matrices for X-,Y-Z-reflection
#
def set_reflect_X(self):
self.set_I()
self.H[0][0] = -1
def set_reflect_Y(self):
self.set_I()
self.H[1][1] = -1
def set_reflect_Z(self):
self.set_I()
self.H[2][2] = -1
##############################################################################
# get rotation part of a homogeneous matrices
# (return as homogeneous matrix)
#
def get_rot(self):
R = Hmat()
for i in range(3):
for j in range(3):
R.H[i,j] = self.H[i,j]
return R
##############################################################################
# get rotation part of a homogeneous matrices
# (return as 3D 3x3 matrix as plain numpy array)
#
def get_rot3D(self):
R = np.array([[1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,0.0,1.0]])
for i in range(3):
for j in range(3):
R[i,j] = self.H[i,j]
return R
##############################################################################
# get x-/y-/z-axis of the rotation part of a homogeneous matrices
# (return as homogeneous vector)
#
def get_rotX(self):
rX = Hvec()
rX.v[0,0] = self.H[0,0]
rX.v[1,0] = self.H[1,0]
rX.v[2,0] = self.H[2,0]
return rX
def get_rotY(self):
rY = Hvec()
rY.v[0,0] = self.H[0,1]
rY.v[1,0] = self.H[1,1]
rY.v[2,0] = self.H[2,1]
return rY
def get_rotZ(self):
rZ = Hvec()
rZ.v[0,0] = self.H[0,2]
rZ.v[1,0] = self.H[1,2]
rZ.v[2,0] = self.H[2,2]
return rZ
##############################################################################
# get translation part of a homogeneous matrices
# (return as homogeneous vector)
#
def get_transl(self):
t = Hvec()
t.v[0,0] = self.H[0,3]
t.v[1,0] = self.H[1,3]
t.v[2,0] = self.H[2,3]
return t
##################################################################################
# child classes of Hmat:
# - HId : homogeneous identity matrix (just for completeness)
# - HRotX/Y/Z : homogeneous Roll, Pitch, Yaw rotation matrices
# - HReflectX/Y/Z : homogeneous X-, Y-, Z-axis reflection matrices
# - HTransl : homogeneous translation matrix, initialized with x,y,z
#
class HId(Hmat):
def __init__(self):
super().__init__(None)
self.set_I()
class HRotX(Hmat):
def __init__(self, a):
super().__init__(None)
self.set_rot_X(a)
class HRotY(Hmat):
def __init__(self, a):
super().__init__(None)
self.set_rot_Y(a)
class HRotZ(Hmat):
def __init__(self, a):
super().__init__(None)
self.set_rot_Z(a)
class HReflectX(Hmat):
def __init__(self):
super().__init__(None)
self.set_reflect_X()
class HReflectY(Hmat):
def __init__(self):
super().__init__(None)
self.set_reflect_X()
class HReflectZ(Hmat):
def __init__(self):
super().__init__(None)
self.set_reflect_X()
class HTransl(Hmat):
def __init__(self, x, y, z):
self.type = 'Hmat'
self.set_I()
self.set_transl_xyz(x,y,z)
##################################################################################
# END : 3D Homogeneous Matrix Class & Sub-Classes
##################################################################################
##################################################################################
#
# 3D Homogeneous Vector Class
#
class Hvec:
def __init__(self, hv=np.array([[0.0,0.0,0.0,1.0]]).T):
self.v = np.copy(hv)
self.v = np.copy(hv)
self.type = 'Hvec'
def __str__(self):
return '{}'.format(self.v)
##############################################################################
# mathematical operators for homogeneous vectors
#
def __add__(self, b):
va = self.v
vb = b.v
vc = va + vb
vc[3][0] = 1
c = Hvec(vc)
return c
def __sub__(self, b):
va = self.v
vb = b.v
vc = va - vb
vc[3][0] = 1
c = Hvec(vc)
return c
def __neg__(self):
va = self.v
vb = -va
vb[3][0] = 1
b = Hvec(vb)
return b
def __mul__(self, c): # (right) scalar multiplication with constant c
va = self.v
vb = c*va
vb[3][0] = 1
b = Hvec(vb)
return b
def __rmul__(self, c): # (left) scalar multiplication with constant c
va = self.v
vb = c*va
vb[3][0] = 1
b = Hvec(vb)
return b
def __eq__(self, b): # equal?
va = self.v
vb = b.v
b = np.array_equal(va, vb)
return b
def __ne__(self, b): # nonequal?
va = self.v
vb = b.v
b = not np.array_equal(va, vb)
return b
##############################################################################
# cross product of two homogeneous vectors
#
def cross(self, v1):
hv0 = self.v
hv1 = v1.v
cx = hv0[1,0]*hv1[2,0] - hv0[2,0]*hv1[1,0]
cy = hv0[2,0]*hv1[0,0] - hv0[0,0]*hv1[2,0]
cz = hv0[0,0]*hv1[1,0] - hv0[1,0]*hv1[0,0]
v2 = Hvec()
v2.set_xyz(cx,cy,cz)
return v2
##############################################################################
# dot product of two homogeneous vectors (excluding the 4th component '1')
#
def dot(self, v1):
v0 = self.v
v1 = v1.v
s = v0[0,0]*v1[0,0]+v0[1,0]*v1[1,0]+v0[2,0]*v1[2,0]
return s
##############################################################################
# set coordinates of a homogeneous vector
#
def set_xyz(self,x,y,z):
self.v[0][0] = x
self.v[1][0] = y
self.v[2][0] = z
###################################################
# generate quaternion from homogeneous point
def hv2q(self):
q = Quat()
q.q[0] = 0
for i in range(3): q.q[i+1] = self.v[i,0]
return q
##############################################################################
# length and normalization of a homogeneous vector
#
def length(self):
l = np.sqrt(self.v[0,0]*self.v[0,0] + self.v[1,0]*self.v[1,0] + self.v[2,0]*self.v[2,0])
return l
def normalize(self):
vn = Hvec()
l = self.length()
if l == 0: return 'ERROR: Zero vector can not be normalized'
for i in range(3): vn.v[i,0] = self.v[i,0]/l
return vn
###################################################
# rotation with Rodrigues' formula
# k : rotation axis
# a : angle (in radians)
#
def rogrigues(self, k, a):
pr = Hvec()
kn = k.normalize()
pr = np.cos(a)*self + np.sin(a)* kn.cross(self) + (1-np.cos(a))*kn.dot(self)*kn
return pr
# END : 3D Homogeneous Vector Class
##################################################################################
##################################################################################
#
# (Rotation) Quaternion Class
#
class Quat:
def __init__(self, q_in=np.array([0.0,0.0,0.0,0.0])):
self.q = np.copy(q_in)
self.type = 'Quat'
def __str__(self):
return '{}'.format(self.q)
##############################################################################
# mathematical operators for quaternions
#
def __add__(self, b):
qa = self.q
qb = b.q
qc = qa + qb
c = Quat(qc)
return c
def __sub__(self, b):
qa = self.q
qb = b.q
qc = qa - qb
c = Quat(qc)
return c
def __matmul__(self, q2): # quaternion product with '@'
qm = Quat()
s1 = self.q[0]
s2 = q2.q[0]
v1 = self.get_v()
v2 = q2.get_v()
qm.q[0] = s1*s2 - np.dot(v1.T,v2)[0,0]
vm = s1*v2 + s2*v1 + v3D_cross(v1,v2)
for i in range(3): qm.q[i+1] = vm[i][0]
return qm
def __eq__(self, b): # equal?
qa = self.q
qb = b.q
b = np.array_equal(qa, qb)
return b
def __ne__(self, b): # nonequal?
qa = self.q
qb = b.q
b = not np.array_equal(qa, qb)
return b
###################################################
# conjugate of quaternion q
def conj(self):
qc = Quat()
qc.q[0] = self.q[0]
for i in range(3): qc.q[i+1] = -self.q[i+1]
return qc
###################################################
# norm of quaternion q
def norm(self):
l = np.sqrt(self.q[0]*self.q[0] + self.q[1]*self.q[1] + self.q[2]*self.q[2] + self.q[3]*self.q[3])
return l
###################################################
# normalize quaternion q
def normalize(self):
l = self.norm()
qn = Quat()
for i in range(4): qn.q[i] = self.q[i]/l
return qn
###################################################
# set the 4 values of a quaternion
def set_q(self, a,b,c,d):
self.q[0] = a
self.q[1] = b
self.q[2] = c
self.q[3] = d
###################################################
# generate quaternion values from (homogeneous) point
def set_point(self, v):
self.q[0] = 0
for i in range(3): self.q[i+1] = v.v[i,0]
###################################################
# generate rotation quaterion from angle and axis
# v : axis as (homogeneous) vector
# a : angle (in radians)
def set_rot_axis_angle(self, v, a):
vn = v.normalize()
self.q = np.array([np.cos(a/2), np.sin(a/2)*vn.v[0,0], np.sin(a/2)*vn.v[1,0], np.sin(a/2)*vn.v[2,0]])
###################################################
# set the scalar part to s and the vector part from a (homogeneous) vector
def set_s_v(self, s, v):
self.q[0] = s
self.q[1] = v.v[0,0]
self.q[2] = v.v[1,0]
self.q[3] = v.v[2,0]
###################################################
# get scalar part of a quaternion
def get_s(self, q):
s = self.q[3]
return s
###################################################
# get vector part of a quaternion as 3D (column) vector
def get_v(q):
v = np.array([[0.0,0.0,0.0]]).T
for i in range(3): v[i,0] = q.q[i+1]
return v
###################################################
# convert quaternion q to homogeneous vector hv
def q2hv(self):
hv = Hvec()
for i in range(3): hv.v[i,0] = self.q[i+1]
return hv
##################################################################################
#
# 3D Vector Functions
# - mainly as tools like 3D cross-product (e.g., in quaternion multiplication)
# - based on plain column vectors as plain np.arary
# - the homogeneous vector / matric classes are the preferred options when using this library
#
def v3D_set(x,y,z):
v = np.array([[x,y,z]]).T
return v
def v3D_cross(v0, v1):
cx = v0[1,0]*v1[2,0] - v0[2,0]*v1[1,0]
cy = v0[2,0]*v1[0,0] - v0[0,0]*v1[2,0]
cz = v0[0,0]*v1[1,0] - v0[1,0]*v1[0,0]
v2 = v3D_set(cx,cy,cz)
return v2
##################################################################################
##################################################################################
#
# 3D DISPLAY CLASS
#
##################################################################################
##################################################################################
#
# 3D Plotting for Homogeneous Matrices and Vectors
#
class Fig:
def __init__(self, min = -10, max = 10):
self.fig = plt.figure()
self.ax = self.fig.add_subplot(projection='3d')
self.amin = min
self.amax = max
self.ax.autoscale(enable=False)
self.ax.set_xlim3d(self.amin, self.amax)
self.ax.set_ylim3d(self.amin, self.amax)
self.ax.set_zlim3d(self.amin, self.amax)
self.ax.scatter3D(0, 0, 0, color='red') # mark origin
self.ax.set_xlabel('X')
self.ax.set_ylabel('Y')
self.ax.set_zlabel('Z')
plt.draw()
##############################################################################
# clear figure and set up axes again
#
def clear(self):
self.ax.cla()
self.ax.set_xlim3d(self.amin, self.amax)
self.ax.set_ylim3d(self.amin, self.amax)
self.ax.set_zlim3d(self.amin, self.amax)
self.ax.scatter3D(0, 0, 0, color='red') # mark origin
self.ax.set_xlabel('X')
self.ax.set_ylabel('Y')
self.ax.set_zlabel('Z')
plt.draw()
##############################################################################
# pause for "wait" seconds to get a nice animation
#
def pause(self, t=0.1):
time = t
plt.pause(time)
##############################################################################
# plot a 3D point hp (in homogeneous coordinates)
#
def plot_point(self, hp, *args, **kwargs):
c = kwargs.get('color', 'black')
label = kwargs.get('label', None)
p = hp.v
self.ax.scatter3D(p[0][0],p[1][0],p[2][0], color=c)
s = (self.amax-self.amin)
self.ax.text(p[0][0],p[1][0],p[2][0]-s/100, label, horizontalalignment='center', verticalalignment='top')
plt.draw()
##############################################################################
# plot a 3D vector hv (in homogeneous coordinates)
#
def plot_vector(self, hv, *args, **kwargs):
c = kwargs.get('color', 'black')
label = kwargs.get('label', None)
v = hv.v
self.ax.quiver(0,0,0,v[0][0],v[1][0],v[2][0], color=c)
s = (self.amax-self.amin)
self.ax.text(v[0][0],v[1][0],v[2][0]-s/100, label, horizontalalignment='center', verticalalignment='top')
plt.draw()
##############################################################################
# plot a 3D arrow from hv0 to hv1 (in homogeneous coordinates)
#
def plot_arrow(self, hv0, hv1, *args, **kwargs):
l = 1.0
l = kwargs.get('length', 1)
c = kwargs.get('color', 'black')
label = kwargs.get('label', None)
v0 = hv0.v
v1 = hv1.v
v2 = v1-v0
self.ax.quiver(v0[0][0],v0[1][0],v0[2][0], v2[0][0],v2[1][0],v2[2][0], length=l, color=c)
s = (self.amax-self.amin)
self.ax.text(v0[0][0],v0[1][0],v0[2][0]-s/100, label, horizontalalignment='center', verticalalignment='top')
plt.draw()
##############################################################################
# plot a 3D line from hv0 to hv1 (in homogeneous coordinates)
#
def plot_line(self, hv0, hv1, *args, **kwargs):
c = kwargs.get('color', 'black')
w = kwargs.get('linewidth', 1)
v0 = hv0.v
v1 = hv1.v
self.ax.plot([v0[0][0],v1[0][0]], [v0[1][0],v1[1][0]], [v0[2][0],v1[2][0]], linewidth=w, color=c)
plt.draw()
##############################################################################
# plot fct for a frame in form of a homogeneous matrix
# H : homogeneous matrix
# (optional) label : name of the frame as string
# (optional) size : scale of the frame
#
def plot_frame(self, H, *args, **kwargs):
s = 1.0
s = kwargs.get('size', s)
l = (self.amax-self.amin)/10
origin = H.get_transl()
self.plot_point(origin, color='black')
label = kwargs.get('label', None)
self.ax.text(H.H[0][3], H.H[1][3], H.H[2][3]-l/10, label, horizontalalignment='center', verticalalignment='top')
vx = H.get_rotX()
self.plot_arrow(origin, origin+vx, length=s, color='red')
vy = H.get_rotY()
self.plot_arrow(origin, origin+vy, length=s, color='green')
vz = H.get_rotZ()
self.plot_arrow(origin, origin+vz, length=s, color='blue')
plt.draw()
##############################################################################
# close the figure
#
def close(self):
plt.close()
# END : 3D Plotting for Homogeneous Matrices and Vectors
##################################################################################
################################################################################################################################
################################################################################################################################
# 2D
################################################################################################################################
################################################################################################################################
##################################################################################
#
# 2D Homogeneous Matrix Class
#
class Hmat2D:
def __init__(self, H = np.array([[1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,0.0,1.0]])):
self.H = np.copy(H)
self.type = 'Hmat'
def __str__(self):
return '{}'.format(self.H)
##############################################################################
# mathematical operators for homogeneous matrices
#
def __matmul__(self, B): # matrix, repsectively matrix/vector multiplication with '@'
if B.type == 'Hmat':
HA = self.H
HB = B.H
HC = HA @ HB
C = Hmat2D(HC)
if B.type == 'Hvec':
HA = self.H
Hb = B.v
Hc = HA @ Hb
C = Hvec2D(Hc)
return C
def __add__(self, B):
HA = self.H
HB = B.H
HC = HA + HB
HC[2][2] = 1
C = Hmat2D(HC)
return C
def __sub__(self, B):
HA = self.H
HB = B.H
HC = HA - HB
HC[2][2] = 1
C = Hmat(HC)
return C
def __neg__(self):
HA = self.H
HB = -HA
HB[2][2] = 1
B = Hmat2D(HB)
return B
def __mul__(self, c): # scalar multiplication for convenience only!!! Rot of H will not be orthoNORMAL anymore
HA = self.H
HB = c*HA
HB[2][2] = 1
B = Hmat2D(HB)
return B
def __rmul__(self, c): # scalar multiplication for convenience only!!! Rot of H will not be orthoNORMAL anymore
HA = self.H
HB = c*HA
HB[2][2] = 1
B = Hmat2D(HB)
return B
def __eq__(self, B): # equal?
HA = self.H
HB = B.H
b = np.array_equal(HA, HB)
return b
def __ne__(self, B): # nonequal?
HA = self.H
HB = B.H
b = not np.array_equal(HA, HB)
return b
##############################################################################
# compute inverse of a homogeneous matrix via rotation transpose
#
def inv(self):
R = self.get_rot()
HRinv = (R.H).T
Hinv = Hmat2D(HRinv)
t = self.get_transl()
t = -Hinv @ t
Hinv.set_transl_v(t)
return Hinv
##############################################################################
# compute transpose of a homogeneous matrix
# (NOTE: this is only meaningful for pure homogeneous rotation matrices, i.e., translation is Zero)
def T(self):
HT = Hmat2D()
HT.H = self.H.T
return HT
##############################################################################
# (re-)set a 2D homogeneous matrix to the 3x3 Identy matrix
#
def set_I(self):
self.H = np.array([[1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,0.0,1.0]])
##############################################################################
# set translation component of a 2D homogeneous matrix
# (via 2D homogeneous vector v, respectively x,y coordinates)
#
def set_transl_v(self, v):
self.H[0,2] = v.v[0,0]
self.H[1,2] = v.v[1,0]
def set_transl_xy(self, x,y):
self.H[0,2] = x
self.H[1,2] = y
##############################################################################
# set rotation component of a 2D homogeneous matrix
# via homogeneous 2D rotation matrix
#
def set_rot(self, alpha):
self.set_I()
self.H[0][0] = np.cos(alpha)
self.H[0][1] = -np.sin(alpha)
self.H[1][0] = np.sin(alpha)
self.H[1][1] = np.cos(alpha)
###################################################
# generate 2D homogeneous matrices for X-,Y-reflection
#
def set_reflect_X(self):
self.set_I()
self.H[0][0] = -1
def set_reflect_Y(self):
self.set_I()
self.H[1][1] = -1
##############################################################################
# get rotation part of a 2D homogeneous matrices
# (return as 2D homogeneous matrix)
#
def get_rot(self):
R = Hmat2D()
for i in range(3):
for j in range(3):
R.H[i,j] = self.H[i,j]
return R
##############################################################################
# get rotation part of a 2D homogeneous matrices
# (return as 2D 2x2 matrix as plain numpy array)
#
def get_rot2D(self):
R = np.array([[1.0,0.0],[0.0,1.0]])
for i in range(2):
for j in range(2):
R[i,j] = self.H[i,j]
return R
##############################################################################
# get x-/y-axis of the rotation part of a 2D homogeneous matrices
# (return as homogeneous vector)
#
def get_rotX(self):
rX = Hvec2D()
rX.v[0,0] = self.H[0,0]
rX.v[1,0] = self.H[1,0]
return rX
def get_rotY(self):
rY = Hvec2D()
rY.v[0,0] = self.H[0,1]
rY.v[1,0] = self.H[1,1]
return rY
##############################################################################
# get translation part of a 2D homogeneous matrices
# (return as homogeneous vector)
#
def get_transl(self):
t = Hvec2D()
t.v[0,0] = self.H[0,2]
t.v[1,0] = self.H[1,2]
return t
##################################################################################
# child classes of Hmat2D:
# - HId2D : 2D homogeneous identity matrix (just for completeness)
# - HRot2D : 2D homogeneous rotation matrix
# - HReflectX/Y2D : 2D homogeneous X-, Y- reflection matrices
# - HTransl2D : 2D homogeneous translation matrix, initialized with x,y
#
class HId2D(Hmat2D):
def __init__(self):
super().__init__(None)
self.set_I()
class HRot2D(Hmat2D):
def __init__(self, a):
super().__init__(None)
self.set_rot(a)
class HReflectX2D(Hmat2D):
def __init__(self):
super().__init__(None)
self.set_reflect_X()
class HReflectY2D(Hmat2D):
def __init__(self):
super().__init__(None)
self.set_reflect_X()
class HTransl2D(Hmat2D):
def __init__(self, x, y):
self.type = 'Hmat'
self.set_I()
self.set_transl_xy(x,y)
##################################################################################
# END : 2D Homogeneous Matrix Class & Sub-Classes
##################################################################################
##################################################################################
#
# 2D Homogeneous Vector Class
#
class Hvec2D:
def __init__(self, hv=np.array([[0.0,0.0,1.0]]).T):
self.v = np.copy(hv)
self.v = np.copy(hv)
self.type = 'Hvec'
def __str__(self):
return '{}'.format(self.v)
##############################################################################
# mathematical operators for homogeneous vectors
#
def __add__(self, b):
va = self.v
vb = b.v
vc = va + vb
vc[2][0] = 1
c = Hvec2D(vc)
return c
def __sub__(self, b):
va = self.v
vb = b.v
vc = va - vb
vc[2][0] = 1
c = Hvec2D(vc)
return c
def __neg__(self):
va = self.v
vb = -va
vb[2][0] = 1
b = Hvec2D(vb)
return b
def __mul__(self, c): # (right) scalar multiplication with constant c
va = self.v
vb = c*va
vb[2][0] = 1
b = Hvec2D(vb)
return b