File: MeshSubEntities.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 (148 lines) | stat: -rw-r--r-- 4,790 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
// 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 "MeshSubEntities.h"
#include "GModel.h"
#include "discreteVertex.h"
#include "discreteEdge.h"
#include "discreteFace.h"
#include "MElement.h"
#include "MPoint.h"
#include "MLine.h"
#include "MTriangle.h"
#include "MQuadrangle.h"
#include "MEdge.h"
#include "MFace.h"
#include "Context.h"

StringXNumber MeshSubEntitiesOptions_Number[] = {
  {GMSH_FULLRC, "InputDimension", nullptr, 1.},
  {GMSH_FULLRC, "InputPhysicalGroup", nullptr, 1.},
  {GMSH_FULLRC, "OuputDimension", nullptr, 0.},
  {GMSH_FULLRC, "OuputPhysicalGroup", nullptr, 2000.},
};

extern "C" {
GMSH_Plugin *GMSH_RegisterMeshSubEntitiesPlugin()
{
  return new GMSH_MeshSubEntitiesPlugin();
}
}

std::string GMSH_MeshSubEntitiesPlugin::getHelp() const
{
  return "Plugin(MeshSubEntities) creates mesh elements for the "
         "entities of dimension `OutputDimension' (0 for vertices, "
         "1 for edges, 2 for faces) of the `InputPhysicalGroup' of "
         "dimension `InputDimension'. The plugin creates new elements "
         "belonging to `OutputPhysicalGroup'.";
}

int GMSH_MeshSubEntitiesPlugin::getNbOptions() const
{
  return sizeof(MeshSubEntitiesOptions_Number) / sizeof(StringXNumber);
}

StringXNumber *GMSH_MeshSubEntitiesPlugin::getOption(int iopt)
{
  return &MeshSubEntitiesOptions_Number[iopt];
}

PView *GMSH_MeshSubEntitiesPlugin::execute(PView *view)
{
  int inputdim = (int)MeshSubEntitiesOptions_Number[0].def;
  int inputphysical = (int)MeshSubEntitiesOptions_Number[1].def;
  int outputdim = (int)MeshSubEntitiesOptions_Number[2].def;
  int outphysical = (int)MeshSubEntitiesOptions_Number[3].def;

  if(inputdim < 0 || inputdim > 3 || outputdim < 0 || outputdim > 3 ||
     outputdim > inputdim) {
    Msg::Error("Bad dimensions");
    return view;
  }

  GModel *m = GModel::current();
  std::map<int, std::vector<GEntity *> > groups;
  m->getPhysicalGroups(inputdim, groups);
  std::vector<GEntity *> entities = groups[inputphysical];

  if(entities.empty()) {
    Msg::Error("Physical group %d (dimension %d) is empty", inputphysical,
               inputdim);
    return view;
  }

  // get input elements
  std::vector<MElement *> elements;
  for(std::size_t i = 0; i < entities.size(); i++)
    for(std::size_t j = 0; j < entities[i]->getNumMeshElements(); j++)
      elements.push_back(entities[i]->getMeshElement(j));

  if(outputdim == 0) { // create point elements for mesh vertices
    std::set<MVertex *> vertices;
    for(std::size_t i = 0; i < elements.size(); i++) {
      for(std::size_t j = 0; j < elements[i]->getNumVertices(); j++) {
        MVertex *v = elements[i]->getVertex(j);
        vertices.insert(v);
      }
    }
    for(auto it = vertices.begin(); it != vertices.end(); ++it) {
      MVertex *v = *it;
      GVertex *gv = nullptr;
      if(v->onWhat() && v->onWhat()->dim() == 0) {
        gv = (GVertex *)v->onWhat();
      }
      else {
        gv = new discreteVertex(m, m->getMaxElementaryNumber(0) + 1);
        v->setEntity(gv);
        m->add(gv);
      }
      gv->physicals.push_back(outphysical);
      if(gv->points.empty()) gv->points.push_back(new MPoint(v));
    }
    m->pruneMeshVertexAssociations();
  }
  else if(outputdim == 1) { // create line elements for mesh edges
    std::set<MEdge, MEdgeLessThan> edges;
    for(std::size_t i = 0; i < elements.size(); i++) {
      for(int j = 0; j < elements[i]->getNumEdges(); j++) {
        MEdge e = elements[i]->getEdge(j);
        edges.insert(e);
      }
    }
    for(auto it = edges.begin(); it != edges.end(); ++it) {
      const MEdge &e = *it;
      GEdge *ge = nullptr;
      MVertex *v0 = e.getVertex(0), *v1 = e.getVertex(1);
      if(v0->onWhat() && v1->onWhat()) {
        if(v0->onWhat()->dim() == 1 &&
           ((v1->onWhat()->dim() == 1 && v0->onWhat() == v1->onWhat()) ||
            v1->onWhat()->dim() == 0))
          ge = (GEdge *)v0->onWhat();
        else if(v1->onWhat()->dim() == 1 &&
                ((v0->onWhat()->dim() == 1 && v0->onWhat() == v1->onWhat()) ||
                 v0->onWhat()->dim() == 0))
          ge = (GEdge *)v1->onWhat();
      }
      if(!ge) {
        ge = new discreteEdge(m, m->getMaxElementaryNumber(1) + 1, nullptr,
                              nullptr);
        v0->setEntity(ge);
        v1->setEntity(ge);
        m->add(ge);
      }
      ge->physicals.push_back(outphysical);
      if(ge->lines.empty()) ge->lines.push_back(new MLine(v0, v1));
    }
  }
  else {
    Msg::Error("Plugin(MeshSubEntities) not coded yet for output dim %d",
               outputdim);
  }

  CTX::instance()->mesh.changed = ENT_ALL;

  return view;
}