diff --git a/CGAL_basic_viewers/examples/CGAL_basic_viewers/CMakeLists.txt b/CGAL_basic_viewers/examples/CGAL_basic_viewers/CMakeLists.txt index acfeb6e8942..e5a6aa07ad8 100644 --- a/CGAL_basic_viewers/examples/CGAL_basic_viewers/CMakeLists.txt +++ b/CGAL_basic_viewers/examples/CGAL_basic_viewers/CMakeLists.txt @@ -7,7 +7,7 @@ # Careful when modifying cmake_minimum_required(VERSION 3.1...3.23) -project(CGAL_basic_viewers) +project(CGAL_basic_viewers_Examples) #CGAL_Qt5 is needed for the drawing. find_package(CGAL REQUIRED OPTIONAL_COMPONENTS Qt5) diff --git a/Nef_3/include/CGAL/draw_nef_3.h b/Nef_3/include/CGAL/draw_nef_3.h index 1c4e2ac1eb9..ccff10a5ef7 100644 --- a/Nef_3/include/CGAL/draw_nef_3.h +++ b/Nef_3/include/CGAL/draw_nef_3.h @@ -14,14 +14,10 @@ #ifndef DRAW_NEF_3_H #define DRAW_NEF_3_H -#include -#include #include #include - -#ifdef CGAL_USE_BASIC_VIEWER - -#include +#include +#include #include #include #include @@ -40,46 +36,44 @@ typedef Local_kernel::Vector_3 Local_vector; template Local_vector get_face_normal(typename Nef_Polyhedron::SHalfedge_const_handle she) { - typedef typename Nef_Polyhedron::SHalfedge_around_facet_const_circulator SHalfedge_around_facet_const_circulator; - - SHalfedge_around_facet_const_circulator he(she); - Local_vector normal = CGAL::NULL_VECTOR; - SHalfedge_around_facet_const_circulator end = he; - unsigned int nb = 0; + typename Nef_Polyhedron::SHalfedge_around_facet_const_circulator he(she), end(he); + Local_vector normal=CGAL::NULL_VECTOR; + unsigned int nb=0; + using GU=internal::Geom_utils::Kernel, + Local_kernel>; CGAL_For_all(he, end) { - internal::newell_single_step_3(Basic_viewer_qt<>::get_local_point - (he->next()->source()->center_vertex()->point()), - Basic_viewer_qt<>::get_local_point(he->source()->center_vertex()-> - point()), normal); + internal::newell_single_step_3(GU::get_local_point(he->next()->source()-> + center_vertex()->point()), + GU::get_local_point(he->source()->center_vertex()-> + point()), normal); ++nb; } CGAL_assertion(nb > 0); - return (typename Local_kernel::Construct_scaled_vector_3()(normal, 1.0 / nb)); + return (typename Local_kernel::Construct_scaled_vector_3()(normal, 1.0/nb)); } template Local_vector get_vertex_normal(typename Nef_Polyhedron::Vertex_const_handle vh) { - typedef typename Nef_Polyhedron::SHalfedge_const_handle SHalfedge_const_handle; - typedef typename Nef_Polyhedron::SHalfedge_const_iterator SHalfedge_const_iterator; - - Local_vector normal = CGAL::NULL_VECTOR; - - SHalfedge_const_iterator it = vh->shalfedges_begin(); - SHalfedge_const_handle end = it; - do { - Local_vector n = get_face_normal(it); - normal = typename Local_kernel::Construct_sum_of_vectors_3()(normal, n); - it = it->snext(); - } while( it != end ); + typename Nef_Polyhedron::SHalfedge_const_iterator it=vh->shalfedges_begin(); + typename Nef_Polyhedron::SHalfedge_const_handle end=it; + Local_vector normal=CGAL::NULL_VECTOR; + do + { + Local_vector n=get_face_normal(it); + normal=typename Local_kernel::Construct_sum_of_vectors_3()(normal, n); + it=it->snext(); + } + while(it!=end); if (!typename Local_kernel::Equal_3()(normal, CGAL::NULL_VECTOR)) { - normal = (typename Local_kernel::Construct_scaled_vector_3()( - normal, 1.0 / CGAL::sqrt(normal.squared_length()))); + normal=(typename Local_kernel::Construct_scaled_vector_3() + (normal, 1.0/CGAL::sqrt(normal.squared_length()))); } return normal; @@ -96,51 +90,63 @@ class Nef_Visitor { typedef typename Nef_Polyhedron::Vertex_const_handle Vertex_const_handle; typedef typename Nef_Polyhedron::SFace_const_handle SFace_const_handle; typedef typename Nef_Polyhedron::SHalfloop_const_handle SHalfloop_const_handle; - typedef typename Nef_Polyhedron::Halffacet_cycle_const_iterator Halffacet_cycle_const_iterator; + typedef typename Nef_Polyhedron::Halffacet_cycle_const_iterator Halffacet_cycle_const_iterator; public: - Nef_Visitor(const Nef_Polyhedron &_nef, - CGAL::Graphic_buffer & _graphic_buffer, - const DrawingFunctor &_drawing_functor) - : n_faces(0), n_edges(0), + Nef_Visitor(const Nef_Polyhedron&_nef, + CGAL::Graphic_buffer& _graphic_buffer, + const DrawingFunctor&_drawing_functor) : + n_faces(0), n_edges(0), nef(_nef), graphic_buffer(_graphic_buffer), - drawing_functor(_drawing_functor) {} + drawing_functor(_drawing_functor) + {} - void visit(Vertex_const_handle vh) { - graphic_buffer.add_point(vh->point()); + void visit(Vertex_const_handle vh) + { + if (!drawing_functor.are_vertices_enabled() || + !drawing_functor.draw_vertex(nef, vh)) + { return; } + + if(drawing_functor.colored_vertex(nef, vh)) + { graphic_buffer.add_point(vh->point(), + drawing_functor.vertex_color(nef, vh)); } + else + { graphic_buffer.add_point(vh->point()); } } void visit(Halffacet_const_handle opposite_facet) { - Halffacet_const_handle f = opposite_facet->twin(); + if (!drawing_functor.are_faces_enabled() || + !drawing_functor.draw_face(nef, opposite_facet)) + { return; } - if (facets_done.find(f) != facets_done.end() || - facets_done.find(opposite_facet) != facets_done.end()) { - return; - } + Halffacet_const_handle f=opposite_facet->twin(); + if (facets_done.find(f)!=facets_done.end() || + facets_done.find(opposite_facet)!=facets_done.end()) + { return; } SHalfedge_const_handle se; Halffacet_cycle_const_iterator fc = f->facet_cycles_begin(); se = SHalfedge_const_handle(fc); // non-zero if shalfedge is returned - if(se == 0) - { //return if not-shalfedge - return; - } + if(se==0) + { return; } //return if not-shalfedge - if(drawing_functor.colored_face(nef, f)) { - CGAL::IO::Color c = drawing_functor.face_color(nef, f); - graphic_buffer.face_begin(c); - } + if(drawing_functor.colored_face(nef, f)) + { graphic_buffer.face_begin(drawing_functor.face_color(nef, f)); } + else + { graphic_buffer.face_begin(); } SHalfedge_around_facet_const_circulator hc_start(se); SHalfedge_around_facet_const_circulator hc_end(hc_start); Vertex_const_handle lastvh; - CGAL_For_all(hc_start, hc_end) { + CGAL_For_all(hc_start, hc_end) + { Vertex_const_handle vh=hc_start->source()->center_vertex(); lastvh=vh; - graphic_buffer.add_point_in_face(vh->point(), draw_function_for_nef_polyhedron::get_vertex_normal(vh)); + graphic_buffer.add_point_in_face(vh->point(), + get_vertex_normal(vh)); } // Now iterate through holes of the face @@ -150,34 +156,43 @@ public: se = SHalfedge_const_handle(fc); hc_start=se; hc_end=hc_start; - CGAL_For_all(hc_start, hc_end) { + CGAL_For_all(hc_start, hc_end) + { Vertex_const_handle vh=hc_start->source()->center_vertex(); - graphic_buffer.add_point_in_face(vh->point(), draw_function_for_nef_polyhedron::get_vertex_normal(vh)); + graphic_buffer.add_point_in_face(vh->point(), + get_vertex_normal(vh)); } graphic_buffer.add_point_in_face(hc_start->source()->center_vertex()->point(), - draw_function_for_nef_polyhedron::get_vertex_normal(hc_start->source()->center_vertex())); - graphic_buffer.add_point_in_face(lastvh->point(), draw_function_for_nef_polyhedron::get_vertex_normal(lastvh)); + get_vertex_normal + (hc_start->source()->center_vertex())); + graphic_buffer.add_point_in_face(lastvh->point(), + get_vertex_normal(lastvh)); ++fc; } graphic_buffer.face_end(); facets_done[f]=true; - n_faces++; + ++n_faces; } void visit(Halfedge_const_handle he) { - Halfedge_const_handle twin = he->twin(); - if (edges_done.find(he) != edges_done.end() || - edges_done.find(twin) != edges_done.end()) - { - // Edge already added - return; - } + if (!drawing_functor.are_edges_enabled() || + !drawing_functor.draw_edge(nef, he)) + { return; } - graphic_buffer.add_segment(he->source()->point(), he->target()->point()); - edges_done[he] = true; - n_edges++; + Halfedge_const_handle twin=he->twin(); + if (edges_done.find(he)!=edges_done.end() || + edges_done.find(twin)!=edges_done.end()) + { return; } // Edge already added + + if(drawing_functor.colored_edge(nef, he)) + { graphic_buffer.add_segment(he->source()->point(), he->target()->point(), + drawing_functor.edge_color(nef, he)); } + else + { graphic_buffer.add_segment(he->source()->point(), he->target()->point()); } + edges_done[he]=true; + ++n_edges; } void visit(SHalfedge_const_handle ) {} @@ -190,28 +205,27 @@ protected: std::unordered_map edges_done; CGAL::Graphic_buffer& graphic_buffer; const DrawingFunctor& drawing_functor; - const Nef_Polyhedron &nef; + const Nef_Polyhedron& nef; }; -template -void compute_elements(const Nef_Polyhedron &nef, CGAL::Graphic_buffer &graphic_buffer, - const DrawingFunctor &m_drawing_functor) +template +void compute_elements(const Nef_Polyhedron &nef, + CGAL::Graphic_buffer &graphic_buffer, + const DrawingFunctor &drawing_functor) { - typedef typename Nef_Polyhedron::Volume_const_iterator Volume_const_iterator; - typedef typename Nef_Polyhedron::Shell_entry_const_iterator Shell_entry_const_iterator; - typedef typename Nef_Polyhedron::SFace_const_handle SFace_const_handle; + typedef typename Nef_Polyhedron::Volume_const_iterator Volume_const_iterator; + typedef typename Nef_Polyhedron::Shell_entry_const_iterator Shell_entry_const_iterator; + typedef typename Nef_Polyhedron::SFace_const_handle SFace_const_handle; Volume_const_iterator c; - - Nef_Visitor V(nef, graphic_buffer, m_drawing_functor); + Nef_Visitor + V(nef, graphic_buffer, drawing_functor); CGAL_forall_volumes(c, nef) { Shell_entry_const_iterator it; CGAL_forall_shells_of(it, c) - { - nef.visit_shell_objects(SFace_const_handle(it), V); - } + { nef.visit_shell_objects(SFace_const_handle(it), V); } } graphic_buffer.negate_all_normals(); @@ -219,65 +233,75 @@ void compute_elements(const Nef_Polyhedron &nef, CGAL::Graphic_buffer -void add_in_graphic_buffer(const Nef_Polyhedron &nef, - CGAL::Graphic_buffer &graphic_buffer, - const DrawingFunctor &m_drawing_functor) { - draw_function_for_nef_polyhedron::compute_elements(nef, graphic_buffer, m_drawing_functor); +#define CGAL_NEF3_TYPE Nef_polyhedron_3 + +// add_in_graphic_buffer +template +void add_in_graphic_buffer(const CGAL_NEF3_TYPE &anef, + CGAL::Graphic_buffer &graphic_buffer, + const DrawingFunctor &drawing_functor) +{ + draw_function_for_nef_polyhedron::compute_elements(anef, + graphic_buffer, + drawing_functor); } -template -void add_in_graphic_buffer(const Nef_Polyhedron &nef, CGAL::Graphic_buffer &graphic_buffer) { - +template +void add_in_graphic_buffer(const CGAL_NEF3_TYPE &anef, + CGAL::Graphic_buffer &graphic_buffer) +{ // Default functor; user can add his own functor. - Drawing_functor + Drawing_functor drawing_functor; - drawing_functor.colored_face = [] (const Nef_Polyhedron&, - typename Nef_Polyhedron::Halffacet_const_handle fh) -> bool - { - return true; - }; + drawing_functor.colored_face = [] (const CGAL_NEF3_TYPE&, + typename CGAL_NEF3_TYPE::Halffacet_const_handle) -> bool + { return true; }; - drawing_functor.face_color = [] (const Nef_Polyhedron&, - typename Nef_Polyhedron::Halffacet_const_handle fh) -> CGAL::IO::Color + drawing_functor.face_color = [] (const CGAL_NEF3_TYPE&, + typename CGAL_NEF3_TYPE::Halffacet_const_handle fh) -> CGAL::IO::Color { - if (fh == nullptr) // use to get the mono color - return CGAL::IO::Color(100, 125, 200); // R G B between 0-255 + if (fh==nullptr) // use to get the mono color + { return CGAL::IO::Color(100, 125, 200); } CGAL::Random random((unsigned int)(std::size_t)(&(*fh))); return get_random_color(random); }; - add_in_graphic_buffer(nef, graphic_buffer, drawing_functor); + add_in_graphic_buffer(anef, graphic_buffer, drawing_functor); } -#define CGAL_NEF3_TYPE Nef_polyhedron_3 +#ifdef CGAL_USE_BASIC_VIEWER +// Specialization of draw function template + typename BufferType=float, class DrawingFunctor> void draw(const CGAL_NEF3_TYPE &anef, const DrawingFunctor &drawing_functor, - bool nofill = false) { + const char *title="Nef Polyhedron Viewer") +{ CGAL::Graphic_buffer buffer; add_in_graphic_buffer(anef, buffer, drawing_functor); - draw_buffer(buffer); + draw_buffer(buffer, title); } -template +template void draw(const CGAL_NEF3_TYPE &anef, - const char *title = "Nef Polyhedron Viewer", - bool nofill = false) { + const char *title="Nef Polyhedron Viewer") +{ CGAL::Graphic_buffer buffer; add_in_graphic_buffer(anef, buffer); - draw_buffer(buffer); + draw_buffer(buffer, title); } -} // End namespace CGAL - #endif // CGAL_USE_BASIC_VIEWER +} // End namespace CGAL + #endif // DRAW_NEF_3_H