SimpleITK  
sitkTemplateFunctions.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef sitkTemplateFunctions_h
19#define sitkTemplateFunctions_h
20
21#include "sitkMacro.h"
22#include "sitkCommon.h"
23#include "sitkExceptionObject.h"
24
25#include <vector>
26#include <ostream>
27#include <iterator>
28
29namespace itk
30{
31
32template <unsigned int VImageDimension>
33class ImageRegion;
34template <typename T>
35class Versor;
36
37namespace simple
38{
39
45template <typename T>
47Unused(const T &) {};
48
56template <typename T>
57SITKCommon_HIDDEN std::ostream &
58 operator<<(std::ostream & os, const std::vector<T> & v)
59{
60 if (v.empty())
61 {
62 return os << "[ ]";
63 }
64
65 os << "[ ";
66 std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(os, ", "));
67 return os << v.back() << " ]";
68}
69
70template <typename TITKPointVector, typename TType>
71TITKPointVector SITKCommon_HIDDEN
72sitkSTLVectorToITKPointVector(const std::vector<TType> & in)
73{
74
75 using itkPointVectorType = TITKPointVector;
76 itkPointVectorType out;
77
78 unsigned int Dimension = itkPointVectorType::value_type::GetPointDimension();
79
80 for (unsigned int i = 0; i + Dimension <= in.size(); i += Dimension)
81 {
82 typename itkPointVectorType::value_type pt(&in[i]);
83 out.push_back(pt);
84 }
85 return out;
86}
87
94template <typename TITKVector, typename TType>
95TITKVector SITKCommon_HIDDEN
96sitkSTLVectorToITK(const std::vector<TType> & in)
97{
98 using itkVectorType = TITKVector;
99 if (in.size() < itkVectorType::Dimension)
100 {
101 sitkExceptionMacro(<< "Unable to convert vector to ITK type\n"
102 << "Expected vector of length " << itkVectorType::Dimension << " but only got " << in.size()
103 << " elements.");
104 }
105 itkVectorType out;
106 for (unsigned int i = 0; i < itkVectorType::Dimension; ++i)
107 {
108 out[i] = in[i];
109 }
110 return out;
111}
112
115template <typename TType, typename TITKVector>
116std::vector<TType> SITKCommon_HIDDEN
117sitkITKVectorToSTL(const TITKVector & in)
118{
119 std::vector<TType> out(TITKVector::Dimension);
120 for (unsigned int i = 0; i < TITKVector::Dimension; ++i)
121 {
122 out[i] = static_cast<TType>(in[i]);
123 }
124 return out;
125}
126
131template <typename TType, typename TVectorOfITKVector>
132std::vector<TType> SITKCommon_HIDDEN
133sitkVectorOfITKVectorToSTL(const TVectorOfITKVector & in)
134{
135 using ITKVectorType = typename TVectorOfITKVector::ValueType;
136 std::vector<TType> out;
137 out.reserve(in.Size() * ITKVectorType::Dimension);
138 for (unsigned int i = 0; i < in.Size(); ++i)
139 {
140 const std::vector<TType> & temp = sitkITKVectorToSTL<TType>(in[i]);
141 out.insert(out.end(), temp.begin(), temp.end());
142 }
143 return out;
144}
145
146
147template <typename TType, typename TITKVector>
148std::vector<TType> SITKCommon_HIDDEN
149sitkITKVectorToSTL(const std::vector<TITKVector> & in)
150{
151 std::vector<TType> out;
152 out.reserve(in.size() * TITKVector::Dimension);
153 typename std::vector<TITKVector>::const_iterator iter = in.begin();
154 while (iter != in.end())
155 {
156 for (unsigned int i = 0; i < TITKVector::Dimension; ++i)
157 {
158 out.push_back(static_cast<TType>((*iter)[i]));
159 }
160 ++iter;
161 }
162
163 return out;
164}
165
169template <unsigned int VImageDimension>
170std::vector<unsigned int> SITKCommon_HIDDEN
172{
173 std::vector<unsigned int> out(VImageDimension * 2);
174 for (unsigned int i = 0; i < VImageDimension; ++i)
175 {
176 out[i] = static_cast<unsigned int>(in.GetIndex(i));
177 out[VImageDimension + i] = static_cast<unsigned int>(in.GetSize(i));
178 }
179 return out;
180}
181
182
183/* \brief Convert to an itk::Matrix type, where the vector is in row
184 * major form. If the vector is of 0-size then an identity matrix will
185 * be constructed.
186 */
187template <typename TDirectionType>
188TDirectionType SITKCommon_HIDDEN
189sitkSTLToITKDirection(const std::vector<double> & direction)
190{
191 TDirectionType itkDirection;
192
193 if (direction.empty())
194 {
195 itkDirection.SetIdentity();
196 }
197 else if (direction.size() == TDirectionType::RowDimensions * TDirectionType::ColumnDimensions)
198 {
199 std::copy(direction.begin(), direction.end(), itkDirection.GetVnlMatrix().begin());
200 }
201 else
202 {
203 sitkExceptionMacro(<< "Length of input (" << direction.size() << ") does not match matrix dimensions ("
204 << TDirectionType::RowDimensions << ", " << TDirectionType::ColumnDimensions << ").\n");
205 }
206 return itkDirection;
207}
208
209
210template <typename TDirectionType>
211std::vector<double> SITKCommon_HIDDEN
212sitkITKDirectionToSTL(const TDirectionType & d)
213{
214 return std::vector<double>(d.GetVnlMatrix().begin(), d.GetVnlMatrix().end());
215}
216
217
218template <typename T, typename TType>
220sitkSTLVectorToITKVersor(const std::vector<TType> & in)
221{
222 using itkVectorType = itk::Versor<T>;
223 if (in.size() != 4)
224 {
225 sitkExceptionMacro(<< "Unable to convert vector to ITK Versor type\n"
226 << "Expected vector of length " << 4 << " but got " << in.size() << " elements.");
227 }
228 itkVectorType out;
229 out.Set(in[0], in[1], in[2], in[3]);
230 return out;
231}
232
233
234template <typename TType, typename T>
235std::vector<TType> SITKCommon_HIDDEN
237{
238 std::vector<TType> out(4);
239 out[0] = in.GetX();
240 out[1] = in.GetY();
241 out[2] = in.GetZ();
242 out[3] = in.GetW();
243 return out;
244}
245
246// Based on p0052r8 : Generic Scope Guard and RAII Wrapper for the
247// Standard Library
248// by Peter Sommerlad and Andrew L. Sandoval
249template <typename F>
251{
252 F f_;
253 bool run_;
254 explicit scope_exit(F f) noexcept
255 : f_(std::move(f))
256 , run_(true)
257 {}
258 scope_exit(scope_exit && rhs) noexcept
259 : f_((rhs.run_ = false, std::move(rhs.f_)))
260 , run_(true)
261 {}
263 {
264 if (run_)
265 f_();
266 }
267
268 scope_exit &
269 operator=(scope_exit && rhs) = delete;
270 scope_exit(const scope_exit &) = delete;
271 scope_exit &
272 operator=(const scope_exit &) = delete;
273};
274
275template <typename F>
277make_scope_exit(F && f) noexcept
278{
279 return scope_exit<F>{ std::forward<F>(f) };
280}
281
282
283} // namespace simple
284} // namespace itk
285
286#endif
const IndexType & GetIndex() const
const SizeType & GetSize() const
ValueType GetZ() const
ValueType GetY() const
void Set(const MatrixType &mat)
ValueType GetW() const
ValueType GetX() const
std::vector< TType > SITKCommon_HIDDEN sitkVectorOfITKVectorToSTL(const TVectorOfITKVector &in)
Convert an ITK style array of ITK fixed width vector to std::vector.
std::vector< TType > SITKCommon_HIDDEN sitkITKVectorToSTL(const TITKVector &in)
Convert an ITK fixed width vector to a std::vector.
SITKCommon_EXPORT std::ostream & operator<<(std::ostream &os, const EventEnum k)
TITKVector SITKCommon_HIDDEN sitkSTLVectorToITK(const std::vector< TType > &in)
Copy the elements of an std::vector into an ITK fixed width vector.
scope_exit< F > make_scope_exit(F &&f) noexcept
std::vector< unsigned int > SITKCommon_HIDDEN sitkITKImageRegionToSTL(const ImageRegion< VImageDimension > &in)
Convert an ITK ImageRegion to and std::vector with the first part being the start index followed by t...
std::vector< TType > SITKCommon_HIDDEN sitkITKVersorToSTL(const itk::Versor< T > &in)
void SITKCommon_HIDDEN Unused(const T &)
A function which does nothing.
std::vector< double > SITKCommon_HIDDEN sitkITKDirectionToSTL(const TDirectionType &d)
TDirectionType SITKCommon_HIDDEN sitkSTLToITKDirection(const std::vector< double > &direction)
itk::Versor< T > SITKCommon_HIDDEN sitkSTLVectorToITKVersor(const std::vector< TType > &in)
TITKPointVector SITKCommon_HIDDEN sitkSTLVectorToITKPointVector(const std::vector< TType > &in)
#define SITKCommon_HIDDEN
Definition sitkCommon.h:44
#define sitkExceptionMacro(x)
Definition sitkMacro.h:70
scope_exit & operator=(scope_exit &&rhs)=delete
scope_exit(const scope_exit &)=delete
scope_exit & operator=(const scope_exit &)=delete
scope_exit(scope_exit &&rhs) noexcept