-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmri-volume-slice.js
283 lines (245 loc) · 9.4 KB
/
mri-volume-slice.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
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
/* global document, ImageData, Uint8ClampedArray */
class MRISlice {
constructor(nifti) {
this._createCanvases();
this.loadNewNifti(nifti);
this.useCrosshairs = true;
this.lastEvent = null;
this.scrollDistance = 1;
this.currentImageData = {};
}
loadNewNifti(nifti) {
if (nifti) {
this._setupNifti(nifti);
this._setCanvasWidthHeight(this.size);
this._drawCrossHairs(this.currentView);
}
}
getZViewSlice(slice) {
const sliceIndex = get1DIndex(0, 0, slice, this.size);
const sliceLength = this.size.x * this.size.y;
const sliceXY = this.volume.slice(sliceIndex, sliceIndex + sliceLength);
return grayScaleToRGBA(sliceXY, this.size.x, this.size.y);
}
getYViewSlice(slice) {
const sliceXZ = [];
const rowLength = this.size.x;
for (let zIndex = this.size.z - 1; zIndex >= 0; zIndex--) {
const rowIndex = get1DIndex(0, slice, zIndex, this.size);
sliceXZ.push(...this.volume.slice(rowIndex, rowIndex + rowLength));
}
return grayScaleToRGBA(sliceXZ, this.size.x, this.size.z);
}
getXViewSlice(slice) {
const sliceYZ = [];
// The order of these nested loops is important
for (let yIndex = 0; yIndex < this.size.y; yIndex++) {
for (let zIndex = 0; zIndex < this.size.z; zIndex++) {
sliceYZ.push(this.volume[get1DIndex(slice, yIndex, zIndex, this.size)]);
}
}
return grayScaleToRGBA(sliceYZ, this.size.z, this.size.y);
}
_createCanvases() {
this.canvases = {
x: document.createElement('canvas'),
y: document.createElement('canvas'),
z: document.createElement('canvas'),
};
this.contexts = {
x: this.canvases.x.getContext('2d'),
y: this.canvases.y.getContext('2d'),
z: this.canvases.z.getContext('2d'),
};
}
_setCanvasWidthHeight(size) {
this.canvases.x.width = size.z;
this.canvases.x.height = size.y;
this.canvases.y.width = size.x;
this.canvases.y.height = size.z;
this.canvases.z.width = size.x;
this.canvases.z.height = size.y;
this.currentImageData.x = this.getXViewSlice(this.currentView.x);
this.currentImageData.y = this.getYViewSlice(this.currentView.y);
this.currentImageData.z = this.getZViewSlice(this.currentView.z);
Object.entries(this.contexts).forEach(([axis, canvas]) => {
const imageData = this.currentImageData[axis];
canvas.putImageData(imageData, 0, 0);
});
}
_setupNifti(nifti) {
this.nifti = nifti;
this.size = {
x: nifti.sizes[0],
y: nifti.sizes[1],
z: nifti.sizes[2],
};
const midAxis = nifti.sizes.map(size => Math.round(size / 2));
this.currentView = {
x: midAxis[0],
y: midAxis[1],
z: midAxis[2],
};
const theMax = nifti.data.reduce((accumulate, item) => {
accumulate = item > accumulate ? item : accumulate; // eslint-disable-line no-param-reassign
return accumulate;
}, 0);
const theMin = nifti.data.reduce((accumulate, item) => {
accumulate = item < accumulate ? item : accumulate; // eslint-disable-line no-param-reassign
return accumulate;
}, 0);
this.volume = new Uint8ClampedArray(
nifti.data.map(item => Math.round((item - theMin) * 255 / (theMax - theMin))),
);
}
mouseNavigationEnabled(isEnabled) {
if (isEnabled) {
this.mouseDown = false;
document.body.addEventListener('mousedown', () => this.mouseDown = true); // eslint-disable-line no-return-assign
document.body.addEventListener('mouseup', () => this.mouseDown = false); // eslint-disable-line no-return-assign
Object.values(this.canvases).forEach((canvas) => {
canvas.addEventListener('mousedown', event => this.updateCanvases(event));
canvas.addEventListener('mousemove', (event) => {
if (this.mouseDown) this.updateCanvases(event);
});
canvas.addEventListener('wheel', (event) => {
event.preventDefault();
const fakeEvent = {
target: event.target,
type: 'wheel',
deltaY: 0,
};
if (event.deltaY > 0) { // scrolling down
fakeEvent.deltaY = this.scrollDistance;
} else if (event.deltaY < 0) { // scrolling up
fakeEvent.deltaY = -this.scrollDistance;
}
this.updateCanvases(fakeEvent);
});
});
} else {
// TODO remove event listeners if
}
}
updateCanvases(event) {
this.lastEvent = event;
const horizontalCoor = event.x - event.target.offsetLeft;
const verticalCoor = event.y - event.target.offsetTop + document.documentElement.scrollTop;
const isXCanvas = event.target === this.canvases.x;
const isYCanvas = event.target === this.canvases.y;
// const isZCanvas = event.target === this.canvases.z;
if (isXCanvas) {
const yViewNeedsUpdating = verticalCoor !== this.currentView.y;
const zViewNeedsUpdating = horizontalCoor !== this.currentView.z;
const xViewNeedsUpdating = event.type === 'wheel';
if (xViewNeedsUpdating) {
this._updateCurrentView({ x: this.currentView.x + event.deltaY });
this.currentImageData.x = this.getXViewSlice(this.currentView.x);
} else {
if (yViewNeedsUpdating) {
this.currentView.y = verticalCoor;
this.currentImageData.y = this.getYViewSlice(verticalCoor);
}
if (zViewNeedsUpdating) {
this.currentView.z = horizontalCoor;
this.currentImageData.z = this.getZViewSlice(horizontalCoor);
}
}
} else if (isYCanvas) {
const xViewNeedsUpdating = horizontalCoor !== this.currentView.x;
const zViewNeedsUpdating = verticalCoor !== this.currentView.z;
const yViewNeedsUpdating = event.type === 'wheel';
if (yViewNeedsUpdating) {
this._updateCurrentView({ y: this.currentView.y + event.deltaY });
this.currentImageData.y = this.getYViewSlice(this.currentView.y);
} else {
if (xViewNeedsUpdating) {
this.currentView.x = horizontalCoor;
this.currentImageData.x = this.getXViewSlice(horizontalCoor);
}
if (zViewNeedsUpdating) {
this.currentView.z = this.size.z - verticalCoor;
this.currentImageData.z = this.getZViewSlice(this.size.z - verticalCoor);
}
}
} else {
const xViewNeedsUpdating = horizontalCoor !== this.currentView.x;
const yViewNeedsUpdating = verticalCoor !== this.currentView.y;
const zViewNeedsUpdating = event.type === 'wheel';
if (zViewNeedsUpdating) {
this._updateCurrentView({ z: this.currentView.z + event.deltaY });
this.currentImageData.z = this.getZViewSlice(this.currentView.z);
} else {
if (xViewNeedsUpdating) {
this.currentView.x = horizontalCoor;
this.currentImageData.x = this.getXViewSlice(horizontalCoor);
}
if (yViewNeedsUpdating) {
this.currentView.y = verticalCoor;
this.currentImageData.y = this.getYViewSlice(verticalCoor);
}
}
}
this._drawCrossHairs(this.currentView);
}
showCrosshairs() {
this.useCrosshairs = true;
this._drawCrossHairs(this.currentView);
}
hideCrossHairs() {
this.useCrosshairs = false;
this._drawCrossHairs(this.currentView);
}
_drawCrossHairs(viewCoors) {
this.contexts.x.putImageData(this.currentImageData.x, 0, 0);
this.contexts.y.putImageData(this.currentImageData.y, 0, 0);
this.contexts.z.putImageData(this.currentImageData.z, 0, 0);
const transparent = 'rgba(255, 255, 255, 0';
const xHairColor = 'yellow';
const yHairColor = 'cyan';
const zHairColor = 'magenta';
this.contexts.z.fillStyle = this.useCrosshairs ? xHairColor : transparent;
this.contexts.y.fillStyle = this.useCrosshairs ? xHairColor : transparent;
this.contexts.z.fillRect(viewCoors.x, 0, 1, this.size.y);
this.contexts.y.fillRect(viewCoors.x, 0, 1, this.size.z);
this.contexts.z.fillStyle = this.useCrosshairs ? yHairColor : transparent;
this.contexts.x.fillStyle = this.useCrosshairs ? yHairColor : transparent;
this.contexts.z.fillRect(0, viewCoors.y, this.size.x, 1);
this.contexts.x.fillRect(0, viewCoors.y, this.size.z, 1);
this.contexts.x.fillStyle = this.useCrosshairs ? zHairColor : transparent;
this.contexts.y.fillStyle = this.useCrosshairs ? zHairColor : transparent;
this.contexts.x.fillRect(viewCoors.z, 0, 1, this.size.y);
this.contexts.y.fillRect(0, this.size.z - viewCoors.z, this.size.x, 1);
}
_updateCurrentView({ x, y, z }) {
if (x && x >= 0 && x < this.size.x) {
this.currentView.x = x;
}
if (y && y >= 0 && y < this.size.y) {
this.currentView.y = y;
}
if (z && z >= 0 && z < this.size.z) {
this.currentView.z = z;
}
}
}
function grayScaleToRGBA(sliceData, width, height) {
const rGBASliceData = sliceData.reduce((accumulate, colourChannels) => {
const alpha = 255;
accumulate.push(colourChannels, colourChannels, colourChannels, alpha);
return accumulate;
}, []);
return new ImageData(new Uint8ClampedArray(rGBASliceData), width, height);
}
function get1DIndex(x, y, z, sizes) {
return x + y * sizes.x + z * sizes.x * sizes.y;
}
// function get3DIndex(index, size) {
// const zChunkLength = size.x * size.y;
// const yChunkLength = size.y;
// const z = Math.floor(index / zChunkLength);
// const y = Math.floor((index - z) / yChunkLength);
// const x = index - z - y;
// return [x, y, z];
// }
export default MRISlice;