]> Creatis software - clitk.git/commitdiff
move parsing utilities to clitkCommon as we are using them more and more
authorschaerer <schaerer>
Thu, 1 Jul 2010 16:15:20 +0000 (16:15 +0000)
committerschaerer <schaerer>
Thu, 1 Jul 2010 16:15:20 +0000 (16:15 +0000)
common/clitkCommon.h
common/clitkCommon.txx
common/clitkDicomRT_Contour.h
vv/vvMeshReader.cxx

index 3f6e72db4d7099d92ed3d1d42e6c19e7aea9868e..410c97683a3404eba2a8a5c7822bd593f87dddd4 100644 (file)
@@ -29,6 +29,8 @@
 #include <itkImageRegionConstIterator.h>
 #include <itkImageRegionIterator.h>
 
+#include <fstream>
+
 //--------------------------------------------------------------------
 namespace clitk {
 
index c2e9a4ae3f92acca36287309d416971cc3a1e574..d7c91405dda8f45ff76138c341784f4d05b16b4c 100644 (file)
    *
    -------------------------------------------------*/
 
+//-------------------------------------------------------
+// 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>
index b59976969eaac0d6ce3808f25ef0bf88c2f60189..85ebe49b83d56d8034414e9e4c0131f77e1cbc47 100644 (file)
@@ -51,33 +51,6 @@ namespace clitk {
   };
   //--------------------------------------------------------------------
 
-  //--------------------------------------------------------------------
-  template<class ElementType>
-  ElementType parse_value(std::string str)
-  {
-    std::istringstream parser(str);
-    ElementType value;
-    parser >> value;
-    if (parser.fail()) {
-      DD(str);
-      DD(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 (getline(ss,token,delim))
-      {
-        result.push_back(parse_value<ElementType>(token));
-      }
-    return result;
-  }
-  //--------------------------------------------------------------------
 
 
 } // end namespace clitk
index d5100b1e336218485b608022da329ade80515765..152a9a3860e1758ebf74abb64ee22b0b6f7e0a93 100644 (file)
@@ -35,6 +35,7 @@
 #include "clitkCommon.h"
 #include "vvMeshReader.h"
 #include "vvProgressDialog.h"
+#include <clitkCommon.h>
 
 vvMeshReader::vvMeshReader() :
   vtk_mode(false)
@@ -76,28 +77,6 @@ void vvMeshReader::run()
   }
 }
 
-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 (getline(ss,token,delim)) {
-    result.push_back(parse_value<ElementType>(token));
-  }
-  return result;
-}
-
 std::vector<std::pair<int,std::string> > vvMeshReader::GetROINames()
 {
   assert(filename!="");
@@ -137,7 +116,7 @@ std::vector<vvMesh::Pointer> vvMeshReader::readSelectedContours()
     ss >> roi_number;
     if (std::find(selected_contours.begin(),selected_contours.end(),roi_number) != selected_contours.end()) { //Only read selected ROIs
       vvMesh::Pointer current_roi=vvMesh::New();
-      std::vector<double> rgb=parse_string<double>(i->GetEntryValue(0x3006,0x002a),'\\');
+      std::vector<double> rgb=clitk::parse_string<double>(i->GetEntryValue(0x3006,0x002a),'\\');
       assert(rgb.size()==3);
       current_roi->r=rgb[0]/255;
       current_roi->g=rgb[1]/255;
@@ -148,8 +127,8 @@ std::vector<vvMesh::Pointer> vvMeshReader::readSelectedContours()
       for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) { //loop over 2D contours
         std::string contour_type=j->GetEntryValue(0x3006,0x0042);
         if (contour_type=="CLOSED_PLANAR ") {
-          int point_number=parse_value<int>(j->GetEntryValue(0x3006,0x0046));
-          std::vector<float> points=parse_string<float>(j->GetEntryValue(0x3006,0x0050),'\\');
+          int point_number=clitk::parse_value<int>(j->GetEntryValue(0x3006,0x0046));
+          std::vector<float> points=clitk::parse_string<float>(j->GetEntryValue(0x3006,0x0050),'\\');
           assert(points.size() == static_cast<unsigned int>(point_number)*3);
           if (z0 == -1) //First contour
             z0=points[2];