G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsReadFile.h
Go to the documentation of this file.
1
14/*
15 Notes:
16 QEPCAD:
17 http://opus.bath.ac.uk/29503/
18 http://opus.bath.ac.uk/29503/9/QEPCADexamplebank.txt
19
20*/
21
22#pragma once
23
24#include <string>
25
26#include <gsCore/gsDebug.h>
27
28namespace gismo
29{
30
31template< class T> class gsMultiPatch;
32
41template<class T = real_t >
43{
44public:
45
65 gsReadFile(std::string const & fn)
66 : m_id(-1)
67 {
68 m_data.read(fn);
69 }
70
75 gsReadFile(std::string const & fn, index_t id)
76 : m_id(id)
77 {
78 m_data.read(fn);
79 }
80
94 template<class Obj>
95 gsReadFile(std::string const & fn, Obj & result)
96 : m_id(-1)
97 {
98 GISMO_ENSURE(m_data.read(fn), "Error reading file.");
99 m_data.getAnyFirst(result);
100 }
101
115 gsReadFile(std::string const & fn, gsMultiPatch<T> & result)
116 : m_id(-1)
117 {
118 GISMO_ENSURE(m_data.read(fn), "Error reading file.");
119 result.clear();
120 memory::unique_ptr< gsMultiPatch<T> > mp =
121 this->operator memory::unique_ptr< gsMultiPatch<T> >();
122 if(mp)
123 result = give(*mp);
124 }
125
126 ~gsReadFile() { m_data.clear(); }
127
128private:
129
132
133 index_t m_id;
134
135public:
136
138 template<class Obj>
139 operator memory::unique_ptr<Obj> ()
140 {
141 // Get the first object in the file
142 if ( this->m_data.template hasAny< Obj >() )
143 return this->m_data.template getAnyFirst< Obj >();
144
145 gsWarn<< "Failed to read object from file (not found).\n";
146 return memory::unique_ptr<Obj>();
147 }
148
150 operator memory::unique_ptr< gsGeometry<T> > ()
151 {
152 // Get the first geometry in the file
153 if ( this->m_data.template hasAny< gsGeometry<T> >() )
154 return this->m_data.template getAnyFirst< gsGeometry<T> >();
155
156 gsWarn<< "Failed to read gsGeometry from file (not found).\n";
157 return memory::unique_ptr< gsGeometry<T> >();
158 }
159
161 operator memory::unique_ptr< gsCurve<T> > ()
162 {
163 // Get the first curve in the file
164 if ( this->m_data.template hasAny< gsCurve<T> >() )
165 return this->m_data.template getAnyFirst< gsCurve<T> >();
166
167 gsWarn<< "Failed to read gsCurve from file (not found).\n";
168 return memory::unique_ptr< gsCurve<T> >();
169 }
170
172 operator memory::unique_ptr< gsBasis<T> > ()
173 {
174 // Get the first basis in the file
175 if ( this->m_data.template hasAny< gsBasis<T> >() )
176 return this->m_data.template getAnyFirst< gsBasis<T> >();
177
178 gsWarn<< "Failed to read gsBasis from file (not found).\n";
179 return memory::unique_ptr< gsBasis<T> >();
180 }
181
183 operator memory::unique_ptr< gsFunctionExpr<T> > () const
184 {
185
186 if ( this->m_data.template hasAny< gsFunctionExpr<T> >() )
187 {
188 if ( -1==m_id ) // Get the first one in the file
189 return this->m_data.template getAnyFirst< gsFunctionExpr<T> >();
190 else
191 return this->m_data.template getId<gsFunctionExpr<T> >(m_id);
192 }
193
194 gsWarn<< "Failed to read gsFunctionExpr from file (not found).\n";
195 return memory::unique_ptr< gsFunctionExpr<T> >();
196 }
197
199 operator memory::unique_ptr< gsPlanarDomain<T> > ()
200 {
201 // Get the first basis in the file
202 if ( this->m_data.template hasAny< gsPlanarDomain<T> >() )
203 return this->m_data.template getAnyFirst< gsPlanarDomain<T> >();
204
205 gsWarn<< "Failed to read gsPlanarDomain from file (not found).\n";
206 return memory::unique_ptr< gsPlanarDomain<T> >();
207 }
208
210 operator memory::unique_ptr< gsMultiPatch<T> > ()
211 {
212 // Get the first MultiPatch tag, if one exists
213 if ( this->m_data.template has< gsMultiPatch<T> >() )
214 return this->m_data.template getFirst< gsMultiPatch<T> >();
215
216 // Else get all geometries and make a multipatch out of that
217 if ( this->m_data.template has< gsGeometry<T> >() )
218 {
219 std::vector< memory::unique_ptr<gsGeometry<T> > > patches =
220 this->m_data.template getAll< gsGeometry<T> >();
221 std::vector< gsGeometry<T>* > releasedPatches = memory::release(patches);
222 return memory::make_unique(new gsMultiPatch<T>( releasedPatches ));
223 }
224
225 gsWarn<< "Failed to read gsMultiPatch from file (not found).\n";
226 return memory::unique_ptr< gsMultiPatch<T> >();
227 }
228
230 operator memory::unique_ptr< gsMesh<T> > ()
231 {
232 // Get the first Mesh, if one exists
233 if ( this->m_data.template has< gsMesh<T> >() )
234 return this->m_data.template getFirst< gsMesh<T> >();
235
236 gsWarn<< "Failed to read gsMesh from file (not found).\n";
237 return memory::unique_ptr< gsMesh<T> >();
238 }
239
241 operator std::vector< memory::unique_ptr< gsBasis<T> > > ()
242 {
243 // Get all bases
244 return this->m_data.template getAll< gsBasis<T> >();
245 }
246
248 operator memory::unique_ptr< gsPde<T> > ()
249 {
250 if ( this->m_data.template has< gsPde<T> >() )
251 return this->m_data.template getFirst< gsPde<T> >();
252
253 gsWarn<< "Failed to read gsPde from file (not found).\n";
254 return memory::unique_ptr< gsPde<T> >();
255 }
256
258 operator memory::unique_ptr< gsPoissonPde<T> > ()
259 {
260 if ( this->m_data.template has< gsPoissonPde<T> >() )
261 return this->m_data.template getFirst< gsPoissonPde<T> >();
262
263 gsWarn<< "Failed to read gsPoissonPde from file (not found).\n";
264 return memory::unique_ptr< gsPoissonPde<T> >();
265 }
266
268 template<class Obj>
269 operator memory::shared_ptr<Obj> ()
270 {
271 return (memory::unique_ptr<Obj>)(*this);
272 }
273
274}; // class gsReadFile
275
276#ifdef GISMO_WITH_PYBIND11
277
281 void pybind11_init_gsReadFile(pybind11::module &m);
282
283#endif // GISMO_WITH_PYBIND11
284
285
287template <typename Object>
288void gsWrite(const Object& obj, const std::string& fname)
289{
291 fd << obj;
292 fd.dump(fname);
293}
294
295} // namespace gismo
A basis represents a family of scalar basis functions defined over a common parameter domain.
Definition gsBasis.h:79
Abstract base class representing a curve.
Definition gsCurve.h:31
This class represents an XML data tree which can be read from or written to a (file) stream.
Definition gsFileData.h:34
Class defining a multivariate (real or vector) function given by a string mathematical expression.
Definition gsFunctionExpr.h:52
Abstract base class representing a geometry map.
Definition gsGeometry.h:93
Class Representing a triangle mesh with 3D vertices.
Definition gsMesh.h:32
Container class for a set of geometry patches and their topology, that is, the interface connections ...
Definition gsMultiPatch.h:100
void clear()
Clear (delete) all patches.
Definition gsMultiPatch.h:388
Abstract class representing a PDE (partial differential equation).
Definition gsPde.h:44
Class representing a Planar domain with an outer boundary and a number of holes.
Definition gsPlanarDomain.h:44
A Poisson PDE.
Definition gsPoissonPde.h:35
Reads an object from a data file, if such the requested object exists in the file.
Definition gsReadFile.h:43
gsReadFile(std::string const &fn, gsMultiPatch< T > &result)
Opens a file and reads a gsMultiPatch object into result.
Definition gsReadFile.h:115
gsReadFile(std::string const &fn, index_t id)
Opens a file and reads an object into a smartpointer (uPtr).
Definition gsReadFile.h:75
gsReadFile(std::string const &fn, Obj &result)
Opens a file and reads an object into result.
Definition gsReadFile.h:95
gsFileData< T > m_data
File data as a Gismo xml tree.
Definition gsReadFile.h:131
gsReadFile(std::string const &fn)
Opens a file and reads an object into a smartpointer (uPtr).
Definition gsReadFile.h:65
#define index_t
Definition gsConfig.h:32
This file contains the debugging and messaging system of G+Smo.
#define gsWarn
Definition gsDebug.h:50
#define GISMO_ENSURE(cond, message)
Definition gsDebug.h:102
std::vector< T * > release(std::vector< unique_ptr< T > > &cont)
Takes a vector of smart pointers, releases them and returns the corresponding raw pointers.
Definition gsMemory.h:228
unique_ptr< T > make_unique(T *x)
Definition gsMemory.h:198
The G+Smo namespace, containing all definitions for the library.
void gsWrite(const Object &obj, const std::string &fname)
Write an arbitrary Gismo object to an XML file with the given filename.
Definition gsReadFile.h:288
S give(S &x)
Definition gsMemory.h:266