ITK  4.10.0
Insight Segmentation and Registration Toolkit
itkFDFCommonImageIO.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkFDFCommonImageIO_h
19 #define itkFDFCommonImageIO_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 namespace itk
32 {
33 
34 std::string RemoveCharacters( std::string, char );
35 
36 void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = " ");
37 
38 std::string ParseLine(std::string line);
39 
40 template <class T>
41 void ConvertFromString (std::string s, T &value)
42 {
43  std::stringstream str;
44  str << s;
45  str >> value;
46 }
47 
48 template <class T>
49 void StringToVector (std::string value, std::vector<T>& values)
50 {
51  std::vector<std::string> tokens;
52 
53  // value consists of something like {256,256}
54  std::string::size_type startBracketPosition = value.find_first_of("{", 0);
55  std::string::size_type endBracketPosition = value.find_first_of("}", startBracketPosition);
56 
57  if ( startBracketPosition != std::string::npos && endBracketPosition != std::string::npos) {
58  std::string elements = value.substr(startBracketPosition + 1, endBracketPosition - startBracketPosition - 1);
59 
60 
61  Tokenize(elements, tokens, ",");
62  }
63 
64  T element;
65 
66  for(unsigned int i=0; i<tokens.size(); i++) {
67  ConvertFromString(tokens[i], element);
68  values.push_back(element);
69  }
70 }
71 
72 template <class T>
73 void PrintVector (std::ostream& os, std::string name, const std::vector<T>& vect)
74 {
75  int size = vect.size();
76 
77  os << name << " {";
78 
79  for(int i=0; i < size; i++) {
80  os << vect[i];
81 
82  if (i < size - 1)
83  os << ", ";
84  }
85 
86  os << "}" << std::endl;
87 }
88 
89 }
90 
91 #endif
void StringToVector(std::string value, std::vector< T > &values)
void PrintVector(std::ostream &os, std::string name, const std::vector< T > &vect)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes...
Definition: itkArray.h:30
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
std::string RemoveCharacters(std::string, char)
std::string ParseLine(std::string line)
void ConvertFromString(std::string s, T &value)