Mercurial > hg > orthanc-stone
comparison Samples/Deprecated/WebAssembly/BasicScene.cpp @ 1381:f4a06ad1580b
Branch broker is now the new default
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Wed, 22 Apr 2020 14:05:47 +0200 |
parents | eac254fb6791 |
children |
comparison
equal
deleted
inserted
replaced
1375:4431ffdcc2a4 | 1381:f4a06ad1580b |
---|---|
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 | |
22 #include "dev.h" | |
23 | |
24 #include <emscripten.h> | |
25 #include <emscripten/html5.h> | |
26 | |
27 // From Stone | |
28 #include "../../Framework/Scene2D/ColorTextureSceneLayer.h" | |
29 #include "../../Framework/StoneInitialization.h" | |
30 | |
31 // From Orthanc framework | |
32 #include <Core/Images/Image.h> | |
33 #include <Core/Logging.h> | |
34 #include <Core/OrthancException.h> | |
35 | |
36 void PrepareScene(OrthancStone::Scene2D& scene) | |
37 { | |
38 using namespace OrthancStone; | |
39 | |
40 // Texture of 2x2 size | |
41 if (1) | |
42 { | |
43 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 2, 2, false); | |
44 | |
45 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0)); | |
46 p[0] = 255; | |
47 p[1] = 0; | |
48 p[2] = 0; | |
49 | |
50 p[3] = 0; | |
51 p[4] = 255; | |
52 p[5] = 0; | |
53 | |
54 p = reinterpret_cast<uint8_t*>(i.GetRow(1)); | |
55 p[0] = 0; | |
56 p[1] = 0; | |
57 p[2] = 255; | |
58 | |
59 p[3] = 255; | |
60 p[4] = 0; | |
61 p[5] = 0; | |
62 | |
63 scene.SetLayer(12, new ColorTextureSceneLayer(i)); | |
64 | |
65 std::unique_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | |
66 l->SetOrigin(-3, 2); | |
67 l->SetPixelSpacing(1.5, 1); | |
68 l->SetAngle(20.0 / 180.0 * M_PI); | |
69 scene.SetLayer(14, l.release()); | |
70 } | |
71 | |
72 // Texture of 1x1 size | |
73 if (1) | |
74 { | |
75 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 1, 1, false); | |
76 | |
77 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0)); | |
78 p[0] = 255; | |
79 p[1] = 0; | |
80 p[2] = 0; | |
81 | |
82 std::unique_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | |
83 l->SetOrigin(-2, 1); | |
84 l->SetAngle(20.0 / 180.0 * M_PI); | |
85 scene.SetLayer(13, l.release()); | |
86 } | |
87 | |
88 // Some lines | |
89 if (1) | |
90 { | |
91 std::unique_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer); | |
92 | |
93 layer->SetThickness(1); | |
94 | |
95 PolylineSceneLayer::Chain chain; | |
96 chain.push_back(ScenePoint2D(0 - 0.5, 0 - 0.5)); | |
97 chain.push_back(ScenePoint2D(0 - 0.5, 2 - 0.5)); | |
98 chain.push_back(ScenePoint2D(2 - 0.5, 2 - 0.5)); | |
99 chain.push_back(ScenePoint2D(2 - 0.5, 0 - 0.5)); | |
100 layer->AddChain(chain, true, 255, 0, 0); | |
101 | |
102 chain.clear(); | |
103 chain.push_back(ScenePoint2D(-5, -5)); | |
104 chain.push_back(ScenePoint2D(5, -5)); | |
105 chain.push_back(ScenePoint2D(5, 5)); | |
106 chain.push_back(ScenePoint2D(-5, 5)); | |
107 layer->AddChain(chain, true, 0, 255, 0); | |
108 | |
109 double dy = 1.01; | |
110 chain.clear(); | |
111 chain.push_back(ScenePoint2D(-4, -4)); | |
112 chain.push_back(ScenePoint2D(4, -4 + dy)); | |
113 chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy)); | |
114 chain.push_back(ScenePoint2D(4, 2)); | |
115 layer->AddChain(chain, false, 0, 0, 255); | |
116 | |
117 scene.SetLayer(50, layer.release()); | |
118 } | |
119 | |
120 // Some text | |
121 if (1) | |
122 { | |
123 std::unique_ptr<TextSceneLayer> layer(new TextSceneLayer); | |
124 layer->SetText("Hello"); | |
125 scene.SetLayer(100, layer.release()); | |
126 } | |
127 } | |
128 | |
129 | |
130 std::unique_ptr<OrthancStone::WebAssemblyViewport> viewport1_; | |
131 std::unique_ptr<OrthancStone::WebAssemblyViewport> viewport2_; | |
132 std::unique_ptr<OrthancStone::WebAssemblyViewport> viewport3_; | |
133 boost::shared_ptr<OrthancStone::ViewportController> controller1_; | |
134 boost::shared_ptr<OrthancStone::ViewportController> controller2_; | |
135 boost::shared_ptr<OrthancStone::ViewportController> controller3_; | |
136 OrthancStone::MessageBroker broker_; | |
137 | |
138 | |
139 EM_BOOL OnWindowResize( | |
140 int eventType, const EmscriptenUiEvent *uiEvent, void *userData) | |
141 { | |
142 if (viewport1_.get() != NULL) | |
143 { | |
144 viewport1_->UpdateSize(); | |
145 } | |
146 | |
147 if (viewport2_.get() != NULL) | |
148 { | |
149 viewport2_->UpdateSize(); | |
150 } | |
151 | |
152 if (viewport3_.get() != NULL) | |
153 { | |
154 viewport3_->UpdateSize(); | |
155 } | |
156 | |
157 return true; | |
158 } | |
159 | |
160 extern "C" | |
161 { | |
162 int main(int argc, char const *argv[]) | |
163 { | |
164 OrthancStone::StoneInitialize(); | |
165 // Orthanc::Logging::EnableInfoLevel(true); | |
166 // Orthanc::Logging::EnableTraceLevel(true); | |
167 EM_ASM(window.dispatchEvent(new CustomEvent("WebAssemblyLoaded"));); | |
168 } | |
169 | |
170 EMSCRIPTEN_KEEPALIVE | |
171 void Initialize() | |
172 { | |
173 viewport1_.reset(new OrthancStone::WebAssemblyViewport("mycanvas1")); | |
174 PrepareScene(viewport1_->GetScene()); | |
175 viewport1_->UpdateSize(); | |
176 | |
177 viewport2_.reset(new OrthancStone::WebAssemblyViewport("mycanvas2")); | |
178 PrepareScene(viewport2_->GetScene()); | |
179 viewport2_->UpdateSize(); | |
180 | |
181 viewport3_.reset(new OrthancStone::WebAssemblyViewport("mycanvas3")); | |
182 PrepareScene(viewport3_->GetScene()); | |
183 viewport3_->UpdateSize(); | |
184 | |
185 viewport1_->GetCompositor().SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, | |
186 FONT_SIZE, Orthanc::Encoding_Latin1); | |
187 viewport2_->GetCompositor().SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, | |
188 FONT_SIZE, Orthanc::Encoding_Latin1); | |
189 viewport3_->GetCompositor().SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, | |
190 FONT_SIZE, Orthanc::Encoding_Latin1); | |
191 | |
192 controller1_.reset(new OrthancStone::ViewportController(boost::make_shared<OrthancStone::UndoStack>(), broker_, *viewport1_)); | |
193 controller2_.reset(new OrthancStone::ViewportController(boost::make_shared<OrthancStone::UndoStack>(), broker_, *viewport2_)); | |
194 controller3_.reset(new OrthancStone::ViewportController(boost::make_shared<OrthancStone::UndoStack>(), broker_, *viewport3_)); | |
195 | |
196 controller1_->FitContent(viewport1_->GetCanvasWidth(), viewport1_->GetCanvasHeight()); | |
197 controller2_->FitContent(viewport2_->GetCanvasWidth(), viewport2_->GetCanvasHeight()); | |
198 controller3_->FitContent(viewport3_->GetCanvasWidth(), viewport3_->GetCanvasHeight()); | |
199 | |
200 viewport1_->Refresh(); | |
201 viewport2_->Refresh(); | |
202 viewport3_->Refresh(); | |
203 | |
204 SetupEvents("mycanvas1", controller1_); | |
205 SetupEvents("mycanvas2", controller2_); | |
206 SetupEvents("mycanvas3", controller3_); | |
207 | |
208 emscripten_set_resize_callback("#window", NULL, false, OnWindowResize); | |
209 } | |
210 } |