SimpleITK  1.2.4
sitkTemplateFunctions.h
Go to the documentation of this file.
1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
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  typedef TITKPointVector itkPointVectorType;
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  typedef TITKVector itkVectorType;
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  typedef typename TVectorOfITKVector::ValueType ITKVectorType;
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.size() == 0 )
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  typedef itk::Versor<T> itkVectorType;
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 
233 }
234 }
235 
236 #endif
ValueType GetW() const
TITKVector SITKCommon_HIDDEN sitkSTLVectorToITK(const std::vector< TType > &in)
Copy the elements of an std::vector into an ITK fixed width vector.
constexpr unsigned int Dimension
std::vector< TType > SITKCommon_HIDDEN sitkVectorOfITKVectorToSTL(const TVectorOfITKVector &in)
Convert an ITK style array of ITK fixed witdth vector to std::vector.
ValueType GetY() const
TDirectionType SITKCommon_HIDDEN sitkSTLToITKDirection(const std::vector< double > &direction)
itk::Versor< T > SITKCommon_HIDDEN sitkSTLVectorToITKVersor(const std::vector< TType > &in)
const SizeType & GetSize() const
std::vector< TType > SITKCommon_HIDDEN sitkITKVectorToSTL(const TITKVector &in)
Convert an ITK fixed width vector to a std::vector.
ValueType GetX() const
TITKPointVector SITKCommon_HIDDEN sitkSTLVectorToITKPointVector(const std::vector< TType > &in)
void SITKCommon_HIDDEN Unused(const T &)
A function which does nothing.
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...
#define sitkExceptionMacro(x)
Definition: sitkMacro.h:88
ValueType GetZ() const
std::vector< double > SITKCommon_HIDDEN sitkITKDirectionToSTL(const TDirectionType &d)
const IndexType & GetIndex() const
std::vector< TType > SITKCommon_HIDDEN sitkITKVersorToSTL(const itk::Versor< T > &in)
#define SITKCommon_HIDDEN
Definition: sitkCommon.h:44