gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
BackgroundMesh.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef BACKGROUND_MESH_H
7 #define BACKGROUND_MESH_H
8 
9 #include <cmath>
10 #include <vector>
11 #include <unordered_map>
12 #include <list>
13 #include <memory>
14 #include "simpleFunction.h"
15 #include "BackgroundMeshTools.h"
16 #include "MLine.h"
17 #include "MTriangle.h"
18 
19 #if defined(HAVE_ANN)
20 #include "ANN/ANN.h"
21 class ANNkd_tree;
22 #endif
23 
24 class GEntity;
25 class GModel;
26 class MElementOctree;
27 class GFace;
28 class GEdge;
29 class MElement;
30 class MVertex;
31 
32 struct crossField2d {
33  double _angle;
34  static void normalizeAngle(double &angle)
35  {
36  if(angle < 0)
37  while(angle < 0) angle += (M_PI * .5);
38  else if(angle >= M_PI * .5)
39  while(angle >= M_PI * .5) angle -= (M_PI * .5);
40  }
42  crossField2d(double a) : _angle(a) {}
44 };
45 
46 class backgroundMesh : public simpleFunction<double> {
48  std::vector<MVertex *> _vertices;
49  std::vector<MElement *> _triangles;
50  std::map<MVertex *, double> _sizes;
51  std::map<MVertex *, MVertex *> _3Dto2D;
52  std::map<MVertex *, MVertex *> _2Dto3D;
53  std::map<MVertex *, double> _distance;
54  std::map<MVertex *, double> _angles;
55  static std::vector<backgroundMesh *> _current;
56  backgroundMesh(GFace *, bool dist = false);
58 #if defined(HAVE_ANN)
59  mutable ANNkd_tree *uv_kdtree;
60  mutable ANNpointArray nodes;
61  ANNidxArray index;
62  ANNdistArray dist;
63  mutable ANNpointArray angle_nodes;
64  mutable ANNkd_tree *angle_kdtree;
65  std::vector<double> _cos, _sin;
66 #endif
67 public:
68  static void set(GFace *);
69  static void setCrossFieldsByDistance(GFace *);
70  static void unset();
71  static backgroundMesh *current();
72  void propagate1dMesh(GFace *);
75  void propagateCrossField(GFace *);
77  void updateSizes(GFace *);
78  double operator()(double u, double v, double w) const; // returns mesh size
79  bool inDomain(double u, double v,
80  double w) const; // returns true if in domain
81  double getAngle(double u, double v, double w) const;
82  double getSmoothness(double u, double v, double w);
83  double getSmoothness(MElement *);
84  void print(const std::string &filename, GFace *gf,
85  const std::map<MVertex *, double> &, int smooth = 0);
86  void print(const std::string &filename, GFace *gf, int choice = 0)
87  {
88  switch(choice) {
89  case 0: print(filename, gf, _sizes); return;
90  case 2: print(filename, gf, _sizes, 1); return;
91  default: print(filename, gf, _angles); return;
92  }
93  }
94  MElement *getMeshElementByCoord(double u, double v, double w,
95  bool strict = true);
96  int getNumMeshElements() const { return _triangles.size(); }
97  std::vector<MVertex *>::iterator begin_vertices()
98  {
99  return _vertices.begin();
100  }
101  std::vector<MVertex *>::iterator end_vertices() { return _vertices.end(); }
102  std::vector<MVertex *>::const_iterator begin_vertices() const
103  {
104  return _vertices.begin();
105  }
106  std::vector<MVertex *>::const_iterator end_vertices() const
107  {
108  return _vertices.end();
109  }
110  std::vector<MElement *>::iterator begin_triangles()
111  {
112  return _triangles.begin();
113  }
114  std::vector<MElement *>::iterator end_triangles() { return _triangles.end(); }
115  std::vector<MElement *>::const_iterator begin_triangles() const
116  {
117  return _triangles.begin();
118  }
119  std::vector<MElement *>::const_iterator end_triangles() const
120  {
121  return _triangles.end();
122  }
123 };
124 
125 /************************************************************/
126 /* Alternative datastructures for storing background meshes */
127 /************************************************************/
128 
130  GEdge *ge = NULL;
131  std::vector<MLine> lines;
132 };
133 
135  GFace *gf = NULL;
136  std::vector<MTriangle> triangles;
137 };
138 
139 /* @brief Store a collection of GEntity background meshes.
140  * Deal with the mesh import (see importEntityMeshes())
141  * New MVertex* instance are created by the import
142  * and are deleted by the destructor.
143  */
145 public:
146  const std::string &name;
148  std::unordered_map<GEdge *, BackgroundMeshGEdge> edgeBackgroundMeshes;
149  std::unordered_map<GFace *, BackgroundMeshGFace> faceBackgroundMeshes;
150  std::vector<MVertex *> mesh_vertices;
151 
152 public:
153  GlobalBackgroundMesh(const std::string &_name) : name(_name), gm(NULL) {}
156  ~GlobalBackgroundMesh(); /* delete the MVertex instances stored in
157  mesh_vertices */
158 
174  int importGModelMeshes(GModel *gm, bool overwriteExisting = true);
175 };
176 
177 /* Global storage for access deep in meshing algorithms without passing
178  * reference everywhere. Use getBackgroundMesh(name) instead of direcly
179  * accessing the global variable. */
180 extern std::vector<std::unique_ptr<GlobalBackgroundMesh> > global_bmeshes;
181 bool backgroudMeshExists(const std::string &name);
182 GlobalBackgroundMesh &getBackgroundMesh(const std::string &name);
183 
184 #endif
backgroundMesh::getMeshElementByCoord
MElement * getMeshElementByCoord(double u, double v, double w, bool strict=true)
Definition: BackgroundMesh.cpp:720
backgroundMesh::_sizes
std::map< MVertex *, double > _sizes
Definition: BackgroundMesh.h:50
MTriangle.h
backgroundMesh::_angles
std::map< MVertex *, double > _angles
Definition: BackgroundMesh.h:54
backgroundMesh::propagateCrossFieldByDistance
void propagateCrossFieldByDistance(GFace *)
Definition: BackgroundMesh.cpp:291
backgroundMesh::print
void print(const std::string &filename, GFace *gf, const std::map< MVertex *, double > &, int smooth=0)
Definition: BackgroundMesh.cpp:672
backgroundMesh::operator()
double operator()(double u, double v, double w) const
Definition: BackgroundMesh.cpp:568
GlobalBackgroundMesh::name
const std::string & name
Definition: BackgroundMesh.h:146
GFace
Definition: GFace.h:33
backgroundMesh::propagate1dMesh
void propagate1dMesh(GFace *)
Definition: BackgroundMesh.cpp:237
backgroundMesh::getSmoothness
double getSmoothness(double u, double v, double w)
Definition: BackgroundMesh.cpp:389
angle
double angle(const SVector3 &a, const SVector3 &b)
Definition: SVector3.h:157
BackgroundMeshGEdge::ge
GEdge * ge
Definition: BackgroundMesh.h:130
BackgroundMeshGEdge::lines
std::vector< MLine > lines
Definition: BackgroundMesh.h:131
MVertex
Definition: MVertex.h:24
backgroundMesh::_2Dto3D
std::map< MVertex *, MVertex * > _2Dto3D
Definition: BackgroundMesh.h:52
backgroundMesh::begin_triangles
std::vector< MElement * >::iterator begin_triangles()
Definition: BackgroundMesh.h:110
GlobalBackgroundMesh::~GlobalBackgroundMesh
~GlobalBackgroundMesh()
Definition: BackgroundMesh.cpp:751
backgroundMesh::_octree
MElementOctree * _octree
Definition: BackgroundMesh.h:47
backgroundMesh::end_triangles
std::vector< MElement * >::iterator end_triangles()
Definition: BackgroundMesh.h:114
BackgroundMeshGFace::gf
GFace * gf
Definition: BackgroundMesh.h:135
backgroundMesh::propagateCrossField
void propagateCrossField(GFace *, simpleFunction< double > *)
Definition: BackgroundMesh.cpp:448
GlobalBackgroundMesh::edgeBackgroundMeshes
std::unordered_map< GEdge *, BackgroundMeshGEdge > edgeBackgroundMeshes
Definition: BackgroundMesh.h:148
backgroundMesh::_distance
std::map< MVertex *, double > _distance
Definition: BackgroundMesh.h:53
backgroundMesh::inDomain
bool inDomain(double u, double v, double w) const
Definition: BackgroundMesh.cpp:562
MLine.h
GEntity
Definition: GEntity.h:31
GlobalBackgroundMesh::GlobalBackgroundMesh
GlobalBackgroundMesh(GlobalBackgroundMesh const &)=delete
backgroundMesh::setCrossFieldsByDistance
static void setCrossFieldsByDistance(GFace *)
Definition: BackgroundMesh.cpp:52
backgroundMesh::begin_vertices
std::vector< MVertex * >::iterator begin_vertices()
Definition: BackgroundMesh.h:97
backgroundMesh::backgroundMesh
backgroundMesh(GFace *, bool dist=false)
Definition: BackgroundMesh.cpp:75
backgroundMesh::begin_vertices
std::vector< MVertex * >::const_iterator begin_vertices() const
Definition: BackgroundMesh.h:102
crossField2d::crossField2d
crossField2d(MVertex *, GEdge *)
Definition: BackgroundMesh.cpp:276
backgroundMesh::propagateCrossFieldHJ
void propagateCrossFieldHJ(GFace *)
Definition: BackgroundMesh.cpp:442
simpleFunction
Definition: GModel.h:30
backgroundMesh::print
void print(const std::string &filename, GFace *gf, int choice=0)
Definition: BackgroundMesh.h:86
crossField2d::crossField2d
crossField2d(double a)
Definition: BackgroundMesh.h:42
GModel
Definition: GModel.h:44
MElementOctree
Definition: MElementOctree.h:15
backgroudMeshExists
bool backgroudMeshExists(const std::string &name)
Definition: BackgroundMesh.cpp:733
global_bmeshes
std::vector< std::unique_ptr< GlobalBackgroundMesh > > global_bmeshes
Definition: BackgroundMesh.cpp:731
GlobalBackgroundMesh
Definition: BackgroundMesh.h:144
MElement
Definition: MElement.h:30
crossField2d::normalizeAngle
static void normalizeAngle(double &angle)
Definition: BackgroundMesh.h:34
backgroundMesh::begin_triangles
std::vector< MElement * >::const_iterator begin_triangles() const
Definition: BackgroundMesh.h:115
backgroundMesh::end_vertices
std::vector< MVertex * >::iterator end_vertices()
Definition: BackgroundMesh.h:101
BackgroundMeshGFace
Definition: BackgroundMesh.h:134
GlobalBackgroundMesh::gm
GModel * gm
Definition: BackgroundMesh.h:147
BackgroundMeshGEdge
Definition: BackgroundMesh.h:129
GlobalBackgroundMesh::importGModelMeshes
int importGModelMeshes(GModel *gm, bool overwriteExisting=true)
Fill the entityMesh map by copying the meshes in the GModel. New MVertex, MLine and MTriangle instanc...
Definition: BackgroundMesh.cpp:764
backgroundMesh::end_triangles
std::vector< MElement * >::const_iterator end_triangles() const
Definition: BackgroundMesh.h:119
simpleFunction.h
backgroundMesh::unset
static void unset()
Definition: BackgroundMesh.cpp:60
backgroundMesh::_vertices
std::vector< MVertex * > _vertices
Definition: BackgroundMesh.h:48
backgroundMesh::getNumMeshElements
int getNumMeshElements() const
Definition: BackgroundMesh.h:96
backgroundMesh::~backgroundMesh
~backgroundMesh()
Definition: BackgroundMesh.cpp:154
crossField2d::_angle
double _angle
Definition: BackgroundMesh.h:33
backgroundMesh::end_vertices
std::vector< MVertex * >::const_iterator end_vertices() const
Definition: BackgroundMesh.h:106
backgroundMesh::updateSizes
void updateSizes(GFace *)
Definition: BackgroundMesh.cpp:505
GEdge
Definition: GEdge.h:26
getBackgroundMesh
GlobalBackgroundMesh & getBackgroundMesh(const std::string &name)
Definition: BackgroundMesh.cpp:741
BackgroundMeshGFace::triangles
std::vector< MTriangle > triangles
Definition: BackgroundMesh.h:136
backgroundMesh::current
static backgroundMesh * current()
Definition: BackgroundMesh.cpp:68
GlobalBackgroundMesh::operator=
GlobalBackgroundMesh & operator=(GlobalBackgroundMesh const &)=delete
BackgroundMeshTools.h
backgroundMesh::_3Dto2D
std::map< MVertex *, MVertex * > _3Dto2D
Definition: BackgroundMesh.h:51
crossField2d::operator+=
crossField2d & operator+=(const crossField2d &)
backgroundMesh::getAngle
double getAngle(double u, double v, double w) const
Definition: BackgroundMesh.cpp:603
GlobalBackgroundMesh::faceBackgroundMeshes
std::unordered_map< GFace *, BackgroundMeshGFace > faceBackgroundMeshes
Definition: BackgroundMesh.h:149
GlobalBackgroundMesh::GlobalBackgroundMesh
GlobalBackgroundMesh(const std::string &_name)
Definition: BackgroundMesh.h:153
GlobalBackgroundMesh::mesh_vertices
std::vector< MVertex * > mesh_vertices
Definition: BackgroundMesh.h:150
backgroundMesh::_current
static std::vector< backgroundMesh * > _current
Definition: BackgroundMesh.h:55
backgroundMesh
Definition: BackgroundMesh.h:46
crossField2d
Definition: BackgroundMesh.h:32
backgroundMesh::_triangles
std::vector< MElement * > _triangles
Definition: BackgroundMesh.h:49
backgroundMesh::set
static void set(GFace *)
Definition: BackgroundMesh.cpp:40