add examples of drawing of polygon with holes

This commit is contained in:
Guillaume Damiand 2024-03-28 17:41:43 +01:00
parent ff4a201b2a
commit 70d398b692
3 changed files with 64 additions and 15 deletions

View File

@ -18,5 +18,6 @@ endforeach()
if(CGAL_Qt6_FOUND)
target_link_libraries(draw_polygon PUBLIC CGAL::CGAL_Basic_viewer)
target_link_libraries(draw_polygon_with_holes PUBLIC CGAL::CGAL_Basic_viewer)
target_link_libraries(draw_polygon_with_holes_2 PUBLIC CGAL::CGAL_Basic_viewer)
target_link_libraries(draw_multipolygon_with_holes PUBLIC CGAL::CGAL_Basic_viewer)
endif()

View File

@ -0,0 +1,46 @@
/*! \file draw_polygon_with_holes_2.cpp
*/
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/draw_polygon_with_holes_2.h>
using K=CGAL::Exact_predicates_inexact_constructions_kernel;
using Point_2=K::Point_2;
using Polygon_2=CGAL::Polygon_2<K>;
using Polygon_with_holes_2=CGAL::Polygon_with_holes_2<K>;
Polygon_2 triangle(double x, double y)
{ // Create a triangle (x, y)
Polygon_2 P;
P.push_back(Point_2(x, y));
P.push_back(Point_2(x+0.5, y+1));
P.push_back(Point_2(x+1, y));
return P;
}
Polygon_2 rectangle(double l)
{ // Create a rectangle with given side length.
Polygon_2 P;
P.push_back(Point_2(0, 0));
P.push_back(Point_2(l, 0));
P.push_back(Point_2(l, l));
P.push_back(Point_2(0, l));
return P;
}
int main()
{
// Create a large rectangle A, with 3 triangle holes
Polygon_with_holes_2 A(rectangle(4));
Polygon_2 H1(triangle(1, 1));
Polygon_2 H2(triangle(2, 1));
Polygon_2 H3(triangle(1.5, 2));
A.add_hole(H1);
A.add_hole(H2);
A.add_hole(H3);
CGAL::draw(A);
return 0;
}

View File

@ -18,7 +18,7 @@
#ifndef CGAL_DRAW_MULTIPOLYGON_WITH_HOLES_2_H
#define CGAL_DRAW_MULTIPOLYGON_WITH_HOLES_2_H
#include <CGAL/Qt/Basic_viewer_qt.h>
#include <CGAL/Qt/Basic_viewer.h>
#ifdef DOXYGEN_RUNNING
namespace CGAL {
@ -53,8 +53,8 @@ namespace CGAL {
// Viewer class for Multipolygon_with_holes_2
template <typename Multipolygon>
class Mpwh_2_basic_viewer_qt : public Basic_viewer_qt {
using Base = Basic_viewer_qt;
class Mpwh_2_basic_viewer_qt : public Qt::Basic_viewer {
using Base = Qt::Basic_viewer;
using Mpwh = Multipolygon;
using Pwh = typename Mpwh::Polygon_with_holes_2;
using Pgn = typename Mpwh::Polygon_2;
@ -67,7 +67,7 @@ public:
/// @param title the title of the window
Mpwh_2_basic_viewer_qt(QWidget* parent, const Mpwh& mpwh,
const char* title = "Basic Multipolygon_with_holes_2 Viewer") :
Base(parent, title, true, true, true, false, false),
Base(parent, gs, title, true, true, true, false, false),
m_mpwh(mpwh) {
if (mpwh.number_of_polygons_with_holes() == 0) return;
@ -109,11 +109,11 @@ public:
/*! Compute the elements of a multipolygon with holes.
*/
void add_elements() {
clear();
gs.clear();
for (auto const& p: m_mpwh.polygons_with_holes()) {
CGAL::IO::Color c(rand()%255,rand()%255,rand()%255);
face_begin(c);
gs.face_begin(c);
const Pnt* point_in_face;
const auto& outer_boundary = p.outer_boundary();
@ -122,10 +122,10 @@ public:
for (auto it = p.holes_begin(); it != p.holes_end(); ++it) {
compute_loop(*it, true);
add_point_in_face(*point_in_face);
gs.add_point_in_face(*point_in_face);
}
face_end();
gs.face_end();
}
}
@ -133,21 +133,21 @@ protected:
/*! Compute the face
*/
void compute_loop(const Pgn& p, bool hole) {
if (hole) add_point_in_face(p.vertex(p.size()-1));
if (hole) gs.add_point_in_face(p.vertex(p.size()-1));
auto prev = p.vertices_begin();
auto it = prev;
add_point(*it);
add_point_in_face(*it);
gs.add_point(*it);
gs.add_point_in_face(*it);
for (++it; it != p.vertices_end(); ++it) {
add_segment(*prev, *it); // add segment with previous point
add_point(*it);
add_point_in_face(*it); // add point in face
gs.add_segment(*prev, *it); // add segment with previous point
gs.add_point(*it);
gs.add_point_in_face(*it); // add point in face
prev = it;
}
// Add the last segment between the last point and the first one
add_segment(*prev, *(p.vertices_begin()));
gs.add_segment(*prev, *(p.vertices_begin()));
}
virtual void keyPressEvent(QKeyEvent* e) {
@ -166,6 +166,8 @@ protected:
}
private:
Graphics_scene gs;
//! The window width in pixels.
int m_width = CGAL_BASIC_VIEWER_INIT_SIZE_X;