]> Creatis software - clitk.git/blobdiff - common/clitkCommon.txx
Add MapToVec (first/second) functions.
[clitk.git] / common / clitkCommon.txx
index 4d16648fbc020aa025c70b5c46de41c16b330ab3..5ee83bdca8f4f3efc23ae2aa8dc12ad28a2fe36e 100644 (file)
@@ -1,40 +1,99 @@
-/*-------------------------------------------------------------------------
-                                                                                
-  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
-  l'Image). All rights reserved. See Doc/License.txt or
-  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
-  
+/*=========================================================================
+  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 above copyright notices for more information.
-  
-  -------------------------------------------------------------------------*/
+  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_TXX
 #define CLITKCOMMON_TXX
 
-/**
-   -------------------------------------------------
-   * @file   clitkCommon.txx
-   * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
-   * @date   18 May 2006
-   * 
-   -------------------------------------------------*/
+//-------------------------------------------------------
+// Utility functions for text file parsing (author: joel schaerer)
+
+//--------------------------------------------------------------------
+template<class ElementType>
+ElementType parse_value(std::string str)
+{
+  std::istringstream parser(str);
+  ElementType value;
+  parser >> value;
+  assert(!parser.fail());
+  return value;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ElementType>
+std::vector<ElementType> parse_string(std::string str,char delim)
+{
+  std::istringstream ss(str);
+  std::string token;
+  std::vector<ElementType> result;
+  while (std::getline(ss,token,delim)) {
+    result.push_back(parse_value<ElementType>(token));
+  }
+  return result;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ElementType>
+std::vector<std::vector<ElementType> > parse_file(const char* filename,char delim)
+{
+  std::ifstream fs(filename);
+  std::string line;
+  std::vector<std::vector<ElementType> > result;
+  while (std::getline(fs,line)) {
+    if (line[0] != '#') //skip comments
+      result.push_back(parse_string<ElementType>(line,delim));
+  }
+  return result;
+}
+//--------------------------------------------------------------------
+
 
 //--------------------------------------------------------------------
 // Convert float, double ... to string
 template<class T>
-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 <class T>
+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<class T>
-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<n-1; i++)
     myStream << clitk::toString<T>(t[i]) << " ";
@@ -46,7 +105,8 @@ std::string toStringVector(const T * t, const int n) {
 //--------------------------------------------------------------------
 // Convert float*, double* ... to string
 template<class T>
-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<n-1; i++)
     myStream << t[i] << " ";
@@ -58,7 +118,8 @@ std::string toStringVector(const T & t, const int n) {
 //--------------------------------------------------------------------
 // Convert float*, double* ... to string
 template<class T>
-std::string toStringVector(const std::vector<T> & t) {
+std::string toStringVector(const std::vector<T> & t)
+{
   return toStringVector(&t[0], t.size());
 }
 //--------------------------------------------------------------------
@@ -66,7 +127,8 @@ std::string toStringVector(const std::vector<T> & t) {
 //--------------------------------------------------------------------
 // Convert a pixel type to another (downcast)
 template<class TPixelUp, class TPixelDown>
-TPixelDown PixelTypeDownCast(const TPixelUp & x) {
+TPixelDown PixelTypeDownCast(const TPixelUp & x)
+{
   return (TPixelDown)lrint(x);
 }
 //--------------------------------------------------------------------
@@ -75,7 +137,7 @@ TPixelDown PixelTypeDownCast(const TPixelUp & x) {
 template<class Type>
 struct vectorComparisonLowerThan: public std::binary_function<int, int, bool> {
   vectorComparisonLowerThan(const std::vector<Type> & v):vect(v) {};
-  bool operator()(int x, int y) { 
+  bool operator()(int x, int y) {
     return (vect[x] < vect[y]);
   }
   const std::vector<Type> & vect;
@@ -86,7 +148,7 @@ struct vectorComparisonLowerThan: public std::binary_function<int, int, bool> {
 template<class Type>
 struct vectorComparisonGreaterThan: public std::binary_function<int, int, bool> {
   vectorComparisonGreaterThan(const std::vector<Type> & v):vect(v) {};
-  bool operator()(int x, int y) { 
+  bool operator()(int x, int y) {
     return (vect[x] > vect[y]);
   }
   const std::vector<Type> & vect;
@@ -95,23 +157,28 @@ struct vectorComparisonGreaterThan: public std::binary_function<int, int, bool>
 
 //--------------------------------------------------------------------
 template<class Type>
-void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing) {
-  index.resize(toSort.size());  
+void GetSortedIndex(const std::vector<Type> & toSort, std::vector<int> & index, bool increasing)
+{
+  index.resize(toSort.size());
   for(unsigned int i=0; i<index.size(); i++) index[i] = i;
-  if (increasing) 
-    std::sort(index.begin(), 
-             index.end(), 
-             vectorComparisonLowerThan<double>(toSort));
-  else 
-    std::sort(index.begin(), 
-             index.end(), 
-             vectorComparisonGreaterThan<double>(toSort));
+  if (increasing)
+    std::sort(index.begin(),
+              index.end(),
+              vectorComparisonLowerThan<double>(toSort));
+  else
+    std::sort(index.begin(),
+              index.end(),
+              vectorComparisonGreaterThan<double>(toSort));
 }
 //--------------------------------------------------------------------
 
 //--------------------------------------------------------------------
 template<class TPixel>
-std::string GetTypeAsString() {
+std::string GetTypeAsString()
+{
+  //  http://www.vtk.org/doc/release/3/html/vtkSetGet_8h-source.html
+  // and
+  // itkImageIOBase.cxx
   const std::type_info & PixType = typeid(TPixel);
   std::string pixelName;
   if (PixType == typeid(char)) pixelName = "char"; // 'plain" char is different from signed char and unsigned char ...
@@ -128,18 +195,21 @@ std::string GetTypeAsString() {
 }
 //--------------------------------------------------------------------
 
+
 //--------------------------------------------------------------------
 template<class ImageType>
-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<ImageType> ConstIteratorType; 
+  typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
   ConstIteratorType pi(input,input->GetLargestPossibleRegion());
-  pi.GoToBegin();  
-  typedef itk::ImageRegionIterator<ImageType> IteratorType; 
+  pi.GoToBegin();
+  typedef itk::ImageRegionIterator<ImageType> IteratorType;
   IteratorType po(output,input->GetLargestPossibleRegion());
-  po.GoToBegin(); 
+  po.GoToBegin();
   while (!pi.IsAtEnd()) {
     po.Set(pi.Get());
     ++pi;
@@ -148,5 +218,22 @@ void CloneImage(const typename ImageType::Pointer & input, typename ImageType::P
 }
 //--------------------------------------------------------------------
 
+
+//--------------------------------------------------------------------
+// http://stackoverflow.com/questions/771453/copy-map-values-to-vector-in-stl
+template <typename M, typename V> 
+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 <typename M, typename V> 
+void MapToVecSecond(const  M & m, V & v) {
+  for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) {
+    v.push_back( it->second );
+  }
+}
+//--------------------------------------------------------------------
+
 #endif /* end #define CLITKCOMMON_TXX */