Use polygon soup OFF reader in polygon mesh OFF reader

This commit is contained in:
Mael Rouxel-Labbé 2020-01-24 18:13:09 +01:00
parent 423ae6ec84
commit 7bb2788d6f
4 changed files with 49 additions and 106 deletions

View File

@ -46,7 +46,6 @@ class GOCAD_builder
public: public:
GOCAD_builder(std::istream& is_) : Base(is_) { } GOCAD_builder(std::istream& is_) : Base(is_) { }
// Implementation of the two functions required by the generic builder
bool read(std::istream& input, bool read(std::istream& input,
Point_container& points, Point_container& points,
Face_container& faces) Face_container& faces)

View File

@ -51,7 +51,6 @@ class OBJ_builder
public: public:
OBJ_builder(std::istream& is_) : Base(is_) { } OBJ_builder(std::istream& is_) : Base(is_) { }
// Implementation of the two functions required by the generic builder
bool read(std::istream& input, Point_container& points, Face_container& faces) bool read(std::istream& input, Point_container& points, Face_container& faces)
{ {
return read_OBJ(input, points, faces); return read_OBJ(input, points, faces);

View File

@ -11,6 +11,9 @@
#ifndef CGAL_BGL_IO_OFF_H #ifndef CGAL_BGL_IO_OFF_H
#define CGAL_BGL_IO_OFF_H #define CGAL_BGL_IO_OFF_H
#include <CGAL/boost/graph/IO/Generic_facegraph_builder.h>
#include <CGAL/IO/OFF.h>
#include <CGAL/assertions.h> #include <CGAL/assertions.h>
#include <CGAL/boost/graph/Euler_operations.h> #include <CGAL/boost/graph/Euler_operations.h>
#include <CGAL/boost/graph/Named_function_parameters.h> #include <CGAL/boost/graph/Named_function_parameters.h>
@ -32,32 +35,28 @@ namespace CGAL {
namespace IO { namespace IO {
namespace internal { namespace internal {
namespace read_off_tools {
inline bool is_whitespace(const std::string& s) // Use CRTP to gain access to the protected members without getters/setters.
template <typename FaceGraph, typename Point>
class OFF_builder
: public Generic_facegraph_builder<FaceGraph, Point, OFF_builder<FaceGraph, Point> >
{ {
for(unsigned int i=0; i<s.size(); ++i) typedef OFF_builder<FaceGraph, Point> Self;
typedef Generic_facegraph_builder<FaceGraph, Point, Self> Base;
typedef typename Base::Point_container Point_container;
typedef typename Base::Face Face;
typedef typename Base::Face_container Face_container;
public:
OFF_builder(std::istream& is_) : Base(is_) { }
bool read(std::istream& input, Point_container& points, Face_container& faces)
{ {
if(s[i] != ' ' && s[i] != '\t') return read_OFF(input, points, faces);
return false;
} }
};
return true;
}
inline std::string next_non_comment(std::istream& is)
{
std::string line;
do
{
std::getline(is, line);
}
while(line[0] == '#' || is_whitespace(line));
return line;
}
} // namespace read_off_tools
} // namespace internal } // namespace internal
} // namespace IO } // namespace IO
@ -76,80 +75,18 @@ inline std::string next_non_comment(std::istream& is)
\pre The data must represent a 2-manifold \pre The data must represent a 2-manifold
\attention The graph `g` is not cleared, and the data from the stream are added.
\see \ref IOStreamOFF \see \ref IOStreamOFF
*/ */
template <typename FaceGraph, typename CGAL_BGL_NP_TEMPLATE_PARAMETERS> template <typename FaceGraph, typename CGAL_BGL_NP_TEMPLATE_PARAMETERS>
bool read_OFF(std::istream& is, bool read_OFF(std::istream& in,
FaceGraph& g, FaceGraph& g,
const CGAL_BGL_NP_CLASS& np) const CGAL_BGL_NP_CLASS& np)
{ {
typedef typename boost::graph_traits<FaceGraph>::vertex_descriptor vertex_descriptor; typedef typename CGAL::GetVertexPointMap<FaceGraph, CGAL_BGL_NP_CLASS>::type VPM;
typedef typename boost::graph_traits<FaceGraph>::vertices_size_type vertices_size_type; typedef typename boost::property_traits<VPM>::value_type Point;
typedef typename boost::graph_traits<FaceGraph>::face_descriptor face_descriptor;
typedef typename boost::graph_traits<FaceGraph>::faces_size_type faces_size_type;
typedef typename CGAL::GetVertexPointMap<FaceGraph, CGAL_BGL_NP_CLASS>::type VPM; IO::internal::OFF_builder<FaceGraph, Point> builder(in);
typedef typename boost::property_traits<VPM>::value_type Point; return builder(g, np);
using namespace IO::internal::read_off_tools;
using parameters::choose_parameter;
using parameters::get_parameter;
VPM vpm = choose_parameter(get_parameter(np, internal_np::vertex_point),
get_property_map(CGAL::vertex_point, g));
vertices_size_type nv, nvf;
faces_size_type nf;
int ignore;
std::string line = next_non_comment(is);
{
std::istringstream iss(line);
std::string off;
iss >> off;
CGAL_assertion( off == "OFF" || off == "COFF");
}
line = next_non_comment(is);
{
std::istringstream iss(line);
iss >> nv >> nf >> ignore;
}
std::vector<vertex_descriptor> vertices(nv);
Point p;
for(vertices_size_type i=0; i<nv; ++i)
{
line = next_non_comment(is);
std::istringstream iss(line);
iss >> p;
vertices[i] = add_vertex(g);
put(vpm, vertices[i], p);
}
for(faces_size_type i=0; i<nf; ++i)
{
line = next_non_comment(is);
std::istringstream iss(line);
iss >> nvf;
std::vector<vertex_descriptor> face(nvf);
for(vertices_size_type j=0; j<nvf; ++j)
{
faces_size_type fvi;
iss >> fvi;
face[j] = vertices[fvi];
}
face_descriptor f = CGAL::Euler::add_face(face, g);
if(f == boost::graph_traits<FaceGraph>::null_face())
return false;
}
return true;
} }
/*! /*!

View File

@ -23,6 +23,10 @@
namespace CGAL { namespace CGAL {
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Read
/*! /*!
* \ingroup IOstreamFunctions * \ingroup IOstreamFunctions
* *
@ -46,16 +50,16 @@ bool read_OFF(std::istream& in,
if(scanner.has_colors()) if(scanner.has_colors())
vcolors.resize(scanner.size_of_vertices()); vcolors.resize(scanner.size_of_vertices());
for(std::size_t i = 0; i < scanner.size_of_vertices(); ++i) for(std::size_t i=0; i<scanner.size_of_vertices(); ++i)
{ {
double x, y, z, w; double x, y, z, w;
scanner.scan_vertex( x, y, z, w); scanner.scan_vertex(x, y, z, w);
CGAL_assertion(w!=0); CGAL_assertion(w!=0);
IO::internal::fill_point( x/w, y/w, z/w, points[i] ); IO::internal::fill_point(x/w, y/w, z/w, points[i]);
if(scanner.has_colors()) if(scanner.has_colors())
{ {
unsigned char r=0, g=0, b=0; unsigned char r=0, g=0, b=0;
scanner.scan_color( r, g, b); scanner.scan_color(r, g, b);
vcolors[i] = Color_rgb(r,g,b); vcolors[i] = Color_rgb(r,g,b);
} }
else else
@ -68,10 +72,10 @@ bool read_OFF(std::istream& in,
} }
bool has_fcolors = false; bool has_fcolors = false;
for(std::size_t i = 0; i < scanner.size_of_facets(); ++i) for(std::size_t i=0; i<scanner.size_of_facets(); ++i)
{ {
std::size_t no; std::size_t no;
scanner.scan_facet( no, i); scanner.scan_facet(no, i);
if(!in) if(!in)
return false; return false;
@ -130,25 +134,25 @@ bool read_OFF(std::istream& in,
points.resize(scanner.size_of_vertices()); points.resize(scanner.size_of_vertices());
polygons.resize(scanner.size_of_facets()); polygons.resize(scanner.size_of_facets());
for(std::size_t i = 0; i < scanner.size_of_vertices(); ++i) for(std::size_t i=0; i<scanner.size_of_vertices(); ++i)
{ {
double x, y, z, w; double x, y, z, w;
scanner.scan_vertex( x, y, z, w); scanner.scan_vertex(x, y, z, w);
CGAL_assertion(w!=0); CGAL_assertion(w != 0);
IO::internal::fill_point( x/w, y/w, z/w, points[i] ); IO::internal::fill_point(x/w, y/w, z/w, points[i]);
scanner.skip_to_next_vertex( i); scanner.skip_to_next_vertex(i);
} }
if(!in) if(!in)
return false; return false;
for(std::size_t i = 0; i < scanner.size_of_facets(); ++i) for(std::size_t i=0; i<scanner.size_of_facets(); ++i)
{ {
std::size_t no; std::size_t no;
scanner.scan_facet( no, i); scanner.scan_facet(no, i);
IO::internal::resize(polygons[i], no); IO::internal::resize(polygons[i], no);
for(std::size_t j = 0; j < no; ++j) for(std::size_t j=0; j<no; ++j)
{ {
std::size_t id; std::size_t id;
scanner.scan_facet_vertex_index(id, i); scanner.scan_facet_vertex_index(id, i);
@ -162,6 +166,10 @@ bool read_OFF(std::istream& in,
return in.good(); return in.good();
} }
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Write
/*! /*!
* \ingroup IOstreamFunctions * \ingroup IOstreamFunctions
* *
@ -177,10 +185,10 @@ bool write_OFF(std::ostream& out,
CGAL::File_writer_OFF writer; CGAL::File_writer_OFF writer;
writer.write_header(out, points.size(), 0, polygons.size()); writer.write_header(out, points.size(), 0, polygons.size());
for(std::size_t i = 0, end = points.size(); i < end; ++i) for(std::size_t i = 0, end = points.size(); i<end; ++i)
{ {
const Point_3& p = points[i]; const Point_3& p = points[i];
writer.write_vertex( p.x(), p.y(), p.z() ); writer.write_vertex(p.x(), p.y(), p.z());
} }
writer.write_facet_header(); writer.write_facet_header();