changeset 1599:73cd85d7da6a

SortedFrames::LookupSopInstanceUid()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 28 Oct 2020 10:55:45 +0100
parents 8563ea5d8ae4
children b253b79906fa
files OrthancStone/Sources/Toolbox/SortedFrames.cpp OrthancStone/Sources/Toolbox/SortedFrames.h UnitTestsSources/SortedFramesTests.cpp
diffstat 3 files changed, 50 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancStone/Sources/Toolbox/SortedFrames.cpp	Mon Oct 26 20:49:28 2020 +0100
+++ b/OrthancStone/Sources/Toolbox/SortedFrames.cpp	Wed Oct 28 10:55:45 2020 +0100
@@ -144,6 +144,7 @@
       delete instances_[i];
     }
 
+    instancesIndex_.clear();
     studyInstanceUid_.clear();
     seriesInstanceUid_.clear();
     frames_.clear();
@@ -155,9 +156,10 @@
   {
     std::unique_ptr<Instance> instance(new Instance(tags));
 
-    std::string studyInstanceUid, seriesInstanceUid;
+    std::string studyInstanceUid, seriesInstanceUid, sopInstanceUid;
     if (!tags.LookupStringValue(studyInstanceUid, Orthanc::DICOM_TAG_STUDY_INSTANCE_UID, false) ||
-        !tags.LookupStringValue(seriesInstanceUid, Orthanc::DICOM_TAG_SERIES_INSTANCE_UID, false))
+        !tags.LookupStringValue(seriesInstanceUid, Orthanc::DICOM_TAG_SERIES_INSTANCE_UID, false) ||
+        !tags.LookupStringValue(sopInstanceUid, Orthanc::DICOM_TAG_SOP_INSTANCE_UID, false))
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
     }
@@ -177,12 +179,37 @@
       }
     }
 
+    if (instancesIndex_.find(sopInstanceUid) != instancesIndex_.end())
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange,
+                                      "Cannot register twice the same SOP Instance UID");
+    }
+
+    instancesIndex_[sopInstanceUid] = instances_.size();
+
     instances_.push_back(instance.release());
     sorted_ = false;
     frames_.clear();
   }
 
 
+  bool SortedFrames::LookupSopInstanceUid(size_t& instanceIndex,
+                                          const std::string& sopInstanceUid) const
+  {
+    InstancesIndex::const_iterator found = instancesIndex_.find(sopInstanceUid);
+    
+    if (found == instancesIndex_.end())
+    {
+      return false;
+    }
+    else
+    {
+      instanceIndex = found->second;
+      return true;
+    }
+  }
+
+  
   void SortedFrames::AddFramesOfInstance(std::set<size_t>& remainingInstances,
                                          size_t index)
   {
--- a/OrthancStone/Sources/Toolbox/SortedFrames.h	Mon Oct 26 20:49:28 2020 +0100
+++ b/OrthancStone/Sources/Toolbox/SortedFrames.h	Wed Oct 28 10:55:45 2020 +0100
@@ -94,11 +94,16 @@
       }
     };
 
+
+    // Maps "SOPInstanceUID" to an index in "instances_"
+    typedef std::map<std::string, size_t>  InstancesIndex;
+
     std::string             studyInstanceUid_;
     std::string             seriesInstanceUid_;
     std::vector<Instance*>  instances_;
     std::vector<Frame>      frames_;
     bool                    sorted_;
+    InstancesIndex          instancesIndex_;
 
     const Instance& GetInstance(size_t index) const;
 
@@ -154,6 +159,9 @@
       return GetInstance(index).GetSopInstanceUid();
     }
 
+    bool LookupSopInstanceUid(size_t& instanceIndex,
+                              const std::string& sopInstanceUid) const;
+
     bool IsSorted() const
     {
       return sorted_;
--- a/UnitTestsSources/SortedFramesTests.cpp	Mon Oct 26 20:49:28 2020 +0100
+++ b/UnitTestsSources/SortedFramesTests.cpp	Wed Oct 28 10:55:45 2020 +0100
@@ -92,6 +92,12 @@
   tags.SetValue(Orthanc::DICOM_TAG_SOP_INSTANCE_UID, "sop2", false);
   tags.SetValue(Orthanc::DICOM_TAG_NUMBER_OF_FRAMES, "2", false);
   f.AddInstance(tags);
+
+  size_t i;
+  ASSERT_TRUE(f.LookupSopInstanceUid(i, "sop1"));  ASSERT_EQ(1u, i);
+  ASSERT_TRUE(f.LookupSopInstanceUid(i, "sop2"));  ASSERT_EQ(2u, i);
+  ASSERT_TRUE(f.LookupSopInstanceUid(i, "sop3"));  ASSERT_EQ(0u, i);
+  ASSERT_FALSE(f.LookupSopInstanceUid(i, "nope"));
     
   f.Sort();
   ASSERT_EQ(3u, f.GetInstancesCount());
@@ -105,6 +111,12 @@
   ASSERT_EQ("sop2", f.GetFrameSopInstanceUid(3));  ASSERT_EQ(0u, f.GetFrameIndex(3));
   ASSERT_EQ("sop2", f.GetFrameSopInstanceUid(4));  ASSERT_EQ(1u, f.GetFrameIndex(4));
   ASSERT_EQ("sop3", f.GetFrameSopInstanceUid(5));  ASSERT_EQ(0u, f.GetFrameIndex(5));
+
+  // The instances must not have been reordered, only the frames
+  ASSERT_TRUE(f.LookupSopInstanceUid(i, "sop1"));  ASSERT_EQ(1u, i);
+  ASSERT_TRUE(f.LookupSopInstanceUid(i, "sop2"));  ASSERT_EQ(2u, i);
+  ASSERT_TRUE(f.LookupSopInstanceUid(i, "sop3"));  ASSERT_EQ(0u, i);
+  ASSERT_FALSE(f.LookupSopInstanceUid(i, "nope"));
 }
 
 
@@ -173,7 +185,7 @@
   tags.Remove(Orthanc::DICOM_TAG_IMAGE_INDEX);
   tags.SetValue(Orthanc::DICOM_TAG_INSTANCE_NUMBER, "30", false);
   f.AddInstance(tags);
-    
+
   f.Sort();
   ASSERT_EQ(4u, f.GetInstancesCount());
   ASSERT_EQ("sop1", f.GetSopInstanceUid(0));