DGtal 1.3.0
Loading...
Searching...
No Matches
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
48template<typename TPoint>
49inline
50std::vector<TPoint>
51DGtal::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
60template<typename TPoint>
61inline
62std::vector<TPoint>
63DGtal::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
109template<typename TPoint>
110inline
111std::vector< std::vector<TPoint> >
112DGtal::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
119template<typename TPoint>
120inline
121std::vector< std::vector<TPoint> >
122DGtal::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 std::operator>>(in_str, valStr);
136 std::istringstream word_str( valStr );
137 word_str >> p[index];
138 isOK = isOK && !word_str.fail();
139 index++;
140 if(isOK && index % (TPoint::dimension)==0){
141 aContour.push_back(p);
142 index=0;
143 }
144 }
145 vectResult.push_back(aContour);
146 }
147
148 getline(in, str );
149 }
150 return vectResult;
151
152}
153
154
155
156
157
158
159
160template<typename TPoint>
161template<typename TInteger>
162inline
163std::vector< DGtal::FreemanChain< TInteger> >
164DGtal::PointListReader<TPoint>::getFreemanChainsFromFile (const std::string &filename){
165 std::vector< FreemanChain< TInteger> > vectResult;
166 std::ifstream infile;
167 infile.open (filename.c_str(), std::ifstream::in);
168 std::string str;
169 getline(infile, str );
170 while ( infile.good() ){
171 if ( ( str != "" ) && ( str[ 0 ] != '#' ) ){
172 std::istringstream in_str( str );
173 int x0=0, y0=0;
174 std::string fcChain;
175 bool isOK = (in_str.operator>> ( x0 ) ) &&
176 (in_str.operator>> (y0) ) &&
177 std::operator>> (in_str, fcChain);
178 FreemanChain< TInteger> fc(fcChain, x0, y0);
179 if(isOK)
180 {
181 vectResult.push_back(fc);
182 }
183 else
184 {
185 std::cerr << "Ignoring entry invalid FreemanChain" << std::endl;
186 }
187 }
188 getline(infile, str );
189 }
190 return vectResult;
191}
192// //
193///////////////////////////////////////////////////////////////////////////////