comparison Plugin/InstanceInformation.cpp @ 0:02f7a0400a91

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Feb 2015 13:45:35 +0100
parents
children a6492d20b2a8
comparison
equal deleted inserted replaced
-1:000000000000 0:02f7a0400a91
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public License
8 * as published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "InstanceInformation.h"
22
23 #include "../Orthanc/OrthancException.h"
24
25 #include <cassert>
26 #include <string.h>
27 #include <json/value.h>
28 #include <json/reader.h>
29 #include <json/writer.h>
30
31
32 namespace OrthancPlugins
33 {
34 void InstanceInformation::SetPosition(const std::vector<float>& normal,
35 const std::vector<float>& position)
36 {
37 if (normal.size() != 3 ||
38 position.size() != 3)
39 {
40 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType);
41 }
42
43 hasPosition_ = true;
44 memcpy(normal_, &normal[0], sizeof(float) * 3);
45 memcpy(position_, &position[0], sizeof(float) * 3);
46 }
47
48 void InstanceInformation::SetIndexInSeries(int index)
49 {
50 hasIndex_ = true;
51 index_ = index;
52 }
53
54 float InstanceInformation::GetPosition(size_t i) const
55 {
56 assert(hasPosition_ && i < 3);
57 return position_[i];
58 }
59
60 float InstanceInformation::GetNormal(size_t i) const
61 {
62 assert(hasPosition_ && i < 3);
63 return normal_[i];
64 }
65
66 int InstanceInformation::GetIndexInSeries() const
67 {
68 assert(hasIndex_);
69 return index_;
70 }
71
72 void InstanceInformation::Serialize(std::string& result) const
73 {
74 Json::Value value = Json::objectValue;
75
76 if (hasPosition_)
77 {
78 value["Normal"] = Json::arrayValue;
79 value["Normal"][0] = normal_[0];
80 value["Normal"][1] = normal_[1];
81 value["Normal"][2] = normal_[2];
82
83 value["Position"] = Json::arrayValue;
84 value["Position"][0] = position_[0];
85 value["Position"][1] = position_[1];
86 value["Position"][2] = position_[2];
87 }
88
89 if (hasIndex_)
90 {
91 value["Index"] = index_;
92 }
93
94 Json::FastWriter writer;
95 result = writer.write(value);
96 }
97
98
99 void InstanceInformation::Deserialize(InstanceInformation& result,
100 const std::string& serialized)
101 {
102 result = InstanceInformation();
103
104 Json::Reader reader;
105 Json::Value value;
106
107 if (!reader.parse(serialized, value) ||
108 value.type() != Json::objectValue)
109 {
110 return;
111 }
112
113 if (value.isMember("Normal"))
114 {
115 std::vector<float> normal(3), position(3);
116 normal[0] = value["Normal"][0].asFloat();
117 normal[1] = value["Normal"][1].asFloat();
118 normal[2] = value["Normal"][2].asFloat();
119 position[0] = value["Position"][0].asFloat();
120 position[1] = value["Position"][1].asFloat();
121 position[2] = value["Position"][2].asFloat();
122 result.SetPosition(normal, position);
123 }
124
125 if (value.isMember("Index"))
126 {
127 result.SetIndexInSeries(value["Index"].asInt());
128 }
129 }
130 }