-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevents.py
230 lines (202 loc) · 7.89 KB
/
events.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
#parameters
#eps: for connect disconnect
#tau: for interruption
import numpy as np
import pdb
class Event:
def __init__(self, event, trajectory = None, t = None):
self.event = event
self.trajectory = trajectory
self.t = t
def checkEpsilonDistance(p1, p2, eps):
return (np.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2 + (p1[2] - p2[2])**2)) <= eps
def findConnectDisconnectEvents(t1_id, t2_id, t1, t2, eps):
dic_t1 = {}
dic_t2 = {}
ti = 0
flag_t1 = [False]*len(t1)
flag_t2 = [False]*len(t2)
while ti < (len(t1)):
tj = 0
while tj < (len(t2)):
if not flag_t1[ti] and not flag_t2[tj] and checkEpsilonDistance(t1[ti], t2[tj], eps):
# print(ti,"Start",tj)
flag_t1[ti] = True
flag_t2[tj] = True
flag_insert = True
first_i = ti
last_i = ti
first_j = tj
last_j = tj
while(flag_insert and last_j<len(t2) and first_j>=0):
# print("flag_t1",flag_t1)
# print("flag_t2", flag_t2)
# print("(" ,first_i, " " , last_i , ") (" , first_j , " " , last_j ,")")
flag_insert = False
#case first_i - 1 insert:
if (first_i - 1 >=0) and not flag_t1[first_i - 1 ]:
if first_j - 1 >= 0 and not flag_t2[first_j - 1] and checkEpsilonDistance(t1[first_i - 1], t2[first_j - 1], eps):
first_i = first_i - 1
first_j = first_j - 1
flag_t1[first_i ] = True
flag_t2[first_j ] = True
flag_insert = True
elif last_j + 1 < len(t2) and not flag_t2[last_j + 1] and checkEpsilonDistance(t1[first_i - 1], t2[last_j + 1], eps):
first_i = first_i - 1
last_j = last_j + 1
flag_t1[first_i ] = True
flag_t2[last_j ] = True
flag_insert = True
elif first_j + 1 < len(t2) and not flag_t2[first_j + 1] and checkEpsilonDistance(t1[first_i - 1], t2[first_j + 1], eps):
first_i = first_i - 1
first_j = first_j + 1
flag_t1[first_i ] = True
flag_t2[first_j ] = True
flag_insert = True
elif last_j - 1 >= 0 and not flag_t2[last_j - 1] and checkEpsilonDistance(t1[first_i - 1], t2[last_j - 1], eps):
first_i = first_i - 1
last_j = last_j - 1
flag_t1[first_i ] = True
flag_t2[last_j ] = True
flag_insert = True
else:
for each_j in range(first_j, last_j + 1):
if checkEpsilonDistance(t1[first_i - 1], t2[each_j], eps):
first_i = first_i - 1
flag_t1[first_i ] = True
flag_t2[each_j] = True
flag_insert = True
break
#case last_i + 1 insert:
if (last_i + 1 < len(t1)) and not flag_t1[last_i + 1]:
if first_j - 1 > 0 and not flag_t2[first_j - 1] and checkEpsilonDistance(t1[last_i + 1], t2[first_j - 1], eps):
last_i = last_i + 1
first_j = first_j - 1
flag_t1[last_i ] = True
flag_t2[first_j ] = True
flag_insert = True
elif last_j + 1 < len(t2) and not flag_t2[last_j + 1] and checkEpsilonDistance(t1[last_i + 1], t2[last_j + 1], eps):
last_i = last_i + 1
last_j = last_j + 1
flag_t1[last_i ] = True
flag_t2[last_j ] = True
flag_insert = True
elif first_j + 1 < len(t2) and not flag_t2[first_j + 1] and checkEpsilonDistance(t1[last_i + 1], t2[first_j + 1], eps):
last_i = last_i + 1
first_j = first_j + 1
flag_t1[last_i ] = True
flag_t2[first_j] = True
flag_insert = True
elif last_j - 1 > 0 and not flag_t2[last_j -1] and checkEpsilonDistance(t1[last_i + 1], t2[last_j - 1], eps):
last_i = last_i + 1
last_j = last_j - 1
flag_t1[last_i ] = True
flag_t2[last_j ] = True
flag_insert = True
else:
for each_j in range(first_j, last_j + 1):
if checkEpsilonDistance(t1[last_i + 1], t2[each_j], eps):
last_i = last_i + 1
flag_t1[last_i ] = True
flag_t2[each_j] = True
flag_insert = True
break
#case first_j - 1 insert:
if (first_j - 1 >= 0 ) and not flag_t2[first_j - 1]:
if first_i - 1 >= 0 and not flag_t1[first_i - 1 ] and checkEpsilonDistance(t2[first_j - 1], t1[first_i - 1], eps):
first_j = first_j - 1
first_i = first_i - 1
flag_t1[first_i ] = True
flag_t2[first_j] = True
flag_insert = True
elif last_i + 1 < len(t1) and not flag_t1[last_i + 1] and checkEpsilonDistance(t2[first_j - 1], t1[last_i + 1], eps):
first_j = first_j - 1
last_i = last_i + 1
flag_t1[last_i ] = True
flag_t2[first_j ] = True
flag_insert = True
elif first_i + 1 < len(t1) and not flag_t1[first_i + 1] and checkEpsilonDistance(t2[first_j - 1], t1[first_i + 1], eps):
first_j = first_j - 1
first_i = first_i + 1
flag_t2[first_j ] = True
flag_t1[first_i ] = True
flag_insert = True
elif last_i - 1 >= 0 and not flag_t1[last_i - 1] and checkEpsilonDistance(t2[first_j - 1], t1[last_i - 1], eps):
first_j = first_j - 1
last_i = last_i - 1
flag_t1[last_i ] = True
flag_t2[first_j] = True
flag_insert = True
else:
for each_i in range(first_i, last_i + 1):
if checkEpsilonDistance(t2[first_j - 1], t1[each_i], eps):
first_j = first_j - 1
flag_t2[first_j] = True
flag_t1[each_i] = True
flag_insert = True
break
#case last_j + 1 insert:
if (last_j + 1 < len(t2)) and not flag_t2[last_j + 1]:
if first_i - 1 >= 0 and not flag_t1[first_i - 1] and checkEpsilonDistance(t2[last_j + 1], t1[first_i - 1], eps):
last_j = last_j + 1
first_i = first_i - 1
flag_t1[first_i ] = True
flag_t2[last_j ] = True
flag_insert = True
elif last_i + 1 < len(t1) and not flag_t1[last_i + 1] and checkEpsilonDistance(t2[last_j + 1], t1[last_i + 1], eps):
last_j = last_j + 1
last_i = last_i + 1
flag_t1[last_i ] = True
flag_t2[last_j ] = True
flag_insert = True
elif first_i + 1 <len(t1) and not flag_t1[first_i + 1] and checkEpsilonDistance(t2[last_j + 1], t1[first_i + 1], eps):
last_j = last_j + 1
first_i = first_i + 1
flag_t1[first_i ] = True
flag_t2[last_j ] = True
flag_insert = True
elif last_i - 1 >= 0 and not flag_t1[last_i - 1] and checkEpsilonDistance(t2[last_j + 1], t1[last_i - 1], eps):
last_j = last_j + 1
last_i = last_i - 1
flag_t1[last_i ] = True
flag_t2[last_j ] = True
flag_insert = True
else:
for each_i in range(first_i, last_i + 1):
if checkEpsilonDistance(t2[last_j + 1], t1[each_i], eps):
last_j = last_j + 1
flag_t2[last_j] = True
flag_t1[each_i] = True
flag_insert = True
break
# print("Connected segment t1 and t2 at (" ,first_i, " " , last_i , ") (" , first_j , " " , last_j ,")", t1_id, t2_id)
# print("Connected segment t1 and t2 at (" first_i " " last_i ") (" first_j " " last_j ")", ti, tj)
#connect event
if dic_t1.get(first_i):
dic_t1 [first_i].append(Event("connect", t2_id, first_j))
else:
dic_t1 [first_i] = [Event("connect", t2_id, first_j)]
if dic_t2.get(first_j):
dic_t2 [first_j].append(Event("connect", t1_id, first_i))
else:
dic_t2 [first_j] = [Event("connect", t1_id, first_i)]
#disconnect event
if last_i + 1 < len(t1): #beacuse it gets disconnected later
if dic_t1.get(last_i + 1):
dic_t1 [last_i + 1].append(Event("disconnect", t2_id, last_j + 1))
else:
dic_t1 [last_i + 1] = [Event("disconnect", t2_id, last_j + 1)]
if last_j + 1 < len(t2):
if dic_t2.get(last_j + 1):
dic_t2 [last_j + 1].append(Event("disconnect", t1_id, last_i + 1))
else:
dic_t2 [last_j + 1] = [Event("disconnect", t1_id, last_i + 1)]
ti = last_i
break
else:
tj += 1
ti += 1
# print(dic_t1, dic_t2)
return dic_t1, dic_t2
# if any of the above takes place repeat
#nothing happened then terminate