From ebbe8571eeb16714bdebe9735059ec47901948e8 Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Thu, 8 Aug 2019 11:46:10 +0200 Subject: [PATCH] Move GOCAD's IO BGL code to BGL. --- BGL/include/CGAL/boost/graph/io.h | 108 ++++++++++ Stream_support/include/CGAL/IO/GOCAD.h | 44 ---- .../include/CGAL/IO/GOCAD/Build_from_gocad.h | 111 ++++++++++ .../include/CGAL/IO/GOCAD/GOCAD_internals.h | 195 ------------------ .../test/Stream_support/test_gocad.cpp | 2 +- 5 files changed, 220 insertions(+), 240 deletions(-) delete mode 100644 Stream_support/include/CGAL/IO/GOCAD.h create mode 100644 Stream_support/include/CGAL/IO/GOCAD/Build_from_gocad.h delete mode 100644 Stream_support/include/CGAL/IO/GOCAD/GOCAD_internals.h diff --git a/BGL/include/CGAL/boost/graph/io.h b/BGL/include/CGAL/boost/graph/io.h index ade5d54f684..934faf0fd5b 100644 --- a/BGL/include/CGAL/boost/graph/io.h +++ b/BGL/include/CGAL/boost/graph/io.h @@ -34,6 +34,7 @@ #include #include #include +#include namespace CGAL { /*! @@ -673,6 +674,113 @@ void write_vtp(std::ostream& os, write_vtp(os, mesh, CGAL::parameters::all_default()); } +namespace GOCAD_internal{ +//Use CRTP to gain access to the protected members without getters/setters. +template +class GOCAD_builder : public Build_from_gocad > +{ + typedef GOCAD_builder Self; + typedef Build_from_gocad Base; + typedef typename Base::Point_3 Point_3; + typedef typename Base::Points_3 Points_3; + typedef typename Base::Facet Facet; + typedef typename Base::Surface Surface; +public: + GOCAD_builder(std::istream& is_) + :Base(is_){} + void do_construct(Facegraph& graph) + { + typedef typename boost::graph_traits::vertex_descriptor + vertex_descriptor; + + std::vector vertices(this->meshPoints.size()); + for(std::size_t id = 0; id < this->meshPoints.size(); ++id) + { + vertices[id] = add_vertex( this->meshPoints[id], graph); + } + // graph.begin_surface( meshPoints.size(), mesh.size()); + typedef typename Points_3::size_type size_type; + + for(size_type i=0; i < this->mesh.size(); i++){ + std::array face; + face[0] = vertices[this->mesh[i].template get<0>()]; + face[1] = vertices[this->mesh[i].template get<1>()]; + face[2] = vertices[this->mesh[i].template get<2>()]; + + CGAL::Euler::add_face(face, graph); + } + } +}; +}//end GOCAD_internal + +template +bool +read_gocad(FaceGraph& polyhedron, std::istream& in, std::string& name, std::string& color) +{ + //typedef typename Polyhedron::HalfedgeDS HDS; + typedef typename boost::property_traits::type>::value_type Point_3; + + GOCAD_internal::GOCAD_builder builder(in); + builder(polyhedron); + name=builder.name; + color=builder.color; + + return in.good() && polyhedron.is_valid(); +} + +template +bool +write_gocad(FaceGraph& polyhedron, std::ostream& os, const std::string& name) +{ + os << "GOCAD TSurf 1\n" + "HEADER {\n" + "name:"; + os << name << std::endl; + os << "*border:on\n" + "*border*bstone:on\n" + "}\n" + "GOCAD_ORIGINAL_COORDINATE_SYSTEM\n" + "NAME Default\n" + "AXIS_NAME \"X\" \"Y\" \"Z\"\n" + "AXIS_UNIT \"m\" \"m\" \"m\"\n" + "ZPOSITIVE Elevation\n" + "END_ORIGINAL_COORDINATE_SYSTEM\n" + "TFACE\n"; + + os.precision(16); + typedef typename boost::property_map::type VPMap; + VPMap vpmap = get(CGAL::vertex_point, polyhedron); + std::map::vertex_descriptor, int> id_map; + { + typename boost::graph_traits::vertex_iterator it, end; + it = vertices(polyhedron).begin(); + end = vertices(polyhedron).end(); + int i=0; + for(; it != end; ++it){ + id_map[*it] = i; + os << "VRTX " << i << " " << get(vpmap, *it) << "\n"; + ++i; + } + } + + { + typename boost::graph_traits::face_iterator it, end; + it = faces(polyhedron).begin(); + end = faces(polyhedron).end(); + for(; it != end; ++it){ + os << "TRGL " << id_map[target(prev(halfedge(*it, polyhedron), polyhedron), polyhedron)] << " " + << id_map[target(halfedge(*it, polyhedron), polyhedron)] << " " + << id_map[target(next(halfedge(*it, polyhedron), polyhedron), polyhedron)] << "\n"; + } + } + + os << "END" << std::endl; + + return true; +} + + } // namespace CGAL + #endif // CGAL_BOOST_GRAPH_IO_H diff --git a/Stream_support/include/CGAL/IO/GOCAD.h b/Stream_support/include/CGAL/IO/GOCAD.h deleted file mode 100644 index cbce12b6785..00000000000 --- a/Stream_support/include/CGAL/IO/GOCAD.h +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2019 GeometryFactory -// -// This file is part of CGAL (www.cgal.org); you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public License as -// published by the Free Software Foundation; either version 3 of the License, -// or (at your option) any later version. -// -// Licensees holding a valid commercial license may use this file in -// accordance with the commercial license agreement provided with the software. -// -// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE -// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. -// -// $URL$ -// $Id$ -// SPDX-License-Identifier: LGPL-3.0+ -// -// Author(s) : Maxime Gimeno - -#ifndef CGAL_IO_GOCAD_H -#define CGAL_IO_GOCAD_H - -#include -#include -#include - -namespace CGAL{ -template -bool -read_gocad(FaceGraph& facegraph, std::istream& in, std::string& name, std::string& color) -{ - return GOCAD_internal::read_gocad(facegraph, in, name, color); -} - -template -bool -write_gocad(FaceGraph& facegraph, std::ostream& os, const std::string& name) -{ - return GOCAD_internal::write_gocad(facegraph, os, name); -} - -}//end CGAL - -#endif //CGAL_IO_GOCAD_H diff --git a/Stream_support/include/CGAL/IO/GOCAD/Build_from_gocad.h b/Stream_support/include/CGAL/IO/GOCAD/Build_from_gocad.h new file mode 100644 index 00000000000..d2bb22c0821 --- /dev/null +++ b/Stream_support/include/CGAL/IO/GOCAD/Build_from_gocad.h @@ -0,0 +1,111 @@ +// Copyright (c) 2019 GeometryFactory +// +// This file is part of CGAL (www.cgal.org); you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 3 of the License, +// or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: LGPL-3.0+ +// +// Author(s) : Maxime Gimeno + +#ifndef CGAL_IO_GOCAD_BUILD_FROM_GOCAD_H +#define CGAL_IO_GOCAD_BUILD_FROM_GOCAD_H + +#include +#include +#include +#include +#include + +namespace CGAL{ + +namespace GOCAD_internal { +template +class Build_from_gocad +{ +protected: + typedef P Point_3; + typedef std::deque Points_3; + typedef boost::tuple Facet; + typedef std::deque Surface; + +public: + std::string name, color; + Build_from_gocad(std::istream& is_) + : is(is_), counter(0) + {} + + void do_construct(){} //specific to Facegraph (declared in BGL) + void + read(std::istream& input, Points_3& points, Surface& surface, int offset = 0) + { + char c; + std::string s, tface("TFACE"); + int i,j,k; + Point_3 p; + bool vertices_read = false; + while(input >> s){ + if(s == tface){ + break; + } + std::string::size_type idx; + + if((idx = s.find("name")) != std::string::npos){ + std::istringstream str(s.substr(idx+5)); + str >> name; + } + if((idx = s.find("color")) != std::string::npos){ + std::istringstream str(s.substr(idx+6)); + str >> color; + } + } + std::getline(input, s); + + while(input.get(c)){ + if((c == 'V')||(c == 'P')){ + input >> s >> i >> p; + if(! vertices_read){ + vertices_read = true; + offset -= i; // Some files start with index 0 others with 1 + } + + points.push_back(p); + + } else if(vertices_read && (c == 'T')){ + input >> c >> c >> c >> i >> j >> k; + surface.push_back(boost::make_tuple(offset+i, offset+j, offset+k)); + } else if(c == 'E'){ + break; + } + std::getline(input, s); + } + } + + void operator()( Facegraph& graph) + { + read(this->is, this->meshPoints, this->mesh); + static_cast(this)->do_construct(graph); + } + +protected: + std::istream& is; + int counter; + Points_3 meshPoints; + Surface mesh; +}; + + + +}//end GOCAD_internal +}//end CGAL +#endif // CGAL_IO_GOCAD_BUILD_FROM_GOCAD_H + diff --git a/Stream_support/include/CGAL/IO/GOCAD/GOCAD_internals.h b/Stream_support/include/CGAL/IO/GOCAD/GOCAD_internals.h deleted file mode 100644 index 843f5f6434b..00000000000 --- a/Stream_support/include/CGAL/IO/GOCAD/GOCAD_internals.h +++ /dev/null @@ -1,195 +0,0 @@ -// Copyright (c) 2019 GeometryFactory -// -// This file is part of CGAL (www.cgal.org); you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public License as -// published by the Free Software Foundation; either version 3 of the License, -// or (at your option) any later version. -// -// Licensees holding a valid commercial license may use this file in -// accordance with the commercial license agreement provided with the software. -// -// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE -// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. -// -// $URL$ -// $Id$ -// SPDX-License-Identifier: LGPL-3.0+ -// -// Author(s) : Maxime Gimeno - -#ifndef CGAL_IO_GOCAD_GOCAD_INTERNALS_H -#define CGAL_IO_GOCAD_GOCAD_INTERNALS_H - -#include -#include -#include -#include -#include -#include - -namespace CGAL{ - -namespace GOCAD_internal { -template -class Build_from_gocad_BGL -{ - typedef P Point_3; - typedef std::deque Points_3; - typedef boost::tuple Facet; - typedef std::deque Surface; - - std::istream& is; - int counter; - Points_3 meshPoints; - Surface mesh; - -public: - - std::string name, color; - - - Build_from_gocad_BGL(std::istream& is_) - : is(is_), counter(0) - {} - - void operator()( Facegraph& graph) { - read(is, meshPoints, mesh); - - typedef typename boost::graph_traits::vertex_descriptor - vertex_descriptor; - - std::vector vertices(meshPoints.size()); - for(std::size_t id = 0; id < meshPoints.size(); ++id) - { - vertices[id] = add_vertex( meshPoints[id], graph); - } -// graph.begin_surface( meshPoints.size(), mesh.size()); - typedef typename Points_3::size_type size_type; - - for(size_type i=0; i < mesh.size(); i++){ - std::array face; - face[0] = vertices[mesh[i].template get<0>()]; - face[1] = vertices[mesh[i].template get<1>()]; - face[2] = vertices[mesh[i].template get<2>()]; - - CGAL::Euler::add_face(face, graph); - } - } - - void - read(std::istream& input, Points_3& points, Surface& surface, int offset = 0) - { - char c; - std::string s, tface("TFACE"); - int i,j,k; - Point_3 p; - bool vertices_read = false; - while(input >> s){ - if(s == tface){ - break; - } - std::string::size_type idx; - - if((idx = s.find("name")) != std::string::npos){ - std::istringstream str(s.substr(idx+5)); - str >> name; - } - if((idx = s.find("color")) != std::string::npos){ - std::istringstream str(s.substr(idx+6)); - str >> color; - } - } - std::getline(input, s); - - while(input.get(c)){ - if((c == 'V')||(c == 'P')){ - input >> s >> i >> p; - if(! vertices_read){ - vertices_read = true; - offset -= i; // Some files start with index 0 others with 1 - } - - points.push_back(p); - - } else if(vertices_read && (c == 'T')){ - input >> c >> c >> c >> i >> j >> k; - surface.push_back(boost::make_tuple(offset+i, offset+j, offset+k)); - } else if(c == 'E'){ - break; - } - std::getline(input, s); - } - } - -}; - -template -bool -read_gocad(FaceGraph& polyhedron, std::istream& in, std::string& name, std::string& color) -{ - //typedef typename Polyhedron::HalfedgeDS HDS; - typedef typename boost::property_traits::type>::value_type Point_3; - - Build_from_gocad_BGL builder(in); - builder(polyhedron); - name=builder.name; - color=builder.color; - - return in.good() && polyhedron.is_valid(); -} - -template -bool -write_gocad(FaceGraph& polyhedron, std::ostream& os, const std::string& name) -{ - os << "GOCAD TSurf 1\n" - "HEADER {\n" - "name:"; - os << name << std::endl; - os << "*border:on\n" - "*border*bstone:on\n" - "}\n" - "GOCAD_ORIGINAL_COORDINATE_SYSTEM\n" - "NAME Default\n" - "AXIS_NAME \"X\" \"Y\" \"Z\"\n" - "AXIS_UNIT \"m\" \"m\" \"m\"\n" - "ZPOSITIVE Elevation\n" - "END_ORIGINAL_COORDINATE_SYSTEM\n" - "TFACE\n"; - - os.precision(16); - typedef typename boost::property_map::type VPMap; - VPMap vpmap = get(CGAL::vertex_point, polyhedron); - std::map::vertex_descriptor, int> id_map; - { - typename boost::graph_traits::vertex_iterator it, end; - it = vertices(polyhedron).begin(); - end = vertices(polyhedron).end(); - int i=0; - for(; it != end; ++it){ - id_map[*it] = i; - os << "VRTX " << i << " " << get(vpmap, *it) << "\n"; - ++i; - } - } - - { - typename boost::graph_traits::face_iterator it, end; - it = faces(polyhedron).begin(); - end = faces(polyhedron).end(); - for(; it != end; ++it){ - os << "TRGL " << id_map[target(prev(halfedge(*it, polyhedron), polyhedron), polyhedron)] << " " - << id_map[target(halfedge(*it, polyhedron), polyhedron)] << " " - << id_map[target(next(halfedge(*it, polyhedron), polyhedron), polyhedron)] << "\n"; - } - } - - os << "END" << std::endl; - - return true; -} - -}//end GOCAD_internal -}//end CGAL -#endif // CGAL_IO_GOCAD_GOCAD_INTERNALS_H - diff --git a/Stream_support/test/Stream_support/test_gocad.cpp b/Stream_support/test/Stream_support/test_gocad.cpp index db85bddc8cd..dfa0cc9c6d0 100644 --- a/Stream_support/test/Stream_support/test_gocad.cpp +++ b/Stream_support/test/Stream_support/test_gocad.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include #include