OTB  9.0.0
Orfeo Toolbox
otbNCCRegistrationFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbNCCRegistrationFilter_hxx
22 #define otbNCCRegistrationFilter_hxx
24 
25 namespace otb
26 {
27 
28 /*
29  * Default constructor
30  */
31 template <class TFixedImage, class TMovingImage, class TDisplacementField>
33 {
34 
36  drfp = NCCRegistrationFunctionType::New();
37 
38  drfp->SetDisplacementField(this->GetDisplacementField());
39 
40  this->SetDifferenceFunction(static_cast<FiniteDifferenceFunctionType*>(drfp.GetPointer()));
41 }
42 
43 template <class TFixedImage, class TMovingImage, class TDisplacementField>
45 {
46  Superclass::PrintSelf(os, indent);
47  os << indent << "NCC Radius: " << this->GetNCCRadius() << std::endl;
48 }
49 
50 /*
51  * Set the function state values before each iteration
52  */
53 template <class TFixedImage, class TMovingImage, class TDisplacementField>
55 {
56 
57  // call the superclass implementation
58  Superclass::InitializeIteration();
59 
60  // set the gradient selection flag
61  NCCRegistrationFunctionType* drfp = dynamic_cast<NCCRegistrationFunctionType*>(this->GetDifferenceFunction().GetPointer());
62 
63  if (!drfp)
64  {
65  itkExceptionMacro(<< "Could not cast difference function to NCCRegistrationFunction");
66  }
67 
68  /*
69  * Smooth the displacement field
70 
71  if ( this->GetSmoothDisplacementField() )
72  {
73  this->SmoothDisplacementField();
74  }
75  */
76 }
77 
78 /*
79  * Get the metric value from the difference function
80  */
81 template <class TFixedImage, class TMovingImage, class TDisplacementField>
83 {
84 
85  NCCRegistrationFunctionType* drfp = dynamic_cast<NCCRegistrationFunctionType*>(this->GetDifferenceFunction().GetPointer());
86 
87  if (!drfp)
88  {
89  itkExceptionMacro(<< "Could not cast difference function to NCCRegistrationFunction");
90  }
91 
92  return drfp->GetEnergy();
93 }
94 
95 /*
96  *
97  */
98 template <class TFixedImage, class TMovingImage, class TDisplacementField>
101 {
102 
103  NCCRegistrationFunctionType* drfp = dynamic_cast<NCCRegistrationFunctionType*>(this->GetDifferenceFunction().GetPointer());
104 
105  if (!drfp)
106  {
107  itkExceptionMacro(<< "Could not cast difference function to NCCRegistrationFunction");
108  }
109 
110  return drfp->GetRadius();
111 }
112 
113 /*
114  *
115  */
116 template <class TFixedImage, class TMovingImage, class TDisplacementField>
118 {
119 
120  NCCRegistrationFunctionType* drfp = dynamic_cast<NCCRegistrationFunctionType*>(this->GetDifferenceFunction().GetPointer());
121 
122  if (!drfp)
123  {
124  itkExceptionMacro(<< "Could not cast difference function to NCCRegistrationFunction");
125  }
126 
127  drfp->SetRadius(radius);
128 
129  this->Modified();
130 }
131 
132 template <class TFixedImage, class TMovingImage, class TDisplacementField>
134 {
135  // get pointers to the input and output
136  typename Superclass::FixedImagePointer fixedPtr = const_cast<TFixedImage*>(this->GetFixedImage());
137  typename Superclass::MovingImagePointer movingPtr = const_cast<TMovingImage*>(this->GetMovingImage());
138  typename TDisplacementField::Pointer outputPtr = this->GetOutput();
139 
140  if (!fixedPtr || !movingPtr || !outputPtr)
141  {
142  return;
143  }
144 
145  // get a copy of the input requested region (should equal the output
146  // requested region)
147  typename TDisplacementField::RegionType requestedRegion;
148  requestedRegion = outputPtr->GetRequestedRegion();
149 
150  // pad the input requested region by the operator radius
151  requestedRegion.PadByRadius(this->GetNCCRadius());
152 
153  // crop the input requested region at the input's largest possible region
154  if (requestedRegion.Crop(fixedPtr->GetLargestPossibleRegion()))
155  {
156  if (requestedRegion.Crop(movingPtr->GetLargestPossibleRegion()))
157  {
158  fixedPtr->SetRequestedRegion(requestedRegion);
159  movingPtr->SetRequestedRegion(requestedRegion);
160  return;
161  }
162  else
163  {
164  // Couldn't crop the region (requested region is outside the largest
165  // possible region). Throw an exception.
166 
167  // store what we tried to request (prior to trying to crop)
168  movingPtr->SetRequestedRegion(requestedRegion);
169 
170  // build an exception
171  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
172  e.SetLocation(ITK_LOCATION);
173  e.SetDescription("Requested region is (at least partially) outside the largest possible region of the moving image.");
174  e.SetDataObject(movingPtr);
175  throw e;
176  }
177  }
178  else
179  {
180  // Couldn't crop the region (requested region is outside the largest
181  // possible region). Throw an exception.
182 
183  // store what we tried to request (prior to trying to crop)
184  fixedPtr->SetRequestedRegion(requestedRegion);
185 
186  // build an exception
187  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
188  e.SetLocation(ITK_LOCATION);
189  e.SetDescription("Requested region is (at least partially) outside the largest possible region of the fixed image.");
190  e.SetDataObject(fixedPtr);
191  throw e;
192  }
193 }
194 
195 /*
196  * Get the metric value from the difference function
197  */
198 template <class TFixedImage, class TMovingImage, class TDisplacementField>
200 {
201  // If we smooth the update buffer before applying it, then the are
202  // approximating a viscuous problem as opposed to an elastic problem
203  /* if ( this->GetSmoothUpdateField() )
204  {
205  this->SmoothUpdateField();
206  }
207  */
208  this->Superclass::ApplyUpdate(dt);
209 
210  NCCRegistrationFunctionType* drfp = dynamic_cast<NCCRegistrationFunctionType*>(this->GetDifferenceFunction().GetPointer());
211 
212  if (!drfp)
213  {
214  itkExceptionMacro(<< "Could not cast difference function to NCCRegistrationFunction");
215  }
216 
217  // this->SetRMSChange( drfp->GetRMSChange() );
218 }
219 
220 } // end namespace otb
221 
222 #endif
otb::NCCRegistrationFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion() override
Definition: otbNCCRegistrationFilter.hxx:133
otb::NCCRegistrationFilter::RadiusType
NCCRegistrationFunctionType::RadiusType RadiusType
Definition: otbNCCRegistrationFilter.h:108
otbNCCRegistrationFilter.h
otb::NCCRegistrationFilter::FiniteDifferenceFunctionType
Superclass::FiniteDifferenceFunctionType FiniteDifferenceFunctionType
Definition: otbNCCRegistrationFilter.h:103
otb::NCCRegistrationFilter::SetNCCRadius
virtual void SetNCCRadius(RadiusType radius)
Definition: otbNCCRegistrationFilter.hxx:117
otb::NCCRegistrationFilter::GetMetric
virtual double GetMetric() const
Definition: otbNCCRegistrationFilter.hxx:82
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::NCCRegistrationFilter::NCCRegistrationFilter
NCCRegistrationFilter()
Definition: otbNCCRegistrationFilter.hxx:32
otb::NCCRegistrationFilter::InitializeIteration
void InitializeIteration() override
Definition: otbNCCRegistrationFilter.hxx:54
otb::NCCRegistrationFunction::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbNCCRegistrationFunction.h:56
otb::NCCRegistrationFilter::GetNCCRadius
virtual RadiusType GetNCCRadius() const
Definition: otbNCCRegistrationFilter.hxx:100
otb::NCCRegistrationFilter::ApplyUpdate
virtual void ApplyUpdate(TimeStepType dt)
Definition: otbNCCRegistrationFilter.hxx:199
otb::NCCRegistrationFilter::TimeStepType
Superclass::TimeStepType TimeStepType
Definition: otbNCCRegistrationFilter.h:85
otb::NCCRegistrationFunction
Definition: otbNCCRegistrationFunction.h:49
otb::NCCRegistrationFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbNCCRegistrationFilter.hxx:44