ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkIndex.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 itkIndex_h
19 #define itkIndex_h
20 
21 #include "itkOffset.h"
22 #include "itkFixedArray.h"
23 #include "itkMath.h"
24 
25 #include <memory>
26 
27 namespace itk
28 {
29 namespace Functor
30 {
31 // Forward reference because of circular dependencies
32 template< unsigned int VIndexDimension >
33 class ITK_TEMPLATE_EXPORT IndexLexicographicCompare;
34 }
35 
71 template< unsigned int VIndexDimension = 2 >
72 class ITK_TEMPLATE_EXPORT Index
73 {
74 public:
76  typedef Index Self;
77 
81 
83  itkStaticConstMacro(Dimension, unsigned int, VIndexDimension);
84 
86  static unsigned int GetIndexDimension() { return VIndexDimension; }
87 
90 
94 
97 
99  const Self
100  operator+(const SizeType & size) const
101  {
102  Self result;
103 
104  for ( unsigned int i = 0; i < VIndexDimension; i++ )
105  { result[i] = m_Index[i] + static_cast< IndexValueType >( size[i] ); }
106  return result;
107  }
108 
110  const Self &
111  operator+=(const SizeType & size)
112  {
113  for ( unsigned int i = 0; i < VIndexDimension; i++ )
114  { m_Index[i] += static_cast< IndexValueType >( size[i] ); }
115  return *this;
116  }
118 
121  const Self
122  operator-(const SizeType & size) const
123  {
124  Self result;
125 
126  for ( unsigned int i = 0; i < VIndexDimension; i++ )
127  { result[i] = m_Index[i] - static_cast< IndexValueType >( size[i] ); }
128  return result;
129  }
130 
132  const Self &
133  operator-=(const SizeType & size)
134  {
135  for ( unsigned int i = 0; i < VIndexDimension; i++ )
136  { m_Index[i] -= static_cast< IndexValueType >( size[i] ); }
137  return *this;
138  }
140 
142  const Self
143  operator+(const OffsetType & offset) const
144  {
145  Self result;
146 
147  for ( unsigned int i = 0; i < VIndexDimension; i++ )
148  { result[i] = m_Index[i] + offset[i]; }
149  return result;
150  }
151 
153  const Self &
154  operator+=(const OffsetType & offset)
155  {
156  for ( unsigned int i = 0; i < VIndexDimension; i++ )
157  { m_Index[i] += offset[i]; }
158  return *this;
159  }
161 
163  const Self &
164  operator-=(const OffsetType & offset)
165  {
166  for ( unsigned int i = 0; i < VIndexDimension; i++ )
167  { m_Index[i] -= offset[i]; }
168  return *this;
169  }
171 
173  const Self
174  operator-(const OffsetType & off) const
175  {
176  Self result;
177 
178  for ( unsigned int i = 0; i < VIndexDimension; i++ )
179  { result[i] = m_Index[i] - off.m_Offset[i]; }
180  return result;
181  }
182 
184  const OffsetType
185  operator-(const Self & vec) const
186  {
187  OffsetType result;
188 
189  for ( unsigned int i = 0; i < VIndexDimension; i++ )
190  { result[i] = m_Index[i] - vec.m_Index[i]; }
191  return result;
192  }
193 
196  const Self
197  operator*(const SizeType & vec) const
198  {
199  Self result;
200 
201  for ( unsigned int i = 0; i < VIndexDimension; i++ )
202  { result[i] = m_Index[i] * static_cast< IndexValueType >( vec.m_Size[i] ); }
203  return result;
204  }
205 
207  bool
208  operator==(const Self & vec) const
209  {
210  bool same = true;
211 
212  for ( unsigned int i = 0; i < VIndexDimension && same; i++ )
213  { same = ( m_Index[i] == vec.m_Index[i] ); }
214  return same;
215  }
216 
218  bool
219  operator!=(const Self & vec) const
220  {
221  bool same = true;
222 
223  for ( unsigned int i = 0; i < VIndexDimension && same; i++ )
224  { same = ( m_Index[i] == vec.m_Index[i] ); }
225  return !same;
226  }
227 
228 // false positive warnings with GCC 4.9
229 #if defined( __GNUC__ )
230 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 )
231 #pragma GCC diagnostic push
232 #pragma GCC diagnostic ignored "-Warray-bounds"
233 #endif
234 #endif
235 
237  IndexValueType & operator[](unsigned int dim)
238  {
239  return m_Index[dim];
240  }
241 #if defined( __GNUC__ )
242 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 )
243 #pragma GCC diagnostic pop
244 #endif
245 #endif
246 
250  IndexValueType operator[](unsigned int dim) const
251  { return m_Index[dim]; }
252 
255  const IndexValueType * GetIndex() const { return m_Index; }
256 
261  void SetIndex(const IndexValueType val[VIndexDimension])
262  {
263  std::copy(val,
264  val+VIndexDimension,
265  m_Index);
266  }
267 
274  void SetElement(unsigned long element, IndexValueType val)
275  { m_Index[element] = val; }
276 
283  IndexValueType GetElement(unsigned long element) const
284  { return m_Index[element]; }
285 
289  static Self GetBasisIndex(unsigned int dim);
290 
293  void Fill(IndexValueType value)
294  { for ( unsigned int i = 0; i < VIndexDimension; ++i ) { m_Index[i] = value; } }
295 
301  IndexValueType m_Index[VIndexDimension];
302 
304  template< typename TCoordRep >
306  {
307  itkForLoopRoundingAndAssignmentMacro(IndexType,
308  ContinuousIndexType,
310  m_Index,
311  point,
312  VIndexDimension);
313  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
314  * Leaving here for documentation purposes
315  * for ( unsigned int i = 0; i < VIndexDimension; ++i )
316  * {
317  * m_Index[i] = Math::Round< IndexValueType >(point[i]);
318  * }
319  */
320  }
322 
324  template< typename TCoordRep >
326  {
327  for ( unsigned int i = 0; i < VIndexDimension; ++i )
328  {
329  m_Index[i] = static_cast< IndexValueType >( point[i] );
330  }
331  }
333 
334 #if defined( ITK_WRAPPING_PARSER )
335  // Do not use c++11 'delete' keyword here. This code block is here to
336  // explicitly provide the wrapping facilities with handles to the default and
337  // copy constructors, and the assignment operator that are otherwise declared
338  // implicitly.
339  Index();
340  Index(const Self&);
341  void operator=(const Self&);
342 #endif
343 };
344 
345 namespace Functor
346 {
355 template< unsigned int VIndexDimension >
356 class ITK_TEMPLATE_EXPORT IndexLexicographicCompare
357 {
358 public:
360  Index< VIndexDimension > const & r) const
361  {
362  for ( unsigned int i = 0; i < VIndexDimension; ++i )
363  {
364  if ( l.m_Index[i] < r.m_Index[i] )
365  {
366  return true;
367  }
368  else if ( l.m_Index[i] > r.m_Index[i] )
369  {
370  return false;
371  }
372  }
373  return false;
374  }
375 };
376 }
377 
378 template< unsigned int VIndexDimension >
379 Index< VIndexDimension >
381 ::GetBasisIndex(unsigned int dim)
382 {
383  Self ind;
384 
385  memset(ind.m_Index, 0, sizeof( IndexValueType ) * VIndexDimension);
386  ind.m_Index[dim] = 1;
387  return ind;
388 }
389 
390 template< unsigned int VIndexDimension >
391 std::ostream & operator<<(std::ostream & os, const Index< VIndexDimension > & ind)
392 {
393  os << "[";
394  for ( unsigned int i = 0; i + 1 < VIndexDimension; ++i )
395  {
396  os << ind[i] << ", ";
397  }
398  if ( VIndexDimension >= 1 )
399  {
400  os << ind[VIndexDimension - 1];
401  }
402  os << "]";
403  return os;
404 }
405 } // end namespace itk
406 
407 #endif
bool operator()(Index< VIndexDimension > const &l, Index< VIndexDimension > const &r) const
Definition: itkIndex.h:359
const IndexValueType * GetIndex() const
Definition: itkIndex.h:255
OffsetValueType m_Offset[VOffsetDimension]
Definition: itkOffset.h:211
const Self & operator-=(const OffsetType &offset)
Definition: itkIndex.h:164
const Self operator-(const OffsetType &off) const
Definition: itkIndex.h:174
IndexValueType & operator[](unsigned int dim)
Definition: itkIndex.h:237
Represent the offset between two n-dimensional indexes in a n-dimensional image.
Definition: itkOffset.h:56
bool operator!=(const Self &vec) const
Definition: itkIndex.h:219
IndexValueType m_Index[VIndexDimension]
Definition: itkIndex.h:301
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
Size< VIndexDimension > SizeType
Definition: itkIndex.h:89
signed long OffsetValueType
Definition: itkIntTypes.h:154
signed long IndexValueType
Definition: itkIntTypes.h:150
const OffsetType operator-(const Self &vec) const
Definition: itkIndex.h:185
Order Index instances lexicographically.
Definition: itkIndex.h:356
Index< VIndexDimension > IndexType
Definition: itkIndex.h:79
Offset< VIndexDimension > OffsetType
Definition: itkIndex.h:92
void CopyWithRound(const FixedArray< TCoordRep, VIndexDimension > &point)
Definition: itkIndex.h:305
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
static Self GetBasisIndex(unsigned int dim)
Definition: itkIndex.h:381
::itk::IndexValueType IndexValueType
Definition: itkIndex.h:80
const Self & operator+=(const OffsetType &offset)
Definition: itkIndex.h:154
bool operator==(const Self &vec) const
Definition: itkIndex.h:208
static unsigned int GetIndexDimension()
Definition: itkIndex.h:86
Index Self
Definition: itkIndex.h:76
void Fill(IndexValueType value)
Definition: itkIndex.h:293
const Self operator*(const SizeType &vec) const
Definition: itkIndex.h:197
SizeValueType m_Size[VDimension]
Definition: itkSize.h:211
const Self operator+(const SizeType &size) const
Definition: itkIndex.h:100
const Self operator+(const OffsetType &offset) const
Definition: itkIndex.h:143
void CopyWithCast(const FixedArray< TCoordRep, VIndexDimension > &point)
Definition: itkIndex.h:325
const Self operator-(const SizeType &size) const
Definition: itkIndex.h:122
const Self & operator-=(const SizeType &size)
Definition: itkIndex.h:133
Functor::IndexLexicographicCompare< VIndexDimension > LexicographicCompare
Definition: itkIndex.h:96
void SetElement(unsigned long element, IndexValueType val)
Definition: itkIndex.h:274
const Self & operator+=(const SizeType &size)
Definition: itkIndex.h:111
IndexValueType GetElement(unsigned long element) const
Definition: itkIndex.h:283
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:72
::itk::OffsetValueType OffsetValueType
Definition: itkIndex.h:93
void SetIndex(const IndexValueType val[VIndexDimension])
Definition: itkIndex.h:261
IndexValueType operator[](unsigned int dim) const
Definition: itkIndex.h:250