-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
146 lines (124 loc) · 5.16 KB
/
draw.py
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
"""
@File : draw.py
@Author : Hyunsoo Shin
@Date : 20. 4. 22.
@Contact : [email protected]
"""
import matplotlib.pyplot as plt
import numpy as np
from Arrow3D import *
conv2Rad = lambda x: x * np.pi / 180
def drawVector(fig, pointA, pointB, **kwargs):
ms = kwargs.get('mutation_scale', 20)
ars = kwargs.get('arrowstyle', '-|>')
lc = kwargs.get('lineColor', 'k')
pc = kwargs.get('projColor', 'k')
pointEnable = kwargs.get('pointEnable', True)
projOn = kwargs.get('proj', True)
lineStyle = kwargs.get('lineStyle', '-')
annotationString = kwargs.get('annotationString', '')
if (3 <= pointA.size <= 4):
xs = [pointA[0], pointB[0]]
ys = [pointA[1], pointB[1]]
zs = [pointA[2], pointB[2]]
else:
xs = [pointA[0, 3], pointB[0, 3]]
ys = [pointA[1, 3], pointB[1, 3]]
zs = [pointA[2, 3], pointB[2, 3]]
out = Arrow3D(xs, ys, zs, mutation_scale=ms, arrowstyle=ars, color=lc, linestyle=lineStyle)
fig.add_artist(out)
if pointEnable: fig.scatter(xs[1], ys[1], zs[1], color='k', s=50)
if projOn:
fig.plot(xs, ys, [0, 0], color=pc, linestyle='--')
fig.plot([xs[0], xs[0]], [ys[0], ys[0]], [0, zs[0]], color=pc, linestyle='--')
fig.plot([xs[1], xs[1]], [ys[1], ys[1]], [0, zs[1]], color=pc, linestyle='--')
if annotationString != '':
fig.text(xs[1], ys[1], zs[1], annotationString, size=15, zorder=1, color='k')
def drawPointWithAxis(fig, *args, **kwargs):
ms = kwargs.get('mutation_scale', 20)
ars = kwargs.get('arrowstyle', '->')
pointEnable = kwargs.get('pointEnable', True)
axisEnable = kwargs.get('axisEnable', True)
lineStyle = kwargs.get('lineStyle', '-')
lineWidth= kwargs.get('lineWidth', 1)
vectorLength = kwargs.get('vectorLength', 1)
if len(args) == 4:
ORG = args[0]
hat_X = args[1]
hat_Y = args[2]
hat_Z = args[3]
xs_n = [ORG[0], ORG[0] + hat_X[0] * vectorLength]
ys_n = [ORG[1], ORG[1] + hat_X[1] * vectorLength]
zs_n = [ORG[2], ORG[2] + hat_X[2] * vectorLength]
xs_o = [ORG[0], ORG[0] + hat_Y[0] * vectorLength]
ys_o = [ORG[1], ORG[1] + hat_Y[1] * vectorLength]
zs_o = [ORG[2], ORG[2] + hat_Y[2] * vectorLength]
xs_a = [ORG[0], ORG[0] + hat_Z[0] * vectorLength]
ys_a = [ORG[1], ORG[1] + hat_Z[1] * vectorLength]
zs_a = [ORG[2], ORG[2] + hat_Z[2] * vectorLength]
else:
tmp = args[0]
ORG = tmp[:3, 3:]
hat_X = tmp[:3, 0:1]
hat_Y = tmp[:3, 1:2]
hat_Z = tmp[:3, 2:3]
xs_n = [ORG[0, 0], ORG[0, 0] + hat_X[0, 0] * vectorLength]
ys_n = [ORG[1, 0], ORG[1, 0] + hat_X[1, 0] * vectorLength]
zs_n = [ORG[2, 0], ORG[2, 0] + hat_X[2, 0] * vectorLength]
xs_o = [ORG[0, 0], ORG[0, 0] + hat_Y[0, 0] * vectorLength]
ys_o = [ORG[1, 0], ORG[1, 0] + hat_Y[1, 0] * vectorLength]
zs_o = [ORG[2, 0], ORG[2, 0] + hat_Y[2, 0] * vectorLength]
xs_a = [ORG[0, 0], ORG[0, 0] + hat_Z[0, 0] * vectorLength]
ys_a = [ORG[1, 0], ORG[1, 0] + hat_Z[1, 0] * vectorLength]
zs_a = [ORG[2, 0], ORG[2, 0] + hat_Z[2, 0] * vectorLength]
if pointEnable: fig.scatter(xs_n[0], ys_n[0], zs_n[0], color='k', s=50)
if axisEnable:
n = Arrow3D(xs_n, ys_n, zs_n, mutation_scale=ms, arrowstyle=ars, color='r', linestyle=lineStyle, linewidth=lineWidth)
o = Arrow3D(xs_o, ys_o, zs_o, mutation_scale=ms, arrowstyle=ars, color='g', linestyle=lineStyle, linewidth=lineWidth)
a = Arrow3D(xs_a, ys_a, zs_a, mutation_scale=ms, arrowstyle=ars, color='b', linestyle=lineStyle, linewidth=lineWidth)
fig.add_artist(n)
fig.add_artist(o)
fig.add_artist(a)
def RotX(phi):
return np.array([[1, 0, 0],
[0, np.cos(phi), -np.sin(phi)],
[0, np.sin(phi), np.cos(phi)]])
def RotY(theta):
return np.array([[np.cos(theta), 0, np.sin(theta)],
[0, 1, 0],
[-np.sin(theta), 0, np.cos(theta)]])
def RotZ(psi):
return np.array([[np.cos(psi), -np.sin(psi), 0],
[np.sin(psi), np.cos(psi), 0],
[0, 0, 1]])
if __name__ =="__main__":
print('start')
import kinematics as kin
import cv2
ORG = np.array([0, 0, 0])
a = [1, 1, 1]
rotation_vector = np.array([0.0, 0.0, 1.0])
# 1
rot_matrix = np.zeros((3, 3))
norm_rotation_vector = rotation_vector / np.linalg.norm(rotation_vector)
print('norm_rotation_vector', norm_rotation_vector)
print('norm rvec', rotation_vector)
cv2.Rodrigues(norm_rotation_vector, rot_matrix)
print('rot_matrix from cv2.Rodrigues\n', rot_matrix)
# 2
# rot_matrix = RotZ(np.deg2rad(10))
rotated_a = np.dot(rot_matrix, np.transpose(a))
print('a', a)
print('rotated_a', rotated_a)
b = np.array([0.1, 0.1, 0.1])
norm_b = np.linalg.norm(b)
normal_b = b / norm_b
print('b', b)
print('normal_b', normal_b)
# Draw
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# drawVector(ax, ORG, a)
# drawVector(ax, ORG, rotated_a, lineColor='b')
# drawVector(ax, ORG, norm_rotation_vector, lineColor='r')
# plt.show()