Commit cc8db4e7 authored by Julien Michel's avatar Julien Michel

ENH: Adding a new watcher designed to watch writers.

TEST: Corrected the FltkFilterWatcher test.
parent f38f45ee
/*=========================================================================
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 "otbStandardWriterWatcher.h"
namespace otb
{
StandardWriterWatcher
::StandardWriterWatcher(itk::ProcessObject* process,
const char *comment)
: WriterWatcherBase(process, comment)
{
m_StarsCount = 50;
}
StandardWriterWatcher
::StandardWriterWatcher( const StandardWriterWatcher& watch)
{
// Initialize state
m_StarsCount = watch.m_StarsCount;
}
void
StandardWriterWatcher
::operator=(const StandardWriterWatcher &watch)
{
// Initialize state
m_StarsCount = watch.m_StarsCount;
}
void
StandardWriterWatcher
::ShowWriterProgress()
{
this->ShowProgress();
}
void
StandardWriterWatcher
::ShowFilterProgress()
{
this->ShowProgress();
}
void
StandardWriterWatcher
::ShowProgress()
{
itk::OStringStream oss;
oss.str("");
oss<<"\r";
if (m_SourceProcess)
{
double progress = m_SourceProcess->GetProgress();
int progressPercent = static_cast<int>(progress*100);
std::string stars(static_cast<int>(progress*m_StarsCount),'*');
std::string blanks(m_StarsCount - stars.length(),' ');
oss << "Current Tile: ";
if(progressPercent<10)
{
oss<<" ";
}
if(progressPercent<100)
{
oss<<" ";
}
oss<<progressPercent << "% [" << stars << blanks << "] ";
}
if (m_Process)
{
double progress = m_Process->GetProgress();
int progressPercent = static_cast<int>(progress*100);
std::string stars(static_cast<int>(progress*m_StarsCount),'*');
std::string blanks(m_StarsCount - stars.length(),' ');
oss << "Writing: ";
if(progressPercent<10)
{
oss<<" ";
}
if(progressPercent<100)
{
oss<<" ";
}
oss<< progressPercent << "% [" << stars << blanks << "]" << std::flush;
}
std::cout<<oss.str();
}
void
StandardWriterWatcher
::StartWriter()
{
m_TimeProbe.Start();
std::cout<<"Writing task: "<< " \"" << m_Comment << "\" " << std::endl;
std::cout << "Writer type: "<<(m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None")<< std::endl;
std::cout << "Filter type: "<<(m_SourceProcess.GetPointer() ? m_SourceProcess->GetNameOfClass() : "None")<< std::endl;
}
void
StandardWriterWatcher
::EndWriter()
{
m_TimeProbe.Stop();
std::cout << std::endl << "Writing task took "
<< m_TimeProbe.GetMeanTime()
<< " seconds." << std::endl;
}
} // 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 __otbStandardWriterWatcher_h
#define __otbStandardWriterWatcher_h
#include "otbWriterWatcherBase.h"
namespace otb
{
/** \class StandardWriterWatcher
* \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
*
* Usage example:
*
* \code
* typedef itk::BinaryThresholdImageWriter<ImageType> WriterType;
* WriterType::Pointer thresholdWriter = WriterType::New();
*
* StandardWriterWatcher watcher(thresholdWriter, "Threshold");
* \endcode
*
* \see itk::SimpleWriterWatcher
* \see otb::fltkWriterWatcher
*/
class ITK_EXPORT StandardWriterWatcher : public otb::WriterWatcherBase
{
public:
/** Constructor. Takes a ProcessObject to monitor and an optional
* comment string that is prepended to each event message. */
StandardWriterWatcher(itk::ProcessObject* process,
const char *comment="");
/** Copy constructor */
StandardWriterWatcher(const StandardWriterWatcher&);
/** operator= */
void operator=(const StandardWriterWatcher& );
/** 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 ShowWriterProgress();
/** Callback method to show the StartEvent */
virtual void StartWriter();
/** Callback method to show the EndEvent */
virtual void EndWriter();
/** Callback method to show the ProgressEvent */
virtual void ShowFilterProgress();
/** Callback method to show the StartEvent */
virtual void StartFilter(){};
/** Callback method to show the EndEvent */
virtual void EndFilter(){};
/** This is the method invoked by ShowFilterProgress() and ShowWriterProgress() */
virtual void ShowProgress();
private:
/** Stars coutning */
int m_StarsCount;
};
} // end namespace otb
#endif
/*=========================================================================
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 "otbWriterWatcherBase.h"
namespace otb
{
WriterWatcherBase
::WriterWatcherBase()
{
// Initialize state
m_Comment = "Not watching an object";
m_Process = 0;
}
WriterWatcherBase
::WriterWatcherBase(itk::ProcessObject* process,
const char *comment)
{
// Initialize state
m_Process = process;
m_Comment = comment;
// Create a series of commands
m_StartWriterCommand = CommandType::New();
m_EndWriterCommand = CommandType::New();
m_ProgressWriterCommand = CommandType::New();
m_StartFilterCommand = CommandType::New();
m_EndFilterCommand = CommandType::New();
m_ProgressFilterCommand = CommandType::New();
// Assign the callbacks
m_StartFilterCommand->SetCallbackFunction(this,
&WriterWatcherBase::StartFilter);
m_EndFilterCommand->SetCallbackFunction(this,
&WriterWatcherBase::EndFilter);
m_ProgressFilterCommand->SetCallbackFunction(this,
&WriterWatcherBase::ShowFilterProgress);
m_StartWriterCommand->SetCallbackFunction(this,
&WriterWatcherBase::StartWriter);
m_EndWriterCommand->SetCallbackFunction(this,
&WriterWatcherBase::EndWriter);
m_ProgressWriterCommand->SetCallbackFunction(this,
&WriterWatcherBase::ShowWriterProgress);
// Add the commands as observers
m_StartWriterTag = m_Process->AddObserver(itk::StartEvent(),
m_StartWriterCommand);
m_EndWriterTag = m_Process->AddObserver(itk::EndEvent(),
m_EndWriterCommand);
m_ProgressWriterTag = m_Process->AddObserver(itk::ProgressEvent(),
m_ProgressWriterCommand);
// Try to get the filter that is wired to m_Process.
if(m_Process->GetInputs()[0]->GetSource())
{
m_SourceProcess = m_Process->GetInputs()[0]->GetSource();
// Add the commands as observers
m_StartFilterTag = m_SourceProcess->AddObserver(itk::StartEvent(),
m_StartFilterCommand);
m_EndFilterTag = m_SourceProcess->AddObserver(itk::EndEvent(),
m_EndFilterCommand);
m_ProgressFilterTag = m_SourceProcess->AddObserver(itk::ProgressEvent(),
m_ProgressFilterCommand);
}
}
WriterWatcherBase
::WriterWatcherBase( const WriterWatcherBase& watch)
{
// Remove any observers we have on the old process object
if (m_Process)
{
if (m_StartWriterCommand)
{
m_Process->RemoveObserver(m_StartWriterTag);
}
if (m_EndWriterCommand)
{
m_Process->RemoveObserver(m_EndWriterTag);
}
if (m_ProgressWriterCommand)
{
m_Process->RemoveObserver(m_ProgressWriterTag);
}
}
if (m_SourceProcess)
{
if (m_StartFilterCommand)
{
m_SourceProcess->RemoveObserver(m_StartFilterTag);
}
if (m_EndFilterCommand)
{
m_SourceProcess->RemoveObserver(m_EndFilterTag);
}
if (m_ProgressFilterCommand)
{
m_SourceProcess->RemoveObserver(m_ProgressFilterTag);
}
}
// Initialize state
m_TimeProbe = watch.m_TimeProbe;
m_Process = watch.m_Process;
m_SourceProcess = watch.m_SourceProcess;
m_Comment = watch.m_Comment;
m_StartFilterTag = 0;
m_EndFilterTag = 0;
m_ProgressFilterTag = 0;
m_StartWriterTag = 0;
m_EndWriterTag = 0;
m_ProgressWriterTag = 0;
// Create a series of commands
if (m_Process)
{
m_StartWriterCommand = CommandType::New();
m_EndWriterCommand = CommandType::New();
m_ProgressWriterCommand = CommandType::New();
// Assign the callbacks
m_StartWriterCommand->SetCallbackFunction(this,
&WriterWatcherBase::StartWriter);
m_EndWriterCommand->SetCallbackFunction(this,
&WriterWatcherBase::EndWriter);
m_ProgressWriterCommand->SetCallbackFunction(this,
&WriterWatcherBase::ShowWriterProgress);
// Add the commands as observers
m_StartWriterTag = m_Process->AddObserver(itk::StartEvent(), m_StartWriterCommand);
m_EndWriterTag = m_Process->AddObserver(itk::EndEvent(), m_EndWriterCommand);
m_ProgressWriterTag
= m_Process->AddObserver(itk::ProgressEvent(), m_ProgressWriterCommand);
}
if (m_SourceProcess)
{
m_StartFilterCommand = CommandType::New();
m_EndFilterCommand = CommandType::New();
m_ProgressFilterCommand = CommandType::New();
// Assign the callbacks
m_StartFilterCommand->SetCallbackFunction(this,
&WriterWatcherBase::StartFilter);
m_EndFilterCommand->SetCallbackFunction(this,
&WriterWatcherBase::EndFilter);
m_ProgressFilterCommand->SetCallbackFunction(this,
&WriterWatcherBase::ShowFilterProgress);
// Add the commands as observers
m_StartFilterTag = m_SourceProcess->AddObserver(itk::StartEvent(), m_StartFilterCommand);
m_EndFilterTag = m_SourceProcess->AddObserver(itk::EndEvent(), m_EndFilterCommand);
m_ProgressFilterTag
= m_SourceProcess->AddObserver(itk::ProgressEvent(), m_ProgressFilterCommand);
}
}
void
WriterWatcherBase
::operator=(const WriterWatcherBase &watch)
{
// Remove any observers we have on the old process object
if (m_Process)
{
if (m_StartWriterCommand)
{
m_Process->RemoveObserver(m_StartWriterTag);
}
if (m_EndWriterCommand)
{
m_Process->RemoveObserver(m_EndWriterTag);
}
if (m_ProgressWriterCommand)
{
m_Process->RemoveObserver(m_ProgressWriterTag);
}
}
if (m_SourceProcess)
{
if (m_StartFilterCommand)
{
m_SourceProcess->RemoveObserver(m_StartFilterTag);
}
if (m_EndFilterCommand)
{
m_SourceProcess->RemoveObserver(m_EndFilterTag);
}
if (m_ProgressFilterCommand)
{
m_SourceProcess->RemoveObserver(m_ProgressFilterTag);
}
}
// Initialize state
m_TimeProbe = watch.m_TimeProbe;
m_Process = watch.m_Process;
m_SourceProcess = watch.m_SourceProcess;
m_Comment = watch.m_Comment;
m_StartFilterTag = 0;
m_EndFilterTag = 0;
m_ProgressFilterTag = 0;
m_StartWriterTag = 0;
m_EndWriterTag = 0;
m_ProgressWriterTag = 0;
// Create a series of commands
if (m_Process)
{
m_StartWriterCommand = CommandType::New();
m_EndWriterCommand = CommandType::New();
m_ProgressWriterCommand = CommandType::New();
// Assign the callbacks
m_StartWriterCommand->SetCallbackFunction(this,
&WriterWatcherBase::StartWriter);
m_EndWriterCommand->SetCallbackFunction(this,
&WriterWatcherBase::EndWriter);
m_ProgressWriterCommand->SetCallbackFunction(this,
&WriterWatcherBase::ShowWriterProgress);
// Add the commands as observers
m_StartWriterTag = m_Process->AddObserver(itk::StartEvent(), m_StartWriterCommand);
m_EndWriterTag = m_Process->AddObserver(itk::EndEvent(), m_EndWriterCommand);
m_ProgressWriterTag
= m_Process->AddObserver(itk::ProgressEvent(), m_ProgressWriterCommand);
}
if (m_SourceProcess)
{
m_StartFilterCommand = CommandType::New();
m_EndFilterCommand = CommandType::New();
m_ProgressFilterCommand = CommandType::New();
// Assign the callbacks
m_StartFilterCommand->SetCallbackFunction(this,
&WriterWatcherBase::StartFilter);
m_EndFilterCommand->SetCallbackFunction(this,
&WriterWatcherBase::EndFilter);
m_ProgressFilterCommand->SetCallbackFunction(this,
&WriterWatcherBase::ShowFilterProgress);
// Add the commands as observers
m_StartFilterTag = m_SourceProcess->AddObserver(itk::StartEvent(), m_StartFilterCommand);
m_EndFilterTag = m_SourceProcess->AddObserver(itk::EndEvent(), m_EndFilterCommand);
m_ProgressFilterTag
= m_SourceProcess->AddObserver(itk::ProgressEvent(), m_ProgressFilterCommand);
}
}
WriterWatcherBase
::~WriterWatcherBase()
{
// Remove any observers we have on the old process object
if (m_Process)
{
if (m_StartWriterCommand)
{
m_Process->RemoveObserver(m_StartWriterTag);
}
if (m_EndWriterCommand)
{
m_Process->RemoveObserver(m_EndWriterTag);
}
if (m_ProgressWriterCommand)
{
m_Process->RemoveObserver(m_ProgressWriterTag);
}
}
if (m_SourceProcess)
{
if (m_StartFilterCommand)
{
m_SourceProcess->RemoveObserver(m_StartFilterTag);
}
if (m_EndFilterCommand)
{
m_SourceProcess->RemoveObserver(m_EndFilterTag);
}
if (m_ProgressFilterCommand)
{
m_SourceProcess->RemoveObserver(m_ProgressFilterTag);
}
}
}
} // 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 __otbWriterWatcherBase_h
#define __otbWriterWatcherBase_h
#include "itkCommand.h"
#include "itkProcessObject.h"
#include "itkTimeProbe.h"
namespace otb
{
/** \class WriterWatcherBase
* \brief This class is an abstract class
* Provides an interface to progress task mechanic
*
* Implement your callbacks
* \li ShowWriterProgress : callback called for each completed tile
* \li StartWriter : callback called at the begin of tile writing
* \li EndWriter : callback called at the end of tile writing
* \li ShowFilterProgress : callback called for each completed pixel
* \li StartFilter : callback called at the begin of filter execution for a given tile
* \li EndFilter : callback called at the end of filter execution for a given tile
*
*/
class ITK_EXPORT WriterWatcherBase
{
public:
/** Constructor. Takes a ProcessObject to monitor and an optional
* comment string that is prepended to each event message. */
WriterWatcherBase(itk::ProcessObject* process,
const char *comment="");
/** Default constructor */
WriterWatcherBase();
/** Copy constructor */
WriterWatcherBase(const WriterWatcherBase&);
/** operator= */
void operator=(const WriterWatcherBase& );
/** Destructor. */
virtual ~WriterWatcherBase();
/** 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;}
protected:
/** Callback method to show the ProgressEvent from the writer */
virtual void ShowWriterProgress() = 0;
/** Callback method to show the StartEvent from the writer*/
virtual void StartWriter() = 0;
/** Callback method to show the EndEvent from the writer*/
virtual void EndWriter() = 0;
/** Callback method to show the ProgressEvent from the filter */
virtual void ShowFilterProgress() = 0;
/** Callback method to show the StartEvent from the filter*/
virtual void StartFilter() = 0;
/** Callback method to show the EndEvent from the filter*/
virtual void EndFilter() = 0;
/** Computing time */
itk::TimeProbe m_TimeProbe;
/** Associated comment */
std::string m_Comment;
/** Abstract process object */
itk::ProcessObject::Pointer m_Process;
/** Second abstract process object representing the source */
itk::ProcessObject::Pointer m_SourceProcess;
/** Internal type */
typedef itk::SimpleMemberCommand<WriterWatcherBase> CommandType;
/** Start writer callback */
CommandType::Pointer m_StartWriterCommand;
/** End writer callback */
CommandType::Pointer m_EndWriterCommand;
/** Progress writer callback */
CommandType::Pointer m_ProgressWriterCommand;
/** Start filter callback */
CommandType::Pointer m_StartFilterCommand;
/** End writer callback */
CommandType::Pointer m_EndFilterCommand;
/** Progress writer callback */
CommandType::Pointer m_ProgressFilterCommand;
/** Start oberserver */
unsigned long m_StartWriterTag;
/** End observer */
unsigned long m_EndWriterTag;
/** Progress observer */
unsigned long m_ProgressWriterTag;
/** Start oberserver */
unsigned long m_StartFilterTag;
/** End observer */
unsigned long m_EndFilterTag;
/** Progress observer */
unsigned long m_ProgressFilterTag;
private:
};
} // end namespace otb
#endif
/*=========================================================================
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 "otbFltkWriterWatcher.h"
namespace otb
{
FltkWriterWatcher
::FltkWriterWatcher(itk::ProcessObject* process,
int x, int y, int w, int h,
const char *comment)
: WriterWatcherBase(process, comment)
{
m_Window = new Fl_Window(x,y,w+10,h+15);
m_Window->label(m_Comment.c_str());
m_Window->begin();
m_FilterProgress = new Fl_Progress(5,5,w,h/2);
m_FilterProgress->selection_color(FL_DARK_BLUE);
m_FilterProgress->minimum(0);
m_FilterProgress->maximum(1);
m_FilterProgress->label("current tile");
m_FilterProgress->align(FL_ALIGN_INSIDE);