ITK  4.6.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 
24 namespace itk
25 {
51 template< unsigned int VDimension = 2 >
52 class Size
53 {
54 public:
56  typedef Size Self;
57 
61 
63  itkStaticConstMacro(Dimension, unsigned int, VDimension);
64 
66  static unsigned int GetSizeDimension(void) { return VDimension; }
67 
69  const Self
70  operator+(const Self & vec) const
71  {
72  Self result;
73 
74  for ( unsigned int i = 0; i < VDimension; i++ )
75  { result[i] = m_Size[i] + vec.m_Size[i]; }
76  return result;
77  }
78 
80  const Self &
81  operator+=(const Self & vec)
82  {
83  for ( unsigned int i = 0; i < VDimension; i++ )
84  { m_Size[i] += vec.m_Size[i]; }
85  return *this;
86  }
88 
90  const Self
91  operator-(const Self & vec) const
92  {
93  Self result;
94 
95  for ( unsigned int i = 0; i < VDimension; i++ )
96  { result[i] = m_Size[i] - vec.m_Size[i]; }
97  return result;
98  }
99 
101  const Self &
102  operator-=(const Self & vec)
103  {
104  for ( unsigned int i = 0; i < VDimension; i++ )
105  { m_Size[i] -= vec.m_Size[i]; }
106  return *this;
107  }
109 
111  const Self
112  operator*(const Self & vec) const
113  {
114  Self result;
115 
116  for ( unsigned int i = 0; i < VDimension; i++ )
117  { result[i] = m_Size[i] * vec.m_Size[i]; }
118  return result;
119  }
120 
122  const Self &
123  operator*=(const Self & vec)
124  {
125  for ( unsigned int i = 0; i < VDimension; i++ )
126  { m_Size[i] *= vec.m_Size[i]; }
127  return *this;
128  }
130 
132  bool
133  operator==(const Self & vec) const
134  {
135  bool same = 1;
136 
137  for ( unsigned int i = 0; i < VDimension && same; i++ )
138  { same = ( m_Size[i] == vec.m_Size[i] ); }
139  return same;
140  }
141 
143  bool
144  operator!=(const Self & vec) const
145  {
146  bool same = 1;
147 
148  for ( unsigned int i = 0; i < VDimension && same; i++ )
149  { same = ( m_Size[i] == vec.m_Size[i] ); }
150  return !same;
151  }
152 
155  SizeValueType & operator[](unsigned int dim)
156  { return m_Size[dim]; }
157 
161  SizeValueType operator[](unsigned int dim) const
162  { return m_Size[dim]; }
163 
166  const SizeValueType * GetSize() const { return m_Size; }
167 
171  void SetSize(const SizeValueType val[VDimension])
172  {
173  std::copy(val,
174  val+VDimension,
175  m_Size);
176  }
177 
184  void SetElement(unsigned long element, SizeValueType val)
185  { m_Size[element] = val; }
186 
193  SizeValueType GetElement(unsigned long element) const
194  { return m_Size[element]; }
195 
198  void Fill(SizeValueType value)
199  { for ( unsigned int i = 0; i < VDimension; ++i ) { m_Size[i] = value; } }
200 
211  SizeValueType m_Size[VDimension];
213 
214 // force gccxml to find the constructors found before the internal upgrade to
215 // gcc 4.2
216 #if defined( CABLE_CONFIGURATION )
217  Size(); //purposely not implemented
218  Size(const Self &); //purposely not implemented
219  void operator=(const Self &); //purposely not implemented
220 
221 #endif
222 };
223 
224 template< unsigned int VDimension >
225 std::ostream & operator<<(std::ostream & os, const Size< VDimension > & size)
226 {
227  os << "[";
228  for ( unsigned int i = 0; i + 1 < VDimension; ++i )
229  {
230  os << size[i] << ", ";
231  }
232  if ( VDimension >= 1 )
233  {
234  os << size[VDimension - 1];
235  }
236  os << "]";
237  return os;
238 }
239 } // end namespace itk
240 
241 #endif
const Self operator+(const Self &vec) const
Definition: itkSize.h:70
Size Self
Definition: itkSize.h:56
itk::SizeValueType SizeValueType
Definition: itkSize.h:60
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
Size< VDimension > SizeType
Definition: itkSize.h:59
const Self & operator+=(const Self &vec)
Definition: itkSize.h:81
SizeValueType & operator[](unsigned int dim)
Definition: itkSize.h:155
const Self & operator-=(const Self &vec)
Definition: itkSize.h:102
const Self & operator*=(const Self &vec)
Definition: itkSize.h:123
unsigned long SizeValueType
Definition: itkIntTypes.h:143
void SetSize(const SizeValueType val[VDimension])
Definition: itkSize.h:171
const Self operator-(const Self &vec) const
Definition: itkSize.h:91
bool operator==(const Self &vec) const
Definition: itkSize.h:133
bool operator!=(const Self &vec) const
Definition: itkSize.h:144
SizeValueType operator[](unsigned int dim) const
Definition: itkSize.h:161
SizeValueType m_Size[VDimension]
Definition: itkSize.h:211
SizeValueType GetElement(unsigned long element) const
Definition: itkSize.h:193
void SetElement(unsigned long element, SizeValueType val)
Definition: itkSize.h:184
const Self operator*(const Self &vec) const
Definition: itkSize.h:112
void Fill(SizeValueType value)
Definition: itkSize.h:198
const SizeValueType * GetSize() const
Definition: itkSize.h:166
static const unsigned int Dimension
Definition: itkSize.h:63
static unsigned int GetSizeDimension(void)
Definition: itkSize.h:66