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.
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.
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/>.
19 * @author Bertrand Kerautret (\c kerautre@loria.fr )
20 * LORIA (CNRS, UMR 7503), University of Nancy, France
24 * Implementation of inline methods defined in MeshReader.h
26 * This file is part of the DGtal library.
29 ///////////////////////////////////////////////////////////////////////////////
30 // IMPLEMENTATION of inline methods.
31 ///////////////////////////////////////////////////////////////////////////////
33 //////////////////////////////////////////////////////////////////////////////
39 //////////////////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////////////////
44 // Implementation of inline methods //
49 template <typename TPoint>
52 DGtal::MeshReader<TPoint>::importOFFFile(const std::string & aFilename,
53 DGtal::Mesh<TPoint> & aMesh,
54 bool invertVertexOrder)
57 DGtal::IOException dgtalio;
60 infile.open (aFilename.c_str(), std::ifstream::in);
64 trace.error() << "MeshReader : can't open " << aFilename << std::endl;
68 getline( infile, str );
70 if ( ! infile.good() )
72 trace.error() << "MeshReader : can't read " << aFilename << std::endl;
75 if ( str.substr(0,3) != "OFF" && str.substr(0,4) != "NOFF")
77 std::cerr <<"*" <<str<<"*"<< std::endl;
78 trace.error() << "MeshReader : No OFF or NOFF format in " << aFilename << std::endl;
81 if ( str.substr(0,4) == "NOFF")
83 trace.warning() << "MeshReader : reading NOFF format from importOFFFile (normal vectors will be ignored)..." << std::endl;
86 // Processing comments
89 getline( infile, str );
90 if ( ! infile.good() ){
91 trace.error() << "MeshReader : Invalid format in " << aFilename << std::endl;
95 while ( str[ 0 ] == '#' || str=="");
96 std::istringstream str_in( str );
97 int nbPoints, nbFaces, nbEdges;
102 // Reading mesh vertex
103 for(int i=0; i<nbPoints; i++){
109 // Needed since a line can also contain vertex colors
110 getline(infile, str);
113 // Reading mesh faces
114 for(int i=0; i<nbFaces; i++){
115 // Reading the number of face vertex
116 unsigned int aNbFaceVertex;
117 infile >> aNbFaceVertex;
118 std::vector<unsigned int> aFace;
119 for (unsigned int j=0; j< aNbFaceVertex; j++){
120 unsigned int anIndex;
122 aFace.push_back(anIndex);
124 if( invertVertexOrder ){
125 for(unsigned int j=0; j < aFace.size()/2; j++){
126 unsigned int tmp=aFace.at(j);
127 aFace.at(j)=aFace.at(aFace.size()-1-j);
128 aFace.at(aFace.size()-1-j)=tmp;
132 // Needed since a can also contain vertex colors
133 getline(infile, str);
135 bool findValidColor=true;
137 std::istringstream inColor(str);
138 double colorR, colorG, colorB, colorT;
139 findValidColor=inColor.good();
140 if(findValidColor && inColor.good()){
143 findValidColor &=!inColor.fail();
144 if(findValidColor && inColor.good()){
147 findValidColor &=!inColor.fail();
148 if(findValidColor && inColor.good()){
151 findValidColor &=!inColor.fail();
153 if(findValidColor && inColor.good()){
155 // Since alpha is optional:
164 DGtal::Color c((unsigned int)(colorR*255.0), (unsigned int)(colorG*255.0),
165 (unsigned int)(colorB*255.0), (unsigned int)(colorT*255.0));
166 aMesh.addFace(aFace, c);
168 aMesh.addFace(aFace);
171 aMesh.addFace(aFace);
183 template <typename TPoint>
186 DGtal::MeshReader<TPoint>::importOFSFile(const std::string & aFilename,
187 DGtal::Mesh<TPoint> & aMesh,
188 bool invertVertexOrder, double scale)
190 std::ifstream infile;
191 DGtal::IOException dgtalio;
194 infile.open (aFilename.c_str(), std::ifstream::in);
198 trace.error() << "MeshReader : can't open " << aFilename << std::endl;
202 getline( infile, str );
204 if ( ! infile.good() )
206 trace.error() << "MeshReader : can't read " << aFilename << std::endl;
209 if ( str.substr(0,3) != "OFS")
211 trace.error() << "MeshReader : No OFS format in " << aFilename << std::endl;
215 // Processing comments
218 getline( infile, str );
219 if ( ! infile.good() ){
220 trace.error() << "MeshReader : Invalid format in " << aFilename << std::endl;
224 while ( str[ 0 ] == '#' || str=="");
225 std::istringstream str_in( str );
229 // Reading mesh vertex
230 for(int i=0; i<nbPoints; i++){
239 // Needed since a line can also contain vertex colors
240 getline(infile, str);
244 getline( infile, str );
245 if ( ! infile.good() ){
246 trace.error() << "MeshReader : Invalid format in " << aFilename << std::endl;
250 while ( str[ 0 ] == '#' || str=="");
251 std::istringstream str_in2( str );
252 unsigned int nbFaces;
254 // Reading mesh faces
255 for(unsigned int i=0; i<nbFaces; i++){
256 // Reading the number of face vertex
257 std::vector<unsigned int> aFace;
258 for (unsigned int j=0; j< 3; j++){
259 unsigned int anIndex;
261 aFace.push_back(anIndex);
263 if( invertVertexOrder ){
264 unsigned int tmp=aFace.at(0);
265 aFace.at(0)=aFace.at(2);
268 aMesh.addFace(aFace);
269 getline(infile, str);
278 template <typename TPoint>
280 DGtal::operator<< ( Mesh<TPoint> & mesh, const std::string &filename ){
281 std::string extension = filename.substr(filename.find_last_of(".") + 1);
282 if(extension== "off") {
283 DGtal::MeshReader<TPoint>::importOFFFile(filename, mesh);
285 }else if(extension== "ofs") {
286 DGtal::MeshReader< TPoint>::importOFSFile(filename, mesh);
300 ///////////////////////////////////////////////////////////////////////////////