GetFEM  5.5
bgeot_mesh_structure.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 1999-2026 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /**@file bgeot_mesh_structure.h
32  @author Yves Renard <[email protected]>
33  @date November 5, 1999.
34  @brief Mesh structure definition
35 */
36 
37 #ifndef BGEOT_MESH_STRUCTURE_H__
38 #define BGEOT_MESH_STRUCTURE_H__
39 
40 #include <set>
41 #include "bgeot_convex_structure.h"
42 #include "dal_tree_sorted.h"
43 
44 namespace bgeot {
45 
46  struct APIDECL mesh_convex_structure {
47  typedef std::vector<size_type> ind_pt_ct;
48 
49  pconvex_structure cstruct; /* type of convex. */
50  ind_pt_ct pts; /* point list indices. */
51 
52  pconvex_structure structure() const { return cstruct; }
53  pconvex_structure &structure() { return cstruct; }
54  mesh_convex_structure() : cstruct(0) {}
55  };
56 
57  struct convex_face
58  {
59  convex_face(size_type element, short_type face) : cv(element), f(face) {}
60  size_type cv = -1;
61  short_type f = -1;
62  bool valid() const {return (cv != size_type(-1)) && (f != short_type(-1));}
63  static convex_face invalid_face() {return {size_type(-1), short_type(-1)};}
64  };
65 
66  /**@addtogroup mesh */
67  ///@{
68  /** Mesh structure definition.
69  * At this point, the mesh is just a graph: the points have no
70  * associated coordinates
71  */
72  class APIDECL mesh_structure {
73 
74  public :
75 
76  typedef std::vector<size_type> ind_cv_ct;
77  typedef std::vector<size_type> ind_set;
78  typedef gmm::tab_ref_index_ref<ind_cv_ct::const_iterator,
79  convex_ind_ct::const_iterator> ind_pt_face_ct;
81 
82  protected :
83 
84  dal::dynamic_tas<mesh_convex_structure, 8> convex_tab;
85  point_ct points_tab;
86 
87  public :
88 
89  /// Return the list of valid convex IDs
90  const dal::bit_vector &convex_index() const
91  { return convex_tab.index(); }
92  /// Return the list of valid convex IDs of a given dimension
93  dal::bit_vector convex_index(dim_type) const;
94  /// The total number of convexes in the mesh
95  size_type nb_convex() const { return convex_tab.card(); }
96  /// The number of convex indexes from 0 to the index of the last convex
98  { return convex_tab.index().last_true()+1; }
99  /// Return true if i is in convex_index()
100  bool is_convex_valid(size_type i) { return (convex_tab.index())[i]; }
101  size_type nb_max_points() const { return points_tab.size(); }
102  /// Return true if the point i is used by at least one convex
103  bool is_point_valid(size_type i) const { return !(points_tab[i].empty()); }
104  /** Return a container to the list of points attached to convex ic.
105  They are ordered according to structure_of_convex(ic) */
106  const ind_set &ind_points_of_convex(size_type ic) const
107  { return convex_tab[ic].pts; }
108  /// Return the "local" index for point ip of the mesh
109  size_type local_ind_of_convex_point(size_type ic, size_type ip) const;
110  /// Return the pconvex_structure of the convex ic.
112  { return convex_tab[ic].cstruct; }
113  /// Return the number of points of convex ic.
115  { return convex_tab[ic].cstruct->nb_points(); }
116  /// Return the number of faces of convex ic.
118  { return short_type(convex_tab[ic].cstruct->nb_faces()); }
119  /// Exchange two point IDs
120  void swap_points(size_type i, size_type j);
121  /// Exchange two convex IDs
122  void swap_convex(size_type cv1, size_type cv2);
123 
124  template<class ITER>
125  size_type add_convex_noverif(pconvex_structure cs, ITER ipts,
126  size_type to_index = size_type(-1));
127  /** Insert a new convex in the mesh_structure.
128  @param cs the structure of the new convex.
129  @param ipts an iterator over a sequence of integers (point IDs of the convex nodes).
130  @param present an optional argument, contains true on return if the convex already exists in the mesh_structure.
131  @return the convex ID
132  */
133  template<class ITER>
134  size_type add_convex(pconvex_structure cs,
135  ITER ipts, bool *present = 0);
136  template<class ITER> size_type add_simplex(dim_type dim, ITER ipts)
137  { return add_convex(simplex_structure(dim), ipts); }
138  size_type add_segment(size_type a, size_type b);
139  /** Remove the convex ic */
140  void sup_convex(size_type ic);
141  /** Remove a convex given its points
142  @param nb the number of points for the convex
143  @param ipts an iterator over the list of point IDs of the convex
144  */
145  template<class ITER>
146  void sup_convex_with_points(ITER ipts, short_type nb);
147  void sup_segment(size_type a, size_type b)
148  { size_type t[2]; t[0] = a; t[1] = b; sup_convex_with_points(&t[0], 2); }
149  /** Insert a new convex corresponding to face f of the convex ic */
150  size_type add_face_of_convex(size_type ic, short_type f);
151  /** Insert new convexes corresponding to the faces of the convex ic */
152  void add_faces_of_convex(size_type ic);
153  /** build a new mesh, such that its convexes are the faces of the
154  convexes of the previous one */
155  void to_faces(dim_type n);
156  /** build a new mesh, such that its convexes are the edges of the
157  convexes of the previous one */
158  void to_edges();
159 
160  size_type nb_convex_with_edge(size_type i1, size_type i2);
161  void convex_with_edge(size_type i1, size_type i2,
162  std::vector<size_type> &ipt) const;
163 
164  /** Return a container of the convexes attached to point ip */
165  const ind_cv_ct &convex_to_point(size_type ip) const
166  { return points_tab[ip]; }
167  /** Return a container of the points attached (via an edge) to point ip */
168  void ind_points_to_point(size_type, ind_set &) const;
169 
170  /** Return true if the convex contains the listed points.
171  @param ic the convex ID.
172  @param nb the number of points which are searched in ic.
173  @param pit an iterator to the list of points searched.
174  */
175  template<class ITER>
176  bool is_convex_having_points(size_type ic,short_type nb, ITER pit) const;
177 
178  /** Return true if the face of the convex contains the given list of points */
179  template<class ITER>
180  bool is_convex_face_having_points(size_type ic, short_type face_num,
181  short_type nb, ITER pit) const;
182 
183  /** Return a container of the (global) point number for face f or convex ic */
184  ind_pt_face_ct ind_points_of_face_of_convex(size_type ic,
185  short_type f) const;
186 
187  size_type memsize() const;
188  /** Reorder the convex IDs and point IDs, such that there is no
189  hole in their numbering. */
190  void optimize_structure();
191  /// erase the mesh
192  void clear();
193  void stat();
194 
195  /** Return in s a list of neighbors of a given convex face.
196  @param ic the convex id.
197  @param f the face number of the convex.
198  @param s the resulting ind_set.
199  */
200  void neighbors_of_convex(size_type ic, short_type f, ind_set &s) const;
201  void neighbours_of_convex(size_type ic, short_type f, ind_set &s) const
202  IS_DEPRECATED { neighbors_of_convex(ic, f, s); }
203 
204  /** Return in s a list of neighbors of a given convex sharing the
205  intersection of a given list of faces
206  @param ic the convex id.
207  @param f the face number of the convex.
208  @param s the resulting ind_set.
209  */
210  void neighbors_of_convex(size_type ic,
211  const std::vector<short_type> &ftab,
212  ind_set &s) const;
213  void neighbours_of_convex(size_type ic,
214  const std::vector<short_type> &ftab,
215  ind_set &s) const IS_DEPRECATED
216  { neighbors_of_convex(ic, ftab, s); }
217 
218  /** Return a list of neighbors of a given convex.
219  @param ic the convex id.
220  @param s the resulting ind_set.
221  */
222  void neighbors_of_convex(size_type ic, ind_set &s) const;
223  void neighbours_of_convex(size_type ic, ind_set &s) const
224  IS_DEPRECATED { neighbors_of_convex(ic, s); }
225 
226  /** Return a neighbor convex of a given convex face.
227  @param ic the convex id.
228  @param f the face number of the convex.
229  @return size_type(-1) if there is no neighbor to this convex and
230  the index of the first neighbor found otherwise.
231  */
232  size_type neighbor_of_convex(size_type ic, short_type f) const;
233  size_type neighbour_of_convex(size_type ic, short_type f) const
234  IS_DEPRECATED { return neighbor_of_convex(ic, f); }
235 
236  /**Return a face of the neighbouring element that is adjacent to the
237  given face
238  @param ic the convex id.
239  @param f the face number of the convex.
240  @return convex_face that contains the neighbors convex id and the
241  adjacent face number, or convex_face::invalid_face() otherwise.
242  */
243  convex_face adjacent_face(size_type ic, short_type f) const;
244 
245  bool is_convex_having_neighbor(size_type ic, short_type f) const
246  { return (neighbor_of_convex(ic, f) != size_type(-1)); }
247  bool is_convex_having_neighbour(size_type ic, short_type f) const
248  IS_DEPRECATED { return (neighbor_of_convex(ic, f) != size_type(-1)); }
249 
250  /** Convex ID of the first convex attached to the point ip. */
252  { return points_tab[ip].empty() ? size_type(-1) : points_tab[ip][0]; }
253  /** Find the local index of the point of global index ip with respect to
254  * the convex cv.
255  * @return local index (a number smaller than
256  * nb_points_of_convex(first_convex_of_point(ip))) or size_type(-1) if
257  * the point is not found.
258  */
259  size_type ind_in_convex_of_point(size_type ic, size_type ip) const;
260  };
261  ///@}
262 
263 
264 
265 
266  /** Return the cuthill_mc_kee ordering on the convexes */
267  void APIDECL cuthill_mckee_on_convexes(const bgeot::mesh_structure &ms,
268  std::vector<size_type> &cmk);
269 
270  template<class ITER>
272  short_type nb, ITER pit) const {
273  const ind_set &pt = ind_points_of_convex(ic);
274  for (short_type i = 0; i < nb; ++i, ++pit)
275  if (std::find(pt.begin(), pt.end(), *pit) == pt.end())
276  return false;
277  return true;
278  }
279 
280  template<class ITER> bool
282  short_type nb, ITER pit) const {
283  ind_pt_face_ct pt = ind_points_of_face_of_convex(ic, face_num);
284  for (short_type i = 0; i < nb; ++i, ++pit)
285  if (std::find(pt.begin(), pt.end(), *pit) == pt.end()) return false;
286  return true;
287  }
288 
289  template<class ITER>
290  size_type mesh_structure::add_convex_noverif(pconvex_structure cs,
291  ITER ipts, size_type is) {
292  mesh_convex_structure s; s.cstruct = cs;
293  size_type nb = cs->nb_points();
294 
295  if (is != size_type(-1)) { sup_convex(is); convex_tab.add_to_index(is,s); }
296  else is = convex_tab.add(s);
297 
298  convex_tab[is].pts.resize(nb);
299  for (size_type i = 0; i < nb; ++i, ++ipts)
300  { convex_tab[is].pts[i] = *ipts; points_tab[*ipts].push_back(is); }
301  return is;
302  }
303 
304  template<class ITER>
306  ITER ipts, bool *present) {
307  if (present) *present = false;
308  for (size_type i = 0; i < points_tab[*ipts].size(); ++i)
309  if (structure_of_convex(points_tab[*ipts][i]) == cs &&
310  is_convex_having_points(points_tab[*ipts][i], cs->nb_points(), ipts))
311  { if (present) *present = true; return points_tab[*ipts][i]; }
312  return add_convex_noverif(cs, ipts);
313  }
314 
315  template<class ITER>
317  if (nb) {
318  for (size_type i = 0; i < points_tab[*ipts].size(); ++i)
319  if (is_convex_having_points(points_tab[*ipts][i], nb, ipts))
320  sup_convex(points_tab[*ipts][i]);
321  }
322  }
323 
324 
325  /* ********************************************************************* */
326  /* */
327  /* Gives the list of edges of a mesh. */
328  /* */
329  /* ********************************************************************* */
330 
331  /* maybe this should be removed from the matlab interface and obsoleted */
332  struct APIDECL edge_list_elt {
333  size_type i, j;
334  size_type cv;
335  inline bool operator < (const edge_list_elt &e) const {
336  if (i < e.i) return true;
337  if (i > e.i) return false;
338  if (j < e.j) return true; else if (j > e.j) return false;
339  if (cv < e.cv) return true;
340  return false;
341  }
342  edge_list_elt(size_type ii, size_type jj, size_type ic = 0) : cv(ic)
343  { i = std::min(ii, jj); j = std::max(ii, jj); }
344  edge_list_elt() {}
345  };
346 
347  typedef dal::dynamic_tree_sorted<edge_list_elt> edge_list;
348 
349  /* do not use that */
350  void APIDECL mesh_edge_list_convex(pconvex_structure cvs,
351  std::vector<size_type> points_of_convex,
352  size_type cv_id, edge_list &el,
353  bool merge_convex);
354  void APIDECL mesh_edge_list(const mesh_structure &m, edge_list &el,
355  bool merge_convex = true);
356 
357 
358 
359 } /* end of namespace bgeot. */
360 
361 
362 #endif /* BGEOT_MESH_STRUCTURE_H__ */
Definition of convex structures.
Mesh structure definition.
bool is_convex_valid(size_type i)
Return true if i is in convex_index()
short_type nb_points_of_convex(size_type ic) const
Return the number of points of convex ic.
const dal::bit_vector & convex_index() const
Return the list of valid convex IDs.
short_type nb_faces_of_convex(size_type ic) const
Return the number of faces of convex ic.
bool is_convex_face_having_points(size_type ic, short_type face_num, short_type nb, ITER pit) const
Return true if the face of the convex contains the given list of points.
bool is_convex_having_points(size_type ic, short_type nb, ITER pit) const
Return true if the convex contains the listed points.
ind_pt_face_ct ind_points_of_face_of_convex(size_type ic, short_type f) const
Return a container of the (global) point number for face f or convex ic.
size_type nb_convex() const
The total number of convexes in the mesh.
void sup_convex_with_points(ITER ipts, short_type nb)
Remove a convex given its points.
const ind_cv_ct & convex_to_point(size_type ip) const
Return a container of the convexes attached to point ip.
size_type first_convex_of_point(size_type ip) const
Convex ID of the first convex attached to the point ip.
size_type add_convex(pconvex_structure cs, ITER ipts, bool *present=0)
Insert a new convex in the mesh_structure.
pconvex_structure structure_of_convex(size_type ic) const
Return the pconvex_structure of the convex ic.
const ind_set & ind_points_of_convex(size_type ic) const
Return a container to the list of points attached to convex ic.
bool is_point_valid(size_type i) const
Return true if the point i is used by at least one convex.
size_type nb_allocated_convex() const
The number of convex indexes from 0 to the index of the last convex.
void sup_convex(size_type ic)
Remove the convex ic.
bool empty(void) const
True if no space is allocated.
Definition: dal_basic.h:228
size_type size(void) const
Number of allocated elements.
Definition: dal_basic.h:224
indexed array reference (given a container X, and a set of indexes I, this class provides a pseudo-co...
Definition: gmm_ref.h:303
a balanced tree stored in a dal::dynamic_array
Basic Geometric Tools.
gmm::uint16_type short_type
used as the common short type integer in the library
Definition: bgeot_config.h:72
std::shared_ptr< const convex_structure > pconvex_structure
Pointer on a convex structure description.
void cuthill_mckee_on_convexes(const bgeot::mesh_structure &ms, std::vector< size_type > &cmk)
Return the cuthill_mc_kee ordering on the convexes.
pconvex_structure simplex_structure(dim_type nc)
Give a pointer on the structures of a simplex of dimension d.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48