OTB  10.0.0
Orfeo Toolbox
otbFunctionWithNeighborhoodToImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbFunctionWithNeighborhoodToImageFilter_hxx
23 #define otbFunctionWithNeighborhoodToImageFilter_hxx
24 
26 #include "otbMacro.h"
27 
28 namespace otb
29 {
30 
34 template <class TInputImage, class TOutputImage, class TFunction>
36 {
37  this->DynamicMultiThreadingOff();
38  this->InPlaceOff();
39  this->SetNumberOfRequiredInputs(1);
40  m_Radius.Fill(1);
41  m_Offset.Fill(1);
42  m_FunctionList.clear();
43  m_Function = FunctionType::New();
44 }
46 
47 template <class TInputImage, class TOutputImage, class TFunction>
49 {
50  Superclass::BeforeThreadedGenerateData();
51 
52  InputImageConstPointer inputPtr = dynamic_cast<const TInputImage*>((itk::ProcessObject::GetInput(0)));
53  if (inputPtr.IsNull())
54  {
55  itkExceptionMacro(<< "At least one input is missing."
56  << " Input is missing :" << inputPtr.GetPointer();)
57  }
58  m_Function->SetInputImage(inputPtr);
59  for (unsigned int i = 0; i < static_cast<unsigned int>(this->GetNumberOfWorkUnits()); ++i)
60  {
61  FunctionPointerType func = m_Function;
62  m_FunctionList.push_back(func);
63  }
64 }
65 
66 template <class TInputImage, class TOutputImage, class TFunction>
68 {
69  // call the superclass' implementation of this method
70  Superclass::GenerateInputRequestedRegion();
71 
72  // get pointers to the input and output
73  InputImagePointer inputPtr = const_cast<TInputImage*>(this->GetInput());
74  OutputImagePointer outputPtr = this->GetOutput();
75 
76  if (!inputPtr || !outputPtr)
77  {
78  return;
79  }
80  // get a copy of the input requested region (should equal the output
81  // requested region)
82  InputImageRegionType inputRequestedRegion = inputPtr->GetRequestedRegion();
83 
84  // pad the input requested region by the operator radius
85  InputImageSizeType maxRad;
86  maxRad[0] = m_Radius[0] + std::abs(m_Offset[0]);
87  maxRad[1] = m_Radius[0] + std::abs(m_Offset[1]);
88  inputRequestedRegion.PadByRadius(maxRad);
89 
90  // crop the input requested region at the input's largest possible region
91  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
92  {
93  inputPtr->SetRequestedRegion(inputRequestedRegion);
94  return;
95  }
96  else
97  {
98  // Couldn't crop the region (requested region is outside the largest
99  // possible region). Throw an exception.
100 
101  // store what we tried to request (prior to trying to crop)
102  inputPtr->SetRequestedRegion(inputRequestedRegion);
103 
104  // build an exception
105  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
106  std::ostringstream msg;
107  msg << this->GetNameOfClass() << "::GenerateInputRequestedRegion()";
108  e.SetLocation(msg.str());
109  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
110  e.SetDataObject(inputPtr);
111  throw e;
112  }
113 }
114 
118 template <class TInputImage, class TOutputImage, class TFunction>
120  itk::ThreadIdType threadId)
121 {
122  // We use dynamic_cast since inputs are stored as DataObjects.
123  InputImageConstPointer inputPtr = dynamic_cast<const TInputImage*>((itk::ProcessObject::GetInput(0)));
124 
125  OutputImagePointer outputPtr = this->GetOutput(0);
126 
127  itk::ImageRegionConstIterator<TInputImage> inputIt(inputPtr, outputRegionForThread);
128  itk::ImageRegionIterator<TOutputImage> outputIt(outputPtr, outputRegionForThread);
129 
130  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
131 
132  inputIt.GoToBegin();
133  outputIt.GoToBegin();
134 
135  while (!inputIt.IsAtEnd())
136  {
137  outputIt.Set(static_cast<OutputImagePixelType>(m_FunctionList[threadId]->EvaluateAtIndex(inputIt.GetIndex())));
138  ++inputIt;
139  ++outputIt;
140 
141  progress.CompletedPixel(); // potential exception thrown here
142  }
143 }
144 } // end namespace otb
145 
146 #endif
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.