Orfeo Toolbox  4.0
otbAddCarvingPathFilter.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 
19 #ifndef __otbAddCarvingPathFilter_txx
20 #define __otbAddCarvingPathFilter_txx
21 
23 #include "itkMacro.h"
24 
25 namespace otb
26 {
30 template <class TInputImage, class TInputPath, class TOutputImage>
33 {
34  m_Value = static_cast<OutputImagePixelType>(255.0);
35  m_Direction = 0;
36 }
40 template <class TInputImage, class TInputPath, class TOutputImage>
41 inline void
43 ::SetInputPath(const TInputPath * path)
44 {
45  this->SetPathInput(path);
46 }
47 template <class TInputImage, class TInputPath, class TOutputImage>
48 const TInputPath *
51 {
52  return this->GetPathInput();
53 }
54 
55 template <class TInputImage, class TInputPath, class TOutputImage>
56 void
59 {
60 
61  const InputImageType * inputImage = this->GetImageInput();
62  const InputPathType * inputPath = this->GetPathInput();
63  OutputImageType * outputImage = this->GetOutput();
64 
65  outputImage->SetBufferedRegion(outputImage->GetRequestedRegion());
66  outputImage->Allocate();
67 
68  InputIteratorType inputIterator(inputImage,
69  inputImage->GetLargestPossibleRegion());
70  OutputIteratorType outputIterator(outputImage,
71  outputImage->GetLargestPossibleRegion());
72 
73  unsigned int dir0;
74  unsigned int dir1;
75 
76  if (m_Direction == 0)
77  {
78  dir0 = 0;
79  dir1 = 1;
80  }
81  else
82  {
83  dir0 = 1;
84  dir1 = 0;
85  }
86 
87  inputIterator.SetFirstDirection(dir0);
88  inputIterator.SetSecondDirection(dir1);
89  inputIterator.GoToBegin();
90  outputIterator.SetFirstDirection(dir0);
91  outputIterator.SetSecondDirection(dir1);
92  outputIterator.GoToBegin();
93 
94  int line = -1;
95  typename InputImageType::IndexType index;
96  typename InputPathType::VertexListType::ConstPointer vertexList = inputPath->GetVertexList();
97  typename InputPathType::VertexListType::ConstIterator pathIterator;
98  typename InputImageType::IndexType indexToAdd;
100 
101  typename InputImageType::SizeType inputSize;
102  inputSize = inputImage->GetLargestPossibleRegion().GetSize();
103 
104  //go to the end of the path
105  pathIterator = vertexList->Begin();
106  while (pathIterator != vertexList->End())
107  {
108  ++pathIterator;
109  }
110  --pathIterator;
111 
112  while (!outputIterator.IsAtEnd())
113  {
114  while (!outputIterator.IsAtEndOfSlice())
115  {
116  bool flag = false;
117  while (!outputIterator.IsAtEndOfLine())
118  {
119  index = outputIterator.GetIndex();
120 
121  if (index[dir1] != line)
122  {
123  line = index[dir1];
124  typename InputImageType::PointType tmpIndex;
125  inputImage->TransformContinuousIndexToPhysicalPoint(pathIterator.Value(), tmpIndex);
126  inputImage->TransformPhysicalPointToIndex(tmpIndex, indexToAdd);
127  if (pathIterator != vertexList->Begin())
128  {
129  --pathIterator;
130  }
131  if (index[dir1] != indexToAdd[dir1])
132  {
133  itkExceptionMacro(<< "Error : " << index[dir1] << " , " << indexToAdd[dir1]);
134  }
135  }
136 
137  if ((flag == true)
138  || ((index[dir0] != indexToAdd[dir0])
139  && (index[dir0] < static_cast<IndexValueType>(inputSize[dir0]))))
140  {
141  outputIterator.Set(inputIterator.Get());
142  ++inputIterator;
143  }
144  else
145  {
146  flag = true;
148  int n = 0;
149  InputIteratorType tmpIterator = inputIterator;
150  --tmpIterator;
151  if (!tmpIterator.IsAtReverseEndOfLine())
152  {
153  newValue = tmpIterator.Get();
154  ++n;
155  }
156  tmpIterator = inputIterator;
157  ++tmpIterator;
158  if (!tmpIterator.IsAtEndOfLine())
159  {
160  newValue += tmpIterator.Get();
161  ++n;
162  }
163  assert(n != 0);
164  newValue /= n;
165  outputIterator.Set(newValue);
166  }
167  ++outputIterator;
168 
169  }
170 
171  if ((outputIterator.GetIndex())[dir0] != (inputIterator.GetIndex())[dir0] + 1)
172  {
173  itkExceptionMacro(<< "Error 2: " << (outputIterator.GetIndex())[dir0] << " , "
174  << (inputIterator.GetIndex())[dir0]);
175  }
176  inputIterator.NextLine();
177  outputIterator.NextLine();
178  }
179  inputIterator.NextSlice();
180  outputIterator.NextSlice();
181  }
182 
183 }
184 
188 template <class TInputImage, class TInputPath, class TOutputImage>
189 void
191 ::PrintSelf(std::ostream& os, itk::Indent indent) const
192 {
193  Superclass::PrintSelf(os, indent);
194  os << indent << "Path Value: " << m_Value << std::endl;
195 }
196 
197 template <class TInputImage, class TInputPath, class TOutputImage>
198 void
201 {
202  // call the superclass' implementation of this method
203  Superclass::GenerateOutputInformation();
204 
205  // get pointers to the input and output
206  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
207  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
208 
209  unsigned int dir0;
210  //unsigned int dir1;
211 
212  if (m_Direction == 0)
213  {
214  dir0 = 0;
215  //dir1 = 1;
216  }
217  else
218  {
219  dir0 = 1;
220  //dir1 = 0;
221  }
222 
223  // we need to compute the output spacing, the output image size, and the
224  // output image start index
225  const typename TInputImage::SpacingType&
226  inputSpacing = inputPtr->GetSpacing();
227  const typename TInputImage::SizeType& inputSize
228  = inputPtr->GetLargestPossibleRegion().GetSize();
229  const typename TInputImage::IndexType& inputStartIndex
230  = inputPtr->GetLargestPossibleRegion().GetIndex();
231 
232  typename TOutputImage::SpacingType outputSpacing;
233  typename TOutputImage::SizeType outputSize;
234  typename TOutputImage::IndexType outputStartIndex;
235 
236  for (unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
237  {
238  outputSpacing[i] = inputSpacing[i];
239  outputSize[i] = (unsigned int) (inputSize[i]);
240  outputStartIndex[i] = inputStartIndex[i];
241  }
242  // we remove one column of the image
243  outputSize[dir0] = inputSize[dir0] + 1;
244 
245  outputPtr->SetSpacing(outputSpacing);
246 
247  typename TOutputImage::RegionType outputLargestPossibleRegion;
248  outputLargestPossibleRegion.SetSize(outputSize);
249  outputLargestPossibleRegion.SetIndex(outputStartIndex);
250 
251  outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
252 }
253 
254 template <class TInputImage, class TInputPath, class TOutputImage>
255 void
258 {
259  // call the superclass' implementation of this method
260  Superclass::GenerateInputRequestedRegion();
261  // get pointers to the input and output
262  InputImagePointer inputPtr =
263  const_cast<InputImageType *>(this->GetInput());
264  if (!inputPtr)
265  {
266  itkExceptionMacro(<< "Input has not been set.");
267  }
268 
269  inputPtr->SetRequestedRegion(inputPtr->GetLargestPossibleRegion());
270 
271 }
272 
273 } // end namespace otb
274 
275 #endif

Generated at Sat Mar 8 2014 15:49:38 for Orfeo Toolbox with doxygen 1.8.3.1