Mercurial > hg > orthanc-stone
changeset 2398:dce64c0ef32b refactoring
created SdlVolumeViewer
line wrap: on
line diff
--- a/Applications/Samples/Common/ISingleViewportApplicationCore.h Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/Common/ISingleViewportApplicationCore.h Thu Nov 12 18:03:35 2026 +0100 @@ -38,12 +38,15 @@ } virtual void CreateComponents(const boost::shared_ptr<StoneApplication::Context>& context, - const boost::shared_ptr<IViewport>& viewport, const boost::shared_ptr<ViewportController>& controller) = 0; virtual IViewportInteractor* CreateMouseInteractor() = 0; virtual void HandleKeyDown(const IEnvironment::ILock& environmentLock, char key) = 0; + + virtual void HandleMouseWheel(const IEnvironment::ILock& environmentLock, + MouseWheelDirection direction, + bool fast) = 0; }; }
--- a/Applications/Samples/Common/RtViewerView.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/Common/RtViewerView.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -227,30 +227,34 @@ void RtViewerView::UpdateLayers() { - // TODO Refactoring - Moving this lock down results in crash => is the environment mutex locked? - std::unique_ptr<IViewport::ILock> lock(viewport_->Lock()); + { + // TODO Refactoring - Moving this lock down results in crash => is the environment mutex locked? + std::unique_ptr<IViewport::ILock> lock(viewport_->Lock()); - if (planes_.size() == 0) - { - RetrieveGeometry(); - } + if (planes_.size() == 0) + { + RetrieveGeometry(); + } - if (currentPlane_ < planes_.size()) - { - if (ctVolumeLayerSource_.get() != NULL) + if (currentPlane_ < planes_.size()) { - ctVolumeLayerSource_->Update(controller_->GetScene(), planes_[currentPlane_]); - } - if (doseVolumeLayerSource_.get() != NULL) - { - doseVolumeLayerSource_->Update(controller_->GetScene(), planes_[currentPlane_]); + if (ctVolumeLayerSource_.get() != NULL) + { + ctVolumeLayerSource_->Update(controller_->GetScene(), planes_[currentPlane_]); + } + if (doseVolumeLayerSource_.get() != NULL) + { + doseVolumeLayerSource_->Update(controller_->GetScene(), planes_[currentPlane_]); + } + if (structLayerSource_.get() != NULL) + { + structLayerSource_->Update(controller_->GetScene(), planes_[currentPlane_]); + } } - if (structLayerSource_.get() != NULL) - { - structLayerSource_->Update(controller_->GetScene(), planes_[currentPlane_]); - } + //lock->Invalidate(); + + controller_->InvalidateViewport(); } - lock->Invalidate(); } void RtViewerView::PrepareViewport() @@ -327,7 +331,7 @@ Scene2D& scene = controller_->GetScene(); int depth = scene.GetMaxDepth() + 1; - ctVolumeLayerSource_.reset(new VolumeSceneLayerSource(scene, depth, volume)); + ctVolumeLayerSource_.reset(new VolumeSceneLayerSource(depth, volume)); if (style != NULL) { @@ -344,7 +348,7 @@ Scene2D& scene = controller_->GetScene(); int depth = scene.GetMaxDepth() + 1; - doseVolumeLayerSource_.reset(new VolumeSceneLayerSource(scene, depth, volume)); + doseVolumeLayerSource_.reset(new VolumeSceneLayerSource(depth, volume)); if (style != NULL) { @@ -358,6 +362,6 @@ Scene2D& scene = controller_->GetScene(); int depth = scene.GetMaxDepth() + 1; - structLayerSource_.reset(new VolumeSceneLayerSource(scene, depth, volume)); + structLayerSource_.reset(new VolumeSceneLayerSource(depth, volume)); } }
--- a/Applications/Samples/Common/SimpleViewerCore.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/Common/SimpleViewerCore.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -122,11 +122,9 @@ void SimpleViewerCore::CreateComponents(const boost::shared_ptr<StoneApplication::Context>& context, - const boost::shared_ptr<IViewport>& viewport, const boost::shared_ptr<ViewportController>& controller) { if (!context || - !viewport || !controller) { throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
--- a/Applications/Samples/Common/SimpleViewerCore.h Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/Common/SimpleViewerCore.h Thu Nov 12 18:03:35 2026 +0100 @@ -65,7 +65,6 @@ protected: virtual void CreateComponents(const boost::shared_ptr<StoneApplication::Context>& context, - const boost::shared_ptr<IViewport>& viewport, const boost::shared_ptr<ViewportController>& controller) ORTHANC_OVERRIDE; virtual IViewportInteractor* CreateMouseInteractor() ORTHANC_OVERRIDE; @@ -73,6 +72,12 @@ virtual void HandleKeyDown(const IEnvironment::ILock& environmentLock, char key) ORTHANC_OVERRIDE; + virtual void HandleMouseWheel(const IEnvironment::ILock& environmentLock, + MouseWheelDirection direction, + bool fast) ORTHANC_OVERRIDE + { + } + public: void LoadFrameFromOrthanc(const std::string& instanceId, unsigned int frameIndex);
--- a/Applications/Samples/Sdl/CMakeLists.txt Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/Sdl/CMakeLists.txt Thu Nov 12 18:03:35 2026 +0100 @@ -116,6 +116,20 @@ target_link_libraries(SdlSimpleViewer OrthancStone ${DCMTK_LIBRARIES}) ############################# +project(SdlVolumeViewer) + +add_executable(SdlVolumeViewer + SdlVolumeViewer.cpp + SingleViewportSdlApplication.cpp + ) + +if (COMMAND DefineSourceBasenameForTarget) + DefineSourceBasenameForTarget(SdlVolumeViewer) +endif() + +target_link_libraries(SdlVolumeViewer OrthancStone ${DCMTK_LIBRARIES}) + +############################# project(UnitTests) add_executable(UnitTests
--- a/Applications/Samples/Sdl/RtViewer/RtViewerSdl.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/Sdl/RtViewer/RtViewerSdl.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -172,8 +172,8 @@ } void RtViewerView::TakeScreenshot(const std::string& target, - unsigned int canvasWidth, - unsigned int canvasHeight) + unsigned int canvasWidth, + unsigned int canvasHeight) { std::unique_ptr<IViewport::ILock> lock(viewport_->Lock()); const Scene2D& scene = controller_->GetScene(); @@ -195,7 +195,7 @@ Orthanc::IImageWriter::WriteToFile(writer, target, png); } - static boost::shared_ptr<RtViewerView> GetViewFromWindowId( + /*static boost::shared_ptr<RtViewerView> GetViewFromWindowId( const std::vector<boost::shared_ptr<RtViewerView> >& views, Uint32 windowID) { @@ -210,22 +210,22 @@ return view; } return boost::shared_ptr<RtViewerView>(); - } + }*/ void RtViewerApp::SdlRunLoop(const std::vector<boost::shared_ptr<RtViewerView> >& views) { using namespace OrthancStone; // const std::vector<boost::shared_ptr<RtViewerView> >& views - std::vector<boost::shared_ptr<SdlViewport> > viewports; - for (size_t i = 0; i < views.size(); ++i) - { - boost::shared_ptr<RtViewerView> view = views[i]; - boost::shared_ptr<IViewport> viewport = view->GetViewport(); - boost::shared_ptr<SdlViewport> sdlViewport = - boost::dynamic_pointer_cast<SdlViewport>(viewport); - viewports.push_back(sdlViewport); - } + /* std::vector<boost::shared_ptr<SdlViewport> > viewports; + for (size_t i = 0; i < views.size(); ++i) + { + boost::shared_ptr<RtViewerView> view = views[i]; + boost::shared_ptr<IViewport> viewport = view->GetViewport(); + boost::shared_ptr<SdlViewport> sdlViewport = + boost::dynamic_pointer_cast<SdlViewport>(viewport); + viewports.push_back(sdlViewport); + }*/ { int scancodeCount = 0; @@ -234,40 +234,19 @@ bool stop = false; while (!stop) { - std::vector<SDL_Event> sdlEvents; - std::map<Uint32,SDL_Event> userEventsMap; + SDL_Event sdlEvent; + while (SDL_PollEvent(&sdlEvent) != 0) { - SDL_Event sdlEvent; - - // FIRST: collect all pending events - while (SDL_PollEvent(&sdlEvent) != 0) + boost::shared_ptr<RtViewerView> view; + for (size_t i = 0; i < views.size(); i++) { - if ( (sdlEvent.type >= SDL_USEREVENT) && - (sdlEvent.type < SDL_LASTEVENT) ) + if (dynamic_cast<OrthancStone::SdlViewport&>(*views[i]->GetViewport()).IsEventFromThisWindow(sdlEvent)) { - // we don't want to have multiple refresh events , - // and since every refresh event is a user event with a special type, - // we use a map - userEventsMap[sdlEvent.type] = sdlEvent; - } - else - { - sdlEvents.push_back(sdlEvent); + view = views[i]; + break; } } - } - - // SECOND: add all user events to sdlEvents - for (std::map<Uint32,SDL_Event>::const_iterator it = userEventsMap.begin(); it != userEventsMap.end(); ++it) - { - sdlEvents.push_back(it->second); - } - - // now process the events - for (std::vector<SDL_Event>::const_iterator it = sdlEvents.begin(); it != sdlEvents.end(); ++it) - { - const SDL_Event& sdlEvent = *it; if (sdlEvent.type == SDL_QUIT) { @@ -276,126 +255,103 @@ } else if (sdlEvent.type == SDL_WINDOWEVENT && (sdlEvent.window.event == SDL_WINDOWEVENT_RESIZED || - sdlEvent.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)) - { - boost::shared_ptr<RtViewerView> view = GetViewFromWindowId( - views, sdlEvent.window.windowID); - - boost::shared_ptr<SdlViewport> sdlViewport = - boost::dynamic_pointer_cast<SdlViewport>(view->GetViewport()); - - sdlViewport->UpdateSize(sdlEvent.window.data1, sdlEvent.window.data2); - } - else if (sdlEvent.type == SDL_WINDOWEVENT && - (sdlEvent.window.event == SDL_WINDOWEVENT_SHOWN || + sdlEvent.window.event == SDL_WINDOWEVENT_SIZE_CHANGED || + sdlEvent.window.event == SDL_WINDOWEVENT_SHOWN || sdlEvent.window.event == SDL_WINDOWEVENT_EXPOSED)) { - boost::shared_ptr<RtViewerView> view = GetViewFromWindowId( - views, sdlEvent.window.windowID); - boost::shared_ptr<SdlViewport> sdlViewport = - boost::dynamic_pointer_cast<SdlViewport>(view->GetViewport()); - - { - std::unique_ptr<IViewport::ILock> lock(sdlViewport->Lock()); - lock->RefreshCanvasSize(); - } + std::unique_ptr<OrthancStone::IViewport::ILock> lock(view->GetViewport()->Lock()); + lock->RefreshCanvasSize(); } else if (sdlEvent.type == SDL_KEYDOWN && sdlEvent.key.repeat == 0 /* Ignore key bounce */) { - boost::shared_ptr<RtViewerView> view = GetViewFromWindowId( - views, sdlEvent.window.windowID); - switch (sdlEvent.key.keysym.sym) { - case SDLK_f: - { - boost::shared_ptr<SdlViewport> sdlViewport = - boost::dynamic_pointer_cast<SdlViewport>(view->GetViewport()); - sdlViewport->ToggleMaximize(); - } - break; + case SDLK_f: + if (view) + { + dynamic_cast<OrthancStone::SdlViewport&>(*view->GetViewport()).ToggleMaximize(); + } + break; - case SDLK_s: - { - std::unique_ptr<IViewport::ILock> lock(view->GetViewport()->Lock()); - lock->GetCompositor().FitContent(view->GetController()->GetScene()); - lock->Invalidate(); - } - break; + case SDLK_s: + if (view) + { + std::unique_ptr<IViewport::ILock> lock(view->GetViewport()->Lock()); + lock->GetCompositor().FitContent(view->GetController()->GetScene()); + lock->Invalidate(); + } + break; - case SDLK_q: - stop = true; - break; + case SDLK_q: + stop = true; + break; - default: - break; + default: + break; } } else if (sdlEvent.type == SDL_MOUSEBUTTONDOWN || sdlEvent.type == SDL_MOUSEMOTION || sdlEvent.type == SDL_MOUSEBUTTONUP) { - boost::shared_ptr<RtViewerView> view = GetViewFromWindowId( - views, sdlEvent.window.windowID); - - std::unique_ptr<IViewport::ILock> lock(view->GetViewport()->Lock()); - if (lock->HasCompositor()) + if (view) { - PointerEvent p; - OrthancStoneHelpers::GetPointerEvent(p, lock->GetCompositor(), - sdlEvent, keyboardState, scancodeCount); - - switch (sdlEvent.type) + std::unique_ptr<IViewport::ILock> lock(view->GetViewport()->Lock()); + if (lock->HasCompositor()) { - case SDL_MOUSEBUTTONDOWN: - view->GetController()->HandleMousePress(p, - lock->GetCompositor().GetCanvasWidth(), - lock->GetCompositor().GetCanvasHeight()); - break; + PointerEvent p; + OrthancStoneHelpers::GetPointerEvent(p, lock->GetCompositor(), + sdlEvent, keyboardState, scancodeCount); + + switch (sdlEvent.type) + { + case SDL_MOUSEBUTTONDOWN: + view->GetController()->HandleMousePress(p, + lock->GetCompositor().GetCanvasWidth(), + lock->GetCompositor().GetCanvasHeight()); + break; - case SDL_MOUSEMOTION: - view->GetController()->HandleMouseMove(p); - break; + case SDL_MOUSEMOTION: + view->GetController()->HandleMouseMove(p); + break; - case SDL_MOUSEBUTTONUP: - view->GetController()->HandleMouseRelease(p); - break; + case SDL_MOUSEBUTTONUP: + view->GetController()->HandleMouseRelease(p); + break; - default: - throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } } } } else if (sdlEvent.type == SDL_MOUSEWHEEL) { - boost::shared_ptr<RtViewerView> view = GetViewFromWindowId( - views, sdlEvent.window.windowID); + if (view) + { + int delta = 0; + if (sdlEvent.wheel.y < 0) + delta = -1; + if (sdlEvent.wheel.y > 0) + delta = 1; - int delta = 0; - if (sdlEvent.wheel.y < 0) - delta = -1; - if (sdlEvent.wheel.y > 0) - delta = 1; + if (SDL_SCANCODE_LCTRL < scancodeCount && + keyboardState[SDL_SCANCODE_LCTRL]) // Speed up scrolling if CTRL is down + { + delta *= 10; + } - if (SDL_SCANCODE_LCTRL < scancodeCount && - keyboardState[SDL_SCANCODE_LCTRL]) // Speed up scrolling if CTRL is down - { - delta *= 10; + view->Scroll(delta); } - - view->Scroll(delta); } else { - for (size_t i = 0; i < views.size(); ++i) + if (view/* && + dynamic_cast<OrthancStone::SdlViewport&>(*view->GetViewport()).IsRefreshEvent(sdlEvent)*/) { - boost::shared_ptr<SdlViewport> sdlViewport = - boost::dynamic_pointer_cast<SdlViewport>(views[i]->GetViewport()); - if (sdlViewport->IsRefreshEvent(sdlEvent)) - { - sdlViewport->Paint(views[i]->GetController()->GetScene()); - } + std::unique_ptr<OrthancStone::IViewport::ILock> lock(view->GetViewport()->Lock()); + lock->GetCompositor().Refresh(view->GetController()->GetScene()); } } } @@ -479,8 +435,7 @@ OrthancStone::StoneApplication::Initialize(configuration); - boost::shared_ptr<OrthancStone::RtViewerApp> app = OrthancStone::RtViewerApp::Create( - OrthancStone::StoneApplication::GetInstance()); + boost::shared_ptr<OrthancStone::RtViewerApp> app = OrthancStone::RtViewerApp::Create(OrthancStone::StoneApplication::GetInstance()); for (std::map<std::string, std::string>::const_iterator it = arguments.begin(); it != arguments.end(); ++it) {
--- a/Applications/Samples/Sdl/SdlHelpers.h Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/Sdl/SdlHelpers.h Thu Nov 12 18:03:35 2026 +0100 @@ -125,7 +125,7 @@ } - inline boost::shared_ptr<OrthancStone::SdlViewport> GetSdlViewportFromWindowId( +/* inline boost::shared_ptr<OrthancStone::SdlViewport> GetSdlViewportFromWindowId( const std::vector<boost::shared_ptr<OrthancStone::SdlViewport> >& viewports, Uint32 windowID) { @@ -140,7 +140,7 @@ } return boost::shared_ptr<OrthancStone::SdlViewport>(); - } + }*/ }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Applications/Samples/Sdl/SdlVolumeViewer.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -0,0 +1,405 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + **/ + + +#include "../../../OrthancStone/Sources/Loaders/OrthancMultiframeVolumeLoader.h" +#include "../../../OrthancStone/Sources/Loaders/OrthancSeriesVolumeProgressiveLoader.h" +#include "../../../OrthancStone/Sources/Scene2D/GrayscaleStyleConfigurator.h" +#include "../../../OrthancStone/Sources/StoneException.h" +#include "../../../OrthancStone/Sources/StoneInitialization.h" +#include "../../../OrthancStone/Sources/Viewport/DefaultViewportInteractor.h" +#include "../../../OrthancStone/Sources/Volumes/DicomVolumeImageMPRSlicer.h" +#include "../../../OrthancStone/Sources/Volumes/VolumeSceneLayerSource.h" +#include "SingleViewportSdlApplication.h" + +#include <boost/program_options.hpp> + + + +namespace OrthancStone +{ + class VolumeViewerCore : + public ISingleViewportApplicationCore, + public Old::ObserverBase<VolumeViewerCore> + { + private: + boost::shared_ptr<StoneApplication::Context> context_; + boost::shared_ptr<ViewportController> controller_; + + VolumeProjection projection_; + size_t currentPlane_; + std::vector<CoordinateSystem3D> planes_; + + boost::shared_ptr<DicomVolumeImage> volume_; + boost::shared_ptr<OrthancSeriesVolumeProgressiveLoader> seriesLoader_; + boost::shared_ptr<OrthancMultiframeVolumeLoader> instanceLoader_; + boost::shared_ptr<IVolumeSlicer> slicer_; + std::unique_ptr<VolumeSceneLayerSource> source_; + + void Invalidate() + { + if (currentPlane_ < planes_.size()) + { + source_->Update(controller_->GetScene(), planes_[currentPlane_]); + } + + controller_->InvalidateViewport(); + } + + void UpdateProjection() + { + if (volume_->HasGeometry()) + { + VolumeImageGeometry geometry = volume_->GetGeometry(); + + const unsigned int depth = geometry.GetProjectionDepth(projection_); + currentPlane_ = depth / 2; + + LOG(WARNING) << "Number of cutting planes along the projection: " << depth; + + planes_.resize(depth); + + for (unsigned int z = 0; z < depth; z++) + { + planes_[z] = geometry.GetProjectionSlice(projection_, z); + } + } + else + { + planes_.clear(); + currentPlane_ = 0; + } + + Invalidate(); + } + + void OnGeometryReady(const DicomVolumeImage::GeometryReadyMessage& message) + { + LOG(WARNING) << "Dimension of the volume: " + << volume_->GetGeometry().GetWidth() << "x" + << volume_->GetGeometry().GetHeight() << "x" + << volume_->GetGeometry().GetDepth(); + UpdateProjection(); + } + + void OnContentUpdated(const DicomVolumeImage::ContentUpdatedMessage& message) + { + Invalidate(); + } + + protected: + virtual void CreateComponents(const boost::shared_ptr<StoneApplication::Context>& context, + const boost::shared_ptr<ViewportController>& controller) ORTHANC_OVERRIDE + { + context_ = context; + controller_ = controller; + + volume_.reset(new DicomVolumeImage); + slicer_.reset(new DicomVolumeImageMPRSlicer(volume_)); + source_.reset(new VolumeSceneLayerSource(0, slicer_)); + + std::unique_ptr<GrayscaleStyleConfigurator> style(new GrayscaleStyleConfigurator); + style->SetLinearInterpolation(true); + source_->SetConfigurator(style.release()); + + seriesLoader_ = OrthancSeriesVolumeProgressiveLoader::Create(*context, volume_, true /* progressive */); + instanceLoader_ = OrthancMultiframeVolumeLoader::Create(*context, volume_); + + Register<DicomVolumeImage::GeometryReadyMessage>(*seriesLoader_, &VolumeViewerCore::OnGeometryReady); + Register<DicomVolumeImage::ContentUpdatedMessage>(*seriesLoader_, &VolumeViewerCore::OnContentUpdated); + + Register<DicomVolumeImage::GeometryReadyMessage>(*instanceLoader_, &VolumeViewerCore::OnGeometryReady); + Register<DicomVolumeImage::ContentUpdatedMessage>(*instanceLoader_, &VolumeViewerCore::OnContentUpdated); + } + + virtual IViewportInteractor* CreateMouseInteractor() ORTHANC_OVERRIDE + { + return new DefaultViewportInteractor; + } + + virtual void HandleKeyDown(const IEnvironment::ILock& environmentLock, + char key) ORTHANC_OVERRIDE + { + switch (key) + { + case 'a': + projection_ = VolumeProjection_Axial; + UpdateProjection(); + break; + + case 'c': + projection_ = VolumeProjection_Coronal; + UpdateProjection(); + break; + + case 's': + projection_ = VolumeProjection_Sagittal; + UpdateProjection(); + break; + } + } + + virtual void HandleMouseWheel(const IEnvironment::ILock& environmentLock, + MouseWheelDirection direction, + bool fast) ORTHANC_OVERRIDE + { + int delta; + + switch (direction) + { + case MouseWheelDirection_Down: + delta = 1; + break; + + case MouseWheelDirection_Up: + delta = -1; + break; + + default: + break; + } + + if (fast) + { + delta *= static_cast<int>(planes_.size()) / 10; + } + + int tmp = static_cast<int>(currentPlane_) + delta; + + size_t nextPlane; + + if (tmp < 0) + { + nextPlane = 0; + } + else if (tmp >= static_cast<int>(planes_.size())) + { + nextPlane = planes_.size() - 1; + } + else + { + nextPlane = tmp; + } + + if (nextPlane != currentPlane_) + { + currentPlane_ = nextPlane; + Invalidate(); + } + } + + public: + VolumeViewerCore() : + // Dummy initialization + projection_(VolumeProjection::VolumeProjection_Axial), + currentPlane_(0) + { + } + + void LoadSeriesFromOrthanc(VolumeProjection projection, + const std::string& seriesId) + { + projection_ = projection; + seriesLoader_->LoadSeries(seriesId); + } + + void LoadInstanceFromOrthanc(VolumeProjection projection, + const std::string& instanceId) + { + projection_ = projection; + instanceLoader_->LoadInstance(instanceId); + } + }; + + + class Startup : public SingleViewportSdlApplication::ICoreStartup + { + private: + std::string orthancUrl_; + Orthanc::ResourceType level_; + std::string resourceId_; + VolumeProjection projection_; + + public: + Startup(int argc, char* argv[]) : + orthancUrl_("http://orthanc:orthanc@localhost:8043"), + level_(Orthanc::ResourceType_Series), + resourceId_("a7e40137-e4b37f56-14ce875b-9a81702d-eac823bf"), + projection_(VolumeProjection_Axial) + { + namespace po = boost::program_options; + po::options_description desc("Usage"); + + desc.add_options() + ("orthanc", po::value<std::string>()->default_value(orthancUrl_), + "Base URL of the Orthanc instance") + + ("series", po::value<std::string>(), + "Orthanc ID of the series to display") + + ("instance", po::value<std::string>(), + "Orthanc ID of the instance to display") + + ("projection", po::value<std::string>()->default_value("axial"), + "Projection to use: \"axial\", \"sagittal\", or \"coronal\"") + ; + + std::cout << desc << std::endl; + + po::variables_map vm; + try + { + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + } + catch (std::exception& e) + { + std::cerr << "Please check your command line options! (\"" << e.what() << "\")" << std::endl; + } + + if (vm.count("orthanc") > 0) + { + // maybe check URL validity here + orthancUrl_ = vm["orthanc"].as<std::string>(); + } + + if (vm.count("series") > 0 && + vm.count("instance") > 0) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType, + "Command-line options \"series\" and \"instance\" are mutually exclusive"); + } + else if (vm.count("series") > 0) + { + level_ = Orthanc::ResourceType_Series; + resourceId_ = vm["series"].as<std::string>(); + } + else if (vm.count("instance") > 0) + { + level_ = Orthanc::ResourceType_Instance; + resourceId_ = vm["instance"].as<std::string>(); + } + + if (vm.count("projection") > 0) + { + const std::string projection = vm["projection"].as<std::string>(); + + if (projection == "axial") + { + projection_ = VolumeProjection_Axial; + } + else if (projection == "sagittal") + { + projection_ = VolumeProjection_Sagittal; + } + else if (projection == "coronal") + { + projection_ = VolumeProjection_Coronal; + } + else + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange, "Bad projection: " + projection); + } + } + } + + const std::string& GetOrthancUrl() const + { + return orthancUrl_; + } + + virtual void Start(ISingleViewportApplicationCore& core) ORTHANC_OVERRIDE + { + switch (level_) + { + case Orthanc::ResourceType_Series: + dynamic_cast<VolumeViewerCore&>(core).LoadSeriesFromOrthanc(projection_, resourceId_); + break; + + case Orthanc::ResourceType_Instance: + dynamic_cast<VolumeViewerCore&>(core).LoadInstanceFromOrthanc(projection_, resourceId_); + break; + + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); + } + } + }; +} + + +/** + * IMPORTANT: The full arguments to "main()" are needed for SDL on + * Windows. Otherwise, one gets the linking error "undefined reference + * to `SDL_main'". https://wiki.libsdl.org/FAQWindows + **/ +int main(int argc, char* argv[]) +{ + int status = 0; + + OrthancStone::StoneInitialize(); + OrthancStone::SdlWindow::GlobalInitialize(); + + try + { + std::unique_ptr<OrthancStone::Startup> startup(new OrthancStone::Startup(argc, argv)); + + Orthanc::WebServiceParameters orthancWebService; + orthancWebService.SetUrl(startup->GetOrthancUrl()); + + OrthancStone::StoneApplication::Configuration configuration; + configuration.SetRemoteOrthancParameters(orthancWebService); + configuration.SetDicomCacheSize(128 * 1024 * 1024); // TODO Refactoring - Remove this + + boost::shared_ptr<OrthancStone::ISingleViewportApplicationCore> core(new OrthancStone::VolumeViewerCore); + + OrthancStone::SingleViewportSdlApplication app( + configuration, core, "Stone of Orthanc", 800, 600, true /* use OpenGL */); + + app.SetCoreStartup(startup.release()); + app.Run(); + } + catch (Orthanc::OrthancException& e) + { + LOG(ERROR) << "OrthancException: " << e.What(); + status = -1; + } + catch (OrthancStone::StoneException& e) + { + LOG(ERROR) << "StoneException: " << e.What(); + status = -1; + } + catch (std::runtime_error& e) + { + LOG(ERROR) << "Runtime error: " << e.what(); + status = -1; + } + catch (...) + { + LOG(ERROR) << "Native exception"; + status = -1; + } + + OrthancStone::SdlWindow::GlobalFinalize(); + OrthancStone::StoneFinalize(); + + return status; +}
--- a/Applications/Samples/Sdl/SingleFrameViewer/SdlSimpleViewer.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/Sdl/SingleFrameViewer/SdlSimpleViewer.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -24,7 +24,6 @@ #include "../../../../OrthancStone/Sources/StoneInitialization.h" #include "../../Common/SampleHelpers.h" #include "../../Common/SimpleViewerCore.h" -#include "../SdlHelpers.h" #include "../SingleViewportSdlApplication.h" #include <boost/program_options.hpp> @@ -73,11 +72,10 @@ protected: virtual void CreateComponents(const boost::shared_ptr<StoneApplication::Context>& context, - const boost::shared_ptr<IViewport>& viewport, const boost::shared_ptr<ViewportController>& controller) ORTHANC_OVERRIDE { toto_.reset(new Toto); - SimpleViewerCore::CreateComponents(context, viewport, controller); + SimpleViewerCore::CreateComponents(context, controller); } @@ -254,6 +252,81 @@ configuration.SetDicomCacheSize(128 * 1024 * 1024); // TODO Refactoring - Remove this configuration.SetRootDirectory("/tmp"); // TODO Refactoring - Remove this +#if 0 + // TODO Refactoring - Move this to another sample file + std::unique_ptr<OrthancStone::PolylineSceneLayer> layer(new OrthancStone::PolylineSceneLayer); + layer->AddRectangle(0, 20, 200, 300, OrthancStone::Color(255,0,0)); + + OrthancStone::Scene2D scene; + scene.SetLayer(1, layer.release()); + + OrthancStone::SdlOpenGLViewport v1("Hello", 640, 480); + OrthancStone::SdlOpenGLViewport v2("World", 640, 480); + + bool stop = false; + while (!stop) + { + v1.Paint(scene); + v2.Paint(scene); + + SDL_Event event; + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_WINDOWEVENT && + (event.window.event == SDL_WINDOWEVENT_RESIZED || + event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED || + event.window.event == SDL_WINDOWEVENT_SHOWN || + event.window.event == SDL_WINDOWEVENT_EXPOSED)) + { + if (v1.IsEventFromThisWindow(event)) + { + std::unique_ptr<OrthancStone::IViewport::ILock> lock(v1.Lock()); + lock->RefreshCanvasSize(); + } + + if (v2.IsEventFromThisWindow(event)) + { + std::unique_ptr<OrthancStone::IViewport::ILock> lock(v2.Lock()); + lock->RefreshCanvasSize(); + } + } + else if (event.type == SDL_QUIT) + { + stop = true; + } + else if (event.type == SDL_TEXTINPUT) + { + std::string s(event.text.text); + if (s.size() == 1) + { + switch (s[0]) + { + case 'f': + if (v1.IsEventFromThisWindow(event)) + { + v1.ToggleMaximize(); + } + if (v2.IsEventFromThisWindow(event)) + { + v2.ToggleMaximize(); + } + break; + + case 'q': + stop = true; + break; + + default: + break; + } + } + } + } + + SDL_Delay(1); + } + +#else boost::shared_ptr<OrthancStone::SimpleViewerCore> core(new OrthancStone::Tutu); OrthancStone::SingleViewportSdlApplication app( @@ -261,6 +334,7 @@ app.SetCoreStartup(new Startup(instanceId, frameIndex)); app.Run(); +#endif } catch (Orthanc::OrthancException& e) {
--- a/Applications/Samples/Sdl/SingleViewportSdlApplication.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/Sdl/SingleViewportSdlApplication.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -35,7 +35,7 @@ void SingleViewportSdlApplication::RunInternal(const boost::shared_ptr<Context>& context) { assert(core_.get() != NULL); - core_->CreateComponents(context, viewport_, controller_); + core_->CreateComponents(context, controller_); controller_->AcquireInteractor(core_->CreateMouseInteractor()); @@ -67,12 +67,8 @@ } else if (event.type == SDL_WINDOWEVENT && (event.window.event == SDL_WINDOWEVENT_RESIZED || - event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)) - { - viewport_->UpdateSize(event.window.data1, event.window.data2); - } - else if (event.type == SDL_WINDOWEVENT && - (event.window.event == SDL_WINDOWEVENT_SHOWN || + event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED || + event.window.event == SDL_WINDOWEVENT_SHOWN || event.window.event == SDL_WINDOWEVENT_EXPOSED)) { std::unique_ptr<IViewport::ILock> viewportLock(viewport_->Lock()); @@ -95,27 +91,62 @@ } else { + // Lock to prevent oracle callbacks std::unique_ptr<IEnvironment::ILock> environmentLock(context->GetEnvironment().AcquireLock()); + core_->HandleKeyDown(*environmentLock, s[0]); } } } + else if (event.type == SDL_MOUSEWHEEL) + { + bool fast = false; + if (keyboardState[SDL_SCANCODE_LCTRL]) // Speed up scrolling if CTRL is down + { + fast = true; + } + + // Lock to prevent oracle callbacks + std::unique_ptr<IEnvironment::ILock> environmentLock(context->GetEnvironment().AcquireLock()); + + if (event.wheel.y > 0) + { + core_->HandleMouseWheel(*environmentLock, MouseWheelDirection_Up, fast); + } + else if (event.wheel.y < 0) + { + core_->HandleMouseWheel(*environmentLock, MouseWheelDirection_Down, fast); + } + } else if (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEMOTION || event.type == SDL_MOUSEBUTTONUP) { - std::unique_ptr<IViewport::ILock> viewportLock(viewport_->Lock()); + bool hasPointer; + PointerEvent p; + unsigned int canvasWidth, canvasHeight; + + { + std::unique_ptr<IViewport::ILock> viewportLock(viewport_->Lock()); - if (viewportLock->HasCompositor()) + if (viewportLock->HasCompositor()) + { + canvasWidth = viewportLock->GetCompositor().GetCanvasWidth(); + canvasHeight = viewportLock->GetCompositor().GetCanvasHeight(); + OrthancStoneHelpers::GetPointerEvent(p, viewportLock->GetCompositor(), event, keyboardState, scancodeCount); + hasPointer = true; + } + } + + if (hasPointer) { - PointerEvent p; - OrthancStoneHelpers::GetPointerEvent(p, viewportLock->GetCompositor(), event, keyboardState, scancodeCount); + // Lock to prevent oracle callbacks + std::unique_ptr<IEnvironment::ILock> environmentLock(context->GetEnvironment().AcquireLock()); switch (event.type) { case SDL_MOUSEBUTTONDOWN: - controller_->HandleMousePress(p, viewportLock->GetCompositor().GetCanvasWidth(), - viewportLock->GetCompositor().GetCanvasHeight()); + controller_->HandleMousePress(p, canvasWidth, canvasHeight); break; case SDL_MOUSEMOTION: @@ -135,6 +166,9 @@ if (paint) { + // Lock to prevent oracle callbacks + std::unique_ptr<IEnvironment::ILock> environmentLock(context->GetEnvironment().AcquireLock()); + viewport_->Paint(controller_->GetScene()); }
--- a/Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -141,7 +141,7 @@ viewportLock->GetCompositor().SetFont(0, font, 16, Orthanc::Encoding_Latin1); } - core_->CreateComponents(GetContext(), viewport_, viewport_->GetController()); + core_->CreateComponents(GetContext(), viewport_->GetController()); viewport_->GetController()->AcquireInteractor(core_->CreateMouseInteractor());
--- a/OrthancStone/Sources/Platforms/Native/ThreadedOracle.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/OrthancStone/Sources/Platforms/Native/ThreadedOracle.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -153,6 +153,7 @@ ThreadedOracle::ThreadedOracle(const StoneApplication::Configuration& configuration) : + state_(State_Initializing), configuration_(configuration), sleepingThread_(new SleepRunnable, configuration.GetWorkersTimeResolution()) { @@ -174,15 +175,35 @@ void ThreadedOracle::Start() { - sleepingThread_.Start(); - threadPool_.Start(); + boost::mutex::scoped_lock lock(mutex_); + + if (state_ == State_Initializing) + { + sleepingThread_.Start(); + threadPool_.Start(); + state_ = State_Running; + } + else + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } } void ThreadedOracle::Stop() { - threadPool_.Stop(); - sleepingThread_.Stop(); + boost::mutex::scoped_lock lock(mutex_); + + if (state_ == State_Running) + { + threadPool_.Stop(); + sleepingThread_.Stop(); + state_ = State_Stopped; + } + else + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } } @@ -228,6 +249,25 @@ throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); } - SubmitInternal(new OracleCallback(environment, client, protection.release())); + { + boost::mutex::scoped_lock lock(mutex_); + + switch (state_) + { + case State_Initializing: + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + + case State_Running: + SubmitInternal(new OracleCallback(environment, client, protection.release())); + break; + + case State_Stopped: + // The application is in the shutdown process + break; + + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + } } }
--- a/OrthancStone/Sources/Platforms/Native/ThreadedOracle.h Mon Nov 09 18:19:22 2026 +0100 +++ b/OrthancStone/Sources/Platforms/Native/ThreadedOracle.h Thu Nov 12 18:03:35 2026 +0100 @@ -54,9 +54,18 @@ class ThreadedOracle : public IOracle { private: + enum State + { + State_Initializing, + State_Running, + State_Stopped + }; + class GenericRunnable; class SleepRunnable; + boost::mutex mutex_; + State state_; StoneApplication::Configuration configuration_; Orthanc::PeriodicRunnableThread sleepingThread_; Orthanc::ThreadPool threadPool_;
--- a/OrthancStone/Sources/Platforms/Sdl/SdlViewport.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/OrthancStone/Sources/Platforms/Sdl/SdlViewport.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -103,12 +103,11 @@ #if ORTHANC_ENABLE_OPENGL == 1 - uint32_t SdlOpenGLViewport::GetSdlWindowId() + bool SdlOpenGLViewport::IsEventFromThisWindow(const SDL_Event &event) { const SdlWindow& sdlWindowWrapper = context_.GetWindow(); SDL_Window* sdlWindow = sdlWindowWrapper.GetObject(); - Uint32 sdlWindowId = SDL_GetWindowID(sdlWindow); - return sdlWindowId; + return event.window.windowID == SDL_GetWindowID(sdlWindow); } #endif @@ -165,12 +164,11 @@ SDL_FreeSurface(sdlSurface_); } } - - uint32_t SdlCairoViewport::GetSdlWindowId() + + bool SdlCairoViewport::IsEventFromThisWindow(const SDL_Event &event) { SDL_Window* sdlWindow = window_.GetObject(); - Uint32 sdlWindowId = SDL_GetWindowID(sdlWindow); - return sdlWindowId; + return event.window.windowID == SDL_GetWindowID(sdlWindow); } void SdlCairoViewport::Paint(const Scene2D& scene)
--- a/OrthancStone/Sources/Platforms/Sdl/SdlViewport.h Mon Nov 09 18:19:22 2026 +0100 +++ b/OrthancStone/Sources/Platforms/Sdl/SdlViewport.h Thu Nov 12 18:03:35 2026 +0100 @@ -45,7 +45,6 @@ #include <SDL_events.h> #include <boost/thread/recursive_mutex.hpp> -#include <boost/enable_shared_from_this.hpp> namespace OrthancStone { @@ -102,6 +101,9 @@ protected: SdlViewport(); + void UpdateSize(unsigned int width, + unsigned int height); + public: bool IsRefreshEvent(const SDL_Event& event) const { @@ -113,10 +115,7 @@ return new SdlLock(*this); } - virtual uint32_t GetSdlWindowId() = 0; - - void UpdateSize(unsigned int width, - unsigned int height); + virtual bool IsEventFromThisWindow(const SDL_Event& event) = 0; virtual void ToggleMaximize() = 0; @@ -142,7 +141,7 @@ virtual ~SdlOpenGLViewport(); - virtual uint32_t GetSdlWindowId() ORTHANC_OVERRIDE; + virtual bool IsEventFromThisWindow(const SDL_Event& event) ORTHANC_OVERRIDE; virtual void Paint(const Scene2D& scene) ORTHANC_OVERRIDE; @@ -170,7 +169,7 @@ virtual ~SdlCairoViewport(); - virtual uint32_t GetSdlWindowId() ORTHANC_OVERRIDE; + virtual bool IsEventFromThisWindow(const SDL_Event& event) ORTHANC_OVERRIDE; virtual void Paint(const Scene2D& scene) ORTHANC_OVERRIDE;
--- a/OrthancStone/Sources/Scene2D/Internals/OpenGLFloatTextureProgram.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/OrthancStone/Sources/Scene2D/Internals/OpenGLFloatTextureProgram.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -88,7 +88,6 @@ else { slope_ = (maxValue - minValue) / 65536.0f; - assert(!LinearAlgebra::IsCloseToZero(slope_)); } const unsigned int width = texture.GetWidth();
--- a/OrthancStone/Sources/Scene2DViewport/ViewportController.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/OrthancStone/Sources/Scene2DViewport/ViewportController.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -336,13 +336,10 @@ { std::unique_ptr<IViewportInteractor> protection(interactor); - if (interactor == NULL) + activeTracker_.reset(); + + if (interactor != NULL) { - throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); - } - else - { - activeTracker_.reset(); interactor_.reset(protection.release()); } }
--- a/OrthancStone/Sources/Volumes/VolumeSceneLayerSource.cpp Mon Nov 09 18:19:22 2026 +0100 +++ b/OrthancStone/Sources/Volumes/VolumeSceneLayerSource.cpp Thu Nov 12 18:03:35 2026 +0100 @@ -47,8 +47,7 @@ lastPlane_.reset(NULL); } - VolumeSceneLayerSource::VolumeSceneLayerSource(Scene2D& scene, - int layerDepth, + VolumeSceneLayerSource::VolumeSceneLayerSource(int layerDepth, const boost::shared_ptr<IVolumeSlicer>& slicer) : layerDepth_(layerDepth), slicer_(slicer), @@ -59,14 +58,6 @@ { throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); } - - if (scene.HasLayer(layerDepth_)) - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); - } - - // we need to book the scene layer depth by adding a dummy layer - scene.SetLayer(layerDepth_, new NullLayer); }
--- a/OrthancStone/Sources/Volumes/VolumeSceneLayerSource.h Mon Nov 09 18:19:22 2026 +0100 +++ b/OrthancStone/Sources/Volumes/VolumeSceneLayerSource.h Thu Nov 12 18:03:35 2026 +0100 @@ -42,7 +42,7 @@ { private: int layerDepth_; - boost::shared_ptr<IVolumeSlicer> slicer_; + boost::shared_ptr<IVolumeSlicer> slicer_; // TODO Refactoring - Should be unique_ptr<> std::unique_ptr<ILayerStyleConfigurator> configurator_; std::unique_ptr<CoordinateSystem3D> lastPlane_; uint64_t lastRevision_; @@ -51,8 +51,7 @@ void ClearLayer(Scene2D& scene); public: - VolumeSceneLayerSource(Scene2D& scene, - int layerDepth, + VolumeSceneLayerSource(int layerDepth, const boost::shared_ptr<IVolumeSlicer>& slicer); const IVolumeSlicer& GetSlicer() const
