Skip to content

Commit 23beca4

Browse files
committed
Merge pull request #4501 from maxGimeno/OFF_reading-Fixes-maxGimeno
Fix OFF reader
2 parents 31416b1 + 26c86d2 commit 23beca4

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

Polyhedron/demo/Polyhedron/Plugins/IO/OFF_io_plugin.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ Polyhedron_demo_off_plugin::load_off(QFileInfo fileinfo) {
100100
// Try to read .off in a surface_mesh
101101
SMesh *surface_mesh = new SMesh();
102102
try{
103-
in >> *surface_mesh;
103+
if(!(in >> *surface_mesh))
104+
{
105+
surface_mesh->clear();
106+
}
104107
} catch(...)
105108
{
106109
surface_mesh->clear();

Polyhedron_IO/include/CGAL/IO/OFF_reader.h

+21-5
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,35 @@ namespace CGAL {
8585
polygons.resize(scanner.size_of_facets());
8686
if(scanner.has_colors())
8787
vcolors.resize(scanner.size_of_vertices());
88+
bool vcolored = false;
8889
for (std::size_t i = 0; i < scanner.size_of_vertices(); ++i) {
8990
double x, y, z, w;
9091
scanner.scan_vertex( x, y, z, w);
9192
CGAL_assertion(w!=0);
9293
IO::internal::fill_point( x/w, y/w, z/w, points[i] );
93-
if(scanner.has_colors())
94+
if(i == 0)
9495
{
95-
unsigned char r=0, g=0, b=0;
96-
scanner.scan_color( r, g, b);
97-
vcolors[i] = Color_rgb(r,g,b);
96+
std::string col;
97+
char ci;
98+
std::getline(in, col);
99+
std::istringstream iss(col);
100+
if(iss >> ci){
101+
std::istringstream iss2(col);
102+
vcolors[i] = scanner.get_color_from_line(iss2);
103+
vcolored = true;
104+
}
98105
}
99106
else
100-
scanner.skip_to_next_vertex(i);
107+
{
108+
if(vcolored){
109+
//stores the RGB value
110+
std::string col;
111+
std::getline(in, col);
112+
std::istringstream iss(col);
113+
vcolors[i] = scanner.get_color_from_line(iss);
114+
}
115+
}
116+
101117
if(!in)
102118
return false;
103119
}

Surface_mesh/include/CGAL/Surface_mesh/Surface_mesh.h

+23-9
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,7 @@ class Surface_mesh
22222222
is.setstate(std::ios::failbit);
22232223
return false;
22242224
}
2225+
is >> sm_skip_comments;
22252226
is >> n >> f >> e;
22262227
if(!is){
22272228
return false;
@@ -2239,39 +2240,52 @@ class Surface_mesh
22392240
boost::tie(vnormal, created) = sm.template add_property_map<Vertex_index,Vector_3>("v:normal",Vector_3(0,0,0));
22402241
v_has_normals = true;
22412242
}
2243+
std::string line;
22422244
char ci;
2243-
22442245
for(int i=0; i < n; i++){
22452246
is >> sm_skip_comments;
2247+
std::getline(is, line);
2248+
std::istringstream iss(line);
22462249
double x, y, z;
2247-
is >> iformat(x) >> iformat(y) >> iformat(z);
2250+
iss >> iformat(x) >> iformat(y) >> iformat(z);
22482251

22492252
Vertex_index vi = sm.add_vertex();
22502253
put(vpm, vi, P(x, y, z));
22512254

22522255

22532256
vertexmap[i] = vi;
22542257
if(v_has_normals){
2255-
is >> v;
2258+
if(!(iss >> v))
2259+
{
2260+
std::cerr<<"This doesn't seem to be a correct NOFF file. Aborting."<<std::endl;
2261+
is.setstate(std::ios::failbit);
2262+
return false;
2263+
}
22562264
vnormal[vi] = v;
22572265
}
22582266

22592267

22602268
if(i == 0 && ((off == "COFF") || (off == "CNOFF"))){
22612269
std::string col;
2262-
std::getline(is, col);
2263-
std::istringstream iss(col);
2264-
if(iss >> ci){
2270+
std::getline(iss, col);
2271+
std::istringstream iss2(col);
2272+
if(iss2 >> ci){
22652273
bool created;
22662274
boost::tie(vcolor, created) = sm.template add_property_map<Vertex_index,CGAL::Color>("v:color",CGAL::Color(0,0,0));
2267-
std::istringstream iss2(col);
2268-
vcolor[vi] = File_scanner_OFF::get_color_from_line(iss2);
2275+
std::istringstream iss3(col);
2276+
vcolor[vi] = File_scanner_OFF::get_color_from_line(iss3);
22692277
vcolored = true;
22702278
}
2279+
else
2280+
{
2281+
std::cerr<<"This doesn't seem to be a correct COFF file. Aborting."<<std::endl;
2282+
is.setstate(std::ios::failbit);
2283+
return false;
2284+
}
22712285
}else{
22722286
if(vcolored){
22732287
//stores the RGB value
2274-
vcolor[vi] = File_scanner_OFF::get_color_from_line(is);
2288+
vcolor[vi] = File_scanner_OFF::get_color_from_line(iss);
22752289
}
22762290
}
22772291
}

0 commit comments

Comments
 (0)