annotate OrthancStone/Sources/Toolbox/OsiriX/CollectionOfAnnotations.cpp @ 1591:5887a4f8594b

moving platform-specific files out of the "OrthancStone" folder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Oct 2020 13:15:03 +0200
parents bd180f97c734
children b782f78aed42
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1584
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "CollectionOfAnnotations.h"
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "ArrayValue.h"
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "IntegerValue.h"
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <OrthancException.h>
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <cassert>
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 namespace OrthancStone
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 namespace OsiriX
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 static void GetAttributes(std::map<std::string, std::string>& target,
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 const pugi::xml_node& node)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute())
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 target[attr.name()] = attr.value();
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 CollectionOfAnnotations::~CollectionOfAnnotations()
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 for (size_t i = 0; i < annotations_.size(); i++)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 assert(annotations_[i] != NULL);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 delete annotations_[i];
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 const Annotation& CollectionOfAnnotations::GetAnnotation(size_t i) const
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 if (i >= annotations_.size())
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 else
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 assert(annotations_[i] != NULL);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 return *annotations_[i];
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 void CollectionOfAnnotations::AddAnnotation(Annotation* annotation)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 if (annotation == NULL)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 else
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 annotations_.push_back(annotation);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 void CollectionOfAnnotations::ParseXml(const std::string& xml)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 pugi::xml_document doc;
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 pugi::xml_parse_result result = doc.load_buffer(xml.empty() ? NULL : xml.c_str(), xml.size());
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 if (!result)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 const pugi::xml_node& root = doc.document_element();
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 if (std::string(root.name()) != "plist" ||
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 !root.first_child() ||
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 root.first_child() != root.last_child())
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 std::map<std::string, std::string> attributes;
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 GetAttributes(attributes, root);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 std::map<std::string, std::string>::const_iterator version = attributes.find("version");
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 if (version == attributes.end() ||
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 version->second != "1.0")
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 std::unique_ptr<IValue> value(IValue::Parse(root.first_child()));
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 const DictionaryValue& dict = dynamic_cast<const DictionaryValue&>(*value);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 std::set<std::string> annotations;
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 dict.GetMembers(annotations);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 for (std::set<std::string>::const_iterator
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 it = annotations.begin(); it != annotations.end(); ++it)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 const ArrayValue& images = dynamic_cast<const ArrayValue&>(dict.GetValue(*it));
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 for (size_t i = 0; i < images.GetSize(); i++)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 const DictionaryValue& image = dynamic_cast<const DictionaryValue&>(images.GetValue(i));
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 const IntegerValue& number = dynamic_cast<const IntegerValue&>(image.GetValue("NumberOfROIs"));
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 const ArrayValue& rois = dynamic_cast<const ArrayValue&>(image.GetValue("ROIs"));
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 if (static_cast<int64_t>(rois.GetSize()) != number.GetValue())
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 for (size_t j = 0; j < rois.GetSize(); j++)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 const DictionaryValue& roi = dynamic_cast<const DictionaryValue&>(rois.GetValue(i));
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 std::unique_ptr<Annotation> annotation(Annotation::Create(roi));
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 if (annotation.get() != NULL)
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 AddAnnotation(annotation.release());
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 }
bd180f97c734 parsing osirix annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 }