-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcalculate_convex_hull.py
173 lines (140 loc) · 5.67 KB
/
calculate_convex_hull.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
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
import argparse
import os
import matplotlib.pyplot as plt
import numpy as np
from rdp import rdp
from utils import hull
FINGER_NAMES = ["Thumb", "Index", "Middle", "Ring", "Pinky(Little)"]
BONE_NAMES = ["PIP", "DIP", "TIP"]
def count_num_in_convex_hull(points_, convex_hull_):
'''
:param points_: points in plane, N*2
:param convex_hull_: list of points to construct a convex hull , M*2
:return: numbers of points in convex hull
'''
points = points_.copy()
convex_hull = convex_hull_.copy()
convex_hull = np.append(convex_hull, [convex_hull[0]], axis=0)
v = convex_hull[1:] - convex_hull[:-1] # NUM*2
v = np.tile(v, (points_.shape[0], 1, 1))
w = -np.tile(convex_hull[:-1], (points_.shape[0], 1, 1)) + points[:, np.newaxis, :] # N*NUM*2
# 2d cross product (w1, w2) x (v1, v2) := w1*v2 - w2*v1
cross_product_2d = w[:, :, 0] * v[:, :, 1] - w[:, :, 1] * v[:, :, 0]
tmp = (cross_product_2d < 1e-6).all(axis=-1).sum()
return tmp
def calculate_convex_hull(joint_angles, args):
all_del_hulls = []
for ID in range(15):
print('*' * 40)
ja = joint_angles[:, ID]
ja_list = [tuple(x) for x in ja.tolist()]
# original convex hull
convex = hull.convex(ja_list)
convex = np.array(convex)
print("ori_hull.shape=", convex.shape)
convex_list = convex.tolist()
# convex hull simplified by the Ramer-Douglas-Peucker algorithm, a polygon simplification algorithm
rdp_convex = rdp(convex_list, epsilon=args.epsilon)
rdp_convex = np.array(rdp_convex)
print("rdp_hull.shape=", rdp_convex.shape)
# convex hull further simplified by a greedy algorithm
dep_hull = rdp_convex.copy()
for i in range(rdp_convex.shape[0]):
tmp_index = np.argwhere(dep_hull == rdp_convex[i])[0][0]
tmp_num = count_num_in_convex_hull(ja, np.delete(dep_hull, tmp_index, axis=0))
tmp_ratio = tmp_num / ja.shape[0]
print("tmp_ratio=", tmp_ratio)
if tmp_ratio > args.ratio:
dep_hull = np.delete(dep_hull, tmp_index, axis=0)
elif args.ratio - args.delta <= tmp_ratio <= args.ratio:
dep_hull = np.delete(dep_hull, tmp_index, axis=0)
break
else:
break
print("dep_hull.shape=", dep_hull.shape)
all_del_hulls.append(dep_hull)
if args.visualize:
fig = plt.figure()
figManager = plt.get_current_fig_manager()
figManager.window.showMaximized()
ax = fig.add_subplot(111)
ax.scatter(ja[:, 0], ja[:, 1], s=0.05, c='r')
ax.set_xlabel("flexion")
ax.set_ylabel("abduction")
ax.set_title('ID:{} Finger:{} Bone:{}'.format(ID, FINGER_NAMES[ID % 5], BONE_NAMES[int(ID / 5)]))
plt.plot(np.append(convex[:, 0], convex[0, 0]), np.append(convex[:, 1], convex[0, 1]), 'ro--', linewidth=3,
label='ori_convex_hull')
plt.plot(np.append(rdp_convex[:, 0], rdp_convex[0, 0]), np.append(rdp_convex[:, 1], rdp_convex[0, 1]),
'gv--',
linewidth=2, label='rdp_convex_hull')
plt.plot(np.append(dep_hull[:, 0], dep_hull[0, 0]),
np.append(dep_hull[:, 1], dep_hull[0, 1]),
'-b*', linewidth=1, label='del_convex_hull')
ja_min = np.min(ja, axis=0)
ja_max = np.max(ja, axis=0)
plt.gca().add_patch(
plt.Rectangle((ja_min[0], ja_min[1]), ja_max[0] - ja_min[0], ja_max[1] - ja_min[1], edgecolor="yellow",
fill=False, linewidth=2, label='rectangle'))
plt.xticks(np.arange(-3, 4, 1))
plt.yticks(np.arange(-2, 2, 0.5))
plt.legend(title='Convex hull category:')
plt.show()
# check if hull_test is in counter-clockwise order
# hull_test = convex
# for END in range(
# hull_test.shape[0]):
# fig = plt.figure()
# figManager = plt.get_current_fig_manager()
# figManager.window.showMaximized()
# ax = fig.add_subplot(111)
# ax.scatter(ja[:, 0], ja[:, 1], s=0.1, c='r')
# ax.set_xlabel("flexion")
# ax.set_ylabel("abduction")
#
# plt.plot(hull_test[:END, 0], hull_test[:END, 1], 'y--', linewidth=1)
# plt.xticks(np.arange(-3, 4, 1))
# plt.yticks(np.arange(-2, 2, 0.5))
# plt.show()
all_del_hulls = np.array(all_del_hulls)
np.save("BMC/CONVEX_HULLS.npy", all_del_hulls)
print("all_del_hulls.shape=", all_del_hulls.shape)
def main(args):
joint_angles = np.load(os.path.join(args.path, "joint_angles.npy"))
print("joint_angles.shape=", joint_angles.shape)
calculate_convex_hull(joint_angles, args)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='calculate convex hull for joint angles ')
parser.add_argument(
'-p',
'--path',
default='BMC',
type=str,
metavar='data_root',
help='directory')
parser.add_argument(
'-vis',
'--visualize',
action='store_true',
help='visualize reconstruction result',
default=True
)
parser.add_argument(
'--epsilon',
type=float,
default=5e-4,
help='epsilon0.'
)
parser.add_argument(
'--ratio',
type=float,
default=0.9995,
help='ration0.'
)
parser.add_argument(
'--delta',
type=float,
default=0.0005,
help='ration_delta.'
)
main(parser.parse_args())