ITK  4.8.0
Insight Segmentation and Registration Toolkit
itkFDFCommonImageIO.h
Go to the documentation of this file.
1 /* Copyright (C) 2004 Glenn Pierce.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Library General Public License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
17 
18 #ifndef _FDFCommonImageIO_H_
19 #define _FDFCommonImageIO_H_
20 
21 #include <itkFDFImageIO.h>
22 #include <itkIndent.h>
23 
24 #include <iostream>
25 #include <fstream>
26 #include <sstream>
27 #include <vector>
28 #include <iterator>
29 #include <algorithm>
30 
31 std::string RemoveCharacters( std::string, char );
32 
33 void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = " ");
34 
35 std::string ParseLine(std::string line);
36 
37 template <class T>
38 void ConvertFromString (std::string s, T &value)
39 {
40  std::stringstream str;
41  str << s;
42  str >> value;
43 }
44 
45 template <class T>
46 void StringToVector (std::string value, std::vector<T>& values)
47 {
48  std::vector<std::string> tokens;
49 
50  // value consists of something like {256,256}
51  std::string::size_type startBracketPosition = value.find_first_of("{", 0);
52  std::string::size_type endBracketPosition = value.find_first_of("}", startBracketPosition);
53 
54  if ( startBracketPosition != std::string::npos && endBracketPosition != std::string::npos) {
55  std::string elements = value.substr(startBracketPosition + 1, endBracketPosition - startBracketPosition - 1);
56 
57 
58  Tokenize(elements, tokens, ",");
59  }
60 
61  T element;
62 
63  for(unsigned int i=0; i<tokens.size(); i++) {
64  ConvertFromString(tokens[i], element);
65  values.push_back(element);
66  }
67 }
68 
69 template <class T>
70 void PrintVector (std::ostream& os, std::string name, const std::vector<T>& vect)
71 {
72  int size = vect.size();
73 
74  os << name << " {";
75 
76  for(int i=0; i < size; i++) {
77  os << vect[i];
78 
79  if (i < size - 1)
80  os << ", ";
81  }
82 
83  os << "}" << std::endl;
84 }
85 
86 #endif
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
void ConvertFromString(std::string s, T &value)
std::string ParseLine(std::string line)
std::string RemoveCharacters(std::string, char)
void PrintVector(std::ostream &os, std::string name, const std::vector< T > &vect)
void StringToVector(std::string value, std::vector< T > &values)