-
Notifications
You must be signed in to change notification settings - Fork 0
/
poisson-ptri.js
137 lines (117 loc) · 3.09 KB
/
poisson-ptri.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
importScripts('http://r3mi.github.io/poly2tri.js/dist/poly2tri.js');
importScripts('https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js');
importScripts('quadtree.js');
const PI2 = Math.PI * 2;
var RADIUS = 20;
var CANDIDATES = 10;
var WIDTH = 100;
var HEIGHT = 100;
var polygons = [];
var points = [];
var active = [];
var quadtree;
function poisson() {
quadtree = quadtreeNew(0, 0, WIDTH, HEIGHT);
// create one random point
var point = {
x: Math.random() * WIDTH,
y: Math.random() * HEIGHT,
vx: 0,
vy: 0,
i: 0,
};
points.push(point);
active.push(point);
quadtreeAdd(quadtree, point);
while (active.length) {
// get a random active sample
var randomIndex = Math.floor(Math.random() * active.length);
var current = active[randomIndex];
var acceptable = null;
// generate candidates
for (var i = 0; i < CANDIDATES; i++) {
var far = Math.random() * RADIUS + RADIUS;
var angle = Math.random() * PI2;
var candidate = {
x: current.x + Math.cos(angle) * far,
y: current.y - Math.sin(angle) * far,
vx: 0,
vy: 0,
i: points.length,
};
var good = true;
// check candidate
if (candidate.x > WIDTH || candidate.y > HEIGHT
|| candidate.x < 0 || candidate.y < 0) {
good = false;
} else {
good = quadtreeFits(quadtree, candidate);
}
if (good) {
acceptable = candidate;
break;
}
}
if (acceptable) {
points.push(acceptable);
active.push(acceptable);
quadtreeAdd(quadtree, acceptable);
} else {
active.splice(randomIndex, 1);
}
}
}
function ptri(pointList, canvas) {
var contour = [
{x: 0, y: 0},
{x: canvas.width, y: 0},
{x: canvas.width, y: canvas.height},
{x: 0, y: canvas.height},
/*
new poly2tri.Point(0, 0),
new poly2tri.Point(canvas.width, 0),
new poly2tri.Point(canvas.width, canvas.height),
new poly2tri.Point(0, canvas.height)
*/
].map((point, index) => ({
...point,
i: index + pointList.length,
}));
var swctx = new poly2tri.SweepContext(contour);
// use original points
/*
points = pointList.map(function (point) {
return new poly2tri.Point(point.x, point.y);
});
swctx.addPoints(points);
*/
swctx.addPoints(pointList);
var triangles = swctx.triangulate().getTriangles();
polygons = [];
for (var tri of triangles) {
polygons.push({points: tri.getPoints().map(point => point.i)});
}
// LOL, this was a circular reference
/*
for (var poly of polygons) {
for (var point of poly.points) {
if (!point.polygons) {
point.polygons = [];
}
point.polygons.push(poly);
}
}
*/
return [polygons, [...pointList, ...contour]];
}
self.onmessage = function (event) {
RADIUS = event.data[0];
CANDIDATES = event.data[1];
WIDTH = event.data[2];
HEIGHT = event.data[3];
poisson();
const [polygons, pointList] = ptri(points, {width: WIDTH, height: HEIGHT});
//console.log('hi', results);
postMessage([polygons, pointList, quadtree]);
self.close();
};