CGAL / cgal

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

Clip problem #8590

Closed AlexGrig55 closed 2 weeks ago

AlexGrig55 commented 2 weeks ago

CGAL 6.0.1, Windows 10

I am trying to clip the closed mesh with another closed mesh and I get an incorrect result. With CGAL::parameters::default_values().clip_volume(false) the result is correct.

clipper: Image

target: Image

result: Image

correct result: Image

clipper file: clipper.off.txt

target file: target.off.txt

code:

#include <CGAL/Polygon_mesh_processing/clip.h>
#include <CGAL/boost/graph/copy_face_graph.h>
#include <CGAL/Polygon_mesh_processing/merge_border_vertices.h>
#include <CGAL/Polygon_mesh_processing/stitch_borders.h>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/corefinement.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>

namespace PMP = CGAL::Polygon_mesh_processing;
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_3 Point_3;
typedef CGAL::Surface_mesh<Point_3> Mesh;

void CheckMeshValidation(const Mesh& mesh)
{
    if (CGAL::Polygon_mesh_processing::does_self_intersect(mesh))
        throw std::exception();
    if(!CGAL::Polygon_mesh_processing::does_bound_a_volume(mesh))
        throw std::exception();
}

void ClipTest()
{
    std::string targetPath = CGAL::data_file_path("target.off");
    std::string clipperPath = CGAL::data_file_path("clipper.off");

    Mesh target;
    Mesh clipper;

    PMP::IO::read_polygon_mesh(targetPath, target);
    PMP::IO::read_polygon_mesh(clipperPath, clipper);

    CheckMeshValidation(target);
    CheckMeshValidation(clipper);

    PMP::clip(target, clipper, CGAL::parameters::default_values().clip_volume(true));

    std::ofstream out("result.off");
    out.precision(17);
    out << target << std::endl;
}
sloriot commented 2 weeks ago

target.off is oriented inside-out. It means that it represent the whole space minus the shape. So when calling with close_volume=true, you intersect the whole space minus the shape with the cube. So you end-up with the cube minus the shape. If not closing the volume, you get only the faces inside the cube. If you can PMP::reverse_face_orientations(target) you should get what you expect.