ITK  4.4.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 // force gccxml to find the constructors found before the internal upgrade to
213 // gcc 4.2
214 #if defined( CABLE_CONFIGURATION )
215  Offset(); //purposely not implemented
216  Offset(const Self &); //purposely not implemented
217  void operator=(const Self &); //purposely not implemented
218 
219 #endif
220 };
221 
222 namespace Functor
223 {
232 template< unsigned int VOffsetDimension >
233 class OffsetLexicographicCompare
234 {
235 public:
237  Offset< VOffsetDimension > const & r) const
238  {
239  for ( unsigned int i = 0; i < VOffsetDimension; ++i )
240  {
241  if ( l.m_Offset[i] < r.m_Offset[i] )
242  {
243  return true;
244  }
245  else if ( l.m_Offset[i] > r.m_Offset[i] )
246  {
247  return false;
248  }
249  }
250  return false;
251  }
252 };
253 }
254 
255 template< unsigned int VOffsetDimension >
256 Offset< VOffsetDimension >
258 ::GetBasisOffset(unsigned int dim)
259 {
260  Self ind;
261 
262  memset(ind.m_Offset, 0, sizeof( OffsetValueType ) * VOffsetDimension);
263  ind.m_Offset[dim] = 1;
264  return ind;
265 }
266 
267 template< unsigned int VOffsetDimension >
268 std::ostream & operator<<(std::ostream & os, const Offset< VOffsetDimension > & ind)
269 {
270  os << "[";
271  unsigned int dimlim = VOffsetDimension - 1;
272  for ( unsigned int i = 0; i < dimlim; ++i )
273  {
274  os << ind[i] << ", ";
275  }
276  if ( VOffsetDimension >= 1 )
277  {
278  os << ind[VOffsetDimension - 1];
279  }
280  os << "]";
281  return os;
282 }
283 } // end namespace itk
284 
285 #endif
286