gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
Bubbles.cpp
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 #include <stdlib.h>
7 #include "GmshGlobal.h"
8 #include "GModel.h"
9 #include "MTriangle.h"
10 #include "Numeric.h"
11 #include "Bubbles.h"
12 #include "OS.h"
13 
15  {GMSH_FULLRC, "ShrinkFactor", nullptr, 0.},
16 };
17 
19  {GMSH_FULLRC, "OutputFile", nullptr, "bubbles.geo"}};
20 
21 extern "C" {
23 }
24 
25 std::string GMSH_BubblesPlugin::getHelp() const
26 {
27  return "Plugin(Bubbles) constructs a geometry consisting of "
28  "`bubbles' inscribed in the Voronoi of an input triangulation. "
29  "`ShrinkFactor' allows to change the size of the bubbles. "
30  "The plugin expects a triangulation in the `z = 0' plane to exist "
31  "in the current model.\n\n"
32  "Plugin(Bubbles) creates one `.geo' file.";
33 }
34 
36 {
37  return sizeof(BubblesOptions_Number) / sizeof(StringXNumber);
38 }
39 
41 {
42  return &BubblesOptions_Number[iopt];
43 }
44 
46 {
47  return sizeof(BubblesOptions_String) / sizeof(StringXString);
48 }
49 
51 {
52  return &BubblesOptions_String[iopt];
53 }
54 
55 static double myangle(double c[3], double p[3])
56 {
57  double v[3] = {1, 0, 0};
58  double n[3] = {0, 0, 1};
59  if(fabs(c[0] - p[0]) < 1e-12 && fabs(c[1] - p[1]) < 1e-12 &&
60  fabs(c[2] - p[2]) < 1e-12)
61  return 0.;
62  return angle_plan(c, v, p, n);
63 }
64 
65 class compareAngle {
66 private:
68 
69 public:
70  compareAngle(const SPoint3 &vv) : v(vv) {}
71  bool operator()(const SPoint3 &b1, const SPoint3 &b2)
72  {
73  double p1[3] = {b1.x(), b1.y(), b1.z()};
74  double p2[3] = {b2.x(), b2.y(), b2.z()};
75  double c[3] = {v.x(), v.y(), v.z()};
76 
77  return myangle(c, p1) < myangle(c, p2);
78  }
79 };
80 
82 {
83  double shrink = (double)BubblesOptions_Number[0].def;
84  std::string fileName = BubblesOptions_String[0].def;
85 
86  FILE *fp = Fopen(fileName.c_str(), "w");
87  if(!fp) {
88  Msg::Error("Could not open output file '%s'", fileName.c_str());
89  return v;
90  }
91 
92  GModel *m = GModel::current();
93 
94  int p = m->getMaxElementaryNumber(0) + 1;
95  int l = m->getMaxElementaryNumber(1) + 1;
96  int s = m->getMaxElementaryNumber(2) + 1;
97  int ll = s, ps = 1;
98 
99  SBoundingBox3d bbox = m->bounds();
100  double lc = norm(SVector3(bbox.max(), bbox.min())) / 100;
101  fprintf(fp, "lc = %g;\n", lc);
102 
103  for(auto vit = m->firstVertex(); vit != m->lastVertex(); vit++)
104  (*vit)->writeGEO(fp, "lc");
105 
106  for(auto eit = m->firstEdge(); eit != m->lastEdge(); eit++)
107  (*eit)->writeGEO(fp);
108 
109  for(auto fit = m->firstFace(); fit != m->lastFace(); fit++) {
110  (*fit)->writeGEO(fp);
111  fprintf(fp, "Delete { Surface {%d}; }\n", (*fit)->tag());
112 
113  int sbeg = s;
114  int llbeg = ll;
115 
116  // compute vertex-to-triangle_barycenter map
117  std::map<MVertex *, std::vector<SPoint3> > v2t;
118  for(std::size_t i = 0; i < (*fit)->triangles.size(); i++)
119  for(int j = 0; j < 3; j++)
120  v2t[(*fit)->triangles[i]->getVertex(j)].push_back(
121  (*fit)->triangles[i]->barycenter());
122 
123  // add boundary vertices in map to get cells "closer" to the boundary
124  for(auto it = v2t.begin(); it != v2t.end(); it++) {
125  MVertex *v = it->first;
126  if(v->onWhat() && v->onWhat()->dim() < 2)
127  it->second.push_back(
128  SPoint3(it->first->x(), it->first->y(), it->first->z()));
129  }
130 
131  for(auto it = v2t.begin(); it != v2t.end(); it++) {
132  if(it->second.size() > 2) {
133  // get barycenter of cell boundary points and order them
134  SPoint3 bc;
135  for(std::size_t i = 0; i < it->second.size(); i++) bc += it->second[i];
136  bc *= 1. / (double)it->second.size();
137  compareAngle comp(bc);
138  std::sort(it->second.begin(), it->second.end(), comp);
139  // shrink cells
140  if(shrink) {
141  for(std::size_t i = 0; i < it->second.size(); i++) {
142  double dir[3] = {it->second[i].x() - bc.x(),
143  it->second[i].y() - bc.y(),
144  it->second[i].z() - bc.z()};
145  it->second[i][0] -= shrink * dir[0];
146  it->second[i][1] -= shrink * dir[1];
147  it->second[i][2] -= shrink * dir[2];
148  }
149  }
150  // create b-spline bounded surface for each cell
151  int nump = it->second.size();
152  for(int i = 0; i < nump; i++) {
153  SPoint3 &b(it->second[i]);
154  fprintf(fp, "Point(%d) = {%.16g, %.16g, %.16g, lc};\n", p++, b.x(),
155  b.y(), b.z());
156  }
157  fprintf(fp, "BSpline(%d) = {", l++);
158  for(int i = nump - 1; i >= 0; i--) fprintf(fp, "%d,", p - i - 1);
159  fprintf(fp, "%d};\n", p - nump);
160  fprintf(fp, "Curve Loop(%d) = {%d};\n", ll++, l - 1);
161  fprintf(fp, "Plane Surface(%d) = {%d};\n", s++, ll - 1);
162  }
163  }
164  fprintf(fp, "Physical Surface(%d) = {%d:%d};\n", ps++, sbeg, s - 1);
165 
166  fprintf(fp, "Plane Surface(%d) = {%d, %d:%d};\n", s++, (*fit)->tag(), llbeg,
167  ll - 1);
168  fprintf(fp, "Physical Surface(%d) = {%d};\n", ps++, s - 1);
169  }
170 
171  fclose(fp);
172 
173  return v;
174 }
GMSH_BubblesPlugin::getHelp
std::string getHelp() const
Definition: Bubbles.cpp:25
StringXString
Definition: Options.h:910
MTriangle.h
GModel::firstEdge
eiter firstEdge()
Definition: GModel.h:356
PView
Definition: PView.h:27
GMSH_RegisterBubblesPlugin
GMSH_Plugin * GMSH_RegisterBubblesPlugin()
Definition: Bubbles.cpp:22
GModel::getMaxElementaryNumber
int getMaxElementaryNumber(int dim)
Definition: GModel.cpp:817
GMSH_Plugin
Definition: Plugin.h:26
OS.h
c
static double c(int i, int j, fullMatrix< double > &CA, const std::vector< SPoint3 > &P, const std::vector< SPoint3 > &Q)
Definition: discreteFrechetDistance.cpp:15
MVertex
Definition: MVertex.h:24
GMSH_BubblesPlugin::execute
PView * execute(PView *)
Definition: Bubbles.cpp:81
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
SPoint3
Definition: SPoint3.h:14
StringXNumber
Definition: Options.h:918
BubblesOptions_String
StringXString BubblesOptions_String[]
Definition: Bubbles.cpp:18
SBoundingBox3d::min
SPoint3 min() const
Definition: SBoundingBox3d.h:90
SVector3
Definition: SVector3.h:16
GMSH_BubblesPlugin::getNbOptionsStr
int getNbOptionsStr() const
Definition: Bubbles.cpp:45
MVertex::onWhat
GEntity * onWhat() const
Definition: MVertex.h:82
GModel::firstVertex
viter firstVertex()
Definition: GModel.h:357
compareAngle::v
SPoint3 v
Definition: Bubbles.cpp:67
BubblesOptions_Number
StringXNumber BubblesOptions_Number[]
Definition: Bubbles.cpp:14
Bubbles.h
Fopen
FILE * Fopen(const char *f, const char *mode)
Definition: OS.cpp:273
GMSH_BubblesPlugin::getNbOptions
int getNbOptions() const
Definition: Bubbles.cpp:35
SPoint3::x
double x(void) const
Definition: SPoint3.h:125
GModel::lastFace
fiter lastFace()
Definition: GModel.h:359
GMSH_BubblesPlugin
Definition: Bubbles.h:15
GModel::bounds
SBoundingBox3d bounds(bool aroundVisible=false)
Definition: GModel.cpp:1043
compareAngle
Definition: Bubbles.cpp:65
GModel::lastVertex
viter lastVertex()
Definition: GModel.h:361
angle_plan
double angle_plan(double v[3], double p1[3], double p2[3], double n[3])
Definition: Numeric.cpp:267
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
norm
void norm(const double *vec, double *norm)
Definition: gmshLevelset.cpp:202
Numeric.h
GModel
Definition: GModel.h:44
GModel::firstFace
fiter firstFace()
Definition: GModel.h:355
SPoint3::y
double y(void) const
Definition: SPoint3.h:127
myangle
static double myangle(double c[3], double p[3])
Definition: Bubbles.cpp:55
GMSH_BubblesPlugin::getOption
StringXNumber * getOption(int iopt)
Definition: Bubbles.cpp:40
GMSH_BubblesPlugin::getOptionStr
StringXString * getOptionStr(int iopt)
Definition: Bubbles.cpp:50
StringXString::def
std::string def
Definition: Options.h:914
GmshGlobal.h
b1
const double b1
Definition: GaussQuadratureHex.cpp:14
compareAngle::operator()
bool operator()(const SPoint3 &b1, const SPoint3 &b2)
Definition: Bubbles.cpp:71
SPoint3::z
double z(void) const
Definition: SPoint3.h:129
GModel::lastEdge
eiter lastEdge()
Definition: GModel.h:360
GModel.h
compareAngle::compareAngle
compareAngle(const SPoint3 &vv)
Definition: Bubbles.cpp:70
SBoundingBox3d::max
SPoint3 max() const
Definition: SBoundingBox3d.h:91
GEntity::dim
virtual int dim() const
Definition: GEntity.h:196
SBoundingBox3d
Definition: SBoundingBox3d.h:21
GModel::current
static GModel * current(int index=-1)
Definition: GModel.cpp:136