code review for polygon triangulation

This commit is contained in:
Guillaume Damiand 2022-10-18 15:28:19 +02:00
parent 999d496cdf
commit 8c546efe71
3 changed files with 92 additions and 182 deletions

View File

@ -1,8 +1,9 @@
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
#include <CGAL/draw_triangulation_2.h>
#include <CGAL/mark_domain_in_triangulation.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/draw_triangulation_2.h>
#include "polygon_triangulation_drawing_functor.h"
#include <iostream>
#include <unordered_map>
@ -57,6 +58,7 @@ int main( )
assert(count > 0);
assert(count < cdt.number_of_faces());
CGAL::draw(cdt, in_domain);
CGAL::Polygon_triangulation_drawing_functor<CDT> myfunctor(in_domain);
CGAL::draw(cdt, myfunctor);
return 0;
}

View File

@ -0,0 +1,88 @@
// Copyright(c) 2022 GeometryFactory (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL$
// $Id$
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Andreas Fabri
#ifndef CGAL_POLYGON_TRIANGULATION_DRAWING_FUNCTOR_H
#define CGAL_POLYGON_TRIANGULATION_DRAWING_FUNCTOR_H
#include <CGAL/license/Triangulation_2.h>
#include <CGAL/Drawing_functor.h>
#include <CGAL/Random.h>
#include <CGAL/draw_triangulation_2.h>
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
namespace CGAL
{
template<class PT>
struct Polygon_triangulation_drawing_functor :
public CGAL::Drawing_functor<PT,
typename PT::Vertex_handle,
typename PT::Finite_edges_iterator,
typename PT::Finite_faces_iterator>
{
template<class IPM>
Polygon_triangulation_drawing_functor(IPM ipm)
{
this->colored_face =
[](const PT&, const typename PT::Finite_faces_iterator) -> bool
{ return true; };
this->face_color =
[](const PT&, const typename PT::Finite_faces_iterator fh) -> CGAL::IO::Color
{
CGAL::Random random((unsigned int)(std::size_t)(&*fh));
return get_random_color(random);
};
this->draw_face=
[ipm](const PT&, const typename PT::Finite_faces_iterator fh) -> bool
{ return get(ipm, fh); };
this->draw_edge=
[ipm](const PT& pt, const typename PT::Finite_edges_iterator eh) -> bool
{
typename PT::Face_handle fh1=eh->first;
typename PT::Face_handle fh2=pt.mirror_edge(*eh).first;
return get(ipm, fh1) || get(ipm, fh2);
};
}
};
#define CGAL_CT2_TYPE CGAL::Constrained_Delaunay_triangulation_2<K, TDS, Itag>
template <class K, class TDS, typename Itag,
typename BufferType=float, class DrawingFunctor>
void add_in_graphic_buffer(const CGAL_CT2_TYPE& ct2,
CGAL::Graphic_buffer<BufferType>& graphic_buffer,
const DrawingFunctor& drawing_functor)
{
draw_function_for_t2::compute_elements(ct2, graphic_buffer, drawing_functor);
}
#ifdef CGAL_USE_BASIC_VIEWER
// Specialization of draw function.
template <class K, class TDS, typename Itag, class DrawingFunctor>
void draw(const CGAL_CT2_TYPE &ct2, const DrawingFunctor &drawingfunctor,
const char *title="Constrained Triangulation_2 Basic Viewer")
{
CGAL::Graphic_buffer<float> buffer;
add_in_graphic_buffer(ct2, buffer, drawingfunctor);
draw_buffer(buffer, title);
}
#endif // CGAL_USE_BASIC_VIEWER
#undef CGAL_CT2_TYPE
}; // end namespace CGAL
#endif // CGAL_POLYGON_TRIANGULATION_DRAWING_FUNCTOR_H

View File

@ -1,180 +0,0 @@
// Copyright(c) 2022 GeometryFactory (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL$
// $Id$
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Andreas Fabri
#ifndef CGAL_DRAW_CT2_H
#define CGAL_DRAW_CT2_H
#include <CGAL/license/Triangulation_2.h>
#include <CGAL/Qt/Basic_viewer_qt.h>
#include <CGAL/Constrained_triangulation_2.h>
#include <CGAL/Triangulation_2/internal/In_domain.h>
#ifdef CGAL_USE_BASIC_VIEWER
#include <CGAL/Qt/init_ogl_context.h>
namespace CGAL
{
// Viewer class for T2
template<class T2, class InDomainPmap>
class SimpleConstrainedTriangulation2ViewerQt : public Basic_viewer_qt
{
typedef Basic_viewer_qt Base;
typedef typename T2::Vertex_handle Vertex_const_handle;
typedef typename T2::Finite_edges_iterator Edge_const_handle;
typedef typename T2::Finite_faces_iterator Facet_const_handle;
typedef typename T2::Point Point;
public:
/// Construct the viewer.
/// @param at2 the t2 to view
/// @param title the title of the window
/// @param anofaces if true, do not draw faces (faces are not computed; this can be
/// usefull for very big object where this time could be long)
SimpleConstrainedTriangulation2ViewerQt(QWidget* parent, const T2& at2,
InDomainPmap ipm,
const char* title="Basic CDT2 Viewer",
bool anofaces=false) :
// First draw: vertices; edges, faces; multi-color; no inverse normal
Base(parent, title, true, true, true, false, false),
t2(at2),
ipm(ipm),
m_nofaces(anofaces)
{
compute_elements();
}
protected:
void compute_face(Facet_const_handle fh)
{
CGAL::IO::Color c = get(ipm, fh)? CGAL::yellow() : CGAL::white();
face_begin(c);
add_point_in_face(fh->vertex(0)->point());
add_point_in_face(fh->vertex(1)->point());
add_point_in_face(fh->vertex(2)->point());
face_end();
}
void compute_edge(Edge_const_handle eh)
{
CGAL::IO::Color c = t2.is_constrained(*eh)? CGAL::green() : CGAL::black();
add_segment(eh->first->vertex(eh->first->cw(eh->second))->point(),
eh->first->vertex(eh->first->ccw(eh->second))->point(),
c);
}
void compute_vertex(Vertex_const_handle vh)
{ add_point(vh->point()); }
void compute_elements()
{
clear();
if (!m_nofaces)
{
for (typename T2::Finite_faces_iterator it=t2.finite_faces_begin();
it!=t2.finite_faces_end(); ++it)
{ compute_face(it); }
}
for (typename T2::Finite_edges_iterator it=t2.finite_edges_begin();
it!=t2.finite_edges_end(); ++it)
{ compute_edge(it); }
for (typename T2::Finite_vertices_iterator it=t2.finite_vertices_begin();
it!=t2.finite_vertices_end(); ++it)
{ compute_vertex(it); }
}
virtual void keyPressEvent(QKeyEvent *e)
{
// Test key pressed:
// const ::Qt::KeyboardModifiers modifiers = e->modifiers();
// if ((e->key()==Qt::Key_PageUp) && (modifiers==Qt::NoButton)) { ... }
// Call: * compute_elements() if the model changed, followed by
// * redraw() if some viewing parameters changed that implies some
// modifications of the buffers
// (eg. type of normal, color/mono)
// * update() just to update the drawing
// Call the base method to process others/classicals key
Base::keyPressEvent(e);
}
protected:
const T2& t2;
InDomainPmap ipm;
bool m_nofaces;
};
// Specialization of draw function.
#define CGAL_T2_TYPE CGAL::Constrained_triangulation_2<Gt, Tds, Itag>
template<class Gt, class Tds, class Itag, class InDomainPmap>
void draw(const CGAL_T2_TYPE& at2,
InDomainPmap ipm)
{
const char* title="Constrained_triangulation_2 Basic Viewer";
bool nofill=false;
#if defined(CGAL_TEST_SUITE)
bool cgal_test_suite=true;
#else
bool cgal_test_suite=qEnvironmentVariableIsSet("CGAL_TEST_SUITE");
#endif
if (!cgal_test_suite)
{
CGAL::Qt::init_ogl_context(4,3);
int argc=1;
const char* argv[2]={"t2_viewer", nullptr};
QApplication app(argc,const_cast<char**>(argv));
SimpleConstrainedTriangulation2ViewerQt<CGAL_T2_TYPE, InDomainPmap>
mainwindow(app.activeWindow(), at2, ipm, title, nofill);
mainwindow.show();
app.exec();
}
}
template<class Gt, class Tds, class Itag>
void draw(const CGAL_T2_TYPE& at2)
{
internal::In_domain<CGAL_T2_TYPE> in_domain;
draw(at2, in_domain);
}
#undef CGAL_T2_TYPE
} // End namespace CGAL
#else
namespace CGAL {
// Specialization of draw function.
#define CGAL_T2_TYPE CGAL::Constrained_triangulation_2<Gt, Tds, Itag>
template<class Gt, class Tds, class Itag, class InDomainPmap>
void draw(const CGAL_T2_TYPE& ,
InDomainPmap )
{}
#undef CGAL_T2_TYPE
} // End namespace CGAL
#endif // CGAL_USE_BASIC_VIEWER
#endif // CGAL_DRAW_CT2_H