OTB  6.7.0
Orfeo Toolbox
otbUnaryFunctorWithIndexImageFilter.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 otbUnaryFunctorWithIndexImageFilter_hxx
22 #define otbUnaryFunctorWithIndexImageFilter_hxx
23 
25 #include "itkImageRegionIterator.h"
26 #include "itkProgressReporter.h"
27 
28 namespace otb
29 {
33 template <class TInputImage, class TOutputImage, class TFunction>
36 {
37  this->SetNumberOfRequiredInputs(1);
38 }
39 template <class TInputImage, class TOutputImage, class TFunction>
40 void
43 {
44  // call the superclass' implementation of this method
45  Superclass::GenerateInputRequestedRegion();
47 
48  // get pointers to the input and output
49  typename Superclass::InputImagePointer inputPtr =
50  const_cast<TInputImage *>(this->GetInput());
51  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
52 
53  if (!inputPtr || !outputPtr)
54  {
55  return;
56  }
57  // get a copy of the input requested region (should equal the output
58  // requested region)
59  typename TInputImage::RegionType inputRequestedRegion;
60  inputRequestedRegion = inputPtr->GetRequestedRegion();
61 
62  // crop the input requested region at the input's largest possible region
63  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
64  {
65  inputPtr->SetRequestedRegion(inputRequestedRegion);
66  return;
67  }
68  else
69  {
70  // Couldn't crop the region (requested region is outside the largest
71  // possible region). Throw an exception.
72 
73  // store what we tried to request (prior to trying to crop)
74  inputPtr->SetRequestedRegion(inputRequestedRegion);
75 
76  // build an exception
77  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
78  std::ostringstream msg;
79  msg << this->GetNameOfClass()
80  << "::GenerateInputRequestedRegion()";
81  e.SetLocation(msg.str());
82  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
83  e.SetDataObject(inputPtr);
84  throw e;
85  }
86 }
87 
91 template <class TInputImage, class TOutputImage, class TFunction>
92 void
94 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
95 {
96  InputImagePointer inputPtr = dynamic_cast<const TInputImage*>(ProcessObjectType::GetInput(0));
97  OutputImagePointer outputPtr = this->GetOutput(0);
99 
100  InputImageRegionType inputRegionForThread;
101  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
102 
103  // Define the iterators
104  IteratorType inputIt = IteratorType(inputPtr, inputRegionForThread);
105  itk::ImageRegionIterator<TOutputImage> outputIt(outputPtr, outputRegionForThread);
106 
107  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
108 
109  inputIt.GoToBegin();
110  outputIt.GoToBegin();
111 
112  while (!inputIt.IsAtEnd())
113  {
114  outputIt.Set(m_Functor(inputIt.Get(), inputIt.GetIndex()));
115  ++inputIt;
116  ++outputIt;
117  progress.CompletedPixel(); // potential exception thrown here
118  }
119 
120 }
121 
122 } // end namespace otb
123 
124 #endif
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
void SetDataObject(DataObject *dobj)
PixelType Get(void) const
virtual void SetDescription(const std::string &s)
virtual void SetLocation(const std::string &s)
void Set(const PixelType &value) const
InputImageType::Pointer InputImagePointer
const IndexType & GetIndex() const
OutputImageType::Pointer OutputImagePointer
InputImageType::RegionType InputImageRegionType
unsigned int ThreadIdType
OutputImageType::RegionType OutputImageRegionType