gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
GEntity.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 <sstream>
7 #include <algorithm>
8 #include "GModel.h"
9 #include "GEntity.h"
10 #include "MElement.h"
11 #include "VertexArray.h"
12 #include "Context.h"
13 #include "GVertex.h"
14 #include "GEdge.h"
15 #include "GFace.h"
16 #include "GRegion.h"
17 #include "closestVertex.h"
18 
20  : _model(m), _tag(t), _meshMaster(this), _visible(1), _selection(0),
21  _allElementsVisible(1), _obb(nullptr), va_lines(nullptr),
22  va_triangles(nullptr)
23 {
24  _color = CTX::instance()->packColor(0, 0, 255, 0);
25 }
26 
28 {
29  if(va_lines) delete va_lines;
30  va_lines = nullptr;
31  if(va_triangles) delete va_triangles;
32  va_triangles = nullptr;
33 }
34 
36 {
37  if(CTX::instance()->hideUnselected && !CTX::instance()->pickElements &&
38  !getSelection())
39  return false;
40  return _visible;
41 }
42 
44 {
45  int r = CTX::instance()->unpackRed(_color);
46  int g = CTX::instance()->unpackGreen(_color);
47  int b = CTX::instance()->unpackBlue(_color);
48  int a = CTX::instance()->unpackAlpha(_color);
49  if(r == 0 && g == 0 && b == 255 && a == 0) return false;
50  return true;
51 }
52 
53 std::string GEntity::getInfoString(bool additional, bool multiline)
54 {
55  std::ostringstream sstream;
56  sstream << getTypeString() << " " << tag();
57 
58  switch(getNativeType()) {
59  case OpenCascadeModel: sstream << " (OCC)"; break;
60  case AcisModel: sstream << " (ACIS)"; break;
61  case ParasolidModel: sstream << " (Parasolid)"; break;
62  default: break;
63  }
64 
65  {
66  std::string name = model()->getElementaryName(dim(), tag());
67  if(name.size()) sstream << ": " << name;
68  }
69 
70  if(additional) {
71  std::string info = getAdditionalInfoString(multiline);
72  if(info.size()) {
73  if(multiline)
74  sstream << "\n";
75  else
76  sstream << " ";
77  sstream << info;
78  }
79  }
80 
81  if(physicals.size()) {
82  for(std::size_t i = 0; i < physicals.size(); i++) {
83  if(multiline)
84  sstream << "\n";
85  else
86  sstream << ", ";
87  sstream << "Physical ";
88  switch(dim()) {
89  case 0: sstream << "Point"; break;
90  case 1: sstream << "Curve"; break;
91  case 2: sstream << "Surface"; break;
92  case 3: sstream << "Volume"; break;
93  }
94  sstream << " " << std::abs(physicals[i]);
95  std::string name =
96  model()->getPhysicalName(dim(), std::abs(physicals[i]));
97  if(name.size()) sstream << ": " << name;
98  }
99  }
100 
101  if(useColor()) {
102  int r = CTX::instance()->unpackRed(_color);
103  int g = CTX::instance()->unpackGreen(_color);
104  int b = CTX::instance()->unpackBlue(_color);
105  if(multiline)
106  sstream << "\n";
107  else
108  sstream << ", ";
109  sstream << "Color (" << r << ", " << g << ", " << b << ")";
110  }
111 
112  return sstream.str();
113 }
114 
115 // removes a MeshVertex
117 {
118  auto it = std::find(mesh_vertices.begin(), mesh_vertices.end(), v);
119  if(it != mesh_vertices.end()) mesh_vertices.erase(it);
120 }
121 
122 GVertex *GEntity::cast2Vertex() { return dynamic_cast<GVertex *>(this); }
123 GEdge *GEntity::cast2Edge() { return dynamic_cast<GEdge *>(this); }
124 GFace *GEntity::cast2Face() { return dynamic_cast<GFace *>(this); }
125 GRegion *GEntity::cast2Region() { return dynamic_cast<GRegion *>(this); }
126 
127 // sets the entity m from which the mesh will be copied
129 {
130  if(gMaster->dim() != dim()) {
131  Msg::Error("Model entity %d of dimension %d cannot"
132  "be the mesh master of entity %d of dimension %d",
133  gMaster->tag(), gMaster->dim(), tag(), dim());
134  return;
135  }
136  _meshMaster = gMaster;
137 }
138 
139 void GEntity::setMeshMaster(GEntity *gMaster, const std::vector<double> &tfo,
140  bool updateCorrespVert)
141 {
142  if(gMaster->dim() != dim()) {
143  Msg::Error("Model entity %d of dimension %d cannot"
144  "be the mesh master of entity %d of dimension %d",
145  gMaster->tag(), gMaster->dim(), tag(), dim());
146  return;
147  }
148 
149  if(tfo.empty()) {
150  GEntity::setMeshMaster(gMaster);
151  return;
152  }
153 
154  if(tfo.size() != 16) {
155  Msg::Error("Periodicity transformation from entity %d to %d (dim %d) has "
156  "%d components, while 16 are required",
157  gMaster->tag(), tag(), gMaster->dim(), tfo.size());
158  return;
159  }
160 
161  affineTransform = tfo;
162  _meshMaster = gMaster;
163  if(updateCorrespVert) updateCorrespondingVertices();
164 }
165 
166 void GEntity::addVerticesInSet(std::set<MVertex *> &vtcs, bool closure) const
167 {
168  vtcs.insert(mesh_vertices.begin(), mesh_vertices.end());
169 
170  if(closure) {
171  switch(dim()) {
172  case 3: {
173  std::vector<GFace *> clos = faces();
174  auto cIter = clos.begin();
175  for(; cIter != clos.end(); ++cIter)
176  (*cIter)->addVerticesInSet(vtcs, true);
177  break;
178  }
179  case 2: {
180  std::vector<GEdge *> clos = edges();
181  auto cIter = clos.begin();
182  for(; cIter != clos.end(); ++cIter)
183  (*cIter)->addVerticesInSet(vtcs, true);
184  break;
185  }
186  case 1: {
187  std::vector<GVertex *> clos = vertices();
188  auto cIter = clos.begin();
189  for(; cIter != clos.end(); ++cIter)
190  (*cIter)->addVerticesInSet(vtcs, true);
191  break;
192  }
193  }
194  }
195 }
196 
198 {
199  if(_meshMaster != this && affineTransform.size() == 16) {
200  correspondingVertices.clear();
201  closestVertexFinder cvf(_meshMaster, true);
202 
203  if(cvf.getNbVtcs()) {
204  std::vector<double> tfo = affineTransform;
205  std::vector<double> inv;
206  invertAffineTransformation(tfo, inv);
207 
208  std::set<MVertex *> vtcs;
209  this->addVerticesInSet(vtcs, true);
210 
211  auto vIter = vtcs.begin();
212  for(; vIter != vtcs.end(); ++vIter) {
213  MVertex *tv = *vIter;
214  // double tgt[4] = {tv->x(),tv->y(),tv->z(),1};
215  // double xyz[4] = {0,0,0,0};
216 
217  // int idx = 0;
218  // for (int i=0;i<3;i++) for (int j=0;j<4;j++) tgt[i] += inv[idx++] *
219  // ori[j];
220  MVertex *sv = cvf(tv->point(), inv);
221 
222  correspondingVertices[tv] = sv;
223 
224  double src[4] = {sv->x(), sv->y(), sv->z(), 1};
225  double xyz[4] = {0, 0, 0, 0};
226  int idx = 0;
227  for(int i = 0; i < 3; i++) {
228  xyz[i] = 0;
229  for(int j = 0; j < 4; j++) xyz[i] += tfo[idx++] * src[j];
230  }
231 
232  tv->x() = xyz[0];
233  tv->y() = xyz[1];
234  tv->z() = xyz[2];
235  }
236  }
237  }
238 }
239 
241 {
242  if(_meshMaster != this && affineTransform.size() == 16) {
243  auto cvIter = correspondingVertices.begin();
244 
245  for(; cvIter != correspondingVertices.end(); ++cvIter) {
246  MVertex *tv = cvIter->first;
247  MVertex *sv = cvIter->second;
248  double src[4] = {sv->x(), sv->y(), sv->z(), 1};
249  double tgt[3] = {0, 0, 0};
250  int idx = 0;
251  for(int i = 0; i < 3; i++)
252  for(int j = 0; j < 4; j++) tgt[i] += affineTransform[idx++] * src[j];
253  tv->x() = tgt[0];
254  tv->y() = tgt[1];
255  tv->z() = tgt[2];
256  }
257 
258  cvIter = correspondingHighOrderVertices.begin();
259 
260  for(; cvIter != correspondingHighOrderVertices.end(); ++cvIter) {
261  MVertex *tv = cvIter->first;
262  MVertex *sv = cvIter->second;
263 
264  double src[4] = {sv->x(), sv->y(), sv->z(), 1};
265  double tgt[3] = {0, 0, 0};
266 
267  int idx = 0;
268  for(int i = 0; i < 3; i++)
269  for(int j = 0; j < 4; j++) tgt[i] += affineTransform[idx++] * src[j];
270 
271  tv->x() = tgt[0];
272  tv->y() = tgt[1];
273  tv->z() = tgt[2];
274  }
275  }
276 }
GEntity::affineTransform
std::vector< double > affineTransform
Definition: GEntity.h:403
CTX::packColor
unsigned int packColor(int R, int G, int B, int A)
Definition: Context.cpp:128
GEntity::cast2Vertex
GVertex * cast2Vertex()
Definition: GEntity.cpp:122
GEntity::correspondingHighOrderVertices
std::map< MVertex *, MVertex * > correspondingHighOrderVertices
Definition: GEntity.h:409
GFace.h
CTX::unpackBlue
int unpackBlue(unsigned int X)
Definition: Context.cpp:152
GFace
Definition: GFace.h:33
GEntity::model
GModel * model() const
Definition: GEntity.h:277
GEntity.h
MVertex
Definition: MVertex.h:24
GEntity::OpenCascadeModel
@ OpenCascadeModel
Definition: GEntity.h:82
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
GEntity::va_triangles
VertexArray * va_triangles
Definition: GEntity.h:68
MVertex::z
double z() const
Definition: MVertex.h:62
GEntity::physicals
std::vector< int > physicals
Definition: GEntity.h:65
VertexArray.h
GEntity::AcisModel
@ AcisModel
Definition: GEntity.h:83
GEntity::getNativeType
virtual ModelType getNativeType() const
Definition: GEntity.h:268
GEntity::edges
virtual std::vector< GEdge * > const & edges() const
Definition: GEntity.h:211
MVertex::point
SPoint3 point() const
Definition: MVertex.h:67
GEntity::getSelection
virtual char getSelection()
Definition: GEntity.h:314
GEntity
Definition: GEntity.h:31
closestVertexFinder::getNbVtcs
unsigned int getNbVtcs() const
Definition: closestVertex.h:46
GEntity::removeMeshVertex
void removeMeshVertex(MVertex *v)
Definition: GEntity.cpp:116
invertAffineTransformation
bool invertAffineTransformation(const std::vector< double > &tfo, std::vector< double > &newTfo)
Definition: affineTransformation.cpp:98
GEntity::ParasolidModel
@ ParasolidModel
Definition: GEntity.h:84
GEntity::_visible
char _visible
Definition: GEntity.h:43
CTX::instance
static CTX * instance()
Definition: Context.cpp:122
GEntity::cast2Face
GFace * cast2Face()
Definition: GEntity.cpp:124
GEdge.h
GModel::getPhysicalName
std::string getPhysicalName(int dim, int num) const
Definition: GModel.cpp:961
GRegion.h
GEntity::_meshMaster
GEntity * _meshMaster
Definition: GEntity.h:40
GEntity::vertices
virtual std::vector< GVertex * > vertices() const
Definition: GEntity.h:218
GEntity::mesh_vertices
std::vector< MVertex * > mesh_vertices
Definition: GEntity.h:56
GVertex
Definition: GVertex.h:23
GEntity::getTypeString
virtual std::string getTypeString()
Definition: GEntity.h:133
GEntity::useColor
virtual bool useColor()
Definition: GEntity.cpp:43
GModel
Definition: GModel.h:44
GEntity::copyMasterCoordinates
void copyMasterCoordinates()
Definition: GEntity.cpp:240
CTX::unpackGreen
int unpackGreen(unsigned int X)
Definition: Context.cpp:144
GEntity::GEntity
GEntity(GModel *m, int t)
Definition: GEntity.cpp:19
GModel::getElementaryName
std::string getElementaryName(int dim, int tag)
Definition: GModel.cpp:1007
GEntity::tag
int tag() const
Definition: GEntity.h:280
GEntity::cast2Region
GRegion * cast2Region()
Definition: GEntity.cpp:125
GEntity::_color
unsigned int _color
Definition: GEntity.h:49
GEntity::addVerticesInSet
void addVerticesInSet(std::set< MVertex * > &, bool closure) const
Definition: GEntity.cpp:166
GRegion
Definition: GRegion.h:28
CTX::unpackAlpha
int unpackAlpha(unsigned int X)
Definition: Context.cpp:160
Context.h
GEntity::getAdditionalInfoString
virtual std::string getAdditionalInfoString(bool multline=false)
Definition: GEntity.h:332
GVertex.h
MElement.h
GEntity::va_lines
VertexArray * va_lines
Definition: GEntity.h:68
GEdge
Definition: GEdge.h:26
GEntity::setMeshMaster
void setMeshMaster(GEntity *)
Definition: GEntity.cpp:128
closestVertexFinder
Definition: closestVertex.h:26
GModel.h
MVertex::y
double y() const
Definition: MVertex.h:61
closestVertex.h
GEntity::cast2Edge
GEdge * cast2Edge()
Definition: GEntity.cpp:123
GEntity::dim
virtual int dim() const
Definition: GEntity.h:196
GEntity::getVisibility
virtual char getVisibility()
Definition: GEntity.cpp:35
GEntity::updateCorrespondingVertices
void updateCorrespondingVertices()
Definition: GEntity.cpp:197
GEntity::correspondingVertices
std::map< MVertex *, MVertex * > correspondingVertices
Definition: GEntity.h:406
CTX::unpackRed
int unpackRed(unsigned int X)
Definition: Context.cpp:136
GEntity::faces
virtual std::vector< GFace * > faces() const
Definition: GEntity.h:208
GEntity::deleteVertexArrays
void deleteVertexArrays()
Definition: GEntity.cpp:27
GEntity::getInfoString
virtual std::string getInfoString(bool additional=true, bool multiline=false)
Definition: GEntity.cpp:53
MVertex::x
double x() const
Definition: MVertex.h:60