Menu

Mesh Fineness

Help
Spooky
2009-07-25
2013-05-27
  • Spooky

    Spooky - 2009-07-25

    Hi There

    Ive managed to get nglib working and making meshes but I cant for the life of me get either the fineness value or the Ng_RestrictMeshSizeGlobal method or maxh in params to have any effect at all on my meshes. What is the correct way to get a finer mesh. My basic code is:

    void MakeMesh(Mesh *maxmesh, Mesh *dest, float fineness)
    {
        DbgView("Fineness %f", fineness);
        if ( maxmesh )
        {
            Ng_Init();

            Ng_Mesh *mesh = Ng_NewMesh();
           
            if ( mesh )
            {
                Ng_Meshing_Parameters mp;
                mp.maxh                    = fineness;    //1.0e+6;
                mp.fineness            = fineness;
                mp.secondorder    = 0;
                Ng_STL_Geometry *stl_geom = Ng_STL_NewGeometry();

                Ng_RestrictMeshSizeGlobal(mesh, fineness);

                DPoint3    p[3];
                for ( int i = 0; i < maxmesh->numFaces; i++ )
                {
                    for ( int v = 0; v < 3; v++ )
                    {
                        Point3 p1 = maxmesh->verts[maxmesh->faces[i].v[v]];

                        p[v].x = p1.x;
                        p[v].y = p1.y;
                        p[v].z = p1.z;
                    }

                    Ng_STL_AddTriangle(stl_geom, (double *)&p[0], (double *)&p[1], (double *)&p[2], NULL);
                }

                Ng_Result ng_res = Ng_STL_InitSTLGeometry(stl_geom);
                CatchError(ng_res, "Error Initialising the STL Geometry....Aborting!!");

                if ( ng_res == NG_OK )
                {
                    ng_res = Ng_STL_MakeEdges(stl_geom, mesh, &mp);
                    CatchError(ng_res, "Error in Edge Meshing....Aborting!!");

                    if ( ng_res == NG_OK )
                    {
                        DbgView("Start Surface Meshing....");
                        ng_res = Ng_STL_GenerateSurfaceMesh(stl_geom, mesh, &mp);

                        CatchError(ng_res, "Error in Surface Meshing....Aborting!!");

                        if ( ng_res == NG_OK )
                        {
                            DbgView("Start Volume Meshing....");
                            Ng_RestrictMeshSizeGlobal(mesh, fineness);
                            ng_res = Ng_GenerateVolumeMesh(mesh, &mp);
                            CatchError(ng_res, "Error in Volume Meshing....Aborting!!");

                            if ( ng_res == NG_OK )
                                BuildMesh(mesh, dest);
                        }
                    }

                    Ng_DeleteMesh(mesh);
                    Ng_Exit();
                }
            }
        }
    }

    But no value of 'fineness' produces any difference in the end mesh, can anyone give me a clue as to what Iam doing wrong here.

    Thanks

    Chris

     
    • Nobody/Anonymous

      Hi,

      maxh ... is the maximal element diameter. If your geometry has size about 1 (meter, inches, ...) you may want to set maxh in the range 0.01 to 0.1

      with fineness you can steer the automatic mesh size control 0 .. coarse, 1 .. fine.
      E.g., it controls the number of elements around a cylinder, or the minimal number of line segments along edges

      If you choose 1e6 or 1e3 should have little effect., for each one of them.

      Joachim

       
      • Spooky

        Spooky - 2009-07-26

        Thankyou for the reply will give that a try now, though Ive done a search on all the source code for nglib and the fineness value in Ng_Meshing_Parameters just doesnt get used by anything so not sure how that as any effect.

         
    • Nobody/Anonymous

      sorry for that.

      now it is connected, please get an SVN update of the nglib directory

      Joachim

       
      • Spooky

        Spooky - 2009-07-26

        Thanks again, Ive got the latest version from SVN and I see the changes you've made but again I've tried all kinds of values for both fineness and maxh and on small meshes and big meshes and I just cant get anything to change. If I give the mesher a simple box all I ever get back is the same mesh, no extra faces or points are added. Could you perhaps give me some values to use for a box mesh that is 1x1x1 units so that I would expect to get a mesh back that has say 4 points along each edge.

         
    • Spooky

      Spooky - 2009-07-27

      Iam really struggling with this, Ive looked at the code for the Gui version and Ive moved a lot of stuff to nglib, I set all the params as I have in Gui editor and it still gives me a simple cube as a result, for example I set mparam.segmentsperedge = 2.0 and it has no effect at all but in the Gui version it works. Spent 3 days so far trying to get this to work any help would be great.

       
    • Philippose Rajan

      Hello there,

      A Good Evening to you. I apologise for the delay in replying to your queries.... and the frustration that it has caused you.

      The problem you are facing was a bug in the Nglib STL meshing functions (Ng_STL_MakeEdges and Ng_STL_GenerateSurfaceMesh).

      The meshing parameters class instance "mparam" is a global variable which is constructed with default values during initialization, and used through out later in the meshing kernel of netgen.

      This object was being erroneously redefined locally in these two functions and the actual required value of "maxh" (which you kept modifying) was being passed to the locally defined copy of the class. Hence, the meshing kernel was still using the unmodified global "mparam", in which the "maxh" defaults to 1.0e+10. This was the reason why you saw no change to the meshes generated irrespective of the value of "maxh" you had defined.

      I have made the required changes to the Nglib library, and committed the changes to SVN. If you update your copy of the Nglib sources and recompile Nglib, things will work out as expected.

      Here is a small example to illustrate the use of the Nglib library.... this program reads in an STL file, a required "maxh" value, and the name of the file to export the generated mesh to as command-line arguments. It generates only a surface mesh:

      #include <iostream>
      #include <fstream>
      #include <cstdlib>

      using namespace std;

      namespace nglib {
      #include <nglib.h>
      }

      int main (int argc, char ** argv)
      {
         using namespace nglib;
        
         cout << "Netgen NgLib - Mesh Density / Fineness sample" << endl;
        
         if (argc != 4)
         {
            cerr << "use: ng_meshFineness <src_stl> <maxh> <dest_mesh>" << endl;
            return 1;
         }
        
         // Define pointer to a new Netgen Mesh
         Ng_Mesh *mesh;
        
         // Define pointer to STL Geometry
         Ng_STL_Geometry *stl_geom;
        
         // Result of Netgen Operations
         Ng_Result ng_res;

         // Initialise the Netgen Core library
         Ng_Init();

         // Actually create the mesh structure
         mesh = Ng_NewMesh();
        
         // Read in the STL File
         stl_geom = Ng_STL_LoadGeometry(argv[1]);
         if(!stl_geom)
         {
            cout << "Error reading in STL File: " << argv[1] << endl;
            return 1;
         }
         cout << "Successfully loaded STL File: " << argv[1] << endl;

         // Set the Meshing Parameters to be used
         // For a cube made of 12 simple edges between 8 points,
         // the mesh "fineness" does not make any difference to the mesh
         // generated, since there are not "special" areas where the mesh
         // should become preferentially finer
         // However, limiting the maximum allowed mesh size controls the
         // density of the resulting mesh.
         // Example: For a cube of unit length, a maximum mesh size of
         // 0.5 will result in an additional point to be placed at the
         // center of each edge.
         Ng_Meshing_Parameters mp;
         mp.maxh = atof(argv[2]);
         mp.fineness = 1.0;
         mp.secondorder = 0;
        
         cout << "Initialise the STL Geometry structure...." << endl;
         ng_res = Ng_STL_InitSTLGeometry(stl_geom);
         if(ng_res != NG_OK)
         {
            cout << "Error Initialising the STL Geometry....Aborting!!" << endl;
             return 1;
         }

         cout << "Start Edge Meshing...." << endl;
         ng_res = Ng_STL_MakeEdges(stl_geom, mesh, &mp);
         if(ng_res != NG_OK)
         {
            cout << "Error in Edge Meshing....Aborting!!" << endl;
             return 1;
         }

         cout << "Start Surface Meshing...." << endl;
         ng_res = Ng_STL_GenerateSurfaceMesh(stl_geom, mesh, &mp);
         if(ng_res != NG_OK)
         {
            cout << "Error in Surface Meshing....Aborting!!" << endl;
             return 1;
         }  

         cout << "Surface Meshing successfully completed....!!" << endl;
        
         cout << "Number of Points = " << Ng_GetNP(mesh) << endl;
         cout << "Number of Surface Elements = " << Ng_GetNSE(mesh) << endl;

         cout << "Saving Mesh in VOL Format...." << endl;  
         Ng_SaveMesh(mesh,argv[3]);
        
         return 0;
      }

      And here is an example of a simple Cube of dimensions (1 x 1 x 1) units in STL format.... simply copy the following lines into an empty file and name it with a ".stl" extension.....

      solid
      facet normal 0 0 -1
      outer loop
      vertex 1 0 0
      vertex 0 0 0
      vertex 0 1 0
      endloop
      endfacet
      facet normal -0 -0 -1
      outer loop
      vertex 1 1 0
      vertex 1 0 0
      vertex 0 1 0
      endloop
      endfacet
      facet normal 0 -1 0
      outer loop
      vertex 0 0 0
      vertex 1 0 0
      vertex 1 0 1
      endloop
      endfacet
      facet normal 0 -1 0
      outer loop
      vertex 0 0 1
      vertex 0 0 0
      vertex 1 0 1
      endloop
      endfacet
      facet normal 0 1 0
      outer loop
      vertex 1 1 1
      vertex 1 1 0
      vertex 0 1 0
      endloop
      endfacet
      facet normal 0 1 0
      outer loop
      vertex 0 1 0
      vertex 0 1 1
      vertex 1 1 1
      endloop
      endfacet
      facet normal 1 0 -0
      outer loop
      vertex 1 1 0
      vertex 1 1 1
      vertex 1 0 1
      endloop
      endfacet
      facet normal 1 0 0
      outer loop
      vertex 1 0 1
      vertex 1 0 0
      vertex 1 1 0
      endloop
      endfacet
      facet normal -1 0 0
      outer loop
      vertex 0 0 0
      vertex 0 0 1
      vertex 0 1 1
      endloop
      endfacet
      facet normal -1 0 0
      outer loop
      vertex 0 1 0
      vertex 0 0 0
      vertex 0 1 1
      endloop
      endfacet
      facet normal 0 0 1
      outer loop
      vertex 0 1 1
      vertex 0 0 1
      vertex 1 0 1
      endloop
      endfacet
      facet normal 0 0 1
      outer loop
      vertex 1 1 1
      vertex 0 1 1
      vertex 1 0 1
      endloop
      endfacet
      endsolid

      The geometry contains only the 8 corner points.

      Experiment with different "maxh" command-line argument values.....

      maxh = 1.0 --> gives 20 points and 36 surface elements in the final mesh
      maxh = 0.5 --> gives 43 points and 82 surface elements in the final mesh

      Hope this helps.....!

      Have a great day ahead!

      regards,
      Philippose

       
      • Spooky

        Spooky - 2009-07-28

        Great Iam getting expected results now, Iam wondering if the Mparam in Ng_GenerateVolumeMesh also needs to be removed? Is it expected with this latest version that changing fineness on a simple cube will have no effect on the mesh generated.

        Chris

         
    • Philippose Rajan

      Oops.... the comments in the code explaining the concept  of limiting the maximum mesh size is not correct :-)! Limiting the maximum size to 0.5 limits the maximum length of any surface mesh element edge to 0.5.... so it is not necessarily only one extra point at the mid of each edge of the cube.

      You can further control how your mesh looks, by passing different "maxh" values for the "Ng_STL_MakeEdges" function, and the "Ng_STL_GenerateSurfaceMesh" function....

      Anyway... now that the code is functioning as expected, you can do the actually experimenting :-)!

      Enjoy!

      Philippose

       
      • Spooky

        Spooky - 2009-07-28

        Brilliant many thanks for your efforts, looking forward to trying it out.
        Chris

         
    • Philippose Rajan

      Hello Chris,

      A Good Evening to you :-)! Great to hear that you were able to move ahead and solve the issue you were facing.....!

      1. Yes, it is expected that a change in the "fineness" value in the meshing parameters for this particular geometry will not have any effect of the mesh generated.

         The reason for this is, that the mesh "fineness" in Netgen is basically the "mesh grading" factor. It has to have a value between 0.0 and 1.0, and tells Netgen how quickly (from a spatial point of view) it is allowed to change the size of the mesh locally..... essentially, it controls how quickly Netgen moves from a fine mesh to a coarse mesh or vice-versa.

         A smaller number for "fineness" will cause Netgen  to grade its mesh size only slowly, resulting in meshes with a larger number of finer elements than probably necessary, but with an overall better mesh quality. 

         A larger number for the "fineness" value will cause Netgen to aggressively move from fine areas of the mesh to coarse areas of the mesh with drastic changes in mesh size. This will result in a smaller number of elements, but will compromise on the quality of the elements.

         Now, the decision whether an area of the geometry should be finer or coarser is based on a variety of factors such as surface curvature, number of required elements per edge, number of required elements per face, edge angles, surface closeness, etc...etc...etc.....

         The cube which we have used as the example has an "ideal" geometry, and there are no "preferential" areas within the geometry where the mesh needs to be finer or coarser, and hence the fineness value will have no effect.... to see the effect of this factor, use a more complex geometry.

      2. No.... you do not need to remove the "mparam" definition in the function "Ng_GenerateVolumeMesh" because if you notice, the function passes this object as one of the arguments to the downstream functions in the mesh generation process....

        MeshVolume (mparam, *m); <====

        OptimizeVolume (mparam, *m); <====

      Have a great day !!

      Philippose

       
  • Nobody/Anonymous

    Hello,

    I have tried to get this to work myself. I am using the example ng_stl.cpp in the directory nglib.

    1. The variable fineness is set in ng_stl.cpp but not copied in nglib.cpp. Also, there is no variable fineness in the MeshingParameters class. You talk about grading. Does this mean the variable fineness in Ng_Meshing_Parameters should be copied to grading in Meshing_Parameters?

    2. Can I control the mesh size via the variable minh? I have found that netgen sometimes does much too much refinement for my liking at a small sharp/pointy feature. Will minh have any effect?

    3. Is there a variable wich controls curvature dependent mesh size?

    Thanks for any guidance,
    Bryn

     
  • Joachim Schoeberl

    Hi Bryn,

    not all features are available via nglib (just nobody had put them in) - sorry. It's best to try things first with the GUI version.

    the fineness variable is planned to have an overall meaning (such as the coarse / fine control) and should effect several variables.

    usually, the mesh gets very fine when there is some small detail (either wanted, or faults) in the stl - geometry. minh does not help against it. The STL doctor might be helpful.

    the STL - chart distance slider controls the curvature dependent mesh size, elements per curvature radius has no effect on STL geometries.

    Joachim

     
  • Nobody/Anonymous

    Hi Joachim,

    Thanks for the answer. I think I have found the parameter which I need to play with. I think it is: stlparam.resthchartdistfac

    Now I can figure out in the gui what other parameters might mean.

    Bryn

     
  • Nobody/Anonymous

    I set this parameter to 0 and managed to remove nearly all &quot;over-refinement&quot;. There are still some regions that are more dense than others. But this might be due to the mesh smoothing. It seems to happen where the node valence number is too low (usually 3-5).

     

Log in to post a comment.