I created two nef_polyhedron cube that was inited from polyhedron_3 which is create by Polyhedron_incremental_builder_3
the bool option i want do is cube1-cube2,but it didn't got right result enter image description here enter image description here it got two more points above larger face
here is my code
#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;
}
I reproduced your polyhedra with R and these are not some cubes. What I can see is consistent with your pictures: