-
Notifications
You must be signed in to change notification settings - Fork 0
/
p5.voronoi.js
748 lines (622 loc) · 16.4 KB
/
p5.voronoi.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
MIT License
Copyright (c) 2018 Francisco Moreira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
const VOR_CELLDRAW_BOUNDED = 1;
const VOR_CELLDRAW_CENTER = 2;
const VOR_CELLDRAW_SITE = 3;
(function() {
var imgWidth;
var imgHeight;
var voronoiDiagram;
var sites = [];
var cells = [];
var notFirst = false;
var nRandoms;
var randomMinimumDist = 0;
var voronoiObj = new Voronoi();
var cellColors = [];
var cellStrokeWeight = 1;
var cellStroke = 0;
var siteStrokeWeight = 3;
var siteStroke = 0;
var drawSites = true;
var jitterStepMax = 20;
var jitterStepMin = 5;
var jitterFactor = 3;
var jitterBorderFlag = false;
var jitterCells = [];
/*
Set cell stroke weight
*/
p5.prototype.voronoiCellStrokeWeight = function(w){
if(w >= 0)
cellStrokeWeight = w;
}
/*
Set site stroke weight
*/
p5.prototype.voronoiSiteStrokeWeight = function(w){
if(w >= 0)
siteStrokeWeight = w;
}
/*
Set cell stroke
*/
p5.prototype.voronoiCellStroke = function(c){
cellStroke = c;
}
/*
Set site stroke
*/
p5.prototype.voronoiSiteStroke = function(c){
siteStroke = c;
}
/*
Set flag to draw sites or not
*/
p5.prototype.voronoiSiteFlag = function(b){
drawSites = b;
}
/*
Add Random Sites
*/
p5.prototype.voronoiRndSites = function(n, newMinimum){
nRandoms = n;
if(newMinimum !== undefined)
randomMinimumDist = newMinimum;
}
/*
Set random minimum distance
*/
p5.prototype.voronoiRndMinDist = function(newMinimum){
randomMinimumDist = newMinimum;
}
/*
Add custom sites
*/
p5.prototype.voronoiSites = function(newSites){
for (var i = 0; i < newSites.length; i++) {
sites.push({x:newSites[i][0],y:newSites[i][1]});
if(newSites[i][2] !== undefined)
cellColors.push([newSites[i][0],newSites[i][1],newSites[i][2]]);
else
cellColors.push([newSites[i][0],newSites[i][1],color(random(0,255),random(0,255),random(0,255))]);
}
}
/*
Add custom site
*/
p5.prototype.voronoiSite = function(nx, ny, nColor){
sites.push({x:nx, y:ny});
if(nColor !== undefined)
cellColors.push([nx,ny,nColor]);
else
cellColors.push([nx,ny,color(random(0,255),random(0,255),random(0,255))]);
}
/*
Remove custom site by index "dx" or by coordinates "dx,dy"
*/
p5.prototype.voronoiRemoveSite = function(dx, dy){
if(dy === undefined){
//Delete by id
sites.splice(dx, 1);
}else{
//Delete by coordinates
for (var i = 0; i < sites.length; i++) {
if(sites[i].x == dx && sites[i].y == dy){
sites.splice(i, 1);
return;
}
}
}
}
/*
Remove all custom sites
*/
p5.prototype.voronoiClearSites = function(){
cellColors = [];
sites = [];
}
/*
Get Cell id in position
*/
p5.prototype.voronoiGetSite = function(x, y, jitter = false){
//Default to normal cells
var target = cells;
//Get Site with Jitter instead
if(jitter){
//Detect if jitter structure is not empty
if(jitterCells.length !== 0){
target = jitterCells;
}else{
console.log("voronoiGetSite: Jitter was not generated, using normal diagram");
}
}
//For each cell
for (var i = 0; i < target.length; i++) {
if(raycast([x, y], target[i]))
return i;
}
}
/*
Raycast
https://github.com/substack/point-in-polygon
*/
function raycast (point, vs) {
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1];
var xj = vs[j][0], yj = vs[j][1];
var intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect){
inside = !inside;
}
}
return inside;
};
/*
Compute
*/
p5.prototype.voronoi = function(width, height, jitterFlag = false){
//Recycle diagram
voronoiObj.recycle(voronoiDiagram);
//Remove Old Randoms
if(notFirst)
sites.splice(sites.length-nRandoms,nRandoms);
//Set Diagram Size
imgWidth = width;
imgHeight = height;
//Set Random Sites
setRandoms(width, height);
//Compute
voronoiDiagram = voronoiObj.compute(sites,{xl:0, xr:width, yt:0, yb:height});
//Simplify Cells structure
simplifyCells();
//Create Jitter
jitterCells = [];
if(jitterFlag)
jitter();
//First instance
if(!notFirst)
notFirst = true;
}
/*
Simplify Gorhill structure to cells
*/
function simplifyCells(){
cells = [];
for (var i = 0; i < voronoiDiagram.cells.length; i++) {
var vertices = [];
for (var j = 0; j < voronoiDiagram.cells[i].halfedges.length; j++) {
vertices.push([voronoiDiagram.cells[i].halfedges[j].getStartpoint().x, voronoiDiagram.cells[i].halfedges[j].getStartpoint().y]);
}
cells.push(vertices);
}
}
/*
Returns Diagram for more advanced uses
*/
p5.prototype.voronoiGetDiagram = function(){
return voronoiDiagram;
}
/*
Returns simplified cells for more advanced use
*/
p5.prototype.voronoiGetCells = function(){
return cells;
}
/*
Add Random Sites
*/
function setRandoms(width, height){
for (var i = 0; i < nRandoms; i++) {
var flag;
var nX = round(random(0,width));
var nY = round(random(0,height));
var triesLimit = 500;
var tries = 0;
if (randomMinimumDist > 0) {
do{
flag = false;
nX = round(random(0,width));
nY = round(random(0,height));
for (var j = 0; j < sites.length; j++) {
if(dist2D(nX, nY, sites[j].x, sites[j].y) < randomMinimumDist)
flag = true;
}
tries++;
}while(flag == true && tries <= triesLimit)
}
sites.push({x:nX, y:nY});
//Set Color
cellColors.push([nX,nY,color(random(0,255),random(0,255),random(0,255))]);
//Warn about maximum tries reached
if (tries >= triesLimit)
console.log("Warning: setRandoms tries limit reached: minimum distance(" + randomMinimumDist + ") not ensured");
}
}
/*
Eucledian Distance
*/
function dist2D(ix, iy, fx, fy){
return (sqrt(sq(ix - fx)+sq(iy - fy)))
}
/*
Get voronoi cell neighbors
*/
p5.prototype.voronoiNeighbors = function(id){
if(id >= voronoiDiagram.cells.length || id === undefined)
return;
//All neighbors
var allNeighbors = [];
for (var i = 0; i < voronoiDiagram.cells[id].halfedges.length; i++) {
if(voronoiDiagram.cells[id].halfedges[i].edge.rSite !== null)
allNeighbors.push(voronoiDiagram.cells[id].halfedges[i].edge.rSite.voronoiId);
allNeighbors.push(voronoiDiagram.cells[id].halfedges[i].edge.lSite.voronoiId);
}
//Remove duplicates
var uniqueNeighbors = removeDuplicates(allNeighbors);
//Remove itself
for (var i = 0; i < uniqueNeighbors.length; i++) {
if(uniqueNeighbors[i] == id){
uniqueNeighbors.splice(i,1);
break;
}
}
return uniqueNeighbors;
}
/*
Remove duplicates in array
*/
function removeDuplicates(arr){
let unique_array = []
for(let i = 0;i < arr.length; i++){
if(unique_array.indexOf(arr[i]) == -1){
unique_array.push(arr[i])
}
}
return unique_array;
}
/*
Draw a voronoi Cell
*/
p5.prototype.voronoiDrawCell = function(x, y, id, type, fill = false, jitter = false){
if(id >= voronoiDiagram.cells.length || id === undefined)
return;
var halfedges = voronoiDiagram.cells[id].halfedges;
var siteX = voronoiDiagram.cells[id].site.x;
var siteY = voronoiDiagram.cells[id].site.y;
push();
//Load Color
setFillColorCell(id);
//Check fill flag
if(!fill)
noFill();
//Default to normal cells
var target = cells;
//Draw Jitter instead
if(jitter){
//Detect if jitter structure is not empty
if(jitterCells.length !== 0){
target = jitterCells;
}else{
console.log("voronoiDrawCell: Jitter was not generated, using normal diagram");
}
}
//Choose draw mode
if (type == VOR_CELLDRAW_BOUNDED) {
drawCellBounded(x, y, id, siteX, siteY, jitter, target);
}else if(type == VOR_CELLDRAW_CENTER){
drawCellCenter(x, y, id, siteX, siteY, jitter, target);
}else if(type == VOR_CELLDRAW_SITE){
drawCellSite(x, y, id, siteX, siteY, jitter, target);
}
}
/*
Draw Cell Bounded
*/
function drawCellBounded(x, y, id, siteX, siteY, jitter, target){
//Stroke Settings
strokeWeight(cellStrokeWeight);
stroke(cellStroke);
//Find minimums
let minX = Number.MAX_VALUE;
let minY = Number.MAX_VALUE;
for (var i = 0; i < target[id].length; i++) {
if (target[id][i][0] < minX)
minX = target[id][i][0];
if (target[id][i][1] < minY)
minY = target[id][i][1];
}
//Draw
beginShape();
for (var i = 0; i < target[id].length; i++) {
vertex(target[id][i][0] - minX + x, target[id][i][1] - minY + y);
}
endShape(CLOSE);
//Draw Site
if(drawSites){
strokeWeight(siteStrokeWeight);
stroke(siteStroke);
point(siteX - minX + x, siteY - minY + y);
}
pop();
}
/*
Draw Cell Centered
*/
function drawCellCenter(x, y, id, siteX, siteY, jitter, target){
//Stroke Settings
strokeWeight(cellStrokeWeight);
stroke(cellStroke);
//Find minimums and maximums
let minX = Number.MAX_VALUE;
let minY = Number.MAX_VALUE;
let maxX = 0;
let maxY = 0;
for (var i = 0; i < target[id].length; i++) {
if (target[id][i][0] < minX)
minX = target[id][i][0];
if (target[id][i][1] < minY)
minY = target[id][i][1];
if (target[id][i][0] > maxX)
maxX = target[id][i][0];
if (target[id][i][1] > maxY)
maxY = target[id][i][1];
}
let dX = maxX - minX;
let dY = maxY - minY;
//Draw
beginShape();
for (var i = 0; i < target[id].length; i++) {
vertex(target[id][i][0] - minX + x - dX/2, target[id][i][1] - minY + y - dY/2);
}
endShape(CLOSE);
//Draw Site
if(drawSites){
strokeWeight(siteStrokeWeight);
stroke(siteStroke);
point(siteX - minX + x-dX/2, siteY - minY + y-dY/2);
}
pop();
}
/*
Draw Cell Site
*/
function drawCellSite(x, y, id, siteX, siteY, jitter, target){
//Stroke Settings
strokeWeight(cellStrokeWeight);
stroke(cellStroke);
//Find minimums and maximums
let minX = Number.MAX_VALUE;
let minY = Number.MAX_VALUE;
for (var i = 0; i < target[id].length; i++) {
if (target[id][i][0] < minX)
minX = target[id][i][0];
if (target[id][i][1] < minY)
minY = target[id][i][1];
}
//Draw
beginShape();
for (var i = 0; i < target[id].length; i++) {
vertex(target[id][i][0] - minX + x - (siteX-minX), target[id][i][1] - minY + y - (siteY-minY));
}
endShape(CLOSE);
//Draw Site
if(drawSites){
strokeWeight(siteStrokeWeight);
stroke(siteStroke);
point(siteX - minX + x-(siteX-minX), siteY - minY + y-(siteY-minY));
}
pop();
}
/*
Draw Diagram
*/
p5.prototype.voronoiDraw = function(x, y, fill = true, jitter = false){
//Default to normal cells
var target = cells;
//Draw Jitter instead
if(jitter){
//Detect if jitter structure is not empty
if(jitterCells.length !== 0){
target = jitterCells;
}else{
console.log("voronoiDraw: Jitter was not generated, using normal diagram");
}
}
push();
//Draw Frame only
if(!fill)
noFill();
//Render Cells
for (var i = 0; i < target.length; i++) {
strokeWeight(cellStrokeWeight);
stroke(cellStroke);
//Load Color
if(fill)
setFillColorCell(i);
//Shape
beginShape();
for (var j = 0; j < target[i].length; j++) {
vertex(target[i][j][0] + x, target[i][j][1] + y);
}
endShape(CLOSE);
//Render Site
if(drawSites){
strokeWeight(siteStrokeWeight);
stroke(siteStroke);
let sX = x + voronoiDiagram.cells[i].site.x;
let sY = y + voronoiDiagram.cells[i].site.y;
point(sX,sY);
}
}
pop();
}
/*
Set fill color from cell
*/
function setFillColorCell(cellId){
let color = voronoiGetColor(cellId);
fill(color);
}
/*
Get color of cell id
*/
p5.prototype.voronoiGetColor = function(cellId){
for (var c = 0; c < cellColors.length; c++) {
if(cellColors[c][0] == voronoiDiagram.cells[cellId].site.x && cellColors[c][1] == voronoiDiagram.cells[cellId].site.y){
return cellColors[c][2];
}
}
}
/*
Change color of cell id edited by YJQ
*/
p5.prototype.voronoiChangeColor = function(cellId,color){
for (var c = 0; c < cellColors.length; c++) {
if(cellColors[c][0] == voronoiDiagram.cells[cellId].site.x && cellColors[c][1] == voronoiDiagram.cells[cellId].site.y){
cellColors[c][2]=color;
}
}
}
/*
Set Jitter step Max
*/
p5.prototype.voronoiJitterStepMax = function(s){
if(s >= 0)
jitterStepMax = s;
}
/*
Set Jitter step Min
*/
p5.prototype.voronoiJitterStepMin = function(s){
if(s >= 0)
jitterStepMin = s;
}
/*
Set Jitter factor
*/
p5.prototype.voronoiJitterFactor = function(f){
jitterFactor = f;
}
/*
Set Jitter border flag
*/
p5.prototype.voronoiJitterBorder = function(f){
jitterBorderFlag = f;
}
/*
Get the edges with jitter
*/
p5.prototype.voronoiGetCellsJitter = function(f){
if(jitterCells.length !== 0){
return jitterCells;
}else{
console.log("voronoiGetCellsJitter: Jitter was not generated, using normal diagram");
return cells;
}
}
/*
Creates jittered version of cells
*/
function jitter(){
var edgeMemory = [];
//For each cell
for (var i = 0; i < voronoiDiagram.cells.length; i++) {
var vertices = [];
//For each edge
for (var j = 0; j < voronoiDiagram.cells[i].halfedges.length; j++) {
const edge = voronoiDiagram.cells[i].halfedges[j];
//Detect diagram edge
if(!jitterBorderFlag){
if((round(edge.getStartpoint().x) == 0 && (round(edge.getEndpoint().x) == 0)) ||
(round(edge.getStartpoint().y) == 0 && (round(edge.getEndpoint().y) == 0)) ||
(round(edge.getStartpoint().x) == imgWidth && (round(edge.getEndpoint().x) == imgWidth))||
(round(edge.getStartpoint().y) == imgHeight && (round(edge.getEndpoint().y) == imgHeight))){
vertices.push([edge.getStartpoint().x, edge.getStartpoint().y]);
continue;
}
}
//Look for edge in memory
var found = false;
var id = 0;
for (var e = 0; e < edgeMemory.length; e++) {
//Found flipped
if(edgeMemory[e][0].getEndpoint() === edge.getStartpoint() &&
edgeMemory[e][0].getStartpoint() === edge.getEndpoint()){
found = true;
id = e;
break;
}
}
//If found flipped -> flipped copy
if(found){
for (var e = edgeMemory[id][1].length-1; e > 0; e--) {
vertices.push(edgeMemory[id][1][e]);
}
}
//If not found do jitter
else{
jitterEdge(vertices, edge, edgeMemory);
}
}
jitterCells.push(vertices);
}
}
/*
Jitter edge and add to vertices list
*/
function jitterEdge(vertices, edge, edgeMemory){
//Edge info to save
memEdge = [];
//Edge Details
const dX = edge.getEndpoint().x - edge.getStartpoint().x;
const dY = edge.getEndpoint().y - edge.getStartpoint().y;
const delta = createVector(dX, dY);
const deltaNorm = delta.copy(); deltaNorm.normalize();
const deltaMag = delta.mag();
const perpendicularNorm = deltaNorm.copy(); perpendicularNorm.rotate(HALF_PI);
let start = createVector(edge.getStartpoint().x, edge.getStartpoint().y);
let pos = start.copy();
let jitterVal = 0;
//Add first edge vertice
vertices.push([start.x,start.y]);
memEdge.push([start.x,start.y]);
//Jitter Vertices
var total = random(jitterStepMin,jitterStepMax+1);
while(total < deltaMag){
//Advance pos
pos = p5.Vector.add(start, p5.Vector.mult(deltaNorm, total));
//Jitter Perpendicularly
jitterVal = (random(0,201)-100)/100;
jitterVal *= jitterFactor;
pos.add(p5.Vector.mult(perpendicularNorm, jitterVal));
//Add vertice
vertices.push([pos.x,pos.y]);
memEdge.push([pos.x,pos.y]);
//Advance
total += random(jitterStepMin,jitterStepMax+1);
}
//Add to edge memory
memEdge.push([edge.getEndpoint().x, edge.getEndpoint().y]);
edgeMemory.push([edge, memEdge]);
}
})();