KWStyle - itkPCAShapeSignedDistanceFunction.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkPCAShapeSignedDistanceFunction.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:43 $
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 _itkPCAShapeSignedDistanceFunction_txx
18 DEF #define _itkPCAShapeSignedDistanceFunction_txx
19
20 #include "itkPCAShapeSignedDistanceFunction.h"
21
22 #include "itkTranslationTransform.h"
23 #include "itkLinearInterpolateImageFunction.h"
24 #include "itkNearestNeighborExtrapolateImageFunction.h"
25
26 namespace itk
27 {
28
29
30 // Constructor with default arguments
31 template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
32 PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
33 ::PCAShapeSignedDistanceFunction()
34 {
35   m_NumberOfPrincipalComponents = 0;
36   m_NumberOfTransformParameters = 0;
37
38   m_MeanImage = NULL;
39   m_PrincipalComponentImages.resize(0);
40   m_PrincipalComponentStandardDeviations.SetSize(0);
41
42   m_Transform = TranslationTransform<TCoordRep, SpaceDimension>::New();
43   m_Interpolators.resize(0);
44   m_Extrapolators.resize(0);
45   m_Selectors.resize(0);
46
47   m_WeightOfPrincipalComponents.SetSize(0);
48   m_TransformParameters.SetSize(0);
49   this->GetParameters().SetSize(0);
50 }
51     
52
53 // Set the number of principal components
54 template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
55 void
56 PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
57 ::SetNumberOfPrincipalComponents(unsigned int n)
58 {
59   m_NumberOfPrincipalComponents = n;
60
61   m_PrincipalComponentImages.resize(n,NULL);
62   m_PrincipalComponentStandardDeviations.SetSize(n);
63   m_PrincipalComponentStandardDeviations.Fill( 1.0 );
64
65   m_WeightOfPrincipalComponents.SetSize(n);
66 }
67
68
69 // Set the parameters
70 template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
71 void
72 PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
73 ::SetParameters( const ParametersType & parameters )
74 {
75   this->m_Parameters = parameters;
76
77   // set the shape parameters
78   unsigned int i;
79   for(i=0; i<m_NumberOfPrincipalComponents; i++)
80     { m_WeightOfPrincipalComponents[i] = parameters[i]; }
81
82   // set the transform parameters
83   m_NumberOfTransformParameters = 
84     parameters.size() - m_NumberOfPrincipalComponents;
85   m_TransformParameters.SetSize(m_NumberOfTransformParameters);
86
87   for(i=0; i<m_NumberOfTransformParameters; i++)
88     {m_TransformParameters[i] = parameters[m_NumberOfPrincipalComponents+i];}
89
90   // set up the transform
91   m_Transform->SetParameters(m_TransformParameters);
92
93 }
94
95
96 // Print self
97 template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
98 void
99 PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
100 ::PrintSelf(std::ostream &os, Indent indent) const
101 {
102   Superclass::PrintSelf(os,indent);
103   
104   os << indent << "Transform: "
105      << m_Transform.GetPointer() << std::endl;
106
107   os << indent << "NumberOfPrincipalComponents: " 
108      << m_NumberOfPrincipalComponents << std::endl;  
109   os << indent << "PrincipalComponentStandardDeviations: "
110      << m_PrincipalComponentStandardDeviations << std::endl;
111   os << indent << "MeanImage: "
112      << m_MeanImage.GetPointer() << std::endl;
113
114   os << indent << "WeightOfPrincipalComponents: " 
115      << m_WeightOfPrincipalComponents << std::endl;
116   os << indent << "TransformParameters: " 
117      << m_TransformParameters << std::endl;
118 }
119
120
121 // Initialize the function
122 template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
123 void
124 PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
125 ::Initialize() throw ( ExceptionObject )
126 {
127   // verify mean image
128   if ( !m_MeanImage )
129     { 
130     itkExceptionMacro( << "MeanImage is not present." ); 
131     }
132
133   // verify principal component images
134   if ( m_PrincipalComponentImages.size() < m_NumberOfPrincipalComponents )
135     {
136     itkExceptionMacro( << "PrincipalComponentsImages does not have at least " 
137                        << m_NumberOfPrincipalComponents
138                        << " number of elements." );
139     }
140
141   // verify image buffered region
142   typename ImageType::RegionType meanImageRegion = 
143     m_MeanImage->GetBufferedRegion();
144
145   for (unsigned int i=0; i< m_NumberOfPrincipalComponents; i++)
146     {
147     if ( !m_PrincipalComponentImages[i] )
148       {
149       itkExceptionMacro( << "PrincipalComponentImages[" 
150                          << i << "] is not present." );
151       }
152
153     if ( m_PrincipalComponentImages[i]->GetBufferedRegion() !=
154 IND ******meanImageRegion )
155       {
156 LEN       itkExceptionMacro( << "The buffered region of the PrincipalComponentImages[" 
157                          << i << "] is different from the MeanImage." );
158       }
159     }
160
161
162   // set up the interpolators/extrapolators for each of the mean and pc images
163   m_Interpolators.resize(m_NumberOfPrincipalComponents + 1);
164   m_Extrapolators.resize(m_NumberOfPrincipalComponents + 1);
165   m_Selectors.resize(m_NumberOfPrincipalComponents+1);
166
167   // interpolator/extrapolator for mean image
168   m_Interpolators[0] = 
169     LinearInterpolateImageFunction<ImageType, CoordRepType>::New();
170   m_Interpolators[0]->SetInputImage(m_MeanImage);
171
172   m_Extrapolators[0] = 
173 IND ****NearestNeighborExtrapolateImageFunction<ImageType, CoordRepType>::New();
174   m_Extrapolators[0]->SetInputImage(m_MeanImage);
175
176   // interpolators/extrapolators for pc images
177   for (unsigned int k=1; k<=m_NumberOfPrincipalComponents; k++)
178     {
179     m_Interpolators[k] = 
180       LinearInterpolateImageFunction<ImageType, CoordRepType>::New();
181     m_Interpolators[k]->SetInputImage(m_PrincipalComponentImages[k-1]);
182
183     m_Extrapolators[k] = 
184 IND ******NearestNeighborExtrapolateImageFunction<ImageType, CoordRepType>::New();
185     m_Extrapolators[k]->SetInputImage(m_PrincipalComponentImages[k-1]);
186     }
187 }
188
189
190 // Evaluate the signed distance
191 template<typename TCoordRep, unsigned int VSpaceDimension, typename TImage>
192 typename PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
193 ::OutputType
194 PCAShapeSignedDistanceFunction<TCoordRep, VSpaceDimension,TImage>
195 ::Evaluate( const PointType& point ) const
196 {
197   // transform the point into the shape model space
198   PointType mappedPoint = m_Transform->TransformPoint(point);
199
200   itkDebugMacro(<< "mappedPoint:" << mappedPoint);
201
202   if(!m_Interpolators[0]->IsInsideBuffer(mappedPoint))
203     {
204     for(unsigned int i=0; i<=m_NumberOfPrincipalComponents; i++)
205       { m_Selectors[i] = m_Extrapolators[i]; }
206     itkDebugMacro(<< "use extrapolator");
207     }
208   else
209     {
210     for(unsigned int i=0; i<=m_NumberOfPrincipalComponents; i++)
211       { m_Selectors[i] = m_Interpolators[i]; }
212     itkDebugMacro(<< "use interpolator");
213     }
214
215   typedef typename NumericTraits<OutputType>::RealType RealType;
216   RealType output = m_Selectors[0]->Evaluate(mappedPoint);
217
218   for(unsigned int i=0; i<m_NumberOfPrincipalComponents; i++)
219     {
220     output += m_Selectors[i+1]->Evaluate(mappedPoint) *
221 IND ******m_PrincipalComponentStandardDeviations[i] *
222 SEM,IND ******m_WeightOfPrincipalComponents[i] ;
223     }
224
225   return output;
226 }
227   
228
229 // namespace
230
231 #endif
232

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