Exemple of native C++ application using CM2 MeshTools.


Get the source-code of this example.

See also the Visual Basic version, the Borland Delphi version, the C# version.

// Very simple C++ program to mesh a square with triangles or quadrangles.
// Uses CM2 TriaMesh and CM2 QuadMesh directly (without the COM interface).

// Includes the necessary CM2 MeshTools headers.
#include <iostream>
#include "math1.h"
#include "meshtools.h"
#include "meshtools1d.h"
#include "triamesh.h"
#include "quadmesh.h"

/// Display handler.
static void display_hdl (unsigned level, const char* msg) { std::cout << msg; }

int
main (int argc, char* argv[]) 
{
   cm2::DoubleMat                     pos;                      // To store the coordinates matrix.
   cm2::UIntVec                       indices;                  // To store the indices of the boundary nodes.
   cm2::UIntMat                       connectB;                 // To store the connectivity matrix of the boundary edges.
   cm2::UIntMat                       connectM;                 // To store the connectivity of the 2-D mesh.

   cm2::triamesh::mesher              mesherT3;                 // Instance of triangle mesher.
   cm2::quadmesh::mesher              mesherQ4;                 // Instance of quadrangle mesher.

   cm2::triamesh::mesher::data_type   dataT3;                   // Data record for a triangle mesher.
   cm2::quadmesh::mesher::data_type   dataQ4;                   // Data record for a quadrangle mesher.

   try
   {
      // DLL REGISTRATION. REPLACE WITH YOUR COMPANY'S KEYS.
      cm2::triamesh::registration ("Licensed to DEMO.", "B36C4016CA0B");
      cm2::quadmesh::registration ("Licensed to DEMO.", "52E503F92365");

      // THE FOUR VERTICES OF THE SQUARE.
      pos.push_back (cm2::DoubleVec2(0.,0.));
      pos.push_back (cm2::DoubleVec2(4.,4.));
      pos.push_back (cm2::DoubleVec2(4.,4.));
      pos.push_back (cm2::DoubleVec2(0.,4.));

      // MESH THE FOUR SEGMENTS (10 ELEMENTS ON EACH SEGMENT).
      cm2::meshtools1D::mesh_straight (pos, 0, 1, 10, indices); indices.pop_back();
      cm2::meshtools1D::mesh_straight (pos, 1, 2, 10, indices); indices.pop_back();
      cm2::meshtools1D::mesh_straight (pos, 2, 3, 10, indices); indices.pop_back();
      cm2::meshtools1D::mesh_straight (pos, 3, 0, 10, indices);
      cm2::meshtools1D::indices_to_connectE2 (indices, connectB);

      // DO THE TRIANGLE MESH.
      dataT3.pos = pos;                                          // The coordinates.
      dataT3.connectB = connectB;                                // The boundary.
      mesherT3.run (dataT3);                                     // Run the mesher.

      if (dataT3.error_code != 0) 
         throw exception (dataT3.msg1)                           // Error reporting (license error, boundary error...)

      pos = dataT3.pos;                                          // The coordinate matrix has been extended with new nodes.
      connectM = dataT3.connectM;                                // The triangle connectivity matrix.
      dataT3.print_info (&display_hdl);                          // Print some info on console about the generated mesh.

      // MEDIT OUTPUT OF THE TRIANGLE MESH.
      cm2::meshtools::medit_output ("T3.mesh", pos, connectM, CM2_FACET3);


      // DO THE SAME WITH THE QUAD MESHER.
      dataQ4.pos = pos;
      dataQ4.connectB = connectB;
      mesherQ4.run (dataQ4);

      if (dataQ4.error_code != 0) 
         throw exception (dataQ4.msg1) 

      pos = dataQ4.pos;
      connectM = dataQ4.connectM;
      dataQ4.print_info (&display_hdl);

      cm2::meshtools::medit_output ("Q4.mesh", pos, connectM, CM2_FACEQ4);
   }
   
   catch (exception ex)
   {
      std::cout << "Exception " << ex.str() << std::endl;
   }
   
   return 0;
}