changeset 109:53bd9277b025 wasm

using the Extent class
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 14 Jun 2017 15:34:08 +0200
parents 37d4ae7052a5
children 53025eecbc95
files Framework/Toolbox/Extent.cpp Framework/Toolbox/Extent.h Framework/Toolbox/ViewportGeometry.cpp Framework/Toolbox/ViewportGeometry.h Framework/Widgets/LayerWidget.cpp Framework/Widgets/LayerWidget.h Framework/Widgets/TestWorldSceneWidget.cpp Framework/Widgets/TestWorldSceneWidget.h Framework/Widgets/WorldSceneWidget.cpp Framework/Widgets/WorldSceneWidget.h
diffstat 10 files changed, 73 insertions(+), 124 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Toolbox/Extent.cpp	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Toolbox/Extent.cpp	Wed Jun 14 15:34:08 2017 +0200
@@ -26,6 +26,28 @@
 
 namespace OrthancStone
 {
+  Extent::Extent(double x1,
+                 double y1,
+                 double x2,
+                 double y2) :
+    empty_(false),
+    x1_(x1),
+    y1_(y1),
+    x2_(x2),
+    y2_(y2)
+  {
+    if (x1_ > x2_)
+    {
+      std::swap(x1_, x2_);
+    }
+
+    if (y1_ > y2_)
+    {
+      std::swap(y1_, y2_);
+    }
+  }
+
+
   void Extent::Reset()
   {
     empty_ = true;
@@ -61,18 +83,18 @@
 
   void Extent::Union(const Extent& other)
   {
-    if (other.IsEmpty())
+    if (other.empty_)
     {
       return;
     }
 
-    if (IsEmpty())
+    if (empty_)
     {
       *this = other;
       return;
     }
 
-    assert(!IsEmpty());
+    assert(!empty_);
 
     x1_ = std::min(x1_, other.x1_);
     y1_ = std::min(y1_, other.y1_);
@@ -83,4 +105,19 @@
            y1_ <= y2_);    // This is the invariant of the structure
   }
 
+
+  bool Extent::IsEmpty() const
+  {
+    if (empty_)
+    {
+      return true;
+    }
+    else
+    {
+      assert(x1_ <= x2_ &&
+             y1_ <= y2_);
+      return (x2_ <= x1_ + 10 * std::numeric_limits<double>::epsilon() ||
+              y2_ <= y1_ + 10 * std::numeric_limits<double>::epsilon());
+    }
+  }
 }
--- a/Framework/Toolbox/Extent.h	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Toolbox/Extent.h	Wed Jun 14 15:34:08 2017 +0200
@@ -38,6 +38,11 @@
       Reset();
     }
 
+    Extent(double x1,
+           double y1,
+           double x2,
+           double y2);
+
     void Reset();
 
     void AddPoint(double x,
@@ -45,10 +50,7 @@
 
     void Union(const Extent& other);
 
-    bool IsEmpty() const
-    {
-      return empty_;
-    }
+    bool IsEmpty() const;
 
     double GetX1() const
     {
--- a/Framework/Toolbox/ViewportGeometry.cpp	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Toolbox/ViewportGeometry.cpp	Wed Jun 14 15:34:08 2017 +0200
@@ -43,18 +43,15 @@
     cairo_matrix_multiply(&transform_, &tmp, &transform_);
 
     // Bring the center of the scene to (0,0)
-    cairo_matrix_init_translate(&tmp, -(x1_ + x2_) / 2.0, -(y1_ + y2_) / 2.0);
+    cairo_matrix_init_translate(&tmp,
+                                -(sceneExtent_.GetX1() + sceneExtent_.GetX2()) / 2.0,
+                                -(sceneExtent_.GetY1() + sceneExtent_.GetY2()) / 2.0);
     cairo_matrix_multiply(&transform_, &tmp, &transform_);
   }
 
 
   ViewportGeometry::ViewportGeometry()
   {
-    x1_ = 0;
-    y1_ = 0;
-    x2_ = 0;
-    y2_ = 0;
-
     width_ = 0;
     height_ = 0;
 
@@ -82,46 +79,14 @@
   }
 
 
-  void ViewportGeometry::SetSceneExtent(double x1,
-                                        double y1,
-                                        double x2,
-                                        double y2)
+  void ViewportGeometry::SetSceneExtent(const Extent& extent)
   {
-    if (x1 == x1_ &&
-        y1 == y1_ &&
-        x2 == x2_ &&
-        y2 == y2_)
-    {
-      return;
-    }
-    else if (x1 > x2 || 
-             y1 > y2)
-    {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
-    }
-    else
-    {
-      LOG(INFO) << "New scene extent: (" << x1 << "," << y1 << ") => (" << x2 << "," << y2 << ")";
+    LOG(INFO) << "New scene extent: ("
+              << extent.GetX1() << "," << extent.GetY1() << ") => ("
+              << extent.GetX2() << "," << extent.GetY2() << ")";
 
-      x1_ = x1;
-      y1_ = y1;
-      x2_ = x2;
-      y2_ = y2;
-
-      ComputeTransform();
-    }
-  }
-
-
-  void ViewportGeometry::GetSceneExtent(double& x1,
-                                        double& y1,
-                                        double& x2,
-                                        double& y2) const
-  {
-    x1 = x1_;
-    y1 = y1_;
-    x2 = x2_;
-    y2 = y2_;
+    sceneExtent_ = extent;
+    ComputeTransform();
   }
 
 
@@ -160,11 +125,10 @@
   {
     if (width_ > 0 &&
         height_ > 0 &&
-        x2_ > x1_ + 10 * std::numeric_limits<double>::epsilon() &&
-        y2_ > y1_ + 10 * std::numeric_limits<double>::epsilon())
+        !sceneExtent_.IsEmpty())
     {
-      double zoomX = static_cast<double>(width_) / (x2_ - x1_);
-      double zoomY = static_cast<double>(height_) / (y2_ - y1_);
+      double zoomX = static_cast<double>(width_) / (sceneExtent_.GetX2() - sceneExtent_.GetX1());
+      double zoomY = static_cast<double>(height_) / (sceneExtent_.GetY2() - sceneExtent_.GetY1());
       zoom_ = zoomX < zoomY ? zoomX : zoomY;
 
       panX_ = 0;
--- a/Framework/Toolbox/ViewportGeometry.h	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Toolbox/ViewportGeometry.h	Wed Jun 14 15:34:08 2017 +0200
@@ -22,6 +22,7 @@
 #pragma once
 
 #include "../Viewport/CairoContext.h"
+#include "../Toolbox/Extent.h"
 
 namespace OrthancStone
 {
@@ -29,10 +30,7 @@
   {
   private:
     // Extent of the scene (in world units)
-    double   x1_;
-    double   y1_;
-    double   x2_;
-    double   y2_;
+    Extent   sceneExtent_;
 
     // Size of the display (in pixels)
     unsigned int  width_;
@@ -53,15 +51,12 @@
     void SetDisplaySize(unsigned int width,
                         unsigned int height);
 
-    void SetSceneExtent(double x1,
-                        double y1,
-                        double x2,
-                        double y2);
+    void SetSceneExtent(const Extent& extent);
 
-    void GetSceneExtent(double& x1,
-                        double& y1,
-                        double& x2,
-                        double& y2) const;
+    const Extent& GetSceneExtent() const
+    {
+      return sceneExtent_;
+    }
 
     void MapDisplayToScene(double& sceneX /* out */,
                            double& sceneY /* out */,
--- a/Framework/Widgets/LayerWidget.cpp	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Widgets/LayerWidget.cpp	Wed Jun 14 15:34:08 2017 +0200
@@ -232,10 +232,7 @@
   }
 
         
-  void LayerWidget::GetSceneExtent(double& x1,
-                                   double& y1,
-                                   double& x2,
-                                   double& y2)
+  Extent LayerWidget::GetSceneExtent()
   {
     Extent sceneExtent;
 
@@ -248,36 +245,7 @@
       sceneExtent.Union(layerExtent);
     }
 
-    if (sceneExtent.IsEmpty())
-    {
-      // Set a default extent of (-1,-1) -> (0,0)
-      x1 = -1;
-      y1 = -1;
-      x2 = 1;
-      y2 = 1;
-    }
-    else
-    {
-      x1 = sceneExtent.GetX1();
-      y1 = sceneExtent.GetY1();
-      x2 = sceneExtent.GetX2();
-      y2 = sceneExtent.GetY2();
-
-      // Ensure the extent is non-empty
-      if (x1 >= x2)
-      {
-        double tmp = x1;
-        x1 = tmp - 0.5;
-        x2 = tmp + 0.5;
-      }
-
-      if (y1 >= y2)
-      {
-        double tmp = y1;
-        y1 = tmp - 0.5;
-        y2 = tmp + 0.5;
-      }
-    }
+    return sceneExtent;
   }
 
   
--- a/Framework/Widgets/LayerWidget.h	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Widgets/LayerWidget.h	Wed Jun 14 15:34:08 2017 +0200
@@ -70,10 +70,7 @@
     void ResetChangedLayers();
         
   protected:
-    virtual void GetSceneExtent(double& x1,
-                                double& y1,
-                                double& x2,
-                                double& y2);
+    virtual Extent GetSceneExtent();
  
     virtual bool RenderScene(CairoContext& context,
                              const ViewportGeometry& view);
--- a/Framework/Widgets/TestWorldSceneWidget.cpp	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Widgets/TestWorldSceneWidget.cpp	Wed Jun 14 15:34:08 2017 +0200
@@ -118,15 +118,9 @@
     }
 
 
-    void TestWorldSceneWidget::GetSceneExtent(double& x1,
-                                              double& y1,
-                                              double& x2,
-                                              double& y2)
+    Extent TestWorldSceneWidget::GetSceneExtent()
     {
-      x1 = -10;
-      x2 = 10;
-      y1 = -.5;
-      y2 = .5;
+      return Extent(-10, -.5, 10, .5);
     }
 
 
--- a/Framework/Widgets/TestWorldSceneWidget.h	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Widgets/TestWorldSceneWidget.h	Wed Jun 14 15:34:08 2017 +0200
@@ -43,10 +43,7 @@
     public:
       TestWorldSceneWidget(bool animate);
 
-      virtual void GetSceneExtent(double& x1,
-                                  double& y1,
-                                  double& x2,
-                                  double& y2);
+      virtual Extent GetSceneExtent();
 
       virtual bool HasUpdateContent() const
       {
--- a/Framework/Widgets/WorldSceneWidget.cpp	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Widgets/WorldSceneWidget.cpp	Wed Jun 14 15:34:08 2017 +0200
@@ -232,9 +232,7 @@
 
   void WorldSceneWidget::SetSceneExtent(ViewportGeometry& view)
   {
-    double x1, y1, x2, y2;
-    GetSceneExtent(x1, y1, x2, y2);
-    view.SetSceneExtent(x1, y1, x2, y2);
+    view.SetSceneExtent(GetSceneExtent());
   }
 
 
--- a/Framework/Widgets/WorldSceneWidget.h	Wed Jun 14 15:13:32 2017 +0200
+++ b/Framework/Widgets/WorldSceneWidget.h	Wed Jun 14 15:34:08 2017 +0200
@@ -61,10 +61,7 @@
 
 
   protected:
-    virtual void GetSceneExtent(double& x1,
-                                double& y1,
-                                double& x2,
-                                double& y2) = 0;
+    virtual Extent GetSceneExtent() = 0;
     
     virtual bool RenderScene(CairoContext& context,
                              const ViewportGeometry& view) = 0;