00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkIndex_h
00018 #define __itkIndex_h
00019
00020 #include "itkMacro.h"
00021 #include "itkOffset.h"
00022 #include "itkSize.h"
00023
00024 #include <memory>
00025
00026 #include "itkExceptionObject.h"
00027
00028 namespace itk
00029 {
00030
00061 template<unsigned int VIndexDimension=2>
00062 class Index {
00063 public:
00065 typedef Index Self;
00066
00068 typedef Index<VIndexDimension> IndexType;
00069 typedef long IndexValueType;
00070
00072 static unsigned int GetIndexDimension() { return VIndexDimension; }
00073
00075 typedef Size<VIndexDimension> SizeType;
00076
00078 typedef Offset<VIndexDimension> OffsetType;
00079 typedef typename OffsetType::OffsetValueType OffsetValueType;
00080
00082 const Self
00083 operator+(const SizeType &size) const
00084 {
00085 Self result;
00086 for (unsigned int i=0; i < VIndexDimension; i++)
00087 { result[i] = m_Index[i] + static_cast<IndexValueType>(size[i]); }
00088 return result;
00089 }
00090
00092 const Self &
00093 operator+=(const SizeType &size)
00094 {
00095 for (unsigned int i=0; i < VIndexDimension; i++)
00096 { m_Index[i] += static_cast<IndexValueType>(size[i]); }
00097 return *this;
00098 }
00099
00101 const Self
00102 operator-(const SizeType &size) const
00103 {
00104 Self result;
00105 for (unsigned int i=0; i < VIndexDimension; i++)
00106 { result[i] = m_Index[i] - static_cast<IndexValueType>(size[i]); }
00107 return result;
00108 }
00109
00111 const Self &
00112 operator-=(const SizeType &size)
00113 {
00114 for (unsigned int i=0; i < VIndexDimension; i++)
00115 { m_Index[i] -= static_cast<IndexValueType>(size[i]); }
00116 return *this;
00117 }
00118
00120 const Self
00121 operator+(const OffsetType &offset) const
00122 {
00123 Self result;
00124 for (unsigned int i=0; i < VIndexDimension; i++)
00125 { result[i] = m_Index[i] + offset[i]; }
00126 return result;
00127 }
00128
00130 const Self &
00131 operator+=(const OffsetType &offset)
00132 {
00133 for (unsigned int i=0; i < VIndexDimension; i++)
00134 { m_Index[i] += offset[i]; }
00135 return *this;
00136 }
00137
00139 const Self &
00140 operator-=(const OffsetType &offset)
00141 {
00142 for (unsigned int i=0; i < VIndexDimension; i++)
00143 { m_Index[i] -= offset[i]; }
00144 return *this;
00145 }
00146
00148 const Self
00149 operator-(const OffsetType &off) const
00150 {
00151 Self result;
00152 for (unsigned int i=0; i < VIndexDimension; i++)
00153 { result[i] = m_Index[i] - off.m_Offset[i]; }
00154 return result;
00155 }
00156
00158 const OffsetType
00159 operator-(const Self &vec) const
00160 {
00161 OffsetType result;
00162 for (unsigned int i=0; i < VIndexDimension; i++)
00163 { result[i] = m_Index[i] - vec.m_Index[i]; }
00164 return result;
00165 }
00166
00169 const Self
00170 operator*(const SizeType &vec) const
00171 {
00172 Self result;
00173 for (unsigned int i=0; i < VIndexDimension; i++)
00174 { result[i] = m_Index[i] * static_cast<IndexValueType>(vec.m_Size[i]); }
00175 return result;
00176 }
00177
00179 bool
00180 operator==(const Self &vec) const
00181 {
00182 bool same=true;
00183 for (unsigned int i=0; i < VIndexDimension && same; i++)
00184 { same = (m_Index[i] == vec.m_Index[i]); }
00185 return same;
00186 }
00187
00189 bool
00190 operator!=(const Self &vec) const
00191 {
00192 bool same=true;
00193 for (unsigned int i=0; i < VIndexDimension && same; i++)
00194 { same = (m_Index[i] == vec.m_Index[i]); }
00195 return !same;
00196 }
00197
00200 IndexValueType & operator[](unsigned int dim)
00201 { return m_Index[dim]; }
00202
00206 IndexValueType operator[](unsigned int dim) const
00207 { return m_Index[dim]; }
00208
00211 const IndexValueType *GetIndex() const { return m_Index; };
00212
00217 void SetIndex(const IndexValueType val[VIndexDimension])
00218 { memcpy(m_Index, val, sizeof(IndexValueType)*VIndexDimension); }
00219
00226 void SetElement(unsigned long element, IndexValueType val )
00227 { m_Index[ element ] = val; }
00228
00235 IndexValueType GetElement( unsigned long element )
00236 { return m_Index[ element ]; }
00237
00241 static Self GetBasisIndex(unsigned int dim);
00242
00245 void Fill(IndexValueType value)
00246 { for(unsigned int i=0;i < VIndexDimension; ++i) m_Index[i] = value; }
00247
00253 IndexValueType m_Index[VIndexDimension];
00254
00255 };
00256
00257
00258 template<unsigned int VIndexDimension>
00259 Index<VIndexDimension>
00260 Index<VIndexDimension>
00261 ::GetBasisIndex(unsigned int dim)
00262 {
00263 Self ind;
00264
00265 memset(ind.m_Index, 0, sizeof(IndexValueType)*VIndexDimension);
00266 ind.m_Index[dim] = 1;
00267 return ind;
00268 }
00269
00270 template<unsigned int VIndexDimension>
00271 std::ostream & operator<<(std::ostream &os, const Index<VIndexDimension> &ind)
00272 {
00273 os << "[";
00274 for (unsigned int i=0; i+1 < VIndexDimension; ++i)
00275 {
00276 os << ind[i] << ", ";
00277 }
00278 if (VIndexDimension >= 1)
00279 {
00280 os << ind[VIndexDimension-1];
00281 }
00282 os << "]";
00283 return os;
00284 }
00285
00286 #ifdef ITK_EXPLICIT_INSTANTIATION
00287 extern template class Index<1>;
00288 extern template class Index<2>;
00289 extern template class Index<3>;
00290 extern template class Index<4>;
00291 extern template class Index<5>;
00292 extern template std::ostream & operator<<(std::ostream &os, const Index<1> &ind);
00293 extern template std::ostream & operator<<(std::ostream &os, const Index<2> &ind);
00294 extern template std::ostream & operator<<(std::ostream &os, const Index<3> &ind);
00295 extern template std::ostream & operator<<(std::ostream &os, const Index<4> &ind);
00296 extern template std::ostream & operator<<(std::ostream &os, const Index<5> &ind);
00297 #endif
00298
00299
00300 }
00301
00302 #endif