diff OrthancStone/Sources/Toolbox/SortedFrames.cpp @ 1631:960bb5fcc440

SortedFrames::GetFrameGeometry()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Nov 2020 18:49:08 +0100
parents 78509230f0d7
children 882e2253a90e
line wrap: on
line diff
--- a/OrthancStone/Sources/Toolbox/SortedFrames.cpp	Mon Nov 09 18:01:32 2020 +0100
+++ b/OrthancStone/Sources/Toolbox/SortedFrames.cpp	Mon Nov 09 18:49:08 2020 +0100
@@ -24,6 +24,7 @@
 
 #include "GeometryToolbox.h"
 
+#include <Logging.h>
 #include <OrthancException.h>
 #include <Toolbox.h>
 
@@ -40,7 +41,8 @@
     }
 
     uint32_t tmp;
-    if (tags.ParseUnsignedInteger32(tmp, Orthanc::DICOM_TAG_NUMBER_OF_FRAMES))
+    if (tags.ParseUnsignedInteger32(tmp, Orthanc::DICOM_TAG_NUMBER_OF_FRAMES) &&
+        tmp > 0)
     {
       numberOfFrames_ = tmp;
     }
@@ -59,9 +61,68 @@
     {
       monochrome1_ = false;
     }
+
+    bool ok = false;
+
+    if (numberOfFrames_ > 1)
+    {
+      std::string offsets, increment;
+      if (tags.LookupStringValue(offsets, Orthanc::DICOM_TAG_GRID_FRAME_OFFSET_VECTOR, false) &&
+          tags.LookupStringValue(increment, Orthanc::DICOM_TAG_FRAME_INCREMENT_POINTER, false))
+      {
+        Orthanc::Toolbox::ToUpperCase(increment);
+        if (increment != "3004,000C")
+        {
+          LOG(WARNING) << "Bad value for the FrameIncrementPointer tags in a multiframe image";
+        }
+        else if (LinearAlgebra::ParseVector(frameOffsets_, offsets))
+        {
+          if (frameOffsets_.size() == numberOfFrames_)
+          {
+            ok = true;
+          }
+          else
+          {
+            LOG(WARNING) << "The size of the GridFrameOffsetVector does not correspond to the number of frames";
+          }
+        }
+        else
+        {
+          LOG(WARNING) << "Cannot parse the GridFrameOffsetVector tag";
+        }
+      }
+      else
+      {
+        LOG(INFO) << "Missing the frame offset information in a multiframe image";
+      }
+    }
+
+    if (!ok)
+    {
+      frameOffsets_.resize(numberOfFrames_);
+      for (size_t i = 0; i < numberOfFrames_; i++)
+      {
+        frameOffsets_[i] = 0;
+      }
+    }
   }
 
 
+  double SortedFrames::Instance::GetFrameOffset(unsigned int frame) const
+  {
+    assert(GetNumberOfFrames() == frameOffsets_.size());
+    
+    if (frame >= GetNumberOfFrames())
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+    }
+    else
+    {
+      return frameOffsets_[frame];
+    }
+  }
+  
+
   SortedFrames::Frame::Frame(const Instance& instance,
                              unsigned int frameNumber) :
     instance_(&instance),
@@ -364,6 +425,24 @@
   }
 
 
+  CoordinateSystem3D SortedFrames::GetFrameGeometry(size_t frameIndex) const
+  {
+    const Frame& frame = GetFrame(frameIndex);
+    CoordinateSystem3D geometry = frame.GetInstance().GetGeometry();
+
+    if (geometry.IsValid())
+    {
+      geometry.SetOrigin(geometry.GetOrigin() + geometry.GetNormal() *
+                         frame.GetInstance().GetFrameOffset(frame.GetFrameNumberInInstance()));
+      return geometry;
+    }
+    else
+    {
+      return geometry;
+    }
+  }
+
+
   bool SortedFrames::LookupFrame(size_t& frameIndex,
                                  const std::string& sopInstanceUid,
                                  unsigned int frameNumber) const