CGAL Nef_Polyhedron Booloperation 错误结果

CGAL Nef_Polyhedron Booloperation incorrect result

提问人:Ttoxicx 提问时间:6/7/2023 更新时间:6/9/2023 访问量:16

问:

我创建了两个nef_polyhedron立方体,它们是从polyhedron_3启动的,由 Polyhedron_incremental_builder_3

我想做的 bool 选项是 cube1-cube2,但它没有得到正确的结果 在此处输入图像描述 在此处输入图像描述 它在较大的面上方又得到了两个点

这是我的代码

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Nef_polyhedron_3.h>
#include <CGAL/Aff_transformation_3.h>
#include <CGAL/boost/graph/convert_nef_polyhedron_to_polygon_mesh.h>
#include <CGAL/Polyhedron_incremental_builder_3.h>
#include <CGAL/Polygon_mesh_processing/corefinement.h>
#include <iostream>

typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef CGAL::Nef_polyhedron_3<Kernel> Nef_polyhedron;
typedef CGAL::Surface_mesh<Kernel::Point_3> Surface_mesh;
typedef CGAL::Point_3<Kernel> Point_3;
typedef CGAL::Aff_transformation_3<Kernel> AFT_3;
typedef Nef_polyhedron::Halffacet_const_iterator Halffacet_iterator;
typedef Nef_polyhedron::Vertex_iterator Vertex_iterator;
typedef Nef_polyhedron::Halfedge_const_iterator Halfedge_iteator;
typedef Nef_polyhedron::Vertex_const_handle Vertex_Handle;
class PolyhedronBuilder : public CGAL::Modifier_base<Polyhedron::HalfedgeDS> {
public:
    void setType(bool type) {
        _type = type;
    }
    void operator()(Polyhedron::HalfedgeDS& hds) {
        typedef typename Polyhedron::HalfedgeDS HalfedgeDS;
        CGAL::Polyhedron_incremental_builder_3<HalfedgeDS> builder(hds, true);

        // build polyhedron
        builder.begin_surface(8, 6);

        // Vertices1
        std::vector<Point_3> Points = {
            Point_3(-10.699169f, -15.407307f, 0.0f),   // Vertex0
            Point_3(-9.425352f, -16.199387f, 0.0f),   // Vertex1
            Point_3(-9.425352f, -16.199387f, 2.0f),   // Vertex2
            Point_3(-10.699169f, -15.407307f, 2.0f),   // Vertex3
            Point_3(2.5042138f, 5.8263035f, 2.0f),   // Vertex4
            Point_3(3.7780309f, 5.0342245f, 2.0f),   // Vertex5
            Point_3(3.7780309f, 5.0342245f, 0.0f),   // Vertex6
            Point_3(2.5042138f, 5.8263035f, 0.0f) //Vertex7
        };

        std::vector<Point_3> Points2 = {
            Point_3(-3.3673427f, -15.031566f, 0.0f),   // Vertex0
            Point_3(12.594898f, -15.031566f, 0.0f),   // Vertex1
            Point_3(12.594898f, -15.031566f, 2.0f),   // Vertex2
            Point_3(-3.3673427f, -15.031566f, 2.0f),   // Vertex3
            Point_3(-3.3673427f, 12.258608f, 2.0f),   // Vertex4
            Point_3(12.594898f, 12.258608f, 2.0f),   // Vertex5
            Point_3(12.594898f, 12.258608f, 0.0f),   // Vertex6
            Point_3(-3.3673427f, 12.258608f, 0.0f) //Vertex7
        };
        if (_type) {
            for (auto point : Points) {
                builder.add_vertex(point);
            }
        }
        else {
            for (auto point : Points2) {
                builder.add_vertex(point);
            }
        }

        // add face

        builder.begin_facet();
        builder.add_vertex_to_facet(0);
        builder.add_vertex_to_facet(1);
        builder.add_vertex_to_facet(2);
        builder.add_vertex_to_facet(3);
        builder.end_facet();


        builder.begin_facet();
        builder.add_vertex_to_facet(4);
        builder.add_vertex_to_facet(5);
        builder.add_vertex_to_facet(6);
        builder.add_vertex_to_facet(7);
        builder.end_facet();


        builder.begin_facet();
        builder.add_vertex_to_facet(1);
        builder.add_vertex_to_facet(0);
        builder.add_vertex_to_facet(7);
        builder.add_vertex_to_facet(6);
        builder.end_facet();


        builder.begin_facet();
        builder.add_vertex_to_facet(3);
        builder.add_vertex_to_facet(2);
        builder.add_vertex_to_facet(5);
        builder.add_vertex_to_facet(4);
        builder.end_facet();


        builder.begin_facet();
        builder.add_vertex_to_facet(0);
        builder.add_vertex_to_facet(3);
        builder.add_vertex_to_facet(4);
        builder.add_vertex_to_facet(7);
        builder.end_facet();


        builder.begin_facet();
        builder.add_vertex_to_facet(2);
        builder.add_vertex_to_facet(1);
        builder.add_vertex_to_facet(6);
        builder.add_vertex_to_facet(5);
        builder.end_facet();

        // finish
        builder.end_surface();
    }

private:
    bool _type = true;
};

void OutputNefPolyhedron(const Nef_polyhedron& nef, std::string strOutFilePath)
{
    Polyhedron temp;
    nef.convert_to_polyhedron(temp);
    /*std::cout << temp;*/

    Surface_mesh output;
    CGAL::convert_nef_polyhedron_to_polygon_mesh(nef, output);
    std::ofstream out;
    out.open(strOutFilePath);
    out << output;
    out.close();
}

int main() {
    // create two polyhedron(cube)
    Polyhedron cube1;
    PolyhedronBuilder builder;
    cube1.delegate(builder);
    Polyhedron cube2;
    PolyhedronBuilder builder1;
    builder1.setType(false);
    cube2.delegate(builder1);
    Nef_polyhedron nef1(cube1);
    Nef_polyhedron nef2(cube2);


    Nef_polyhedron::Vertex_const_handle handle;
    Nef_polyhedron::Vertex_const_iterator v = nef1.vertices_begin();
    Nef_polyhedron::Halfedge_const_iterator e = nef1.halfedges_begin();
    Nef_polyhedron::Halffacet_const_iterator f = nef1.halffacets_begin();
    // bool operation
    Nef_polyhedron nefUnion = nef2 + nef1;
    Nef_polyhedron nefIntersect = nef2 * nef1;
    Nef_polyhedron nefSubtract = nef2 - nef1;

    //print edges
    for (Halfedge_iteator e = nefSubtract.halfedges_begin(); e != nefSubtract.halfedges_end(); ++e)
    {
        Point_3 source = e->source()->point();
        Point_3 target = e->target()->point();
        std::cout << "Edge: " << source << " --> " << target << std::endl;
    }

    // output
    /*OutputNefPolyhedron(nef1, "D:/nef1.off");
    OutputNefPolyhedron(nef2, "D:/nef2.off");
    OutputNefPolyhedron(nefUnion, "D:/union.off");
    OutputNefPolyhedron(nefIntersect, "D:/intersect.off");*/
    OutputNefPolyhedron(nefSubtract, "D:/subtract.off");
    return 0;
}
CGAL 布尔运算

评论


答:

1赞 Stéphane Laurent 6/9/2023 #1

我用 R 复制了你的多面体,这些不是一些立方体。我所看到的与您的图片一致:

enter image description here

library(rgl)

vs1 <- cbind(
  c(-10.699169, -15.407307, 0.0),    
  c(-9.425352, -16.199387, 0.0),    
  c(-9.425352, -16.199387, 2.0),    
  c(-10.699169, -15.407307, 2.0),    
  c(2.5042138, 5.8263035, 2.0),    
  c(3.7780309, 5.0342245, 2.0),    
  c(3.7780309, 5.0342245, 0.0),    
  c(2.5042138, 5.8263035, 0.0) 
)

vs2 <- cbind(
  c(-3.3673427, -15.031566, 0.0),    
  c(12.594898, -15.031566, 0.0),    
  c(12.594898, -15.031566, 2.0),    
  c(-3.3673427, -15.031566, 2.0),    
  c(-3.3673427, 12.258608, 2.0),    
  c(12.594898, 12.258608, 2.0),    
  c(12.594898, 12.258608, 0.0),    
  c(-3.3673427, 12.258608, 0.0) 
)

quads <- 1 + cbind(
  c(0,1,2,3),
  c(4,5,6,7),
  c(1,0,7,6),
  c(3,2,5,4),
  c(0,3,4,7),
  c(2,1,6,5)
)

cub1 <- qmesh3d(
  vertices = vs1, indices = quads
)
cub2 <- qmesh3d(
  vertices = vs2, indices = quads
)

shade3d(cub1, color = "blue", alpha = 1, depth_test = "lequal")
shade3d(cub2, color = "green", alpha = 0.2)