ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkSize.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 __itkSize_h
19 #define __itkSize_h
20 
21 #include "itkMacro.h"
22 #include "itkIntTypes.h"
23 #include <cstring>
24 
25 namespace itk
26 {
52 template< unsigned int VDimension = 2 >
53 class Size
54 {
55 public:
57  typedef Size Self;
58 
62 
64  itkStaticConstMacro(Dimension, unsigned int, VDimension);
65 
67  static unsigned int GetSizeDimension(void) { return VDimension; }
68 
70  const Self
71  operator+(const Self & vec) const
72  {
73  Self result;
74 
75  for ( unsigned int i = 0; i < VDimension; i++ )
76  { result[i] = m_Size[i] + vec.m_Size[i]; }
77  return result;
78  }
79 
81  const Self &
82  operator+=(const Self & vec)
83  {
84  for ( unsigned int i = 0; i < VDimension; i++ )
85  { m_Size[i] += vec.m_Size[i]; }
86  return *this;
87  }
89 
91  const Self
92  operator-(const Self & vec) const
93  {
94  Self result;
95 
96  for ( unsigned int i = 0; i < VDimension; i++ )
97  { result[i] = m_Size[i] - vec.m_Size[i]; }
98  return result;
99  }
100 
102  const Self &
103  operator-=(const Self & vec)
104  {
105  for ( unsigned int i = 0; i < VDimension; i++ )
106  { m_Size[i] -= vec.m_Size[i]; }
107  return *this;
108  }
110 
112  const Self
113  operator*(const Self & vec) const
114  {
115  Self result;
116 
117  for ( unsigned int i = 0; i < VDimension; i++ )
118  { result[i] = m_Size[i] * vec.m_Size[i]; }
119  return result;
120  }
121 
123  const Self &
124  operator*=(const Self & vec)
125  {
126  for ( unsigned int i = 0; i < VDimension; i++ )
127  { m_Size[i] *= vec.m_Size[i]; }
128  return *this;
129  }
131 
133  bool
134  operator==(const Self & vec) const
135  {
136  bool same = 1;
137 
138  for ( unsigned int i = 0; i < VDimension && same; i++ )
139  { same = ( m_Size[i] == vec.m_Size[i] ); }
140  return same;
141  }
142 
144  bool
145  operator!=(const Self & vec) const
146  {
147  bool same = 1;
148 
149  for ( unsigned int i = 0; i < VDimension && same; i++ )
150  { same = ( m_Size[i] == vec.m_Size[i] ); }
151  return !same;
152  }
153 
156  SizeValueType & operator[](unsigned int dim)
157  { return m_Size[dim]; }
158 
162  SizeValueType operator[](unsigned int dim) const
163  { return m_Size[dim]; }
164 
167  const SizeValueType * GetSize() const { return m_Size; }
168 
172  void SetSize(const SizeValueType val[VDimension])
173  { memcpy(m_Size, val, sizeof( SizeValueType ) * VDimension); }
174 
181  void SetElement(unsigned long element, SizeValueType val)
182  { m_Size[element] = val; }
183 
190  SizeValueType GetElement(unsigned long element) const
191  { return m_Size[element]; }
192 
195  void Fill(SizeValueType value)
196  { for ( unsigned int i = 0; i < VDimension; ++i ) { m_Size[i] = value; } }
197 
208  SizeValueType m_Size[VDimension];
210 
211 // force gccxml to find the constructors found before the internal upgrade to
212 // gcc 4.2
213 #if defined( CABLE_CONFIGURATION )
214  Size(); //purposely not implemented
215  Size(const Self &); //purposely not implemented
216  void operator=(const Self &); //purposely not implemented
217 
218 #endif
219 };
220 
221 template< unsigned int VDimension >
222 std::ostream & operator<<(std::ostream & os, const Size< VDimension > & size)
223 {
224  os << "[";
225  for ( unsigned int i = 0; i + 1 < VDimension; ++i )
226  {
227  os << size[i] << ", ";
228  }
229  if ( VDimension >= 1 )
230  {
231  os << size[VDimension - 1];
232  }
233  os << "]";
234  return os;
235 }
236 } // end namespace itk
237 
238 #endif
239