ITK  4.2.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 >
30 class OffsetLexicographicCompare;
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  { memcpy(m_Offset, val, sizeof( OffsetValueType ) * VOffsetDimension); }
190 
194  static Self GetBasisOffset(unsigned int dim);
195 
198  void Fill(OffsetValueType value)
199  { for ( unsigned int i = 0; i < VOffsetDimension; ++i ) { m_Offset[i] = value; } }
200 
206  OffsetValueType m_Offset[VOffsetDimension];
207 
208 // force gccxml to find the constructors found before the internal upgrade to
209 // gcc 4.2
210 #if defined( CABLE_CONFIGURATION )
211  Offset(); //purposely not implemented
212  Offset(const Self &); //purposely not implemented
213  void operator=(const Self &); //purposely not implemented
214 
215 #endif
216 };
217 
218 namespace Functor
219 {
228 template< unsigned int VOffsetDimension >
230 {
231 public:
233  Offset< VOffsetDimension > const & r) const
234  {
235  for ( unsigned int i = 0; i < VOffsetDimension; ++i )
236  {
237  if ( l.m_Offset[i] < r.m_Offset[i] )
238  {
239  return true;
240  }
241  else if ( l.m_Offset[i] > r.m_Offset[i] )
242  {
243  return false;
244  }
245  }
246  return false;
247  }
248 };
249 }
250 
251 template< unsigned int VOffsetDimension >
252 Offset< VOffsetDimension >
254 ::GetBasisOffset(unsigned int dim)
255 {
256  Self ind;
257 
258  memset(ind.m_Offset, 0, sizeof( OffsetValueType ) * VOffsetDimension);
259  ind.m_Offset[dim] = 1;
260  return ind;
261 }
262 
263 template< unsigned int VOffsetDimension >
264 std::ostream & operator<<(std::ostream & os, const Offset< VOffsetDimension > & ind)
265 {
266  os << "[";
267  unsigned int dimlim = VOffsetDimension - 1;
268  for ( unsigned int i = 0; i < dimlim; ++i )
269  {
270  os << ind[i] << ", ";
271  }
272  if ( VOffsetDimension >= 1 )
273  {
274  os << ind[VOffsetDimension - 1];
275  }
276  os << "]";
277  return os;
278 }
279 } // end namespace itk
280 
281 #endif
282