-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrelax_ww.js
164 lines (146 loc) · 5 KB
/
relax_ww.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
var PatternData;
var step = 0.02;
var metric = 2;
var wgtfn = 0;
// --------------------------------------------------
// relax dot pattern with periodic bounds
// --------------------------------------------------
function relaxStep_ww(step, metric, wgtfn)
{
var DotCoordinatesNew = PatternData.DotCoordinates;
if (PatternData.RelaxState == 0)
{
// change all dots to type 2 (large area)
for (var i=0; i < PatternData.DotCoordinates.length; i++)
PatternData.DotCoordinates[i].type = 2;
}
for (var i=0; i < PatternData.DotCoordinates.length; i++)
{
if (PatternData.DotCoordinates[i].type != 2) continue;
var ptx = PatternData.DotCoordinates[i].x;
var pty = PatternData.DotCoordinates[i].y;
var ix = Math.floor(ptx/PatternData.DotRadius);
var iy = Math.floor(pty/PatternData.DotRadius);
var ax = 0.0;
var ay = 0.0;
var wsum = 0.0;
var wgt1 = 1.0;
//if (metric == 2)
if (wgtfn != 0)
{
var yn = (pty-PatternData.GridSize/2)/(PatternData.GridSize/2);
wgt1 = Math.abs(yn*yn);
}
for (var diy=iy-1; diy <= iy+1; diy++)
for (var dix=ix-1; dix <= ix+1; dix++)
{
var dix2 = dix;
var diy2 = diy;
if (dix2 < 0) dix2 += PatternData.GridCnt;
if (diy2 < 0) diy2 += PatternData.GridCnt;
if (dix2 >= PatternData.GridCnt) dix2 -= PatternData.GridCnt;
if (diy2 >= PatternData.GridCnt) diy2 -= PatternData.GridCnt;
for (var j=0; j < PatternData.DotGrid[diy2*PatternData.GridCnt+dix2].length; j++)
if (PatternData.DotGrid[diy2*PatternData.GridCnt+dix2][j] != i)
{
var ptx2 = PatternData.DotCoordinates[PatternData.DotGrid[diy2*PatternData.GridCnt+dix2][j]].x;
var pty2 = PatternData.DotCoordinates[PatternData.DotGrid[diy2*PatternData.GridCnt+dix2][j]].y;
var wgt = 1.0;
if (wgtfn != 0)
{
var yn = (pty2-PatternData.GridSize/2)/(PatternData.GridSize/2);
//wgt = 1.0-Math.sqrt(Math.abs(yn))*0.975;
if (Math.abs(yn) > 0.01)
wgt = 0.15*(1.0/Math.abs(yn))-0.05;
else
wgt = 0.15*(1.0/0.01)-0.05;
}
var dx = ptx-ptx2;
var dy = pty-pty2;
if (Math.abs(dx) > PatternData.GridSize/2)
{
if (ptx2 > ptx) ptx2 -= PatternData.GridSize;
else ptx2 += PatternData.GridSize;
dx = ptx-ptx2;
}
if (Math.abs(dy) > PatternData.GridSize/2)
{
if (pty2 > pty) pty2 -= PatternData.GridSize;
else pty2 += PatternData.GridSize;
dy = pty-pty2;
}
if (metric == 2)
{
var d = dx*dx + dy*dy*(PatternData.DotRadius/PatternData.DotRadiusY)*(PatternData.DotRadius/PatternData.DotRadiusY);
}
else if (metric > 100)
{
var d = Math.max(Math.abs(dx) + Math.abs(dy)*(PatternData.DotRadius/PatternData.DotRadiusY));
d = d*d;
}
else
{
var d = Math.pow(Math.pow(Math.abs(dx), metric) + Math.pow(Math.abs(dy)*(PatternData.DotRadius/PatternData.DotRadiusY), metric), 2.0/metric);
}
if ((d > 0.0) && (d < PatternData.DotRadius*PatternData.DotRadius))
{
ax += dx*wgt/(d*wgt*wgt);
ay += dy*wgt/(d*wgt*wgt);
wsum += 1.0/(d*wgt*wgt);
}
}
}
if (wsum > 0)
{
//DotCoordinatesNew[i].x = PatternData.DotCoordinates[i].x + step*ax/wsum + (step*wgt1*0.18);
DotCoordinatesNew[i].x = PatternData.DotCoordinates[i].x + step*ax/wsum + (step*wgt1*0.14);
//DotCoordinatesNew[i].x = PatternData.DotCoordinates[i].x + step*ax/wsum;
DotCoordinatesNew[i].y = PatternData.DotCoordinates[i].y + step*ay/wsum;
if (DotCoordinatesNew[i].x < 0) DotCoordinatesNew[i].x += PatternData.GridSize;
if (DotCoordinatesNew[i].y < 0) DotCoordinatesNew[i].y += PatternData.GridSize;
if (DotCoordinatesNew[i].x >= PatternData.GridSize) DotCoordinatesNew[i].x -= PatternData.GridSize;
if (DotCoordinatesNew[i].y >= PatternData.GridSize) DotCoordinatesNew[i].y -= PatternData.GridSize;
}
}
for (var i=0; i < PatternData.GridCnt*PatternData.GridCnt; i++)
PatternData.DotGrid[i] = new Array();
for (var i=0; i < PatternData.DotCoordinates.length; i++)
{
var ix = Math.floor(DotCoordinatesNew[i].x/PatternData.DotRadius);
var iy = Math.floor(DotCoordinatesNew[i].y/PatternData.DotRadius);
PatternData.DotCoordinates[i].x = DotCoordinatesNew[i].x;
PatternData.DotCoordinates[i].y = DotCoordinatesNew[i].y;
PatternData.DotGrid[iy*PatternData.GridCnt+ix].push(i);
}
PatternData.RelaxState = 2;
}
self.addEventListener('message', function(e) {
var data = e.data;
if (typeof(data) === "string")
if (data.length > 100)
data = JSON.parse(data);
if (typeof(data) === "string")
{
switch (data) {
case 'step':
self.postMessage('doing relax step');
relaxStep_ww(step, metric, wgtfn);
self.postMessage('done relax step');
break;
case 'stop':
self.postMessage(JSON.stringify(PatternData));
self.close(); // Terminates the worker.
break;
default:
self.postMessage('unknown command: ' + data);
};
}
else
{
PatternData = data.data;
step = data.step;
metric = data.metric;
wgtfn = data.wgtfn;
self.postMessage("data transferred "+e.data.length);
}
}, false);