OTB  10.0.0
Orfeo Toolbox
otbUnaryFunctorWithIndexImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 otbUnaryFunctorWithIndexImageFilter_hxx
22 #define otbUnaryFunctorWithIndexImageFilter_hxx
23 
25 #include "itkImageRegionIterator.h"
26 #include "itkProgressReporter.h"
27 #include "otbMacro.h" //for
28 
29 namespace otb
30 {
34 template <class TInputImage, class TOutputImage, class TFunction>
36 {
37  this->SetNumberOfRequiredInputs(1);
38  this->DynamicMultiThreadingOn();
39 }
40 template <class TInputImage, class TOutputImage, class TFunction>
42 {
43  // call the superclass' implementation of this method
44  Superclass::GenerateInputRequestedRegion();
46 
47  // get pointers to the input and output
48  typename Superclass::InputImagePointer inputPtr = const_cast<TInputImage*>(this->GetInput());
49  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
50 
51  if (!inputPtr || !outputPtr)
52  {
53  return;
54  }
55  // get a copy of the input requested region (should equal the output
56  // requested region)
57  typename TInputImage::RegionType inputRequestedRegion;
58  inputRequestedRegion = inputPtr->GetRequestedRegion();
59 
60  // crop the input requested region at the input's largest possible region
61  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
62  {
63  inputPtr->SetRequestedRegion(inputRequestedRegion);
64  return;
65  }
66  else
67  {
68  // Couldn't crop the region (requested region is outside the largest
69  // possible region). Throw an exception.
70 
71  // store what we tried to request (prior to trying to crop)
72  inputPtr->SetRequestedRegion(inputRequestedRegion);
73 
74  // build an exception
75  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
76  std::ostringstream msg;
77  msg << this->GetNameOfClass() << "::GenerateInputRequestedRegion()";
78  e.SetLocation(msg.str());
79  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
80  e.SetDataObject(inputPtr);
81  throw e;
82  }
83 }
84 
88 template <class TInputImage, class TOutputImage, class TFunction>
90 {
91  InputImagePointer inputPtr = dynamic_cast<const TInputImage*>(ProcessObjectType::GetInput(0));
92  OutputImagePointer outputPtr = this->GetOutput(0);
94 
95  InputImageRegionType inputRegionForThread;
96  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
97 
98  // Define the iterators
99  IteratorType inputIt = IteratorType(inputPtr, inputRegionForThread);
100  itk::ImageRegionIterator<TOutputImage> outputIt(outputPtr, outputRegionForThread);
101 
102  inputIt.GoToBegin();
103  outputIt.GoToBegin();
104 
105  while (!inputIt.IsAtEnd())
106  {
107  outputIt.Set(m_Functor(inputIt.Get(), inputIt.GetIndex()));
108  ++inputIt;
109  ++outputIt;
110  }
111 }
112 
113 } // end namespace otb
114 
115 #endif
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
itk::ImageRegionConstIteratorWithIndex< TInputImage > IteratorType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.