changeset 2405:ea776b77ba13 refactoring

VolumeViewerCore running in wasm
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 15 Nov 2026 13:36:15 +0100
parents ae04ac9a48af
children 063855278b85
files Applications/Samples/Common/VolumeViewerCore.cpp Applications/Samples/Common/VolumeViewerCore.h Applications/Samples/Sdl/CMakeLists.txt Applications/Samples/Sdl/SdlVolumeViewer.cpp Applications/Samples/Sdl/SingleViewportSdlApplication.cpp Applications/Samples/WebAssembly/CMakeLists.txt Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp
diffstat 7 files changed, 334 insertions(+), 205 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Applications/Samples/Common/VolumeViewerCore.cpp	Sun Nov 15 13:36:15 2026 +0100
@@ -0,0 +1,215 @@
+/**
+ * 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 "VolumeViewerCore.h"
+
+#include "../../../OrthancStone/Sources/Scene2D/GrayscaleStyleConfigurator.h"
+#include "../../../OrthancStone/Sources/Viewport/DefaultViewportInteractor.h"
+#include "../../../OrthancStone/Sources/Volumes/DicomVolumeImageMPRSlicer.h"
+
+
+namespace OrthancStone
+{
+  void VolumeViewerCore::Invalidate()
+  {
+    if (currentPlane_ < planes_.size())
+    {
+      source_->Update(controller_->GetScene(), planes_[currentPlane_]);
+    }
+
+    controller_->InvalidateViewport();
+  }
+
+
+  void VolumeViewerCore::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 VolumeViewerCore::Handle(const OrthancStone::New::IObservable& source,
+                                const DicomVolumeImage::GeometryReadyMessage& message)
+  {
+    LOG(WARNING) << "Dimension of the volume: "
+                 << volume_->GetGeometry().GetWidth() << "x"
+                 << volume_->GetGeometry().GetHeight() << "x"
+                 << volume_->GetGeometry().GetDepth();
+    UpdateProjection();
+  }
+
+
+  void VolumeViewerCore::Handle(const OrthancStone::New::IObservable& source,
+                                const DicomVolumeImage::ContentUpdatedMessage& message)
+  {
+    Invalidate();
+  }
+
+
+  void VolumeViewerCore::CreateComponents(const boost::shared_ptr<StoneApplication::Context>& context,
+                                          const boost::shared_ptr<ViewportController>& controller)
+  {
+    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_);
+
+    New::IObservable::Bind<DicomVolumeImage::GeometryReadyMessage>(*seriesLoader_, shared_from_this());
+    New::IObservable::Bind<DicomVolumeImage::ContentUpdatedMessage>(*seriesLoader_, shared_from_this());
+
+    New::IObservable::Bind<DicomVolumeImage::GeometryReadyMessage>(*instanceLoader_, shared_from_this());
+    New::IObservable::Bind<DicomVolumeImage::ContentUpdatedMessage>(*instanceLoader_, shared_from_this());
+  }
+
+
+  IViewportInteractor* VolumeViewerCore::CreateMouseInteractor()
+  {
+    return new DefaultViewportInteractor;
+  }
+
+
+  void VolumeViewerCore::HandleKeyDown(const IEnvironment::ILock& environmentLock,
+                                       char key)
+  {
+    switch (key)
+    {
+      case 'a':
+        projection_ = VolumeProjection_Axial;
+        UpdateProjection();
+        break;
+
+      case 'c':
+        projection_ = VolumeProjection_Coronal;
+        UpdateProjection();
+        break;
+
+      case 's':
+        projection_ = VolumeProjection_Sagittal;
+        UpdateProjection();
+        break;
+    }
+  }
+
+
+  void VolumeViewerCore::HandleMouseWheel(const IEnvironment::ILock& environmentLock,
+                                          MouseWheelDirection direction,
+                                          bool fast)
+  {
+    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();
+    }
+  }
+
+
+  VolumeViewerCore::VolumeViewerCore() :
+    // Dummy initialization
+    projection_(VolumeProjection_Axial),
+    currentPlane_(0)
+  {
+  }
+
+
+  void VolumeViewerCore::LoadSeriesFromOrthanc(VolumeProjection projection,
+                                               const std::string& seriesId)
+  {
+    projection_ = projection;
+    seriesLoader_->LoadSeries(seriesId);
+  }
+
+
+  void VolumeViewerCore::LoadInstanceFromOrthanc(VolumeProjection projection,
+                                                 const std::string& instanceId)
+  {
+    projection_ = projection;
+    instanceLoader_->LoadInstance(instanceId);
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Applications/Samples/Common/VolumeViewerCore.h	Sun Nov 15 13:36:15 2026 +0100
@@ -0,0 +1,85 @@
+/**
+ * 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/>.
+ **/
+
+
+#pragma once
+
+#include "ISingleViewportApplicationCore.h"
+#include "../../../OrthancStone/Sources/Loaders/OrthancMultiframeVolumeLoader.h"
+#include "../../../OrthancStone/Sources/Loaders/OrthancSeriesVolumeProgressiveLoader.h"
+#include "../../../OrthancStone/Sources/Volumes/VolumeSceneLayerSource.h"
+
+
+namespace OrthancStone
+{
+  class VolumeViewerCore :
+    public ISingleViewportApplicationCore,
+    public boost::enable_shared_from_this<VolumeViewerCore>,
+    public New::TypedObserver<DicomVolumeImage::GeometryReadyMessage>,
+    public New::TypedObserver<DicomVolumeImage::ContentUpdatedMessage>
+  {
+  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();
+
+    void UpdateProjection();
+
+  protected:
+    virtual void Handle(const OrthancStone::New::IObservable& source,
+                        const DicomVolumeImage::GeometryReadyMessage& message) ORTHANC_OVERRIDE;
+
+    virtual void Handle(const OrthancStone::New::IObservable& source,
+                        const DicomVolumeImage::ContentUpdatedMessage& message) ORTHANC_OVERRIDE;
+
+    virtual void CreateComponents(const boost::shared_ptr<StoneApplication::Context>& context,
+                                  const boost::shared_ptr<ViewportController>& controller) ORTHANC_OVERRIDE;
+
+    virtual IViewportInteractor* CreateMouseInteractor() ORTHANC_OVERRIDE;
+
+    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:
+    VolumeViewerCore();
+
+    void LoadSeriesFromOrthanc(VolumeProjection projection,
+                               const std::string& seriesId);
+
+    void LoadInstanceFromOrthanc(VolumeProjection projection,
+                                 const std::string& instanceId);
+  };
+}
--- a/Applications/Samples/Sdl/CMakeLists.txt	Thu Nov 12 21:55:48 2026 +0100
+++ b/Applications/Samples/Sdl/CMakeLists.txt	Sun Nov 15 13:36:15 2026 +0100
@@ -119,6 +119,7 @@
 project(SdlVolumeViewer)
 
 add_executable(SdlVolumeViewer
+  ../Common/VolumeViewerCore.cpp
   SdlVolumeViewer.cpp
   SingleViewportSdlApplication.cpp
   )
--- a/Applications/Samples/Sdl/SdlVolumeViewer.cpp	Thu Nov 12 21:55:48 2026 +0100
+++ b/Applications/Samples/Sdl/SdlVolumeViewer.cpp	Sun Nov 15 13:36:15 2026 +0100
@@ -20,14 +20,9 @@
  **/
 
 
-#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 "../Common/VolumeViewerCore.h"
 #include "SingleViewportSdlApplication.h"
 
 #include <boost/program_options.hpp>
@@ -36,204 +31,6 @@
 
 namespace OrthancStone
 {
-  class VolumeViewerCore :
-    public ISingleViewportApplicationCore,
-    public boost::enable_shared_from_this<VolumeViewerCore>,
-    public New::TypedObserver<DicomVolumeImage::GeometryReadyMessage>,
-    public New::TypedObserver<DicomVolumeImage::ContentUpdatedMessage>
-  {
-  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();
-    }
-
-    virtual void Handle(const OrthancStone::New::IObservable& source,
-                        const DicomVolumeImage::GeometryReadyMessage& message) ORTHANC_OVERRIDE
-    {
-      LOG(WARNING) << "Dimension of the volume: "
-                   << volume_->GetGeometry().GetWidth() << "x"
-                   << volume_->GetGeometry().GetHeight() << "x"
-                   << volume_->GetGeometry().GetDepth();
-      UpdateProjection();
-    }
-
-    virtual void Handle(const OrthancStone::New::IObservable& source,
-                        const DicomVolumeImage::ContentUpdatedMessage& message) ORTHANC_OVERRIDE
-    {
-      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_);
-
-      New::IObservable::Bind<DicomVolumeImage::GeometryReadyMessage>(*seriesLoader_, shared_from_this());
-      New::IObservable::Bind<DicomVolumeImage::ContentUpdatedMessage>(*seriesLoader_, shared_from_this());
-
-      New::IObservable::Bind<DicomVolumeImage::GeometryReadyMessage>(*instanceLoader_, shared_from_this());
-      New::IObservable::Bind<DicomVolumeImage::ContentUpdatedMessage>(*instanceLoader_, shared_from_this());
-    }
-
-    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_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:
--- a/Applications/Samples/Sdl/SingleViewportSdlApplication.cpp	Thu Nov 12 21:55:48 2026 +0100
+++ b/Applications/Samples/Sdl/SingleViewportSdlApplication.cpp	Sun Nov 15 13:36:15 2026 +0100
@@ -109,6 +109,7 @@
           // Lock to prevent oracle callbacks
           std::unique_ptr<IEnvironment::ILock> environmentLock(context->GetEnvironment().AcquireLock());
 
+          // Note that MouseWheelDirection is reversed wrt. WebAssembly
           if (event.wheel.y > 0)
           {
             core_->HandleMouseWheel(*environmentLock, MouseWheelDirection_Up, fast);
--- a/Applications/Samples/WebAssembly/CMakeLists.txt	Thu Nov 12 21:55:48 2026 +0100
+++ b/Applications/Samples/WebAssembly/CMakeLists.txt	Sun Nov 15 13:36:15 2026 +0100
@@ -150,6 +150,7 @@
 
 add_executable(SingleFrameViewerWasm
   ../Common/SimpleViewerCore.cpp
+  ../Common/VolumeViewerCore.cpp  # TODO Refactoring - Remove
   SingleFrameViewer/SingleFrameViewer.cpp
   )
 
--- a/Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp	Thu Nov 12 21:55:48 2026 +0100
+++ b/Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp	Sun Nov 15 13:36:15 2026 +0100
@@ -21,6 +21,7 @@
 
 
 #include "../../Common/SimpleViewerCore.h"
+#include "../../Common/VolumeViewerCore.h"   // TODO Refactoring - Remove
 
 #include "SingleFrameViewerApplication.h"
 
@@ -101,6 +102,29 @@
       return EM_FALSE; // Don't prevent the browser's default behavior
     }
 
+    static EM_BOOL OnWheel(int eventType,
+                           const EmscriptenWheelEvent *wheelEvent,
+                           void *userData)
+    {
+      SingleViewportWebAssemblyApplication& that = *reinterpret_cast<SingleViewportWebAssemblyApplication*>(userData);
+
+      const bool fast = wheelEvent->mouse.ctrlKey;
+
+      // Note that MouseWheelDirection is reversed wrt. WebAssembly
+      if (wheelEvent->deltaY < 0)
+      {
+        std::unique_ptr<IEnvironment::ILock> environmentLock(that.GetContext()->GetEnvironment().AcquireLock());
+        that.core_->HandleMouseWheel(*environmentLock, MouseWheelDirection_Up, fast);
+      }
+      else if (wheelEvent->deltaY > 0)
+      {
+        std::unique_ptr<IEnvironment::ILock> environmentLock(that.GetContext()->GetEnvironment().AcquireLock());
+        that.core_->HandleMouseWheel(*environmentLock, MouseWheelDirection_Down, fast);
+      }
+
+      return EM_TRUE;
+    }
+
   public:
     SingleViewportWebAssemblyApplication(const Configuration& configuration,
                                          const boost::shared_ptr<ISingleViewportApplicationCore>& core) :
@@ -146,6 +170,7 @@
         viewport_->GetController()->AcquireInteractor(core_->CreateMouseInteractor());
 
         emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, EM_FALSE, OnKeyDown);
+        emscripten_set_wheel_callback(viewport_->GetCanvasCssSelector().c_str(), this, EM_TRUE, OnWheel);
       }
     }
 
@@ -182,7 +207,9 @@
       configuration.SetLocalOrthancRoot("..");
       configuration.SetDicomCacheSize(128 * 1024 * 1024);  // 128MB
 
-      boost::shared_ptr<OrthancStone::SimpleViewerCore> core(new OrthancStone::SimpleViewerCore);
+      boost::shared_ptr<OrthancStone::ISingleViewportApplicationCore> core;
+      core.reset(new OrthancStone::SimpleViewerCore);
+      //core.reset(new OrthancStone::VolumeViewerCore);
       application2_.reset(new OrthancStone::SingleViewportWebAssemblyApplication(configuration, core));
 
 #if 0
@@ -203,6 +230,8 @@
     {
       application2_->Setup(canvasId, true);
 
+      //dynamic_cast<OrthancStone::VolumeViewerCore&>(*application2_->GetCore()).LoadSeriesFromOrthanc(OrthancStone::VolumeProjection_Coronal, "a7e40137-e4b37f56-14ce875b-9a81702d-eac823bf");
+
 #if 0
       if (application_.get() != NULL)
       {