Orfeo Toolbox  3.16
itkIndex.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkIndex.h,v $
5  Language: C++
6  Date: $Date: 2009-10-27 16:06:22 $
7  Version: $Revision: 1.66 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkIndex_h
18 #define __itkIndex_h
19 
20 #include "itkMacro.h"
21 #include "itkOffset.h"
22 #include "itkSize.h"
23 #include "itkFixedArray.h"
24 #include "itkMath.h"
25 
26 #include <memory>
27 
28 #include "itkExceptionObject.h"
29 
30 namespace itk
31 {
32 
33 namespace Functor
34 {
35 template<unsigned int VIndexDimension> class IndexLexicographicCompare;
36 }
37 
67 template<unsigned int VIndexDimension=2>
68 class Index {
69 public:
71  typedef Index Self;
72 
75  typedef long IndexValueType;
76 
78  static unsigned int GetIndexDimension() { return VIndexDimension; }
79 
82 
86 
89 
91  const Self
92  operator+(const SizeType &size) const
93  {
94  Self result;
95  for (unsigned int i=0; i < VIndexDimension; i++)
96  { result[i] = m_Index[i] + static_cast<IndexValueType>(size[i]); }
97  return result;
98  }
99 
101  const Self &
102  operator+=(const SizeType &size)
103  {
104  for (unsigned int i=0; i < VIndexDimension; i++)
105  { m_Index[i] += static_cast<IndexValueType>(size[i]); }
106  return *this;
107  }
108 
110  const Self
111  operator-(const SizeType &size) const
112  {
113  Self result;
114  for (unsigned int i=0; i < VIndexDimension; i++)
115  { result[i] = m_Index[i] - static_cast<IndexValueType>(size[i]); }
116  return result;
117  }
118 
120  const Self &
121  operator-=(const SizeType &size)
122  {
123  for (unsigned int i=0; i < VIndexDimension; i++)
124  { m_Index[i] -= static_cast<IndexValueType>(size[i]); }
125  return *this;
126  }
127 
129  const Self
130  operator+(const OffsetType &offset) const
131  {
132  Self result;
133  for (unsigned int i=0; i < VIndexDimension; i++)
134  { result[i] = m_Index[i] + offset[i]; }
135  return result;
136  }
137 
139  const Self &
140  operator+=(const OffsetType &offset)
141  {
142  for (unsigned int i=0; i < VIndexDimension; i++)
143  { m_Index[i] += offset[i]; }
144  return *this;
145  }
146 
148  const Self &
149  operator-=(const OffsetType &offset)
150  {
151  for (unsigned int i=0; i < VIndexDimension; i++)
152  { m_Index[i] -= offset[i]; }
153  return *this;
154  }
155 
157  const Self
158  operator-(const OffsetType &off) const
159  {
160  Self result;
161  for (unsigned int i=0; i < VIndexDimension; i++)
162  { result[i] = m_Index[i] - off.m_Offset[i]; }
163  return result;
164  }
165 
167  const OffsetType
168  operator-(const Self &vec) const
169  {
170  OffsetType result;
171  for (unsigned int i=0; i < VIndexDimension; i++)
172  { result[i] = m_Index[i] - vec.m_Index[i]; }
173  return result;
174  }
175 
178  const Self
179  operator*(const SizeType &vec) const
180  {
181  Self result;
182  for (unsigned int i=0; i < VIndexDimension; i++)
183  { result[i] = m_Index[i] * static_cast<IndexValueType>(vec.m_Size[i]); }
184  return result;
185  }
186 
188  bool
189  operator==(const Self &vec) const
190  {
191  bool same=true;
192  for (unsigned int i=0; i < VIndexDimension && same; i++)
193  { same = (m_Index[i] == vec.m_Index[i]); }
194  return same;
195  }
196 
198  bool
199  operator!=(const Self &vec) const
200  {
201  bool same=true;
202  for (unsigned int i=0; i < VIndexDimension && same; i++)
203  { same = (m_Index[i] == vec.m_Index[i]); }
204  return !same;
205  }
206 
209  IndexValueType & operator[](unsigned int dim)
210  { return m_Index[dim]; }
211 
215  IndexValueType operator[](unsigned int dim) const
216  { return m_Index[dim]; }
217 
220  const IndexValueType *GetIndex() const { return m_Index; };
221 
226  void SetIndex(const IndexValueType val[VIndexDimension])
227  { memcpy(m_Index, val, sizeof(IndexValueType)*VIndexDimension); }
228 
235  void SetElement(unsigned long element, IndexValueType val )
236  { m_Index[ element ] = val; }
237 
244  IndexValueType GetElement( unsigned long element ) const
245  { return m_Index[ element ]; }
246 
250  static Self GetBasisIndex(unsigned int dim);
251 
254  void Fill(IndexValueType value)
255  { for(unsigned int i=0;i < VIndexDimension; ++i) m_Index[i] = value; }
256 
262  IndexValueType m_Index[VIndexDimension];
263 
265  template <class TCoordRep>
267  {
268 #ifdef ITK_USE_TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING
269  itkForLoopRoundingAndAssignmentMacro(IndexType,ContinuousIndexType,IndexValueType,m_Index,point,VIndexDimension);
270 #else
271  for(unsigned int i=0;i < VIndexDimension; ++i)
272  {
273  m_Index[i] = Math::Round<IndexValueType>( point[i] );
274  }
275 #endif
276  }
277 
279  template <class TCoordRep>
281  {
282  for(unsigned int i=0;i < VIndexDimension; ++i)
283  {
284  m_Index[i] = static_cast< IndexValueType>( point[i] );
285  }
286  }
287 
288 // force gccxml to find the constructors found before the internal upgrade to gcc 4.2
289 #if defined(CABLE_CONFIGURATION)
290  Index(); //purposely not implemented
291  Index(const Self&); //purposely not implemented
292  void operator=(const Self&); //purposely not implemented
293 #endif
294 
295 };
296 
297 namespace Functor
298 {
306 template<unsigned int VIndexDimension>
308 {
309 public:
311  Index<VIndexDimension> const& r) const
312  {
313  for(unsigned int i=0; i < VIndexDimension; ++i)
314  {
315  if(l.m_Index[i] < r.m_Index[i])
316  {
317  return true;
318  }
319  else if(l.m_Index[i] > r.m_Index[i])
320  {
321  return false;
322  }
323  }
324  return false;
325  }
326 };
327 }
328 
329 template<unsigned int VIndexDimension>
330 Index<VIndexDimension>
332 ::GetBasisIndex(unsigned int dim)
333 {
334  Self ind;
335 
336  memset(ind.m_Index, 0, sizeof(IndexValueType)*VIndexDimension);
337  ind.m_Index[dim] = 1;
338  return ind;
339 }
340 
341 template<unsigned int VIndexDimension>
342 std::ostream & operator<<(std::ostream &os, const Index<VIndexDimension> &ind)
343 {
344  os << "[";
345  for (unsigned int i=0; i+1 < VIndexDimension; ++i)
346  {
347  os << ind[i] << ", ";
348  }
349  if (VIndexDimension >= 1)
350  {
351  os << ind[VIndexDimension-1];
352  }
353  os << "]";
354  return os;
355 }
356 
357 } // end namespace itk
358 
359 #endif

Generated at Sat Feb 2 2013 23:45:39 for Orfeo Toolbox with doxygen 1.8.1.1