ITK  4.6.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 template< unsigned int VIndexDimension >
33 }
34 
70 template< unsigned int VIndexDimension = 2 >
71 class Index
72 {
73 public:
75  typedef Index Self;
76 
80 
82  itkStaticConstMacro(Dimension, unsigned int, VIndexDimension);
83 
85  static unsigned int GetIndexDimension() { return VIndexDimension; }
86 
89 
93 
96 
98  const Self
99  operator+(const SizeType & size) const
100  {
101  Self result;
102 
103  for ( unsigned int i = 0; i < VIndexDimension; i++ )
104  { result[i] = m_Index[i] + static_cast< IndexValueType >( size[i] ); }
105  return result;
106  }
107 
109  const Self &
110  operator+=(const SizeType & size)
111  {
112  for ( unsigned int i = 0; i < VIndexDimension; i++ )
113  { m_Index[i] += static_cast< IndexValueType >( size[i] ); }
114  return *this;
115  }
117 
120  const Self
121  operator-(const SizeType & size) const
122  {
123  Self result;
124 
125  for ( unsigned int i = 0; i < VIndexDimension; i++ )
126  { result[i] = m_Index[i] - static_cast< IndexValueType >( size[i] ); }
127  return result;
128  }
129 
131  const Self &
132  operator-=(const SizeType & size)
133  {
134  for ( unsigned int i = 0; i < VIndexDimension; i++ )
135  { m_Index[i] -= static_cast< IndexValueType >( size[i] ); }
136  return *this;
137  }
139 
141  const Self
142  operator+(const OffsetType & offset) const
143  {
144  Self result;
145 
146  for ( unsigned int i = 0; i < VIndexDimension; i++ )
147  { result[i] = m_Index[i] + offset[i]; }
148  return result;
149  }
150 
152  const Self &
153  operator+=(const OffsetType & offset)
154  {
155  for ( unsigned int i = 0; i < VIndexDimension; i++ )
156  { m_Index[i] += offset[i]; }
157  return *this;
158  }
160 
162  const Self &
163  operator-=(const OffsetType & offset)
164  {
165  for ( unsigned int i = 0; i < VIndexDimension; i++ )
166  { m_Index[i] -= offset[i]; }
167  return *this;
168  }
170 
172  const Self
173  operator-(const OffsetType & off) const
174  {
175  Self result;
176 
177  for ( unsigned int i = 0; i < VIndexDimension; i++ )
178  { result[i] = m_Index[i] - off.m_Offset[i]; }
179  return result;
180  }
181 
183  const OffsetType
184  operator-(const Self & vec) const
185  {
186  OffsetType result;
187 
188  for ( unsigned int i = 0; i < VIndexDimension; i++ )
189  { result[i] = m_Index[i] - vec.m_Index[i]; }
190  return result;
191  }
192 
195  const Self
196  operator*(const SizeType & vec) const
197  {
198  Self result;
199 
200  for ( unsigned int i = 0; i < VIndexDimension; i++ )
201  { result[i] = m_Index[i] * static_cast< IndexValueType >( vec.m_Size[i] ); }
202  return result;
203  }
204 
206  bool
207  operator==(const Self & vec) const
208  {
209  bool same = true;
210 
211  for ( unsigned int i = 0; i < VIndexDimension && same; i++ )
212  { same = ( m_Index[i] == vec.m_Index[i] ); }
213  return same;
214  }
215 
217  bool
218  operator!=(const Self & vec) const
219  {
220  bool same = true;
221 
222  for ( unsigned int i = 0; i < VIndexDimension && same; i++ )
223  { same = ( m_Index[i] == vec.m_Index[i] ); }
224  return !same;
225  }
226 
227 // false positive warnings with GCC 4.9
228 #if defined( __GNUC__ )
229 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 )
230 #pragma GCC diagnostic push
231 #pragma GCC diagnostic ignored "-Warray-bounds"
232 #endif
233 #endif
234 
236  IndexValueType & operator[](unsigned int dim)
237  {
238  return m_Index[dim];
239  }
240 #if defined( __GNUC__ )
241 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 )
242 #pragma GCC diagnostic pop
243 #endif
244 #endif
245 
249  IndexValueType operator[](unsigned int dim) const
250  { return m_Index[dim]; }
251 
254  const IndexValueType * GetIndex() const { return m_Index; }
255 
260  void SetIndex(const IndexValueType val[VIndexDimension])
261  {
262  std::copy(val,
263  val+VIndexDimension,
264  m_Index);
265  }
266 
273  void SetElement(unsigned long element, IndexValueType val)
274  { m_Index[element] = val; }
275 
282  IndexValueType GetElement(unsigned long element) const
283  { return m_Index[element]; }
284 
288  static Self GetBasisIndex(unsigned int dim);
289 
292  void Fill(IndexValueType value)
293  { for ( unsigned int i = 0; i < VIndexDimension; ++i ) { m_Index[i] = value; } }
294 
300  IndexValueType m_Index[VIndexDimension];
301 
303  template< typename TCoordRep >
305  {
306  itkForLoopRoundingAndAssignmentMacro(IndexType,
307  ContinuousIndexType,
309  m_Index,
310  point,
311  VIndexDimension);
312  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
313  * Leaving here for documentation purposes
314  * for ( unsigned int i = 0; i < VIndexDimension; ++i )
315  * {
316  * m_Index[i] = Math::Round< IndexValueType >(point[i]);
317  * }
318  */
319  }
321 
323  template< typename TCoordRep >
325  {
326  for ( unsigned int i = 0; i < VIndexDimension; ++i )
327  {
328  m_Index[i] = static_cast< IndexValueType >( point[i] );
329  }
330  }
332 
333 // force gccxml to find the constructors found before the internal upgrade to
334 // gcc 4.2
335 #if defined( CABLE_CONFIGURATION )
336  Index(); //purposely not implemented
337  Index(const Self &); //purposely not implemented
338  void operator=(const Self &); //purposely not implemented
339 
340 #endif
341 };
342 
343 namespace Functor
344 {
353 template< unsigned int VIndexDimension >
354 class IndexLexicographicCompare
355 {
356 public:
358  Index< VIndexDimension > const & r) const
359  {
360  for ( unsigned int i = 0; i < VIndexDimension; ++i )
361  {
362  if ( l.m_Index[i] < r.m_Index[i] )
363  {
364  return true;
365  }
366  else if ( l.m_Index[i] > r.m_Index[i] )
367  {
368  return false;
369  }
370  }
371  return false;
372  }
373 };
374 }
375 
376 template< unsigned int VIndexDimension >
377 Index< VIndexDimension >
379 ::GetBasisIndex(unsigned int dim)
380 {
381  Self ind;
382 
383  memset(ind.m_Index, 0, sizeof( IndexValueType ) * VIndexDimension);
384  ind.m_Index[dim] = 1;
385  return ind;
386 }
387 
388 template< unsigned int VIndexDimension >
389 std::ostream & operator<<(std::ostream & os, const Index< VIndexDimension > & ind)
390 {
391  os << "[";
392  for ( unsigned int i = 0; i + 1 < VIndexDimension; ++i )
393  {
394  os << ind[i] << ", ";
395  }
396  if ( VIndexDimension >= 1 )
397  {
398  os << ind[VIndexDimension - 1];
399  }
400  os << "]";
401  return os;
402 }
403 } // end namespace itk
404 
405 #endif
bool operator()(Index< VIndexDimension > const &l, Index< VIndexDimension > const &r) const
Definition: itkIndex.h:357
const IndexValueType * GetIndex() const
Definition: itkIndex.h:254
OffsetValueType m_Offset[VOffsetDimension]
Definition: itkOffset.h:210
const Self & operator-=(const OffsetType &offset)
Definition: itkIndex.h:163
const Self operator-(const OffsetType &off) const
Definition: itkIndex.h:173
IndexValueType & operator[](unsigned int dim)
Definition: itkIndex.h:236
Represent the offset between two n-dimensional indexes in a n-dimensional image.
Definition: itkOffset.h:55
bool operator!=(const Self &vec) const
Definition: itkIndex.h:218
IndexValueType m_Index[VIndexDimension]
Definition: itkIndex.h:300
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
Size< VIndexDimension > SizeType
Definition: itkIndex.h:88
signed long OffsetValueType
Definition: itkIntTypes.h:154
signed long IndexValueType
Definition: itkIntTypes.h:150
const OffsetType operator-(const Self &vec) const
Definition: itkIndex.h:184
Order Index instances lexicographically.
Definition: itkIndex.h:32
Index< VIndexDimension > IndexType
Definition: itkIndex.h:78
Offset< VIndexDimension > OffsetType
Definition: itkIndex.h:91
void CopyWithRound(const FixedArray< TCoordRep, VIndexDimension > &point)
Definition: itkIndex.h:304
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
static Self GetBasisIndex(unsigned int dim)
Definition: itkIndex.h:379
::itk::IndexValueType IndexValueType
Definition: itkIndex.h:79
const Self & operator+=(const OffsetType &offset)
Definition: itkIndex.h:153
bool operator==(const Self &vec) const
Definition: itkIndex.h:207
static unsigned int GetIndexDimension()
Definition: itkIndex.h:85
Index Self
Definition: itkIndex.h:75
void Fill(IndexValueType value)
Definition: itkIndex.h:292
const Self operator*(const SizeType &vec) const
Definition: itkIndex.h:196
SizeValueType m_Size[VDimension]
Definition: itkSize.h:211
const Self operator+(const SizeType &size) const
Definition: itkIndex.h:99
const Self operator+(const OffsetType &offset) const
Definition: itkIndex.h:142
void CopyWithCast(const FixedArray< TCoordRep, VIndexDimension > &point)
Definition: itkIndex.h:324
static const unsigned int Dimension
Definition: itkIndex.h:82
const Self operator-(const SizeType &size) const
Definition: itkIndex.h:121
const Self & operator-=(const SizeType &size)
Definition: itkIndex.h:132
Functor::IndexLexicographicCompare< VIndexDimension > LexicographicCompare
Definition: itkIndex.h:95
void SetElement(unsigned long element, IndexValueType val)
Definition: itkIndex.h:273
const Self & operator+=(const SizeType &size)
Definition: itkIndex.h:110
IndexValueType GetElement(unsigned long element) const
Definition: itkIndex.h:282
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:71
::itk::OffsetValueType OffsetValueType
Definition: itkIndex.h:92
void SetIndex(const IndexValueType val[VIndexDimension])
Definition: itkIndex.h:260
IndexValueType operator[](unsigned int dim) const
Definition: itkIndex.h:249