gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
Triangulate.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 <stdlib.h>
8 #include "GmshConfig.h"
9 #include "GModel.h"
10 #include "discreteFace.h"
11 #include "GmshMessage.h"
12 #include "MVertex.h"
13 #include "Triangulate.h"
14 #include "Context.h"
15 
16 #if defined(HAVE_MESH)
18 #endif
19 
21  {GMSH_FULLRC, "View", nullptr, -1.}
22 };
23 
24 extern "C" {
26 {
27  return new GMSH_TriangulatePlugin();
28 }
29 }
30 
32 {
33  return "Plugin(Triangulate) triangulates the points in the "
34  "view `View', assuming that all the points belong "
35  "to a surface that can be projected one-to-one "
36  "onto a plane. \n\n"
37  "If `View' < 0, the plugin is run on the current view.\n\n"
38  "Plugin(Triangulate) creates one new list-based view.";
39 }
40 
42 {
43  return sizeof(TriangulateOptions_Number) / sizeof(StringXNumber);
44 }
45 
47 {
48  return &TriangulateOptions_Number[iopt];
49 }
50 
51 #if defined(HAVE_MESH)
52 
53 namespace {
54  class PointData : public MVertex {
55  public:
56  std::vector<double> v;
57  PointData(double x, double y, double z, int numVal) : MVertex(x, y, z)
58  {
59  v.resize(3 + numVal);
60  v[0] = x;
61  v[1] = y;
62  v[2] = z;
63  }
64  };
65 } // namespace
66 
68 {
69  int iView = (int)TriangulateOptions_Number[0].def;
70 
71  PView *v1 = getView(iView, v);
72  if(!v1) return v;
73  PViewData *data1 = v1->getData();
74 
75  if(data1->hasMultipleMeshes()) {
76  Msg::Error("Triangulate plugin cannot be applied to multi-mesh views");
77  return v1;
78  }
79 
80  // create list of points with associated data
81  std::vector<MVertex *> points;
82  int numSteps = data1->getNumTimeSteps();
83  for(int ent = 0; ent < data1->getNumEntities(0); ent++) {
84  for(int ele = 0; ele < data1->getNumElements(0, ent); ele++) {
85  if(data1->skipElement(0, ent, ele)) continue;
86  if(data1->getNumNodes(0, ent, ele) != 1) continue;
87  int numComp = data1->getNumComponents(0, ent, ele);
88  double x, y, z;
89  data1->getNode(0, ent, ele, 0, x, y, z);
90  PointData *p = new PointData(x, y, z, numComp * numSteps);
91  for(int step = 0; step < numSteps; step++)
92  for(int comp = 0; comp < numComp; comp++)
93  data1->getValue(step, ent, ele, 0, comp,
94  p->v[3 + numComp * step + comp]);
95  points.push_back(p);
96  }
97  }
98 
99  if(points.size() < 3) {
100  Msg::Error("Need at least 3 points to triangulate");
101  for(std::size_t i = 0; i < points.size(); i++) delete points[i];
102  return v1;
103  }
104 
105  // get bounding box
106  SBoundingBox3d bbox;
107  for(std::size_t i = 0; i < points.size(); i++) bbox += points[i]->point();
108  double lc = 10 * norm(SVector3(bbox.max(), bbox.min()));
109 
110  // project points onto plane
111  discreteFace *s =
112  new discreteFace(GModel::current(), GModel::current()->getNumFaces() + 1);
113  s->computeMeanPlane(points);
114  double x, y, z, VX[3], VY[3];
115  s->getMeanPlaneData(VX, VY, x, y, z);
116  for(std::size_t i = 0; i < points.size(); i++) {
117  double vec[3] = {points[i]->x() - x, points[i]->y() - y,
118  points[i]->z() - z};
119  double u = prosca(vec, VX);
120  double v = prosca(vec, VY);
121  points[i]->x() = u;
122  points[i]->y() = v;
123  points[i]->z() = 0.;
124  }
125  delete s;
126 
127  PView *v2;
128  PViewDataList *data2;
129 
130  std::vector<MTriangle *> tris;
131  for(std::size_t i = 0; i < points.size(); i++) {
132  double XX = 1.e-12 * lc * (double)rand() / (double)RAND_MAX;
133  double YY = 1.e-12 * lc * (double)rand() / (double)RAND_MAX;
134  points[i]->x() += XX;
135  points[i]->y() += YY;
136  }
137  delaunayMeshIn2D(points, tris);
138 
139  v2 = new PView();
140  data2 = getDataList(v2);
141  for(std::size_t i = 0; i < tris.size(); i++) {
142  PointData *p[3];
143  p[0] = (PointData *)tris[i]->getVertex(0);
144  p[1] = (PointData *)tris[i]->getVertex(1);
145  p[2] = (PointData *)tris[i]->getVertex(2);
146  int numComp = 0;
147  std::vector<double> *vec = nullptr;
148  if((int)p[0]->v.size() == 3 + 9 * numSteps &&
149  (int)p[1]->v.size() == 3 + 9 * numSteps &&
150  (int)p[2]->v.size() == 3 + 9 * numSteps) {
151  numComp = 9;
152  data2->NbTT++;
153  vec = &data2->TT;
154  }
155  else if((int)p[0]->v.size() == 3 + 3 * numSteps &&
156  (int)p[1]->v.size() == 3 + 3 * numSteps &&
157  (int)p[2]->v.size() == 3 + 3 * numSteps) {
158  numComp = 3;
159  data2->NbVT++;
160  vec = &data2->VT;
161  }
162  else {
163  numComp = 1;
164  data2->NbST++;
165  vec = &data2->ST;
166  }
167  for(int nod = 0; nod < 3; nod++) vec->push_back(p[nod]->v[0]);
168  for(int nod = 0; nod < 3; nod++) vec->push_back(p[nod]->v[1]);
169  for(int nod = 0; nod < 3; nod++) vec->push_back(p[nod]->v[2]);
170  for(int step = 0; step < numSteps; step++)
171  for(int nod = 0; nod < 3; nod++)
172  for(int comp = 0; comp < numComp; comp++)
173  vec->push_back(p[nod]->v[3 + numComp * step + comp]);
174  delete tris[i];
175  }
176 
177  for(std::size_t i = 0; i < points.size(); i++) delete points[i];
178 
179  for(int i = 0; i < data1->getNumTimeSteps(); i++)
180  data2->Time.push_back(data1->getTime(i));
181  data2->setName(data1->getName() + "_Triangulate");
182  data2->setFileName(data1->getName() + "_Triangulate.pos");
183  data2->finalize();
184 
185  return v2;
186 }
187 
188 #else
189 
191 {
192  Msg::Error("Plugin(Triangulate) requires mesh module");
193  return v;
194 }
195 
196 #endif
PView
Definition: PView.h:27
PViewData::skipElement
virtual bool skipElement(int step, int ent, int ele, bool checkVisibility=false, int samplingRate=1)
Definition: PViewData.cpp:90
GMSH_Plugin
Definition: Plugin.h:26
PViewData::getNumTimeSteps
virtual int getNumTimeSteps()=0
PViewDataList::NbTT
int NbTT
Definition: PViewDataList.h:30
PViewDataList
Definition: PViewDataList.h:17
MVertex
Definition: MVertex.h:24
PViewData::getNode
virtual int getNode(int step, int ent, int ele, int nod, double &x, double &y, double &z)
Definition: PViewData.h:141
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
MVertex::z
double z() const
Definition: MVertex.h:62
StringXNumber
Definition: Options.h:918
PViewData::getValue
virtual void getValue(int step, int ent, int ele, int idx, double &val)
Definition: PViewData.h:159
SBoundingBox3d::min
SPoint3 min() const
Definition: SBoundingBox3d.h:90
SVector3
Definition: SVector3.h:16
PViewData::hasMultipleMeshes
virtual bool hasMultipleMeshes()
Definition: PViewData.h:216
GmshMessage.h
PViewData::getNumEntities
virtual int getNumEntities(int step=-1)
Definition: PViewData.h:127
PViewData::setFileName
virtual void setFileName(const std::string &val)
Definition: PViewData.h:75
TriangulateOptions_Number
StringXNumber TriangulateOptions_Number[]
Definition: Triangulate.cpp:20
PViewData::getTime
virtual double getTime(int step)
Definition: PViewData.h:94
MVertex.h
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
norm
void norm(const double *vec, double *norm)
Definition: gmshLevelset.cpp:202
Triangulate.h
PView::getData
PViewData * getData(bool useAdaptiveIfAvailable=false)
Definition: PView.cpp:233
GFace::getMeanPlaneData
void getMeanPlaneData(double VX[3], double VY[3], double &x, double &y, double &z) const
Definition: GFace.cpp:902
meshGFaceDelaunayInsertion.h
PViewData::getNumNodes
virtual int getNumNodes(int step, int ent, int ele)
Definition: PViewData.h:137
prosca
double prosca(double const a[3], double const b[3])
Definition: Numeric.h:112
PViewDataList::finalize
bool finalize(bool computeMinMax=true, const std::string &interpolationScheme="")
Definition: PViewDataList.cpp:81
PViewData::setName
virtual void setName(const std::string &val)
Definition: PViewData.h:71
discreteFace.h
PViewDataList::ST
std::vector< double > ST
Definition: PViewDataList.h:31
PViewDataList::TT
std::vector< double > TT
Definition: PViewDataList.h:31
PViewData
Definition: PViewData.h:29
delaunayMeshIn2D
void delaunayMeshIn2D(std::vector< MVertex * > &v, std::vector< MTriangle * > &result, bool removeBox, std::vector< MEdge > *edgesToRecover, bool hilbertSort)
Definition: meshGFaceDelaunayInsertion.cpp:1834
GMSH_RegisterTriangulatePlugin
GMSH_Plugin * GMSH_RegisterTriangulatePlugin()
Definition: Triangulate.cpp:25
GMSH_TriangulatePlugin::getOption
StringXNumber * getOption(int iopt)
Definition: Triangulate.cpp:46
Context.h
PViewData::getNumComponents
virtual int getNumComponents(int step, int ent, int ele)
Definition: PViewData.h:152
GMSH_TriangulatePlugin::getNbOptions
int getNbOptions() const
Definition: Triangulate.cpp:41
z
const double z
Definition: GaussQuadratureQuad.cpp:56
GMSH_PostPlugin::getView
virtual PView * getView(int index, PView *view)
Definition: Plugin.cpp:81
PViewDataList::Time
std::vector< double > Time
Definition: PViewDataList.h:25
PViewData::getNumElements
virtual int getNumElements(int step=-1, int ent=-1)
Definition: PViewData.h:131
GModel.h
PViewDataList::NbVT
int NbVT
Definition: PViewDataList.h:30
PViewData::getName
virtual std::string getName()
Definition: PViewData.h:70
MVertex::y
double y() const
Definition: MVertex.h:61
GMSH_TriangulatePlugin::getHelp
std::string getHelp() const
Definition: Triangulate.cpp:31
GMSH_TriangulatePlugin
Definition: Triangulate.h:15
PViewDataList::VT
std::vector< double > VT
Definition: PViewDataList.h:31
SBoundingBox3d::max
SPoint3 max() const
Definition: SBoundingBox3d.h:91
SBoundingBox3d
Definition: SBoundingBox3d.h:21
PViewDataList::NbST
int NbST
Definition: PViewDataList.h:30
GModel::current
static GModel * current(int index=-1)
Definition: GModel.cpp:136
MVertex::x
double x() const
Definition: MVertex.h:60
GMSH_PostPlugin::getDataList
virtual PViewDataList * getDataList(PView *view, bool showError=true)
Definition: Plugin.cpp:107
GFace::computeMeanPlane
void computeMeanPlane(const std::vector< MVertex * > &points)
Definition: GFace.cpp:756
discreteFace
Definition: discreteFace.h:18
GMSH_TriangulatePlugin::execute
PView * execute(PView *)
Definition: Triangulate.cpp:190