/*========================================================================= Program: vv http://www.creatis.insa-lyon.fr/rio/vv Authors belong to: - University of LYON http://www.universite-lyon.fr/ - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the copyright notices for more information. It is distributed under dual licence - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html ===========================================================================**/ #ifndef CLITKCOMMON_H #define CLITKCOMMON_H // clitk include #include "clitkPortability.h" #include "clitkDD.h" #include "clitkExceptionObject.h" // itk include (include std) #include #include #include #include // std include #include // Include for "rusage" #include #if defined(unix) || defined(__APPLE__) # include # include #elif defined(WIN32) # include #endif #if ITK_VERSION_MAJOR < 4 namespace itk { typedef unsigned int ThreadIdType; } #endif //-------------------------------------------------------------------- namespace clitk { typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; #define CLITK_TRY_CATCH_EXIT(func) \ try { \ func; \ } \ catch (const itk::ExceptionObject& e) { \ e.Print(std::cout); \ exit(-1);\ } \ catch (const std::exception& e) { \ std::cout << e.what() << std::endl; \ exit(-2);\ } \ catch (...) { \ std::cout << "Unknown excpetion" << std::endl; \ exit(-3); \ } //-------------------------------------------------------------------- // when everything goes wrong #define WHEREAMI "[ " << __FILE__ << " ] line " << __LINE__ #define FATAL(a) std::cerr << "ERROR in " << WHEREAMI << ": " << a; exit(0); //-------------------------------------------------------------------- // GGO with modified struct name #define GGO(ggo_filename, args_info) \ args_info_##ggo_filename args_info; \ cmdline_parser_##ggo_filename##2(argc, argv, &args_info, 1, 1, 0); \ if (args_info.config_given) \ cmdline_parser_##ggo_filename##_configfile (args_info.config_arg, &args_info, 0, 0, 1); \ else cmdline_parser_##ggo_filename(argc, argv, &args_info); //-------------------------------------------------------------------- // skip line with # void skipComment(std::istream & is); //-------------------------------------------------------------------- // linear (rough) conversion from Hounsfield Unit to density double HU2density(double HU); //-------------------------------------------------------------------- // Return filename extension std::string GetExtension(const std::string& filename); //-------------------------------------------------------------------- // Convert float, double ... to string template std::string toString(const T & t); template std::string toStringVector(const T * t, const int n); template std::string toStringVector(const T & t, const int n); template std::string toStringVector(const std::vector & t); template bool fromString(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&)=std::dec); //-------------------------------------------------------------------- // Display a progress % void VerboseInProgress(const int nb, const int current, const int percentage); void VerboseInProgressInPercentage(const int nb, const int current, const int percentage); //-------------------------------------------------------------------- // Convert a pixel type to another (downcast) template TPixelDown PixelTypeDownCast(const TPixelUp & x); template<> float PixelTypeDownCast(const double & x); //-------------------------------------------------------------------- // Return the indexes of sorted values in a vector template struct vectorComparisonLowerThan; template struct vectorComparisonGreaterThan; template void GetSortedIndex(const std::vector & toSort, std::vector & index, bool increasing=true); //-------------------------------------------------------------------- // Return the name of a type as a string template std::string GetTypeAsString(); //-------------------------------------------------------------------- // Convert radian / degree double rad2deg(double anglerad); double deg2rad(double anglerad); //-------------------------------------------------------------------- int GetTypeSizeFromString(const std::string & type); //-------------------------------------------------------------------- // Special case to handle "signed_char = schar" ... template bool IsSameType(std::string t) { return (t==GetTypeAsString()); } template<> bool IsSameType(std::string t); template<> bool IsSameType(std::string t); template<> bool IsSameType(std::string t); template<> bool IsSameType(std::string t); //-------------------------------------------------------------------- template std::string CreateListOfTypes(bool last=true) { return GetTypeAsString(); } template std::string CreateListOfTypes(bool last=true) { if (last) return CreateListOfTypes()+" and "+CreateListOfTypes(); else return CreateListOfTypes()+", "+CreateListOfTypes(); } template std::string CreateListOfTypes(bool last=true) { if (last) return CreateListOfTypes(false)+" and "+CreateListOfTypes(); else return CreateListOfTypes(false)+", "+CreateListOfTypes(); } template std::string CreateListOfTypes(bool last=true) { if (last) return CreateListOfTypes(false)+" and "+CreateListOfTypes(); else return CreateListOfTypes(false)+", "+CreateListOfTypes(); } template std::string CreateListOfTypes(bool last=true) { if (last) return CreateListOfTypes(false)+" and "+CreateListOfTypes(); else return CreateListOfTypes(false)+", "+CreateListOfTypes(); } template std::string CreateListOfTypes(bool last=true) { if (last) return CreateListOfTypes(false)+" and "+CreateListOfTypes(); else return CreateListOfTypes(false)+", "+CreateListOfTypes(); } template std::string CreateListOfTypes(bool last=true) { if (last) return CreateListOfTypes(false)+" and "+CreateListOfTypes(); else return CreateListOfTypes(false)+", "+CreateListOfTypes(); } template std::string CreateListOfTypes(bool last=true) { if (last) return CreateListOfTypes(false)+" and "+CreateListOfTypes(); else return CreateListOfTypes(false)+", "+CreateListOfTypes(); } //-------------------------------------------------------------------- //-------------------------------------------------------------------- void FindAndReplace(std::string & line, const std::string & tofind, const std::string & replacement); void FindAndReplace(std::string & line, const std::vector & tofind, const std::vector & toreplace); void FindAndReplace(std::ifstream & in, const std::vector & tofind, const std::vector & toreplace, std::ofstream & out); //-------------------------------------------------------------------- //-------------------------------------------------------------------- double ComputeEuclideanDistanceFromPointToPlane(const itk::ContinuousIndex point, const itk::ContinuousIndex pointInPlane, const itk::ContinuousIndex normalPlane); //-------------------------------------------------------------------- // Open a File for reading/writing void openFileForReading(std::ifstream & is, const std::string & filename); void openFileForWriting(std::ofstream & os, const std::string & filename); //-------------------------------------------------------------------- void readDoubleFromFile(const std::string & filename, std::vector & list); //-------------------------------------------------------------------- double cotan(double i); double invcotan(double i); //-------------------------------------------------------------------- void disableStdCerr(); void enableStdCerr(); //-------------------------------------------------------------------- template void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output); //-------------------------------------------------------------------- void PrintMemoryUsed(); //-------------------------------------------------------------------- // Convert a map to a vector template void MapToVecFirst(const M & m, V & v); template void MapToVecSecond(const M & m, V & v); #include "clitkCommon.txx" } // end namespace #endif /* end #define CLITKCOMMON_H */