Search code examples
c++visual-c++mesh

Create a simple mesh using NetGen


I am trying to manually create a mesh using NetGen, but I get an access violation exception stepping into Ng_GenerateVolumeMesh. This is the C++ code that I using:

namespace nglib {
#include <nglib.h>
}

int main() {
    using namespace nglib;

    // NetGen initialization
    Ng_Init();

    // Creating Ng_Mesh object
    Ng_Mesh* mesh = Ng_NewMesh();

    // Adding vertices to the mesh
    double v0[] = { 0, 0, 0 };
    double v1[] = { 1, 0, 0 };
    double v2[] = { 1, 1, 0 };
    double v3[] = { 0, 1, 0 };
    double v00[] = { 0, 0, 1 };
    double v10[] = { 1, 0, 1 };
    double v20[] = { 1, 1, 1 };
    double v30[] = { 0, 1, 1 };
    Ng_AddPoint(mesh, v0);
    Ng_AddPoint(mesh, v1);
    Ng_AddPoint(mesh, v2);
    Ng_AddPoint(mesh, v3);
    Ng_AddPoint(mesh, v00);
    Ng_AddPoint(mesh, v10);
    Ng_AddPoint(mesh, v20);
    Ng_AddPoint(mesh, v30);

    // Creating faces (triangles)
    int trig0[] = { 0, 3, 2};
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig0);
    int trig1[] = { 0, 2, 1 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig1);

    int trig2[] = { 0, 5, 4 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig2);
    int trig3[] = { 0, 1, 5 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig3);

    int trig4[] = { 1, 6, 5 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig4);
    int trig5[] = { 1, 2, 6 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig5);

    int trig6[] = { 2, 7, 6 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig6);
    int trig7[] = { 2, 3, 7 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig7);

    int trig8[] = { 3, 4, 7 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig8);
    int trig9[] = { 3, 0, 4 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig9);

    int trig10[] = { 4, 6, 7 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig10);
    int trig11[] = { 4, 5, 6 };
    Ng_AddSurfaceElement(mesh, NG_TRIG, trig11);

    // Meshing options
    Ng_Meshing_Parameters mp;
    mp.maxh = .1;
    mp.second_order = 1;

    // Mesh generation
    Ng_Result res = Ng_GenerateVolumeMesh(mesh, &mp);

    // Shutdown
    Ng_Exit();

    return 0;
}

I have built the library from sources. https://github.com/NGSolve/ngsolve.git commit date Wed Apr 3 09:16:29 2024.

The exception says (sorry it's in Italian):

Eccezione generata in corrispondenza di 0x00007FFE894DEB45 (nglib.dll) in TestNetGen.exe: 0xC0000005: violazione di accesso durante la lettura del percorso 0x000001803583E410.

Stack trace:

[Frame inline] nglib.dll!netgen::TABLE<int,1>::operator[](int) Riga 252 C++
nglib.dll!netgen::Mesh::CalcSurfacesOfNode() Riga 2250  C++
nglib.dll!netgen::Mesh::Compress() Riga 4172    C++
nglib.dll!netgen::MeshVolume(const netgen::MeshingParameters & mp, netgen::Mesh & mesh3d) Riga 590  C++
nglib.dll!nglib::Ng_GenerateVolumeMesh(void * * mesh, nglib::Ng_Meshing_Parameters * mp) Riga 373   C++
TestNetGen.exe!main() Riga 73   C++

Edit:

I found this question that seems to be the same problem.


Solution

  • My example was correct but the index of the vertices has to start from one, not from zero.

    A complete revised example follows:

    namespace nglib {
    #include <nglib.h>
    }
    
    int main() {
        using namespace nglib;
    
        // NetGen initialization
        Ng_Init();
    
        // Creating Ng_Mesh object
        Ng_Mesh* mesh = Ng_NewMesh();
    
        // Adding vertices to the mesh
        double v0[] = { 0, 0, 0 };
        double v1[] = { 1, 0, 0 };
        double v2[] = { 1, 1, 0 };
        double v3[] = { 0, 1, 0 };
        double v00[] = { 0, 0, 1 };
        double v10[] = { 1, 0, 1 };
        double v20[] = { 1, 1, 1 };
        double v30[] = { 0, 1, 1 };
        Ng_AddPoint(mesh, v0);
        Ng_AddPoint(mesh, v1);
        Ng_AddPoint(mesh, v2);
        Ng_AddPoint(mesh, v3);
        Ng_AddPoint(mesh, v00);
        Ng_AddPoint(mesh, v10);
        Ng_AddPoint(mesh, v20);
        Ng_AddPoint(mesh, v30);
    
        // Creating faces (triangles)
        int trig0[] = { 1, 4, 3 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig0);
        int trig1[] = { 1, 3, 2 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig1);
    
        int trig2[] = { 1, 6, 5 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig2);
        int trig3[] = { 1, 2, 6 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig3);
    
        int trig4[] = { 2, 7, 6 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig4);
        int trig5[] = { 2, 3, 7 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig5);
    
        int trig6[] = { 3, 8, 7 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig6);
        int trig7[] = { 3, 4, 8 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig7);
    
        int trig8[] = { 4, 5, 8 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig8);
        int trig9[] = { 4, 1, 5 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig9);
    
        int trig10[] = { 5, 7, 8 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig10);
        int trig11[] = { 5, 6, 7 };
        Ng_AddSurfaceElement(mesh, NG_TRIG, trig11);
    
        // Set default mesh parameters
        Ng_Meshing_Parameters mp = Ng_Meshing_Parameters();
    
        // Mesh generation
        Ng_Result res = Ng_GenerateVolumeMesh(mesh, &mp);
    
        // Free memory
        Ng_DeleteMesh(mesh);
    
        // Shutdown di NetGen
        Ng_Exit();
    
        return 0;
    }