MshSaver.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // based on MSH writer from PyMesh
  2. // Copyright (c) 2015 Qingnan Zhou <[email protected]>
  3. // Copyright (C) 2020 Vladimir Fonov <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla
  6. // Public License v. 2.0. If a copy of the MPL was not distributed
  7. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_MSH_SAVER_H
  9. #define IGL_MSH_SAVER_H
  10. #include "igl_inline.h"
  11. #include <fstream>
  12. #include <string>
  13. #include <vector>
  14. namespace igl {
  15. /// Class for dumping information to .msh file
  16. /// depends only on c++stl library
  17. /// current implementation works only with 3D information
  18. class MshSaver {
  19. public:
  20. typedef double Float;
  21. typedef std::vector<int> IndexVector;
  22. typedef std::vector<int> IntVector;
  23. typedef std::vector<Float> FloatVector;
  24. typedef std::vector<FloatVector> FloatField;
  25. typedef std::vector<IntVector> IntField;
  26. typedef std::vector<std::string> FieldNames;
  27. /// Write a .msh to a given path
  28. /// @param[in] filename path to output file
  29. /// @param[in] binary whether to write in binary format
  30. MshSaver(const std::string& filename, bool binary=true);
  31. ~MshSaver();
  32. public:
  33. // Only these element types are supported right now
  34. enum {ELEMENT_LINE=1, ELEMENT_TRI=2, ELEMENT_QUAD=3,
  35. ELEMENT_TET=4, ELEMENT_HEX=5, ELEMENT_PRISM=6 };
  36. public:
  37. // save mesh geometry
  38. void save_mesh(
  39. const FloatVector& nodes,
  40. const IndexVector& elements,
  41. const IntVector& element_lengths,
  42. const IntVector& element_type,
  43. const IntVector& element_tags );
  44. // save additional fields associated with the mesh
  45. // add node scalar field
  46. void save_scalar_field(const std::string& fieldname, const FloatVector& field);
  47. // add node vectot field
  48. void save_vector_field(const std::string& fieldname, const FloatVector& field);
  49. // add element scalar field
  50. void save_elem_scalar_field(const std::string& fieldname, const FloatVector& field);
  51. // add element vector field
  52. void save_elem_vector_field(const std::string& fieldname, const FloatVector& field);
  53. // add element tensor field
  54. void save_elem_tensor_field(const std::string& fieldname, const FloatVector& field);
  55. protected:
  56. void save_header();
  57. void save_nodes(const FloatVector& nodes);
  58. void save_elements(const IndexVector& elements,
  59. const IntVector& element_lengths,
  60. const IntVector& element_type,
  61. const IntVector& element_tags);
  62. private:
  63. bool m_binary;
  64. size_t m_num_nodes;
  65. size_t m_num_elements;
  66. std::ofstream fout;
  67. };
  68. } //igl
  69. #ifndef IGL_STATIC_LIBRARY
  70. # include "MshSaver.cpp"
  71. #endif
  72. #endif //MSH_SAVER_H