gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
discreteEdge.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 <vector>
7 #include "GmshConfig.h"
8 #include "GmshMessage.h"
9 #include "discreteEdge.h"
10 #include "MLine.h"
11 #include "MEdge.h"
12 #include "GModelIO_GEO.h"
13 #include "Geo.h"
14 
15 #if defined(HAVE_MESH)
16 #include "meshGEdge.h"
17 #include "Context.h"
18 #endif
19 
20 discreteEdge::discreteEdge(GModel *model, int num, GVertex *_v0, GVertex *_v1)
21  : GEdge(model, num, _v0, _v1)
22 {
23  bool ok;
24  Curve *c = CreateCurve(num, MSH_SEGM_DISCRETE, 0, nullptr, nullptr, -1, -1,
25  0., 1., ok);
28 }
29 
30 discreteEdge::discreteEdge(GModel *model, int num) : GEdge(model, num)
31 {
32  bool ok;
33  Curve *c = CreateCurve(num, MSH_SEGM_DISCRETE, 0, nullptr, nullptr, -1, -1,
34  0., 1., ok);
37 }
38 
40 {
41  // used for temporary discrete edges, that should not lead to the creation of
42  // the corresponding entity in GEO internals
43 }
44 
45 bool discreteEdge::_getLocalParameter(const double &t, int &iLine,
46  double &tLoc) const
47 {
48  for(iLine = 0; iLine < (int)_discretization.size() - 1; iLine++) {
49  double tmin = _pars[iLine];
50  double tmax = _pars[iLine + 1];
51  if(t >= tmin && t <= tmax) {
52  tLoc = (t - tmin) / (tmax - tmin);
53  return true;
54  }
55  }
56  return false;
57 }
58 
59 GPoint discreteEdge::point(double par) const
60 {
61  double tLoc;
62  int iEdge;
63 
64  if(!_getLocalParameter(par, iEdge, tLoc)) return GPoint();
65 
66  SPoint3 vB = _discretization[iEdge];
67  SPoint3 vE = _discretization[iEdge + 1];
68 
69  // linear Lagrange mesh
70  SPoint3 v = vB + (vE - vB) * tLoc;
71  return GPoint(v.x(), v.y(), v.z(), this, par);
72 }
73 
75 {
76  double tLoc;
77  int iEdge;
78 
79  if(!_getLocalParameter(par, iEdge, tLoc)) return SVector3();
80 
81  SPoint3 vB = _discretization[iEdge];
82  SPoint3 vE = _discretization[iEdge + 1];
83 
84  double dx, dy, dz;
85  dx = (vE.x() - vB.x()); // / dt;
86  dy = (vE.y() - vB.y()); // / dt;
87  dz = (vE.z() - vB.z()); // / dt;
88 
89  SVector3 der(dx, dy, dz);
90  return der;
91 }
92 
93 SPoint2 discreteEdge::reparamOnFace(const GFace *face, double epar,
94  int dir) const
95 {
96  GPoint p = point(epar);
97  double guess[2];
98  GPoint ps = face->closestPoint(SPoint3(p.x(), p.y(), p.z()), guess);
99  return SPoint2(ps.u(), ps.v());
100 }
101 
102 double discreteEdge::curvature(double par) const
103 {
104  double tLoc;
105  int iEdge;
106  if(_discretization.size() <= 3)
107  return 0.0; // no clue on how to compute curvature with so few data...
108 
109  if(!_getLocalParameter(par, iEdge, tLoc)) return 0.0;
110 
111  // Take 3 points x y z : radius of curvature is equal to |x-y| |x-z| |z-y| /
112  // area (x,y,z)
113 
114  int iEdgePlus = iEdge + 1, iEdgeMinus = iEdge - 1;
115 
116  if(iEdge == 0) {
117  iEdge++;
118  iEdgePlus++;
119  iEdgeMinus++;
120  if(periodic(0)) { iEdgeMinus = _discretization.size() - 2; }
121  }
122  else if(iEdge == (int)(_discretization.size() - 2)) {
123  iEdge--;
124  iEdgePlus--;
125  iEdgeMinus--;
126  if(periodic(0)) { iEdgePlus = 0; }
127  }
128 
129  SPoint3 a =
130  (_discretization[iEdgeMinus] + _discretization[iEdgeMinus + 1]) * .5;
131  SPoint3 b = (_discretization[iEdge] + _discretization[iEdge + 1]) * .5;
132  SPoint3 c =
133  (_discretization[iEdgePlus] + _discretization[iEdgePlus + 1]) * .5;
134 
135  double A = b.distance(c);
136  double B = c.distance(a);
137  double C = a.distance(b);
138 
139  // radius of the circumcircle ...
140  // Heron's formula for the area of a triangle
141  double R =
142  A * B * C / sqrt((A + B + C) * (-A + B + C) * (A - B + C) * (A + B - C));
143 
144  return R = 0.0 ? 1.E22 : 1. / R;
145 }
146 
148 {
149  return Range<double>(0, (double)(_discretization.size() - 1));
150 }
151 
153 {
154  if(lines.empty()) return 0;
155 
156  if(!_discretization.empty()) return 0;
157 
158  bool sorted = true;
159 
160  std::vector<MVertex *> vertices;
161  vertices.push_back(lines[0]->getVertex(0));
162  for(std::size_t i = 0; i < lines.size(); i++) {
163  if(lines[i]->getVertex(0) == vertices.back()) {
164  vertices.push_back(lines[i]->getVertex(1));
165  }
166  else {
167  sorted = false;
168  break;
169  }
170  }
171 
172  if(!sorted) {
173  Msg::Debug("Sorting nodes in discrete curve %d", tag());
174  std::vector<MEdge> allEdges;
175  for(std::size_t i = 0; i < lines.size(); i++) {
176  allEdges.push_back(MEdge(lines[i]->getVertex(0), lines[i]->getVertex(1)));
177  }
178  std::vector<std::vector<MVertex *> > vs;
179  SortEdgeConsecutive(allEdges, vs);
180  if(vs.size() == 1) { vertices = vs[0]; }
181  else {
182  Msg::Error("Discrete curve %d has wrong topology", tag());
183  return 1;
184  }
185  }
186 
187  GVertex *g0 = dynamic_cast<GVertex *>(vertices.front()->onWhat());
188  GVertex *g1 = dynamic_cast<GVertex *>(vertices.back()->onWhat());
189 
190  if(g0) { setBeginVertex(g0); }
191  else if(g1 && g1->xyz().distance(vertices.front()->point()) < 1e-14) {
192  setBeginVertex(g1);
193  }
194 
195  if(g1) { setEndVertex(g1); }
196  else if(g0 && g0->xyz().distance(vertices.back()->point()) < 1e-14) {
197  setEndVertex(g0);
198  }
199 
200  if(!getBeginVertex() || !getEndVertex()) {
201  Msg::Warning("Discrete curve %d has no begin or end point", tag());
202  return 1;
203  }
204 
205  for(std::size_t i = 0; i < vertices.size(); i++) {
206  _discretization.push_back(
207  SPoint3(vertices[i]->x(), vertices[i]->y(), vertices[i]->z()));
208  _pars.push_back((double)i);
209  }
210 
211  return 0;
212 }
213 
214 void discreteEdge::mesh(bool verbose)
215 {
216 #if defined(HAVE_MESH)
217  if(_discretization.empty()) return;
218  meshGEdge mesher;
219  mesher(this);
220 #endif
221 }
222 
223 bool discreteEdge::writeParametrization(FILE *fp, bool binary)
224 {
225  std::size_t N = _discretization.size();
226  if(N != _pars.size()) {
227  Msg::Error("Wrong number of parameters in STL mesh of curve %d", tag());
228  return false;
229  }
230  if(binary) {
231  fwrite(&N, sizeof(std::size_t), 1, fp);
232  std::vector<double> d(4 * N);
233  for(std::size_t i = 0; i < N; i++) {
234  d[4 * i + 0] = _discretization[i].x();
235  d[4 * i + 1] = _discretization[i].y();
236  d[4 * i + 2] = _discretization[i].z();
237  d[4 * i + 3] = _pars[i];
238  }
239  fwrite(&d[0], sizeof(double), 4 * N, fp);
240  }
241  else {
242  fprintf(fp, "%lu\n", N);
243  for(std::size_t i = 0; i < N; i++) {
244  fprintf(fp, "%.16g %.16g %.16g %.16g\n", _discretization[i].x(),
245  _discretization[i].y(), _discretization[i].z(), _pars[i]);
246  }
247  }
248  return true;
249 }
250 
251 bool discreteEdge::readParametrization(FILE *fp, bool binary)
252 {
253  std::size_t N;
254  if(binary) {
255  if(fread(&N, sizeof(std::size_t), 1, fp) != 1) { return false; }
256  }
257  else {
258  if(fscanf(fp, "%lu", &N) != 1) { return false; }
259  }
260  _pars.resize(N);
261  _discretization.resize(N);
262 
263  std::vector<double> d(4 * N);
264  if(binary) {
265  if(fread(&d[0], sizeof(double), 4 * N, fp) != 4 * N) { return false; }
266  }
267  else {
268  for(std::size_t i = 0; i < N; i++) {
269  if(fscanf(fp, "%lf %lf %lf %lf", &d[4 * i + 0], &d[4 * i + 1],
270  &d[4 * i + 2], &d[4 * i + 3]) != 4) {
271  return false;
272  }
273  }
274  }
275 
276  for(std::size_t i = 0; i < N; i++) {
277  _discretization[i] = SPoint3(d[4 * i + 0], d[4 * i + 1], d[4 * i + 2]);
278  _pars[i] = d[4 * i + 3];
279  }
280  return true;
281 }
GEO_Internals::Curves
Tree_T * Curves
Definition: GModelIO_GEO.h:18
MSH_SEGM_DISCRETE
#define MSH_SEGM_DISCRETE
Definition: GeoDefines.h:31
discreteEdge::point
virtual GPoint point(double p) const
Definition: discreteEdge.cpp:59
GEdge::periodic
virtual bool periodic(int dim) const
Definition: GEdge.h:219
Geo.h
discreteEdge::reparamOnFace
virtual SPoint2 reparamOnFace(const GFace *face, double epar, int dir) const
Definition: discreteEdge.cpp:93
discreteEdge::readParametrization
bool readParametrization(FILE *fp, bool binary)
Definition: discreteEdge.cpp:251
MEdge
Definition: MEdge.h:14
GPoint::y
double y() const
Definition: GPoint.h:22
CreateReversedCurve
Curve * CreateReversedCurve(Curve *c)
Definition: Geo.cpp:1147
Curve
Definition: Geo.h:74
discreteEdge::curvature
virtual double curvature(double par) const
Definition: discreteEdge.cpp:102
GFace
Definition: GFace.h:33
GEdge::lines
std::vector< MLine * > lines
Definition: GEdge.h:42
GEntity::model
GModel * model() const
Definition: GEntity.h:277
SPoint2
Definition: SPoint2.h:12
Msg::Debug
static void Debug(const char *fmt,...)
Definition: GmshMessage.cpp:752
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
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
discreteEdge::_pars
std::vector< double > _pars
Definition: discreteEdge.h:14
GEdge::setEndVertex
void setEndVertex(GVertex *gv)
Definition: GEdge.h:62
SPoint3
Definition: SPoint3.h:14
SVector3
Definition: SVector3.h:16
GModelIO_GEO.h
meshGEdge.h
GmshMessage.h
discreteEdge::writeParametrization
bool writeParametrization(FILE *fp, bool binary)
Definition: discreteEdge.cpp:223
MLine.h
GPoint
Definition: GPoint.h:13
GFace::closestPoint
virtual GPoint closestPoint(const SPoint3 &queryPoint, const double initialGuess[2]) const
Definition: GFace.cpp:1323
GPoint::z
double z() const
Definition: GPoint.h:23
SPoint3::x
double x(void) const
Definition: SPoint3.h:125
SortEdgeConsecutive
bool SortEdgeConsecutive(const std::vector< MEdge > &e, std::vector< std::vector< MVertex * > > &vs)
Definition: MEdge.cpp:68
discreteEdge::_discretization
std::vector< SPoint3 > _discretization
Definition: discreteEdge.h:15
discreteEdge::discreteEdge
discreteEdge(GModel *model, int num, GVertex *_v0, GVertex *_v1)
Definition: discreteEdge.cpp:20
Range
Definition: Range.h:10
GPoint::u
double u() const
Definition: GPoint.h:27
meshGEdge
Definition: meshGEdge.h:12
GVertex
Definition: GVertex.h:23
Tree_Add
void * Tree_Add(Tree_T *tree, void *data)
Definition: TreeUtils.cpp:37
GModel
Definition: GModel.h:44
GEdge::getBeginVertex
virtual GVertex * getBeginVertex() const
Definition: GEdge.h:63
discreteEdge::createGeometry
int createGeometry()
Definition: discreteEdge.cpp:152
SPoint3::y
double y(void) const
Definition: SPoint3.h:127
GEntity::tag
int tag() const
Definition: GEntity.h:280
discreteEdge::parBounds
virtual Range< double > parBounds(int) const
Definition: discreteEdge.cpp:147
GEdge::vertices
virtual std::vector< GVertex * > vertices() const
Definition: GEdge.cpp:971
SPoint3::distance
double distance(const SPoint3 &p) const
Definition: SPoint3.h:176
discreteEdge::firstDer
virtual SVector3 firstDer(double par) const
Definition: discreteEdge.cpp:74
MEdge.h
discreteEdge.h
Context.h
GVertex::xyz
virtual SPoint3 xyz() const
Definition: GVertex.h:43
z
const double z
Definition: GaussQuadratureQuad.cpp:56
discreteEdge::mesh
virtual void mesh(bool verbose)
Definition: discreteEdge.cpp:214
GEdge
Definition: GEdge.h:26
GEdge::setBeginVertex
void setBeginVertex(GVertex *gv)
Definition: GEdge.h:61
SPoint3::z
double z(void) const
Definition: SPoint3.h:129
GPoint::v
double v() const
Definition: GPoint.h:28
CreateCurve
Curve * CreateCurve(int Num, int Typ, int Order, List_T *Liste, List_T *Knots, int p1, int p2, double u1, double u2, bool &ok)
Definition: Geo.cpp:445
GEdge::getEndVertex
virtual GVertex * getEndVertex() const
Definition: GEdge.h:64
GModel::getGEOInternals
GEO_Internals * getGEOInternals()
Definition: GModel.h:315
GPoint::x
double x() const
Definition: GPoint.h:21
discreteEdge::_getLocalParameter
bool _getLocalParameter(const double &t, int &iEdge, double &tLoc) const
Definition: discreteEdge.cpp:45