Mercurial > hg > orthanc-stone
comparison Applications/Samples/WebAssembly/RtViewer/RtViewerWasm.cpp @ 1538:d1806b4e4839
moving OrthancStone/Samples/ as Applications/Samples/
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 11 Aug 2020 13:24:38 +0200 |
parents | OrthancStone/Samples/WebAssembly/RtViewer/RtViewerWasm.cpp@301571299212 |
children | 4cfdaf4ef3fe |
comparison
equal
deleted
inserted
replaced
1537:de8cf5859e84 | 1538:d1806b4e4839 |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 #include "../../Common/RtViewerApp.h" | |
22 #include "../../Common/RtViewerView.h" | |
23 #include "../../Common/SampleHelpers.h" | |
24 | |
25 // Stone of Orthanc includes | |
26 #include "../../../Sources/Loaders/WebAssemblyLoadersContext.h" | |
27 #include "../../../Sources/StoneException.h" | |
28 #include "../../../Sources/StoneInitialization.h" | |
29 #include "../../../Sources/Viewport/WebGLViewport.h" | |
30 //#include "../../../Sources/OpenGL/WebAssemblyOpenGLContext.h" | |
31 | |
32 #include <Toolbox.h> | |
33 | |
34 #include <boost/program_options.hpp> | |
35 #include <boost/shared_ptr.hpp> | |
36 // #include <boost/pointer_cast.hpp> this include might be necessary in more recent boost versions | |
37 | |
38 #include <emscripten.h> | |
39 #include <emscripten/html5.h> | |
40 | |
41 | |
42 #define DISPATCH_JAVASCRIPT_EVENT(name) \ | |
43 EM_ASM( \ | |
44 const customEvent = document.createEvent("CustomEvent"); \ | |
45 customEvent.initCustomEvent(name, false, false, undefined); \ | |
46 window.dispatchEvent(customEvent); \ | |
47 ); | |
48 | |
49 #define EXTERN_CATCH_EXCEPTIONS \ | |
50 catch (Orthanc::OrthancException& e) \ | |
51 { \ | |
52 LOG(ERROR) << "OrthancException: " << e.What(); \ | |
53 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \ | |
54 } \ | |
55 catch (OrthancStone::StoneException& e) \ | |
56 { \ | |
57 LOG(ERROR) << "StoneException: " << e.What(); \ | |
58 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \ | |
59 } \ | |
60 catch (std::exception& e) \ | |
61 { \ | |
62 LOG(ERROR) << "Runtime error: " << e.what(); \ | |
63 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \ | |
64 } \ | |
65 catch (...) \ | |
66 { \ | |
67 LOG(ERROR) << "Native exception"; \ | |
68 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \ | |
69 } | |
70 | |
71 namespace OrthancStone | |
72 { | |
73 // typedef EM_BOOL (*OnMouseWheelFunc)(int eventType, const EmscriptenWheelEvent* wheelEvent, void* userData); | |
74 | |
75 EM_BOOL RtViewerView_Scroll(int eventType, | |
76 const EmscriptenWheelEvent* wheelEvent, | |
77 void* userData) | |
78 { | |
79 RtViewerView* that = reinterpret_cast<RtViewerView*>(userData); | |
80 | |
81 int delta = 0; | |
82 if (wheelEvent->deltaY < 0) | |
83 delta = -1; | |
84 if (wheelEvent->deltaY > 0) | |
85 delta = 1; | |
86 | |
87 that->Scroll(delta); | |
88 | |
89 return 1; | |
90 } | |
91 | |
92 boost::shared_ptr<IViewport> RtViewerView::CreateViewport( | |
93 const std::string& canvasId) | |
94 { | |
95 boost::shared_ptr<IViewport> viewport = WebGLViewport::Create(canvasId); | |
96 | |
97 void* userData = reinterpret_cast<void*>(this); | |
98 | |
99 // manually add the mouse wheel handler | |
100 | |
101 std::string selector = "#" + canvasId; | |
102 | |
103 emscripten_set_wheel_callback_on_thread( | |
104 selector.c_str(), | |
105 userData, | |
106 false, | |
107 &RtViewerView_Scroll, | |
108 EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD); | |
109 | |
110 return viewport; | |
111 } | |
112 | |
113 void RtViewerView::TakeScreenshot(const std::string& target, | |
114 unsigned int canvasWidth, | |
115 unsigned int canvasHeight) | |
116 { | |
117 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
118 } | |
119 | |
120 | |
121 void RtViewerApp::RunWasm() | |
122 { | |
123 loadersContext_.reset(new OrthancStone::WebAssemblyLoadersContext(1, 4, 1)); | |
124 | |
125 // we are in WASM --> downcast to concrete type | |
126 boost::shared_ptr<WebAssemblyLoadersContext> loadersContext = | |
127 boost::dynamic_pointer_cast<WebAssemblyLoadersContext>(loadersContext_); | |
128 | |
129 if (HasArgument("orthanc")) | |
130 loadersContext->SetLocalOrthanc(GetArgument("orthanc")); | |
131 else | |
132 loadersContext->SetLocalOrthanc(".."); | |
133 | |
134 loadersContext->SetDicomCacheSize(128 * 1024 * 1024); // 128MB | |
135 | |
136 CreateLoaders(); | |
137 | |
138 CreateView("RtViewer_Axial", VolumeProjection_Axial); | |
139 CreateView("RtViewer_Coronal", VolumeProjection_Coronal); | |
140 CreateView("RtViewer_Sagittal", VolumeProjection_Sagittal); | |
141 | |
142 for (size_t i = 0; i < views_.size(); ++i) | |
143 { | |
144 views_[i]->PrepareViewport(); | |
145 } | |
146 | |
147 StartLoaders(); | |
148 } | |
149 } | |
150 | |
151 extern "C" | |
152 { | |
153 boost::shared_ptr<OrthancStone::RtViewerApp> g_app; | |
154 | |
155 int main(int argc, char const *argv[]) | |
156 { | |
157 try | |
158 { | |
159 OrthancStone::StoneInitialize(); | |
160 Orthanc::Logging::Initialize(); | |
161 Orthanc::Logging::EnableTraceLevel(true); | |
162 | |
163 LOG(WARNING) << "Initializing native Stone"; | |
164 | |
165 LOG(WARNING) << "Compiled with Emscripten " << __EMSCRIPTEN_major__ | |
166 << "." << __EMSCRIPTEN_minor__ | |
167 << "." << __EMSCRIPTEN_tiny__; | |
168 | |
169 LOG(INFO) << "Endianness: " << Orthanc::EnumerationToString(Orthanc::Toolbox::DetectEndianness()); | |
170 | |
171 g_app = OrthancStone::RtViewerApp::Create(); | |
172 | |
173 DISPATCH_JAVASCRIPT_EVENT("WasmModuleInitialized"); | |
174 } | |
175 EXTERN_CATCH_EXCEPTIONS; | |
176 } | |
177 | |
178 EMSCRIPTEN_KEEPALIVE | |
179 void Initialize(const char* canvasId) | |
180 { | |
181 try | |
182 { | |
183 g_app->RunWasm(); | |
184 } | |
185 EXTERN_CATCH_EXCEPTIONS; | |
186 } | |
187 | |
188 EMSCRIPTEN_KEEPALIVE | |
189 void SetArgument(const char* key, const char* value) | |
190 { | |
191 // This is called for each GET argument (cf. "app.js") | |
192 LOG(INFO) << "Received GET argument: [" << key << "] = [" << value << "]"; | |
193 g_app->SetArgument(key, value); | |
194 } | |
195 | |
196 } |