ITK  4.2.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 
28 namespace itk
29 {
30 namespace Functor
31 {
32 template< unsigned int VIndexDimension >
34 }
35 
71 template< unsigned int VIndexDimension = 2 >
72 class 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 
230  IndexValueType & operator[](unsigned int dim)
231  { return m_Index[dim]; }
232 
236  IndexValueType operator[](unsigned int dim) const
237  { return m_Index[dim]; }
238 
241  const IndexValueType * GetIndex() const { return m_Index; }
242 
247  void SetIndex(const IndexValueType val[VIndexDimension])
248  { memcpy(m_Index, val, sizeof( IndexValueType ) * VIndexDimension); }
249 
256  void SetElement(unsigned long element, IndexValueType val)
257  { m_Index[element] = val; }
258 
265  IndexValueType GetElement(unsigned long element) const
266  { return m_Index[element]; }
267 
271  static Self GetBasisIndex(unsigned int dim);
272 
275  void Fill(IndexValueType value)
276  { for ( unsigned int i = 0; i < VIndexDimension; ++i ) { m_Index[i] = value; } }
277 
283  IndexValueType m_Index[VIndexDimension];
284 
286  template< class TCoordRep >
288  {
289  itkForLoopRoundingAndAssignmentMacro(IndexType,
290  ContinuousIndexType,
292  m_Index,
293  point,
294  VIndexDimension);
295  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
296  * Leaving here for documentation purposes
297  * for ( unsigned int i = 0; i < VIndexDimension; ++i )
298  * {
299  * m_Index[i] = Math::Round< IndexValueType >(point[i]);
300  * }
301  */
302  }
304 
306  template< class TCoordRep >
308  {
309  for ( unsigned int i = 0; i < VIndexDimension; ++i )
310  {
311  m_Index[i] = static_cast< IndexValueType >( point[i] );
312  }
313  }
315 
316 // force gccxml to find the constructors found before the internal upgrade to
317 // gcc 4.2
318 #if defined( CABLE_CONFIGURATION )
319  Index(); //purposely not implemented
320  Index(const Self &); //purposely not implemented
321  void operator=(const Self &); //purposely not implemented
322 
323 #endif
324 };
325 
326 namespace Functor
327 {
336 template< unsigned int VIndexDimension >
338 {
339 public:
341  Index< VIndexDimension > const & r) const
342  {
343  for ( unsigned int i = 0; i < VIndexDimension; ++i )
344  {
345  if ( l.m_Index[i] < r.m_Index[i] )
346  {
347  return true;
348  }
349  else if ( l.m_Index[i] > r.m_Index[i] )
350  {
351  return false;
352  }
353  }
354  return false;
355  }
356 };
357 }
358 
359 template< unsigned int VIndexDimension >
360 Index< VIndexDimension >
362 ::GetBasisIndex(unsigned int dim)
363 {
364  Self ind;
365 
366  memset(ind.m_Index, 0, sizeof( IndexValueType ) * VIndexDimension);
367  ind.m_Index[dim] = 1;
368  return ind;
369 }
370 
371 template< unsigned int VIndexDimension >
372 std::ostream & operator<<(std::ostream & os, const Index< VIndexDimension > & ind)
373 {
374  os << "[";
375  for ( unsigned int i = 0; i + 1 < VIndexDimension; ++i )
376  {
377  os << ind[i] << ", ";
378  }
379  if ( VIndexDimension >= 1 )
380  {
381  os << ind[VIndexDimension - 1];
382  }
383  os << "]";
384  return os;
385 }
386 } // end namespace itk
387 
388 #endif
389