KWStyle - itkImageRegion.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImageRegion.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:39 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12   Portions of this code are covered under the VTK copyright.
13   See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
14
15      This software is distributed WITHOUT ANY WARRANTY; without even 
16      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
17 IND *****PURPOSE.  See the above copyright notices for more information.
18
19 =========================================================================*/
20 DEF #ifndef _itkImageRegion_txx
21 DEF #define _itkImageRegion_txx
22 #include "itkImageRegion.h"
23
24 namespace itk
25 {
26
27 /**
28  * Instantiate object.
29  */
30 template<unsigned int VImageDimension>
31 ImageRegion<VImageDimension>
32 ::ImageRegion()
33 {
34   m_Index.Fill(0);
35   m_Size.Fill(0);
36 }
37
38 /**
39  * Destructor for the ImageRegion class.
40  */
41 template<unsigned int VImageDimension>
42 ImageRegion<VImageDimension>
43 ::~ImageRegion()
44 {
45 }
46
47 template<unsigned int VImageDimension>
48 unsigned long 
49 ImageRegion<VImageDimension>
50 ::GetNumberOfPixels() const
51 {
52   unsigned long numPixels=1;
53
54   for (unsigned int i=0; i<VImageDimension; i++)
55     {
56     numPixels *= m_Size[i];
57     }
58   
59   return numPixels;
60 }
61
62 template<unsigned int VImageDimension>
63 void
64 ImageRegion<VImageDimension>
65 ::PrintSelf(std::ostream& os, Indent indent) const
66 {
67   Superclass::PrintSelf(os, indent);
68   
69   os << indent << "Dimension: " << this->GetImageDimension() << std::endl;
70   os << indent << "Index: " << this->GetIndex() << std::endl;
71   os << indent << "Size: " << this->GetSize() << std::endl;
72 }
73
74
75 template<unsigned int VImageDimension>
76 void
77 ImageRegion<VImageDimension>
78 ::PadByRadius(unsigned long radius)
79 {
80   unsigned long radiusVector[VImageDimension];
81
82   for (unsigned int i=0; i < VImageDimension; ++i)
83     {
84     radiusVector[i] = radius;
85     }
86
87   this->PadByRadius(radiusVector);
88 }
89
90 template<unsigned int VImageDimension>
91 void
92 ImageRegion<VImageDimension>
93 ::PadByRadius(const SizeType &radius)
94 {
95   this->PadByRadius( radius.GetSize() );
96 }
97
98 template<unsigned int VImageDimension>
99 void
100 ImageRegion<VImageDimension>
101 ::PadByRadius(const unsigned long radius[VImageDimension])
102 {
103   for (unsigned int i = 0; i < VImageDimension; i++)
104     {
105     m_Size[i] += 2 * radius[i];
106     m_Index[i] -= static_cast<long>(radius[i]);
107     }  
108 }
109
110 template<unsigned int VImageDimension>
111 bool
112 ImageRegion<VImageDimension>
113 ::Crop(const Self& region)
114 {
115   long crop;
116   unsigned int i;
117   bool cropPossible = true;
118
119   // Can we crop?
120   for (i = 0; i < VImageDimension && cropPossible; i++)
121     {
122     // Is left edge of current region to the right of the right edge
123     // of the region to crop with? (if so, we cannot crop)
124     if (m_Index[i] >= region.GetIndex()[i]
125 IND ********+ static_cast<long>(region.GetSize()[i]))
126       {
127       cropPossible = false;
128       }
129     // If right edge of the current region to the left of the left
130     // edge of the region to crop with? (if so, we cannot crop)
131     if (m_Index[i] + static_cast<long>(m_Size[i]) <= region.GetIndex()[i])
132       {
133       cropPossible = false;
134       }
135     }
136
137   // if we cannot crop, return without changing anythin
138   if (!cropPossible)
139     {
140     return cropPossible;
141     }
142
143   // we can crop, so crop
144   for (i=0; i < VImageDimension; i++)
145     {
146     // first check the start index
147     if (m_Index[i] < region.GetIndex()[i])
148       {
149       // how much do we need to adjust
150       crop = region.GetIndex()[i] - m_Index[i];
151
152       // adjust the start index and the size of the current region
153       m_Index[i] += crop;
154       m_Size[i] -= static_cast<unsigned long>(crop);
155       }
156     // now check the final size
157     if (m_Index[i] + static_cast<long>(m_Size[i])
158 IND ********> region.GetIndex()[i] + static_cast<long>(region.GetSize()[i]))
159       {
160       // how much do we need to adjust
161       crop = m_Index[i] + static_cast<long>(m_Size[i])
162 IND ********- region.GetIndex()[i] - static_cast<long>(region.GetSize()[i]);
163
164       // adjust the size
165       m_Size[i] -= static_cast<unsigned long>(crop);
166       }
167     }
168
169   return cropPossible;
170 }
171
172
173 template<unsigned int VImageDimension>
174 typename ImageRegion<VImageDimension>::SliceRegion
175 ImageRegion<VImageDimension>
176 ::Slice(const unsigned long dim) const
177 {
178   Index<SliceDimension> sliceIndex;
179   Size<SliceDimension> sliceSize;
180
181   unsigned int ii = 0;
182   for (unsigned int i=0; i < VImageDimension; i++)
183     {
184     if (i != dim)
185       {
186       sliceIndex[ii] = m_Index[i];
187       sliceSize[ii] = m_Size[i];
188       ++ii;
189       }
190     }
191
192   return ImageRegion<SliceDimension>(sliceIndex, sliceSize);
193 }
194
195
196 // end namespace itk
197 #endif
198

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:14PM
© Kitware Inc.