comparison Framework/Deprecated/Toolbox/ParallelSlices.cpp @ 811:ffec76a5f7eb

deprecating ParallelSlices
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 28 May 2019 18:21:00 +0200
parents Framework/Toolbox/ParallelSlices.cpp@c3bbb130abc4
children 2d8ab34c8c91
comparison
equal deleted inserted replaced
810:7608e8107aa1 811:ffec76a5f7eb
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "ParallelSlices.h"
23
24 #include "../../Toolbox/GeometryToolbox.h"
25 #include "../../Volumes/ImageBuffer3D.h"
26
27 #include <Core/Logging.h>
28 #include <Core/OrthancException.h>
29
30 namespace Deprecated
31 {
32 ParallelSlices::ParallelSlices()
33 {
34 Clear();
35 }
36
37
38 ParallelSlices::ParallelSlices(const ParallelSlices& other)
39 {
40 normal_ = other.normal_;
41
42 slices_.resize(other.slices_.size());
43
44 for (size_t i = 0; i < slices_.size(); i++)
45 {
46 assert(other.slices_[i] != NULL);
47 slices_[i] = new OrthancStone::CoordinateSystem3D(*other.slices_[i]);
48 }
49 }
50
51
52 void ParallelSlices::Clear()
53 {
54 for (size_t i = 0; i < slices_.size(); i++)
55 {
56 if (slices_[i] != NULL)
57 {
58 delete slices_[i];
59 slices_[i] = NULL;
60 }
61 }
62
63 slices_.clear();
64 OrthancStone::LinearAlgebra::AssignVector(normal_, 0, 0, 1);
65 }
66
67
68 ParallelSlices::~ParallelSlices()
69 {
70 Clear();
71 }
72
73
74 void ParallelSlices::AddSlice(const OrthancStone::CoordinateSystem3D& slice)
75 {
76 if (slices_.empty())
77 {
78 normal_ = slice.GetNormal();
79 slices_.push_back(new OrthancStone::CoordinateSystem3D(slice));
80 }
81 else if (OrthancStone::GeometryToolbox::IsParallel(slice.GetNormal(), normal_))
82 {
83 slices_.push_back(new OrthancStone::CoordinateSystem3D(slice));
84 }
85 else
86 {
87 LOG(ERROR) << "Trying to add a slice that is not parallel to the previous ones";
88 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
89 }
90 }
91
92
93 void ParallelSlices::AddSlice(const OrthancStone::Vector& origin,
94 const OrthancStone::Vector& axisX,
95 const OrthancStone::Vector& axisY)
96 {
97 OrthancStone::CoordinateSystem3D slice(origin, axisX, axisY);
98 AddSlice(slice);
99 }
100
101
102 const OrthancStone::CoordinateSystem3D& ParallelSlices::GetSlice(size_t index) const
103 {
104 if (index >= slices_.size())
105 {
106 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
107 }
108 else
109 {
110 return *slices_[index];
111 }
112 }
113
114
115 bool ParallelSlices::ComputeClosestSlice(size_t& closestSlice,
116 double& closestDistance,
117 const OrthancStone::Vector& origin) const
118 {
119 if (slices_.empty())
120 {
121 return false;
122 }
123
124 double reference = boost::numeric::ublas::inner_prod(origin, normal_);
125
126 closestSlice = 0;
127 closestDistance = std::numeric_limits<double>::infinity();
128
129 for (size_t i = 0; i < slices_.size(); i++)
130 {
131 double distance = fabs(boost::numeric::ublas::inner_prod(slices_[i]->GetOrigin(), normal_) - reference);
132
133 if (distance < closestDistance)
134 {
135 closestSlice = i;
136 closestDistance = distance;
137 }
138 }
139
140 return true;
141 }
142
143
144 ParallelSlices* ParallelSlices::Reverse() const
145 {
146 std::auto_ptr<ParallelSlices> reversed(new ParallelSlices);
147
148 for (size_t i = slices_.size(); i > 0; i--)
149 {
150 const OrthancStone::CoordinateSystem3D& slice = *slices_[i - 1];
151
152 reversed->AddSlice(slice.GetOrigin(),
153 -slice.GetAxisX(),
154 slice.GetAxisY());
155 }
156
157 return reversed.release();
158 }
159
160
161 ParallelSlices* ParallelSlices::FromVolumeImage(const OrthancStone::VolumeImageGeometry& geometry,
162 OrthancStone::VolumeProjection projection)
163 {
164 const OrthancStone::Vector dimensions = geometry.GetVoxelDimensions(OrthancStone::VolumeProjection_Axial);
165 const OrthancStone::CoordinateSystem3D& axial = geometry.GetAxialGeometry();
166
167 std::auto_ptr<ParallelSlices> result(new ParallelSlices);
168
169 switch (projection)
170 {
171 case OrthancStone::VolumeProjection_Axial:
172 for (unsigned int z = 0; z < geometry.GetDepth(); z++)
173 {
174 OrthancStone::Vector origin = axial.GetOrigin();
175 origin += static_cast<double>(z) * dimensions[2] * axial.GetNormal();
176
177 result->AddSlice(origin,
178 axial.GetAxisX(),
179 axial.GetAxisY());
180 }
181 break;
182
183 case OrthancStone::VolumeProjection_Coronal:
184 for (unsigned int y = 0; y < geometry.GetHeight(); y++)
185 {
186 OrthancStone::Vector origin = axial.GetOrigin();
187 origin += static_cast<double>(y) * dimensions[1] * axial.GetAxisY();
188 origin += static_cast<double>(geometry.GetDepth() - 1) * dimensions[2] * axial.GetNormal();
189
190 result->AddSlice(origin,
191 axial.GetAxisX(),
192 -axial.GetNormal());
193 }
194 break;
195
196 case OrthancStone::VolumeProjection_Sagittal:
197 for (unsigned int x = 0; x < geometry.GetWidth(); x++)
198 {
199 OrthancStone::Vector origin = axial.GetOrigin();
200 origin += static_cast<double>(x) * dimensions[0] * axial.GetAxisX();
201 origin += static_cast<double>(geometry.GetDepth() - 1) * dimensions[2] * axial.GetNormal();
202
203 result->AddSlice(origin,
204 axial.GetAxisY(),
205 -axial.GetNormal());
206 }
207 break;
208
209 default:
210 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
211 }
212
213 return result.release();
214 }
215 }