ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkOffset.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 itkOffset_h
19 #define itkOffset_h
20 
21 #include "itkSize.h"
22 
23 #include <memory>
24 
25 namespace itk
26 {
27 namespace Functor
28 {
29 // Forward reference because of circular dependencies
30 template< unsigned int VOffsetDimension >
31 class ITK_TEMPLATE_EXPORT OffsetLexicographicCompare;
32 }
33 
55 template< unsigned int VOffsetDimension = 2 >
56 class ITK_TEMPLATE_EXPORT Offset
57 {
58 public:
60  typedef Offset Self;
61 
63  itkStaticConstMacro(Dimension, unsigned int, VOffsetDimension);
64 
66  static unsigned int GetOffsetDimension() { return VOffsetDimension; }
67 
71 
74 
76  const Self
77  operator+(const Self & offset) const
78  {
79  Self result;
80 
81  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
82  { result[i] = m_Offset[i] + offset[i]; }
83  return result;
84  }
85 
87  const Self
88  operator+(const Size< VOffsetDimension > & size) const
89  {
90  Self result;
91 
92  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
93  { result[i] = m_Offset[i] + size[i]; }
94  return result;
95  }
96 
98  const Self &
100  {
101  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
102  { m_Offset[i] += size[i]; }
103  return *this;
104  }
106 
108  const Self &
110  {
111  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
112  { m_Offset[i] -= size[i]; }
113  return *this;
114  }
116 
118  const Self
119  operator-(const Self & vec)
120  {
121  Self result;
122 
123  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
124  { result[i] = m_Offset[i] - vec.m_Offset[i]; }
125  return result;
126  }
127 
129  const Self &
130  operator+=(const Self & vec)
131  {
132  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
133  { m_Offset[i] += vec.m_Offset[i]; }
134  return *this;
135  }
137 
139  const Self &
140  operator-=(const Self & vec)
141  {
142  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
143  { m_Offset[i] -= vec.m_Offset[i]; }
144  return *this;
145  }
147 
149  bool
150  operator==(const Self & vec) const
151  {
152  bool same = 1;
153 
154  for ( unsigned int i = 0; i < VOffsetDimension && same; i++ )
155  { same = ( m_Offset[i] == vec.m_Offset[i] ); }
156  return same;
157  }
158 
160  bool
161  operator!=(const Self & vec) const
162  {
163  bool same = 1;
164 
165  for ( unsigned int i = 0; i < VOffsetDimension && same; i++ )
166  { same = ( m_Offset[i] == vec.m_Offset[i] ); }
167  return !same;
168  }
169 
172  OffsetValueType & operator[](unsigned int dim)
173  { return m_Offset[dim]; }
174 
178  OffsetValueType operator[](unsigned int dim) const
179  { return m_Offset[dim]; }
180 
183  const OffsetValueType * GetOffset() const { return m_Offset; }
184 
189  void SetOffset(const OffsetValueType val[VOffsetDimension])
190  {
191  std::copy(val,
192  val+VOffsetDimension,
193  m_Offset);
194  }
195 
199  static Self GetBasisOffset(unsigned int dim);
200 
203  void Fill(OffsetValueType value)
204  { for ( unsigned int i = 0; i < VOffsetDimension; ++i ) { m_Offset[i] = value; } }
205 
211  OffsetValueType m_Offset[VOffsetDimension];
212 
213 #if defined( ITK_WRAPPING_PARSER )
214  // Do not use c++11 'delete' keyword here. This code block is here to
215  // explicitly provide the wrapping facilities with handles to the default and
216  // copy constructors, and the assignment operator that are otherwise declared
217  // implicitly.
218  Offset();
219  Offset(const Self&);
220  void operator=(const Self&);
221 #endif
222 };
223 
224 namespace Functor
225 {
234 template< unsigned int VOffsetDimension >
235 class ITK_TEMPLATE_EXPORT OffsetLexicographicCompare
236 {
237 public:
239  Offset< VOffsetDimension > const & r) const
240  {
241  for ( unsigned int i = 0; i < VOffsetDimension; ++i )
242  {
243  if ( l.m_Offset[i] < r.m_Offset[i] )
244  {
245  return true;
246  }
247  else if ( l.m_Offset[i] > r.m_Offset[i] )
248  {
249  return false;
250  }
251  }
252  return false;
253  }
254 };
255 }
256 
257 template< unsigned int VOffsetDimension >
258 Offset< VOffsetDimension >
260 ::GetBasisOffset(unsigned int dim)
261 {
262  Self ind;
263 
264  memset(ind.m_Offset, 0, sizeof( OffsetValueType ) * VOffsetDimension);
265  ind.m_Offset[dim] = 1;
266  return ind;
267 }
268 
269 template< unsigned int VOffsetDimension >
270 std::ostream & operator<<(std::ostream & os, const Offset< VOffsetDimension > & ind)
271 {
272  os << "[";
273  unsigned int dimlim = VOffsetDimension - 1;
274  for ( unsigned int i = 0; i < dimlim; ++i )
275  {
276  os << ind[i] << ", ";
277  }
278  if ( VOffsetDimension >= 1 )
279  {
280  os << ind[VOffsetDimension - 1];
281  }
282  os << "]";
283  return os;
284 }
285 } // end namespace itk
286 
287 #endif
OffsetValueType m_Offset[VOffsetDimension]
Definition: itkOffset.h:211
Represent the offset between two n-dimensional indexes in a n-dimensional image.
Definition: itkOffset.h:56
const Self & operator-=(const Self &vec)
Definition: itkOffset.h:140
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
signed long OffsetValueType
Definition: itkIntTypes.h:154
const Self operator+(const Size< VOffsetDimension > &size) const
Definition: itkOffset.h:88
const OffsetValueType * GetOffset() const
Definition: itkOffset.h:183
Order Offset instances lexicographically.
Definition: itkOffset.h:235
const Self & operator+=(const Self &vec)
Definition: itkOffset.h:130
bool operator==(const Self &vec) const
Definition: itkOffset.h:150
OffsetValueType operator[](unsigned int dim) const
Definition: itkOffset.h:178
bool operator()(Offset< VOffsetDimension > const &l, Offset< VOffsetDimension > const &r) const
Definition: itkOffset.h:238
void SetOffset(const OffsetValueType val[VOffsetDimension])
Definition: itkOffset.h:189
void Fill(OffsetValueType value)
Definition: itkOffset.h:203
Offset Self
Definition: itkOffset.h:60
static unsigned int GetOffsetDimension()
Definition: itkOffset.h:66
bool operator!=(const Self &vec) const
Definition: itkOffset.h:161
const Self operator-(const Self &vec)
Definition: itkOffset.h:119
Functor::OffsetLexicographicCompare< VOffsetDimension > LexicographicCompare
Definition: itkOffset.h:73
OffsetValueType & operator[](unsigned int dim)
Definition: itkOffset.h:172
const Self & operator-=(const Size< VOffsetDimension > &size)
Definition: itkOffset.h:109
itk::OffsetValueType OffsetValueType
Definition: itkOffset.h:70
Offset< VOffsetDimension > OffsetType
Definition: itkOffset.h:69
const Self & operator+=(const Size< VOffsetDimension > &size)
Definition: itkOffset.h:99
static Self GetBasisOffset(unsigned int dim)
Definition: itkOffset.h:260
const Self operator+(const Self &offset) const
Definition: itkOffset.h:77