File: Skin.cpp

package info (click to toggle)
gmsh 4.8.4%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 87,812 kB
  • sloc: cpp: 378,014; ansic: 99,669; yacc: 7,216; python: 6,680; java: 3,486; lisp: 659; lex: 621; perl: 571; makefile: 470; sh: 440; xml: 415; javascript: 113; pascal: 35; modula3: 32
file content (330 lines) | stat: -rw-r--r-- 9,687 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/gmsh/gmsh/issues.

#include <set>
#include "Skin.h"
#include "Context.h"
#include "GmshDefines.h"
#include "MTriangle.h"
#include "MQuadrangle.h"
#include "MLine.h"
#include "MFace.h"
#include "MEdge.h"
#include "discreteFace.h"
#include "discreteEdge.h"

StringXNumber SkinOptions_Number[] = {{GMSH_FULLRC, "Visible", nullptr, 1.},
                                      {GMSH_FULLRC, "FromMesh", nullptr, 0.},
                                      {GMSH_FULLRC, "View", nullptr, -1.}};

extern "C" {
GMSH_Plugin *GMSH_RegisterSkinPlugin() { return new GMSH_SkinPlugin(); }
}

std::string GMSH_SkinPlugin::getHelp() const
{
  return "Plugin(Skin) extracts the boundary (skin) of the current "
         "mesh (if `FromMesh' = 1), or from the the view `View' (in which "
         "case it creates a new view). If `View' < 0 and `FromMesh' = 0, "
         "the plugin is run on the current view.\n"
         "If `Visible' is set, the plugin only extracts the skin of visible "
         "entities.";
}

int GMSH_SkinPlugin::getNbOptions() const
{
  return sizeof(SkinOptions_Number) / sizeof(StringXNumber);
}

StringXNumber *GMSH_SkinPlugin::getOption(int iopt)
{
  return &SkinOptions_Number[iopt];
}

class ElmData {
public:
  int numComp;
  std::vector<double> x, y, z;
  std::vector<double> v;
  ElmData(int n) : numComp(n) {}
  SPoint3 barycenter() const
  {
    SPoint3 p(0., 0., 0.);
    int N = x.size();
    for(int i = 0; i < N; i++) {
      p[0] += x[i];
      p[1] += y[i];
      p[2] += z[i];
    }
    p[0] /= (double)N;
    p[1] /= (double)N;
    p[2] /= (double)N;
    return p;
  }
  void addInView(PViewDataList *data) const
  {
    std::vector<double> *vec = nullptr;
    switch(x.size()) {
    case 1:
      if(numComp == 1) {
        data->NbSP++;
        vec = &data->SP;
        break;
      }
      else if(numComp == 3) {
        data->NbVP++;
        vec = &data->VP;
        break;
      }
      else if(numComp == 9) {
        data->NbTP++;
        vec = &data->TP;
        break;
      }
      break;
    case 2:
      if(numComp == 1) {
        data->NbSL++;
        vec = &data->SL;
        break;
      }
      else if(numComp == 3) {
        data->NbVL++;
        vec = &data->VL;
        break;
      }
      else if(numComp == 9) {
        data->NbTL++;
        vec = &data->TL;
        break;
      }
      break;
    case 3:
      if(numComp == 1) {
        data->NbST++;
        vec = &data->ST;
        break;
      }
      else if(numComp == 3) {
        data->NbVT++;
        vec = &data->VT;
        break;
      }
      else if(numComp == 9) {
        data->NbTT++;
        vec = &data->TT;
        break;
      }
      break;
    case 4:
      if(numComp == 1) {
        data->NbSQ++;
        vec = &data->SQ;
        break;
      }
      else if(numComp == 3) {
        data->NbVQ++;
        vec = &data->VQ;
        break;
      }
      else if(numComp == 9) {
        data->NbTQ++;
        vec = &data->TQ;
        break;
      }
      break;
    }
    if(!vec) return;
    for(std::size_t i = 0; i < x.size(); i++) vec->push_back(x[i]);
    for(std::size_t i = 0; i < y.size(); i++) vec->push_back(y[i]);
    for(std::size_t i = 0; i < z.size(); i++) vec->push_back(z[i]);
    for(std::size_t i = 0; i < v.size(); i++) vec->push_back(v[i]);
  }
};

class ElmDataLessThan {
public:
  static double tolerance;
  bool operator()(const ElmData &e1, const ElmData &e2) const
  {
    SPoint3 p1 = e1.barycenter();
    SPoint3 p2 = e2.barycenter();
    if(p1.x() - p2.x() > tolerance) return true;
    if(p1.x() - p2.x() < -tolerance) return false;
    if(p1.y() - p2.y() > tolerance) return true;
    if(p1.y() - p2.y() < -tolerance) return false;
    if(p1.z() - p2.z() > tolerance) return true;
    return false;
  }
};

double ElmDataLessThan::tolerance = 1.e-12;

static int getBoundary(int type, const int (**boundary)[6][4])
{
  static const int tri[6][4] = {{0, 1, -1, -1}, {1, 2, -1, -1}, {2, 0, -1, -1}};
  static const int qua[6][4] = {
    {0, 1, -1, -1}, {1, 2, -1, -1}, {2, 3, -1, -1}, {3, 0, -1, -1}};
  static const int tet[6][4] = {
    {0, 1, 3, -1}, {0, 2, 1, -1}, {0, 3, 2, -1}, {1, 2, 3, -1}};
  static const int hex[6][4] = {{0, 1, 5, 4}, {0, 3, 2, 1}, {0, 4, 7, 3},
                                {1, 2, 6, 5}, {2, 3, 7, 6}, {4, 5, 6, 7}};
  static const int pri[6][4] = {
    {0, 1, 4, 3}, {0, 3, 5, 2}, {1, 2, 5, 4}, {0, 2, 1, -1}, {3, 4, 5, -1}};
  static const int pyr[6][4] = {
    {0, 3, 2, 1}, {0, 1, 4, -1}, {0, 4, 3, -1}, {1, 2, 4, -1}, {2, 3, 4, -1}};
  switch(type) {
  case TYPE_TRI: *boundary = &tri; return 3;
  case TYPE_QUA: *boundary = &qua; return 4;
  case TYPE_TET: *boundary = &tet; return 4;
  case TYPE_HEX: *boundary = &hex; return 6;
  case TYPE_PRI: *boundary = &pri; return 5;
  case TYPE_PYR: *boundary = &pyr; return 5;
  default: return 0;
  }
}

static void getBoundaryFromMesh(GModel *m, int visible)
{
  int dim = m->getDim();
  std::vector<GEntity *> entities;
  m->getEntities(entities);
  std::set<MFace, MFaceLessThan> bndFaces;
  std::set<MEdge, MEdgeLessThan> bndEdges;
  for(std::size_t i = 0; i < entities.size(); i++) {
    GEntity *ge = entities[i];
    if(ge->dim() != dim) continue;
    if(visible && !ge->getVisibility()) continue;
    for(std::size_t j = 0; j < ge->getNumMeshElements(); j++) {
      MElement *e = ge->getMeshElement(j);
      if(dim == 2) {
        for(int i = 0; i < e->getNumEdges(); i++) {
          MEdge f = e->getEdge(i);
          if(bndEdges.find(f) == bndEdges.end())
            bndEdges.insert(f);
          else
            bndEdges.erase(f);
        }
      }
      else if(dim == 3) {
        for(int i = 0; i < e->getNumFaces(); i++) {
          MFace f = e->getFace(i);
          if(bndFaces.find(f) == bndFaces.end())
            bndFaces.insert(f);
          else
            bndFaces.erase(f);
        }
      }
    }
  }

  if(dim == 2) {
    discreteEdge *e =
      new discreteEdge(m, m->getMaxElementaryNumber(1) + 1, nullptr, nullptr);
    m->add(e);
    for(auto it = bndEdges.begin(); it != bndEdges.end(); it++) {
      e->lines.push_back(new MLine(it->getVertex(0), it->getVertex(1)));
    }
  }
  else if(dim == 3) {
    discreteFace *f = new discreteFace(m, m->getMaxElementaryNumber(2) + 1);
    m->add(f);
    for(auto it = bndFaces.begin(); it != bndFaces.end(); it++) {
      if(it->getNumVertices() == 3)
        f->triangles.push_back(
          new MTriangle(it->getVertex(0), it->getVertex(1), it->getVertex(2)));
      else if(it->getNumVertices() == 4)
        f->quadrangles.push_back(
          new MQuadrangle(it->getVertex(0), it->getVertex(1), it->getVertex(2),
                          it->getVertex(3)));
    }
  }
}

PView *GMSH_SkinPlugin::execute(PView *v)
{
  int visible = (int)SkinOptions_Number[0].def;
  int fromMesh = (int)SkinOptions_Number[1].def;
  int iView = (int)SkinOptions_Number[2].def;

  // compute boundary of current mesh
  if(fromMesh) {
    getBoundaryFromMesh(GModel::current(), visible);
    return v;
  }

  // compute boundary of post-processing data set
  PView *v1 = getView(iView, v);
  if(!v1) return v;
  PViewData *data1 = getPossiblyAdaptiveData(v1);

  if(data1->hasMultipleMeshes()) {
    Msg::Error("Skin plugin cannot be applied to multi-mesh views");
    return v;
  }

  Msg::Info("Extracting boundary from View[%d]", v1->getIndex());

  PView *v2 = new PView();
  PViewDataList *data2 = getDataList(v2);

  std::set<ElmData, ElmDataLessThan> skin;
  ElmDataLessThan::tolerance = CTX::instance()->lc * 1.e-12;

  int firstNonEmptyStep = data1->getFirstNonEmptyTimeStep();
  for(int ent = 0; ent < data1->getNumEntities(firstNonEmptyStep); ent++) {
    if(visible && data1->skipEntity(firstNonEmptyStep, ent)) continue;
    for(int ele = 0; ele < data1->getNumElements(firstNonEmptyStep, ent);
        ele++) {
      if(data1->skipElement(firstNonEmptyStep, ent, ele, visible)) continue;
      int numComp = data1->getNumComponents(firstNonEmptyStep, ent, ele);
      int type = data1->getType(firstNonEmptyStep, ent, ele);
      const int(*boundary)[6][4];
      int numBoundary = getBoundary(type, &boundary);
      if(!numBoundary) continue;
      for(int i = 0; i < numBoundary; i++) {
        ElmData e(numComp);
        for(int j = 0; j < 4; j++) {
          int nod = (*boundary)[i][j];
          if(nod < 0) continue;
          double x, y, z;
          data1->getNode(firstNonEmptyStep, ent, ele, nod, x, y, z);
          e.x.push_back(x);
          e.y.push_back(y);
          e.z.push_back(z);
        }
        auto it = skin.find(e);
        if(it == skin.end()) {
          for(int step = 0; step < data1->getNumTimeSteps(); step++) {
            if(data1->hasTimeStep(step)) {
              for(int j = 0; j < 4; j++) {
                int nod = (*boundary)[i][j];
                if(nod < 0) continue;
                double v;
                for(int comp = 0; comp < numComp; comp++) {
                  data1->getValue(step, ent, ele, nod, comp, v);
                  e.v.push_back(v);
                }
              }
            }
          }
          skin.insert(e);
        }
        else
          skin.erase(it);
      }
    }
  }

  for(auto it = skin.begin(); it != skin.end(); it++) it->addInView(data2);

  for(int i = 0; i < data1->getNumTimeSteps(); i++)
    if(data1->hasTimeStep(i)) data2->Time.push_back(data1->getTime(i));
  data2->setName(data1->getName() + "_Skin");
  data2->setFileName(data1->getName() + "_Skin.pos");
  data2->finalize();

  return v2;
}