[Gmsh] Generating triangle mesh only from given point cloud and outline edges via the API

Takuya OSHIMA oshima at eng.niigata-u.ac.jp
Mon Aug 8 15:56:53 CEST 2011


Hi,

I want to attain something similar to an old post:
http://www.geuz.org/pipermail/gmsh/2008/003655.html

which is in my case generating a triangle mesh from (and only from)
existing nodes and outline edges via the Gmsh library API. Now that
Gmsh 2.5.0 supports embedded vertices, I managed to write a small
demonstration code as shown below.

I thought using a huge characteristic length would work to suppress
addition of generated vertices, but in reality it didn't. Is there any
way I can suppress the addition of generated vertices? If someone
points me where to start, I wouldn't mind modifying the Gmsh source.

Thanks,
Takuya

Takuya OSHIMA, Ph.D.
Faculty of Engineering, Niigata University
8050 Ikarashi-Ninocho, Nishi-ku, Niigata, 950-2181, JAPAN

// Create square surface mesh with triangular elements

#include "Gmsh.h"
#include "GModel.h"

#include <math.h>
#include <iostream>

int main(int argc, char **argv)
{
  const double lc = MAXFLOAT;
  const int nDivs = 10;
  GmshInitialize(argc, argv);

  GModel *m = new GModel;
  m->setFactory("Gmsh");

  // Add boundary vertices and edges of outline polygon
  std::vector<std::vector<GEdge *> > edges(1);
  GVertex *v, *v0;
  for(int i = 0; i < nDivs; ++i)
    {
    GVertex *vOld;
    if(i == 0)
      {
      vOld = v0 = m->addVertex(0., 0., 0., lc);
      }
    else
      {
      vOld = v;
      }
    v = m->addVertex((i + 1.0) / nDivs, 0., 0., lc);
    edges[0].push_back(m->addLine(vOld, v));
    }
  for(int i = 0; i < nDivs; ++i)
    {
    GVertex *vOld = v;
    v = m->addVertex(1.0, (i + 1.0) / nDivs, 0., lc);
    edges[0].push_back(m->addLine(vOld, v));
    }
  for(int i = 0; i < nDivs; ++i)
    {
    GVertex *vOld = v;
    v = m->addVertex(1.0 - (i + 1.0) / nDivs, 1.0, 0., lc);
    edges[0].push_back(m->addLine(vOld, v));
    }
  for(int i = 0; i < nDivs; ++i)
    {
    GVertex *vOld = v;
    v = ((i == nDivs - 1)
        ? v0: m->addVertex(0., 1.0 - (i + 1.0) / nDivs, 0., lc));
    edges[0].push_back(m->addLine(vOld, v));
    }

  // Create surface
  GFace *f0 = m->addPlanarFace(edges);

  // Add point cloud inside the polygon as embedded vertices
  // -- I want to have only those given vertices inside the outline edges!
  GVertex *v1 = m->addVertex(0.3, 0.3, 0., lc);
  f0->addEmbeddedVertex(v1);
  v1 = m->addVertex(0.8, 0.2, 0., lc);
  f0->addEmbeddedVertex(v1);
  v1 = m->addVertex(0.4, 0.7, 0., lc);
  f0->addEmbeddedVertex(v1);
  v1 = m->addVertex(0.7, 0.6, 0., lc);
  f0->addEmbeddedVertex(v1);

  // Create mesh
  m->mesh(2);
  m->writeMSH("test.msh");

  delete m;

  GmshFinalize();
}