gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
GModelIO_GEOM.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 "GModel.h"
7 #include "OS.h"
8 #include "MTriangle.h"
9 
10 static bool getMeshVertices(int num, int *indices, std::vector<MVertex *> &vec,
11  std::vector<MVertex *> &vertices)
12 {
13  for(int i = 0; i < num; i++) {
14  if(indices[i] < 0 || indices[i] > (int)(vec.size() - 1)) {
15  Msg::Error("Wrong node index %d", indices[i]);
16  return false;
17  }
18  else
19  vertices.push_back(vec[indices[i]]);
20  }
21  return true;
22 }
23 
24 int GModel::readGEOM(const std::string &name)
25 {
26  // this is a format (from geomview?) that Bruno Levy's Graphite code
27  // can write
28  FILE *fp = Fopen(name.c_str(), "r");
29  if(!fp) {
30  Msg::Error("Unable to open file '%s'", name.c_str());
31  return 0;
32  }
33 
34  int numNodes, numElements, dummy;
35  if(fscanf(fp, "%d %d %d", &numNodes, &numElements, &dummy) != 3) {
36  fclose(fp);
37  return 0;
38  }
39 
40  if(!numNodes || !numElements) {
41  Msg::Warning("No nodes or elements found");
42  fclose(fp);
43  return 0;
44  }
45 
46  Msg::Info("%d nodes, %d elements", numNodes, numElements);
47 
48  std::vector<MVertex *> vertexVector;
49  std::map<int, std::vector<MElement *> > elements[1];
50 
51  vertexVector.resize(numNodes);
52  for(int i = 0; i < numNodes; i++) {
53  double x, y, z;
54  if(fscanf(fp, "%lf %lf %lf", &x, &y, &z) != 3) break;
55  vertexVector[i] = new MVertex(x, y, z);
56  }
57 
58  for(int i = 0; i < numElements; i++) {
59  int N, n[3];
60  if(fscanf(fp, "%d", &N) != 1) break;
61  switch(N) {
62  case 3: {
63  if(fscanf(fp, "%d %d %d", &n[0], &n[1], &n[2]) != 3) break;
64  for(int i = 0; i < 3; i++) n[i]--;
65  std::vector<MVertex *> vertices;
66  if(!getMeshVertices(3, n, vertexVector, vertices)) break;
67  elements[0][1].push_back(new MTriangle(vertices));
68  } break;
69  default: Msg::Error("Unknown element type in .geom reader"); break;
70  }
71  }
72 
73  for(int i = 0; i < (int)(sizeof(elements) / sizeof(elements[0])); i++)
74  _storeElementsInEntities(elements[i]);
76  _storeVerticesInEntities(vertexVector);
77 
78  fclose(fp);
79  return 1;
80 }
MTriangle.h
Msg::Info
static void Info(const char *fmt,...)
Definition: GmshMessage.cpp:587
OS.h
MVertex
Definition: MVertex.h:24
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
GModel::readGEOM
int readGEOM(const std::string &name)
Definition: GModelIO_GEOM.cpp:24
GModel::_storeElementsInEntities
void _storeElementsInEntities(std::map< int, std::vector< MElement * > > &map)
Definition: GModel.cpp:2273
Fopen
FILE * Fopen(const char *f, const char *mode)
Definition: OS.cpp:273
getMeshVertices
static bool getMeshVertices(int num, int *indices, std::vector< MVertex * > &vec, std::vector< MVertex * > &vertices)
Definition: GModelIO_GEOM.cpp:10
GModel::vertices
std::set< GVertex *, GEntityPtrLessThan > vertices
Definition: GModel.h:146
MTriangle
Definition: MTriangle.h:26
z
const double z
Definition: GaussQuadratureQuad.cpp:56
GModel.h
GModel::_storeVerticesInEntities
void _storeVerticesInEntities(std::map< int, MVertex * > &vertices)
Definition: GModel.cpp:2496
GModel::_associateEntityWithMeshVertices
void _associateEntityWithMeshVertices()
Definition: GModel.cpp:2470