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
19namespace igl {
20
23class MshLoader {
24 public:
25
26 struct msh_struct {
28 msh_struct(int _tag=0,int _type=0):
29 tag(_tag),el_type(_type){}
30 bool operator== (const msh_struct& a) const {
31 return this->tag==a.tag &&
32 this->el_type==a.el_type;
33 }
34
35 bool operator< (const msh_struct& a) const {
36 return (this->tag*100+this->el_type) <
37 (a.tag*100+a.el_type);
38 }
39 };
40
41 typedef double Float;
42
43 typedef std::vector<int> IndexVector;
44 typedef std::vector<int> IntVector;
45 typedef std::vector<Float> FloatVector;
46 typedef std::vector<FloatVector> FloatField;
47 typedef std::vector<IntVector> IntField;
48 typedef std::vector<std::string> FieldNames;
49 typedef std::multimap<msh_struct,int> StructIndex;
50 typedef std::vector<msh_struct> StructVector;
51
55 // 2nd order elements
60 // other elements
62 public:
65 MshLoader(const std::string &filename);
66
67 public:
68
69 // get nodes , x,y,z sequentially
70 const FloatVector& get_nodes() const { return m_nodes; }
71 // get elements , identifying nodes that create an element
72 // variable length per element
73 const IndexVector& get_elements() const { return m_elements; }
74
75 // get element types
76 const IntVector& get_elements_types() const { return m_elements_types; }
77 // get element lengths
78 const IntVector& get_elements_lengths() const { return m_elements_lengths; }
79 // get element tags ( physical (0) and elementary (1) )
80 const IntField& get_elements_tags() const { return m_elements_tags; }
81 // get element IDs
82 const IntVector& get_elements_ids() const { return m_elements_ids; }
83
84 // get reverse index from node to element
85 const IndexVector& get_elements_nodes_idx() const { return m_elements_nodes_idx; }
86
87 // get fields assigned per node, all fields and components sequentially
88 const FloatField& get_node_fields() const { return m_node_fields;}
89 // get node field names,
90 const FieldNames& get_node_fields_names() const { return m_node_fields_names;}
91 // get number of node field components
92 const IntVector& get_node_fields_components() const {return m_node_fields_components;}
93
94 int get_node_field_components(size_t c) const
95 {
96 return m_node_fields_components[c];
97 }
98
99 // get fields assigned per element, all fields and components sequentially
100 const FloatField& get_element_fields() const { return m_element_fields;}
101 // get element field names
102 const FieldNames& get_element_fields_names() const { return m_element_fields_names;}
103 // get number of element field components
104 const IntVector& get_element_fields_components() const {return m_element_fields_components;}
105
106 int get_element_field_components(size_t c) const {
107 return m_element_fields_components[c];
108 }
109 // check if field is present at node level
110 bool is_node_field(const std::string& fieldname) const {
111 return (std::find(std::begin(m_node_fields_names),
112 std::end(m_node_fields_names),
113 fieldname) != std::end(m_node_fields_names) );
114 }
115 // check if field is present at element level
116 bool is_element_field(const std::string& fieldname) const {
117 return (std::find(std::begin(m_element_fields_names),
118 std::end(m_element_fields_names),
119 fieldname) != std::end(m_node_fields_names) );
120 }
121
122 // check if all elements have ids assigned sequentially
124
125 // create tag index
126 // tag_column: ( physical (0) or elementary (1) ) specifying which tag to use
127 void index_structures(int tag_column);
128
129 // get tag index, call index_structure_tags first
131 {
132 return m_structure_index;
133 }
134
135 // get size of a structure identified by tag and element type
137 {
138 return m_structure_length;
139 }
140
143 {
144 return m_structures;
145 }
146
147 public:
148 // helper function, calculate number of nodes associated with an element
149 static int num_nodes_per_elem_type(int elem_type);
150
151 private:
152 void parse_nodes(std::ifstream& fin);
153 void parse_elements(std::ifstream& fin);
154 void parse_node_field(std::ifstream& fin);
155 void parse_element_field(std::ifstream& fin);
156 void parse_unknown_field(std::ifstream& fin,
157 const std::string& fieldname);
158
159 private:
160 bool m_binary;
161 size_t m_data_size;
162
163 FloatVector m_nodes; // len x 3 vector
164
165 IndexVector m_elements; // linear array for nodes corresponding to each element
166 IndexVector m_elements_nodes_idx; // element indexes
167
168 IntVector m_elements_ids; // element id's
169 IntVector m_elements_types; // Element types
170 IntVector m_elements_lengths; // Element lengths
171 IntField m_elements_tags; // Element tags, currently 2xtags per element
172
173 FloatField m_node_fields; // Float field defined at each node
174 IntVector m_node_fields_components; // Number of components for node field
175 FieldNames m_node_fields_names; // Node field name
176
177 FloatField m_element_fields; // Float field defined at each element
178 IntVector m_element_fields_components; // Number of components for element field
179 FieldNames m_element_fields_names; // Element field name
180
181 StructIndex m_structure_index; // index tag ids
182 StructVector m_structures; // unique structures
183 StructIndex m_structure_length; // length of structures with consistent element type
184};
185
186} //igl
187
188#ifndef IGL_STATIC_LIBRARY
189# include "MshLoader.cpp"
190#endif
191
192#endif //IGL_MSH_LOADER_H
Class for loading information from .msh file depends only on c++stl library.
Definition MshLoader.h:23
MshLoader(const std::string &filename)
Load a .msh file from a given path.
const IntVector & get_node_fields_components() const
Definition MshLoader.h:92
const IndexVector & get_elements_nodes_idx() const
Definition MshLoader.h:85
bool is_node_field(const std::string &fieldname) const
Definition MshLoader.h:110
const IntVector & get_elements_types() const
Definition MshLoader.h:76
const StructIndex & get_structure_index() const
Definition MshLoader.h:130
std::vector< int > IntVector
Definition MshLoader.h:44
const FloatVector & get_nodes() const
Definition MshLoader.h:70
const FloatField & get_element_fields() const
Definition MshLoader.h:100
bool is_element_map_identity() const
bool is_element_field(const std::string &fieldname) const
Definition MshLoader.h:116
std::vector< int > IndexVector
Definition MshLoader.h:43
const IndexVector & get_elements() const
Definition MshLoader.h:73
const IntVector & get_elements_ids() const
Definition MshLoader.h:82
std::vector< FloatVector > FloatField
Definition MshLoader.h:46
std::vector< msh_struct > StructVector
Definition MshLoader.h:50
const StructVector & get_structures() const
get list of structures
Definition MshLoader.h:142
static int num_nodes_per_elem_type(int elem_type)
std::vector< std::string > FieldNames
Definition MshLoader.h:48
const FloatField & get_node_fields() const
Definition MshLoader.h:88
double Float
Definition MshLoader.h:41
std::vector< Float > FloatVector
Definition MshLoader.h:45
const FieldNames & get_node_fields_names() const
Definition MshLoader.h:90
int get_node_field_components(size_t c) const
Definition MshLoader.h:94
@ ELEMENT_PYRAMID
Definition MshLoader.h:54
@ ELEMENT_TET_2ND_ORDER
Definition MshLoader.h:57
@ ELEMENT_LINE_2ND_ORDER
Definition MshLoader.h:56
@ ELEMENT_PRISM_2ND_ORDER
Definition MshLoader.h:58
@ ELEMENT_PRISM
Definition MshLoader.h:53
@ ELEMENT_HEX
Definition MshLoader.h:53
@ ELEMENT_TRI_2ND_ORDER
Definition MshLoader.h:56
@ ELEMENT_TET
Definition MshLoader.h:53
@ ELEMENT_LINE
Definition MshLoader.h:52
@ ELEMENT_PYRAMID_2ND_ORDER
Definition MshLoader.h:59
@ ELEMENT_HEX_2ND_ORDER
Definition MshLoader.h:58
@ ELEMENT_POINT
Definition MshLoader.h:61
@ ELEMENT_TRI
Definition MshLoader.h:52
@ ELEMENT_QUAD_2ND_ORDER
Definition MshLoader.h:57
@ ELEMENT_QUAD
Definition MshLoader.h:52
int get_element_field_components(size_t c) const
Definition MshLoader.h:106
void index_structures(int tag_column)
const StructIndex & get_structure_length() const
Definition MshLoader.h:136
std::multimap< msh_struct, int > StructIndex
Definition MshLoader.h:49
const IntVector & get_element_fields_components() const
Definition MshLoader.h:104
const IntField & get_elements_tags() const
Definition MshLoader.h:80
const FieldNames & get_element_fields_names() const
Definition MshLoader.h:102
const IntVector & get_elements_lengths() const
Definition MshLoader.h:78
std::vector< IntVector > IntField
Definition MshLoader.h:47
Definition AABB.h:17
Definition MshLoader.h:26
msh_struct(int _tag=0, int _type=0)
Definition MshLoader.h:28
int tag
Definition MshLoader.h:27
int el_type
Definition MshLoader.h:27
bool operator<(const msh_struct &a) const
Definition MshLoader.h:35
bool operator==(const msh_struct &a) const
Definition MshLoader.h:30