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 OffsetType &offset) const
00103 {
00104 Self result;
00105 for (unsigned int i=0; i < VIndexDimension; i++)
00106 { result[i] = m_Index[i] + offset[i]; }
00107 return result;
00108 }
00109
00111 const Self &
00112 operator+=(const OffsetType &offset)
00113 {
00114 for (unsigned int i=0; i < VIndexDimension; i++)
00115 { m_Index[i] += offset[i]; }
00116 return *this;
00117 }
00118
00120 const Self &
00121 operator-=(const OffsetType &offset)
00122 {
00123 for (unsigned int i=0; i < VIndexDimension; i++)
00124 { m_Index[i] -= offset[i]; }
00125 return *this;
00126 }
00127
00129 const Self
00130 operator-(const OffsetType &off) const
00131 {
00132 Self result;
00133 for (unsigned int i=0; i < VIndexDimension; i++)
00134 { result[i] = m_Index[i] - off.m_Offset[i]; }
00135 return result;
00136 }
00137
00139 const OffsetType
00140 operator-(const Self &vec)
00141 {
00142 OffsetType result;
00143 for (unsigned int i=0; i < VIndexDimension; i++)
00144 { result[i] = m_Index[i] - vec.m_Index[i]; }
00145 return result;
00146 }
00147
00150 const Self
00151 operator*(const SizeType &vec)
00152 {
00153 Self result;
00154 for (unsigned int i=0; i < VIndexDimension; i++)
00155 { result[i] = m_Index[i] * static_cast<IndexValueType>(vec.m_Size[i]); }
00156 return result;
00157 }
00158
00160 bool
00161 operator==(const Self &vec) const
00162 {
00163 bool same=true;
00164 for (unsigned int i=0; i < VIndexDimension && same; i++)
00165 { same = (m_Index[i] == vec.m_Index[i]); }
00166 return same;
00167 }
00168
00170 bool
00171 operator!=(const Self &vec) const
00172 {
00173 bool same=true;
00174 for (unsigned int i=0; i < VIndexDimension && same; i++)
00175 { same = (m_Index[i] == vec.m_Index[i]); }
00176 return !same;
00177 }
00178
00181 IndexValueType & operator[](unsigned int dim)
00182 { return m_Index[dim]; }
00183
00187 IndexValueType operator[](unsigned int dim) const
00188 { return m_Index[dim]; }
00189
00192 const IndexValueType *GetIndex() const { return m_Index; };
00193
00198 void SetIndex(const IndexValueType val[VIndexDimension])
00199 { memcpy(m_Index, val, sizeof(IndexValueType)*VIndexDimension); }
00200
00207 void SetElement(unsigned long element, IndexValueType val )
00208 { m_Index[ element ] = val; }
00209
00216 IndexValueType GetElement( unsigned long element )
00217 { return m_Index[ element ]; }
00218
00222 static Self GetBasisIndex(unsigned int dim);
00223
00226 void Fill(IndexValueType value)
00227 { for(unsigned int i=0;i < VIndexDimension; ++i) m_Index[i] = value; }
00228
00234 IndexValueType m_Index[VIndexDimension];
00235
00236 };
00237
00238
00239 template<unsigned int VIndexDimension>
00240 Index<VIndexDimension>
00241 Index<VIndexDimension>
00242 ::GetBasisIndex(unsigned int dim)
00243 {
00244 Self ind;
00245
00246 memset(ind.m_Index, 0, sizeof(IndexValueType)*VIndexDimension);
00247 ind.m_Index[dim] = 1;
00248 return ind;
00249 }
00250
00251 template<unsigned int VIndexDimension>
00252 std::ostream & operator<<(std::ostream &os, const Index<VIndexDimension> &ind)
00253 {
00254 os << "[";
00255 for (unsigned int i=0; i+1 < VIndexDimension; ++i)
00256 {
00257 os << ind[i] << ", ";
00258 }
00259 if (VIndexDimension >= 1)
00260 {
00261 os << ind[VIndexDimension-1];
00262 }
00263 os << "]";
00264 return os;
00265 }
00266
00267
00268 }
00269
00270 #endif