/
xtk_lesson10.js
187 lines (154 loc) · 5.16 KB
/
xtk_lesson10.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
/* globals Stats*/
// XTK imports
import XRenderer3D from '../../src/helpers/x/helpers.x.renderer3d';
// ...
// GOGOGO
let xRenderer3D = new XRenderer3D();
xRenderer3D._update();
// all the code below is a THREEJS/AMI mix that should be removed
import ControlsTrackball from '../../src/controls/controls.trackball';
import HelpersStack from '../../src/helpers/helpers.stack';
import LoadersVolume from '../../src/loaders/loaders.volume';
// standard global variables
let controls, renderer, stats, scene, camera, stackHelper, threeD;
function init() {
// this function is executed on each animation frame
function animate() {
// if (stackHelper) {
// stackHelper.index += 1;
// if (stackHelper.outOfBounds === true) {
// stackHelper.orientation = (stackHelper.orientation + 1) % 3;
// stackHelper.index = 0;
// }
// }
controls.update();
renderer.render(scene, camera);
stats.update();
// request new frame
requestAnimationFrame(function() {
animate();
});
}
// renderer
threeD = document.getElementById('r3d');
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(threeD.offsetWidth, threeD.offsetHeight);
renderer.setClearColor(0x673AB7, 1);
renderer.setPixelRatio(window.devicePixelRatio);
threeD.appendChild(renderer.domElement);
// stats
stats = new Stats();
threeD.appendChild(stats.domElement);
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera(45, threeD.offsetWidth / threeD.offsetHeight, 1, 10000000);
camera.position.x = 250;
camera.position.y = 250;
camera.position.z = 250;
// light
// var dirLight = new THREE.DirectionalLight( 0xffffff );
// dirLight.position.set( 200, 200, 1000 ).normalize();
// camera.add( dirLight );
// camera.add( dirLight.target );
// let particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
// scene.add( particleLight );
scene.add( new THREE.AmbientLight( 0x353535 ) );
let directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
directionalLight.position.set( 200, 200, 1000 ).normalize();
scene.add( directionalLight );
let directionalLight2 = new THREE.DirectionalLight( 0xffffff, 1 );
directionalLight2.position.set( -200, -200, -1000 ).normalize();
scene.add( directionalLight2 );
// controls
controls = new ControlsTrackball(camera, threeD);
controls.rotateSpeed = 1.4;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
animate();
}
window.onload = function() {
// init threeJS...
init();
// load vtk file
var loader1 = new THREE.VTKLoader();
loader1.load( 'https://cdn.rawgit.com/FNNDSC/data/master/vtk/marc_avf/avf.vtk', function ( geometry ) {
geometry.computeVertexNormals();
var material = new THREE.MeshLambertMaterial( {
shading: THREE.SmoothShading,
color: 0xE91E63,
side: THREE.DoubleSide} );
var mesh = new THREE.Mesh( geometry, material );
var RASToLPS = new THREE.Matrix4();
RASToLPS.set(-1, 0, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
mesh.applyMatrix(RASToLPS);
scene.add( mesh );
} );
// instantiate the loader
// it loads and parses the dicom image
let loader = new LoadersVolume(threeD);
var t2 = [
'avf_float_32.nii.gz'
];
var files = t2.map(function(v) {
return 'https://cdn.rawgit.com/FNNDSC/data/master/nifti/marc_avf/' + v;
});
// load sequence for each file
let seriesContainer = [];
let loadSequence = [];
files.forEach(function(url) {
loadSequence.push(
Promise.resolve()
// fetch the file
.then(function() {
return loader.fetch(url);
})
.then(function(data) {
return loader.parse(data);
})
.then(function(series) {
seriesContainer.push(series);
})
.catch(function(error) {
window.console.log('oops... something went wrong...');
window.console.log(error);
})
);
});
// load sequence for all files
Promise
.all(loadSequence)
.then(function() {
loader.free();
loader = null;
// make a proper function for this guy...
let series = seriesContainer[0].mergeSeries(seriesContainer)[0];
let stack = series.stack[0];
stackHelper = new HelpersStack(stack);
stackHelper.bbox.color = 0xF9F9F9;
stackHelper.border.color = 0xF9F9F9;
scene.add(stackHelper);
window.console.log(stackHelper.stack.minMax);
window.console.log(stackHelper);
// update camrea's and control's target
let centerLPS = stackHelper.stack.worldCenter();
camera.lookAt(centerLPS.x, centerLPS.y, centerLPS.z);
camera.updateProjectionMatrix();
controls.target.set(centerLPS.x, centerLPS.y, centerLPS.z);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize, false);
})
.catch(function(error) {
window.console.log('oops... something went wrong...');
window.console.log(error);
});
};