KWStyle - itkLinearInterpolateImageFunction.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkLinearInterpolateImageFunction.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:41 $
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      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 DEF #ifndef _itkLinearInterpolateImageFunction_txx
18 DEF #define _itkLinearInterpolateImageFunction_txx
19 #include "itkLinearInterpolateImageFunction.h"
20
21 #include "vnl/vnl_math.h"
22
23 namespace itk
24 {
25
26 /**
27  * Define the number of neighbors
28  */
29 template<class TInputImage, class TCoordRep>
30 const unsigned long
31 LinearInterpolateImageFunction< TInputImage, TCoordRep >
32 ::m_Neighbors = 1 << TInputImage::ImageDimension;
33
34
35 /**
36  * Constructor
37  */
38 template<class TInputImage, class TCoordRep>
39 LinearInterpolateImageFunction< TInputImage, TCoordRep >
40 ::LinearInterpolateImageFunction()
41 {
42
43 }
44
45
46 /**
47  * PrintSelf
48  */
49 template<class TInputImage, class TCoordRep>
50 void
51 LinearInterpolateImageFunction< TInputImage, TCoordRep >
52 ::PrintSelf(std::ostream& os, Indent indent) const
53 {
54   this->Superclass::PrintSelf(os,indent);
55 }
56
57
58 /**
59  * Evaluate at image index position
60  */
61 template<class TInputImage, class TCoordRep>
62 typename LinearInterpolateImageFunction< TInputImage, TCoordRep >
63 ::OutputType
64 LinearInterpolateImageFunction< TInputImage, TCoordRep >
65 ::EvaluateAtContinuousIndex(
66   const ContinuousIndexType& index) const
67 {
68   unsigned int dim;  // index over dimension
69
70   /**
71    * Compute base index = closet index below point
72    * Compute distance from point to base index
73    */
74   signed long baseIndex[ImageDimension];
75   double distance[ImageDimension];
76   long tIndex;
77
78   for( dim = 0; dim < ImageDimension; dim++ )
79     {
80     // The following "if" block is equivalent to the following line without
81     // having to call floor.
82     //    baseIndex[dim] = (long) floor( index[dim] );
83     if (index[dim] >= 0.0)
84       {
85       baseIndex[dim] = (long) index[dim];
86       }
87     else
88       {
89       tIndex = (long) index[dim];
90       if (double(tIndex) != index[dim])
91         {
92         tIndex--;
93         }
94       baseIndex[dim] = tIndex;
95       }
96     distance[dim] = index[dim] - double( baseIndex[dim] );
97     }
98   
99   /**
100    * Interpolated value is the weighted sum of each of the surrounding
101    * neighbors. The weight for each neighbor is the fraction overlap
102    * of the neighbor pixel with respect to a pixel centered on point.
103    */
104   RealType value = NumericTraits<RealType>::Zero;
105   RealType totalOverlap = NumericTraits<RealType>::Zero;
106
107   for( unsigned int counter = 0; counter < m_Neighbors; counter++ )
108     {
109
110     double overlap = 1.0;          // fraction overlap
111     unsigned int upper = counter;  // each bit indicates upper/lower neighbour
112     IndexType neighIndex;
113
114     // get neighbor index and overlap fraction
115     for( dim = 0; dim < ImageDimension; dim++ )
116       {
117
118       if ( upper & 1 )
119         {
120         neighIndex[dim] = baseIndex[dim] + 1;
121         overlap *= distance[dim];
122         }
123       else
124         {
125         neighIndex[dim] = baseIndex[dim];
126         overlap *= 1.0 - distance[dim];
127         }
128
129       upper >>= 1;
130
131       }
132     
133     // get neighbor value only if overlap is not zero
134     if( overlap )
135       {
136 LEN       value += overlap * static_cast<RealType>( this->GetInputImage()->GetPixel( neighIndex ) );
137       totalOverlap += overlap;
138       }
139
140     if( totalOverlap == 1.0 )
141       {
142       // finished
143       break;
144       }
145
146     }
147
148   return ( static_cast<OutputType>( value ) );
149 }
150
151 // namespace itk
152
153 #endif
154

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