Search code examples
computational-geometrymeshcgaltetrahedra

Keep patch numbers during tetrahedral meshing with CGAL


Input

I have several meshes in the .off format that together enclose a volume. For instance, take patch-01.off, patch-20.off and patch-30.off that are available with CGAL-4.11 in examples/Mesh_3/data/patches.

Desired output

I would like to get a tetrahedral mesh of this volume and save it in the .mesh format. The difficult part is that I want each line corresponding to a triangle to end with a number 0, 1 or 2 indicating to which of the input patches the triangle corresponds. Currently, I don't care about the tags of the vertices or tetrahedra.

Almost working solution

I tried modifying the CGAL example examples/Mesh_3/mesh_polyhedral_complex.cpp (the modified portion is marked):

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>

#include <CGAL/Mesh_triangulation_3.h>
#include <CGAL/Mesh_complex_3_in_triangulation_3.h>
#include <CGAL/Mesh_criteria_3.h>

#include <CGAL/Polyhedral_complex_mesh_domain_3.h>
#include <CGAL/make_mesh_3.h>

#include <cstdlib>

// Domain
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Mesh_polyhedron_3<K>::type Polyhedron;
typedef CGAL::Polyhedral_complex_mesh_domain_3<K> Mesh_domain;


#ifdef CGAL_CONCURRENT_MESH_3
typedef CGAL::Parallel_tag Concurrency_tag;
#else
typedef CGAL::Sequential_tag Concurrency_tag;
#endif

// Triangulation
typedef CGAL::Mesh_triangulation_3<Mesh_domain,CGAL::Default,Concurrency_tag>::type Tr;

typedef CGAL::Mesh_complex_3_in_triangulation_3<
  Tr,Mesh_domain::Corner_index,Mesh_domain::Curve_segment_index> C3t3;

// Criteria
typedef CGAL::Mesh_criteria_3<Tr> Mesh_criteria;

// To avoid verbose function and named parameters call
using namespace CGAL::parameters;

// THE MODIFICATION STARTS HERE
const char* const filenames[] = {
  "data/patches/patch-01.off",
  "data/patches/patch-20.off",
  "data/patches/patch-30.off",
};

const std::pair<int, int> incident_subdomains[] = {
  std::make_pair(0, 1),
  std::make_pair(1, 0),
  std::make_pair(1, 0),
};
// THE REMAINDER OF THE FILE IS UNCHANGED.

int main()
{
  const std::size_t nb_patches = sizeof(filenames) / sizeof(const char*);
  CGAL_assertion(sizeof(incident_subdomains) ==
                 nb_patches * sizeof(std::pair<int, int>));
  std::vector<Polyhedron> patches(nb_patches);
  for(std::size_t i = 0; i < nb_patches; ++i) {
    std::ifstream input(filenames[i]);
    if(!(input >> patches[i])) {
      std::cerr << "Error reading " << filenames[i] << " as a polyhedron!\n";
      return EXIT_FAILURE;
    }
  }
  // Create domain
  Mesh_domain domain(patches.begin(), patches.end(),
                     incident_subdomains, incident_subdomains+nb_patches);

  domain.detect_features(); //includes detection of borders

  // Mesh criteria
  Mesh_criteria criteria(edge_size = 8,
                         facet_angle = 25, facet_size = 8, facet_distance = 0.2,
                         cell_radius_edge_ratio = 3, cell_size = 10);

  // Mesh generation
  C3t3 c3t3 = CGAL::make_mesh_3<C3t3>(domain, criteria);

  // Output
  std::ofstream medit_file("out.mesh");
  c3t3.output_to_medit(medit_file);

  return EXIT_SUCCESS;
}

This creates a well-looking tetrahedral mesh and saves it to out.mesh. However, all the triangles have a tag 1, as shown in the following excerpt (lines 1318--1328 in out.mesh).

52.527837077556413 58.272620021324407 30.13290265121827 1
0.06169736357779243 30.258121963438846 69.405198139655852 1
Triangles
2944
923 898 888 1
923 898 888 1
905 903 890 1
905 903 890 1
354 385 375 1
354 385 375 1

When I display the result in medit, all the triangles have the same colour, while (to put the question other way) I would like each of the input patches to be of different colour.

Question

What do I need to modify in the example above?

Side note

I noticed that out.mesh seems to contain two copies of each triangle. Is this related to the problem? How can I get rid of the copies?

Related questions

There already is a similar question. The difference is that they have a single file and try to convey the patch info through colour, whereas my patches are in separate files.


Solution

  • thanks for you precise question.

    There is a very easy solution for your question, but that involves an undocumented feature of output_to_medit(). Just replace the line:

    c3t3.output_to_medit(medit_file);
    

    by

    c3t3.output_to_medit(medit_file, false, true);
    

    and that will tag the facets with surface patches IDs.