Orfeo Toolbox  4.0
otbUnaryFunctorNeighborhoodWithOffsetImageFilter.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 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbUnaryFunctorNeighborhoodWithOffsetImageFilter_txx
19 #define __otbUnaryFunctorNeighborhoodWithOffsetImageFilter_txx
20 
22 #include "itkImageRegionIterator.h"
25 #include "itkProgressReporter.h"
28 
29 namespace otb
30 {
34 template <class TInputImage, class TOutputImage, class TFunction>
37 {
38  this->SetNumberOfRequiredInputs(1);
39  m_Radius.Fill(1);
40  m_Offset.Fill(1);
41  m_FunctorList.clear();
42 }
43 
44 template <class TInputImage, class TOutputImage, class TFunction>
45 void
48 {
49  Superclass::BeforeThreadedGenerateData();
50 
51  for (itk::ThreadIdType i = 0; i < this->GetNumberOfThreads(); ++i)
52  {
53  m_FunctorList.push_back(m_Functor);
54  }
55 }
56 
57 template <class TInputImage, class TOutputImage, class TFunction>
58 void
61 {
62  // call the superclass' implementation of this method
63  Superclass::GenerateInputRequestedRegion();
64 
65  // get pointers to the input and output
66  typename Superclass::InputImagePointer inputPtr =
67  const_cast<TInputImage *>(this->GetInput());
68  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
69 
70  if (!inputPtr || !outputPtr)
71  {
72  return;
73  }
74  // get a copy of the input requested region (should equal the output
75  // requested region)
76  typename TInputImage::RegionType inputRequestedRegion;
77  inputRequestedRegion = inputPtr->GetRequestedRegion();
78 
79  // pad the input requested region by the operator radius
80  InputImageSizeType maxRad;
81  maxRad[0] = m_Radius[0] + vcl_abs(m_Offset[0]);
82  maxRad[1] = m_Radius[1] + vcl_abs(m_Offset[1]);
83  inputRequestedRegion.PadByRadius(maxRad);
84 
85  // crop the input requested region at the input's largest possible region
86  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
87  {
88  inputPtr->SetRequestedRegion(inputRequestedRegion);
89  return;
90  }
91  else
92  {
93  // Couldn't crop the region (requested region is outside the largest
94  // possible region). Throw an exception.
95 
96  // store what we tried to request (prior to trying to crop)
97  inputPtr->SetRequestedRegion(inputRequestedRegion);
98 
99  // build an exception
100  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
101  std::ostringstream msg;
102  msg << this->GetNameOfClass()
103  << "::GenerateInputRequestedRegion()";
104  e.SetLocation(msg.str().c_str());
105  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
106  e.SetDataObject(inputPtr);
107  throw e;
108  }
109 }
110 
114 template <class TInputImage, class TOutputImage, class TFunction>
115 void
117 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
118 {
121  // We use dynamic_cast since inputs are stored as DataObjects. The
122  // ImageToImageFilter::GetInput(int) always returns a pointer to a
123  // TInputImage so it cannot be used for the second input.
124  InputImagePointer inputPtr = dynamic_cast<const TInputImage*>(ProcessObjectType::GetInput(0));
125  OutputImagePointer outputPtr = this->GetOutput(0);
126 
128 
129  // Neighborhood+offset iterator
130  RadiusType rOff;
131  rOff[0] = m_Radius[0] + vcl_abs(m_Offset[0]);
132  rOff[1] = m_Radius[1] + vcl_abs(m_Offset[1]);
133  NeighborhoodIteratorType neighInputOffIt;
134  // Find the data-set boundary "faces"
137  faceListOff = bCOff(inputPtr, outputRegionForThread, rOff);
139 
140  // support progress methods/callbacks
141  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
142 
143  // Process each of the boundary faces. These are N-d regions which border
144  // the edge of the buffer.
145  fitOff = faceListOff.begin();
146 
147  while (fitOff != faceListOff.end())
148  {
149  // Neighborhood+offset iterator
150  neighInputOffIt = itk::ConstNeighborhoodIterator<TInputImage>(rOff, inputPtr, *fitOff);
151  neighInputOffIt.OverrideBoundaryCondition(&nbcOff);
152  neighInputOffIt.GoToBegin();
153 
154  outputIt = itk::ImageRegionIterator<TOutputImage>(outputPtr, *fitOff);
155 
156  while (!outputIt.IsAtEnd())
157  {
158 
159  outputIt.Set(m_FunctorList[threadId](neighInputOffIt.GetNeighborhood()));
160 
161  ++neighInputOffIt;
162  ++outputIt;
163  progress.CompletedPixel();
164  }
165  ++fitOff;
166  }
167 }
168 
169 } // end namespace otb
170 
171 #endif

Generated at Sat Mar 8 2014 16:23:27 for Orfeo Toolbox with doxygen 1.8.3.1