Orfeo Toolbox  4.0
itkProgressAccumulator.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #include "itkProgressAccumulator.h"
19 
20 namespace itk
21 {
24 {
25  m_MiniPipelineFilter = 0;
26 
27  // Initialize the progress values
28  m_AccumulatedProgress = 0.0f;
29  m_BaseAccumulatedProgress = 0.0f;
30 
31  // Create a member command
32  m_CallbackCommand = CommandType::New();
33  m_CallbackCommand->SetCallbackFunction(this, &Self::ReportProgress);
34 }
35 
38 {
39  UnregisterAllFilters();
40 }
41 
42 void
45 {
46  // Observe the filter
47  unsigned long progressTag =
48  filter->AddObserver(ProgressEvent(), m_CallbackCommand);
49  unsigned long startTag =
50  filter->AddObserver(StartEvent(), m_CallbackCommand);
51 
52  // Create a record for the filter
53  struct FilterRecord record;
54 
55  record.Filter = filter;
56  record.Weight = weight;
57  record.ProgressObserverTag = progressTag;
58  record.StartObserverTag = startTag;
59 
60  // Add the record to the list
61  m_FilterRecord.push_back(record);
62 }
63 
64 void
67 {
68  // The filters should no longer be observing us
69  FilterRecordVector::iterator it;
70 
71  for ( it = m_FilterRecord.begin(); it != m_FilterRecord.end(); ++it )
72  {
73  it->Filter->RemoveObserver(it->ProgressObserverTag);
74  it->Filter->RemoveObserver(it->StartObserverTag);
75  }
76 
77  // Clear the filter array
78  m_FilterRecord.clear();
79 
80  // Reset the accumulated progress
81  m_AccumulatedProgress = 0.0f;
82  m_BaseAccumulatedProgress = 0.0f;
83 }
84 
85 #if ! defined ( ITK_FUTURE_LEGACY_REMOVE )
86 void
89 {
90  // Reset the accumulated progress
91  m_AccumulatedProgress = 0.0f;
92  m_BaseAccumulatedProgress = 0.0f;
93 
94  // Reset each of the individial progress meters
95  FilterRecordVector::iterator it;
96  for ( it = m_FilterRecord.begin(); it != m_FilterRecord.end(); ++it )
97  {
98  it->Filter->UpdateProgress(0.0f);
99  }
100 }
101 #endif
102 
103 #if ! defined ( ITK_FUTURE_LEGACY_REMOVE )
104 void
107 {
108  // Do nothing. After all, this method is deprecated.
109 }
110 #endif
111 
112 void
114 ::ReportProgress(Object *who, const EventObject & event)
115 {
116  ProgressEvent pe;
117  StartEvent se;
118 
119  if ( typeid( event ) == typeid( pe ) )
120  {
121  // Start the progress from the progress accumulated so far.
122  m_AccumulatedProgress = m_BaseAccumulatedProgress;
123 
124  // Add up the new progress from different filters.
125  FilterRecordVector::iterator it;
126  for ( it = m_FilterRecord.begin(); it != m_FilterRecord.end(); ++it )
127  {
128  m_AccumulatedProgress += it->Filter->GetProgress() * it->Weight;
129  }
130 
131  // Update the progress of the client mini-pipeline filter
132  m_MiniPipelineFilter->UpdateProgress(m_AccumulatedProgress);
133 
134  // check for abort
135  if ( m_MiniPipelineFilter->GetAbortGenerateData() )
136  {
137  // Abort the filter that is reporting progress
138  FilterRecordVector::iterator fit;
139  for ( fit = m_FilterRecord.begin(); fit != m_FilterRecord.end(); ++fit )
140  {
141  if ( who == fit->Filter )
142  {
143  fit->Filter->AbortGenerateDataOn();
144  }
145  }
146  }
147  }
148  else if ( typeid( event ) == typeid( se ) )
149  {
150  // When a filter is restarted, we can capture the progress it has made so far and add it
151  // to the accumulated value.
152  // This is especially helpful when streaming is used since the filter is restarted multiple
153  // times for different regions of the input.
154  // By capturing the start event, it is no longer necessary for filters that use the ProgressAccumulator
155  // to explicitly call ResetFilterProgressAndKeepAccumulatedProgress().
156 
157  FilterRecordVector::iterator it;
158  for ( it = m_FilterRecord.begin(); it != m_FilterRecord.end(); ++it )
159  {
160  if( who == it->Filter )
161  {
162  // On a start event, we need to capture the accumulated progress for this filter
163  // and then reset this filter's progress.
164  // It is not necessary to call UpdateProgress(0.0f) explicitly on the filter because this is done
165  // automatically when the filter is restarted.
166  m_BaseAccumulatedProgress += it->Filter->GetProgress() * it->Weight;
167  }
168  }
169  }
170 }
171 
173 ::PrintSelf(std::ostream & os, Indent indent) const
174 {
175  Superclass::PrintSelf(os, indent);
176 
177  if ( m_MiniPipelineFilter )
178  {
179  os << indent << m_MiniPipelineFilter << std::endl;
180  }
181  os << indent << m_AccumulatedProgress << std::endl;
182  os << indent << m_BaseAccumulatedProgress << std::endl;
183 }
184 } // End namespace itk

Generated at Sat Mar 8 2014 15:23:52 for Orfeo Toolbox with doxygen 1.8.3.1