diff --git a/Modules/CameraCalibration/mitkEndoDebug.cpp b/Modules/CameraCalibration/mitkEndoDebug.cpp index 5941f03309..42720c3523 100644 --- a/Modules/CameraCalibration/mitkEndoDebug.cpp +++ b/Modules/CameraCalibration/mitkEndoDebug.cpp @@ -1,207 +1,246 @@ #include "mitkEndoDebug.h" #include #include #include #include #include #include namespace mitk { struct EndoDebugData { EndoDebugData() : m_DebugEnabled(false) , m_ShowImagesInDebug(false) , m_ShowImagesTimeOut(false) , m_Mutex(itk::FastMutexLock::New()) + , m_DebugImagesOutputDirectory("") { } std::set m_FilesToDebug; std::set m_SymbolsToDebug; bool m_DebugEnabled; bool m_ShowImagesInDebug; size_t m_ShowImagesTimeOut; std::ofstream m_Stream; itk::FastMutexLock::Pointer m_Mutex; + std::string m_DebugImagesOutputDirectory; }; EndoDebug::EndoDebug() : d ( new EndoDebugData ) { } EndoDebug::~EndoDebug() { if(d->m_Stream.is_open()) d->m_Stream.close(); delete d; } EndoDebug& EndoDebug::GetInstance() { static EndoDebug instance; return instance; } + std::string EndoDebug::GetUniqueFileName( const std::string& dir, const std::string& ext ) + { + std::stringstream s; + s.precision( 0 ); + + std::string filename; + int i = 0; + while( filename.empty() || itksys::SystemTools::FileExists( (dir+"/"+filename).c_str() ) ) + { + s.str(""); + s << i; + filename = s.str() + "." + ext; + ++i; + } + + filename = dir+"/"+filename; + + return filename; + } + std::string EndoDebug::GetFilenameWithoutExtension(const std::string& s) { return itksys::SystemTools::GetFilenameWithoutExtension( s ); } void EndoDebug::AddFileToDebug(const std::string& s) { { itk::MutexLockHolder lock(*d->m_Mutex); d->m_FilesToDebug.insert( s ); } } void EndoDebug::AddSymbolToDebug(const std::string& symbolToDebug) { { itk::MutexLockHolder lock(*d->m_Mutex); d->m_SymbolsToDebug.insert( symbolToDebug ); } } bool EndoDebug::DebugSymbol(const std::string& s) { { itk::MutexLockHolder lock(*d->m_Mutex); return d->m_SymbolsToDebug.find(s) != d->m_SymbolsToDebug.end(); } } bool EndoDebug::DebugFile(const std::string& s) { std::string filename = GetFilenameWithoutExtension(s); { itk::MutexLockHolder lock(*d->m_Mutex); return d->m_FilesToDebug.find(filename) != d->m_FilesToDebug.end(); } } bool EndoDebug::Debug( const std::string& fileToDebug, const std::string& symbol ) { bool debug = false; { bool debugEnabled = false; size_t filesSize = 0; size_t symbolsSize = 0; bool symbolFound = false; { itk::MutexLockHolder lock(*d->m_Mutex); debugEnabled = d->m_DebugEnabled; filesSize = d->m_FilesToDebug.size(); symbolsSize = d->m_SymbolsToDebug.size(); symbolFound = d->m_SymbolsToDebug.find(symbol) != d->m_SymbolsToDebug.end(); } if( debugEnabled ) { if( filesSize == 0 ) debug = true; else debug = DebugFile(fileToDebug); // ok debug is determined so far, now check if symbol set if( symbolsSize > 0 ) { debug = symbolFound; } else { // do not show symbol debug output if no symbols are set at all if( !symbol.empty() ) debug = false; } } } return debug; } void EndoDebug::SetDebugEnabled(bool _DebugEnabled) { { itk::MutexLockHolder lock(*d->m_Mutex); d->m_DebugEnabled = _DebugEnabled; } } + void EndoDebug::SetDebugImagesOutputDirectory(const std::string& _DebugImagesOutputDirectory) + { + { + itk::MutexLockHolder lock(*d->m_Mutex); + d->m_DebugImagesOutputDirectory = _DebugImagesOutputDirectory; + } + + } + bool EndoDebug::GetDebugEnabled() { { itk::MutexLockHolder lock(*d->m_Mutex); return d->m_DebugEnabled; } } void EndoDebug::SetShowImagesInDebug(bool _ShowImagesInDebug) { { itk::MutexLockHolder lock(*d->m_Mutex); d->m_ShowImagesInDebug = _ShowImagesInDebug; } } bool EndoDebug::GetShowImagesInDebug() { { itk::MutexLockHolder lock(*d->m_Mutex); return d->m_ShowImagesInDebug; } } void EndoDebug::SetShowImagesTimeOut(size_t _ShowImagesTimeOut) { { itk::MutexLockHolder lock(*d->m_Mutex); d->m_ShowImagesTimeOut = _ShowImagesTimeOut; } } + std::string EndoDebug::GetDebugImagesOutputDirectory() const + { + { + itk::MutexLockHolder lock(*d->m_Mutex); + return d->m_DebugImagesOutputDirectory; + } + } + bool EndoDebug::GetShowImagesTimeOut() { { itk::MutexLockHolder lock(*d->m_Mutex); return d->m_ShowImagesTimeOut; } } void EndoDebug::SetLogFile( const std::string& file ) { { itk::MutexLockHolder lock(*d->m_Mutex); d->m_Stream.open ( file.c_str(), std::ios::out | std::ios::app); } } void EndoDebug::ShowMessage( const std::string& message ) { { itk::MutexLockHolder lock(*d->m_Mutex); if(d->m_Stream.is_open()) { char *timestr; struct tm *newtime; time_t aclock; time(&aclock); newtime = localtime(&aclock); timestr = asctime(newtime); d->m_Stream << timestr << ", " << message; } else std::cout << message << std::flush; } } } diff --git a/Modules/CameraCalibration/mitkEndoDebug.h b/Modules/CameraCalibration/mitkEndoDebug.h index 299e104e84..98301ce900 100644 --- a/Modules/CameraCalibration/mitkEndoDebug.h +++ b/Modules/CameraCalibration/mitkEndoDebug.h @@ -1,209 +1,232 @@ #ifndef mitkEndoDebug_h #define mitkEndoDebug_h #include #include #include #include #include namespace mitk { /// /// again d pointer impl /// struct EndoDebugData; /// /// class responsible for handling debug matters /// in endotracking /// struct mitkCameraCalibration_EXPORT EndoDebug { /// /// singleton class /// static EndoDebug& GetInstance(); + /// + /// helper function getting unique file name + /// + static std::string GetUniqueFileName( const std::string& dir, const std::string& ext="jpg" ); + /// /// set if debug is enabled at all /// void SetDebugEnabled(bool _DebugEnabled); /// /// \return true if debug should be enabled /// bool GetDebugEnabled(); /// /// set if debug is enabled at all /// void SetShowImagesInDebug(bool _ShowImagesInDebug); /// /// \return true if debug should be enabled /// bool GetShowImagesInDebug(); /// /// set if debug is enabled at all /// void SetShowImagesTimeOut(size_t _ShowImagesTimeOut); /// /// \return true if debug should be enabled /// bool GetShowImagesTimeOut(); + /// + /// sets an output directory. if set all images that are shown are also written to that + /// output dir + /// + void SetDebugImagesOutputDirectory(const std::string& _DebugImagesOutputDirectory); + + /// + /// \return true if debug should be enabled + /// + std::string GetDebugImagesOutputDirectory() const; + /// /// \return the basename of a file without path /// std::string GetFilenameWithoutExtension(const std::string& s); /// /// add a file to debug ( if one or more files are set ) /// only those files will be debugged when using the macros /// below. e.g. call AddFileToDebug("MyClass.cpp"), then /// statements like endodebug(...) will be evaluated in /// MyClass.cpp and nowhere else /// void AddFileToDebug(const std::string& fileToDebug); /// /// same as files to debug, but the user can provide /// any symbol string. if one or more symbols /// are set only for these symbols Debug() will return true /// void AddSymbolToDebug(const std::string& symbolToDebug); /// /// \return true if file should be debugged /// bool DebugFile( const std::string& fileToDebug ); /// /// \return true if symbol should be debugged /// bool DebugSymbol( const std::string& symbolToDebug ); /// /// \return the all in all status if debug output /// should be generated /// bool Debug( const std::string& fileToDebug, const std::string& symbol="" ); /// /// set a log file. if a log file is set and debug is activated all messages will be appended to that file /// void SetLogFile( const std::string& file ); /// /// shows a message or writes it to a log file if a file is set (and is valid for writing) /// void ShowMessage( const std::string& message ); /// /// init defaults /// EndoDebug(); /// /// delete d pointer /// virtual ~EndoDebug(); private: /// /// d pointer /// EndoDebugData* d; }; } // DISABLE DEBUGGING FOR RELEASE MODE ON WINDOWS #if (defined(WIN32) && !defined(_DEBUG)) || defined(NDEBUG) #define endodebugmarker #define endodebug(msg) #define endodebugvar(var) #define endodebugsymbol(var, mSymbol) #define endodebugimg(imgVariableName) #define endodebugbegin if( false ) { #define endodebugend } #else /// /// macro for debugging purposes /// #define endodebugmarker\ if( mitk::EndoDebug::GetInstance().Debug(__FILE__) ) \ { \ std::ostringstream ___ostringstream; \ ___ostringstream << mitk::EndoDebug::GetInstance().GetFilenameWithoutExtension(__FILE__) << ", " << __LINE__\ << ": " << __FUNCTION__ << std::endl;\ mitk::EndoDebug::GetInstance().ShowMessage( ___ostringstream.str() ); \ } /// /// macro for debugging purposes /// #define endodebug(msg)\ if( mitk::EndoDebug::GetInstance().Debug(__FILE__) ) \ { \ std::ostringstream ___ostringstream; \ ___ostringstream << mitk::EndoDebug::GetInstance().GetFilenameWithoutExtension(__FILE__) << ", " << __LINE__\ << ": " << msg << std::endl;\ mitk::EndoDebug::GetInstance().ShowMessage( ___ostringstream.str() ); \ } /// /// macro for debugging variables /// #define endodebugvar(var)\ if( mitk::EndoDebug::GetInstance().Debug(__FILE__) ) \ { \ std::ostringstream ___ostringstream; \ ___ostringstream << mitk::EndoDebug::GetInstance().GetFilenameWithoutExtension(__FILE__) << ", " << __LINE__\ << ": " #var " = " << var << std::endl;\ mitk::EndoDebug::GetInstance().ShowMessage( ___ostringstream.str() ); \ } /// /// macro for debugging a variable as symbol /// #define endodebugsymbol(var, mSymbol)\ if( mitk::EndoDebug::GetInstance().Debug(__FILE__, mSymbol) ) \ { \ std::ostringstream ___ostringstream; \ ___ostringstream << mitk::EndoDebug::GetInstance().GetFilenameWithoutExtension(__FILE__) << ", " << __LINE__\ << ": " #var " = " << var << std::endl;\ mitk::EndoDebug::GetInstance().ShowMessage( ___ostringstream.str() ); \ } /// /// macro for showing cv image if in debug mode /// highgui.h must be included before /// #define endodebugimg(imgVariableName)\ if( mitk::EndoDebug::GetInstance().Debug(__FILE__) \ - && mitk::EndoDebug::GetInstance().GetShowImagesInDebug()) \ + && mitk::EndoDebug::GetInstance().GetShowImagesInDebug() \ + && (imgVariableName).cols > 0 && (imgVariableName).rows > 0 && (imgVariableName).data) \ { \ std::ostringstream ___ostringstream; \ ___ostringstream << mitk::EndoDebug::GetInstance().GetFilenameWithoutExtension(__FILE__) << ", " << __LINE__\ << ": Showing " #imgVariableName << std::endl; \ mitk::EndoDebug::GetInstance().ShowMessage( ___ostringstream.str() ); \ + std::string outputFile = mitk::EndoDebug::GetInstance().GetDebugImagesOutputDirectory(); \ + if( !outputFile.empty() ) \ + {\ + outputFile = mitk::EndoDebug::GetInstance().GetUniqueFileName(outputFile, "jpg");\ + cv::imwrite(outputFile, imgVariableName);\ + }\ cv::imshow( "Debug", imgVariableName ); \ cv::waitKey( mitk::EndoDebug::GetInstance().GetShowImagesTimeOut() ); \ } /// /// macro for a section that should only be executed if debugging is enabled /// #define endodebugbegin \ if( mitk::EndoDebug::GetInstance().Debug(__FILE__) ) \ { /// /// macro for a section that should only be executed if debugging is enabled /// #define endodebugend \ } #endif #endif // mitkEndoDebug_h