ITK  4.11.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 template< unsigned int VOffsetDimension >
31 }
32 
54 template< unsigned int VOffsetDimension = 2 >
55 class Offset
56 {
57 public:
59  typedef Offset Self;
60 
62  itkStaticConstMacro(Dimension, unsigned int, VOffsetDimension);
63 
65  static unsigned int GetOffsetDimension() { return VOffsetDimension; }
66 
70 
73 
75  const Self
76  operator+(const Self & offset) const
77  {
78  Self result;
79 
80  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
81  { result[i] = m_Offset[i] + offset[i]; }
82  return result;
83  }
84 
86  const Self
87  operator+(const Size< VOffsetDimension > & size) const
88  {
89  Self result;
90 
91  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
92  { result[i] = m_Offset[i] + size[i]; }
93  return result;
94  }
95 
97  const Self &
99  {
100  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
101  { m_Offset[i] += size[i]; }
102  return *this;
103  }
105 
107  const Self &
109  {
110  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
111  { m_Offset[i] -= size[i]; }
112  return *this;
113  }
115 
117  const Self
118  operator-(const Self & vec)
119  {
120  Self result;
121 
122  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
123  { result[i] = m_Offset[i] - vec.m_Offset[i]; }
124  return result;
125  }
126 
128  const Self &
129  operator+=(const Self & vec)
130  {
131  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
132  { m_Offset[i] += vec.m_Offset[i]; }
133  return *this;
134  }
136 
138  const Self &
139  operator-=(const Self & vec)
140  {
141  for ( unsigned int i = 0; i < VOffsetDimension; i++ )
142  { m_Offset[i] -= vec.m_Offset[i]; }
143  return *this;
144  }
146 
148  bool
149  operator==(const Self & vec) const
150  {
151  bool same = 1;
152 
153  for ( unsigned int i = 0; i < VOffsetDimension && same; i++ )
154  { same = ( m_Offset[i] == vec.m_Offset[i] ); }
155  return same;
156  }
157 
159  bool
160  operator!=(const Self & vec) const
161  {
162  bool same = 1;
163 
164  for ( unsigned int i = 0; i < VOffsetDimension && same; i++ )
165  { same = ( m_Offset[i] == vec.m_Offset[i] ); }
166  return !same;
167  }
168 
171  OffsetValueType & operator[](unsigned int dim)
172  { return m_Offset[dim]; }
173 
177  OffsetValueType operator[](unsigned int dim) const
178  { return m_Offset[dim]; }
179 
182  const OffsetValueType * GetOffset() const { return m_Offset; }
183 
188  void SetOffset(const OffsetValueType val[VOffsetDimension])
189  {
190  std::copy(val,
191  val+VOffsetDimension,
192  m_Offset);
193  }
194 
198  static Self GetBasisOffset(unsigned int dim);
199 
202  void Fill(OffsetValueType value)
203  { for ( unsigned int i = 0; i < VOffsetDimension; ++i ) { m_Offset[i] = value; } }
204 
210  OffsetValueType m_Offset[VOffsetDimension];
211 
212 #if defined( ITK_WRAPPING_PARSER )
213  // Do not use c++11 'delete' keyword here. This code block is here to
214  // explicitly provide the wrapping facilities with handles to the default and
215  // copy constructors, and the assignment operator that are otherwise declared
216  // implicitly.
217  Offset();
218  Offset(const Self&);
219  void operator=(const Self&);
220 #endif
221 };
222 
223 namespace Functor
224 {
233 template< unsigned int VOffsetDimension >
234 class OffsetLexicographicCompare
235 {
236 public:
238  Offset< VOffsetDimension > const & r) const
239  {
240  for ( unsigned int i = 0; i < VOffsetDimension; ++i )
241  {
242  if ( l.m_Offset[i] < r.m_Offset[i] )
243  {
244  return true;
245  }
246  else if ( l.m_Offset[i] > r.m_Offset[i] )
247  {
248  return false;
249  }
250  }
251  return false;
252  }
253 };
254 }
255 
256 template< unsigned int VOffsetDimension >
257 Offset< VOffsetDimension >
259 ::GetBasisOffset(unsigned int dim)
260 {
261  Self ind;
262 
263  memset(ind.m_Offset, 0, sizeof( OffsetValueType ) * VOffsetDimension);
264  ind.m_Offset[dim] = 1;
265  return ind;
266 }
267 
268 template< unsigned int VOffsetDimension >
269 std::ostream & operator<<(std::ostream & os, const Offset< VOffsetDimension > & ind)
270 {
271  os << "[";
272  unsigned int dimlim = VOffsetDimension - 1;
273  for ( unsigned int i = 0; i < dimlim; ++i )
274  {
275  os << ind[i] << ", ";
276  }
277  if ( VOffsetDimension >= 1 )
278  {
279  os << ind[VOffsetDimension - 1];
280  }
281  os << "]";
282  return os;
283 }
284 } // end namespace itk
285 
286 #endif
OffsetValueType m_Offset[VOffsetDimension]
Definition: itkOffset.h:210
Represent the offset between two n-dimensional indexes in a n-dimensional image.
Definition: itkOffset.h:55
const Self & operator-=(const Self &vec)
Definition: itkOffset.h:139
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:87
const OffsetValueType * GetOffset() const
Definition: itkOffset.h:182
Order Offset instances lexicographically.
Definition: itkOffset.h:30
const Self & operator+=(const Self &vec)
Definition: itkOffset.h:129
bool operator==(const Self &vec) const
Definition: itkOffset.h:149
OffsetValueType operator[](unsigned int dim) const
Definition: itkOffset.h:177
bool operator()(Offset< VOffsetDimension > const &l, Offset< VOffsetDimension > const &r) const
Definition: itkOffset.h:237
static const unsigned int Dimension
Definition: itkOffset.h:62
void SetOffset(const OffsetValueType val[VOffsetDimension])
Definition: itkOffset.h:188
void Fill(OffsetValueType value)
Definition: itkOffset.h:202
Offset Self
Definition: itkOffset.h:59
static unsigned int GetOffsetDimension()
Definition: itkOffset.h:65
bool operator!=(const Self &vec) const
Definition: itkOffset.h:160
const Self operator-(const Self &vec)
Definition: itkOffset.h:118
Functor::OffsetLexicographicCompare< VOffsetDimension > LexicographicCompare
Definition: itkOffset.h:72
OffsetValueType & operator[](unsigned int dim)
Definition: itkOffset.h:171
const Self & operator-=(const Size< VOffsetDimension > &size)
Definition: itkOffset.h:108
itk::OffsetValueType OffsetValueType
Definition: itkOffset.h:69
Offset< VOffsetDimension > OffsetType
Definition: itkOffset.h:68
const Self & operator+=(const Size< VOffsetDimension > &size)
Definition: itkOffset.h:98
static Self GetBasisOffset(unsigned int dim)
Definition: itkOffset.h:259
const Self operator+(const Self &offset) const
Definition: itkOffset.h:76