RINGMesh  Version 5.0.0
A programming library for geological model meshes
io_stl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2017, Association Scientifique pour la Geologie et ses
3  * Applications (ASGA). All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ASGA nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASGA BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * http://www.ring-team.org
28  *
29  * RING Project
30  * Ecole Nationale Superieure de Geologie - GeoRessources
31  * 2 Rue du Doyen Marcel Roubault - TSA 70605
32  * 54518 VANDOEUVRE-LES-NANCY
33  * FRANCE
34  */
35 
36 namespace {
37 
38  void save_header( const GeoModel3D& geomodel, std::ostream& out )
39  {
40  out << "solid " << geomodel.name() << EOL;
41  }
42 
43  void save_footer( const GeoModel3D& geomodel, std::ostream& out )
44  {
45  out << "endsolid" << geomodel.name() << EOL;
46  }
47 
48  void save_normal(
49  const GeoModel3D& geomodel,
50  index_t triangle_id,
51  std::ostream& out )
52  {
53  out << "facet normal " << geomodel.mesh.polygons.normal( triangle_id )
54  << EOL;
55  }
56 
57  void begin_triangle( std::ostream& out )
58  {
59  out << "outer loop" << EOL;
60  }
61 
62  void end_triangle( std::ostream& out )
63  {
64  out << "endloop" << EOL;
65  }
66 
67  void save_triangle_vertex(
68  const GeoModel3D& geomodel,
69  index_t triangle_id,
70  index_t local_vertex_id,
71  std::ostream& out )
72  {
73  out << "vertex "
74  << geomodel.mesh.vertices.vertex(
75  geomodel.mesh.polygons.vertex(
76  ElementLocalVertex( triangle_id, local_vertex_id ) ) )
77  << EOL;
78  }
79 
80  void save_triangle(
81  const GeoModel3D& geomodel,
82  index_t triangle_id,
83  std::ostream& out )
84  {
85  save_normal( geomodel, triangle_id, out );
86  begin_triangle( out );
87  for( auto vertex : range( 3 ) ) {
88  save_triangle_vertex( geomodel, triangle_id, vertex, out );
89  }
90  end_triangle( out );
91  }
92 
93  void save_triangles( const GeoModel3D& geomodel, std::ostream& out )
94  {
95  for( auto triangle : range( geomodel.mesh.polygons.nb_triangle() ) ) {
96  save_triangle( geomodel, triangle, out );
97  }
98  }
99 
100  void check_stl_validity( const GeoModel3D& geomodel )
101  {
102  if( geomodel.mesh.polygons.nb() != geomodel.mesh.polygons.nb_triangle() ) {
103  throw RINGMeshException( "I/O",
104  "Geological model save in STL format support only triangles" );
105  }
106  }
107 
119  class STLIOHandler final: public GeoModelIOHandler< 3 > {
120  public:
121  void load( const std::string& filename, GeoModel3D& geomodel ) final
122  {
123  ringmesh_unused( filename );
124  ringmesh_unused( geomodel );
125  throw RINGMeshException( "I/O",
126  "Geological model loading of a from STL mesh not yet implemented" );
127  }
128 
129  void save(
130  const GeoModel3D& geomodel,
131  const std::string& filename ) final
132  {
133  check_stl_validity( geomodel );
134  std::ofstream out( filename.c_str() );
135  out.precision( 17 );
136  save_header( geomodel, out );
137  save_triangles( geomodel, out );
138  save_footer( geomodel, out );
139  out << std::flush;
140  }
141  };
142 }
void ringmesh_unused(const T &)
Definition: common.h:105
const char EOL
Definition: io.h:45