diff Framework/Radiography/RadiographyLayer.cpp @ 430:b85f635f1eb5 am-vsol-upgrade

added serialization for RadiographyScene
author am@osimis.io
date Thu, 29 Nov 2018 15:11:19 +0100
parents 6decc0ba9da5
children 4eb96c6b4e96
line wrap: on
line diff
--- a/Framework/Radiography/RadiographyLayer.cpp	Wed Nov 28 10:46:32 2018 +0100
+++ b/Framework/Radiography/RadiographyLayer.cpp	Thu Nov 29 15:11:19 2018 +0100
@@ -32,18 +32,41 @@
   }
 
 
+  RadiographyLayer::Geometry::Geometry() :
+    hasCrop_(false),
+    panX_(0),
+    panY_(0),
+    angle_(0),
+    resizeable_(false),
+    pixelSpacingX_(1),
+    pixelSpacingY_(1)
+  {
+
+  }
+
+  void RadiographyLayer::Geometry::GetCrop(unsigned int &x, unsigned int &y, unsigned int &width, unsigned int &height) const
+  {
+    if (!hasCrop_)
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);  // you should probably use Radiography::GetCrop() or at least call HasCrop() before
+
+    x = cropX_;
+    y = cropY_;
+    width = cropWidth_;
+    height = cropHeight_;
+  }
+
   void RadiographyLayer::UpdateTransform()
   {
-    transform_ = AffineTransform2D::CreateScaling(pixelSpacingX_, pixelSpacingY_);
+    transform_ = AffineTransform2D::CreateScaling(geometry_.GetPixelSpacingX(), geometry_.GetPixelSpacingY());
 
     double centerX, centerY;
     GetCenter(centerX, centerY);
 
     transform_ = AffineTransform2D::Combine(
-      AffineTransform2D::CreateOffset(panX_ + centerX, panY_ + centerY),
-      AffineTransform2D::CreateRotation(angle_),
-      AffineTransform2D::CreateOffset(-centerX, -centerY),
-      transform_);
+          AffineTransform2D::CreateOffset(geometry_.GetPanX() + centerX, geometry_.GetPanY() + centerY),
+          AffineTransform2D::CreateRotation(geometry_.GetAngle()),
+          AffineTransform2D::CreateOffset(-centerX, -centerY),
+          transform_);
 
     transformInverse_ = AffineTransform2D::Invert(transform_);
   }
@@ -73,28 +96,28 @@
 
     switch (corner)
     {
-      case Corner_TopLeft:
-        x = dx;
-        y = dy;
-        break;
+    case Corner_TopLeft:
+      x = dx;
+      y = dy;
+      break;
 
-      case Corner_TopRight:
-        x = dx + dwidth;
-        y = dy;
-        break;
+    case Corner_TopRight:
+      x = dx + dwidth;
+      y = dy;
+      break;
 
-      case Corner_BottomLeft:
-        x = dx;
-        y = dy + dheight;
-        break;
+    case Corner_BottomLeft:
+      x = dx;
+      y = dy + dheight;
+      break;
 
-      case Corner_BottomRight:
-        x = dx + dwidth;
-        y = dy + dheight;
-        break;
+    case Corner_BottomRight:
+      x = dx + dwidth;
+      y = dy + dheight;
+      break;
 
-      default:
-        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+    default:
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
     }
 
     transform_.Apply(x, y);
@@ -105,7 +128,7 @@
                                   double y) const
   {
     transformInverse_.Apply(x, y);
-        
+
     unsigned int cropX, cropY, cropWidth, cropHeight;
     GetCrop(cropX, cropY, cropWidth, cropHeight);
 
@@ -127,7 +150,7 @@
 
     cairo_t* cr = context.GetObject();
     cairo_set_line_width(cr, 2.0 / zoom);
-        
+
     double x, y;
     x = dx;
     y = dy;
@@ -162,18 +185,16 @@
     index_(0),
     hasSize_(false),
     width_(0),
-    height_(0),
-    hasCrop_(false),
-    pixelSpacingX_(1),
-    pixelSpacingY_(1),
-    panX_(0),
-    panY_(0),
-    angle_(0),
-    resizeable_(false)
+    height_(0)
   {
     UpdateTransform();
   }
 
+  void RadiographyLayer::ResetCrop()
+  {
+    geometry_.ResetCrop();
+    UpdateTransform();
+  }
 
   void RadiographyLayer::SetCrop(unsigned int x,
                                  unsigned int y,
@@ -184,34 +205,36 @@
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
     }
-        
+
     if (x + width > width_ ||
         y + height > height_)
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
     }
-        
-    hasCrop_ = true;
-    cropX_ = x;
-    cropY_ = y;
-    cropWidth_ = width;
-    cropHeight_ = height;
 
+    geometry_.SetCrop(x, y, width, height);
     UpdateTransform();
   }
 
-      
+  void RadiographyLayer::SetGeometry(const Geometry& geometry)
+  {
+    geometry_ = geometry;
+
+    if (hasSize_)
+    {
+      UpdateTransform();
+    }
+  }
+
+
   void RadiographyLayer::GetCrop(unsigned int& x,
                                  unsigned int& y,
                                  unsigned int& width,
                                  unsigned int& height) const
   {
-    if (hasCrop_)
+    if (GetGeometry().HasCrop())
     {
-      x = cropX_;
-      y = cropY_;
-      width = cropWidth_;
-      height = cropHeight_;
+      GetGeometry().GetCrop(x, y, width, height);
     }
     else 
     {
@@ -222,10 +245,10 @@
     }
   }
 
-      
+  
   void RadiographyLayer::SetAngle(double angle)
   {
-    angle_ = angle;
+    geometry_.SetAngle(angle);
     UpdateTransform();
   }
 
@@ -239,7 +262,7 @@
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize);
     }
-        
+
     hasSize_ = true;
     width_ = width;
     height_ = height;
@@ -251,7 +274,7 @@
   Extent2D RadiographyLayer::GetExtent() const
   {
     Extent2D extent;
-       
+
     unsigned int x, y, width, height;
     GetCrop(x, y, width, height);
 
@@ -264,7 +287,7 @@
     AddToExtent(extent, dx + dwidth, dy);
     AddToExtent(extent, dx, dy + dheight);
     AddToExtent(extent, dx + dwidth, dy + dheight);
-        
+
     return extent;
   }
 
@@ -282,7 +305,7 @@
     else
     {
       transformInverse_.Apply(sceneX, sceneY);
-        
+
       int x = static_cast<int>(std::floor(sceneX));
       int y = static_cast<int>(std::floor(sceneY));
 
@@ -320,8 +343,7 @@
   void RadiographyLayer::SetPan(double x,
                                 double y)
   {
-    panX_ = x;
-    panY_ = y;
+    geometry_.SetPan(x, y);
     UpdateTransform();
   }
 
@@ -329,8 +351,7 @@
   void RadiographyLayer::SetPixelSpacing(double x,
                                          double y)
   {
-    pixelSpacingX_ = x;
-    pixelSpacingY_ = y;
+    geometry_.SetPixelSpacing(x, y);
     UpdateTransform();
   }
 
@@ -352,8 +373,8 @@
     GetCrop(cropX, cropY, cropWidth, cropHeight);
     GetCornerInternal(x, y, corner, cropX, cropY, cropWidth, cropHeight);
   }
-      
-      
+
+
   bool RadiographyLayer::LookupCorner(Corner& corner /* out */,
                                       double x,
                                       double y,
@@ -366,26 +387,26 @@
       Corner_BottomLeft,
       Corner_BottomRight
     };
-        
+
     unsigned int cropX, cropY, cropWidth, cropHeight;
     GetCrop(cropX, cropY, cropWidth, cropHeight);
 
     double threshold = Square(viewportDistance / zoom);
-        
+
     for (size_t i = 0; i < 4; i++)
     {
       double cx, cy;
       GetCornerInternal(cx, cy, CORNERS[i], cropX, cropY, cropWidth, cropHeight);
 
       double d = Square(cx - x) + Square(cy - y);
-        
+
       if (d <= threshold)
       {
         corner = CORNERS[i];
         return true;
       }
     }
-        
+
     return false;
   }
 }