-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
219 lines (192 loc) · 7.12 KB
/
index.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
let scale = 2;
let previewNthCount = 1;
let pickedColor = '#000000';
let pickedThickness = 1;
let showGridLoadedImg = true;
let forceGridLines = false;
const R = 0xFF0000;
const G = 0x00FF00;
const B = 0x0000FF;
const fakeCanvas = document.createElement('canvas');
let EMULATION_RUNNING = false;
let STOP_EMULATION = false;
// DEFAULT IMG.
let imgIn = [
[G, G, R, G, G],
[G, R, R, R, G],
[G, R, B, R, R],
[G, R, R, R, G],
[G, G, R, G, G],
];
async function paintIterativeProbabilityPatternFullyDeductive({ windows }, { W, H }, USE_WEIGHTED_PICK = true) {
return wfc_paintIterativeProbabilityPatternFullyDeductive({ windows }, { W, H }, USE_WEIGHTED_PICK, false);
}
async function paintIterativeProbabilityPatternFullyDeductivePaintPredictions({ windows }, { W, H }, USE_WEIGHTED_PICK = true) {
return wfc_paintIterativeProbabilityPatternFullyDeductive({ windows }, { W, H }, USE_WEIGHTED_PICK, true);
}
async function paintIterativeSnakeLinePattern({ windows }, { W, H }, WEIGHTED_PICK) {
return wfc_paintIterativePattern({ windows }, { W, H }, true, false, WEIGHTED_PICK);
}
async function paintIterativeSnakeLinePatternGreedy({ windows }, { W, H }, WEIGHTED_PICK) {
return wfc_paintIterativePattern({ windows }, { W, H }, true, true, WEIGHTED_PICK);
}
async function paintIterativeScanLinePattern({ windows }, { W, H }, WEIGHTED_PICK) {
return wfc_paintIterativePattern({ windows }, { W, H }, false, false, WEIGHTED_PICK);
}
async function paintIterativeScanLinePatternGreedy({ windows }, { W, H }, WEIGHTED_PICK) {
return wfc_paintIterativePattern({ windows }, { W, H }, false, true, WEIGHTED_PICK);
}
async function paintIterativeProbabilityPattern({ windows }, { W, H }, WEIGHTED_PICK) {
return wfc_paintIterativeProbabilityPattern({ windows }, { W, H }, false, true, WEIGHTED_PICK)
}
async function paintIterativeProbabilityPatternRecursive({ windows }, { W, H }, WEIGHTED_PICK) {
return wfc_paintIterativeProbabilityPatternRecursive({ windows }, { W, H }, false, true, WEIGHTED_PICK);
}
function update() {
window.lookupsCache = null;
preview();
}
function preview() {
previewNthCount = +update_preview_nth.value;
scale = +preview_scale.value;
const WIN_SIZE = +input_window_size.value;
let img = {
mat: imgIn,
W: imgIn[0].length,
H: imgIn.length
}
drawInputCanvas(img);
drawInputCanvas(img, fakeCanvas, 1);
const windows = analyzeInput(img, WIN_SIZE);
drawAugmentationCanvas(windows);
const outPixelW = +out_window_width_size.value;
const outPixelH = +out_window_height_size.value;
disp_canvas.width = outPixelW * WIN_SIZE * scale;
disp_canvas.height = outPixelH * WIN_SIZE * scale;
}
function getAlgorithm() {
switch (algo_selection.value) {
case 'lineScan': return paintIterativeScanLinePattern;
case 'lineScanGreed': return paintIterativeScanLinePatternGreedy;
case 'lineSnakeScan': return paintIterativeSnakeLinePattern;
case 'lineSnakeScanGreed': return paintIterativeSnakeLinePatternGreedy;
case 'neighborIter': return paintIterativeProbabilityPattern;
case 'neighborRecurse': return paintIterativeProbabilityPatternRecursive;
case 'fullyDeductiveNeighbor': return paintIterativeProbabilityPatternFullyDeductive;
case 'fullyDeductiveNeighborPaintPredictions': return paintIterativeProbabilityPatternFullyDeductivePaintPredictions;
default: return () => { alert('unknown algo'); throw new Error('not implemented'); };
}
}
async function run() {
if (!EMULATION_RUNNING) {
EMULATION_RUNNING = true;
} else {
STOP_EMULATION = true;
return;
}
const USE_WEIGHTED = weighted_checkbox.checked;
try {
var prevValue = run_btn.innerText;
run_btn.innerText = 'preparing algorithm, this may take a while...';
run_btn.disabled = true;
await sleepMs(100);
const WIN_SIZE = +input_window_size.value;
let img = imgInToImg(imgIn);
drawInputCanvas(img);
if (!window.lookupsCache) {
window.lookupsCache = {};
const windows = analyzeInput(img, WIN_SIZE);
drawAugmentationCanvas(windows);
window.lookupsCache.lookups = constructConcatenationLookups(windows);
} else {
window.lookupsCache.lookups.windows = shuffle(window.lookupsCache.lookups.windows);
}
} finally {
run_btn.innerText = prevValue;
run_btn.disabled = false;
await sleepMs(100);
}
const { lookups } = window.lookupsCache;
const outPixelW = +out_window_width_size.value;
const outPixelH = +out_window_height_size.value;
try {
const algo = getAlgorithm();
try {
var prevValue = run_btn.innerText;
run_btn.innerText = 'Running... (click to stop)';
await algo(lookups, { W: outPixelW, H: outPixelH }, USE_WEIGHTED);
} finally {
run_btn.innerText = prevValue;
}
} finally {
STOP_EMULATION = false;
EMULATION_RUNNING = false;
}
}
function main() {
thickness_input.value = pickedThickness;
colorpicker.value = pickedColor;
update_preview_nth.value = previewNthCount = 1;
let mouseIsDown = false;
const fakeCtx = fakeCanvas.getContext('2d');
function onCanvasActionComplete() {
imgIn = canvasToImgIn(fakeCanvas);
update(true);
}
function colorCanvas(e, preview = false) {
const ctx = input_canvas.getContext('2d');
const { x, y } = getMousePos(input_canvas, e);
const thickness = pickedThickness;
const cellX = Math.floor(x / scale);
const cellY = Math.floor(y / scale);
ctx.fillStyle = pickedColor;
fakeCtx.fillStyle = pickedColor;
ctx.fillRect(cellX * scale, cellY * scale, scale * thickness, scale * thickness);
if (!preview) {
fakeCtx.fillRect(cellX, cellY, 1 * thickness, 1 * thickness);
}
const img = imgInToImg(imgIn);
if (forceGridLines) {
drawGrid(img);
}
if (preview) {
drawInputCanvas(img);
ctx.fillStyle = pickedColor;
ctx.fillRect(cellX * scale, cellY * scale, scale * thickness, scale * thickness);
}
}
input_canvas.onmouseenter = () => {
forceGridLines = showGridLoadedImg;
}
input_canvas.onmouseout = () => {
forceGridLines = false;
if (mouseIsDown) {
onCanvasActionComplete()
mouseIsDown = false;
} else {
const img = imgInToImg(imgIn);
drawInputCanvas(img);
// update(true);
}
}
input_canvas.onmousedown = (e) => {
mouseIsDown = true;
if (EMULATION_RUNNING) {
STOP_EMULATION = true;
}
colorCanvas(e);
}
input_canvas.onmouseup = (e) => {
if (mouseIsDown) {
colorCanvas(e);
onCanvasActionComplete();
}
mouseIsDown = false;
}
input_canvas.onmousemove = (e) => {
if (!mouseIsDown) return colorCanvas(e, true);
colorCanvas(e);
return false;
}
update();
}