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