changeset 582:e36e69a380a5

Scene2D
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 19 Apr 2019 16:43:08 +0200
parents d933fc19214a
children f9ac154c5a63
files Framework/OpenGL/OpenGLIncludes.h Framework/OpenGL/OpenGLProgram.h Framework/OpenGL/OpenGLShader.h Framework/OpenGL/OpenGLTexture.h Framework/Scene2D/ISceneLayer.h Framework/Scene2D/Scene2D.cpp Framework/Scene2D/Scene2D.h Resources/CMake/OrthancStoneConfiguration.cmake
diffstat 8 files changed, 339 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/OpenGL/OpenGLIncludes.h	Fri Apr 19 16:43:08 2019 +0200
@@ -0,0 +1,41 @@
+/**
+ * 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/>.
+ **/
+
+
+#pragma once
+
+#if !defined(ORTHANC_ENABLE_OPENGL)
+#  error The macro ORTHANC_ENABLE_OPENGL must be defined
+#endif
+
+#if ORTHANC_ENABLE_OPENGL != 1
+#  error Support for OpenGL is disabled
+#endif
+
+#if defined(__APPLE__)
+#  include <OpenGL/gl.h>
+#  include <OpenGL/glext.h>
+#else
+#  if defined(_WIN32)
+#    include <windows.h>
+#  endif
+#  include <GL/gl.h>
+#  include <GL/glext.h>
+#endif
--- a/Framework/OpenGL/OpenGLProgram.h	Fri Apr 19 16:17:13 2019 +0200
+++ b/Framework/OpenGL/OpenGLProgram.h	Fri Apr 19 16:43:08 2019 +0200
@@ -21,21 +21,7 @@
 
 #pragma once
 
-#if !defined(ORTHANC_ENABLE_OPENGL)
-#  error The macro ORTHANC_ENABLE_OPENGL must be defined
-#endif
-
-#if ORTHANC_ENABLE_OPENGL != 1
-#  error Support for OpenGL is disabled
-#endif
-
-#if defined(__APPLE__)
-#  include <OpenGL/gl.h>
-#  include <OpenGL/glext.h>
-#else
-#  include <GL/gl.h>
-#  include <GL/glext.h>
-#endif
+#include "OpenGLIncludes.h"
 
 #include <string>
 #include <boost/noncopyable.hpp>
--- a/Framework/OpenGL/OpenGLShader.h	Fri Apr 19 16:17:13 2019 +0200
+++ b/Framework/OpenGL/OpenGLShader.h	Fri Apr 19 16:43:08 2019 +0200
@@ -21,21 +21,7 @@
 
 #pragma once
 
-#if !defined(ORTHANC_ENABLE_OPENGL)
-#  error The macro ORTHANC_ENABLE_OPENGL must be defined
-#endif
-
-#if ORTHANC_ENABLE_OPENGL != 1
-#  error Support for OpenGL is disabled
-#endif
-
-#if defined(__APPLE__)
-#  include <OpenGL/gl.h>
-#  include <OpenGL/glext.h>
-#else
-#  include <GL/gl.h>
-#  include <GL/glext.h>
-#endif
+#include "OpenGLIncludes.h"
 
 #include <string>
 #include <boost/noncopyable.hpp>
--- a/Framework/OpenGL/OpenGLTexture.h	Fri Apr 19 16:17:13 2019 +0200
+++ b/Framework/OpenGL/OpenGLTexture.h	Fri Apr 19 16:43:08 2019 +0200
@@ -21,21 +21,7 @@
 
 #pragma once
 
-#if !defined(ORTHANC_ENABLE_OPENGL)
-#  error The macro ORTHANC_ENABLE_OPENGL must be defined
-#endif
-
-#if ORTHANC_ENABLE_OPENGL != 1
-#  error Support for OpenGL is disabled
-#endif
-
-#if defined(__APPLE__)
-#  include <OpenGL/gl.h>
-#  include <OpenGL/glext.h>
-#else
-#  include <GL/gl.h>
-#  include <GL/glext.h>
-#endif
+#include "OpenGLIncludes.h"
 
 #include <Core/Images/ImageAccessor.h>
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/Scene2D/ISceneLayer.h	Fri Apr 19 16:43:08 2019 +0200
@@ -0,0 +1,54 @@
+/**
+ * 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/>.
+ **/
+
+
+#pragma once
+
+#include "../Toolbox/Extent2D.h"
+
+#include <boost/noncopyable.hpp>
+#include <stdint.h>
+
+namespace OrthancStone
+{
+  class ISceneLayer : public boost::noncopyable
+  {
+  public:
+    enum Type
+    {
+      Type_InfoPanel,
+      Type_Texture,
+      Type_Polyline,
+      Type_Text
+    };
+
+    virtual ~ISceneLayer()
+    {
+    }
+
+    virtual ISceneLayer* Clone() const = 0;
+
+    virtual Type GetType() const = 0;
+
+    virtual bool GetBoundingBox(Extent2D& target) const = 0;
+
+    virtual uint64_t GetRevision() const = 0;
+  };
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/Scene2D/Scene2D.cpp	Fri Apr 19 16:43:08 2019 +0200
@@ -0,0 +1,151 @@
+/**
+ * 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 "Scene2D.h"
+
+#include <Core/OrthancException.h>
+
+
+namespace OrthancStone
+{
+  Scene2D::Scene2D(const Scene2D& other) :
+    sceneToCanvas_(other.sceneToCanvas_),
+    canvasToScene_(other.canvasToScene_)
+  {
+    for (Content::const_iterator it = other.content_.begin();
+         it != other.content_.end(); ++it)
+    {
+      content_[it->first] = it->second->Clone();
+    }
+  }
+
+    
+  Scene2D::~Scene2D()
+  {
+    for (Content::iterator it = content_.begin(); 
+         it != content_.end(); ++it)
+    {
+      assert(it->second != NULL);
+      delete it->second;
+    }
+  }
+
+
+  void Scene2D::SetLayer(int depth,
+                         ISceneLayer* layer)  // Takes ownership
+  {
+    std::auto_ptr<ISceneLayer> protection(layer);
+
+    if (layer == NULL)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
+    }
+
+    Content::iterator found = content_.find(depth);
+
+    if (found == content_.end())
+    {
+      content_[depth] = protection.release();
+    }
+    else
+    {
+      assert(found->second != NULL);
+      delete found->second;
+      found->second = protection.release();
+    }
+  }
+
+
+  void Scene2D::DeleteLayer(int depth)
+  {
+    Content::iterator found = content_.find(depth);
+
+    if (found != content_.end())
+    {
+      assert(found->second != NULL);
+      delete found->second;
+      content_.erase(found);
+    }    
+  }
+
+  
+  void Scene2D::Apply(IVisitor& visitor)
+  {
+    for (Content::const_iterator it = content_.begin(); 
+         it != content_.end(); ++it)
+    {
+      assert(it->second != NULL);
+      visitor.Visit(*it->second, it->first);
+    }
+  }
+
+
+  void Scene2D::SetSceneToCanvasTransform(const AffineTransform2D& transform)
+  {
+    // Make sure the transform is invertible before making any change
+    AffineTransform2D inverse = AffineTransform2D::Invert(transform);
+
+    sceneToCanvas_ = transform;
+    canvasToScene_ = inverse;
+  }
+
+
+  void Scene2D::FitContent(unsigned int canvasWidth,
+                           unsigned int canvasHeight)
+  {
+    Extent2D extent;
+
+    for (Content::const_iterator it = content_.begin(); 
+         it != content_.end(); ++it)
+    {
+      assert(it->second != NULL);
+      
+      Extent2D tmp;
+      if (it->second->GetBoundingBox(tmp))
+      {
+        extent.Union(tmp);
+      }
+    }
+
+    if (!extent.IsEmpty())
+    {
+      double zoomX = static_cast<double>(canvasWidth) / extent.GetWidth();
+      double zoomY = static_cast<double>(canvasHeight) / extent.GetHeight();
+
+      double zoom = std::min(zoomX, zoomY);
+      if (LinearAlgebra::IsCloseToZero(zoom))
+      {
+        zoom = 1;
+      }
+
+      double panX = extent.GetCenterX();
+      double panY = extent.GetCenterY();
+
+      // Bring the center of the scene to (0,0)
+      AffineTransform2D t1 = AffineTransform2D::CreateOffset(-panX, -panY);
+      
+      // Scale the scene
+      AffineTransform2D t2 = AffineTransform2D::CreateScaling(zoom, zoom);
+
+      SetSceneToCanvasTransform(AffineTransform2D::Combine(t2, t1));
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/Scene2D/Scene2D.h	Fri Apr 19 16:43:08 2019 +0200
@@ -0,0 +1,89 @@
+/**
+ * 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/>.
+ **/
+
+
+#pragma once
+
+#include "ISceneLayer.h"
+#include "../Toolbox/AffineTransform2D.h"
+
+#include <map>
+
+namespace OrthancStone
+{
+  class Scene2D : public boost::noncopyable
+  {
+  public:
+    class IVisitor : public boost::noncopyable
+    {
+    public:
+      virtual ~IVisitor()
+      {
+      }
+
+      virtual void Visit(const ISceneLayer& layer,
+                         int depth) = 0;
+    };
+
+  private:
+    typedef std::map<int, ISceneLayer*>  Content;
+
+    Content    content_;
+
+    AffineTransform2D  sceneToCanvas_;
+    AffineTransform2D  canvasToScene_;
+
+    Scene2D(const Scene2D& other);
+    
+  public:
+    Scene2D()
+    {
+    }
+    
+    ~Scene2D();
+
+    Scene2D* Clone() const
+    {
+      return new Scene2D(*this);
+    }
+
+    void SetLayer(int depth,
+                  ISceneLayer* layer);  // Takes ownership
+
+    void DeleteLayer(int depth);
+
+    void Apply(IVisitor& visitor);
+
+    const AffineTransform2D& GetSceneToCanvasTransform() const
+    {
+      return sceneToCanvas_;
+    }
+
+    const AffineTransform2D& GetCanvasToSceneTransform() const
+    {
+      return canvasToScene_;
+    }
+
+    void SetSceneToCanvasTransform(const AffineTransform2D& transform);
+
+    void FitContent(unsigned int canvasWidth,
+                    unsigned int canvasHeight);
+  };
+}
--- a/Resources/CMake/OrthancStoneConfiguration.cmake	Fri Apr 19 16:17:13 2019 +0200
+++ b/Resources/CMake/OrthancStoneConfiguration.cmake	Fri Apr 19 16:43:08 2019 +0200
@@ -289,6 +289,7 @@
   ${ORTHANC_STONE_ROOT}/Framework/Radiography/RadiographyTextLayer.cpp
   ${ORTHANC_STONE_ROOT}/Framework/Radiography/RadiographyWidget.cpp
   ${ORTHANC_STONE_ROOT}/Framework/Radiography/RadiographyWindowingTracker.cpp
+  ${ORTHANC_STONE_ROOT}/Framework/Scene2D/Scene2D.cpp
   ${ORTHANC_STONE_ROOT}/Framework/SmartLoader.cpp
   ${ORTHANC_STONE_ROOT}/Framework/StoneEnumerations.cpp
   ${ORTHANC_STONE_ROOT}/Framework/StoneException.h