Hermes2D  3.0
mesh_reader_h2d_bson.h
1 // This file is part of Hermes2D
2 //
3 // Hermes2D is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // Hermes2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Hermes2D; if not, see <http://www.gnu.prg/licenses/>.
15 
16 #ifndef _MESH_READER_H2D_BSON_H_
17 #define _MESH_READER_H2D_BSON_H_
18 
19 #include "mesh_reader.h"
20 
21 #ifdef WITH_BSON
22 #include "bson.h"
23 namespace Hermes
24 {
25  namespace Hermes2D
26  {
42  class HERMES_API MeshReaderH2DBSON : public MeshReader
43  {
44  public:
45  MeshReaderH2DBSON();
46  virtual ~MeshReaderH2DBSON();
47 
49  virtual void load(const char *filename, MeshSharedPtr mesh);
50 
52  void save(const char *filename, MeshSharedPtr mesh);
53 
57  void load(const char *filename, std::vector<MeshSharedPtr> meshes);
58 
60  void save(const char *filename, std::vector<MeshSharedPtr> meshes);
61 
62  private:
63  struct vertex_BSON
64  {
65  vertex_BSON(){}
66  vertex_BSON(double x, double y, int i) : x(x), y(y), i(i) {}
67  void save_to_BSON(bson& bw)
68  {
69  bson_append_start_object(&bw, "vertex");
70  bson_append_double(&bw, "x", x);
71  bson_append_double(&bw, "y", y);
72  bson_append_int(&bw, "i", i);
73  bson_append_finish_object(&bw);
74  }
75  void load_from_BSON(bson& sub)
76  {
77  bson_iterator sub_it;
78  bson_find(&sub_it, &sub, "x");
79  this->x = bson_iterator_double(&sub_it);
80  bson_find(&sub_it, &sub, "y");
81  this->y = bson_iterator_double(&sub_it);
82  bson_find(&sub_it, &sub, "i");
83  this->i = bson_iterator_int(&sub_it);
84  }
85  double x;
86  double y;
87  int i;
88  };
89 
90  struct edge_BSON
91  {
92  edge_BSON(){}
93  edge_BSON(int v1, int v2, std::string marker, int i) : v1(v1), v2(v2), marker(marker), i(i) {}
94  void save_to_BSON(bson& bw)
95  {
96  bson_append_start_object(&bw, "edge");
97  bson_append_int(&bw, "v1", v1);
98  bson_append_int(&bw, "v2", v2);
99  bson_append_string(&bw, "marker", marker.c_str());
100  bson_append_int(&bw, "i", i);
101  bson_append_finish_object(&bw);
102  }
103  void load_from_BSON(bson& sub)
104  {
105  bson_iterator sub_it;
106  bson_find(&sub_it, &sub, "v1");
107  this->v1 = bson_iterator_int(&sub_it);
108  bson_find(&sub_it, &sub, "v2");
109  this->v2 = bson_iterator_int(&sub_it);
110  bson_find(&sub_it, &sub, "marker");
111  this->marker = bson_iterator_string(&sub_it);
112  bson_find(&sub_it, &sub, "i");
113  this->i = bson_iterator_int(&sub_it);
114  }
115  int v1;
116  int v2;
117  std::string marker;
118  int i;
119  };
120 
121  struct element_BSON
122  {
123  element_BSON(){}
124  element_BSON(int v1, int v2, int v3, int v4, std::string marker, int i) : v1(v1), v2(v2), v3(v3), v4(v4), marker(marker), i(i) {}
125  void save_to_BSON(bson& bw)
126  {
127  bson_append_start_object(&bw, "element");
128  bson_append_int(&bw, "v1", v1);
129  bson_append_int(&bw, "v2", v2);
130  bson_append_int(&bw, "v3", v3);
131  bson_append_int(&bw, "v4", v4);
132  bson_append_string(&bw, "marker", marker.c_str());
133  bson_append_int(&bw, "i", i);
134  bson_append_finish_object(&bw);
135  }
136  void load_from_BSON(bson& sub)
137  {
138  bson_iterator sub_it;
139  bson_find(&sub_it, &sub, "v1");
140  this->v1 = bson_iterator_int(&sub_it);
141  bson_find(&sub_it, &sub, "v2");
142  this->v2 = bson_iterator_int(&sub_it);
143  bson_find(&sub_it, &sub, "v3");
144  this->v3 = bson_iterator_int(&sub_it);
145  bson_find(&sub_it, &sub, "v4");
146  this->v4 = bson_iterator_int(&sub_it);
147  bson_find(&sub_it, &sub, "marker");
148  this->marker = bson_iterator_string(&sub_it);
149  bson_find(&sub_it, &sub, "i");
150  this->i = bson_iterator_int(&sub_it);
151  }
152  int v1;
153  int v2;
154  int v3;
155  int v4;
156  std::string marker;
157  int i;
158  };
159 
160  struct arc_BSON
161  {
162  arc_BSON(){}
163  arc_BSON(int p1, int p2, double angle) : p1(p1), p2(p2), angle(angle) {}
164  void save_to_BSON(bson& bw)
165  {
166  bson_append_start_object(&bw, "arc");
167  bson_append_int(&bw, "p1", p1);
168  bson_append_int(&bw, "p2", p2);
169  bson_append_double(&bw, "angle", angle);
170  bson_append_finish_object(&bw);
171  }
172  void load_from_BSON(bson& sub)
173  {
174  bson_iterator sub_it;
175  bson_find(&sub_it, &sub, "p1");
176  this->p1 = bson_iterator_int(&sub_it);
177  bson_find(&sub_it, &sub, "p2");
178  this->p2 = bson_iterator_int(&sub_it);
179  bson_find(&sub_it, &sub, "angle");
180  this->angle = bson_iterator_double(&sub_it);
181  }
182  int p1;
183  int p2;
184  double angle;
185  };
186 
187  struct refinement_BSON
188  {
189  refinement_BSON() {}
190  refinement_BSON(int id, int type) : id(id), type(type) {}
191  void save_to_BSON(bson& bw)
192  {
193  bson_append_start_object(&bw, "refinement");
194  bson_append_int(&bw, "id", id);
195  bson_append_int(&bw, "type", type);
196  bson_append_finish_object(&bw);
197  }
198  void load_from_BSON(bson& sub)
199  {
200  bson_iterator sub_it;
201  bson_find(&sub_it, &sub, "id");
202  this->id = bson_iterator_int(&sub_it);
203  bson_find(&sub_it, &sub, "type");
204  this->type = bson_iterator_int(&sub_it);
205  }
206  int id;
207  int type;
208  };
209 
210  struct subdomain_BSON
211  {
212  void save_to_BSON(bson& bw)
213  {
214  bson_append_start_object(&bw, "subdomain");
215  // vertices
216  bson_append_start_array(&bw, "vertices");
217  for (int i = 0; i < vertices.size(); i++)
218  bson_append_int(&bw, "i", vertices[i]);
219  bson_append_finish_array(&bw);
220 
221  // elements
222  bson_append_start_array(&bw, "elements");
223  for (int i = 0; i < elements.size(); i++)
224  bson_append_int(&bw, "i", elements[i]);
225  bson_append_finish_array(&bw);
226 
227  // boundary edges
228  bson_append_start_array(&bw, "boundary_edges");
229  for (int i = 0; i < boundary_edges.size(); i++)
230  bson_append_int(&bw, "i", boundary_edges[i]);
231  bson_append_finish_array(&bw);
232 
233  // inner edges
234  bson_append_start_array(&bw, "inner_edges");
235  for (int i = 0; i < inner_edges.size(); i++)
236  bson_append_int(&bw, "i", inner_edges[i]);
237  bson_append_finish_array(&bw);
238 
239  // refinements
240  bson_append_start_array(&bw, "refinements");
241  for (int i = 0; i < refinements.size(); i++)
242  refinements[i].save_to_BSON(bw);
243  bson_append_finish_array(&bw);
244 
245  bson_append_finish_object(&bw);
246  }
247  void load_from_BSON(bson& sub)
248  {
249  bson sub_sub;
250  bson_iterator sub_it, sub_sub_it;
251  // vertices
252  bson_find(&sub_it, &sub, "vertices");
253  bson_iterator_subobject_init(&sub_it, &sub_sub, 0);
254  bson_iterator_init(&sub_sub_it, &sub_sub);
255  while (bson_iterator_next(&sub_sub_it))
256  this->vertices.push_back(bson_iterator_int(&sub_sub_it));
257 
258  // elements
259  bson_find(&sub_it, &sub, "elements");
260  bson_iterator_subobject_init(&sub_it, &sub_sub, 0);
261  bson_iterator_init(&sub_sub_it, &sub_sub);
262  while (bson_iterator_next(&sub_sub_it))
263  this->elements.push_back(bson_iterator_int(&sub_sub_it));
264 
265  // boundary_edges
266  bson_find(&sub_it, &sub, "boundary_edges");
267  bson_iterator_subobject_init(&sub_it, &sub_sub, 0);
268  bson_iterator_init(&sub_sub_it, &sub_sub);
269  while (bson_iterator_next(&sub_sub_it))
270  this->boundary_edges.push_back(bson_iterator_int(&sub_sub_it));
271 
272  // inner_edges
273  bson_find(&sub_it, &sub, "inner_edges");
274  bson_iterator_subobject_init(&sub_it, &sub_sub, 0);
275  bson_iterator_init(&sub_sub_it, &sub_sub);
276  while (bson_iterator_next(&sub_sub_it))
277  this->inner_edges.push_back(bson_iterator_int(&sub_sub_it));
278 
279  // refinements
280  bson_find(&sub_it, &sub, "refinements");
281  bson_iterator_subobject_init(&sub_it, &sub_sub, 0);
282  bson_iterator_init(&sub_sub_it, &sub_sub);
283  bson b_sub_sub;
284  while (bson_iterator_next(&sub_sub_it))
285  {
286  refinement_BSON refinement;
287  bson_iterator_subobject_init(&sub_sub_it, &b_sub_sub, 0);
288  refinement.load_from_BSON(b_sub_sub);
289  this->refinements.push_back(refinement);
290  }
291  }
292  std::vector<int> vertices;
293  std::vector<int> elements;
294  std::vector<int> boundary_edges;
295  std::vector<int> inner_edges;
296  std::vector<refinement_BSON> refinements;
297  };
298 
299  static bool elementCompare(element_BSON el_i, element_BSON el_j) { return (el_i.i < el_j.i); }
300 
301  void load_domain(bson& br, MeshSharedPtr mesh, std::map<int, int>& vertex_is, std::map<int, int>& element_is, std::map<int, int>& edge_is,
302  std::vector<element_BSON>& elements, std::vector<edge_BSON>& edges, std::vector<vertex_BSON>& vertices, std::vector<arc_BSON>& arcs, std::vector<subdomain_BSON>& subdomains);
303  };
304  }
305 }
306 #endif
307 #endif
::xsd::cxx::tree::id< char, ncname > id
C++ type corresponding to the ID XML Schema built-in type.
Definition: adapt.h:24
::xsd::cxx::tree::type type
C++ type corresponding to the anyType XML Schema built-in type.
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.