RINGMesh  Version 5.0.0
A programming library for geological model meshes
io_smesh.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  void merge_colocated_vertices( double epsilon, LineMesh3D& mesh )
38  {
39  std::vector< index_t > old2new;
40  index_t nb_colocated = NO_ID;
41  std::tie( nb_colocated, old2new ) =
42  mesh.vertex_nn_search().get_colocated_index_mapping( epsilon );
43  if( nb_colocated > 0 ) {
44  std::unique_ptr< LineMeshBuilder3D > builder =
45  LineMeshBuilder3D::create_builder( mesh );
46  for( auto e : range( mesh.nb_edges() ) ) {
47  for( auto i : range( 2 ) ) {
48  index_t v = mesh.edge_vertex( ElementLocalVertex( e, i ) );
49  builder->set_edge_vertex( EdgeLocalVertex( e, i ), old2new[v] );
50  }
51  }
52  std::vector< bool > delete_vertices( mesh.nb_vertices(), false );
53  for( auto v : range( mesh.nb_vertices() ) ) {
54  if( old2new[v] != v ) {
55  delete_vertices[v] = true;
56  }
57  }
58  builder->delete_vertices( delete_vertices );
59  }
60  }
61 
62  class SmeshIOHandler final: public WellGroupIOHandler {
63  public:
64  void load( const std::string& filename, WellGroup3D& wells ) final
65  {
66  GEO::LineInput in( filename );
67  if( !in.OK() ) {
68  throw RINGMeshException( "I/O", "Could not open file" );
69  }
70 
71  std::unique_ptr< LineMesh3D > mesh = LineMesh3D::create_mesh(
72  GeogramLineMesh3D::type_name_static() );
73  std::unique_ptr< LineMeshBuilder3D > builder =
74  LineMeshBuilder3D::create_builder( *mesh );
75  std::string name = GEO::FileSystem::base_name( filename );
76 
77  bool is_first_part = true;
78 
79  while( !in.eof() ) {
80  in.get_line();
81  in.get_fields();
82  if( in.nb_fields() == 0 ) continue;
83  if( GEO::String::string_starts_with( in.field( 0 ), "#" ) ) {
84  continue;
85  }
86  if( is_first_part ) {
87  index_t nb_vertices = in.field_as_uint( 0 );
88  builder->create_vertices( nb_vertices );
89  Box3D box;
90 
91  for( auto v : range( nb_vertices ) ) {
92  do {
93  in.get_line();
94  in.get_fields();
95  } while( in.nb_fields() == 0 );
96  vec3 point;
97  point[0] = in.field_as_double( 1 );
98  point[1] = in.field_as_double( 2 );
99  point[2] = in.field_as_double( 3 );
100  builder->set_vertex( v, point );
101  box.add_point( point );
102  }
103  is_first_part = false;
104  } else {
105  index_t nb_edges = in.field_as_uint( 0 );
106  builder->create_edges( nb_edges );
107  for( auto e : range( nb_edges ) ) {
108  do {
109  in.get_line();
110  in.get_fields();
111  } while( in.nb_fields() == 0 );
112  builder->set_edge_vertex( EdgeLocalVertex( e, 0 ), in.field_as_uint( 1 ) );
113  builder->set_edge_vertex( EdgeLocalVertex( e, 1 ), in.field_as_uint( 2 ) );
114  }
115  merge_colocated_vertices( wells.geomodel()->epsilon(), *mesh );
116  wells.add_well( *mesh, name );
117  break;
118  }
119  }
120  }
121  void save( const WellGroup3D& wells, const std::string& filename ) final
122  {
123  ringmesh_unused( wells );
124  ringmesh_unused( filename );
125  throw RINGMeshException( "I/O",
126  "Saving of a WellGroup from Smesh not implemented yet" );
127  }
128  };
129 }
vecn< 3 > vec3
Definition: types.h:76
void ringmesh_unused(const T &)
Definition: common.h:105