Orfeo Toolbox  4.0
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  }
116 
119  const Self
120  operator-(const SizeType & size) const
121  {
122  Self result;
123 
124  for ( unsigned int i = 0; i < VIndexDimension; i++ )
125  { result[i] = m_Index[i] - static_cast< IndexValueType >( size[i] ); }
126  return result;
127  }
128 
130  const Self &
131  operator-=(const SizeType & size)
132  {
133  for ( unsigned int i = 0; i < VIndexDimension; i++ )
134  { m_Index[i] -= static_cast< IndexValueType >( size[i] ); }
135  return *this;
136  }
137 
139  const Self
140  operator+(const OffsetType & offset) const
141  {
142  Self result;
143 
144  for ( unsigned int i = 0; i < VIndexDimension; i++ )
145  { result[i] = m_Index[i] + offset[i]; }
146  return result;
147  }
148 
150  const Self &
151  operator+=(const OffsetType & offset)
152  {
153  for ( unsigned int i = 0; i < VIndexDimension; i++ )
154  { m_Index[i] += offset[i]; }
155  return *this;
156  }
157 
159  const Self &
160  operator-=(const OffsetType & offset)
161  {
162  for ( unsigned int i = 0; i < VIndexDimension; i++ )
163  { m_Index[i] -= offset[i]; }
164  return *this;
165  }
166 
168  const Self
169  operator-(const OffsetType & off) const
170  {
171  Self result;
172 
173  for ( unsigned int i = 0; i < VIndexDimension; i++ )
174  { result[i] = m_Index[i] - off.m_Offset[i]; }
175  return result;
176  }
177 
179  const OffsetType
180  operator-(const Self & vec) const
181  {
182  OffsetType result;
183 
184  for ( unsigned int i = 0; i < VIndexDimension; i++ )
185  { result[i] = m_Index[i] - vec.m_Index[i]; }
186  return result;
187  }
188 
191  const Self
192  operator*(const SizeType & vec) const
193  {
194  Self result;
195 
196  for ( unsigned int i = 0; i < VIndexDimension; i++ )
197  { result[i] = m_Index[i] * static_cast< IndexValueType >( vec.m_Size[i] ); }
198  return result;
199  }
200 
202  bool
203  operator==(const Self & vec) const
204  {
205  bool same = true;
206 
207  for ( unsigned int i = 0; i < VIndexDimension && same; i++ )
208  { same = ( m_Index[i] == vec.m_Index[i] ); }
209  return same;
210  }
211 
213  bool
214  operator!=(const Self & vec) const
215  {
216  bool same = true;
217 
218  for ( unsigned int i = 0; i < VIndexDimension && same; i++ )
219  { same = ( m_Index[i] == vec.m_Index[i] ); }
220  return !same;
221  }
222 
225  IndexValueType & operator[](unsigned int dim)
226  { return m_Index[dim]; }
227 
231  IndexValueType operator[](unsigned int dim) const
232  { return m_Index[dim]; }
233 
236  const IndexValueType * GetIndex() const { return m_Index; }
237 
242  void SetIndex(const IndexValueType val[VIndexDimension])
243  {
244  std::copy(val,
245  val+VIndexDimension,
246  m_Index);
247  }
248 
255  void SetElement(unsigned long element, IndexValueType val)
256  { m_Index[element] = val; }
257 
264  IndexValueType GetElement(unsigned long element) const
265  { return m_Index[element]; }
266 
270  static Self GetBasisIndex(unsigned int dim);
271 
274  void Fill(IndexValueType value)
275  { for ( unsigned int i = 0; i < VIndexDimension; ++i ) { m_Index[i] = value; } }
276 
282  IndexValueType m_Index[VIndexDimension];
283 
285  template< typename TCoordRep >
287  {
288  itkForLoopRoundingAndAssignmentMacro(IndexType,
289  ContinuousIndexType,
291  m_Index,
292  point,
293  VIndexDimension);
294  /* NON TEMPLATE_META_PROGRAMMING_LOOP_UNROLLING data version
295  * Leaving here for documentation purposes
296  * for ( unsigned int i = 0; i < VIndexDimension; ++i )
297  * {
298  * m_Index[i] = Math::Round< IndexValueType >(point[i]);
299  * }
300  */
301  }
302 
304  template< typename TCoordRep >
306  {
307  for ( unsigned int i = 0; i < VIndexDimension; ++i )
308  {
309  m_Index[i] = static_cast< IndexValueType >( point[i] );
310  }
311  }
312 
313 // force gccxml to find the constructors found before the internal upgrade to
314 // gcc 4.2
315 #if defined( CABLE_CONFIGURATION )
316  Index(); //purposely not implemented
317  Index(const Self &); //purposely not implemented
318  void operator=(const Self &); //purposely not implemented
319 
320 #endif
321 };
322 
323 namespace Functor
324 {
333 template< unsigned int VIndexDimension >
334 class IndexLexicographicCompare
335 {
336 public:
338  Index< VIndexDimension > const & r) const
339  {
340  for ( unsigned int i = 0; i < VIndexDimension; ++i )
341  {
342  if ( l.m_Index[i] < r.m_Index[i] )
343  {
344  return true;
345  }
346  else if ( l.m_Index[i] > r.m_Index[i] )
347  {
348  return false;
349  }
350  }
351  return false;
352  }
353 };
354 }
355 
356 template< unsigned int VIndexDimension >
357 Index< VIndexDimension >
359 ::GetBasisIndex(unsigned int dim)
360 {
361  Self ind;
362 
363  memset(ind.m_Index, 0, sizeof( IndexValueType ) * VIndexDimension);
364  ind.m_Index[dim] = 1;
365  return ind;
366 }
367 
368 template< unsigned int VIndexDimension >
369 std::ostream & operator<<(std::ostream & os, const Index< VIndexDimension > & ind)
370 {
371  os << "[";
372  for ( unsigned int i = 0; i + 1 < VIndexDimension; ++i )
373  {
374  os << ind[i] << ", ";
375  }
376  if ( VIndexDimension >= 1 )
377  {
378  os << ind[VIndexDimension - 1];
379  }
380  os << "]";
381  return os;
382 }
383 } // end namespace itk
384 
385 #endif

Generated at Sat Mar 8 2014 14:55:59 for Orfeo Toolbox with doxygen 1.8.3.1