G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsFileData.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <iostream>
17 #include <string>
18 #include <list>
19 
20 #include <gsIO/gsXml.h>
21 #include <gsIO/gsFileManager.h>
22 
23 namespace gismo
24 {
25 
32 template<class T>
34 {
35 public:
36  typedef internal::gsXmlTree FileData;
37  typedef internal::gsXmlNode gsXmlNode;
38  typedef internal::gsXmlAttribute gsXmlAttribute;
39  typedef std::string String;
40 
41  typedef gsVector3d<T> Point_3;
42 
43 public:
44 
45  gsFileData();
46 
53  explicit gsFileData(String const & fn, bool recursive=false);
54 
63  bool read(String const & fn, bool recursive=false) ;
64 
65  ~gsFileData();
66 
68  void clear();
69 
71  int numData() const { return data->numNodes();}
72 
74  void save(String const & fname = "dump", bool compress = false) const;
75 
77  void writeIges(String const & fname);
78 
80  void saveCompressed(String const & fname = "dump") const;
81 
83  void dump(String const & fname = "dump") const;
84 
85  void addComment(String const & message);
86 
87  // Returns the path of the last file where data was read from
88  //
89  // If the corresponding file did not exist, the return value is an
90  // empty string.
91  String lastPath() const { return m_lastPath; }
92 
96  void setFloatPrecision(const unsigned k) { data->setFloatPrecision(k); }
97 
101  unsigned getFloatPrecision() const { return data->getFloatPrecision(); }
102 
103 private:
105  FileData * data;
106 
107  // Used to hold parsed data of native gismo XML files
108  std::list<std::vector<char> > m_buffer;
109 
110  // Holds the last path that was used in an I/O operation
111  mutable String m_lastPath;
112 
113 protected:
114 
115 /*
116  * File readers
117  */
118 
120  bool readXmlFile( String const & fn, bool recursive=false);
121 
123  bool readXmlGzFile( String const & fn, bool recursive=false);
124 
126  bool readGismoXmlStream(std::istream & is, bool recursive=false);
127 
129  bool readAxelFile(String const & fn);
130  bool readAxelSurface( gsXmlNode * node );
131  bool readAxelCurve ( gsXmlNode * node );
132  bool readAxelMesh ( gsXmlNode * node );
133 
135  bool readGeompFile( String const & fn );
136 
138  bool readGoToolsFile(String const & fn);
139 
141  bool readOffFile(String const & fn);
142 
144  bool readStlFile(String const & fn);
145 
147  bool readObjFile(String const & fn);
148 
150  bool readBrepFile(String const & fn);
151 
153  bool readIgesFile(String const & fn);
154 
156  bool readX3dFile(String const & fn);
157 
159  bool read3dmFile(String const & fn);
160 
162  bool readParasolidFile(String const & fn);
163 
164  bool readCsvFile(String const & fn);
165 
166  // Show the line number where something went wrong
167  void ioError(int lineNumber,const String& str);
168 
169 public:
170 
171  // Generic functions to fetch Gismo object
172  // template<class Object>
173  // inline Object * get( gsXmlNode * node)
174  // {
175  // return internal::gsXml<Object>::get(node);// Using gsXmlUtils
176  // }
177 
179  template<class Object>
180  inline memory::unique_ptr<Object> getId( const int & id) const
181  {
182  return memory::make_unique( internal::gsXml<Object>::getId( getXmlRoot(), id ) );
183  }
184 
186  template<class Object>
187  inline void getId( const int & id, Object& result) const
188  {
189  memory::unique_ptr<Object> obj = getId<Object>(id);
190  result = give(*obj);
191  }
192 
194  template<class Object>
195  inline memory::unique_ptr<Object> getLabel(const std::string & name) const
196  {
197  return memory::make_unique( internal::gsXml<Object>::getLabel( getXmlRoot(), name ) );
198  }
199 
201  template<class Object>
202  inline void getLabel(const std::string & name, Object& result) const
203  {
204  memory::unique_ptr<Object> obj = getLabel<Object>(name);
205  result = give(*obj);
206  }
207 
209  template<class Object>
210  inline String tag() const
211  { return internal::gsXml<Object>::tag(); }
212 
214  template<class Object>
215  inline String type() const
216  { return internal::gsXml<Object>::type(); }
217 
219  template<class Object>
220  inline bool has() const
221  {
222  return getFirstNode( internal::gsXml<Object>::tag(),
223  internal::gsXml<Object>::type() ) != 0 ;
224  }
225 
227  inline bool hasId(int id) const {
228  gsXmlNode* root = getXmlRoot();
229  // const gsXmlAttribute * id_at;
230  gsXmlNode* nd = internal::searchId(id, root, NULL, false);
231  return (bool)nd;
232  }
233 
235  inline bool hasTag(std::string tag) const
236  {
237  return getAnyFirstNode(tag.c_str());
238  }
239 
242  template<class Object>
243  inline bool hasAny() const
244  {
245  return getAnyFirstNode( internal::gsXml<Object>::tag(),
246  internal::gsXml<Object>::type() ) != 0 ;
247  }
248 
250  template<class Object>
251  inline int count() const
252  {
253  int i(0);
254  for (gsXmlNode * child = getFirstNode( internal::gsXml<Object>::tag(),
255  internal::gsXml<Object>::type() ) ;
256  child; child = getNextSibling(child, internal::gsXml<Object>::tag(),
257  internal::gsXml<Object>::type() ))
258  ++i;
259  return i;
260  }
261 
262 
264  template<class Object>
265  void operator<<(const Object & obj)
266  {
267  this->add<Object>(obj);
268  }
269 
271  template<class Object>
272  void add (const Object & obj, int id = -1)
273  {
274  gsXmlNode* node =
275  internal::gsXml<Object>::put(obj, *data);
276  if ( ! node )
277  {
278  gsInfo<<"gsFileData: Trouble inserting "<<internal::gsXml<Object>::tag()
279  <<" to the XML tree. is \"put\" implemented ??\n";
280  }
281  else
282  {
283  data->appendToRoot(node,id);
284  }
285  }
286 
288  template<class Object>
289  void addWithLabel (const Object & obj, std::string label)
290  {
291  gsXmlNode* node =
292  internal::gsXml<Object>::put(obj, *data);
293  if ( ! node )
294  {
295  gsInfo<<"gsFileData: Trouble inserting "<<internal::gsXml<Object>::tag()
296  <<" to the XML tree. is \"put\" implemented ??\n";
297  }
298  else
299  {
300  data->appendToRoot(node,-1,label);
301  }
302  }
303 
305  void addString (const std::string & s)
306  {
307  gsXmlNode* node = internal::makeNode("string",s,*data);
308  data->appendToRoot(node);
309  }
310 
312  void addString (const std::string & s, const std::string & label)
313  {
314  gsXmlNode* node = internal::makeNode("string",s,*data);
315  node->append_attribute(internal::makeAttribute("label", label, *data));
316  data->appendToRoot(node);
317  }
318 
324  void addInclude( const std::string & filename, const real_t & time=-1.,
325  const index_t & id=-1, const std::string & label="");
326 
327 protected:
328  void getInclude(gsFileData & res, index_t id, real_t time, std::string label);
329 
330 public:
335  {
336  return getInclude(res, id, -1., "");
337  }
338 
342  void getIncludeByTime(gsFileData & res, real_t time)
343  {
344  return getInclude(res, -1,time, "");
345  }
346 
350  void getIncludeByLabel(gsFileData & res, std::string label)
351  {
352  return getInclude(res, -1,-1.,label);
353  }
354 
355  std::string getString () const
356  {
357 
358  gsXmlNode * node = getFirstNode("string");
359  //node = getNextSibling(node, "string");
360  std::string res( node->value() );
361  return res;
362  }
363 
364  std::string getString(index_t id) const
365  {
366  //GISMO_ASSERT(id < 0, "Id " << id << " should be >= 0!");
367 
368  gsXmlNode * root = getXmlRoot();
369  gsXmlNode * nd = internal::searchId(id, root, "string");
370  if (nd)
371  {
372  std::string res(nd->value());
373  return res;
374  }
375  GISMO_ERROR("String with id " << id << " does not exist!");
376  }
377 
378  std::string getStringByLabel (const std::string & label) const
379  {
380  //GISMO_ASSERT(id < 0, "Id " << id << " should be >= 0!");
381 
382  gsXmlNode * root = getXmlRoot();
383  gsXmlNode * nd = internal::searchNode( root, "label", label, "string");
384  if (nd)
385  {
386  std::string res(nd->value());
387  return res;
388  }
389  GISMO_ERROR("String with label " << label << " does not exist!");
390  }
391 
393  size_t bufferSize() const { return m_buffer.front().size(); };
394 
396  std::ostream &print(std::ostream &os) const;
397 
398 /*
405  template<class Object>
406  bool operator>>(Object * obj)
407  {
408  gsWarn<< "getting "<< typeid(Object).name() <<"\n";
409  gsXmlNode* node = getFirstNode(internal::gsXml<Object>::tag(),
410  internal::gsXml<Object>::type() );
411  if ( !node )
412  {
413  gsWarn<<"gsFileData: false!\n";
414  return false;
415  }
416  else
417  {
418  obj = internal::gsXml<Object>::get(node);
419  this->deleteXmlSubtree( node );
420  return true;
421  }
422  }
423 */
424 
437  template<class Object>
438  inline memory::unique_ptr<Object> getFirst() const
439  {
440  gsXmlNode* node = getFirstNode(internal::gsXml<Object>::tag(),
441  internal::gsXml<Object>::type() );
442  if ( !node )
443  {
444  gsWarn<<"gsFileData: getFirst: Didn't find any "<<
445  internal::gsXml<Object>::type()<<" "<<
446  internal::gsXml<Object>::tag() <<". Error.\n";
447  return memory::unique_ptr<Object>();
448  }
449  return memory::make_unique( internal::gsXml<Object>::get(node) );// Using gsXmlUtils
450  }
451 
465  template<class Object>
466  bool getFirst(Object & result) const
467  {
468  gsXmlNode* node = getFirstNode(internal::gsXml<Object>::tag(),
469  internal::gsXml<Object>::type() );
470  if ( !node )
471  {
472  gsWarn<<"gsFileData: getFirst: Didn't find any "<<
473  internal::gsXml<Object>::type()<<" "<<
474  internal::gsXml<Object>::tag() <<". Error.\n";
475  return false;
476  }
477  internal::gsXml<Object>::get_into(node, result);// Using gsXmlUtils
478  return true;
479  }
480 
482  template<class Object>
483  inline std::vector< memory::unique_ptr<Object> > getAll() const
484  {
485  std::vector< memory::unique_ptr<Object> > result;
486 
487  for (gsXmlNode * child = getFirstNode( internal::gsXml<Object>::tag(),
488  internal::gsXml<Object>::type() ) ;
489  child; child = getNextSibling(child, internal::gsXml<Object>::tag(),
490  internal::gsXml<Object>::type() ))
491  {
492  result.push_back( memory::make_unique(internal::gsXml<Object>::get(child)) );
493  }
494  return result;
495  }
496 
509  template<class Object>
510  inline memory::unique_ptr<Object> getAnyFirst() const
511  {
512  gsXmlNode* node = getAnyFirstNode(internal::gsXml<Object>::tag(),
513  internal::gsXml<Object>::type() );
514  if ( !node )
515  {
516  gsWarn <<"gsFileData: getAnyFirst: Didn't find any "<<
517  internal::gsXml<Object>::type()<<" "<<
518  internal::gsXml<Object>::tag() <<". Error.\n";
519  return memory::unique_ptr<Object>();
520  }
521  return memory::make_unique( internal::gsXml<Object>::get(node) );// Using gsXmlUtils
522  }
523 
537  template<class Object>
538  bool getAnyFirst(Object & result) const
539  {
540  gsXmlNode* node = getAnyFirstNode(internal::gsXml<Object>::tag(),
541  internal::gsXml<Object>::type() );
542  if ( !node )
543  {
544  gsWarn <<"gsFileData: getAnyFirst: Didn't find any "<<
545  internal::gsXml<Object>::type()<<" "<<
546  internal::gsXml<Object>::tag() <<". Error.\n";
547  return false;
548  }
549  internal::gsXml<Object>::get_into(node, result);// Using gsXmlUtils
550  return true;
551  }
552 
554  String contents () const;
555 
557  int numTags () const;
558 
559 private:
560 
561  gsXmlNode * getXmlRoot() const;
562  static void deleteXmlSubtree (gsXmlNode* node);
563 
564  // getFirst ? (tag and or type)
565  gsXmlNode * getFirstNode ( const String & name = "",
566  const String & type = "" ) const;
567 
568  // getAny
569  gsXmlNode * getAnyFirstNode( const String & name = "",
570  const String & type = "" ) const;
571 
572  // getNext
573  static gsXmlNode * getNextSibling( gsXmlNode* const & node,
574  const String & name = "",
575  const String & type = "" );
576 
577  // Helpers for X3D files
578  void addX3dShape(gsXmlNode * shape);
579  void addX3dTransform(gsXmlNode * shape);
580 
581 }; // class gsFileData
582 
583 // Print out operator
584 template<class T>
585 std::ostream &operator<<(std::ostream &os, const gsFileData<T> & fd)
586 {return fd.print(os); }
587 
588 #ifdef GISMO_WITH_PYBIND11
589 
593  void pybind11_init_gsFileData(pybind11::module &m);
594 
595 #endif // GISMO_WITH_PYBIND11
596 
597 } // namespace gismo
598 
599 #ifndef GISMO_BUILD_LIB
600 
601 #include GISMO_HPP_HEADER(gsFileData.hpp)
602 #endif
void addWithLabel(const Object &obj, std::string label)
Add the object to the Xml tree, same as &lt;&lt;, but also allows to set the XML label attribute.
Definition: gsFileData.h:289
unique_ptr< T > make_unique(T *x)
Definition: gsMemory.h:198
void addString(const std::string &s, const std::string &label)
Add a string to the Xml tree.
Definition: gsFileData.h:312
A fixed-size, statically allocated 3D vector.
Definition: gsVector.h:218
unsigned getFloatPrecision() const
Definition: gsFileData.h:101
bool readStlFile(String const &fn)
Reads STL mesh file.
Definition: gsFileData.hpp:1327
String tag() const
Prints the XML tag of a Gismo object.
Definition: gsFileData.h:210
bool readParasolidFile(String const &fn)
Reads parasolid files.
Definition: gsFileData.hpp:2539
void addString(const std::string &s)
Add a string to the Xml tree.
Definition: gsFileData.h:305
bool has() const
Returns true if an Object exists in the filedata.
Definition: gsFileData.h:220
bool getFirst(Object &result) const
Definition: gsFileData.h:466
memory::unique_ptr< Object > getId(const int &id) const
Searches and fetches the Gismo object with a given id.
Definition: gsFileData.h:180
bool hasTag(std::string tag) const
Returns true if an entry of tag exists in the xml file.
Definition: gsFileData.h:235
void saveCompressed(String const &fname="dump") const
Save file contents to compressed xml file.
Definition: gsFileData.hpp:131
S give(S &x)
Definition: gsMemory.h:266
#define index_t
Definition: gsConfig.h:32
size_t bufferSize() const
Returns the size of the data.
Definition: gsFileData.h:393
void add(const Object &obj, int id=-1)
Add the object to the Xml tree, same as &lt;&lt;, but also allows to set the XML id and label attributes...
Definition: gsFileData.h:272
gsXmlNode * searchId(const int id, gsXmlNode *root, const char *tag_name=NULL, const bool print_warning=true)
Definition: gsXml.h:254
void getIncludeById(gsFileData &res, index_t id)
Looks for a referenced Gismo .xml file ( &lt;xmlfile&gt; tag ) in the current xml tree, parses it in the gs...
Definition: gsFileData.h:334
int numTags() const
Counts the number of Objects/tags in the filedata.
Definition: gsFileData.hpp:2602
bool readXmlGzFile(String const &fn, bool recursive=false)
Reads a file with xml.gz extension.
Definition: gsFileData.hpp:240
bool getAnyFirst(Object &result) const
Definition: gsFileData.h:538
gsXmlNode * searchNode(gsXmlNode *root, const std::string &attr_name, const std::string &value, const char *tag_name=NULL)
Definition: gsXml.h:231
void operator<<(const Object &obj)
Inserts an object to the XML tree.
Definition: gsFileData.h:265
bool readOffFile(String const &fn)
Reads Off mesh file.
Definition: gsFileData.hpp:1259
bool readGismoXmlStream(std::istream &is, bool recursive=false)
Reads Gismo&#39;s native XML file.
Definition: gsFileData.hpp:252
#define gsWarn
Definition: gsDebug.h:50
String contents() const
Lists the contents of the filedata.
Definition: gsFileData.hpp:2583
void getIncludeByTime(gsFileData &res, real_t time)
Looks for a referenced Gismo .xml file ( &lt;xmlfile&gt; tag ) in the current xml tree, parses it in the gs...
Definition: gsFileData.h:342
bool hasAny() const
Definition: gsFileData.h:243
bool readObjFile(String const &fn)
Reads Wavefront OBJ file.
Definition: gsFileData.hpp:1410
bool read(String const &fn, bool recursive=false)
Definition: gsFileData.hpp:162
void writeIges(String const &fname)
Save multipatch contents to an IGES file.
void addInclude(const std::string &filename, const real_t &time=-1., const index_t &id=-1, const std::string &label="")
Add a reference ( &lt;xmlfile&gt; tag ) to another Gismo .xml file to the xml tree.
Definition: gsFileData.hpp:296
gsXmlAttribute * makeAttribute(const std::string &name, const std::string &value, gsXmlTree &data)
Helper to allocate XML attribute.
Definition: gsXml.cpp:37
#define gsInfo
Definition: gsDebug.h:43
memory::unique_ptr< Object > getAnyFirst() const
Definition: gsFileData.h:510
bool readIgesFile(String const &fn)
Reads Iges file.
Definition: gsFileData.hpp:2149
memory::unique_ptr< Object > getLabel(const std::string &name) const
Searches and fetches a poitner to a Gismo object with a given label.
Definition: gsFileData.h:195
bool readAxelFile(String const &fn)
Reads Axel file.
Definition: gsFileData.hpp:349
gsXmlNode * makeNode(const std::string &name, gsXmlTree &data)
Helper to allocate XML node.
Definition: gsXml.cpp:54
int numData() const
Reports the number of objects which are held in the file data.
Definition: gsFileData.h:71
void dump(String const &fname="dump") const
Dump file contents to an xml file.
Definition: gsFileData.hpp:86
bool hasId(int id) const
Returns true if an Object exists in the filedata.
Definition: gsFileData.h:227
std::vector< memory::unique_ptr< Object > > getAll() const
Returns a vector with all Objects found in the XML data.
Definition: gsFileData.h:483
String type() const
Prints the XML tag type of a Gismo object.
Definition: gsFileData.h:215
bool readGeompFile(String const &fn)
Reads GeoPDEs txt file.
Definition: gsFileData.hpp:882
memory::unique_ptr< Object > getFirst() const
Definition: gsFileData.h:438
FileData * data
File data as an xml tree.
Definition: gsFileData.h:105
int count() const
Counts the number of Objects in the filedata.
Definition: gsFileData.h:251
void getLabel(const std::string &name, Object &result) const
Searches and fetches the Gismo object with a given label.
Definition: gsFileData.h:202
void clear()
Clear all data.
Definition: gsFileData.hpp:69
bool read3dmFile(String const &fn)
Reads 3DM file.
Definition: gsFileData.hpp:2527
bool readX3dFile(String const &fn)
Reads X3D file.
Definition: gsFileData.hpp:2460
Utility class for finding files and handling paths.
EIGEN_STRONG_INLINE idMat_expr id(const index_t dim)
The identity matrix of dimension dim.
Definition: gsExpressions.h:4472
#define GISMO_ERROR(message)
Definition: gsDebug.h:118
bool readGoToolsFile(String const &fn)
Reads GoTools file.
Definition: gsFileData.hpp:511
This class represents an XML data tree which can be read from or written to a (file) stream...
Definition: gsFileData.h:33
void save(String const &fname="dump", bool compress=false) const
Save file contents to an xml file.
Definition: gsFileData.hpp:98
void getIncludeByLabel(gsFileData &res, std::string label)
Looks for a referenced Gismo .xml file ( &lt;xmlfile&gt; tag ) in the current xml tree, parses it in the gs...
Definition: gsFileData.h:350
bool readBrepFile(String const &fn)
Reads OpenCascade brep file.
Definition: gsFileData.hpp:1694
Provides declaration of input/output XML utilities struct.
bool readXmlFile(String const &fn, bool recursive=false)
Reads a file with xml extension.
Definition: gsFileData.hpp:229
std::ostream & print(std::ostream &os) const
Prints the XML data as a string.
Definition: gsFileData.hpp:77
void setFloatPrecision(const unsigned k)
Definition: gsFileData.h:96
void getId(const int &id, Object &result) const
Searches and fetches the Gismo object with a given id.
Definition: gsFileData.h:187