ITK  4.4.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 
229  IndexValueType & operator[](unsigned int dim)
230  { return m_Index[dim]; }
231 
235  IndexValueType operator[](unsigned int dim) const
236  { return m_Index[dim]; }
237 
240  const IndexValueType * GetIndex() const { return m_Index; }
241 
246  void SetIndex(const IndexValueType val[VIndexDimension])
247  {
248  std::copy(val,
249  val+VIndexDimension,
250  m_Index);
251  }
252 
259  void SetElement(unsigned long element, IndexValueType val)
260  { m_Index[element] = val; }
261 
268  IndexValueType GetElement(unsigned long element) const
269  { return m_Index[element]; }
270 
274  static Self GetBasisIndex(unsigned int dim);
275 
278  void Fill(IndexValueType value)
279  { for ( unsigned int i = 0; i < VIndexDimension; ++i ) { m_Index[i] = value; } }
280 
286  IndexValueType m_Index[VIndexDimension];
287 
289  template< class TCoordRep >
291  {
292  itkForLoopRoundingAndAssignmentMacro(IndexType,
293  ContinuousIndexType,
295  m_Index,
296  point,
297  VIndexDimension);
298  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
299  * Leaving here for documentation purposes
300  * for ( unsigned int i = 0; i < VIndexDimension; ++i )
301  * {
302  * m_Index[i] = Math::Round< IndexValueType >(point[i]);
303  * }
304  */
305  }
307 
309  template< class TCoordRep >
311  {
312  for ( unsigned int i = 0; i < VIndexDimension; ++i )
313  {
314  m_Index[i] = static_cast< IndexValueType >( point[i] );
315  }
316  }
318 
319 // force gccxml to find the constructors found before the internal upgrade to
320 // gcc 4.2
321 #if defined( CABLE_CONFIGURATION )
322  Index(); //purposely not implemented
323  Index(const Self &); //purposely not implemented
324  void operator=(const Self &); //purposely not implemented
325 
326 #endif
327 };
328 
329 namespace Functor
330 {
339 template< unsigned int VIndexDimension >
340 class IndexLexicographicCompare
341 {
342 public:
344  Index< VIndexDimension > const & r) const
345  {
346  for ( unsigned int i = 0; i < VIndexDimension; ++i )
347  {
348  if ( l.m_Index[i] < r.m_Index[i] )
349  {
350  return true;
351  }
352  else if ( l.m_Index[i] > r.m_Index[i] )
353  {
354  return false;
355  }
356  }
357  return false;
358  }
359 };
360 }
361 
362 template< unsigned int VIndexDimension >
363 Index< VIndexDimension >
365 ::GetBasisIndex(unsigned int dim)
366 {
367  Self ind;
368 
369  memset(ind.m_Index, 0, sizeof( IndexValueType ) * VIndexDimension);
370  ind.m_Index[dim] = 1;
371  return ind;
372 }
373 
374 template< unsigned int VIndexDimension >
375 std::ostream & operator<<(std::ostream & os, const Index< VIndexDimension > & ind)
376 {
377  os << "[";
378  for ( unsigned int i = 0; i + 1 < VIndexDimension; ++i )
379  {
380  os << ind[i] << ", ";
381  }
382  if ( VIndexDimension >= 1 )
383  {
384  os << ind[VIndexDimension - 1];
385  }
386  os << "]";
387  return os;
388 }
389 } // end namespace itk
390 
391 #endif
392