view Samples/WebAssembly/BasicMPR.cpp @ 826:2de01660debe

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 29 May 2019 16:48:56 +0200
parents 9a6c7a5dcb76
children d71cf8504159
line wrap: on
line source

/**
 * Stone of Orthanc
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2019 Osimis S.A., 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 "dev.h"

#include <emscripten.h>

#include "../../Framework/Loaders/OrthancSeriesVolumeProgressiveLoader.h"
#include "../../Framework/Oracle/SleepOracleCommand.h"
#include "../../Framework/Oracle/WebAssemblyOracle.h"
#include "../../Framework/Scene2D/GrayscaleStyleConfigurator.h"
#include "../../Framework/StoneInitialization.h"
#include "../../Framework/Volumes/VolumeSceneLayerSource.h"


namespace OrthancStone
{
  class VolumeSlicerWidget : public IObserver
  {
  private:
    OrthancStone::WebAssemblyViewport      viewport_;
    std::auto_ptr<VolumeSceneLayerSource>  source_;
    VolumeProjection                       projection_;
    std::vector<CoordinateSystem3D>        planes_;
    size_t                                 currentPlane_;

    void Handle(const DicomVolumeImage::GeometryReadyMessage& message)
    {
      LOG(INFO) << "Geometry is available";

      const VolumeImageGeometry& geometry = message.GetOrigin().GetGeometry();

      const unsigned int depth = geometry.GetProjectionDepth(projection_);
      currentPlane_ = depth / 2;
      
      planes_.resize(depth);

      for (unsigned int z = 0; z < depth; z++)
      {
        planes_[z] = geometry.GetProjectionSlice(projection_, z);
      }

      Refresh();

      viewport_.FitContent();
    }
    
  public:
    VolumeSlicerWidget(MessageBroker& broker,
                         const std::string& canvas,
                         VolumeProjection projection) :
      IObserver(broker),
      viewport_(broker, canvas),
      projection_(projection),
      currentPlane_(0)
    {
    }

    void UpdateSize()
    {
      viewport_.UpdateSize();
    }

    void SetSlicer(int layerDepth,
                   const boost::shared_ptr<IVolumeSlicer>& slicer,
                   IObservable& loader,
                   ILayerStyleConfigurator* configurator)
    {
      if (source_.get() != NULL)
      {
        throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls,
                                        "Only one slicer can be registered");
      }
      
      loader.RegisterObserverCallback(
        new Callable<VolumeSlicerWidget, DicomVolumeImage::GeometryReadyMessage>
        (*this, &VolumeSlicerWidget::Handle));

      source_.reset(new VolumeSceneLayerSource(viewport_.GetScene(), layerDepth, slicer));

      if (configurator != NULL)
      {
        source_->SetConfigurator(configurator);
      }
    }    

    void Refresh()
    {
      if (source_.get() != NULL &&
          currentPlane_ < planes_.size())
      {
        source_->Update(planes_[currentPlane_]);
        viewport_.Refresh();
      }
    }

    size_t GetSlicesCount() const
    {
      return planes_.size();
    }

    void Scroll(int delta)
    {
      if (!planes_.empty())
      {
        int tmp = static_cast<int>(currentPlane_) + delta;
        unsigned int next;

        if (tmp < 0)
        {
          next = 0;
        }
        else if (tmp >= static_cast<int>(planes_.size()))
        {
          next = planes_.size() - 1;
        }
        else
        {
          next = static_cast<size_t>(tmp);
        }

        if (next != currentPlane_)
        {
          currentPlane_ = next;
          Refresh();
        }
      }
    }
  };
}




boost::shared_ptr<OrthancStone::DicomVolumeImage>  ct_(new OrthancStone::DicomVolumeImage);

boost::shared_ptr<OrthancStone::OrthancSeriesVolumeProgressiveLoader>  loader_;

std::auto_ptr<OrthancStone::VolumeSlicerWidget>  widget1_;
std::auto_ptr<OrthancStone::VolumeSlicerWidget>  widget2_;
std::auto_ptr<OrthancStone::VolumeSlicerWidget>  widget3_;

OrthancStone::MessageBroker  broker_;
OrthancStone::WebAssemblyOracle  oracle_(broker_);


EM_BOOL OnWindowResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
{
  try
  {
    if (widget1_.get() != NULL)
    {
      widget1_->UpdateSize();
    }
  
    if (widget2_.get() != NULL)
    {
      widget2_->UpdateSize();
    }
  
    if (widget3_.get() != NULL)
    {
      widget3_->UpdateSize();
    }
  }
  catch (Orthanc::OrthancException& e)
  {
    LOG(ERROR) << "Exception while updating canvas size: " << e.What();
  }
  
  return true;
}




EM_BOOL OnAnimationFrame(double time, void *userData)
{
  try
  {
    if (widget1_.get() != NULL)
    {
      widget1_->Refresh();
    }
  
    if (widget2_.get() != NULL)
    {
      widget2_->Refresh();
    }
  
    if (widget3_.get() != NULL)
    {
      widget3_->Refresh();
    }

    return true;
  }
  catch (Orthanc::OrthancException& e)
  {
    LOG(ERROR) << "Exception in the animation loop, stopping now: " << e.What();
    return false;
  }  
}


static bool ctrlDown_ = false;


EM_BOOL OnMouseWheel(int eventType,
                     const EmscriptenWheelEvent *wheelEvent,
                     void *userData)
{
  try
  {
    if (userData != NULL)
    {
      int delta = 0;

      if (wheelEvent->deltaY < 0)
      {
        delta = -1;
      }
           
      if (wheelEvent->deltaY > 0)
      {
        delta = 1;
      }

      OrthancStone::VolumeSlicerWidget& widget =
        *reinterpret_cast<OrthancStone::VolumeSlicerWidget*>(userData);
      
      if (ctrlDown_)
      {
        delta *= static_cast<int>(widget.GetSlicesCount() / 10);
      }

      widget.Scroll(delta);
    }
  }
  catch (Orthanc::OrthancException& e)
  {
    LOG(ERROR) << "Exception in the wheel event: " << e.What();
  }
  
  return true;
}


EM_BOOL OnKeyDown(int eventType,
                  const EmscriptenKeyboardEvent *keyEvent,
                  void *userData)
{
  ctrlDown_ = keyEvent->ctrlKey;
  return false;
}


EM_BOOL OnKeyUp(int eventType,
                const EmscriptenKeyboardEvent *keyEvent,
                void *userData)
{
  ctrlDown_ = false;
  return false;
}




namespace OrthancStone
{
  class TestSleep : public IObserver
  {
  private:
    WebAssemblyOracle&  oracle_;

    void Schedule()
    {
      oracle_.Schedule(*this, new OrthancStone::SleepOracleCommand(2000));
    }
    
    void Handle(const SleepOracleCommand::TimeoutMessage& message)
    {
      LOG(INFO) << "TIMEOUT";
      Schedule();
    }
    
  public:
    TestSleep(MessageBroker& broker,
              WebAssemblyOracle& oracle) :
      IObserver(broker),
      oracle_(oracle)
    {
      oracle.RegisterObserverCallback(
        new Callable<TestSleep, SleepOracleCommand::TimeoutMessage>
        (*this, &TestSleep::Handle));

      LOG(INFO) << "STARTING";
      Schedule();
    }
  };

  static TestSleep testSleep(broker_, oracle_);
}


extern "C"
{
  int main(int argc, char const *argv[]) 
  {
    OrthancStone::StoneInitialize();
    Orthanc::Logging::EnableInfoLevel(true);
    // Orthanc::Logging::EnableTraceLevel(true);
    EM_ASM(window.dispatchEvent(new CustomEvent("WebAssemblyLoaded")););
  }

  EMSCRIPTEN_KEEPALIVE
  void Initialize()
  {
    try
    {
      loader_.reset(new OrthancStone::OrthancSeriesVolumeProgressiveLoader(ct_, oracle_, oracle_));
    
      widget1_.reset(new OrthancStone::VolumeSlicerWidget(broker_, "mycanvas1", OrthancStone::VolumeProjection_Axial));
      widget1_->SetSlicer(0, loader_, *loader_, new OrthancStone::GrayscaleStyleConfigurator);
      widget1_->UpdateSize();

      widget2_.reset(new OrthancStone::VolumeSlicerWidget(broker_, "mycanvas2", OrthancStone::VolumeProjection_Coronal));
      widget2_->SetSlicer(0, loader_, *loader_, new OrthancStone::GrayscaleStyleConfigurator);
      widget2_->UpdateSize();

      widget3_.reset(new OrthancStone::VolumeSlicerWidget(broker_, "mycanvas3", OrthancStone::VolumeProjection_Sagittal));
      widget3_->SetSlicer(0, loader_, *loader_, new OrthancStone::GrayscaleStyleConfigurator);
      widget3_->UpdateSize();

      emscripten_set_resize_callback("#window", NULL, false, OnWindowResize);

      emscripten_set_wheel_callback("mycanvas1", widget1_.get(), false, OnMouseWheel);
      emscripten_set_wheel_callback("mycanvas2", widget2_.get(), false, OnMouseWheel);
      emscripten_set_wheel_callback("mycanvas3", widget3_.get(), false, OnMouseWheel);

      emscripten_set_keydown_callback("#window", NULL, false, OnKeyDown);
      emscripten_set_keyup_callback("#window", NULL, false, OnKeyUp);
    
      emscripten_request_animation_frame_loop(OnAnimationFrame, NULL);

      loader_->LoadSeries("a04ecf01-79b2fc33-58239f7e-ad9db983-28e81afa");
    }
    catch (Orthanc::OrthancException& e)
    {
      LOG(ERROR) << "Exception during Initialize(): " << e.What();
    }
  }
}