RINGMesh  Version 5.0.0
A programming library for geological model meshes
io.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 #include <ringmesh/io/io.h>
37 
38 #include <cstring>
39 
40 #include <geogram/basic/file_system.h>
41 
44 
51 namespace RINGMesh
52 {
59  bool compare_files( const std::string& f1, const std::string& f2 )
60  {
61  const auto MAX_LINE_LEN =
62  static_cast< unsigned int >( std::pow( 2, 16 ) - 1 );
63 
64  std::ifstream lFile( f1.c_str() );
65  std::ifstream rFile( f2.c_str() );
66 
67  std::unique_ptr< char[] > lBuffer( new char[MAX_LINE_LEN]() );
68  std::unique_ptr< char[] > rBuffer( new char[MAX_LINE_LEN]() );
69 
70  do
71  {
72  lFile.read( lBuffer.get(), MAX_LINE_LEN );
73  rFile.read( rBuffer.get(), MAX_LINE_LEN );
74  size_t numberOfRead = static_cast< size_t >( lFile.gcount() );
75 
76  if( std::memcmp( lBuffer.get(), rBuffer.get(), numberOfRead ) != 0 )
77  {
78  return false;
79  }
80  } while( lFile.good() || rFile.good() );
81  return true;
82  }
83 
85  {
86  GeoModelIOHandler2D::initialize();
87  GeoModelIOHandler3D::initialize();
89  }
90 
91  /***************************************************************************/
92 
93  template < index_t DIMENSION >
95  const std::string& filename, GeoModel< DIMENSION >& geomodel )
96  {
97  load( filename, geomodel );
99  "I/O", " Loaded geomodel ", geomodel.name(), " from ", filename );
100  return is_geomodel_valid( geomodel );
101  }
102 
103  template < index_t DIMENSION >
105  const GeoModel< DIMENSION >& geomodel, const std::string& filename )
106  {
107  save( geomodel, filename );
108  }
109 
110  index_t find_geomodel_dimension( const std::string& filename )
111  {
112  std::string ext = GEO::FileSystem::extension( filename );
113  if( GeoModelIOHandlerFactory2D::has_creator( ext ) )
114  {
115  return GeoModelIOHandler2D::get_handler( filename )
116  ->dimension( filename );
117  }
118  else if( GeoModelIOHandlerFactory3D::has_creator( ext ) )
119  {
120  return GeoModelIOHandler3D::get_handler( filename )
121  ->dimension( filename );
122  }
123  else
124  {
126  }
127  return 0;
128  }
129 
130  template < index_t DIMENSION >
132  GeoModel< DIMENSION >& geomodel, const std::string& filename )
133  {
134  if( !GEO::FileSystem::is_file( filename ) )
135  {
136  throw RINGMeshException( "I/O", "File does not exist: ", filename );
137  }
138  Logger::out( "I/O", "Loading file ", filename, "..." );
139 
140  std::unique_ptr< GeoModelIOHandler< DIMENSION > > handler(
142  return handler->load_geomodel( filename, geomodel );
143  }
144 
145  template < index_t DIMENSION >
147  const GeoModel< DIMENSION >& geomodel, const std::string& filename )
148  {
149  Logger::out( "I/O", "Saving file ", filename, "..." );
150 
151  std::unique_ptr< GeoModelIOHandler< DIMENSION > > handler(
153  handler->save_geomodel( geomodel, filename );
154  }
155 
156  /************************************************************************/
157 
158  template < index_t DIMENSION >
159  std::unique_ptr< GeoModelIOHandler< DIMENSION > >
160  GeoModelIOHandler< DIMENSION >::create( const std::string& format )
161  {
162  auto handler = GeoModelIOHandlerFactory< DIMENSION >::create( format );
163  if( !handler )
164  {
165  Logger::err( "I/O", "Currently supported file formats are: " );
166  for( const std::string& name :
168  {
169  Logger::err( "I/O", " ", name );
170  }
171 
172  throw RINGMeshException(
173  "I/O", "Unsupported file format: ", format );
174  }
175  return handler;
176  }
177 
178  template < index_t DIMENSION >
179  std::unique_ptr< GeoModelIOHandler< DIMENSION > >
181  const std::string& filename )
182  {
183  return create( GEO::FileSystem::extension( filename ) );
184  }
185 
186  template class RINGMESH_API GeoModelIOHandler< 2 >;
187  template class RINGMESH_API GeoModelIOHandler< 3 >;
188 
189  template bool RINGMESH_API geomodel_load( GeoModel2D&, const std::string& );
190  template void RINGMESH_API geomodel_save(
191  const GeoModel2D&, const std::string& );
192 
193  template bool RINGMESH_API geomodel_load( GeoModel3D&, const std::string& );
194  template void RINGMESH_API geomodel_save(
195  const GeoModel3D&, const std::string& );
196 
197 } // namespace RINGMesh
void RINGMESH_API mesh_initialize()
Definition: io.cpp:84
static std::unique_ptr< BaseClass > create(const Key &key, const Args &... args)
Definition: factory.h:85
void save_geomodel(const GeoModel< DIMENSION > &geomodel, const std::string &filename)
Definition: io.cpp:104
bool is_geomodel_valid(const GeoModel< DIMENSION > &geomodel, ValidityCheckMode validity_check_mode=ValidityCheckMode::ALL)
Check global geomodel validity.
static std::unique_ptr< GeoModelIOHandler< DIMENSION > > get_handler(const std::string &filename)
Definition: io.cpp:180
const std::string & name() const
Gets the name of the GeoModel.
Definition: geomodel.h:111
bool geomodel_load(GeoModel< DIMENSION > &geomodel, const std::string &filename)
Definition: io.cpp:131
static void err(const std::string &feature, const Args &... args)
Definition: logger.h:68
static void out(const std::string &feature, const Args &... args)
Definition: logger.h:61
void geomodel_save(const GeoModel< DIMENSION > &geomodel, const std::string &filename)
Definition: io.cpp:146
index_t RINGMESH_API find_geomodel_dimension(const std::string &filename)
Definition: io.cpp:110
static std::unique_ptr< GeoModelIOHandler > create(const std::string &format)
Definition: io.cpp:160
bool RINGMESH_API compare_files(const std::string &f1, const std::string &f2)
Definition: io.cpp:59
Classes to build GeoModel from various inputs.
Definition: algorithm.h:48
bool load_geomodel(const std::string &filename, GeoModel< DIMENSION > &geomodel)
Definition: io.cpp:94
#define ringmesh_assert_not_reached