RINGMesh  Version 5.0.0
A programming library for geological model meshes
io_tetgen.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  class TetGenIOHandler final: public GeoModelIOHandler< 3 > {
38  public:
39  void load( const std::string& filename, GeoModel3D& geomodel ) final
40  {
41  ringmesh_unused( filename );
42  ringmesh_unused( geomodel );
43  throw RINGMeshException( "I/O",
44  "Loading of a GeoModel from TetGen not implemented yet" );
45  }
46  void save( const GeoModel3D& geomodel, const std::string& filename ) final
47  {
48  std::string directory = GEO::FileSystem::dir_name( filename );
49  std::string file = GEO::FileSystem::base_name( filename );
50 
51  std::ostringstream oss_node;
52  oss_node << directory << "/" << file << ".node";
53  std::ofstream node( oss_node.str().c_str() );
54  node.precision( 16 );
55 
56  const GeoModelMesh3D& mesh = geomodel.mesh;
57  node << mesh.vertices.nb() << " 3 0 0" << EOL;
58  for( auto v : range( mesh.vertices.nb() ) ) {
59  node << v << SPACE << mesh.vertices.vertex( v ) << EOL;
60  }
61 
62  std::ostringstream oss_ele;
63  oss_ele << directory << "/" << file << ".ele";
64  std::ofstream ele( oss_ele.str().c_str() );
65  std::ostringstream oss_neigh;
66  oss_neigh << directory << "/" << file << ".neigh";
67  std::ofstream neigh( oss_neigh.str().c_str() );
68 
69  ele << mesh.cells.nb() << " 4 1" << EOL;
70  neigh << mesh.cells.nb() << " 4" << EOL;
71  index_t nb_tet_exported = 0;
72  for( auto m : range( geomodel.nb_regions() ) ) {
73  for( auto tet : range( mesh.cells.nb_tet( m ) ) ) {
74  index_t cell = mesh.cells.tet( m, tet );
75  ele << nb_tet_exported << SPACE
76  << mesh.cells.vertex( ElementLocalVertex( cell, 0 ) )
77  << SPACE
78  << mesh.cells.vertex( ElementLocalVertex( cell, 1 ) )
79  << SPACE
80  << mesh.cells.vertex( ElementLocalVertex( cell, 2 ) )
81  << SPACE
82  << mesh.cells.vertex( ElementLocalVertex( cell, 3 ) )
83  << SPACE << m + 1 << EOL;
84  neigh << nb_tet_exported;
85  for( auto f : range( mesh.cells.nb_facets( tet ) ) ) {
86  neigh << SPACE;
87  index_t adj = mesh.cells.adjacent( cell, f );
88  if( adj == GEO::NO_CELL ) {
89  neigh << -1;
90  } else {
91  neigh << adj;
92  }
93  }
94  neigh << EOL;
95  nb_tet_exported++;
96  }
97  }
98  ele << std::flush;
99  neigh << std::flush;
100  node << std::flush;
101  }
102  };
103 
104 }
const char SPACE
Definition: io.h:46
void ringmesh_unused(const T &)
Definition: common.h:105
const char EOL
Definition: io.h:45