KWStyle - itkImageToImageFilterDetail.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImageToImageFilterDetail.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:40 $
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 #ifndef __itkImageToImageFilterDetail_h
21 #define __itkImageToImageFilterDetail_h
22
23 #include "itkImageRegion.h"
24 #include "itkSmartPointer.h"
25
26 namespace itk
27 {
28
29 /** ImageToImageFilterDetail namespace to house implementations of
30  * functions that are dependent on dimension. These functions are
31  * overloaded based on type unique for each different dimension.
32  * These functions cannot be member functions of a class like
33  * ImageToImageFilter since explicit instantiation of an
34  * ImageToImageFilter would force the instantiation of all versions of
35  * these overloaded functions.
36  */
37 namespace ImageToImageFilterDetail
38 {
39 IND **/** \struct  DispatchBase
40 LEN,IND **** \brief Base class for a class used to dispatch to dimension specific implementations.
41 IND ****
42 IND **** DispatchBase is base class used as the default case when implementations
43 IND **** are dispatched to overloaded routines based on dimension.
44 IND ****
45 IND **** \sa Dispatch
46 IND ****/
47 IND,IND,IND **struct DispatchBase {};
48
49 IND **/** \struct BooleanDispatch
50 IND **** \brief Templated class to produce a unique type "true" and "false".
51 IND ****
52 IND **** BooleanDispatch is a templated class that produce a unique type for
53 IND **** for "true" and for "false".  These types may be used to decide which
54 IND **** version of an overloaded function to call.
55 IND ****/
56 IND **template <bool>
57 IND,IND,IND **struct BooleanDispatch {};
58
59 IND **/** \struct IntDispatch
60 IND **** \brief Templated class to produce a unique type for each integer
61 IND ****
62 IND **** IntDispatch is a templated class that produces a unique
63 IND **** type for each integer.  IntDispatch is typically
64 IND **** used as a parameter to an overloaded function where a different
65 IND **** version of the routine would need to be called for each integer value.
66 IND ****/
67 IND **template <int>
68 IND,IND,IND **struct IntDispatch : public DispatchBase {};
69   
70 IND **/** \struct UnsignedIntDispatch
71 LEN,IND **** \brief Templated class to produce a unique type for each unsigned integer (usually a dimension).
72 IND ****
73 IND **** UnsignedIntDispatch is a templated class that produces a unique
74 IND **** type for each unsigned integer.  UnsignedIntDispatch is typically
75 IND **** used as a parameter to an overloaded function where each version
76 IND **** of the overloaded function is for a unique dimension.  For
77 IND **** instance, an algorithm may provide two implementations: one
78 IND **** optimized for two-dimensional images and another for any of other
79 IND **** data dimension.  For instance:
80 IND ****
81 IND ****     void Calculate(const DispatchBase&);           // General ND version
82 IND ****     void Calculate(const UnsignedIntDispatch<2>&); // 2D optimized version
83 IND ****/
84 IND **template <unsigned int>
85 IND,IND,IND **struct UnsignedIntDispatch : public DispatchBase {};
86
87 IND **/** \struct BinaryBooleanDispatch
88 IND **** \brief Templated class to produce a unique type for a pairing of booleans.
89 IND ****
90 IND **** BinaryBooleanDispatch is a templated class that produces a unique type
91 IND **** for each pairing of two boolean values ((true, true), (true, false),
92 IND **** (false, true), (false, false)).
93 IND ****/
94 IND **template <bool B1, bool B2>
95 IND **struct BinaryBooleanDispatch
96 IND **{
97 IND ****/** Typedefs to extract the unique types for the first and second
98 IND ********template parameters (true/false) */
99 IND ****typedef BooleanDispatch<B1> FirstType;
100 IND ****typedef BooleanDispatch<B2> SecondType;
101 IND **};
102
103 IND **/** \struct BinaryIntDispatch
104 IND **** \brief Templated class to produce a unique type for a pairing of integers.
105 IND ****
106 IND **** IntBooleanDispatch is a templated class that produces a unique type
107 IND **** for each pairing of two integer values.
108 IND ****/
109 IND **template <int D1, int D2>
110 IND **struct BinaryIntDispatch
111 IND **{
112 IND ****/** Typedefs to extract the unique types for the first and second
113 IND ********template parameters (unique type for the integer value) */
114 IND ****typedef IntDispatch<D1> FirstType;
115 IND ****typedef IntDispatch<D2> SecondType;
116 IND **};
117   
118 IND **/** \struct BinaryUnsignedIntDispatch
119 LEN,IND **** \brief Templated class to produce a unique type for a pairing of unsigned integers (usually two dimensions).
120 IND ****
121 IND **** BinaryUnsignedIntDispatch is a templated class that produces a
122 IND **** unique type for a pairing of unsigned integers.
123 IND **** BinaryUnsignedIntDispatch is typically used a parameter to an
124 IND **** overloaded function where each version of the overloaded function
125 IND **** is for a different pairing of unsigned integers.  This type may
126 IND **** be used to produce a unique type for an (input dimension, output
127    * dimension) pairing.
128 IND ****/
129 IND **template <unsigned int D1, unsigned int D2>
130 IND **struct BinaryUnsignedIntDispatch : public DispatchBase
131 IND **{
132 IND ****/** Typedefs to extract the unique types for the first and second
133 IND ********template parameters (unique type for the unsigned integer value) */
134 IND ****typedef UnsignedIntDispatch<D1> FirstType;
135 IND ****typedef UnsignedIntDispatch<D2> SecondType;
136     
137 IND ****/** Helper types to determine whether the two integers are the same,
138 IND ****** the first greater than the second, or the first less than the second.
139 IND ******
140 IND ****** ComparisonType will be either IntDispatch<0>, IntDispatch<1>, or
141 IND ****** IntDispatch<-1>. 
142 IND ******
143 IND ****** IntDispatch<0> means the two specified integers are the same.
144 IND ****** IntDispatch<1> means the first integer is greater than the second
145 IND ****** IntDispatch<-1> means the first integer is less than the second
146 IND ******
147 IND ****** The FirstEqualsSecondType, FirstGreaterThanSecondType,
148 IND ****** FirstLessThanSecondType typedefs are provided as convenience types
149 IND ****** which can be used to declare arguments to functions.  They themselves
150 IND ****** do not indicate the relationship between D1 and D2.
151 IND ******/
152 IND ****typedef IntDispatch<(D1 > D2) - (D1 < D2)> ComparisonType;
153 TDA,IND ****typedef IntDispatch<0>  FirstEqualsSecondType;  
154 TDA,IND ****typedef IntDispatch<1>  FirstGreaterThanSecondType;  
155 TDA,IND ****typedef IntDispatch<-1> FirstLessThanSecondType;  
156 IND **};
157
158 IND **/**
159 IND **** Copy an image region (start index and size) for the case where
160 IND **** the source and destination region are the same dimension.  This
161 IND **** is a trivial copy of a region. This is an overloaded function.
162 IND **** The other versions of this functions handle the case where the
163 IND **** destination dimension is greater than the source dimension and
164 IND **** the case where the destination dimension is less than the source
165 IND **** dimension.
166 IND ****
167 WCM,IND **** Note that the use of source and destination reflect where where
168 IND **** is information is coming from and going to.  When used in the
169 IND **** pipeline mechanism, the region requested by the output of a
170 IND **** filter is used to define the region required on the input.  In
171 IND **** this case the output of the filter is the source region and the
172 IND **** input of the filter is the destination region.
173 IND ****/
174 IND **template <unsigned int D1, unsigned int D2>
175 IND **void ImageToImageFilterDefaultCopyRegion(const typename
176                   BinaryUnsignedIntDispatch<D1, D2>::FirstEqualsSecondType &,
177                   ImageRegion<D1> &destRegion,
178                   const ImageRegion<D2> &srcRegion)
179 IND **{
180 IND ****destRegion = srcRegion;
181 IND **}
182
183 IND **/**
184 IND **** Copy an image region (start index and size) for the case where
185 IND **** the source region has a greater dimension than the destination
186 IND **** region.  This copies the first portion of the source into
187 IND **** destination. This is an overloaded function.  The other versions
188 IND **** of this functions handle the case where the source dimension is
189 IND **** less than the destination dimension and the case where the
190 IND **** destination and source are the same dimension.
191 IND ****
192 IND **** Note that the use of source and destination reflect where
193 IND **** where is information is coming from and going to.  When used
194 IND **** in the pipeline mechanism, the region requested by the output
195 IND **** of a filter is used to define the region required on the input.
196 IND **** In this case the output of the filter is the source and the
197 IND **** input of the filter is the destination.
198 IND ****/
199 IND **template <unsigned int D1, unsigned int D2>
200 IND **void ImageToImageFilterDefaultCopyRegion(const typename
201                   BinaryUnsignedIntDispatch<D1, D2>::FirstLessThanSecondType &,
202                   ImageRegion<D1> &destRegion,
203                   const ImageRegion<D2> &srcRegion)
204 IND **{
205 IND ****// Source dimension is greater than the destination dimension, copy the
206 IND ****// first part of the source into the destination
207 IND ****unsigned int dim;
208 IND ****Index<D1> destIndex;
209 IND ****Size<D1>  destSize;
210 IND ****const Index<D2> &srcIndex = srcRegion.GetIndex();
211 IND ****const Size<D2> &srcSize = srcRegion.GetSize();
212     
213 IND ****// copy what we can
214 IND ****for (dim=0; dim < D1; ++dim)
215       {
216       destIndex[dim] = srcIndex[dim];
217       destSize[dim] = srcSize[dim];
218       }
219     
220     destRegion.SetIndex(destIndex);
221     destRegion.SetSize(destSize);
222 IND **}
223   
224   /**
225    * Copy an image region (start index and size) for the case where
226    * the source region has a lesser dimension than the destination
227    * region.  This copies the source into the first part of the
228    * destination. This is an overloaded function.  The other versions
229    * of this functions handle the case where the source dimension is
230    * less than the destination dimension and the case where the
231    * destination and source are the same dimension.
232    *
233    * Note that the use of source and destination reflect where
234    * where is information is coming from and going to.  When used
235    * in the pipeline mechanism, the region requested by the output
236    * of a filter is used to define the region required on the input.
237    * In this case the output of the filter is the source and the
238    * input of the filter is the destination.
239    */
240   template <unsigned int D1, unsigned int D2>
241 IND **void ImageToImageFilterDefaultCopyRegion(const typename
242                BinaryUnsignedIntDispatch<D1, D2>::FirstGreaterThanSecondType &,
243                ImageRegion<D1> &destRegion,
244                const ImageRegion<D2> &srcRegion)
245 IND **{
246 IND ****// Source dimension is less than the destination dimension, copy source
247 IND ****// into the first part of the destination and set zeros elsewhere.
248 IND ****unsigned int dim;
249 IND ****Index<D1> destIndex;
250 IND ****Size<D1>  destSize;
251 IND ****const Index<D2> &srcIndex = srcRegion.GetIndex();
252 IND ****const Size<D2> &srcSize = srcRegion.GetSize();
253     
254 IND ****// copy what we can
255 IND ****for (dim=0; dim < D2; ++dim)
256       {
257       destIndex[dim] = srcIndex[dim];
258       destSize[dim] = srcSize[dim];
259       }
260     // fill in the rest of the dimensions with zero/one
261     for (; dim < D1; ++dim)
262       {
263       destIndex[dim] = 0;
264       destSize[dim] = 1;
265       }
266     
267     destRegion.SetIndex(destIndex);
268     destRegion.SetSize(destSize);
269 IND **}
270
271
272   /** \class ImageRegionCopier
273 LEN    * \brief Function object used to dispatching to a routine to copy a region (start index and size).
274    *
275    * Function object used for dispatching to various routines to copy
276    * a region (start index and size).  Most filters use this function
277    * object as trivial copy because they require the input image
278    * dimension to match the output image dimension.  However, some
279    * filters like itk::ExtractImageFilter can support output images of
280    * a lower dimension that the input.
281    *
282    * This function object is used by the default implementation of
283    * ImageToImageFilter::GenerateInputRequestedRegion(). It can also
284    * be used in routines like ImageSource::ThreadedGenerateData()
285 WCM    * where a filter may need to map the the output region for a
286    * particular thread to an input region.
287    *
288    * This copier uses a "dispatch pattern" to call one of three
289    * overloaded functions depending on whether the source and
290    * destination dimensions are the same, the source is of a higher
291    * dimension than the destination, or the source is of a lower
292    * dimension than the destination. The use of an overloaded function
293    * is required for proper compilation of the various cases.
294    * 
295    * For the latter two cases, trivial implementations are used.  If
296    * the source dimension is a lower dimension than the destination,
297    * the output region information is copied into the first portion of
298    * the destination region and the rest of the input region is set to
299    * zero.  If the source region is a higher dimension than the
300    * destination, the first portion of the source region is copied to
301    * the destination region.
302    *
303    * If a filter needs a different behavior is will need to override
304    * the CallCopyOutputRegionToInputRegion() method or the 
305    * CallCopyInputRegionToOutputRegion() method and delegate to the
306    * appropriate RegionCopier class. 
307    */
308   template <unsigned int D1, unsigned int D2>
309 IND ****class ITK_EXPORT ImageRegionCopier 
310 IND **{
311 IND **public:
312 IND ****virtual void operator() (ImageRegion<D1> &destRegion,
313                             const ImageRegion<D2> &srcRegion) const
314     {
315 LEN,IND ******typedef typename BinaryUnsignedIntDispatch<D1, D2>::ComparisonType ComparisonType;
316 IND ******ImageToImageFilterDefaultCopyRegion<D1, D2>(
317                       ComparisonType(),
318 IND **********************destRegion, srcRegion);
319     }
320 IND ****virtual ~ImageRegionCopier() {}
321 IND **};
322
323
324 IND **/** Stream operator for ImageRegionCopier objects. Just prints the RTTI
325 IND ******typename. */
326 IND **template<unsigned int D1, unsigned int D2>
327 IND **std::ostream & operator<<(std::ostream &os, const ImageRegionCopier<D1, D2> &
328                             copier)
329 IND **{
330 IND ****os << "ImageRegionCopier: "
331        << typeid(ImageRegionCopier<D1, D2>).name() << std::endl;
332     return os;
333 IND **}
334
335   /** operator!= for ImageRegionCopier objects. */
336   template<unsigned int D1, unsigned int D2>
337 IND **bool operator!=(const ImageRegionCopier<D1, D2> &c1,
338                   const ImageRegionCopier<D1, D2> &c2)
339 IND **{
340 IND ****return &c1 != &c2;
341 IND **}
342   
343 // end of namespace ImageToImageFilterDetail
344     
345 // end namespace itk
346
347 //#ifndef ITK_MANUAL_INSTANTIATION
348 //#include "itkImageToImageFilterDetail.txx"
349 //#endif
350
351 #endif
352

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