changeset 1702:9980875edc7c db-changes

started work on SliceOrdering
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 12 Oct 2015 17:49:23 +0200
parents 4aaaecae5803
children b80e76dd1d56
files CMakeLists.txt Core/DicomFormat/DicomMap.cpp Core/DicomFormat/DicomTag.h NEWS OrthancServer/OrthancRestApi/OrthancRestResources.cpp OrthancServer/SliceOrdering.cpp OrthancServer/SliceOrdering.h
diffstat 7 files changed, 316 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Mon Oct 12 14:47:58 2015 +0200
+++ b/CMakeLists.txt	Mon Oct 12 17:49:23 2015 +0200
@@ -146,21 +146,28 @@
 
 
 set(ORTHANC_SERVER_SOURCES
+  OrthancServer/DatabaseWrapper.cpp
+  OrthancServer/DatabaseWrapperBase.cpp
+  OrthancServer/DicomDirWriter.cpp
+  OrthancServer/DicomFindQuery.cpp
+  OrthancServer/DicomModification.cpp
   OrthancServer/DicomProtocol/DicomFindAnswers.cpp
   OrthancServer/DicomProtocol/DicomServer.cpp
   OrthancServer/DicomProtocol/DicomUserConnection.cpp
   OrthancServer/DicomProtocol/RemoteModalityParameters.cpp
   OrthancServer/DicomProtocol/ReusableDicomUserConnection.cpp
-  OrthancServer/DicomModification.cpp
+  OrthancServer/ExportedResource.cpp
   OrthancServer/FromDcmtkBridge.cpp
-  OrthancServer/ParsedDicomFile.cpp
-  OrthancServer/DicomDirWriter.cpp
   OrthancServer/Internals/CommandDispatcher.cpp
+  OrthancServer/Internals/DicomImageDecoder.cpp
   OrthancServer/Internals/FindScp.cpp
   OrthancServer/Internals/MoveScp.cpp
   OrthancServer/Internals/StoreScp.cpp
-  OrthancServer/Internals/DicomImageDecoder.cpp
+  OrthancServer/LuaScripting.cpp
+  OrthancServer/OrthancFindRequestHandler.cpp
+  OrthancServer/OrthancHttpHandler.cpp
   OrthancServer/OrthancInitialization.cpp
+  OrthancServer/OrthancMoveRequestHandler.cpp
   OrthancServer/OrthancPeerParameters.cpp
   OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp
   OrthancServer/OrthancRestApi/OrthancRestApi.cpp
@@ -169,21 +176,15 @@
   OrthancServer/OrthancRestApi/OrthancRestModalities.cpp
   OrthancServer/OrthancRestApi/OrthancRestResources.cpp
   OrthancServer/OrthancRestApi/OrthancRestSystem.cpp
-  OrthancServer/ServerIndex.cpp
-  OrthancServer/ToDcmtkBridge.cpp
-  OrthancServer/DatabaseWrapper.cpp
-  OrthancServer/DatabaseWrapperBase.cpp
+  OrthancServer/ParsedDicomFile.cpp
+  OrthancServer/QueryRetrieveHandler.cpp
+  OrthancServer/ResourceFinder.cpp
   OrthancServer/ServerContext.cpp
   OrthancServer/ServerEnumerations.cpp
+  OrthancServer/ServerIndex.cpp
   OrthancServer/ServerToolbox.cpp
-  OrthancServer/OrthancFindRequestHandler.cpp
-  OrthancServer/OrthancMoveRequestHandler.cpp
-  OrthancServer/ExportedResource.cpp
-  OrthancServer/ResourceFinder.cpp
-  OrthancServer/DicomFindQuery.cpp
-  OrthancServer/QueryRetrieveHandler.cpp
-  OrthancServer/LuaScripting.cpp
-  OrthancServer/OrthancHttpHandler.cpp
+  OrthancServer/SliceOrdering.cpp
+  OrthancServer/ToDcmtkBridge.cpp
 
   # From "lua-scripting" branch
   OrthancServer/DicomInstanceToStore.cpp
--- a/Core/DicomFormat/DicomMap.cpp	Mon Oct 12 14:47:58 2015 +0200
+++ b/Core/DicomFormat/DicomMap.cpp	Mon Oct 12 17:49:23 2015 +0200
@@ -83,7 +83,8 @@
     DICOM_TAG_NUMBER_OF_TEMPORAL_POSITIONS,
     DICOM_TAG_NUMBER_OF_SLICES,
     DICOM_TAG_NUMBER_OF_TIME_SLICES,
-    DICOM_TAG_SERIES_INSTANCE_UID
+    DICOM_TAG_SERIES_INSTANCE_UID,
+    DICOM_TAG_IMAGE_ORIENTATION_PATIENT    // New in db v6
   };
 
   static DicomTag instanceTags[] =
@@ -95,7 +96,8 @@
     DICOM_TAG_INSTANCE_NUMBER,
     DICOM_TAG_NUMBER_OF_FRAMES,
     DICOM_TAG_TEMPORAL_POSITION_IDENTIFIER,
-    DICOM_TAG_SOP_INSTANCE_UID
+    DICOM_TAG_SOP_INSTANCE_UID,
+    DICOM_TAG_IMAGE_POSITION_PATIENT    // New in db v6
   };
 
 
--- a/Core/DicomFormat/DicomTag.h	Mon Oct 12 14:47:58 2015 +0200
+++ b/Core/DicomFormat/DicomTag.h	Mon Oct 12 17:49:23 2015 +0200
@@ -134,6 +134,8 @@
   static const DicomTag DICOM_TAG_PIXEL_REPRESENTATION(0x0028, 0x0103);
   static const DicomTag DICOM_TAG_PLANAR_CONFIGURATION(0x0028, 0x0006);
   static const DicomTag DICOM_TAG_PHOTOMETRIC_INTERPRETATION(0x0028, 0x0004);
+  static const DicomTag DICOM_TAG_IMAGE_ORIENTATION_PATIENT(0x0020, 0x0037);
+  static const DicomTag DICOM_TAG_IMAGE_POSITION_PATIENT(0x0020, 0x0032);
 
   // Tags related to date and time
   static const DicomTag DICOM_TAG_ACQUISITION_DATE(0x0008, 0x0022);
--- a/NEWS	Mon Oct 12 14:47:58 2015 +0200
+++ b/NEWS	Mon Oct 12 17:49:23 2015 +0200
@@ -5,6 +5,7 @@
 * "/tools/create-dicom": Support of binary tags encoded using data URI scheme
 * "/tools/create-dicom": Support of hierarchical structures (creation of sequences)
 * "/modify" can insert/modify sequences
+* "/series/.../ordered-slices" to order the slices of a 2D+t, 3D or 3D+t image
 * New URIs for attachments: ".../compress", ".../uncompress" and ".../is-compressed"
 
 Plugins
--- a/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Mon Oct 12 14:47:58 2015 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Mon Oct 12 17:49:23 2015 +0200
@@ -39,6 +39,7 @@
 #include "../ResourceFinder.h"
 #include "../DicomFindQuery.h"
 #include "../ServerContext.h"
+#include "../SliceOrdering.h"
 
 
 namespace Orthanc
@@ -1093,6 +1094,16 @@
   }
 
 
+  static void OrderSlices(RestApiGetCall& call)
+  {
+    const std::string id = call.GetUriComponent("id", "");
+
+    ServerIndex& index = OrthancRestApi::GetIndex(call);
+    SliceOrdering ordering(index, id);
+
+  }
+
+
   void OrthancRestApi::RegisterResources()
   {
     Register("/instances", ListResources<ResourceType_Instance>);
@@ -1187,5 +1198,7 @@
     Register("/series/{id}/instances-tags", GetChildInstancesTags);
 
     Register("/instances/{id}/content/*", GetRawContent);
+
+    Register("/series/{id}/ordered-slices", OrderSlices);
   }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/SliceOrdering.cpp	Mon Oct 12 17:49:23 2015 +0200
@@ -0,0 +1,215 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "PrecompiledHeadersServer.h"
+#include "SliceOrdering.h"
+
+#include "../Core/Toolbox.h"
+
+#include <boost/lexical_cast.hpp>
+
+
+namespace Orthanc
+{
+  static bool TokenizeVector(std::vector<float>& result,
+                             const std::string& value,
+                             unsigned int expectedSize)
+  {
+    std::vector<std::string> tokens;
+    Toolbox::TokenizeString(tokens, value, '\\');
+
+    if (tokens.size() != expectedSize)
+    {
+      return false;
+    }
+
+    result.resize(tokens.size());
+
+    for (size_t i = 0; i < tokens.size(); i++)
+    {
+      try
+      {
+        result[i] = boost::lexical_cast<float>(tokens[i]);
+      }
+      catch (boost::bad_lexical_cast&)
+      {
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+
+  static bool TokenizeVector(std::vector<float>& result,
+                             const DicomMap& map,
+                             const DicomTag& tag,
+                             unsigned int expectedSize)
+  {
+    const DicomValue* value = map.TestAndGetValue(tag);
+
+    if (value == NULL ||
+        value->IsNull())
+    {
+      return false;
+    }
+    else
+    {
+      return TokenizeVector(result, value->AsString(), expectedSize);
+    }
+  }
+
+
+  struct SliceOrdering::Instance
+  {
+    std::string   instanceId_;
+    bool          hasPosition_;
+    Vector        position_;   
+    bool          hasIndexInSeries_;
+    size_t        indexInSeries_;
+
+    Instance(ServerIndex& index,
+          const std::string& instanceId) :
+      instanceId_(instanceId)
+    {
+      DicomMap instance;
+      if (!index.GetMainDicomTags(instance, instanceId, ResourceType_Instance, ResourceType_Instance))
+      {
+        throw OrthancException(ErrorCode_UnknownResource);
+      }
+      
+      std::vector<float> tmp;
+      hasPosition_ = TokenizeVector(tmp, instance, DICOM_TAG_IMAGE_POSITION_PATIENT, 3);
+
+      if (hasPosition_)
+      {
+        position_[0] = tmp[0];
+        position_[1] = tmp[1];
+        position_[2] = tmp[2];
+      }
+
+      std::string s;
+      hasIndexInSeries_ = false;
+
+      try
+      {
+        if (index.LookupMetadata(s, instanceId, MetadataType_Instance_IndexInSeries))
+        {
+          indexInSeries_ = boost::lexical_cast<size_t>(s);
+          hasIndexInSeries_ = true;
+        }
+      }
+      catch (boost::bad_lexical_cast&)
+      {
+      }
+    }
+  };
+
+
+  void SliceOrdering::ComputeNormal()
+  {
+    DicomMap series;
+    if (!index_.GetMainDicomTags(series, seriesId_, ResourceType_Series, ResourceType_Series))
+    {
+      throw OrthancException(ErrorCode_UnknownResource);
+    }
+
+    std::vector<float> cosines;
+    hasNormal_ = TokenizeVector(cosines, series, DICOM_TAG_IMAGE_ORIENTATION_PATIENT, 6);
+
+    if (hasNormal_)
+    {
+      normal_[0] = cosines[1] * cosines[5] - cosines[2] * cosines[4];
+      normal_[1] = cosines[2] * cosines[3] - cosines[0] * cosines[5];
+      normal_[2] = cosines[0] * cosines[4] - cosines[1] * cosines[3];
+    }
+  }
+
+
+  void SliceOrdering::CreateInstances()
+  {
+    std::list<std::string> instancesId;
+    index_.GetChildren(instancesId, seriesId_);
+
+    instances_.reserve(instancesId.size());
+    for (std::list<std::string>::const_iterator
+           it = instancesId.begin(); it != instancesId.end(); ++it)
+    {
+      instances_.push_back(new Instance(index_, *it));
+    }
+  }
+  
+
+  bool SliceOrdering::SortUsingPositions()
+  {
+    if (!hasNormal_)
+    {
+      return false;
+    }
+
+    for (size_t i = 0; i < instances_.size(); i++)
+    {
+      assert(instances_[i] != NULL);
+      if (!instances_[i]->hasPosition_)
+      {
+        return false;
+      }
+    }
+
+    
+
+    return true;
+  }
+
+
+  SliceOrdering::SliceOrdering(ServerIndex& index,
+                               const std::string& seriesId) :
+    index_(index),
+    seriesId_(seriesId)
+  {
+    ComputeNormal();
+    CreateInstances();
+  }
+
+
+  SliceOrdering::~SliceOrdering()
+  {
+    for (std::vector<Instance*>::iterator
+           it = instances_.begin(); it != instances_.end(); ++it)
+    {
+      if (*it != NULL)
+      {
+        delete *it;
+      }
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/SliceOrdering.h	Mon Oct 12 17:49:23 2015 +0200
@@ -0,0 +1,64 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "ServerIndex.h"
+
+namespace Orthanc
+{
+  class SliceOrdering
+  {
+  private:
+    typedef float Vector[3];
+
+    struct Instance;
+
+    ServerIndex&             index_;
+    std::string              seriesId_;
+    bool                     hasNormal_;
+    Vector                   normal_;
+    std::vector<Instance*>   instances_;
+
+    void ComputeNormal();
+
+    void CreateInstances();
+
+    bool SortUsingPositions();
+
+  public:
+    SliceOrdering(ServerIndex& index,
+                  const std::string& seriesId);
+
+    ~SliceOrdering();
+  };
+}