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