libigl v2.5.0
Loading...
Searching...
No Matches
MshLoader.h
Go to the documentation of this file.
1// based on MSH reader from PyMesh
2
3// Copyright (c) 2015 Qingnan Zhou <qzhou@adobe.com>
4// Copyright (C) 2020 Vladimir Fonov <vladimir.fonov@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9#ifndef IGL_MSH_LOADER_H
10#define IGL_MSH_LOADER_H
11#include "igl_inline.h"
12
13#include <fstream>
14#include <map>
15#include <string>
16#include <vector>
17#include <algorithm>
18#include <unordered_map>
19
20namespace igl {
21
24class MshLoader {
25 public:
26
27 struct msh_struct {
29 msh_struct(int _tag=0,int _type=0):
30 tag(_tag),el_type(_type){}
31 bool operator== (const msh_struct& a) const {
32 return this->tag==a.tag &&
33 this->el_type==a.el_type;
34 }
35
36 bool operator< (const msh_struct& a) const {
37 return (this->tag*100+this->el_type) <
38 (a.tag*100+a.el_type);
39 }
40 };
41
42 typedef double Float;
43
44 typedef std::vector<int> IndexVector;
45 typedef std::vector<int> IntVector;
46 typedef std::vector<Float> FloatVector;
47 typedef std::vector<FloatVector> FloatField;
48 typedef std::vector<IntVector> IntField;
49 typedef std::vector<std::string> FieldNames;
50 typedef std::multimap<msh_struct,int> StructIndex;
51 typedef std::vector<msh_struct> StructVector;
52
56 // 2nd order elements
61 // other elements
63 public:
66 MshLoader(const std::string &filename);
67
68 public:
69
70 // get nodes , x,y,z sequentially
71 const FloatVector& get_nodes() const { return m_nodes; }
72 // get elements , identifying nodes that create an element
73 // variable length per element
74 const IndexVector& get_elements() const { return m_elements; }
75
76 // get element types
77 const IntVector& get_elements_types() const { return m_elements_types; }
78 // get element lengths
79 const IntVector& get_elements_lengths() const { return m_elements_lengths; }
80 // get element tags ( physical (0) and elementary (1) )
81 const IntField& get_elements_tags() const { return m_elements_tags; }
82 // get element IDs
83 const IntVector& get_elements_ids() const { return m_elements_ids; }
84
85 // get reverse index from node to element
86 const IndexVector& get_elements_nodes_idx() const { return m_elements_nodes_idx; }
87
88 // get fields assigned per node, all fields and components sequentially
89 const FloatField& get_node_fields() const { return m_node_fields;}
90 // get node field names,
91 const FieldNames& get_node_fields_names() const { return m_node_fields_names;}
92 // get number of node field components
93 const IntVector& get_node_fields_components() const {return m_node_fields_components;}
94
95 int get_node_field_components(size_t c) const
96 {
97 return m_node_fields_components[c];
98 }
99
100 // get fields assigned per element, all fields and components sequentially
101 const FloatField& get_element_fields() const { return m_element_fields;}
102 // get element field names
103 const FieldNames& get_element_fields_names() const { return m_element_fields_names;}
104 // get number of element field components
105 const IntVector& get_element_fields_components() const {return m_element_fields_components;}
106
107 int get_element_field_components(size_t c) const {
108 return m_element_fields_components[c];
109 }
110 // check if field is present at node level
111 bool is_node_field(const std::string& fieldname) const {
112 return (std::find(std::begin(m_node_fields_names),
113 std::end(m_node_fields_names),
114 fieldname) != std::end(m_node_fields_names) );
115 }
116 // check if field is present at element level
117 bool is_element_field(const std::string& fieldname) const {
118 return (std::find(std::begin(m_element_fields_names),
119 std::end(m_element_fields_names),
120 fieldname) != std::end(m_node_fields_names) );
121 }
122
123 // check if all elements have ids assigned sequentially
125
126 // create tag index
127 // tag_column: ( physical (0) or elementary (1) ) specifying which tag to use
128 void index_structures(int tag_column);
129
130 // get tag index, call index_structure_tags first
132 {
133 return m_structure_index;
134 }
135
136 // get size of a structure identified by tag and element type
138 {
139 return m_structure_length;
140 }
141
144 {
145 return m_structures;
146 }
147
148 public:
149 // helper function, calculate number of nodes associated with an element
150 static int num_nodes_per_elem_type(int elem_type);
151
152 private:
153 void parse_nodes(std::ifstream& fin);
154 void parse_elements(std::ifstream& fin);
155 void parse_node_field(std::ifstream& fin);
156 void parse_element_field(std::ifstream& fin);
157 void parse_unknown_field(std::ifstream& fin,
158 const std::string& fieldname);
159 int node_dense_index(int node_tag) const;
160 int element_dense_index(int elem_tag) const;
161
162 private:
163 bool m_binary;
164 size_t m_data_size;
165 std::unordered_map<int, int> m_node_tag_to_dense;
166 std::unordered_map<int, int> m_element_tag_to_dense;
167
168 FloatVector m_nodes; // len x 3 vector
169
170 IndexVector m_elements; // linear array for nodes corresponding to each element
171 IndexVector m_elements_nodes_idx; // element indexes
172
173 IntVector m_elements_ids; // element id's
174 IntVector m_elements_types; // Element types
175 IntVector m_elements_lengths; // Element lengths
176 IntField m_elements_tags; // Element tags, currently 2xtags per element
177
178 FloatField m_node_fields; // Float field defined at each node
179 IntVector m_node_fields_components; // Number of components for node field
180 FieldNames m_node_fields_names; // Node field name
181
182 FloatField m_element_fields; // Float field defined at each element
183 IntVector m_element_fields_components; // Number of components for element field
184 FieldNames m_element_fields_names; // Element field name
185
186 StructIndex m_structure_index; // index tag ids
187 StructVector m_structures; // unique structures
188 StructIndex m_structure_length; // length of structures with consistent element type
189};
190
191} //igl
192
193#ifndef IGL_STATIC_LIBRARY
194# include "MshLoader.cpp"
195#endif
196
197#endif //IGL_MSH_LOADER_H
MshLoader(const std::string &filename)
Load a .msh file from a given path.
const IntVector & get_node_fields_components() const
Definition MshLoader.h:93
const IndexVector & get_elements_nodes_idx() const
Definition MshLoader.h:86
bool is_node_field(const std::string &fieldname) const
Definition MshLoader.h:111
const IntVector & get_elements_types() const
Definition MshLoader.h:77
const StructIndex & get_structure_index() const
Definition MshLoader.h:131
std::vector< int > IntVector
Definition MshLoader.h:45
const FloatVector & get_nodes() const
Definition MshLoader.h:71
const FloatField & get_element_fields() const
Definition MshLoader.h:101
bool is_element_map_identity() const
bool is_element_field(const std::string &fieldname) const
Definition MshLoader.h:117
std::vector< int > IndexVector
Definition MshLoader.h:44
const IndexVector & get_elements() const
Definition MshLoader.h:74
const IntVector & get_elements_ids() const
Definition MshLoader.h:83
std::vector< FloatVector > FloatField
Definition MshLoader.h:47
std::vector< msh_struct > StructVector
Definition MshLoader.h:51
const StructVector & get_structures() const
get list of structures
Definition MshLoader.h:143
static int num_nodes_per_elem_type(int elem_type)
std::vector< std::string > FieldNames
Definition MshLoader.h:49
const FloatField & get_node_fields() const
Definition MshLoader.h:89
double Float
Definition MshLoader.h:42
@ ELEMENT_PYRAMID
Definition MshLoader.h:55
@ ELEMENT_TET_2ND_ORDER
Definition MshLoader.h:58
@ ELEMENT_LINE_2ND_ORDER
Definition MshLoader.h:57
@ ELEMENT_PRISM_2ND_ORDER
Definition MshLoader.h:59
@ ELEMENT_PRISM
Definition MshLoader.h:54
@ ELEMENT_HEX
Definition MshLoader.h:54
@ ELEMENT_TRI_2ND_ORDER
Definition MshLoader.h:57
@ ELEMENT_TET
Definition MshLoader.h:54
@ ELEMENT_LINE
Definition MshLoader.h:53
@ ELEMENT_PYRAMID_2ND_ORDER
Definition MshLoader.h:60
@ ELEMENT_HEX_2ND_ORDER
Definition MshLoader.h:59
@ ELEMENT_POINT
Definition MshLoader.h:62
@ ELEMENT_TRI
Definition MshLoader.h:53
@ ELEMENT_QUAD_2ND_ORDER
Definition MshLoader.h:58
@ ELEMENT_QUAD
Definition MshLoader.h:53
std::vector< Float > FloatVector
Definition MshLoader.h:46
const FieldNames & get_node_fields_names() const
Definition MshLoader.h:91
int get_node_field_components(size_t c) const
Definition MshLoader.h:95
int get_element_field_components(size_t c) const
Definition MshLoader.h:107
void index_structures(int tag_column)
const StructIndex & get_structure_length() const
Definition MshLoader.h:137
std::multimap< msh_struct, int > StructIndex
Definition MshLoader.h:50
const IntVector & get_element_fields_components() const
Definition MshLoader.h:105
const IntField & get_elements_tags() const
Definition MshLoader.h:81
const FieldNames & get_element_fields_names() const
Definition MshLoader.h:103
const IntVector & get_elements_lengths() const
Definition MshLoader.h:79
std::vector< IntVector > IntField
Definition MshLoader.h:48
Definition AABB.h:18
msh_struct(int _tag=0, int _type=0)
Definition MshLoader.h:29
int tag
Definition MshLoader.h:28
int el_type
Definition MshLoader.h:28
bool operator<(const msh_struct &a) const
Definition MshLoader.h:36
bool operator==(const msh_struct &a) const
Definition MshLoader.h:31