otbImageToPointSetFilter.txx 8.53 KB
Newer Older
Patrick Imbo's avatar
Patrick Imbo committed
1
2
/*=========================================================================

Patrick Imbo's avatar
nomsg    
Patrick Imbo committed
3
4
5
6
7
8
9
10
11
12
  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


13
14
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Patrick Imbo's avatar
nomsg    
Patrick Imbo committed
15
     PURPOSE.  See the above copyright notices for more information.
Patrick Imbo's avatar
Patrick Imbo committed
16
17

=========================================================================*/
18
19
#ifndef __otbImageToPointSetFilter_txx
#define __otbImageToPointSetFilter_txx
Patrick Imbo's avatar
Patrick Imbo committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include "otbImageToPointSetFilter.h"


namespace otb
{

/**
 *
 */
template <class TInputImage, class TOutputPointSet>
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::ImageToPointSetFilter()
{
Romain Garrigues's avatar
Romain Garrigues committed
34
  this->ProcessObjectType::SetNumberOfRequiredInputs(1);
Patrick Imbo's avatar
Patrick Imbo committed
35
36

  OutputPointSetPointer output
37
  = dynamic_cast<OutputPointSetType*>(this->MakeOutput(0).GetPointer());
Patrick Imbo's avatar
Patrick Imbo committed
38

Romain Garrigues's avatar
Romain Garrigues committed
39
40
  ProcessObjectType::SetNumberOfRequiredOutputs(1);
  ProcessObjectType::SetNthOutput(0, output.GetPointer());
Patrick Imbo's avatar
Patrick Imbo committed
41
42
43
44
45
46
47

}

/**
 *
 */
template <class TInputImage, class TOutputPointSet>
48
void
Patrick Imbo's avatar
Patrick Imbo committed
49
50
51
52
53
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::SetInput(unsigned int idx,const InputImageType *input)
{
  // process object is not const-correct, the const_cast
  // is required here.
54
  this->ProcessObjectType::SetNthInput(idx,
55
                                       const_cast< InputImageType * >(input) );
Patrick Imbo's avatar
Patrick Imbo committed
56
}
57

58
59
60
61
62
63
64
65
66
67
68
/**
 *
 */
template <class TInputImage, class TOutputPointSet>
void
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::SetInput(const InputImageType *input)
{
  // process object is not const-correct, the const_cast
  // is required here.
  this->ProcessObjectType::SetNthInput(0,
69
                                       const_cast< InputImageType * >(input) );
70
71
}

Patrick Imbo's avatar
Patrick Imbo committed
72
73
74
75
76
77
/**
 *
 */
template <class TInputImage, class TOutputPointSet>
const typename ImageToPointSetFilter<TInputImage,TOutputPointSet>::InputImageType *
ImageToPointSetFilter<TInputImage,TOutputPointSet>
78
::GetInput(unsigned int idx)
Patrick Imbo's avatar
Patrick Imbo committed
79
{
80

Patrick Imbo's avatar
Patrick Imbo committed
81
  return dynamic_cast<const InputImageType*>
82
         (this->ProcessObjectType::GetInput(idx));
Patrick Imbo's avatar
Patrick Imbo committed
83
84
}

85
86
87
88
89
90
/**
 *
 */
template <class TInputImage, class TOutputPointSet>
const typename ImageToPointSetFilter<TInputImage,TOutputPointSet>::InputImageType *
ImageToPointSetFilter<TInputImage,TOutputPointSet>
91
::GetInput(void)
92
{
93
  if (this->GetNumberOfInputs() < 1)
94
    return 0;
95

96
  return dynamic_cast<const InputImageType*>
97
         (this->ProcessObjectType::GetInput(0));
98
99
}

Patrick Imbo's avatar
Patrick Imbo committed
100
101
102
103
/**
 *
 */
template <class TInputImage, class TOutputPointSet>
104
void
Patrick Imbo's avatar
Patrick Imbo committed
105
106
107
108
109
110
111
112
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
  Superclass::PrintSelf(os,indent);
}

/**
 * copy information from first input to all outputs
113
 * This is a void implementation to prevent the
Patrick Imbo's avatar
Patrick Imbo committed
114
115
116
 * ProcessObject version to be called
 */
template <class TInputImage, class TOutputPointSet>
117
void
Patrick Imbo's avatar
Patrick Imbo committed
118
119
120
121
122
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::GenerateOutputInformation()
{
}

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * GenerateData
 */
template <class TInputImage, class TOutputPointSet>
void
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::GenerateData(void)
{
  // Call a method that can be overridden by a subclass to perform
  // some calculations prior to splitting the main computations into
  // separate threads
  this->BeforeThreadedGenerateData();

  // Set up the multithreaded processing
  ThreadStruct str;
  str.Filter = this;

  // Initializing object per thread
141
142
143
  typename PointsContainerType::Pointer defaultPointsContainer = PointsContainerType::New();
  this->m_PointsContainerPerThread
        = OutputPointsContainerForThreadType(this->GetNumberOfThreads(),defaultPointsContainer);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172


  // Setting up multithreader
  this->GetMultiThreader()->SetNumberOfThreads(this->GetNumberOfThreads());
  this->GetMultiThreader()->SetSingleMethod(this->ThreaderCallback, &str);

  // multithread the execution
  this->GetMultiThreader()->SingleMethodExecute();

  // Call a method that can be overridden by a subclass to perform
  // some calculations after all the threads have completed
  this->AfterThreadedGenerateData();

}

template <class TInputImage, class TOutputPointSet>
void
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::BeforeThreadedGenerateData(void)
{
//  this->AllocateOutputs();
}

template <class TInputImage, class TOutputPointSet>
void
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::AfterThreadedGenerateData(void)
{
  // copy the lists to the output
173
174
175
176
  PointsContainerType * outputPointsContainer = this->GetOutput()->GetPoints();
  outputPointsContainer->Initialize();
  typedef typename PointsContainerType::ConstIterator OutputPointsContainerIterator;
  for (unsigned int i=0; i< this->m_PointsContainerPerThread.size(); ++i)
177
  {
178
    if (this->m_PointsContainerPerThread[i].IsNotNull())
179
    {
180
181
      for (OutputPointsContainerIterator it = this->m_PointsContainerPerThread[i]->Begin();
           it != this->m_PointsContainerPerThread[i]->End();
182
183
           ++it)
      {
184
        outputPointsContainer->push_back(it.Value());
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
      }
    }
  }

}

template <class TInputImage, class TOutputPointSet>
void
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::ThreadedGenerateData(const InputImageRegionType&, int)
{
  // The following code is equivalent to:
  // itkExceptionMacro("subclass should override this method!!!");
  // The ExceptionMacro is not used because gcc warns that a
  // 'noreturn' function does return
  itk::OStringStream message;
  message << "itk::ERROR: " << this->GetNameOfClass()
  << "(" << this << "): " << "Subclass should override this method!!!";
  itk::ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(),ITK_LOCATION);
  throw e_;

}

template <class TInputImage, class TOutputPointSet>
ITK_THREAD_RETURN_TYPE
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::ThreaderCallback( void *arg )
{
  ThreadStruct *str;
  int total, threadId, threadCount;

  threadId = ((itk::MultiThreader::ThreadInfoStruct *)(arg))->ThreadID;
  threadCount = ((itk::MultiThreader::ThreadInfoStruct *)(arg))->NumberOfThreads;
  str = (ThreadStruct *)(((itk::MultiThreader::ThreadInfoStruct *)(arg))->UserData);

  // execute the actual method with appropriate output region
  // first find out how many pieces extent can be split into.
  typename TInputImage::RegionType splitRegion;
  total = str->Filter->SplitRequestedRegion(threadId, threadCount,
                                            splitRegion);

  if (threadId < total)
  {
      str->Filter->ThreadedGenerateData(splitRegion, threadId);
  }
  // else
  //   {
  //   otherwise don't use this thread. Sometimes the threads dont
  //   break up very well and it is just as efficient to leave a
  //   few threads idle.
  //   }

  return ITK_THREAD_RETURN_VALUE;
}

template <class TInputImage, class TOutputPointSet>
int
ImageToPointSetFilter<TInputImage,TOutputPointSet>
::SplitRequestedRegion(int i, int num, InputImageRegionType& splitRegion)
{
  // Get the output pointer
  typename InputImageType::ConstPointer inputPtr = this->GetInput();
  const typename TInputImage::SizeType& requestedRegionSize
    = inputPtr->GetLargestPossibleRegion().GetSize();

  int splitAxis;
  typename TInputImage::IndexType splitIndex;
  typename TInputImage::SizeType splitSize;

  // Initialize the splitRegion to the output requested region
  splitRegion = inputPtr->GetLargestPossibleRegion();
  splitIndex = splitRegion.GetIndex();
  splitSize = splitRegion.GetSize();

  // split on the outermost dimension available
  splitAxis = inputPtr->GetImageDimension() - 1;
  while (requestedRegionSize[splitAxis] == 1)
    {
    --splitAxis;
    if (splitAxis < 0)
      { // cannot split
      itkDebugMacro("  Cannot Split");
      return 1;
      }
    }

  // determine the actual number of pieces that will be generated
  typename TInputImage::SizeType::SizeValueType range = requestedRegionSize[splitAxis];
  int valuesPerThread = (int)::vcl_ceil(range/(double)num);
  int maxThreadIdUsed = (int)::vcl_ceil(range/(double)valuesPerThread) - 1;

  // Split the region
  if (i < maxThreadIdUsed)
    {
    splitIndex[splitAxis] += i*valuesPerThread;
    splitSize[splitAxis] = valuesPerThread;
    }
  if (i == maxThreadIdUsed)
    {
    splitIndex[splitAxis] += i*valuesPerThread;
    // last thread needs to process the "rest" dimension being split
    splitSize[splitAxis] = splitSize[splitAxis] - i*valuesPerThread;
    }

  // set the split region ivars
  splitRegion.SetIndex( splitIndex );
  splitRegion.SetSize( splitSize );

  itkDebugMacro("  Split Piece: " << splitRegion );

  return maxThreadIdUsed + 1;
}
Patrick Imbo's avatar
Patrick Imbo committed
297
298
299
300

} // end namespace otb

#endif