SimpleITK  1.0.1
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 
33 namespace simple {
34 
40 template <typename T>
41 void SITKCommon_HIDDEN Unused( const T &) {};
42 
50 template <typename T>
51 SITKCommon_HIDDEN std::ostream & operator<<( std::ostream & os, const std::vector<T>& v)
52 {
53  if ( v.empty() )
54  {
55  return os << "[ ]";
56  }
57 
58  os << "[ ";
59  std::copy( v.begin(), v.end()-1, std::ostream_iterator<T>(os, ", ") );
60  return os << v.back() << " ]";
61 }
62 
63 template< typename TITKPointVector, typename TType>
64 TITKPointVector SITKCommon_HIDDEN sitkSTLVectorToITKPointVector( const std::vector< TType > & in )
65 {
66 
67  typedef TITKPointVector itkPointVectorType;
68  itkPointVectorType out;
69 
70  unsigned int Dimension = itkPointVectorType::value_type::GetPointDimension();
71 
72  for( unsigned int i = 0; i <= in.size()- Dimension; i += Dimension )
73  {
74  typename itkPointVectorType::value_type pt(&in[i]);
75  out.push_back(pt);
76  }
77  return out;
78 
79 }
80 
87 template< typename TITKVector, typename TType>
88 TITKVector SITKCommon_HIDDEN sitkSTLVectorToITK( const std::vector< TType > & in )
89 {
90  typedef TITKVector itkVectorType;
91  if ( in.size() < itkVectorType::Dimension )
92  {
93  sitkExceptionMacro(<<"Unable to convert vector to ITK type\n"
94  << "Expected vector of length " << itkVectorType::Dimension
95  << " but only got " << in.size() << " elements." );
96  }
97  itkVectorType out;
98  for( unsigned int i = 0; i < itkVectorType::Dimension; ++i )
99  {
100  out[i] = in[i];
101  }
102  return out;
103 }
104 
107 template<typename TType, typename TITKVector>
108 std::vector<TType> SITKCommon_HIDDEN sitkITKVectorToSTL( const TITKVector & in )
109 {
110  std::vector<TType> out( TITKVector::Dimension );
111  for( unsigned int i = 0; i < TITKVector::Dimension; ++i )
112  {
113  out[i] = static_cast<TType>(in[i]);
114  }
115  return out;
116 }
117 
118 
119 template<typename TType, typename TITKVector>
120 std::vector<TType> SITKCommon_HIDDEN sitkITKVectorToSTL( const std::vector<TITKVector> & in )
121 {
122  std::vector<TType> out;
123  out.reserve( in.size()*TITKVector::Dimension );
124  typename std::vector<TITKVector>::const_iterator iter = in.begin();
125  while(iter!=in.end())
126  {
127  for( unsigned int i = 0; i < TITKVector::Dimension; ++i )
128  {
129  out.push_back(static_cast<TType>((*iter)[i]));
130  }
131  ++iter;
132  }
133 
134  return out;
135 }
136 
140 template<unsigned int VImageDimension>
142 {
143  std::vector<unsigned int> out( VImageDimension*2 );
144  for( unsigned int i = 0; i < VImageDimension; ++i )
145  {
146  out[i] = static_cast<unsigned int>(in.GetIndex(i));
147  out[VImageDimension+i] = static_cast<unsigned int>(in.GetSize(i));
148  }
149  return out;
150 }
151 
152 
153 /* \brief Convert to an itk::Matrix type, where the vector is in row
154  * major form. If the vector is of 0-size then an identity matrix will
155  * be constructed.
156  */
157 template< typename TDirectionType >
158 TDirectionType SITKCommon_HIDDEN sitkSTLToITKDirection( const std::vector<double> &direction )
159 {
160  TDirectionType itkDirection;
161 
162  if ( direction.size() == 0 )
163  {
164  itkDirection.SetIdentity();
165  }
166  else if( direction.size() == TDirectionType::RowDimensions*TDirectionType::ColumnDimensions )
167  {
168  std::copy( direction.begin(), direction.end(), itkDirection.GetVnlMatrix().begin() );
169  }
170  else
171  {
172  sitkExceptionMacro(<<"Length of input ("<<direction.size()<<") does not match matrix dimensions ("
173  <<TDirectionType::RowDimensions<<", "<<TDirectionType::ColumnDimensions<<").\n");
174  }
175  return itkDirection;
176 }
177 
178 
179 template< typename TDirectionType >
180 std::vector<double> SITKCommon_HIDDEN sitkITKDirectionToSTL( const TDirectionType & d)
181 {
182  return std::vector<double>( d.GetVnlMatrix().begin(), d.GetVnlMatrix().end() );
183 }
184 
185 
186 
187 }
188 }
189 
190 #endif
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
TDirectionType SITKCommon_HIDDEN sitkSTLToITKDirection(const std::vector< double > &direction)
const SizeType & GetSize() const
std::vector< TType > SITKCommon_HIDDEN sitkITKVectorToSTL(const TITKVector &in)
Convert an ITK fixed width vector to a std::vector.
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:89
std::vector< double > SITKCommon_HIDDEN sitkITKDirectionToSTL(const TDirectionType &d)
const IndexType & GetIndex() const
#define SITKCommon_HIDDEN
Definition: sitkCommon.h:44