DGtal  1.5.beta
PointListReader.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file PointListReader.ih
19  * @author Bertrand Kerautret (\c kerautre@loria.fr )
20  * LORIA (CNRS, UMR 7503), University of Nancy, France
21  *
22  * @date 2011/03/31
23  *
24  * Implementation of inline methods defined in PointListReader.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 //////////////////////////////////////////////////////////////////////////////
34 #include <cstdlib>
35 #include <sstream>
36 #include <fstream>
37 #include <limits>
38 //////////////////////////////////////////////////////////////////////////////
39 
40 
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 // Implementation of inline methods //
44 
45 
46 
47 
48 template<typename TPoint>
49 inline
50 std::vector<TPoint>
51 DGtal::PointListReader<TPoint>::getPointsFromFile (const std::string &filename, std::vector<unsigned int> aVectPosition)
52 {
53  std::ifstream infile;
54  infile.open (filename.c_str(), std::ifstream::in);
55  return DGtal::PointListReader<TPoint>::getPointsFromInputStream(infile, aVectPosition);
56 }
57 
58 
59 
60 template<typename TPoint>
61 inline
62 std::vector<TPoint>
63 DGtal::PointListReader<TPoint>::getPointsFromInputStream (std::istream &in, std::vector<unsigned int> aVectPosition)
64 {
65  if(aVectPosition.size()==0){
66  for(unsigned int i=0; i<TPoint::dimension; i++){
67  aVectPosition.push_back(i);
68  }
69  }
70  std::vector<TPoint> vectResult;
71  std::string str;
72  getline(in, str );
73  while ( in.good() ){
74  if ( ( str != "" ) && ( str[ 0 ] != '#' ) ){
75  std::istringstream in_str( str );
76  unsigned int idx = 0;
77  std::string val;
78  unsigned int nbFound=0;
79  TPoint p;
80  while ( !in_str.fail()&& (nbFound<TPoint::dimension)){
81  std::operator>>(in_str,val);
82  if(!in_str.fail()){
83  std::istringstream valFromStr( val );
84  typename TPoint::Component valConverted;
85  valFromStr >> valConverted;
86  if(!valFromStr.fail()){
87  for(unsigned int j=0; j< TPoint::dimension; j++){
88  if (idx == aVectPosition.at(j) ){
89  nbFound++;
90  p[j]=valConverted;
91  }
92  }
93  }
94  }
95  ++idx;
96  }
97  if(nbFound==TPoint::dimension){
98  vectResult.push_back(p);
99  }
100  }
101  getline(in, str );
102  }
103  return vectResult;
104 }
105 
106 
107 
108 
109 template<typename TPoint>
110 inline
111 std::vector< std::vector<TPoint> >
112 DGtal::PointListReader<TPoint>::getPolygonsFromFile(const std::string &filename){
113  std::ifstream infile;
114  infile.open (filename.c_str(), std::ifstream::in);
115  return DGtal::PointListReader<TPoint>::getPolygonsFromInputStream(infile);
116 }
117 
118 
119 template<typename TPoint>
120 inline
121 std::vector< std::vector<TPoint> >
122 DGtal::PointListReader<TPoint>::getPolygonsFromInputStream(std::istream & in){
123  std::vector< std::vector< TPoint > > vectResult;
124  std::string str;
125  getline(in, str );
126  while ( in.good() ){
127  if ( ( str != "" ) && ( str[ 0 ] != '#' ) ){
128  std::vector <TPoint> aContour;
129  std::istringstream in_str( str );
130  std::string valStr="";
131  bool isOK = true;
132  TPoint p;
133  unsigned int index =0;
134  while ( in_str.good() ){
135  valStr="";
136  std::operator>>(in_str, valStr);
137  std::istringstream word_str( valStr );
138  word_str >> p[index];
139  isOK = isOK && !word_str.fail();
140  index++;
141  if(isOK && index % (TPoint::dimension)==0){
142  aContour.push_back(p);
143  index=0;
144  }
145  }
146  vectResult.push_back(aContour);
147  }
148 
149  getline(in, str );
150  }
151  return vectResult;
152 
153 }
154 
155 
156 
157 
158 
159 
160 
161 template<typename TPoint>
162 template<typename TInteger>
163 inline
164 std::vector< DGtal::FreemanChain< TInteger> >
165 DGtal::PointListReader<TPoint>::getFreemanChainsFromFile (const std::string &filename){
166  std::vector< FreemanChain< TInteger> > vectResult;
167  std::ifstream infile;
168  infile.open (filename.c_str(), std::ifstream::in);
169  std::string str;
170  getline(infile, str );
171  while ( infile.good() ){
172  if ( ( str != "" ) && ( str[ 0 ] != '#' ) ){
173  std::istringstream in_str( str );
174  int x0=0, y0=0;
175  std::string fcChain;
176  bool isOK = (in_str.operator>> ( x0 ) ) &&
177  (in_str.operator>> (y0) ) &&
178  std::operator>> (in_str, fcChain);
179  FreemanChain< TInteger> fc(fcChain, x0, y0);
180  if(isOK)
181  {
182  vectResult.push_back(fc);
183  }
184  else
185  {
186  std::cerr << "Ignoring entry invalid FreemanChain" << std::endl;
187  }
188  }
189  getline(infile, str );
190  }
191  return vectResult;
192 }
193 // //
194 ///////////////////////////////////////////////////////////////////////////////