-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouteBoxer.js
299 lines (296 loc) · 9.36 KB
/
RouteBoxer.js
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
class RouteBoxer {
constructor(map, path, distance, method) {
this.boxes = null; // the list of boxes/
this.distance = distance; //in KM the distance that we want to find along the path.
this.marklist = new Array();
this.verticalBoxes = new Array();
this.horizontalBoxes = new Array();
this.path = path;
this.map = map;
this.boxcheckingmethod = method;
}
GetVerticalResult() {
return this.resultVerticalBoxes;
}
GetHorizontalResult() {
return this.resultHorizontalBoxes;
}
GetDistanceFromLatLonInKm(lat1, lng1, lat2, lng2) {
var R = 6371; // Radius of the earth in km
var dLat = this.deg2rad(lat2 - lat1); // deg2rad below
var dLng = this.deg2rad(lng2 - lng1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
return d;
}
deg2rad(deg) {
return deg * (Math.PI / 180)
}
// This function finds the bounded box of the path google direction api has found
// The idea to find the bouneded box is simple.
// I try to find the largest latitude, the largest longitude,
// the smallest latitude and the smallest longitude in the list of geopoints.
// These 4 values from a bound of the bounded box of the path.
FindCoverBox() {
var pointList = this.path;
var maxLat = -190;
var maxLng = -190;
var minLat = 190;
var minLng = 190;
for (var i = 0; i < pointList.length; ++i) {
var point = pointList[i];
if (point.lat > maxLat) maxLat = point.lat;
if (point.lat < minLat) minLat = point.lat;
if (point.lng > maxLng) maxLng = point.lng;
if (point.lng < minLng) minLng = point.lng;
}
var _height = this.GetDistanceFromLatLonInKm(minLat, 0, maxLat, 0);
var _width = this.GetDistanceFromLatLonInKm(0, minLng, 0, maxLng);
console.log(_height);
console.log(_width);
return {
bounds: {
north: maxLat,
south: minLat,
east: maxLng,
west: minLng,
},
edges: {
height: _height,
width: _width,
}
}
}
//Generate box list and display on the map
GenerateBoxes(draw = true) {
if (distance === 0) return;
var boundedBox = this.FindCoverBox();
var dLat = this.distance / 110.574;
var dLng = this.distance / 111.320;
var currentRightBound = boundedBox.bounds.west - 3 * dLng;
var currentBottomBound = boundedBox.bounds.north + 3 * dLat;
var stopLng = boundedBox.bounds.east + 2 * dLng;
var stopLat = boundedBox.bounds.south - 2 * dLat;
var latList = new Array();
var lngList = new Array();
while (currentBottomBound > stopLat) {
currentBottomBound = currentBottomBound - dLat;
latList.push(currentBottomBound);
}
while (currentRightBound < stopLng) {
currentRightBound = currentRightBound + dLng;
lngList.push(currentRightBound);
}
this.boxes = new Array();
for (var i = 0; i < latList.length - 1; ++i) {
this.boxes.push(new Array());
for (var j = 0; j < lngList.length - 1; ++j) {
var bound = { north: latList[i], south: latList[i + 1], west: lngList[j], east: lngList[j + 1] };
this.boxes[i].push(new Box(i, j, bound));
if(draw)this.boxes[i][j].DrawBox(this.map);
}
}
}
//Mark all boxes that are crossed by the path.
MarkBoxes() {
var r = this.boxes.length;
var c = this.boxes[0].length;
var pathLength = this.path.length - 1;
for (var i = 1; i < r - 1; ++i) {
for (var j = 1; j < c - 1; ++j) {
for (var k = 0; k < pathLength; ++k) {
var isMarked = this.boxes[i][j].Mark(this.path[k], this.path[k + 1], this.boxcheckingmethod);
if (isMarked) {
this.marklist.push({ x: i, y: j });
}
}
}
}
}
// Add neighbours of marked boxes in markboxes step
// Also add them to marklist
Expanded() {
var r = this.boxes.length;
var c = this.boxes[0].length;
var length = this.marklist.length;
for (var i = 0; i < length; ++i) {
var x = this.marklist[i].x;
var y = this.marklist[i].y;
this.boxes[x - 1][y + 1].MarkExpanded();
this.marklist.push({ x: x - 1, y: y + 1 });
this.boxes[x + 1][y + 1].MarkExpanded();
this.marklist.push({ x: x + 1, y: y + 1 });
this.boxes[x][y + 1].MarkExpanded();
this.marklist.push({ x: x, y: y + 1 });
this.boxes[x - 1][y - 1].MarkExpanded();
this.marklist.push({ x: x - 1, y: y - 1 });
this.boxes[x + 1][y - 1].MarkExpanded();
this.marklist.push({ x: x + 1, y: y - 1 });
this.boxes[x][y - 1].MarkExpanded();
this.marklist.push({ x: x, y: y - 1 });
this.boxes[x - 1][y].MarkExpanded();
this.marklist.push({ x: x - 1, y: y });
this.boxes[x + 1][y].MarkExpanded();
this.marklist.push({ x: x + 1, y: y });
}
}
// Clear all box on the map.
ClearGeneratedBoxesOnMap(clearmemory) {
for (var i = 0; i < this.boxes.length; ++i) {
for (var j = 0; j < this.boxes[i].length; ++j) {
this.boxes[i][j].ClearBoxOnMap();
}
}
if (clearmemory) {
this.boxes = null;
}
}
//sleep function
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
RouteBoxerAlgorithm(mode){
if(mode == 'horizontal'){
this.GenerateBoxes(false);
this.MarkBoxes();
this.Expanded();
var temp = this.HorizontalMerge(this.boxes);
this.horizontalBoxes = this.GetResult(temp);
this.DrawBoxes(this.horizontalBoxes, Box.MERGECOLOR());
} else {
this.GenerateBoxes(false);
this.MarkBoxes();
this.Expanded();
var temp = this.VerticalMerge(this.boxes);
this.verticalBoxes = this.GetResult(temp);
this.DrawBoxes(this.verticalBoxes, Box.MERGECOLOR());
}
}
// The main function of routeboxer algorithm
// map is the google map object
// sleeptime in second
async VisualizeRouteBoxerAlgorithm(sleeptime) {
sleeptime = sleeptime * 1000;
await this.sleep(sleeptime);
this.GenerateBoxes();
await this.sleep(sleeptime);
this.MarkBoxes();
await this.sleep(sleeptime);
this.Expanded();
await this.sleep(sleeptime);
this.ClearGeneratedBoxesOnMap(false);
var temp = this.HorizontalMerge(this.boxes);
this.DrawBoxes(temp);
await this.sleep(sleeptime);
this.horizontalBoxes = this.GetResult(temp);
this.ClearBoxesOfTheListOnMap(temp);
this.DrawBoxes(this.horizontalBoxes, Box.MERGECOLOR());
await this.sleep(sleeptime);
this.ClearBoxesOfTheListOnMap(this.horizontalBoxes);
this.boxes = new Array();
this.marklist = new Array();
this.GenerateBoxes();
await this.sleep(sleeptime);
this.MarkBoxes();
await this.sleep(sleeptime);
this.Expanded();
await this.sleep(sleeptime);
this.ClearGeneratedBoxesOnMap(false);
temp = this.VerticalMerge(this.boxes);
this.ClearBoxesOfTheListOnMap(this.horizontalBoxes);
this.DrawBoxes(temp);
await this.sleep(sleeptime);
this.ClearBoxesOfTheListOnMap(temp);
this.verticalBoxes = this.GetResult(temp);
this.DrawBoxes(this.verticalBoxes, Box.MERGECOLOR());
}
HorizontalMerge(boxes) {
var r = boxes.length;
var c = boxes[0].length;
var current_box_index = -1;
var result = new Array();
for (var i = 0; i < r; ++i) {
for (var j = 0; j < c; ++j) {
if (boxes[i][j].IsMarked()) {
if (current_box_index == -1) {
var t = boxes[i][j];
result.push(t);
current_box_index = result.length - 1;
}
else {
result[current_box_index].Merge(boxes[i][j]);
}
} else {
current_box_index = -1;
}
}
}
return result;
}
VerticalMerge(boxes) {
var r = boxes.length;
var c = boxes[0].length;
var current_box_index = -1;
var result = new Array();
for (var j = 0; j < c; ++j) {
for (var i = 0; i < r; ++i) {
if (boxes[i][j].IsMarked()) {
if (current_box_index == -1) {
var t = boxes[i][j];
result.push(t);
current_box_index = result.length - 1;
}
else {
result[current_box_index].Merge(boxes[i][j]);
}
} else {
current_box_index = -1;
}
}
}
return result;
}
ClearBoxesOfTheListOnMap(boxes, clearmemory = false) {
var length = boxes.length;
for (var i = 0; i < length; ++i) {
boxes[i].ClearBoxOnMap(this.map);
}
if (clearmemory) {
boxes = null;
}
}
// Draw boxes on the map.
DrawBoxes(boxes, color = null) {
var length = boxes.length;
if (color != null) {
for (var i = 0; i < length; ++i) {
boxes[i].DrawBox(this.map, color);
}
} else {
for (var i = 0; i < length; ++i) {
boxes[i].DrawBox(this.map);
}
}
}
// Get result function merge result from horizontal or verticala merge step
GetResult(boxes) {
var result = new Array();
var length = boxes.length;
result.push(boxes[0]);
var isAbleToMerge = false;
var current_box_index = 0;
for (var i = 1; i < length; ++i) {
isAbleToMerge = result[current_box_index].Merge(boxes[i]);
if (!isAbleToMerge) {
++current_box_index;
result.push(boxes[i]);
}
}
return result;
}
}