Mercurial > hg > orthanc-stl
comparison Sources/viewer.js @ 0:4e889a8e8be2
initial commit of the viewer plugin
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 04 Jul 2023 19:15:27 +0200 |
parents | |
children | 0f03a8a0bd6f |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4e889a8e8be2 |
---|---|
1 /** | |
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium | |
3 * SPDX-License-Identifier: GPL-3.0-or-later | |
4 */ | |
5 | |
6 /** | |
7 * STL plugin for Orthanc | |
8 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium | |
9 * | |
10 * This program is free software: you can redistribute it and/or | |
11 * modify it under the terms of the GNU General Public License as | |
12 * published by the Free Software Foundation, either version 3 of the | |
13 * License, or (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, but | |
16 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 * General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 **/ | |
23 | |
24 | |
25 import * as THREE from 'three'; | |
26 | |
27 import { OrbitControls } from './OrbitControls.js'; | |
28 import { STLLoader } from './STLLoader.js'; | |
29 | |
30 | |
31 // http://stackoverflow.com/a/21903119/881731 | |
32 function GetUrlParameter(sParam) | |
33 { | |
34 var sPageURL = decodeURIComponent(window.location.search.substring(1)); | |
35 var sURLVariables = sPageURL.split('&'); | |
36 | |
37 for (var i = 0; i < sURLVariables.length; i++) { | |
38 var sParameterName = sURLVariables[i].split('='); | |
39 | |
40 if (sParameterName[0] === sParam) { | |
41 return sParameterName[1] === undefined ? '' : sParameterName[1]; | |
42 } | |
43 } | |
44 | |
45 return ''; | |
46 }; | |
47 | |
48 var instanceId = GetUrlParameter('instance'); | |
49 | |
50 | |
51 const scene = new THREE.Scene(); | |
52 | |
53 const renderer = new THREE.WebGLRenderer(); | |
54 renderer.setSize(window.innerWidth, window.innerHeight); | |
55 document.body.appendChild(renderer.domElement); | |
56 | |
57 const material = new THREE.MeshPhongMaterial(); | |
58 | |
59 const light = new THREE.AmbientLight(0x555555); | |
60 scene.add(light); | |
61 | |
62 const light2 = new THREE.PointLight(0xaaaaaa, 2); | |
63 light2.position.set(10, 10, 10); | |
64 scene.add(light2); | |
65 | |
66 const loader = new STLLoader() | |
67 loader.load( | |
68 '../instances/' + instanceId + '/content/0042-0011', | |
69 function (geometry) { | |
70 const frustumSize = 200; | |
71 | |
72 geometry.computeBoundingBox(); | |
73 geometry.translate(-(geometry.boundingBox.min.x + geometry.boundingBox.max.x) / 2.0, | |
74 -(geometry.boundingBox.min.y + geometry.boundingBox.max.y) / 2.0, | |
75 -(geometry.boundingBox.min.z + geometry.boundingBox.max.z) / 2.0); | |
76 geometry.scale((frustumSize / 2.0) / (geometry.boundingBox.max.x - geometry.boundingBox.min.x), | |
77 (frustumSize / 2.0) / (geometry.boundingBox.max.y - geometry.boundingBox.min.y), | |
78 (frustumSize / 2.0) / (geometry.boundingBox.max.z - geometry.boundingBox.min.z)); | |
79 | |
80 const mesh = new THREE.Mesh(geometry, material); | |
81 scene.add(mesh); | |
82 | |
83 const aspect = window.innerWidth / window.innerHeight; | |
84 const camera = new THREE.OrthographicCamera( | |
85 frustumSize * aspect / - 2, frustumSize * aspect / 2, | |
86 frustumSize / 2, frustumSize / - 2, 1, frustumSize); | |
87 | |
88 camera.position.z = 100; | |
89 | |
90 const controls = new OrbitControls(camera, renderer.domElement); | |
91 | |
92 //controls.update() must be called after any manual changes to the camera's transform | |
93 controls.update(); | |
94 | |
95 function animate() { | |
96 requestAnimationFrame(animate); | |
97 | |
98 // required if controls.enableDamping or controls.autoRotate are set to true | |
99 controls.update(); | |
100 light2.position.copy(camera.position); | |
101 | |
102 renderer.render(scene, camera); | |
103 } | |
104 | |
105 animate(); | |
106 | |
107 function onWindowResize() { | |
108 const aspect = window.innerWidth / window.innerHeight; | |
109 camera.left = - frustumSize * aspect / 2; | |
110 camera.right = frustumSize * aspect / 2; | |
111 camera.top = frustumSize / 2; | |
112 camera.bottom = - frustumSize / 2; | |
113 camera.updateProjectionMatrix(); | |
114 renderer.setSize(window.innerWidth, window.innerHeight); | |
115 } | |
116 | |
117 window.addEventListener('resize', onWindowResize ); | |
118 }, | |
119 function (xhr) { | |
120 }, | |
121 function (error) { | |
122 console.log(error); | |
123 } | |
124 ); |