CGAL / cgal

The public CGAL repository, see the README below
https://github.com/CGAL/cgal#readme
Other
4.98k stars 1.39k forks source link

Surface mesh approximation error or bug #4904

Open KeepFaithMe opened 4 years ago

KeepFaithMe commented 4 years ago

Please use the following template to help us solving your issue.

Issue Details

When I used the function 'VSA::approximate_triangle_mesh' to approximate my triangle_mesh,a error or bug ocurred.The details of this error are as follows: CGAL error: assertion violation! Expression : chord_size > 2 File :..\vge_variational_shape_appraximation.h Line: 1856 I've dealt with other models with this function, and none of them have had this problem.So, I'm confused.

Source Code

The source code is https://doc.cgal.org/latest/Surface_mesh_approximation/Surface_mesh_approximation_2vsa_simple_approximation_example_8cpp-example.html CGAL version:4.14.1 the data is:https://gist.github.com/KeepFaithMe/adf34b1da4defc36fe16fdeb5d9e95c6

Environment

sgiraudot commented 4 years ago

The problem comes from the fact that your input is not 2-manifold: surface mesh approximation only works on 2-manifold meshes as stated at the beginning of the manual.

One way to fix this is to load your input as a polygon soup, then orient it (which will fix non-manifold cases by duplicating vertices) and build the mesh from it. See this example which does exactly this.

This modified code works for your data set:

#include <iostream>
#include <fstream>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_mesh_processing/orient_polygon_soup.h>
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
#include <CGAL/IO/OFF_reader.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Surface_mesh_approximation/approximate_triangle_mesh.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Surface_mesh<Kernel::Point_3> Mesh;

namespace VSA = CGAL::Surface_mesh_approximation;

int main(int argc, char** argv)
{
  // First read polygon soup
  std::vector<Kernel::Point_3> points;
  std::vector<std::vector<std::size_t> > polygons;
  std::ifstream input(argv[1]);
  if(!input || !CGAL::read_OFF(input, points, polygons) || points.empty())
  {
    std::cerr << "Cannot open file " << std::endl;
    return EXIT_FAILURE;
  }

  // Then orient it
  CGAL::Polygon_mesh_processing::orient_polygon_soup(points, polygons);

  // Then create the mesh from it
  Mesh mesh;
  CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points, polygons, mesh);

  // The output will be an indexed triangle mesh
  std::vector<Kernel::Point_3> anchors;
  std::vector<std::array<std::size_t, 3> > triangles;

  // free function interface with named parameters
  VSA::approximate_triangle_mesh(mesh,
    CGAL::parameters::verbose_level(VSA::MAIN_STEPS).
    max_number_of_proxies(200).
    anchors(std::back_inserter(anchors)). // anchor points
    triangles(std::back_inserter(triangles))); // indexed triangles

  std::cout << "#anchor points: " << anchors.size() << std::endl;
  std::cout << "#triangles: " << triangles.size() << std::endl;

  return EXIT_SUCCESS;
}
KeepFaithMe commented 4 years ago

using the code :'CGAL::Polygon_mesh_processing::orient_polygon_soup(points, polygons)' and 'CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points, polygons, mesh)',will a new vertex or a new face be introduced?And Can the self-intersection problem in mesh be solved by using the above two pieces of code?

afabri commented 4 years ago

@sgiraudot let'add a precondition in the code.

KeepFaithMe commented 4 years ago

When I used the function 'VSA::approximate_triangle_mesh' to approximate my triangle_mesh, I found that the number of faces in each category varies greatly. Is there any parameter that can be adjusted to make the number of faces between categories more balance.

afabri commented 3 years ago

@sgiraudot As the function takes a FaceGraph as argument it is a combinatorial 2-manifold. Should the precondition be that it is a geometric 2-manifold?