Commit a01aa690 authored by Mathieu Deltorre's avatar Mathieu Deltorre
Browse files

Ajout d'un watcher de progression de l'avancement d'un filtrer en mode "console"

parent 1132f97e
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
Some parts of this code are derived from ITK. See ITKCopyright.txt
for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "otbStandardFilterWatcher.h"
namespace otb
{
StandardFilterWatcher
::StandardFilterWatcher(itk::ProcessObject* process,
const char *comment)
{
// Initialize state
m_Process = process;
m_Comment = comment;
m_StarsCount = 50;
// Create a series of commands
m_StartFilterCommand = CommandType::New();
m_EndFilterCommand = CommandType::New();
m_ProgressFilterCommand = CommandType::New();
// Assign the callbacks
m_StartFilterCommand->SetCallbackFunction(this,
&StandardFilterWatcher::StartFilter);
m_EndFilterCommand->SetCallbackFunction(this,
&StandardFilterWatcher::EndFilter);
m_ProgressFilterCommand->SetCallbackFunction(this,
&StandardFilterWatcher::ShowProgress);
// Add the commands as observers
m_StartTag = m_Process->AddObserver(itk::StartEvent(),
m_StartFilterCommand);
m_EndTag = m_Process->AddObserver(itk::EndEvent(),
m_EndFilterCommand);
m_ProgressTag = m_Process->AddObserver(itk::ProgressEvent(),
m_ProgressFilterCommand);
}
StandardFilterWatcher
::StandardFilterWatcher( const StandardFilterWatcher& watch)
{
// Remove any observers we have on the old process object
if (m_Process)
{
if (m_StartFilterCommand)
{
m_Process->RemoveObserver(m_StartTag);
}
if (m_EndFilterCommand)
{
m_Process->RemoveObserver(m_EndTag);
}
if (m_ProgressFilterCommand)
{
m_Process->RemoveObserver(m_ProgressTag);
}
}
// Initialize state
m_TimeProbe = watch.m_TimeProbe;
m_Process = watch.m_Process;
m_Comment = watch.m_Comment;
m_StarsCount = watch.m_StarsCount;
m_StartTag = 0;
m_EndTag = 0;
m_ProgressTag = 0;
// Create a series of commands
if (m_Process)
{
m_StartFilterCommand = CommandType::New();
m_EndFilterCommand = CommandType::New();
m_ProgressFilterCommand = CommandType::New();
// Assign the callbacks
m_StartFilterCommand->SetCallbackFunction(this,
&StandardFilterWatcher::StartFilter);
m_EndFilterCommand->SetCallbackFunction(this,
&StandardFilterWatcher::EndFilter);
m_ProgressFilterCommand->SetCallbackFunction(this,
&StandardFilterWatcher::ShowProgress);
// Add the commands as observers
m_StartTag = m_Process->AddObserver(itk::StartEvent(), m_StartFilterCommand);
m_EndTag = m_Process->AddObserver(itk::EndEvent(), m_EndFilterCommand);
m_ProgressTag
= m_Process->AddObserver(itk::ProgressEvent(), m_ProgressFilterCommand);
}
}
void
StandardFilterWatcher
::operator=(const StandardFilterWatcher &watch)
{
// Remove any observers we have on the old process object
if (m_Process)
{
if (m_StartFilterCommand)
{
m_Process->RemoveObserver(m_StartTag);
}
if (m_EndFilterCommand)
{
m_Process->RemoveObserver(m_EndTag);
}
if (m_ProgressFilterCommand)
{
m_Process->RemoveObserver(m_ProgressTag);
}
}
// Initialize state
m_TimeProbe = watch.m_TimeProbe;
m_Process = watch.m_Process;
m_Comment = watch.m_Comment;
m_StarsCount = watch.m_StarsCount;
m_StartTag = 0;
m_EndTag = 0;
m_ProgressTag = 0;
// Create a series of commands
if (m_Process)
{
m_StartFilterCommand = CommandType::New();
m_EndFilterCommand = CommandType::New();
m_ProgressFilterCommand = CommandType::New();
// Assign the callbacks
m_StartFilterCommand->SetCallbackFunction(this,
&StandardFilterWatcher::StartFilter);
m_EndFilterCommand->SetCallbackFunction(this,
&StandardFilterWatcher::EndFilter);
m_ProgressFilterCommand->SetCallbackFunction(this,
&StandardFilterWatcher::ShowProgress);
// Add the commands as observers
m_StartTag = m_Process->AddObserver(itk::StartEvent(), m_StartFilterCommand);
m_EndTag = m_Process->AddObserver(itk::EndEvent(), m_EndFilterCommand);
m_ProgressTag
= m_Process->AddObserver(itk::ProgressEvent(), m_ProgressFilterCommand);
}
}
StandardFilterWatcher
::~StandardFilterWatcher()
{
// Remove any observers we have on the old process object
if (m_Process)
{
if (m_StartTag)
{
m_Process->RemoveObserver(m_StartTag);
}
if (m_EndTag)
{
m_Process->RemoveObserver(m_EndTag);
}
if (m_ProgressTag)
{
m_Process->RemoveObserver(m_ProgressTag);
}
}
}
void
StandardFilterWatcher
::ShowProgress()
{
if (m_Process)
{
int progressPercent = static_cast<int>(m_Process->GetProgress()*100);
std::string stars(static_cast<int>(m_Process->GetProgress()*m_StarsCount),'*');
std::string blanks(static_cast<int>(m_StarsCount - m_Process->GetProgress()*m_StarsCount),' ');
std::cout << "\r" << progressPercent << "% [" << stars << blanks << "]" << std::flush;
}
}
void
StandardFilterWatcher
::StartFilter()
{
m_TimeProbe.Start();
std::cout << (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None")
<< " \"" << m_Comment << "\" " << std::endl;
std::cout << "Prgress task : " << std::flush;
}
void
StandardFilterWatcher
::EndFilter()
{
m_TimeProbe.Stop();
std::cout << std::endl << "Filter took "
<< m_TimeProbe.GetMeanTime()
<< " seconds.";
}
} // end namespace otb
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
Some parts of this code are derived from ITK. See ITKCopyright.txt
for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbStandardFilterWatcher_h
#define __otbStandardFilterWatcher_h
#include "itkCommand.h"
#include "itkProcessObject.h"
#include "itkTimeProbe.h"
namespace otb
{
/** \class StandardFilterWatcher
* \brief This class shows the percentage progress execution
* of the pipeline filtering process
*
* This class is based on oberservers desgin patter
* Abstract class ProcessObject is the subject
* Event are oberservers
*
* \example Example usage
*
* \code
* typedef itk::BinaryThresholdImageFilter<ImageType> FilterType;
* FilterType::Pointer thresholdFilter = FilterType::New();
*
* StandardFilterWatcher watcher(thresholdFilter, "Threshold");
* \endcode
*
* \see itk::SimpleFilterWatcher
* \see otb::fltkFilterWatcher
*/
class ITK_EXPORT StandardFilterWatcher
{
public:
/** Constructor. Takes a ProcessObject to monitor and an optional
* comment string that is prepended to each event message. */
StandardFilterWatcher(itk::ProcessObject* process,
const char *comment="");
/** Copy constructor */
StandardFilterWatcher(const StandardFilterWatcher&);
/** operator= */
void operator=(const StandardFilterWatcher& );
/** Destructor. */
virtual ~StandardFilterWatcher();
/** Methods to access member data */
/** Get a pointer to the process object being watched. */
itk::ProcessObject *GetProcess () {return m_Process.GetPointer();}
/** Get the comment for the watcher. */
std::string GetComment() {return m_Comment;}
/** Get a reference to the TimeProbe */
itk::TimeProbe &GetTimeProbe() {return m_TimeProbe;}
/** Get/Set number of stars */
void SetStars( int count ) { m_StarsCount = count;}
const int& GetStars() const { return m_StarsCount;}
protected:
/** Callback method to show the ProgressEvent */
virtual void ShowProgress();
/** Callback method to show the StartEvent */
virtual void StartFilter();
/** Callback method to show the EndEvent */
virtual void EndFilter();
private:
/** Computing time */
itk::TimeProbe m_TimeProbe;
/** Associated comment */
std::string m_Comment;
/** Stars coutning */
int m_StarsCount;
/** Abstract process object */
itk::ProcessObject::Pointer m_Process;
/** Internal type */
typedef itk::SimpleMemberCommand<StandardFilterWatcher> CommandType;
/** Start filter callback */
CommandType::Pointer m_StartFilterCommand;
/** End filter callback */
CommandType::Pointer m_EndFilterCommand;
/** Progress filter callback */
CommandType::Pointer m_ProgressFilterCommand;
/** Start oberserver */
unsigned long m_StartTag;
/** End observer */
unsigned long m_EndTag;
/** Progress observer */
unsigned long m_ProgressTag;
};
} // end namespace otb
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment