-
Notifications
You must be signed in to change notification settings - Fork 12
/
sorting.py
317 lines (225 loc) · 9.37 KB
/
sorting.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
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
import numpy as np
import networkx as nx
import abc
from .utils import *
class BaseSort(abc.ABC):
def __init__(self):
pass
def __str__(self):
return 'BaseSorter Feature'
@abc.abstractmethod
def sort(self, vectors: np.ndarray) -> np.ndarray:
"""
Sorts the scan vectors in a particular order
:param vectors: The un-sorted array of scan vectors
:return: The sorted array of scan vectors
"""
raise NotImplementedError('Sort method must be implemented')
class UnidirectionalSort(BaseSort):
"""
Method simply passes the hatch vectors in their current form.
"""
def __init__(self):
super().__init__()
def __str__(self):
return 'Unidrectional Hatch Sort'
def sort(self, scanVectors: np.ndarray) -> np.ndarray:
""" This approach simply flips the odd pair of hatches"""
class FlipSort(BaseSort):
"""
Sort method flips all pairs of scan vectors so that their direction alternates across the input
"""
def __init__(self):
super().__init__()
def __str__(self):
return 'Alternating Hatch Sort'
def sort(self, scanVectors: np.ndarray) -> np.ndarray:
""" This approach simply flips the odd pair of hatches"""
sv = to3DHatchArray(scanVectors)
sv = np.flip(sv, 1)
return from3DHatchArray(sv)
class AlternateSort(BaseSort):
"""
Sort method flips pairs of scan vectors so that their direction alternates across adjacent vectors.
"""
def __init__(self):
super().__init__()
def __str__(self):
return 'Alternating Hatch Sort'
def sort(self, scanVectors: np.ndarray) -> np.ndarray:
""" This approach simply flips the odd pair of hatches"""
sv = to3DHatchArray(scanVectors)
sv[1:-1:2] = np.flip(sv[1:-1:2], 1)
#vectorCopy = scanVectors.copy()
# return vectorCopy
return from3DHatchArray(sv)
class LinearSort(BaseSort):
"""
A linear sort approaches to sorting the scan vectors based on the current hatch angle specified in
:attr:`pyslm.hatching.sorting.LinearSort.hatchAngle`. The approach takes the dot product of the hatch mid-point
and the projection along the X-axis is sorted in ascending order (+ve X direction).
"""
def __init__(self):
super().__init__()
self._hatchAngle = 0.0
@property
def hatchAngle(self) -> float:
"""
The hatch angle reference across the scan vectors to be sorted
"""
return self._hatchAngle
@hatchAngle.setter
def hatchAngle(self, angle: float):
self._hatchAngle = angle
def sort(self, scanVectors: np.ndarray) -> np.ndarray:
# requires an n x 2 x 2 array
# Sort along the x-axis and obtain the indices of the sorted array
theta_h = np.deg2rad(self._hatchAngle)
# Find the unit vector normal based on the hatch angle
norm = np.array([np.cos(theta_h), np.sin(theta_h)])
midPoints = np.mean(scanVectors, axis=1)
idx2 = norm.dot(midPoints.T)
idx3 = np.argsort(idx2)
sortIdx = np.arange(len(midPoints))[idx3]
return scanVectors[sortIdx]
class GreedySort(BaseSort):
"""
The greedy sort approach is a heuristic approach to sorting the scan vectors based on the current hatch angle
specified in :attr:`pyslm.hatching.sorting.LinearSort.hatchAngle` and clustering vectors together based on the
hatch group distance - :attr:`pyslm.hatching.sorting.LinearSort.hatchTol`.
The approach finds clusters of scan vectors based on their connectivity based on a threshold
"""
def __init__(self, hatchAngle = 0.0, hatchTol = None):
super().__init__()
self._hatchAngle = hatchAngle
self._sortY = False
self._clusterDistance = 5 # mm
if hatchTol:
self._hatchTol = hatchTol
else:
self._hatchTol = 0.1 * 5 # hatchDistance * 5
def __str__(self):
return 'GreedySort Feature'
@property
def hatchAngle(self) -> float:
"""
The hatch angle reference across the scan vectors to be sorted
"""
return self._hatchAngle
@hatchAngle.setter
def hatchAngle(self, angle: float):
self._hatchAngle = angle
@property
def hatchTol(self):
""" The hatch group tolerance specifies the arbitrary distance used for grouping the scan vectors into
'scanning clusters'"""
return self._hatchTol
@hatchTol.setter
def hatchTol(self, tolerance):
self._hatchTol = tolerance
@property
def sortY(self) -> bool:
""" Used to set the sorting mode (default sort along x)"""
return self._sortY
@sortY.setter
def sortY(self, state: bool):
self._sortY = state
def sort(self, scanVectors):
"""
Sorts the scan vectors
"""
from scipy.spatial import distance_matrix
theta_h = np.deg2rad(self._hatchAngle)
# vectors is actually the list of midpoints
midPoints = np.mean(scanVectors, axis=1)
#print('{:=^60} \n'.format(' Finding hatch distance '))
# TODO find a more efficient way of producing distance matrix usign KD-tree
# scipy spatial distancematrix
distMap = distance_matrix(midPoints, midPoints)
distMap += np.eye(distMap.shape[0]) * 1e7
G = nx.from_numpy_matrix(distMap < self._hatchTol)
# print(' Time taken to find hatch distance', time.time() - start)
# print('{:=^60} \n'.format(' Finding hatch distance (finished) '))
# #G = nx.from_scipy_sparse_matrix(sA)
# print('num con components', nx.number_connected_components(G))
graphs = [G.subgraph(c) for c in nx.algorithms.connected_components(G)] #graphs = list(nx.connected_component_subgraphs(G))
clusterPaths = []
for i in range(len(graphs)):
# Locate the mid points
gNodes = np.array([n for n in graphs[i]] )
# graphPnts = midPoints[gNodes]
# sortXidx = np.argsort(graphPnts[:,0], axis=0)
# sortPnts = graphPnts[sortXidx]
#
# distTravelled = np.cumsum(np.diff(sortPnts[:,0]))
#
# bounds = np.argwhere(np.diff(np.divmod(distTravelled,20)[0]))
# branchPnts.append(gNodes[bounds])
#
# Find the unit vector normal
# Sort the cluster of hatches by the hatch direction
norm = np.array([np.cos(theta_h),np.sin(theta_h)])
idx2 = norm.dot(midPoints[gNodes].T)
idx3 = np.argsort(idx2)
# Add the sorted cluster of vectors
shortPath = gNodes[idx3]
clusterPaths.append(shortPath)
# Next part of algorithm greedily collects the scan vectors then moves onto
# the next group cluster after a set distance [clusterDistance]
scanVectorList = []
lastScanIdx = [0] * len(clusterPaths)
dPos = 0
maxMove = dPos
complete = False
grpId = 0
grp = []
# The cluster groups should be sorted by the first index
#sort(clusterPaths)
firstPnts = midPoints[[path[0] for path in clusterPaths]]
if self._sortY:
clusterPaths = [clusterPaths[i] for i in np.argsort(firstPnts[:,1])]
else:
clusterPaths = [clusterPaths[i] for i in np.argsort(firstPnts[:,0])]
advancePos = True
while not complete:
if advancePos:
maxMove += self._clusterDistance
advancePos = True # Reset the flag
for i in range(len(clusterPaths)):
innerDist = 0
clusterNodes = np.array(clusterPaths[i])
if lastScanIdx[i] == (len(clusterNodes)):
continue # Finished scanning the cluster
# Get the next point in the cluster
pnt = midPoints[clusterNodes[lastScanIdx[i]]]
if self._sortY:
if pnt[1] > maxMove:
continue # Don't jump ahead
else:
if pnt[0] > maxMove:
continue # Don't jump ahead
while innerDist < self._clusterDistance:
if lastScanIdx[i] < (len(clusterNodes)):
#print('adding', i, lastScanIdx[i])
scanVectorList.append(clusterNodes[lastScanIdx[i]])
grp.append(grpId)
lastScanIdx[i] += 1
if lastScanIdx[i] == (len(clusterNodes)):
break
pntCur = midPoints[clusterNodes[lastScanIdx[i]]]
pntPrev = midPoints[clusterNodes[lastScanIdx[i]-1]]
delta = pntCur - pntPrev
if self._sortY:
innerDist += delta[1]
dPos = np.max([dPos, pntCur[1]])
else:
innerDist += delta[0]
dPos = np.max([dPos, pntCur[0]])
# because a valid group was found move back to start
grpId += 1
advancePos = False
break
clusterLen = [len(path) for path in clusterPaths]
complete = np.sum(np.array(lastScanIdx) - clusterLen) == 0
idx6 = np.arange(len(midPoints))[scanVectorList]
return scanVectors[idx6]