mirror of https://github.com/CGAL/cgal
Move STL bgl writer into BGL, and replace it by a soup writer in Stream_support.
Write a SS test for read and write soups in STL
This commit is contained in:
parent
c15dd227e0
commit
6dbf2d3d78
|
|
@ -33,6 +33,8 @@
|
||||||
#include <CGAL/boost/graph/helpers.h>
|
#include <CGAL/boost/graph/helpers.h>
|
||||||
#include <CGAL/boost/graph/named_params_helper.h>
|
#include <CGAL/boost/graph/named_params_helper.h>
|
||||||
#include <CGAL/boost/graph/named_function_params.h>
|
#include <CGAL/boost/graph/named_function_params.h>
|
||||||
|
#include <CGAL/boost/graph/properties.h>
|
||||||
|
#include <CGAL/Kernel_traits.h>
|
||||||
#ifdef CGAL_USE_VTK
|
#ifdef CGAL_USE_VTK
|
||||||
#include <CGAL/IO/VTK/vtk_internals.h>
|
#include <CGAL/IO/VTK/vtk_internals.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -846,6 +848,70 @@ bool read_vtp(const char* filename, FaceGraph& face_graph)
|
||||||
return VTK_internal::vtkPointSet_to_polygon_mesh(data, face_graph);
|
return VTK_internal::vtkPointSet_to_polygon_mesh(data, face_graph);
|
||||||
}
|
}
|
||||||
#endif //CGAL_USE_VTK
|
#endif //CGAL_USE_VTK
|
||||||
|
|
||||||
|
template <class TriangleMesh>
|
||||||
|
std::ostream&
|
||||||
|
write_STL(const TriangleMesh& tm, std::ostream& out)
|
||||||
|
{
|
||||||
|
typedef typename boost::graph_traits<TriangleMesh>::face_descriptor face_descriptor;
|
||||||
|
typedef typename boost::graph_traits<TriangleMesh>::halfedge_descriptor halfedge_descriptor;
|
||||||
|
typedef typename boost::property_map<TriangleMesh, boost::vertex_point_t>::const_type Vpm;
|
||||||
|
typedef typename boost::property_traits<Vpm>::reference Point_3_ref;
|
||||||
|
typedef typename boost::property_traits<Vpm>::value_type Point_3;
|
||||||
|
typedef typename Kernel_traits<Point_3>::Kernel::Vector_3 Vector_3;
|
||||||
|
|
||||||
|
Vpm vpm = get(boost::vertex_point, tm);
|
||||||
|
|
||||||
|
if (get_mode(out) == IO::BINARY)
|
||||||
|
{
|
||||||
|
out << "FileType: Binary ";
|
||||||
|
const boost::uint32_t N32 = static_cast<boost::uint32_t>(faces(tm).size());
|
||||||
|
out.write(reinterpret_cast<const char *>(&N32), sizeof(N32));
|
||||||
|
|
||||||
|
for(face_descriptor f : faces(tm))
|
||||||
|
{
|
||||||
|
halfedge_descriptor h = halfedge(f, tm);
|
||||||
|
Point_3_ref p = get(vpm, target(h, tm));
|
||||||
|
Point_3_ref q = get(vpm, target(next(h, tm), tm));
|
||||||
|
Point_3_ref r = get(vpm, source(h, tm));
|
||||||
|
|
||||||
|
Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0):
|
||||||
|
unit_normal(p,q,r);
|
||||||
|
|
||||||
|
const float coords[12]={
|
||||||
|
static_cast<float>(n.x()), static_cast<float>(n.y()), static_cast<float>(n.z()),
|
||||||
|
static_cast<float>(p.x()), static_cast<float>(p.y()), static_cast<float>(p.z()),
|
||||||
|
static_cast<float>(q.x()), static_cast<float>(q.y()), static_cast<float>(q.z()),
|
||||||
|
static_cast<float>(r.x()), static_cast<float>(r.y()), static_cast<float>(r.z()) };
|
||||||
|
|
||||||
|
for (int i=0; i<12; ++i)
|
||||||
|
out.write(reinterpret_cast<const char *>(&coords[i]), sizeof(coords[i]));
|
||||||
|
out << " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
out << "solid\n";
|
||||||
|
for(face_descriptor f : faces(tm))
|
||||||
|
{
|
||||||
|
halfedge_descriptor h = halfedge(f, tm);
|
||||||
|
Point_3_ref p = get(vpm, target(h, tm));
|
||||||
|
Point_3_ref q = get(vpm, target(next(h, tm), tm));
|
||||||
|
Point_3_ref r = get(vpm, source(h, tm));
|
||||||
|
|
||||||
|
Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0):
|
||||||
|
unit_normal(p,q,r);
|
||||||
|
out << "facet normal " << n << "\nouter loop\n";
|
||||||
|
out << "vertex " << p << "\n";
|
||||||
|
out << "vertex " << q << "\n";
|
||||||
|
out << "vertex " << r << "\n";
|
||||||
|
out << "endloop\nendfacet\n";
|
||||||
|
}
|
||||||
|
out << "endsolid\n";
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace CGAL
|
} // namespace CGAL
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <CGAL/boost/graph/io.h>
|
#include <CGAL/boost/graph/io.h>
|
||||||
|
#include <CGAL/IO/STL/STL_writer.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ if (VTK_FOUND)
|
||||||
polyhedron_demo_plugin(vtk_plugin VTK_io_plugin KEYWORDS IO Mesh_3)
|
polyhedron_demo_plugin(vtk_plugin VTK_io_plugin KEYWORDS IO Mesh_3)
|
||||||
target_link_libraries(vtk_plugin PUBLIC scene_surface_mesh_item scene_polylines_item scene_c3t3_item scene_points_with_normal_item
|
target_link_libraries(vtk_plugin PUBLIC scene_surface_mesh_item scene_polylines_item scene_c3t3_item scene_points_with_normal_item
|
||||||
${VTK_LIBRARIES})
|
${VTK_LIBRARIES})
|
||||||
add_definitions(-DCGAL_USE_VTK)
|
target_compile_definitions(vtk_plugin PRIVATE -DCGAL_USE_VTK)
|
||||||
else()
|
else()
|
||||||
message(STATUS "NOTICE : the vtk IO plugin needs VTK libraries and will not be compiled.")
|
message(STATUS "NOTICE : the vtk IO plugin needs VTK libraries and will not be compiled.")
|
||||||
endif()
|
endif()
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@
|
||||||
#include <CGAL/Three/Three.h>
|
#include <CGAL/Three/Three.h>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include <CGAL/IO/Polyhedron_builder_from_STL.h>
|
#include <CGAL/IO/STL_reader.h>
|
||||||
#include <CGAL/IO/STL_writer.h>
|
#include <CGAL/boost/graph/io.h>
|
||||||
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
|
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,9 @@
|
||||||
#define CGAL_IO_STL_WRITER_H
|
#define CGAL_IO_STL_WRITER_H
|
||||||
|
|
||||||
#define CGAL_DEPRECATED_HEADER "<CGAL/IO/STL_writer.h>"
|
#define CGAL_DEPRECATED_HEADER "<CGAL/IO/STL_writer.h>"
|
||||||
#define CGAL_REPLACEMENT_HEADER "<CGAL/IO/STL/STL_writer.h>"
|
#define CGAL_REPLACEMENT_HEADER "<CGAL/boost/graph/io.h>"
|
||||||
#include <CGAL/internal/deprecation_warning.h>
|
#include <CGAL/internal/deprecation_warning.h>
|
||||||
#include <CGAL/IO/STL/STL_writer.h>
|
#include <CGAL/boost/graph/io.h>
|
||||||
#include <CGAL/enable_warnings.h>
|
#include <CGAL/enable_warnings.h>
|
||||||
|
|
||||||
#endif // CGAL_IO_STL_WRITER_H
|
#endif // CGAL_IO_STL_WRITER_H
|
||||||
|
|
|
||||||
|
|
@ -21,40 +21,35 @@
|
||||||
#define CGAL_IO_STL_STL_WRITER_H
|
#define CGAL_IO_STL_STL_WRITER_H
|
||||||
|
|
||||||
#include <CGAL/Kernel_traits.h>
|
#include <CGAL/Kernel_traits.h>
|
||||||
#include <CGAL/boost/graph/properties.h>
|
#include <CGAL/Kernel/global_functions.h>
|
||||||
|
|
||||||
#include <boost/cstdint.hpp>
|
#include <boost/cstdint.hpp>
|
||||||
#include <boost/graph/graph_traits.hpp>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace CGAL{
|
namespace CGAL{
|
||||||
|
|
||||||
template <class TriangleMesh>
|
template <class Point, class Triangle>
|
||||||
std::ostream&
|
std::ostream&
|
||||||
write_STL(const TriangleMesh& tm, std::ostream& out)
|
write_STL(const std::vector<Point>& points,
|
||||||
|
const std::vector<Triangle>& facets,
|
||||||
|
std::ostream& out)
|
||||||
{
|
{
|
||||||
typedef typename boost::graph_traits<TriangleMesh>::face_descriptor face_descriptor;
|
typedef typename CGAL::Kernel_traits<Point>::Kernel K;
|
||||||
typedef typename boost::graph_traits<TriangleMesh>::halfedge_descriptor halfedge_descriptor;
|
typedef typename K::Vector_3 Vector_3;
|
||||||
typedef typename boost::property_map<TriangleMesh, boost::vertex_point_t>::const_type Vpm;
|
|
||||||
typedef typename boost::property_traits<Vpm>::reference Point_3_ref;
|
|
||||||
typedef typename boost::property_traits<Vpm>::value_type Point_3;
|
|
||||||
typedef typename Kernel_traits<Point_3>::Kernel::Vector_3 Vector_3;
|
|
||||||
|
|
||||||
Vpm vpm = get(boost::vertex_point, tm);
|
|
||||||
|
|
||||||
if (get_mode(out) == IO::BINARY)
|
if (get_mode(out) == IO::BINARY)
|
||||||
{
|
{
|
||||||
out << "FileType: Binary ";
|
out << "FileType: Binary ";
|
||||||
const boost::uint32_t N32 = static_cast<boost::uint32_t>(faces(tm).size());
|
const boost::uint32_t N32 = static_cast<boost::uint32_t>(facets.size());
|
||||||
out.write(reinterpret_cast<const char *>(&N32), sizeof(N32));
|
out.write(reinterpret_cast<const char *>(&N32), sizeof(N32));
|
||||||
|
|
||||||
for(face_descriptor f : faces(tm))
|
for(auto face : facets)
|
||||||
{
|
{
|
||||||
halfedge_descriptor h = halfedge(f, tm);
|
const Point& p = points[face[0]];
|
||||||
Point_3_ref p = get(vpm, target(h, tm));
|
const Point& q = points[face[1]];
|
||||||
Point_3_ref q = get(vpm, target(next(h, tm), tm));
|
const Point& r = points[face[2]];
|
||||||
Point_3_ref r = get(vpm, source(h, tm));
|
|
||||||
|
|
||||||
Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0):
|
Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0):
|
||||||
unit_normal(p,q,r);
|
unit_normal(p,q,r);
|
||||||
|
|
@ -73,12 +68,11 @@ write_STL(const TriangleMesh& tm, std::ostream& out)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
out << "solid\n";
|
out << "solid\n";
|
||||||
for(face_descriptor f : faces(tm))
|
for(auto face : facets)
|
||||||
{
|
{
|
||||||
halfedge_descriptor h = halfedge(f, tm);
|
const Point& p = points[face[0]];
|
||||||
Point_3_ref p = get(vpm, target(h, tm));
|
const Point& q = points[face[1]];
|
||||||
Point_3_ref q = get(vpm, target(next(h, tm), tm));
|
const Point& r = points[face[2]];
|
||||||
Point_3_ref r = get(vpm, source(h, tm));
|
|
||||||
|
|
||||||
Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0):
|
Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0):
|
||||||
unit_normal(p,q,r);
|
unit_normal(p,q,r);
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@
|
||||||
#include <CGAL/array.h>
|
#include <CGAL/array.h>
|
||||||
#include <CGAL/assertions.h>
|
#include <CGAL/assertions.h>
|
||||||
#include <CGAL/Has_member.h>
|
#include <CGAL/Has_member.h>
|
||||||
|
#include <CGAL/Point_3.h>
|
||||||
|
|
||||||
#include <boost/mpl/logical.hpp>
|
#include <boost/mpl/logical.hpp>
|
||||||
#include <boost/utility/enable_if.hpp>
|
#include <boost/utility/enable_if.hpp>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
#include <CGAL/IO/STL/STL_reader.h>
|
||||||
|
#include <CGAL/IO/STL/STL_writer.h>
|
||||||
|
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
int main ()
|
||||||
|
{
|
||||||
|
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
|
||||||
|
typedef K::Point_3 Point;
|
||||||
|
typedef std::vector<std::size_t> Polygon;
|
||||||
|
//make a tetrahedorn soup.
|
||||||
|
std::vector<Point> ps(4);
|
||||||
|
ps[0] = Point(0,0,0); ps[1] = Point(1,0,0);
|
||||||
|
ps[2] = Point(0,1,0); ps[3] = Point(0,0,1);
|
||||||
|
std::vector<Polygon> faces(4);
|
||||||
|
|
||||||
|
faces[0].push_back(0);
|
||||||
|
faces[0].push_back(2);
|
||||||
|
faces[0].push_back(1);
|
||||||
|
|
||||||
|
faces[1].push_back(0);
|
||||||
|
faces[1].push_back(3);
|
||||||
|
faces[1].push_back(2);
|
||||||
|
|
||||||
|
faces[2].push_back(1);
|
||||||
|
faces[2].push_back(2);
|
||||||
|
faces[2].push_back(3);
|
||||||
|
|
||||||
|
faces[3].push_back(0);
|
||||||
|
faces[3].push_back(1);
|
||||||
|
faces[3].push_back(3);
|
||||||
|
|
||||||
|
std::ofstream os("tetra.stl");
|
||||||
|
CGAL::write_STL(ps, faces, os);
|
||||||
|
if(!os)
|
||||||
|
{
|
||||||
|
std::cerr<<"error during STL writing."<<std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
os.close();
|
||||||
|
ps.clear();
|
||||||
|
faces.clear();
|
||||||
|
std::ifstream is("tetra.stl");
|
||||||
|
if(!CGAL::read_STL(is, ps, faces))
|
||||||
|
{
|
||||||
|
std::cerr<<"error during STL reading."<<std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(ps.size() != 4 || faces.size() != 4)
|
||||||
|
{
|
||||||
|
std::cerr<<"error during STL file interpretation."<<std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue