Orfeo Toolbox  4.2
otbNCCRegistrationFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbNCCRegistrationFilter_txx
19 #define __otbNCCRegistrationFilter_txx
21 
22 namespace otb
23 {
24 
25 /*
26  * Default constructor
27  */
28 template <class TFixedImage, class TMovingImage, class TDisplacementField>
31 {
32 
34  drfp = NCCRegistrationFunctionType::New();
35 
36  drfp->SetDisplacementField(this->GetDisplacementField());
37 
38  this->SetDifferenceFunction(static_cast<FiniteDifferenceFunctionType *>(
39  drfp.GetPointer()));
40 
41 }
42 
43 template <class TFixedImage, class TMovingImage, class TDisplacementField>
44 void
46 ::PrintSelf(std::ostream& os, itk::Indent indent) const
47 {
48  Superclass::PrintSelf(os, indent);
49  os << indent << "NCC Radius: " <<
50  this->GetNCCRadius() << std::endl;
51 }
52 
53 /*
54  * Set the function state values before each iteration
55  */
56 template <class TFixedImage, class TMovingImage, class TDisplacementField>
57 void
60 {
61 
62  // call the superclass implementation
63  Superclass::InitializeIteration();
64 
65  // set the gradient selection flag
67  dynamic_cast<NCCRegistrationFunctionType *>
68  (this->GetDifferenceFunction().GetPointer());
69 
70  if (!drfp)
71  {
72  itkExceptionMacro(<<
73  "Could not cast difference function to NCCRegistrationFunction");
74  }
75 
76  /*
77  * Smooth the displacement field
78 
79  if ( this->GetSmoothDisplacementField() )
80  {
81  this->SmoothDisplacementField();
82  }
83  */
84 }
85 
86 /*
87  * Get the metric value from the difference function
88  */
89 template <class TFixedImage, class TMovingImage, class TDisplacementField>
90 double
92 ::GetMetric() const
93 {
94 
96  dynamic_cast<NCCRegistrationFunctionType *>
97  (this->GetDifferenceFunction().GetPointer());
98 
99  if (!drfp)
100  {
101  itkExceptionMacro(<<
102  "Could not cast difference function to NCCRegistrationFunction");
103  }
104 
105  return drfp->GetEnergy();
106 
107 }
108 
109 /*
110  *
111  */
112 template <class TFixedImage, class TMovingImage, class TDisplacementField>
116 {
117 
119  dynamic_cast<NCCRegistrationFunctionType *>
120  (this->GetDifferenceFunction().GetPointer());
121 
122  if (!drfp)
123  {
124  itkExceptionMacro(<<
125  "Could not cast difference function to NCCRegistrationFunction");
126  }
127 
128  return drfp->GetRadius();
129 
130 }
131 
132 /*
133  *
134  */
135 template <class TFixedImage, class TMovingImage, class TDisplacementField>
136 void
139 {
140 
142  dynamic_cast<NCCRegistrationFunctionType *>
143  (this->GetDifferenceFunction().GetPointer());
144 
145  if (!drfp)
146  {
147  itkExceptionMacro(<<
148  "Could not cast difference function to NCCRegistrationFunction");
149  }
150 
151  drfp->SetRadius(radius);
152 
153  this->Modified();
154 }
155 
156 template <class TFixedImage, class TMovingImage, class TDisplacementField>
157 void
160 {
161  // get pointers to the input and output
162  typename Superclass::FixedImagePointer fixedPtr =
163  const_cast<TFixedImage *>(this->GetFixedImage());
164  typename Superclass::MovingImagePointer movingPtr =
165  const_cast<TMovingImage *>(this->GetMovingImage());
166  typename TDisplacementField::Pointer outputPtr = this->GetOutput();
167 
168  if (!fixedPtr || !movingPtr || !outputPtr)
169  {
170  return;
171  }
172 
173  // get a copy of the input requested region (should equal the output
174  // requested region)
175  typename TDisplacementField::RegionType requestedRegion;
176  requestedRegion = outputPtr->GetRequestedRegion();
177 
178  // pad the input requested region by the operator radius
179  requestedRegion.PadByRadius(this->GetNCCRadius());
180 
181  // crop the input requested region at the input's largest possible region
182  if (requestedRegion.Crop(fixedPtr->GetLargestPossibleRegion()))
183  {
184  if (requestedRegion.Crop(movingPtr->GetLargestPossibleRegion()))
185  {
186  fixedPtr->SetRequestedRegion(requestedRegion);
187  movingPtr->SetRequestedRegion(requestedRegion);
188  return;
189  }
190  else
191  {
192  // Couldn't crop the region (requested region is outside the largest
193  // possible region). Throw an exception.
194 
195  // store what we tried to request (prior to trying to crop)
196  movingPtr->SetRequestedRegion(requestedRegion);
197 
198  // build an exception
199  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
200  e.SetLocation(ITK_LOCATION);
201  e.SetDescription(
202  "Requested region is (at least partially) outside the largest possible region of the moving image.");
203  e.SetDataObject(movingPtr);
204  throw e;
205 
206  }
207  }
208  else
209  {
210  // Couldn't crop the region (requested region is outside the largest
211  // possible region). Throw an exception.
212 
213  // store what we tried to request (prior to trying to crop)
214  fixedPtr->SetRequestedRegion(requestedRegion);
215 
216  // build an exception
217  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
218  e.SetLocation(ITK_LOCATION);
219  e.SetDescription("Requested region is (at least partially) outside the largest possible region of the fixed image.");
220  e.SetDataObject(fixedPtr);
221  throw e;
222  }
223 }
224 
225 /*
226  * Get the metric value from the difference function
227  */
228 template <class TFixedImage, class TMovingImage, class TDisplacementField>
229 void
232 {
233  // If we smooth the update buffer before applying it, then the are
234  // approximating a viscuous problem as opposed to an elastic problem
235  /* if ( this->GetSmoothUpdateField() )
236  {
237  this->SmoothUpdateField();
238  }
239  */
240  this->Superclass::ApplyUpdate(dt);
241 
243  dynamic_cast<NCCRegistrationFunctionType *>
244  (this->GetDifferenceFunction().GetPointer());
245 
246  if (!drfp)
247  {
248  itkExceptionMacro(<<
249  "Could not cast difference function to NCCRegistrationFunction");
250  }
251 
252 // this->SetRMSChange( drfp->GetRMSChange() );
253 
254 }
255 
256 } // end namespace otb
257 
258 #endif

Generated at Sat Aug 30 2014 16:17:09 for Orfeo Toolbox with doxygen 1.8.3.1