X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FclitkCommon.txx;h=845e62fe520deb89def17b26d99c0e3fa7be1c98;hb=d55f025b18f68066a52b8f33c2dc6481e82c2580;hp=f9eeb8c8bfd9280319f0ed0d38a47a393827b87b;hpb=0b7c9b1e1215634b02cbd38d4e4ba101d6111ba8;p=clitk.git diff --git a/common/clitkCommon.txx b/common/clitkCommon.txx index f9eeb8c..845e62f 100644 --- a/common/clitkCommon.txx +++ b/common/clitkCommon.txx @@ -1,9 +1,9 @@ /*========================================================================= Program: vv http://www.creatis.insa-lyon.fr/rio/vv - Authors belong to: + Authors belong to: - University of LYON http://www.universite-lyon.fr/ - - Léon Bérard cancer center http://oncora1.lyon.fnclcc.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 @@ -14,31 +14,86 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html -======================================================================-====*/ +===========================================================================**/ #ifndef CLITKCOMMON_TXX #define CLITKCOMMON_TXX -/** - ------------------------------------------------- - * @file clitkCommon.txx - * @author David Sarrut - * @date 18 May 2006 - * - -------------------------------------------------*/ + +//------------------------------------------------------- +// Utility functions for text file parsing (author: joel schaerer) + +//-------------------------------------------------------------------- +template +ElementType parse_value(std::string str) +{ + std::istringstream parser(str); + ElementType value; + parser >> value; + assert(!parser.fail()); + return value; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +std::vector parse_string(std::string str,char delim) +{ + std::istringstream ss(str); + std::string token; + std::vector result; + while (std::getline(ss,token,delim)) { + result.push_back(parse_value(token)); + } + return result; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +std::vector > parse_file(const char* filename,char delim) +{ + std::ifstream fs(filename); + std::string line; + std::vector > result; + while (std::getline(fs,line)) { + if (line[0] != '#') //skip comments + result.push_back(parse_string(line,delim)); + } + return result; +} +//-------------------------------------------------------------------- + //-------------------------------------------------------------------- // Convert float, double ... to string template -std::string toString(const T & t) { +std::string toString(const T & t) +{ std::ostringstream myStream; myStream << t << std::flush; return(myStream.str()); } //-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +// http://www.codeguru.com/forum/showthread.php?t=231054 +template +bool fromString(T& t, const std::string& s, + std::ios_base& (*f)(std::ios_base&)) +{ + std::istringstream iss(s); + return !(iss >> f >> t).fail(); +} +//-------------------------------------------------------------------- + + //-------------------------------------------------------------------- // Convert float*, double* ... to string template -std::string toStringVector(const T * t, const int n) { +std::string toStringVector(const T * t, const int n) +{ std::ostringstream myStream; for(int i=0; i(t[i]) << " "; @@ -50,7 +105,8 @@ std::string toStringVector(const T * t, const int n) { //-------------------------------------------------------------------- // Convert float*, double* ... to string template -std::string toStringVector(const T & t, const int n) { +std::string toStringVector(const T & t, const int n) +{ std::ostringstream myStream; for(int i=0; i -std::string toStringVector(const std::vector & t) { +std::string toStringVector(const std::vector & t) +{ return toStringVector(&t[0], t.size()); } //-------------------------------------------------------------------- @@ -70,7 +127,8 @@ std::string toStringVector(const std::vector & t) { //-------------------------------------------------------------------- // Convert a pixel type to another (downcast) template -TPixelDown PixelTypeDownCast(const TPixelUp & x) { +TPixelDown PixelTypeDownCast(const TPixelUp & x) +{ return (TPixelDown)lrint(x); } //-------------------------------------------------------------------- @@ -79,7 +137,7 @@ TPixelDown PixelTypeDownCast(const TPixelUp & x) { template struct vectorComparisonLowerThan: public std::binary_function { vectorComparisonLowerThan(const std::vector & v):vect(v) {}; - bool operator()(int x, int y) { + bool operator()(int x, int y) { return (vect[x] < vect[y]); } const std::vector & vect; @@ -90,7 +148,7 @@ struct vectorComparisonLowerThan: public std::binary_function { template struct vectorComparisonGreaterThan: public std::binary_function { vectorComparisonGreaterThan(const std::vector & v):vect(v) {}; - bool operator()(int x, int y) { + bool operator()(int x, int y) { return (vect[x] > vect[y]); } const std::vector & vect; @@ -99,23 +157,25 @@ struct vectorComparisonGreaterThan: public std::binary_function //-------------------------------------------------------------------- template -void GetSortedIndex(const std::vector & toSort, std::vector & index, bool increasing) { - index.resize(toSort.size()); +void GetSortedIndex(const std::vector & toSort, std::vector & index, bool increasing) +{ + index.resize(toSort.size()); for(unsigned int i=0; i(toSort)); - else - std::sort(index.begin(), - index.end(), - vectorComparisonGreaterThan(toSort)); + if (increasing) + std::sort(index.begin(), + index.end(), + vectorComparisonLowerThan(toSort)); + else + std::sort(index.begin(), + index.end(), + vectorComparisonGreaterThan(toSort)); } //-------------------------------------------------------------------- //-------------------------------------------------------------------- template -std::string GetTypeAsString() { +std::string GetTypeAsString() +{ // http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html // and // itkImageIOBase.cxx @@ -135,19 +195,21 @@ std::string GetTypeAsString() { } //-------------------------------------------------------------------- + //-------------------------------------------------------------------- template -void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) { +void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) +{ output->SetRegions(input->GetLargestPossibleRegion()); output->SetOrigin(input->GetOrigin()); output->SetSpacing(input->GetSpacing()); output->Allocate(); - typedef itk::ImageRegionConstIterator ConstIteratorType; + typedef itk::ImageRegionConstIterator ConstIteratorType; ConstIteratorType pi(input,input->GetLargestPossibleRegion()); - pi.GoToBegin(); - typedef itk::ImageRegionIterator IteratorType; + pi.GoToBegin(); + typedef itk::ImageRegionIterator IteratorType; IteratorType po(output,input->GetLargestPossibleRegion()); - po.GoToBegin(); + po.GoToBegin(); while (!pi.IsAtEnd()) { po.Set(pi.Get()); ++pi; @@ -156,5 +218,39 @@ void CloneImage(const typename ImageType::Pointer & input, typename ImageType::P } //-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +// http://stackoverflow.com/questions/771453/copy-map-values-to-vector-in-stl +template +void MapToVecFirst(const M & m, V & v) { + for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) { + v.push_back( it->first ); + } +} +template +void MapToVecSecond(const M & m, V & v) { + for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) { + v.push_back( it->second ); + } +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +//http://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string +template +int inline findAndReplace(T& source, const T& find, const T& replace) +{ + int num=0; + int fLen = find.size(); + int rLen = replace.size(); + for (int pos=0; (pos=source.find(find, pos))!=T::npos; pos+=rLen) { + num++; + source.replace(pos, fLen, replace); + } + return num; +} +//-------------------------------------------------------------------- + #endif /* end #define CLITKCOMMON_TXX */