// Copyright (c) 2017 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) : Sebastien Loriot #ifndef CGAL_IO_STL_STL_WRITER_H #define CGAL_IO_STL_STL_WRITER_H #include #include #include namespace CGAL{ template std::ostream& write_STL(const std::vector& points, const std::vector& facets, std::ostream& out) { typedef typename CGAL::Kernel_traits::Kernel K; typedef typename K::Vector_3 Vector_3; if (get_mode(out) == IO::BINARY) { out << "FileType: Binary "; const boost::uint32_t N32 = static_cast(facets.size()); out.write(reinterpret_cast(&N32), sizeof(N32)); for(auto face : facets) { const Point& p = points[face[0]]; const Point& q = points[face[1]]; const Point& r = points[face[2]]; Vector_3 n = collinear(p,q,r) ? Vector_3(1,0,0): unit_normal(p,q,r); const float coords[12]={ static_cast(n.x()), static_cast(n.y()), static_cast(n.z()), static_cast(p.x()), static_cast(p.y()), static_cast(p.z()), static_cast(q.x()), static_cast(q.y()), static_cast(q.z()), static_cast(r.x()), static_cast(r.y()), static_cast(r.z()) }; for (int i=0; i<12; ++i) out.write(reinterpret_cast(&coords[i]), sizeof(coords[i])); out << " "; } } else { out << "solid\n"; for(auto face : facets) { const Point& p = points[face[0]]; const Point& q = points[face[1]]; const Point& r = points[face[2]]; 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; } } // end of namespace CGAL #endif // CGAL_IO_STL_STL_WRITER_H