SimpleITK  2.0.0
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 template<unsigned int VImageDimension> class ImageRegion;
32 template<typename T> class Versor;
33 
34 namespace simple {
35 
41 template <typename T>
42 void SITKCommon_HIDDEN Unused( const T &) {};
43 
51 template <typename T>
52 SITKCommon_HIDDEN std::ostream & operator<<( std::ostream & os, const std::vector<T>& v)
53 {
54  if ( v.empty() )
55  {
56  return os << "[ ]";
57  }
58 
59  os << "[ ";
60  std::copy( v.begin(), v.end()-1, std::ostream_iterator<T>(os, ", ") );
61  return os << v.back() << " ]";
62 }
63 
64 template< typename TITKPointVector, typename TType>
65 TITKPointVector SITKCommon_HIDDEN sitkSTLVectorToITKPointVector( const std::vector< TType > & in )
66 {
67 
68  using itkPointVectorType = TITKPointVector;
69  itkPointVectorType out;
70 
71  unsigned int Dimension = itkPointVectorType::value_type::GetPointDimension();
72 
73  for( unsigned int i = 0; i + Dimension <= in.size(); i += Dimension )
74  {
75  typename itkPointVectorType::value_type pt(&in[i]);
76  out.push_back(pt);
77  }
78  return out;
79 
80 }
81 
88 template< typename TITKVector, typename TType>
89 TITKVector SITKCommon_HIDDEN sitkSTLVectorToITK( const std::vector< TType > & in )
90 {
91  using itkVectorType = TITKVector;
92  if ( in.size() < itkVectorType::Dimension )
93  {
94  sitkExceptionMacro(<<"Unable to convert vector to ITK type\n"
95  << "Expected vector of length " << itkVectorType::Dimension
96  << " but only got " << in.size() << " elements." );
97  }
98  itkVectorType out;
99  for( unsigned int i = 0; i < itkVectorType::Dimension; ++i )
100  {
101  out[i] = in[i];
102  }
103  return out;
104 }
105 
108 template<typename TType, typename TITKVector>
109 std::vector<TType> SITKCommon_HIDDEN sitkITKVectorToSTL( const TITKVector & in )
110 {
111  std::vector<TType> out( TITKVector::Dimension );
112  for( unsigned int i = 0; i < TITKVector::Dimension; ++i )
113  {
114  out[i] = static_cast<TType>(in[i]);
115  }
116  return out;
117 }
118 
123 template<typename TType, typename TVectorOfITKVector>
124 std::vector<TType> SITKCommon_HIDDEN sitkVectorOfITKVectorToSTL( const TVectorOfITKVector & in )
125 {
126  using ITKVectorType = typename TVectorOfITKVector::ValueType;
127  std::vector<TType> out;
128  out.reserve( in.Size()*ITKVectorType::Dimension );
129  for( unsigned int i = 0; i < in.Size(); ++i )
130  {
131  const std::vector<TType> &temp = sitkITKVectorToSTL<TType>(in[i]);
132  out.insert(out.end(), temp.begin(), temp.end());
133  }
134  return out;
135 }
136 
137 
138 template<typename TType, typename TITKVector>
139 std::vector<TType> SITKCommon_HIDDEN sitkITKVectorToSTL( const std::vector<TITKVector> & in )
140 {
141  std::vector<TType> out;
142  out.reserve( in.size()*TITKVector::Dimension );
143  typename std::vector<TITKVector>::const_iterator iter = in.begin();
144  while(iter!=in.end())
145  {
146  for( unsigned int i = 0; i < TITKVector::Dimension; ++i )
147  {
148  out.push_back(static_cast<TType>((*iter)[i]));
149  }
150  ++iter;
151  }
152 
153  return out;
154 }
155 
159 template<unsigned int VImageDimension>
161 {
162  std::vector<unsigned int> out( VImageDimension*2 );
163  for( unsigned int i = 0; i < VImageDimension; ++i )
164  {
165  out[i] = static_cast<unsigned int>(in.GetIndex(i));
166  out[VImageDimension+i] = static_cast<unsigned int>(in.GetSize(i));
167  }
168  return out;
169 }
170 
171 
172 /* \brief Convert to an itk::Matrix type, where the vector is in row
173  * major form. If the vector is of 0-size then an identity matrix will
174  * be constructed.
175  */
176 template< typename TDirectionType >
177 TDirectionType SITKCommon_HIDDEN sitkSTLToITKDirection( const std::vector<double> &direction )
178 {
179  TDirectionType itkDirection;
180 
181  if ( direction.empty() )
182  {
183  itkDirection.SetIdentity();
184  }
185  else if( direction.size() == TDirectionType::RowDimensions*TDirectionType::ColumnDimensions )
186  {
187  std::copy( direction.begin(), direction.end(), itkDirection.GetVnlMatrix().begin() );
188  }
189  else
190  {
191  sitkExceptionMacro(<<"Length of input ("<<direction.size()<<") does not match matrix dimensions ("
192  <<TDirectionType::RowDimensions<<", "<<TDirectionType::ColumnDimensions<<").\n");
193  }
194  return itkDirection;
195 }
196 
197 
198 template< typename TDirectionType >
199 std::vector<double> SITKCommon_HIDDEN sitkITKDirectionToSTL( const TDirectionType & d)
200 {
201  return std::vector<double>( d.GetVnlMatrix().begin(), d.GetVnlMatrix().end() );
202 }
203 
204 
205 template< typename T, typename TType>
207 {
208  using itkVectorType = itk::Versor<T>;
209  if ( in.size() != 4 )
210  {
211  sitkExceptionMacro(<<"Unable to convert vector to ITK Versor type\n"
212  << "Expected vector of length " << 4
213  << " but got " << in.size() << " elements." );
214  }
215  itkVectorType out;
216  out.Set(in[0],in[1],in[2],in[3]);
217  return out;
218 }
219 
220 
221 template< typename TType, typename T>
222 std::vector<TType> SITKCommon_HIDDEN sitkITKVersorToSTL( const itk::Versor<T> & in )
223 {
224  std::vector<TType> out(4);
225  out[0] = in.GetX();
226  out[1] = in.GetY();
227  out[2] = in.GetZ();
228  out[3] = in.GetW();
229  return out;
230 }
231 
232 // Based on p0052r8 : Generic Scope Guard and RAII Wrapper for the
233 // Standard Library
234 // by Peter Sommerlad and Andrew L. Sandoval
235 template <typename F>
236 struct scope_exit {
237  F f_;
238  bool run_;
239  explicit scope_exit(F f) noexcept : f_(std::move(f)), run_(true) {}
240  scope_exit(scope_exit&& rhs) noexcept : f_((rhs.run_ = false, std::move(rhs.f_))), run_(true) {}
242  {
243  if (run_)
244  f_();
245  }
246 
247  scope_exit& operator=(scope_exit&& rhs) = delete;
248  scope_exit(scope_exit const&) = delete;
249  scope_exit& operator=(scope_exit const&) = delete;
250 };
251 
252 template <typename F>
254 {
255  return scope_exit<F>{ std::forward<F>(f) };
256 }
257 
258 
259 }
260 }
261 
262 #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:89
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:240
itk::Versor::GetY
ValueType GetY() const
itk::simple::sitkITKVersorToSTL
std::vector< TType > SITKCommon_HIDDEN sitkITKVersorToSTL(const itk::Versor< T > &in)
Definition: sitkTemplateFunctions.h:222
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:31
sitkCommon.h
itk::simple::sitkVectorOfITKVectorToSTL
std::vector< TType > SITKCommon_HIDDEN sitkVectorOfITKVectorToSTL(const TVectorOfITKVector &in)
Convert an ITK style array of ITK fixed witdth vector to std::vector.
Definition: sitkTemplateFunctions.h:124
itk::ImageRegion::GetSize
const SizeType & GetSize() const
itk::Versor
Definition: sitkTemplateFunctions.h:32
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:109
sitkMacro.h
itk::simple::sitkSTLToITKDirection
TDirectionType SITKCommon_HIDDEN sitkSTLToITKDirection(const std::vector< double > &direction)
Definition: sitkTemplateFunctions.h:177
itk::simple::scope_exit::run_
bool run_
Definition: sitkTemplateFunctions.h:238
itk::simple::sitkSTLVectorToITKVersor
itk::Versor< T > SITKCommon_HIDDEN sitkSTLVectorToITKVersor(const std::vector< TType > &in)
Definition: sitkTemplateFunctions.h:206
SITKCommon_HIDDEN
#define SITKCommon_HIDDEN
Definition: sitkCommon.h:44
itk::simple::sitkSTLVectorToITKPointVector
TITKPointVector SITKCommon_HIDDEN sitkSTLVectorToITKPointVector(const std::vector< TType > &in)
Definition: sitkTemplateFunctions.h:65
itk::simple::sitkITKDirectionToSTL
std::vector< double > SITKCommon_HIDDEN sitkITKDirectionToSTL(const TDirectionType &d)
Definition: sitkTemplateFunctions.h:199
itk::simple::Unused
void SITKCommon_HIDDEN Unused(const T &)
A function which does nothing.
Definition: sitkTemplateFunctions.h:42
itk::Versor::GetX
ValueType GetX() const
itk::simple::scope_exit::~scope_exit
~scope_exit()
Definition: sitkTemplateFunctions.h:241
itk::simple::scope_exit::scope_exit
scope_exit(F f) noexcept
Definition: sitkTemplateFunctions.h:239
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:160
itk::simple::scope_exit
Definition: sitkTemplateFunctions.h:236
Dimension
constexpr unsigned int Dimension
itk::simple::make_scope_exit
scope_exit< F > make_scope_exit(F &&f) noexcept
Definition: sitkTemplateFunctions.h:253
sitkExceptionMacro
#define sitkExceptionMacro(x)
Definition: sitkMacro.h:71
itk::simple::scope_exit::f_
F f_
Definition: sitkTemplateFunctions.h:237