-
Notifications
You must be signed in to change notification settings - Fork 1
/
fields.html
324 lines (287 loc) · 9.79 KB
/
fields.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
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>STEP Fields - WebGL2 </title>
<script type="text/javascript" src="./fields.js"></script>
<script type="text/javascript" src="./view.js"></script>
<script type="text/javascript" src="./space.js"></script>
<script type="text/javascript" src="./js/gl-matrix-min.js"></script>
<script src="./js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="../dcmio/DicomMetaDictionary.js"></script>
<script type="text/javascript" src="../dcmio/BufferStream.js"></script>
<script type="text/javascript" src="../dcmio/ValueRepresentation.js"></script>
<script type="text/javascript" src="../dcmio/Tag.js"></script>
<script type="text/javascript" src="../dcmio/DicomMessage.js"></script>
<script type="text/javascript" src="../dcmio/normalizers.js"></script>
<script src="./js/pouchdb-5.1.0.js"></script>
<script src="./js/pouchdb.memory.js"></script>
</head>
<body>
<canvas id="renderCanvas"></canvas>
<script>
'use strict'
let step = {};
let chronicle;
let deltaX = .5;
function randomPoint() {
let point = [0,0,0];
point = point.map( element=>(-.5 + Math.random())*2. );
point = [-250 + Math.random()*500.,
-450 + Math.random()*500.,
-280 + Math.random()*40.];
return(point);
}
let fiducials = [];
for (let index=0; index < 10; index++) {
fiducials.push(new Fiducial({
point: randomPoint(),
radius: 10 * Math.random(),
}));
}
let fiducialField = new FiducialField({
fiducials,
rgba: [Math.random(), Math.random(), Math.random(), Math.random()],
opacityScale: 100.,
});
////////////////////////
// -- duplicated from index.html
function readDICOM(arrayBuffer) {
let dicomData = DicomMessage.readFile(arrayBuffer);
let dataset = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
return (dataset);
}
function requestInstance(url, loaded) {
let dataRequest = new XMLHttpRequest();
dataRequest.responseType = "arraybuffer";
dataRequest.onload = function (event) {
let arrayBuffer = dataRequest.response;
loaded(arrayBuffer);
};
dataRequest.onprogress = function () {
console.log('LOADING', dataRequest.status);
};
dataRequest.open("GET", url, true);
dataRequest.send(null);
}
////////////////////////
let animationFrameRequestID;
let animate = function(onoff='on') {
if ( onoff == 'off' || onoff == false ) {
window.cancelAnimationFrame(animationFrameRequestID)
return;
}
let target = step.view.target();
target[0] += deltaX;
if (target[0] > 50 || target[0] < -50.) {deltaX *= -1;}
step.view.look({at: target});
step.space.requestRender(step.view);
animationFrameRequestID = window.requestAnimationFrame(animate);
};
let uniforms = {
pointLight: { type: '3fv', value: [10., 30., -15.] },
gradientSize: { type: '1f', value: .001 },
rayMaxSteps: { type: '1i', value: 10000 },
sampleStep: { type: '1f', value: 0.1 },
};
$(function () {
// once document is loaded...
chronicle = new PouchDB('http://quantome.org:5984/chronicle');
let onResize = function() {
renderCanvas.width = window.innerWidth;
renderCanvas.height = window.innerHeight;
if (step.space) {
step.space.requestRender(step.view);
}
}
window.onresize = onResize;
onResize();
step.space = new Space({
canvasSelector: '#renderCanvas',
uniforms,
fields: [fiducialField],
});
step.view = new View({
viewBoxMax : [250, 49, -240],
viewBoxMin : [-250, -250, -280],
viewPoint : [0., -100., -1500.],
});
step.space._render(step.view);
let ctSeriesUID = "1.2.826.0.1.3680043.2.1125.1.99742212059636314189164026308034236";
ctSeriesUID = "1.3.6.1.4.1.14519.5.2.1.2744.7002.248974378224961074547541151175"
seriesField(ctSeriesUID);
let segInstanceURL = 'http://quantome.org:5984/chronicle/1.2.276.0.7230010.3.1.4.8323329.18234.1440001306.224431/object.dcm'
requestInstance(segInstanceURL, function(arrayBuffer) {
let dataset = Normalizer.normalizeToDataset([readDICOM(arrayBuffer)]);
let segmentationField = new SegmentationField({dataset});
step.view.viewBoxMin = segmentationField.bounds.min;
step.view.viewBoxMax = segmentationField.bounds.max;
step.view.look({at: segmentationField.center});
uniforms.pointLight.value = step.view.viewPoint;
fiducials.push(new Fiducial({
point: segmentationField.bounds.min,
radius: 50.5,
}));
fiducials.push(new Fiducial({
point: segmentationField.bounds.max,
radius: 50.5,
}));
step.space.fields.push(segmentationField);
step.space.updateFields();
step.space.requestRender(step.view);
addEventListeners();
});
});
function instancesField(instanceURLs) {
var seriesDatasets = [];
instanceURLs.forEach(function(instanceURL) {
requestInstance(instanceURL, function(arrayBuffer) {
let dataset = readDICOM(arrayBuffer);
seriesDatasets.push(dataset);
if (seriesDatasets.length == instanceURLs.length) {
if (animationFrameRequestID) {
window.cancelAnimationFrame(animationFrameRequestID);
animationFrameRequestID = undefined;
}
let dataset = Normalizer.normalizeToDataset(seriesDatasets);
let imageField = new ImageField({dataset});
step.view.viewBoxMin = imageField.bounds.min;
step.view.viewBoxMax = imageField.bounds.max;
step.view.look({at: imageField.center});
uniforms.pointLight.value = step.view.viewPoint;
fiducials.push(new Fiducial({
point: imageField.bounds.min,
radius: 80.5,
}));
fiducials.push(new Fiducial({
point: imageField.bounds.max,
radius: 80.5,
}));
console.log(imageField);
step.space.fields.push(imageField);
step.space.updateFields();
step.space.requestRender(step.view);
addEventListeners();
}
});
});
}
function seriesField(seriesUID) {
chronicle.query("instances/seriesInstances", {
reduce : false,
stale : 'update_after',
key : seriesUID,
}).then(function(data) {
let instanceURLs = [];
data.rows.forEach(row => {
instanceURLs.push(chronicle._db_name + "/" + row.value[1] + '/object.dcm');
});
instancesField(instanceURLs);
}).catch(function (err) {
console.error(err);
});
}
// TODO: currently the last field added, but should
// come from a UI selection of the active tool and target image
function selectedImageField() {
let fields = step.space.fields;
let imageField = fields[fields.length-1];
return (imageField);
}
let startPoint = undefined;
let startWindow = undefined;
function onMouseEvent(mouseEvent) {
let point = [-1. + (2. * mouseEvent.clientX / step.space.canvas.width),
-1. + (2. * mouseEvent.clientY / step.space.canvas.height)];
switch (mouseEvent.type) {
case 'mousedown': {
startPoint = point.slice();
let imageField = selectedImageField();
startWindow = [imageField.windowWidth, imageField.windowCenter];
}
break;
case 'mousemove': {
if (mouseEvent.buttons == 1) {
let delta = [0,1].map(e=>point[e]-startPoint[e]);
let imageField = selectedImageField();
// TODO: figure out a good way to automatically determine the gain
imageField.windowWidth = startWindow[0] + delta[0] * 500.;
imageField.windowWidth = Math.max(imageField.windowWidth, 0.);
imageField.windowCenter = startWindow[1] + delta[1] * 500.;
step.space.requestRender(step.view);
}
}
break;
case 'mouseup': {
startPoint = undefined;
}
}
}
function addEventListeners() {
step.space.canvas.addEventListener('mousedown', onMouseEvent, {passive:true});
step.space.canvas.addEventListener('mousemove', onMouseEvent, {passive:true});
step.space.canvas.addEventListener('mouseup', onMouseEvent, {passive:true});
step.space.canvas.addEventListener('mousewheel', wheelEvent=>{
let delta = .1;
if (wheelEvent.wheelDelta < 0) {
delta *= -1;
}
step.view.viewPoint[2] += delta;
step.view.look({from: step.view.viewPoint});
step.space.requestRender(step.view);
}, {passive:true});
window.addEventListener('keydown', keyboardEvent=>{
switch (keyboardEvent.key) {
case "ArrowUp": {
step.view.orbit(0, 1);
step.space.requestRender(step.view);
}
break;
case "ArrowRight": {
step.view.orbit(1, 0);
step.space.requestRender(step.view);
}
break;
case "ArrowLeft": {
step.view.orbit(-1, 0);
step.space.requestRender(step.view);
}
break;
case "ArrowDown": {
step.view.orbit(0, -1);
step.space.requestRender(step.view);
}
break;
case "a": {
step.view.slice({plane: "axial", offset: 0.5, thickness: 1});
step.space.requestRender(step.view);
}
break;
case "s": {
step.view.slice({plane: "sagittal", offset: 0.5, thickness: 1});
step.space.requestRender(step.view);
}
break;
case "c": {
step.view.slice({plane: "coronal", offset: 0.5, thickness: 1});
step.space.requestRender(step.view);
}
break;
case "v": {
step.view.look({viewNear: 0, viewFar: 3e38});
step.space.requestRender(step.view);
}
break;
}
}, {passive: true});
}
</script>
<p>
This demo uses <a href='https://www.khronos.org/registry/webgl/specs/latest/2.0/'>WebGL2</a>. Not all devices and browsers are supported. As of this writing only the latest Chrome Canary or Firefox Nightly will work. <a href='https://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation'>See this page for details.</a>
</p>
<p>
For windows be sure to turn off ANGLE and use your native OpenGL. <a href='https://github.com/mrdoob/three.js/wiki/How-to-use-OpenGL-or-ANGLE-rendering-on-Windows'>See this page for more info</a>
</p>
</body>
</html>