-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
356 lines (308 loc) · 11.8 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<style>
.row {
display: flex;
flex-direction: row;
align-items: center;
}
.inset-row {
display: flex;
flex-direction: row;
margin-left: 20px;
align-items: center;
}
.col {
display: flex;
flex-direction: column;
}
#dragAndDrop {
min-height: 200px;
white-space: pre-line;
border: 1px dashed black;
border-radius: 10px;
}
#status_span {
margin-left: 25px;
}
#info {
width:100%
display: -webkit-flex;
display: flex;
}
#simplify_box {
height: 20vh;
overflow: auto;
border: 1px solid black;
}
#note_box {
margin-right: 30px;
}
#download_container {
display: flex;
justify-content: center;
}
#download {
width:50%;
height:40px;
margin:0 auto;
margin-top:10px;
cursor: pointer;
border: 0px;
border-radius: 10px;
background: rgb(240, 240, 240);
}
.download_ready_signal {
animation: download_ready_signal 2s linear infinite;
}
@keyframes download_ready_signal {
50% {
opacity: 0.1;
color: green;
width: 60%;
}
}
button {
cursor: pointer;
}
</style>
<script type="text/javascript">
`use strict`;
var worker = new Worker('worker.js');
worker.onmessage = function(e) {
const log = e.data.log;
if (log !== undefined) {
var logger = document.getElementById('simplify_log');
logger.innerHTML += '<li>' + log + '</li>';
var box = document.getElementById('simplify_box');
box.scrollTop = box.scrollHeight;
return;
}
const file = e.data.blob;
if (file !== undefined) {
const s_name = get_meshname(SIMPLIFY_FILE.name); //SIMPLIFY_FILE.simplify_name;
let url = window.URL.createObjectURL(file);
var download = document.getElementById('download_a');
download.href = url;
download.download = s_name;
var download_button = document.getElementById('download');
download_button.innerHTML = "Click to download "+"<br>" +s_name;
download_button.disabled = false;
download_button.className = "download_ready_signal";
setTimeout(function (){
download_button.className = "";
}, 2000);
put_status("Ready to download");
return;
}
console.error("Unknown Message from WebWorker", e.data);
}
worker.onerror = function(e) {
console.log(e);
console.log(
'ERROR: Line ', e.lineno, ' in ', e.filename, ': ', e.message
);
}
function get_meshname(filename) { //simplify_name
var e = document.getElementById("meshFormat");
var meshExt = e.options[e.selectedIndex].value;
const extension = filename.toLowerCase().slice(filename.lastIndexOf(".")+1, filename.length);
if (extension==="gz") {
return "nii2mesh_"+filename.split('.').slice(0, -1).join('.').split('.').slice(0, -1).join('.')+"."+meshExt
}
return "nii2mesh_"+filename.split('.').slice(0, -1).join('.')+"."+meshExt
}
let SIMPLIFY_FILE = {
'blob': undefined,
get name(){
if (this.exists)
return this.blob.name;
},
get size() {
if (this.exists)
return this.blob.size;
},
get exists() {
if (typeof this.blob !== 'undefined')
return true;
else {
return false;
}
}
}
function update_simplify_to() {
const slider_value = get_value_from_slider('slider');
document.getElementById('percentage').innerHTML = slider_value;
if (SIMPLIFY_FILE.exists) {
document.getElementById('size').innerHTML = Math.ceil(slider_value * SIMPLIFY_FILE.size/(100*1024*1024));
}
}
function update_postSmooth_to() {
const slider_value = get_value_from_slider('postSmooth');
document.getElementById('postSmoothVal').innerHTML = slider_value;
}
function getFloat(id) {
let str = document.getElementById(id).value;
return parseFloat(str.replace(',','.').replace(' ','')) //handle comma decimal separator
}
function customIsoHandler() {
let isoSelect = document.getElementById('isoDarkMediumBright123')
let custom = document.getElementById('customIsoLevel')
let customIsoLevel = document.getElementById('customIsoLevel')
if (isoSelect.value === "4") {
customIsoLevel.style.display = "block"
} else {
customIsoLevel.style.display = "none"
}
}
function onlyNumberKey(evt) {
// Only ASCII character in that range allowed 46= period, 44 = comma
var ASCIICode = (evt.which) ? evt.which : evt.keyCode
if (ASCIICode > 31 && (ASCIICode < 44 || ASCIICode > 57))
return false;
if (ASCIICode === 47) // "/"
return false;
return true;
}
function get_value_from_slider(id) {
return document.getElementById(id).valueAsNumber;
}
function uploaded(file) {
if (file === undefined) { // via upload button
var uploadform = document.getElementById("fileuploadform");
file = uploadform.files[0];
// this helps to force trigger even if user upload the same file
// https://stackoverflow.com/a/12102992/5260518
uploadform.value = null;
}
if (file === undefined) // not via upload button defined otherwise
file = SIMPLIFY_FILE.blob;
var uploadform = document.getElementById("fileuploadform");
console.log(uploadform);
uploadform.files[0] = file;
SIMPLIFY_FILE.blob = file;
check_file(post_to_worker);
}
function check_file(success_cb) {
put_status("Checking file");
const filename = SIMPLIFY_FILE.name;
const extension = filename.toLowerCase().slice(filename.lastIndexOf(".")+1, filename.length);
if (extension!=="nii" && extension!=="gz") {
put_status("Please upload an NIfTI (.nii; .nii.gz) file not "+ extension);
return;
}
success_cb();
}
function post_to_worker() {
update_simplify_to();
put_status("Simplifying by your browser...See log below");
var logger = document.getElementById('simplify_log');
logger.innerHTML = ''; //clear old messages
worker.postMessage(
{"blob":SIMPLIFY_FILE.blob,
"percentage": get_value_from_slider('slider')/100,
"quality": 1,
"isolevel": getFloat('customIsoLevel'),
"isoDarkMediumBright123": document.getElementById('isoDarkMediumBright123').value,
"preSmooth": document.getElementById('preSmooth').checked,
"onlyLargest": document.getElementById('onlyLargest').checked,
"fillBubbles": document.getElementById('fillBubbles').checked,
"postSmooth": get_value_from_slider('postSmooth'),
"simplify_name": get_meshname(SIMPLIFY_FILE.name)
}
);
}
function dodrop(event) {
var dt = event.dataTransfer;
var file = dt.files[0];
var filename = file.name;
uploaded(file);
}
function put_status(text)
{
document.getElementById("status").textContent = text;
}
function init() {
update_simplify_to();
customIsoHandler();
}
window.onload = init;
</script>
<body>
<h3>NIfTI to Mesh conversion <a href="https://rordenlab.github.io/nii2meshWeb/bet.nii.gz">(you can get a sample NIfTI brain here)</a></h3>
<div id="dragAndDrop"
ondragenter="event.stopPropagation(); event.preventDefault();"
ondragover="event.stopPropagation(); event.preventDefault();"
ondrop="event.stopPropagation(); event.preventDefault();
dodrop(event);">
<ol>
<li>Drag and drop NIfTI file here or <input type="file" onChange="uploaded()" id="fileuploadform"/></li>
<li><a href="https://github.com/neurolabusc/nii2mesh">Options:</a>
<div class="col">
<div class="inset-row">
<p>Isolevel </p>
<select id="isoDarkMediumBright123" name="Isolevel" onChange="customIsoHandler()">
<option value="1">Dark</option>
<option value="2" selected>Medium</option>
<option value="3">Bright</option>
<option value="4">Custom</option>
</select>
<input type="text" id="customIsoLevel" value="0.5" onkeypress="return onlyNumberKey(event)"><br>
</div>
<div class="inset-row">
<p>Pre-smooth </p>
<input type="checkbox" name="preSmooth" id="preSmooth" checked>
</div>
<div class="inset-row">
<p>Only retain largest single cluster </p>
<input type="checkbox" name="onlyLargest" id="onlyLargest">
</div>
<div class="inset-row">
<p>Fill internal bubbles </p>
<input type="checkbox" name="fillBubbles" id="fillBubbles" >
</div>
<div class="inset-row">
<p>Mesh reduction percent</p>
<input type="range" min="1" max="100" value="25" id="slider" step="1" oninput="update_simplify_to()"> <span id="percentage">25</span>% NIfTI mb: <span id="size">??</span>
</div>
<div class="inset-row">
<p>Post-smoothing iterations </p>
<input type="range" min="0" max="4096" step="1" value="0" id="postSmooth" oninput="update_postSmooth_to()">
<p id="postSmoothVal"></p>
</div>
<div class="inset-row">
<p>Mesh format </p>
<select id="meshFormat" name="Mesh format">
<option value="obj">obj</option>
<option value="gii">GIfTI</option>
<option value="mz3">mz3</option>
<option value="ply">ply</option>
<option value="stl">stl</option>
<option value="vtk">vtk</option>
</select>
</div>
</div>
</li>
<li>Click <button onclick="uploaded()">Convert to Mesh</button></li>
</ol>
<span id="status_span">Status: <span id="status">Waiting for upload</span>
</div>
<div id="download_container">
<a id="download_a" hidden></a>
<button id="download" disabled onclick="document.getElementById('download_a').click()">Nothing to download yet...</button>
</div>
<div>
<h3>Simplification Process Log</h3>
<div id="simplify_box">
<ul id="simplify_log">
</ul>
</div>
</div>
</div>
<div id="log"></div>
<a href="https://github.com/rordenlab/nii2meshWeb"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a>
</body>
</html>