Orfeo Toolbox  4.0
otbFunctionWithNeighborhoodToImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12  Some parts of this code are derived from ITK. See ITKCopyright.txt
13  for details.
14 
15 
16  This software is distributed WITHOUT ANY WARRANTY; without even
17  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18  PURPOSE. See the above copyright notices for more information.
19 
20 =========================================================================*/
21 #ifndef __otbFunctionWithNeighborhoodToImageFilter_txx
22 #define __otbFunctionWithNeighborhoodToImageFilter_txx
23 
25 #include "otbMacro.h"
26 
27 namespace otb
28 {
29 
33 template<class TInputImage, class TOutputImage, class TFunction>
36 {
37  this->InPlaceOff();
38  this->SetNumberOfRequiredInputs(1);
39  m_Radius.Fill(1);
40  m_Offset.Fill(1);
41  m_FunctionList.clear();
42  m_Function = FunctionType::New();
43 
44 }
45 
46 template <class TInputImage, class TOutputImage, class TFunction>
47 void
50 {
51  Superclass::BeforeThreadedGenerateData();
52 
53  InputImageConstPointer inputPtr = dynamic_cast<const TInputImage*>((itk::ProcessObject::GetInput(0)));
54  if (inputPtr.IsNull())
55  {
56  itkExceptionMacro(<< "At least one input is missing."
57  << " Input is missing :" << inputPtr.GetPointer(); )
58 
59  }
60  m_Function->SetInputImage(inputPtr);
61  for (unsigned int i = 0; i < static_cast<unsigned int>(this->GetNumberOfThreads()); ++i)
62  {
63  FunctionPointerType func = m_Function;
64  m_FunctionList.push_back(func);
65  }
66 }
67 
68 template <class TInputImage, class TOutputImage, class TFunction>
69 void
72 {
73  // call the superclass' implementation of this method
74  Superclass::GenerateInputRequestedRegion();
75 
76  // get pointers to the input and output
77  InputImagePointer inputPtr = const_cast<TInputImage *>(this->GetInput());
78  OutputImagePointer outputPtr = this->GetOutput();
79 
80  if (!inputPtr || !outputPtr)
81  {
82  return;
83  }
84  // get a copy of the input requested region (should equal the output
85  // requested region)
86  InputImageRegionType inputRequestedRegion = inputPtr->GetRequestedRegion();
87 
88  // pad the input requested region by the operator radius
89  InputImageSizeType maxRad;
90  maxRad[0] = m_Radius[0] + vcl_abs(m_Offset[0]);
91  maxRad[1] = m_Radius[0] + vcl_abs(m_Offset[1]);
92  inputRequestedRegion.PadByRadius(maxRad);
93 
94  // crop the input requested region at the input's largest possible region
95  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
96  {
97  inputPtr->SetRequestedRegion(inputRequestedRegion);
98  return;
99  }
100  else
101  {
102  // Couldn't crop the region (requested region is outside the largest
103  // possible region). Throw an exception.
104 
105  // store what we tried to request (prior to trying to crop)
106  inputPtr->SetRequestedRegion(inputRequestedRegion);
107 
108  // build an exception
109  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
110  std::ostringstream msg;
111  msg << this->GetNameOfClass()
112  << "::GenerateInputRequestedRegion()";
113  e.SetLocation(msg.str().c_str());
114  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
115  e.SetDataObject(inputPtr);
116  throw e;
117  }
118 }
119 
123 template<class TInputImage, class TOutputImage, class TFunction>
124 void
126 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
127  itk::ThreadIdType threadId)
128 {
129  // We use dynamic_cast since inputs are stored as DataObjects.
130  InputImageConstPointer inputPtr = dynamic_cast<const TInputImage*>((itk::ProcessObject::GetInput(0)));
131 
132  OutputImagePointer outputPtr = this->GetOutput(0);
133 
134  itk::ImageRegionConstIterator<TInputImage> inputIt(inputPtr, outputRegionForThread);
135  itk::ImageRegionIterator<TOutputImage> outputIt(outputPtr, outputRegionForThread);
136 
137  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
138 
139  inputIt.GoToBegin();
140  outputIt.GoToBegin();
141 
142  while (!inputIt.IsAtEnd())
143  {
144  outputIt.Set(static_cast<OutputImagePixelType>(m_FunctionList[threadId]->EvaluateAtIndex(inputIt.GetIndex())));
145  ++inputIt;
146  ++outputIt;
147 
148  progress.CompletedPixel(); // potential exception thrown here
149  }
150 }
151 } // end namespace otb
152 
153 #endif

Generated at Sat Mar 8 2014 15:56:35 for Orfeo Toolbox with doxygen 1.8.3.1