Mercurial > hg > orthanc
changeset 4735:e17fdc43ef6c
optimized version of DicomPath::IsMatch()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 05 Jul 2021 17:03:46 +0200 |
parents | b51c08bd5c38 |
children | bf852fd773b7 |
files | OrthancFramework/Sources/DicomFormat/DicomPath.cpp OrthancFramework/Sources/DicomFormat/DicomPath.h OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp |
diffstat | 3 files changed, 92 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Sources/DicomFormat/DicomPath.cpp Mon Jul 05 16:12:10 2021 +0200 +++ b/OrthancFramework/Sources/DicomFormat/DicomPath.cpp Mon Jul 05 17:03:46 2021 +0200 @@ -372,4 +372,42 @@ } } } + + + bool DicomPath::IsMatch(const DicomPath& pattern, + const std::vector<Orthanc::DicomTag>& prefixTags, + const std::vector<size_t>& prefixIndexes, + const DicomTag& finalTag) + { + if (prefixTags.size() != prefixIndexes.size()) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + + if (prefixTags.size() < pattern.GetPrefixLength()) + { + return false; + } + else + { + for (size_t i = 0; i < pattern.GetPrefixLength(); i++) + { + if (prefixTags[i] != pattern.GetPrefixTag(i) || + (!pattern.IsPrefixUniversal(i) && + prefixIndexes[i] != pattern.GetPrefixIndex(i))) + { + return false; + } + } + + if (prefixTags.size() == pattern.GetPrefixLength()) + { + return (finalTag == pattern.GetFinalTag()); + } + else + { + return (prefixTags[pattern.GetPrefixLength()] == pattern.GetFinalTag()); + } + } + } }
--- a/OrthancFramework/Sources/DicomFormat/DicomPath.h Mon Jul 05 16:12:10 2021 +0200 +++ b/OrthancFramework/Sources/DicomFormat/DicomPath.h Mon Jul 05 17:03:46 2021 +0200 @@ -130,5 +130,10 @@ static bool IsMatch(const DicomPath& pattern, const DicomPath& path); + + static bool IsMatch(const DicomPath& pattern, + const std::vector<Orthanc::DicomTag>& prefixTags, + const std::vector<size_t>& prefixIndexes, + const DicomTag& finalTag); }; }
--- a/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp Mon Jul 05 16:12:10 2021 +0200 +++ b/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp Mon Jul 05 17:03:46 2021 +0200 @@ -2261,6 +2261,31 @@ } +static bool MyIsMatch(const DicomPath& a, + const DicomPath& b) +{ + bool expected = DicomPath::IsMatch(a, b); + + std::vector<DicomTag> prefixTags; + std::vector<size_t> prefixIndexes; + + for (size_t i = 0; i < b.GetPrefixLength(); i++) + { + prefixTags.push_back(b.GetPrefixTag(i)); + prefixIndexes.push_back(b.GetPrefixIndex(i)); + } + + if (expected == DicomPath::IsMatch(a, prefixTags, prefixIndexes, b.GetFinalTag())) + { + return expected; + } + else + { + throw OrthancException(ErrorCode_InternalError); + } +} + + TEST(DicomModification, DicomPath) { // Those are samples inspired by those from "man dcmodify" @@ -2366,30 +2391,30 @@ ASSERT_THROW(DicomPath::Parse("(0010,0010)0].PatientID"), OrthancException); ASSERT_THROW(DicomPath::Parse("(0010,0010)[-1].PatientID"), OrthancException); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)"), - DicomPath::Parse("(0010,0010)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)"), - DicomPath::Parse("(0010,0020)"))); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), - DicomPath::Parse("(0010,0010)"))); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[*].(0010,0020)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[2].(0010,0020)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); - ASSERT_THROW(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), - DicomPath::Parse("(0010,0010)[*].(0010,0020)")), OrthancException); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[*].(0010,0020)[*].(0010,0030)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); - ASSERT_TRUE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)[3].(0010,0030)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); - ASSERT_FALSE(DicomPath::IsMatch(DicomPath::Parse("(0010,0010)[2].(0010,0020)[2].(0010,0030)"), - DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)"), + DicomPath::Parse("(0010,0010)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)"), + DicomPath::Parse("(0010,0020)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), + DicomPath::Parse("(0010,0010)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)[*].(0010,0020)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)[2].(0010,0020)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)"))); + ASSERT_THROW(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)"), + DicomPath::Parse("(0010,0010)[*].(0010,0020)")), OrthancException); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)[*].(0010,0020)[*].(0010,0030)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); + ASSERT_TRUE(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)[1].(0010,0020)[3].(0010,0030)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); + ASSERT_FALSE(MyIsMatch(DicomPath::Parse("(0010,0010)[2].(0010,0020)[2].(0010,0030)"), + DicomPath::Parse("(0010,0010)[1].(0010,0020)[2].(0010,0030)[3].(0010,0040)"))); }