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 
29 namespace itk
30 {
31 
32 template <unsigned int VImageDimension>
33 class ImageRegion;
34 template <typename T>
35 class Versor;
36 
37 namespace simple
38 {
39 
45 template <typename T>
47 Unused(const T &){};
48 
56 template <typename T>
57 SITKCommon_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 
70 template <typename TITKPointVector, typename TType>
71 TITKPointVector SITKCommon_HIDDEN
72 sitkSTLVectorToITKPointVector(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 
94 template <typename TITKVector, typename TType>
95 TITKVector SITKCommon_HIDDEN
96 sitkSTLVectorToITK(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 
115 template <typename TType, typename TITKVector>
116 std::vector<TType> SITKCommon_HIDDEN
117 sitkITKVectorToSTL(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 
131 template <typename TType, typename TVectorOfITKVector>
132 std::vector<TType> SITKCommon_HIDDEN
133 sitkVectorOfITKVectorToSTL(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 
147 template <typename TType, typename TITKVector>
148 std::vector<TType> SITKCommon_HIDDEN
149 sitkITKVectorToSTL(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 
169 template <unsigned int VImageDimension>
170 std::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  */
187 template <typename TDirectionType>
188 TDirectionType SITKCommon_HIDDEN
189 sitkSTLToITKDirection(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 
210 template <typename TDirectionType>
211 std::vector<double> SITKCommon_HIDDEN
212 sitkITKDirectionToSTL(const TDirectionType & d)
213 {
214  return std::vector<double>(d.GetVnlMatrix().begin(), d.GetVnlMatrix().end());
215 }
216 
217 
218 template <typename T, typename TType>
220 sitkSTLVectorToITKVersor(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 
234 template <typename TType, typename T>
235 std::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
249 template <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(scope_exit const &) = delete;
271  scope_exit &
272  operator=(scope_exit const &) = delete;
273 };
274 
275 template <typename F>
276 scope_exit<F>
277 make_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
itk::simple::sitkSTLVectorToITK
TITKVector SITKCommon_HIDDEN sitkSTLVectorToITK(const std::vector< TType > &in)
Copy the elements of an std::vector into an ITK fixed width vector.
Definition: sitkTemplateFunctions.h:96
itk::Versor::GetW
ValueType GetW() const
itk::Versor::GetZ
ValueType GetZ() const
itk::simple::scope_exit::scope_exit
scope_exit(scope_exit &&rhs) noexcept
Definition: sitkTemplateFunctions.h:258
itk::Versor::GetY
ValueType GetY() const
itk::simple::sitkITKVersorToSTL
std::vector< TType > SITKCommon_HIDDEN sitkITKVersorToSTL(const itk::Versor< T > &in)
Definition: sitkTemplateFunctions.h:236
itk::simple::operator<<
SITKCommon_EXPORT std::ostream & operator<<(std::ostream &os, const EventEnum k)
itk::ImageRegion::GetIndex
const IndexType & GetIndex() const
itk::ImageRegion
Definition: sitkTemplateFunctions.h:33
sitkCommon.h
itk::simple::sitkVectorOfITKVectorToSTL
std::vector< TType > SITKCommon_HIDDEN sitkVectorOfITKVectorToSTL(const TVectorOfITKVector &in)
Convert an ITK style array of ITK fixed width vector to std::vector.
Definition: sitkTemplateFunctions.h:133
itk::ImageRegion::GetSize
const SizeType & GetSize() const
itk::ImageRegion
ImageRegion(const Index< VImageDimension > &, const Size< VImageDimension > &) -> ImageRegion< VImageDimension >
itk::Versor
Definition: sitkTemplateFunctions.h:35
itk::simple::scope_exit::operator=
scope_exit & operator=(scope_exit &&rhs)=delete
sitkExceptionObject.h
itk::simple::sitkITKVectorToSTL
std::vector< TType > SITKCommon_HIDDEN sitkITKVectorToSTL(const TITKVector &in)
Convert an ITK fixed width vector to a std::vector.
Definition: sitkTemplateFunctions.h:117
sitkMacro.h
itk::simple::sitkSTLToITKDirection
TDirectionType SITKCommon_HIDDEN sitkSTLToITKDirection(const std::vector< double > &direction)
Definition: sitkTemplateFunctions.h:189
itk::simple::scope_exit::run_
bool run_
Definition: sitkTemplateFunctions.h:253
itk::simple::sitkSTLVectorToITKVersor
itk::Versor< T > SITKCommon_HIDDEN sitkSTLVectorToITKVersor(const std::vector< TType > &in)
Definition: sitkTemplateFunctions.h:220
SITKCommon_HIDDEN
#define SITKCommon_HIDDEN
Definition: sitkCommon.h:44
itk::simple::sitkSTLVectorToITKPointVector
TITKPointVector SITKCommon_HIDDEN sitkSTLVectorToITKPointVector(const std::vector< TType > &in)
Definition: sitkTemplateFunctions.h:72
itk::simple::sitkITKDirectionToSTL
std::vector< double > SITKCommon_HIDDEN sitkITKDirectionToSTL(const TDirectionType &d)
Definition: sitkTemplateFunctions.h:212
itk::simple::Unused
void SITKCommon_HIDDEN Unused(const T &)
A function which does nothing.
Definition: sitkTemplateFunctions.h:47
itk::Versor::GetX
ValueType GetX() const
itk::simple::scope_exit::~scope_exit
~scope_exit()
Definition: sitkTemplateFunctions.h:262
itk::simple::scope_exit::scope_exit
scope_exit(F f) noexcept
Definition: sitkTemplateFunctions.h:254
itk
itk::simple::sitkITKImageRegionToSTL
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...
Definition: sitkTemplateFunctions.h:171
itk::simple::scope_exit
Definition: sitkTemplateFunctions.h:250
Dimension
constexpr unsigned int Dimension
itk::simple::make_scope_exit
scope_exit< F > make_scope_exit(F &&f) noexcept
Definition: sitkTemplateFunctions.h:277
sitkExceptionMacro
#define sitkExceptionMacro(x)
Definition: sitkMacro.h:70
itk::simple::scope_exit::f_
F f_
Definition: sitkTemplateFunctions.h:252