Make the computation of the kernel of a Surface_mesh work

This commit is contained in:
Andreas Fabri 2017-03-08 17:38:39 +01:00 committed by Maxime Gimeno
parent 6231a925a2
commit adde363ada
4 changed files with 95 additions and 39 deletions

View File

@ -105,7 +105,6 @@ void copy_face_graph_impl(const SourceMesh& sm, TargetMesh& tm,
put(tm_vpm, tm_h_src, conv(get(sm_vpm, sm_h_src)));
}
}
//create faces and connect halfedges
BOOST_FOREACH(sm_face_descriptor sm_f, faces(sm))
{
@ -125,7 +124,6 @@ void copy_face_graph_impl(const SourceMesh& sm, TargetMesh& tm,
tm_h_prev=tm_h;
}
}
// update next/prev of tm border halfedges by visiting faces of the mesh
BOOST_FOREACH(tm_halfedge_descriptor h, tm_border_halfedges)
{
@ -136,7 +134,6 @@ void copy_face_graph_impl(const SourceMesh& sm, TargetMesh& tm,
while( !is_border(candidate, tm) );
set_next(h, candidate, tm);
}
// update halfedge vertex of all but the vertex halfedge
BOOST_FOREACH(tm_vertex_descriptor v, vertices(tm))
{

View File

@ -3,4 +3,4 @@ include( polyhedron_demo_macros )
polyhedron_demo_plugin(convex_hull_plugin Convex_hull_plugin)
target_link_libraries(convex_hull_plugin scene_polyhedron_item scene_points_with_normal_item scene_polylines_item scene_polyhedron_selection_item)
polyhedron_demo_plugin(kernel_plugin Kernel_plugin)
target_link_libraries(kernel_plugin scene_polyhedron_item)
target_link_libraries(kernel_plugin scene_polyhedron_item scene_surface_mesh_item)

View File

@ -1,3 +1,4 @@
#include <fstream>
#include <QApplication>
#include <QMainWindow>
#include <QMessageBox>
@ -5,6 +6,7 @@
#include <QStringList>
#include "Scene_polyhedron_item.h"
#include "Scene_surface_mesh_item.h"
#include "Polyhedron_type.h"
#include <CGAL/Three/Polyhedron_demo_plugin_interface.h>
@ -13,6 +15,8 @@
#include <CGAL/convex_hull_3.h>
#include <CGAL/Dualizer.h>
#include <CGAL/translate.h>
#include <CGAL/boost/graph/Dual.h>
#include <CGAL/boost/graph/copy_face_graph.h>
#include "Kernel_type.h"
typedef Kernel::Triangle_3 Triangle;
@ -35,7 +39,8 @@ public:
}
bool applicable(QAction*) const {
return qobject_cast<Scene_polyhedron_item*>(scene->item(scene->mainSelectionIndex()));
return qobject_cast<Scene_polyhedron_item*>(scene->item(scene->mainSelectionIndex())) ||
qobject_cast<Scene_surface_mesh_item*>(scene->item(scene->mainSelectionIndex()));
}
void init(QMainWindow* mainWindow, CGAL::Three::Scene_interface* scene_interface, Messages_interface*)
@ -64,18 +69,25 @@ void Polyhedron_demo_kernel_plugin::on_actionKernel_triggered()
Scene_polyhedron_item* item =
qobject_cast<Scene_polyhedron_item*>(scene->item(index));
if(item)
Scene_surface_mesh_item* sm_item =
qobject_cast<Scene_surface_mesh_item*>(scene->item(index));
if(item || sm_item)
{
QApplication::setOverrideCursor(Qt::WaitCursor);
Polyhedron* pMesh = item->polyhedron();
Polyhedron* pMesh = (item)?item->polyhedron():NULL;
Scene_surface_mesh_item::SMesh* sMesh = (sm_item)?sm_item->polyhedron():NULL;
typedef CGAL::Exact_rational ET;
typedef Polyhedron_kernel<Kernel,ET> Polyhedron_kernel;
// get triangles from polyhedron
std::list<Triangle> triangles;
get_triangles(*pMesh,std::back_inserter(triangles));
if(pMesh){
get_triangles(*pMesh,std::back_inserter(triangles));
} else {
get_triangles(*sMesh,std::back_inserter(triangles));
}
// solve LP
std::cout << "Solve linear program..." << triangles.size();
Polyhedron_kernel kernel;
@ -90,9 +102,6 @@ void Polyhedron_demo_kernel_plugin::on_actionKernel_triggered()
}
std::cout << "done" << std::endl;
// add kernel as new polyhedron
Polyhedron *pKernel = new Polyhedron;
// get inside point
Point inside_point = kernel.inside_point();
Vector translate = inside_point - CGAL::ORIGIN;
@ -125,21 +134,71 @@ void Polyhedron_demo_kernel_plugin::on_actionKernel_triggered()
CGAL::convex_hull_3(dual_points.begin(),dual_points.end(),convex_hull);
std::cout << "ok" << std::endl;
// dualize and translate back to get final kernel
Dualizer<Polyhedron,Kernel> dualizer;
dualizer.run(convex_hull,*pKernel);
::translate<Polyhedron,Kernel>(*pKernel,translate);
pKernel->inside_out();
if(pMesh){
// add kernel as new polyhedron
Polyhedron *pKernel = new Polyhedron;
// dualize and translate back to get final kernel
Dualizer<Polyhedron,Kernel> dualizer;
dualizer.run(convex_hull,*pKernel);
::translate<Polyhedron,Kernel>(*pKernel,translate);
pKernel->inside_out();
Scene_polyhedron_item* new_item = new Scene_polyhedron_item(pKernel);
new_item->setName(tr("%1 (kernel)").arg(item->name()));
new_item->setColor(Qt::magenta);
new_item->setRenderingMode(item->renderingMode());
item->setRenderingMode(Wireframe);
scene->addItem(new_item);
scene->itemChanged(item);
} else { // sMesh
// add kernel as new polyhedron
typedef Scene_surface_mesh_item::SMesh SMesh;
SMesh* pKernel = new SMesh;
typedef CGAL::Dual<Polyhedron> Dual;
typedef boost::graph_traits<Dual>::face_descriptor dual_face_descriptor;
typedef boost::graph_traits<Dual>::vertex_descriptor dual_vertex_descriptor;
Scene_polyhedron_item* new_item = new Scene_polyhedron_item(pKernel);
new_item->setName(tr("%1 (kernel)").arg(item->name()));
new_item->setColor(Qt::magenta);
new_item->setRenderingMode(item->renderingMode());
item->setRenderingMode(Wireframe);
scene->addItem(new_item);
scene->itemChanged(item);
std::ofstream out("primal.off");
out << convex_hull << std::endl;
out.close();
Dual dual(convex_hull);
std::map<dual_vertex_descriptor,Point> vpm;
BOOST_FOREACH(boost::graph_traits<Polyhedron>::face_descriptor fd, faces(convex_hull)){
Point points[3];
int i = 0;
BOOST_FOREACH(boost::graph_traits<Polyhedron>::vertex_descriptor vd,
vertices_around_face(halfedge(fd,convex_hull),convex_hull)){
points[i++] = get(CGAL::vertex_point,convex_hull,vd);
}
Plane plane(points[0],points[1],points[2]);
FT sqd = CGAL::squared_distance(Point(CGAL::ORIGIN),plane);
FT distance_to_origin = std::sqrt(sqd);
Vector normal = plane.orthogonal_vector();
normal = normal / std::sqrt(normal * normal);
vpm[fd] = CGAL::ORIGIN + normal / distance_to_origin + translate;
}
CGAL::copy_face_graph(dual, *pKernel,
CGAL::Emptyset_iterator(),
CGAL::Emptyset_iterator(),
CGAL::Emptyset_iterator(),
boost::make_assoc_property_map(vpm));
// pKernel->inside_out();
Scene_surface_mesh_item* new_item = new Scene_surface_mesh_item(pKernel);
new_item->setName(tr("%1 (kernel)").arg(sm_item->name()));
new_item->setColor(Qt::magenta);
new_item->setRenderingMode(sm_item->renderingMode());
sm_item->setRenderingMode(Wireframe);
scene->addItem(new_item);
scene->itemChanged(sm_item);
}
QApplication::restoreOverrideCursor();
}
}

View File

@ -161,19 +161,19 @@ template <class Polyhedron, class OutputIterator>
void get_triangles(Polyhedron& polyhedron,
OutputIterator out)
{
typedef typename Polyhedron::Facet_iterator Facet_iterator;
typedef typename Polyhedron::Halfedge_handle Halfedge_handle;
typedef typename Polyhedron::Point_3 Point;
typedef typename Polyhedron::Traits::Triangle_3 Triangle;
typedef typename boost::graph_traits<Polyhedron>::face_descriptor face_descriptor;
typedef typename boost::graph_traits<Polyhedron>::vertex_descriptor vertex_descriptor;
typedef typename boost::property_map<Polyhedron, CGAL::vertex_point_t>::type VPmap;
typedef typename boost::property_traits<VPmap>::value_type Point;
typedef typename CGAL::Kernel_traits<Point>::Kernel::Triangle_3 Triangle;
for(Facet_iterator f = polyhedron.facets_begin();
f != polyhedron.facets_end();
f++)
{
Halfedge_handle he = f->halfedge();
const Point& a = he->vertex()->point();
const Point& b = he->next()->vertex()->point();
const Point& c = he->next()->next()->vertex()->point();
*out++ = Triangle(a,b,c);
}
VPmap vpmap = get(CGAL::vertex_point,polyhedron);
BOOST_FOREACH(face_descriptor fd, faces(polyhedron)){
Point points[3];
int i = 0;
BOOST_FOREACH(vertex_descriptor vd, vertices_around_face(halfedge(fd,polyhedron),polyhedron)){
points[i++] = get(vpmap,vd);
}
*out++ = Triangle(points[0],points[1], points[2]);
}
}