1010import math
1111import random
1212import numpy as np
13+ import itertools
1314
1415show_animation = True
1516
@@ -39,7 +40,7 @@ def plot(self):
3940
4041 # convert global coordinate
4142 gx , gy = self .calc_global_contour ()
42- plt .plot (gx , gy , "-xr " )
43+ plt .plot (gx , gy , "-r " )
4344
4445 def calc_global_contour (self ):
4546 gx = [(ix * math .cos (self .yaw ) + iy * math .sin (self .yaw ))
@@ -133,34 +134,29 @@ def ray_casting_filter(xl, yl, thetal, rangel, angle_reso):
133134
134135def adoptive_range_segmentation (ox , oy ):
135136
136- S = []
137-
138- checked = [False ] * len (ox )
139- R = 5.0
137+ alpha = 0.2
140138
139+ # Setup initial cluster
140+ S = []
141141 for i , _ in enumerate (ox ):
142- if checked [i ]:
143- continue
144- C = []
145- r = R
142+ C = set ()
143+ R = alpha * np .linalg .norm ([ox [i ], oy [i ]])
146144 for j , _ in enumerate (ox ):
147145 d = math .sqrt ((ox [i ] - ox [j ])** 2 + (oy [i ] - oy [j ])** 2 )
148- if d <= r :
149- C .append (j )
150- checked [j ] = True
146+ if d <= R :
147+ C .add (j )
151148 S .append (C )
152149
153- # Merge claster
154- fS = []
155- for k , _ in enumerate (S ):
156- for l , _ in enumerate (S ):
157- if k == l :
158- continue
159-
160- for k , _ in enumerate (S [k ]):
161-
162- print (S )
163- input ()
150+ # Merge cluster
151+ while 1 :
152+ no_change = True
153+ for (c1 , c2 ) in list (itertools .permutations (range (len (S )), 2 )):
154+ if S [c1 ] & S [c2 ]:
155+ S [c1 ] = (S [c1 ] | S .pop (c2 ))
156+ no_change = False
157+ break
158+ if no_change :
159+ break
164160
165161 return S
166162
@@ -188,7 +184,7 @@ def main():
188184 ox , oy = get_observation_points ([v1 , v2 ], angle_reso )
189185
190186 # step1: Adaptive Range Segmentation
191- ids = adoptive_range_segmentation (ox , oy )
187+ idsets = adoptive_range_segmentation (ox , oy )
192188
193189 if show_animation : # pragma: no cover
194190 plt .cla ()
@@ -197,7 +193,11 @@ def main():
197193 v1 .plot ()
198194 v2 .plot ()
199195
200- plt .plot (ox , oy , "ob" )
196+ # plt.plot(ox, oy, "ob")
197+ for ids in idsets :
198+ plt .plot ([ox [i ] for i in range (len (ox )) if i in ids ],
199+ [oy [i ] for i in range (len (ox )) if i in ids ],
200+ "o" )
201201 # plt.plot(x, y, "xr")
202202 # plot_circle(ex, ey, er, "-r")
203203 plt .pause (0.1 )
0 commit comments