Skip to content
Snippets Groups Projects
Commit c4af7ce7 authored by Julien Malik's avatar Julien Malik
Browse files

ENH: add a watcher which outputs only one line on the console

parent d7344ce3
No related branches found
No related tags found
No related merge requests found
/*=========================================================================
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 "otbStandardOneLineFilterWatcher.h"
namespace otb
{
StandardOneLineFilterWatcher
::StandardOneLineFilterWatcher(itk::ProcessObject* process,
const char *comment)
: FilterWatcherBase(process, comment)
{
m_StarsCount = 50;
m_CurrentNbStars = -1;
}
StandardOneLineFilterWatcher
::StandardOneLineFilterWatcher(itk::ProcessObject* process,
const std::string& comment)
: FilterWatcherBase(process, comment.c_str())
{
m_StarsCount = 50;
}
StandardOneLineFilterWatcher
::StandardOneLineFilterWatcher(const StandardOneLineFilterWatcher& watch) : FilterWatcherBase(watch)
{
// Initialize state
m_StarsCount = watch.m_StarsCount;
}
void
StandardOneLineFilterWatcher
::operator =(const StandardOneLineFilterWatcher& watch)
{
// Initialize state
FilterWatcherBase::operator=(watch);
m_StarsCount = watch.m_StarsCount;
}
void
StandardOneLineFilterWatcher
::ShowProgress()
{
if (m_Process)
{
int progressPercent = static_cast<int>(m_Process->GetProgress() * 100);
int nbStars = static_cast<int>(m_Process->GetProgress() * m_StarsCount);
int nbBlanks = m_StarsCount - nbStars;
if (nbBlanks < 0)
{
nbBlanks = 0;
}
if (nbStars > m_StarsCount)
{
nbStars = m_StarsCount;
}
if (progressPercent > 100)
{
progressPercent = 100;
}
if (nbStars > m_CurrentNbStars)
{
std::string stars(nbStars, '*');
std::string blanks(nbBlanks, ' ');
std::cout << "\r"
<< m_Comment
<< ": "
<< progressPercent << "% [" << stars << blanks << "]"
<< std::flush;
}
m_CurrentNbStars = nbStars;
}
}
void
StandardOneLineFilterWatcher
::StartFilter()
{
m_TimeProbe.Start();
}
void
StandardOneLineFilterWatcher
::EndFilter()
{
m_TimeProbe.Stop();
std::cout << " ("
<< 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 __otbStandardOneLineFilterWatcher_h
#define __otbStandardOneLineFilterWatcher_h
#include "otbFilterWatcherBase.h"
namespace otb
{
/** \class StandardOneLineFilterWatcher
* \brief This class shows the percentage progress execution
* of the pipeline filtering process
*
* This class is based on Observer design patter
* Abstract class ProcessObject is the subject
* Event are observers
*
* Usage example:
*
* \code
* typedef itk::BinaryThresholdImageFilter<ImageType> FilterType;
* FilterType::Pointer thresholdFilter = FilterType::New();
*
* StandardOneLineFilterWatcher watcher(thresholdFilter, "Threshold");
* \endcode
*
* \see itk::SimpleFilterWatcher
* \see otb::fltkFilterWatcher
*/
class /*ITK_EXPORT*/ StandardOneLineFilterWatcher : public FilterWatcherBase
{
public:
/** Constructor. Takes a ProcessObject to monitor and an optional
* comment string that is prepended to each event message. */
StandardOneLineFilterWatcher(itk::ProcessObject* process,
const char *comment = "");
StandardOneLineFilterWatcher(itk::ProcessObject* process,
const std::string& comment = "");
/** Default constructor */
StandardOneLineFilterWatcher() : m_StarsCount(0) {};
/** Copy constructor */
StandardOneLineFilterWatcher(const StandardOneLineFilterWatcher&);
/** operator= */
void operator =(const StandardOneLineFilterWatcher&);
/** 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:
/** Stars coutning */
int m_StarsCount;
int m_CurrentNbStars;
};
} // end namespace otb
#endif
......@@ -703,6 +703,12 @@ ADD_TEST(coTuStandardFilterWatcherNew ${COMMON_TESTS7}
${INPUTDATA}/qb_RoadExtract.img
)
# ------------- otb::StandardOneLineFilterWatcherNew ----------------------------
ADD_TEST(coTuStandardOneLineFilterWatcher ${COMMON_TESTS7}
otbStandardOneLineFilterWatcherTest
${INPUTDATA}/qb_RoadExtract.img
)
# ------------- otb::DataNode ----------------------------
ADD_TEST(coTvDataNode ${COMMON_TESTS7}
......@@ -1213,6 +1219,7 @@ otbCommonTests7.cxx
otbGenericInterpolateImageFunctionNew.cxx
otbMirrorBoundaryConditionTest.cxx
otbStandardFilterWatcherNew.cxx
otbStandardOneLineFilterWatcherTest.cxx
otbDataNodeTest.cxx
otbVectorDataNew.cxx
otbVectorDataSourceNew.cxx
......
......@@ -27,6 +27,7 @@ void RegisterTests()
REGISTER_TEST(otbGenericInterpolateImageFunctionNew);
REGISTER_TEST(otbMirrorBoundaryConditionTest);
REGISTER_TEST(otbStandardFilterWatcherNew);
REGISTER_TEST(otbStandardOneLineFilterWatcherTest);
REGISTER_TEST(otbDataNodeTest);
REGISTER_TEST(otbVectorDataNew);
REGISTER_TEST(otbVectorDataSourceNew);
......
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.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 "itkMacro.h"
#include "otbImageFileReader.h"
#include "otbImage.h"
#include "otbStandardOneLineFilterWatcher.h"
#include "itkGradientMagnitudeImageFilter.h"
int otbStandardOneLineFilterWatcherTest(int argc, char * argv[])
{
const unsigned int Dimension = 2;
typedef unsigned char PixelType;
typedef otb::Image<PixelType, Dimension> ImageType;
typedef otb::ImageFileReader<ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
typedef itk::GradientMagnitudeImageFilter<ImageType, ImageType> FilterType;
FilterType::Pointer gradient = FilterType::New();
typedef otb::StandardOneLineFilterWatcher WatcherType;
WatcherType watcher1(gradient, "Gradient");
gradient->SetInput(reader->GetOutput());
gradient->Update();
// Test copy constructor.
WatcherType watcher2( watcher1 );
if ( watcher1.GetNameOfClass() != watcher2.GetNameOfClass()
|| watcher1.GetProcess() != watcher2.GetProcess()
|| watcher1.GetComment() != watcher2.GetComment() )
{
std::cout << "Copy constructor failed." << std::endl;
return EXIT_FAILURE;
}
// Test default constructor.
WatcherType watcher3;
// Test assignment operator.
watcher3 = watcher2;
if ( watcher3.GetNameOfClass() != watcher2.GetNameOfClass()
|| watcher3.GetProcess() != watcher2.GetProcess()
|| watcher3.GetComment() != watcher2.GetComment() )
{
std::cout << "Operator= failed." << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment