annotate Applications/Samples/WebAssembly/RtViewer/RtViewerWasmApp.js @ 1923:f4cdcba8c32a

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Mar 2022 17:39:19 +0100
parents 7053b8a0aaec
children 07964689cb0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1589
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
1 /**
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
2 * Stone of Orthanc
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
4 * Department, University Hospital of Liege, Belgium
1871
7053b8a0aaec upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1870
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
7053b8a0aaec upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1870
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1589
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
7 *
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
8 * This program is free software: you can redistribute it and/or
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
9 * modify it under the terms of the GNU Affero General Public License
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
11 * the License, or (at your option) any later version.
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
12 *
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
13 * This program is distributed in the hope that it will be useful, but
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
16 * Affero General Public License for more details.
1596
4fb8fdf03314 removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1589
diff changeset
17 *
1589
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
18 * You should have received a copy of the GNU Affero General Public License
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
20 **/
8c5f9864545f adding missing headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1538
diff changeset
21
1393
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
22
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
23 // This object wraps the functions exposed by the wasm module
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
24
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
25 const WasmModuleWrapper = function() {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
26 this._InitializeViewport = undefined;
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
27 };
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
28
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
29 WasmModuleWrapper.prototype.Setup = function(Module) {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
30 this._SetArgument = Module.cwrap('SetArgument', null, [ 'string', 'string' ]);
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
31 this._Initialize = Module.cwrap('Initialize', null, [ 'string' ]);
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
32 };
1384
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
33
1393
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
34 WasmModuleWrapper.prototype.SetArgument = function(key, value) {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
35 this._SetArgument(key, value);
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
36 };
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
37
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
38 WasmModuleWrapper.prototype.Initialize = function(canvasId) {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
39 this._Initialize(canvasId);
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
40 };
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
41
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
42 var wasmModuleWrapper = new WasmModuleWrapper();
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
43
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
44 $(document).ready(function() {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
45
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
46 window.addEventListener('WasmModuleInitialized', function() {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
47
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
48 // bind the C++ global functions
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
49 wasmModuleWrapper.Setup(Module);
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
50
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
51 console.warn('Native C++ module initialized');
1384
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
52
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
53 // Loop over the GET arguments
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
54 var parameters = window.location.search.substr(1);
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
55 if (parameters != null && parameters != '') {
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
56 var tokens = parameters.split('&');
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
57 for (var i = 0; i < tokens.length; i++) {
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
58 var arg = tokens[i].split('=');
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
59 if (arg.length == 2) {
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
60 // Send each GET argument to WebAssembly
1393
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
61 wasmModuleWrapper.SetArgument(arg[0], decodeURIComponent(arg[1]));
1384
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
62 }
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
63 }
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
64 }
1393
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
65 wasmModuleWrapper.Initialize();
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
66 });
1384
24bcff8ea58f RtViewer : SDL ok. Preparation for WASM builds ongoing
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff changeset
67
1393
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
68 window.addEventListener('StoneException', function() {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
69 alert('Exception caught in C++ code');
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
70 });
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
71
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
72 var scriptSource;
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
73
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
74 if ('WebAssembly' in window) {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
75 console.warn('Loading WebAssembly');
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
76 scriptSource = 'RtViewerWasm.js';
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
77 } else {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
78 console.error('Your browser does not support WebAssembly!');
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
79 }
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
80
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
81 // Option 1: Loading script using plain HTML
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
82
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
83 /*
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
84 var script = document.createElement('script');
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
85 script.src = scriptSource;
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
86 script.type = 'text/javascript';
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
87 document.body.appendChild(script);
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
88 */
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
89
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
90 // Option 2: Loading script using AJAX (gives the opportunity to
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
91 // report explicit errors)
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
92
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
93 axios.get(scriptSource)
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
94 .then(function (response) {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
95 var script = document.createElement('script');
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
96 script.innerHTML = response.data;
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
97 script.type = 'text/javascript';
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
98 document.body.appendChild(script);
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
99 })
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
100 .catch(function (error) {
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
101 alert('Cannot load the WebAssembly framework');
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
102 });
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
103 });
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
104
1517
307a805d0587 rtviewer --> stone-rtviewer
Benjamin Golinvaux <bgo@osimis.io>
parents: 1512
diff changeset
105 // http://localhost:9979/stone-rtviewer/index.html?loglevel=trace&ctseries=a04ecf01-79b2fc33-58239f7e-ad9db983-28e81afa&rtdose=830a69ff-8e4b5ee3-b7f966c8-bccc20fb-d322dceb&rtstruct=54460695-ba3885ee-ddf61ac0-f028e31d-a6e474d9
1393
27e0a00bd3e8 RtViewer SingleFrameViewer OK : wasm SDL single viewport
Benjamin Golinvaux <bgo@osimis.io>
parents: 1384
diff changeset
106