-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
566 lines (516 loc) · 18.5 KB
/
main.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
var input_formula = '-x';
var power = 1;
var distance_choice = 0;
var gradient_choice = 0;
var operator_choice = 0;
var neighbor_choice = 0;
var operator_boolean = false;
var djikstra_heat_map = function(p){
var node_grid = [];
var open_list = [];
var neighbor_list = [];
var target_node;
var x_lim,y_lim;
var tile_size = 30;
var time = 0;
p.setup = function(){
p.createCanvas(720,480);
x_lim = p.ceil(p.width/tile_size);
y_lim = p.ceil(p.height/tile_size);
p.init_grid();
p.init_neighbor();
p.colorMode(p.HSB);
}
p.draw = function(){
p.background(0);
p.draw_heatmap();
p.draw_grid();
/*
if(open_list.length > 0){
if(p.millis() - time){
p.djikstra_algorithm();
time = p.millis();
}
}
*/
}
p.init_grid = function(){
for(y = 0;y < y_lim; y++){
var temp_array = [];
for(x = 0;x < x_lim; x++){
var new_node = new node(p.createVector(x,y),-1000);
temp_array.push(new_node);
}
node_grid.push(temp_array);
}
target_node = node_grid[p.floor(y_lim/2)][p.floor(x_lim * 8/10)];
target_node.distance = 0;
}
p.init_neighbor = function(){
for(x = 0;x < 3; x++){
for(y = 0;y < 3; y++){
var new_vector = p.createVector(x-1,y-1);
neighbor_list.push(new_vector);
}
}
neighbor_list.splice(4,1);
}
p.draw_grid = function(){
p.stroke(100);
p.strokeWeight(1);
for(x = 0;x < x_lim; x++){
p.line(x*tile_size,0,x*tile_size,p.height);
}
for(y=0;y < y_lim; y++){
p.line(0,y*tile_size,p.width,y*tile_size);
}
}
p.draw_heatmap = function(){
p.noStroke();
for(y = 0;y < y_lim; y++){
for(x = 0;x < x_lim; x++){
var current_node = node_grid[y][x];
switch(current_node.distance){
case -1000:
p.fill(0);
break;
case 0:
p.fill(118,100,100);
break;
case -9999:
p.fill(0,0,53);
break;
default:
var color = p.map(current_node.distance,0,p.max(x_lim,y_lim),0,200);
p.fill(color,255,255);
}
p.rect(x*tile_size,y*tile_size,tile_size,tile_size);
p.noStroke();
p.fill(0);
//p.text(current_node.distance.toFixed(2),x*tile_size,(y+.5)*tile_size)
}
}
}
p.in_grid = function(input){
return input.x >= 0 && input.x < x_lim && input.y >= 0 && input.y < y_lim;
}
p.mouseDragged = function(){
var mouse_vector = p.createVector(p.floor(p.mouseX/tile_size),p.floor(p.mouseY/tile_size));
node_grid[mouse_vector.y][mouse_vector.x].distance = -9999;
}
p.keyPressed = function(){
open_list.push(target_node);
p.djikstra_algorithm();
}
p.djikstra_algorithm = function(){
while(open_list.length > 0){
var current_node = open_list[0];
for(i = 0;i < neighbor_list.length; i++){
var direction_vector = neighbor_list[i];
var neighbor_vector = p5.Vector.add(direction_vector,current_node.position);
if(p.in_grid(neighbor_vector)){
var neighbor_node = node_grid[neighbor_vector.y][neighbor_vector.x];
var distance = neighbor_node.distance;
var new_distance = current_node.distance + direction_vector.mag(); //modifying the added value might impact on curl of the vector field
if(distance != -9999){
if(distance == -1000){
neighbor_node.distance = new_distance;
open_list.push(neighbor_node);
}
else if(distance > new_distance && distance != 0){
neighbor_node.distance = new_distance;
}
}
}
}
open_list.splice(0,1);
}
}
}
//Solve issues of edge tiles not being computed
var vectorfield_pathfinding = function(p){
var node_grid = [];
var open_list = [];
var neighbor_list = [];
var neighbor_djikstra = [];
var target_node;
var x_lim,y_lim;
var tile_size = 30;
var time = 0;
var sobel_x =
[[-1,0,1],
[-2,0,2],
[-1,0,1]
]
var sobel_y =
[[-1,-2,-1],
[0,0,0],
[1,2,1]
]
p.setup = function(){
p.createCanvas(720,480);
x_lim = p.ceil(p.width/tile_size);
y_lim = p.ceil(p.height/tile_size);
p.init_grid();
p.init_djikstra_neighbor();
p.colorMode(p.HSB);
}
p.draw = function(){
p.background(0);
if(operator_boolean){
p.choose_operator();
//p.draw_grid();
}
else{
p.draw_heatmap();
p.draw_grid();
p.display_arrows();
}
/*
if(open_list.length > 0){
if(p.millis() - time){
p.djikstra_algorithm();
time = p.millis();
}
}
*/
}
p.init_grid = function(){
node_grid = [];
for(y = 0;y < y_lim; y++){
var temp_array = [];
for(x = 0;x < x_lim; x++){
var new_node = new node(p.createVector(x,y),-1000);
temp_array.push(new_node);
}
node_grid.push(temp_array);
}
target_node = node_grid[p.floor(y_lim/2)][p.floor(x_lim * 8/10)];
target_node.distance = 0;
}
p.reset_grid = function(){
for(y = 0;y < y_lim; y++){
for(x = 0;x < x_lim; x++){
var current_node = node_grid[y][x];
if(current_node.distance != -9999){
current_node.distance = -1000;
}
}
}
target_node = node_grid[p.floor(y_lim/2)][p.floor(x_lim * 8/10)];
target_node.distance = 0;
}
p.init_neighbor = function(){
neighbor_list = [];
switch(p.int(neighbor_choice)){
case 0:
for(x = 0;x < 3; x++){
for(y = 0;y < 3; y++){
var new_vector = p.createVector(x-1,y-1);
neighbor_list.push(new_vector);
}
}
neighbor_list.splice(4,1);
break;
case 1:
neighbor_list.push(p.createVector(0,-1));
neighbor_list.push(p.createVector(0,1));
neighbor_list.push(p.createVector(1,0));
neighbor_list.push(p.createVector(-1,0));
break;
case 2:
for(x = 0;x < 5; x++){
for(y = 0;y < 5; y++){
var new_vector = p.createVector(x-2,y-2);
neighbor_list.push(new_vector);
}
}
break;
case 3:
neighbor_list.push(p.createVector(-2,0));
neighbor_list.push(p.createVector(-1,0));
neighbor_list.push(p.createVector(1,0));
neighbor_list.push(p.createVector(2,0));
neighbor_list.push(p.createVector(0,1));
neighbor_list.push(p.createVector(0,2));
neighbor_list.push(p.createVector(0,-1));
neighbor_list.push(p.createVector(0,-2));
neighbor_list.push(p.createVector(1,1));
neighbor_list.push(p.createVector(-1,1));
neighbor_list.push(p.createVector(1,-1));
neighbor_list.push(p.createVector(-1,-1));
break;
}
console.log(neighbor_list);
}
p.init_djikstra_neighbor = function(){
for(x = 0;x < 3; x++){
for(y = 0;y < 3; y++){
var new_vector = p.createVector(x-1,y-1);
neighbor_djikstra.push(new_vector);
}
}
neighbor_djikstra.splice(4,1);
}
p.draw_grid = function(){
p.stroke(100);
p.strokeWeight(1);
for(x = 0;x < x_lim; x++){
p.line(x*tile_size,0,x*tile_size,p.height);
}
for(y=0;y < y_lim; y++){
p.line(0,y*tile_size,p.width,y*tile_size);
}
}
p.draw_heatmap = function(){
p.noStroke();
for(y = 0;y < y_lim; y++){
for(x = 0;x < x_lim; x++){
var current_node = node_grid[y][x];
switch(current_node.distance){
case -1000:
p.fill(0);
break;
case 0:
p.fill(118,100,100);
break;
case -9999:
p.fill(0,0,53);
break;
default:
var color = p.map(current_node.distance,0,p.max(x_lim,y_lim),0,200);
p.fill(color,255,255);
}
p.rect(x*tile_size,y*tile_size,tile_size,tile_size);
p.noStroke();
p.fill(0);
//p.text(current_node.distance.toFixed(2),x*tile_size,(y+.5)*tile_size)
}
}
}
p.display_arrows = function(){
p.stroke(0);
for(x = 0;x < x_lim; x++){
for(y = 0;y < y_lim; y++){
var current_node = node_grid[y][x];
if(current_node.direction != 0){
var position = p.createVector((x+.5)*tile_size,(y+.5)*tile_size);
var arrow_position = current_node.direction.copy().mult(tile_size/2);
p.push();
p.translate(position.x,position.y);
p.line(0,0,arrow_position.x,arrow_position.y);
p.push();
p.translate(arrow_position.x,arrow_position.y);
p.rotate(arrow_position.heading() + 3*p.PI/4);
p.line(0,0,tile_size/5,0);
p.line(0,0,0,tile_size/5);
p.pop();
p.pop();
}
}
}
}
p.in_grid = function(input){
return input.x >= 0 && input.x < x_lim && input.y >= 0 && input.y < y_lim;
}
p.in_screen = function(){
return p.mouseX >= 0 && p.mouseX <= p.width && p.mouseY >= 0 && p.mouseY <= p.height;
}
p.mouseDragged = function(){
if(p.in_screen()){
var mouse_vector = p.createVector(p.floor(p.mouseX/tile_size),p.floor(p.mouseY/tile_size));
node_grid[mouse_vector.y][mouse_vector.x].distance = -9999;
}
}
p.compute_vectorfield = function(){
p.init_neighbor();
p.reset_grid();
open_list.push(target_node);
p.djikstra_algorithm();
p.choose_gradient();
p.compute_divergence();
p.compute_curl();
}
p.djikstra_algorithm = function(){
while(open_list.length > 0){
var current_node = open_list[0];
for(i = 0;i < neighbor_djikstra.length; i++){
var direction_vector = neighbor_djikstra[i];
var neighbor_vector = p5.Vector.add(direction_vector,current_node.position);
if(p.in_grid(neighbor_vector)){
var neighbor_node = node_grid[neighbor_vector.y][neighbor_vector.x];
var distance = neighbor_node.distance;
var new_distance = current_node.distance + p.choose_distance(direction_vector); //modifying the added value might impact on curl of the vector field
if(distance != -9999){
if(distance == -1000){
neighbor_node.distance = new_distance;
open_list.push(neighbor_node);
}
else if(distance > new_distance && distance != 0){
neighbor_node.distance = new_distance;
}
}
}
}
open_list.splice(0,1);
}
}
p.kernel_convolution_function = function(){
for(y = 0;y < y_lim; y++){
for(x = 0;x < x_lim; x++){
var current_node = node_grid[y][x];
var output_vector = p.createVector(0,0);
for(i = 0;i < neighbor_list.length; i++){
var direction_vector = neighbor_list[i];
var neighbor_vector = p5.Vector.add(current_node.position,direction_vector);
if(p.in_grid(neighbor_vector)){
var neighbor_node = node_grid[neighbor_vector.y][neighbor_vector.x];
var distance = neighbor_node.distance;
output_vector.add(direction_vector.copy().mult(p.compute_distance_function(distance)));
}
//add else statement to correct boundarie issues
}
output_vector.normalize();
current_node.direction = output_vector.copy();
}
}
}
p.kernel_convolution_sobel = function(){ //add in-grid if just like in kernel_convolution_function
for(y1 = 1;y1 < y_lim-1; y1++){
for(x1 = 1;x1 < x_lim-1; x1++){
var current_node = node_grid[y1][x1];
var x_gradient = 0;
var y_gradient = 0;
for(x2 = 0;x2 < 3; x2++){
for(y2 = 0;y2 < 3; y2++){
var neighbor_node = node_grid[y1 + y2 - 1][x1 + x2 - 1];
x_gradient -= neighbor_node.distance * sobel_x[y2][x2];
y_gradient -= neighbor_node.distance * sobel_y[y2][x2];
}
}
var angle = p.atan2(y_gradient,x_gradient);
current_node.direction = p5.Vector.fromAngle(angle).copy();
}
}
}
p.kernel_convolution_minimum = function(){ //add in-grid if just like in kernel_convolution_function
for(x = 1;x < x_lim-1; x++){
for(y = 1;y < y_lim-1; y++){
var current_node = node_grid[y][x];
var direction = [];
var minimum = 10000
for(i = 0;i < neighbor_list.length; i++){
var neighbor = neighbor_list[i];
var neighbor_node = node_grid[y + neighbor.y][x + neighbor.x];
if(neighbor_node.distance < minimum && neighbor_node.distance != -9999){
minimum = neighbor_node.distance;
direction = neighbor.copy();
}
}
current_node.direction = direction.copy();
}
}
}
p.compute_divergence = function(){
for(x = 2;x < x_lim-2; x++){
for(y = 2;y < y_lim-2; y++){
var current_node = node_grid[y][x];
var next_x_node = node_grid[y][x+1];
var next_y_node = node_grid[y+1][x];
var prev_x_node = node_grid[y][x-1];
var prev_y_node = node_grid[y-1][x];
var x_difference = p5.Vector.sub(next_x_node.direction,prev_x_node.direction).x;
var y_difference = p5.Vector.sub(next_y_node.direction,prev_y_node.direction).y;
var divergence = x_difference + y_difference;
current_node.divergence = divergence;
}
}
}
p.compute_curl = function(){
for(x = 2;x < x_lim-2; x++){
for(y = 2;y < y_lim-2; y++){
var current_node = node_grid[y][x];
var next_x_node = node_grid[y][x+1];
var next_y_node = node_grid[y+1][x];
var prev_x_node = node_grid[y][x-1];
var prev_y_node = node_grid[y-1][x];
var x_difference = p5.Vector.sub(next_x_node.direction,prev_x_node.direction).y;
var y_difference = p5.Vector.sub(next_y_node.direction,prev_y_node.direction).x;
var curl = x_difference - y_difference;
current_node.curl = curl;
}
}
}
p.draw_divergence = function(){
for(x = 0;x < x_lim; x++){
for(y = 0;y < y_lim; y++){
var current_node = node_grid[y][x];
var color = p.map(current_node.divergence,0,-4,0,255);
p.fill(230,255,color);
p.rect(x * tile_size,y * tile_size, tile_size, tile_size);
}
}
}
p.draw_curl = function(){
for(x = 0;x < x_lim; x++){
for(y = 0;y < y_lim; y++){
var current_node = node_grid[y][x];
var color = p.map(current_node.curl,0,-2,2,255);
p.fill(230,255,color);
p.rect(x * tile_size,y * tile_size, tile_size, tile_size);
}
}
}
p.choose_distance = function(input){
switch(distance_choice){
case 0:
return 1;
break;
case 1:
return p.ceil(input.mag());
break;
case 2:
return input.mag();
break;
}
}
p.choose_gradient = function(){
switch (gradient_choice){
case 0:
p.kernel_convolution_function();
break;
case 1:
p.kernel_convolution_sobel();
break;
case 2:
p.kernel_convolution_minimum();
break;
}
}
p.choose_operator = function(){
switch (operator_choice){
case 0:
p.draw_divergence();
break;
case 1:
p.draw_curl();
break;
}
}
p.compute_distance_function = function(input){
var buffer_formula = input_formula;
buffer_formula = buffer_formula.replaceAll('x',input);
var output = math.evaluate(buffer_formula);
return output;
}
}
var myp5 = new p5(vectorfield_pathfinding,'vectorfield_pathfinding');
class node{
constructor(position,distance){
this.position = position;
this.distance = distance;
this.direction = 0;
this.divergence = 0;
this.curl = 0;
}
}