diff --git a/Polygon/doc/Polygon/Polygon.txt b/Polygon/doc/Polygon/Polygon.txt index 44e90d92af8..a268bfac755 100644 --- a/Polygon/doc/Polygon/Polygon.txt +++ b/Polygon/doc/Polygon/Polygon.txt @@ -70,6 +70,18 @@ and 3D Linear Geometric %Kernel. \cgalExample{Polygon/projected_polygon.cpp} +\subsection subsecPolygonDraw Draw a Polygon + +A polygon can be visualized by calling the `CGAL::draw()` function as shown in the following example. This function opens a new window showing the given polygon. The function is blocking, that is the program continues as soon as the user closes the window. + +\cgalExample{Polygon/draw_polygon.cpp} + +This function requires CGAL_Qt5, and is only available if the flag CGAL_USE_BASIC_VIEWER is defined at compile time. + +\cgalFigureBegin{fig_draw_polygon,draw_polygon.png} +Result of the run of the draw_polygon program. A window shows the polygon and allows to navigate through the scene. +\cgalFigureEnd + */ \section secPolygonWithHole Polygons with Holes diff --git a/Polygon/doc/Polygon/examples.txt b/Polygon/doc/Polygon/examples.txt index 57e9d79494f..9c85508b860 100644 --- a/Polygon/doc/Polygon/examples.txt +++ b/Polygon/doc/Polygon/examples.txt @@ -3,4 +3,5 @@ \example Polygon/polygon_algorithms.cpp \example Polygon/Polygon.cpp \example Polygon/projected_polygon.cpp +\example Polygon/draw_polygon.cpp */ diff --git a/Polygon/doc/Polygon/fig/draw_polygon.png b/Polygon/doc/Polygon/fig/draw_polygon.png new file mode 100644 index 00000000000..6da8e0ae0c9 Binary files /dev/null and b/Polygon/doc/Polygon/fig/draw_polygon.png differ diff --git a/Polygon/examples/Polygon/CMakeLists.txt b/Polygon/examples/Polygon/CMakeLists.txt index 19bae0815b9..a3e75a199ff 100644 --- a/Polygon/examples/Polygon/CMakeLists.txt +++ b/Polygon/examples/Polygon/CMakeLists.txt @@ -6,7 +6,11 @@ project( Polygon_Examples ) cmake_minimum_required(VERSION 2.8.10) -find_package(CGAL QUIET) +find_package(CGAL QUIET COMPONENTS Qt5) + +if(CGAL_Qt5_FOUND) + add_definitions(-DCGAL_USE_BASIC_VIEWER -DQT_NO_KEYWORDS) +endif() if ( CGAL_FOUND ) @@ -22,6 +26,10 @@ if ( CGAL_FOUND ) create_single_source_cgal_program( "${cppfile}" ) endforeach() + if(CGAL_Qt5_FOUND) + target_link_libraries(draw_polygon PUBLIC CGAL::CGAL_Qt5) + endif() + else() message(STATUS "This program requires the CGAL library, and will not be compiled.") diff --git a/Polygon/examples/Polygon/draw_polygon.cpp b/Polygon/examples/Polygon/draw_polygon.cpp new file mode 100644 index 00000000000..072e41387e3 --- /dev/null +++ b/Polygon/examples/Polygon/draw_polygon.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K; +typedef CGAL::Polygon_2 Polygon_2; +typedef CGAL::Point_2 Point; + +int main() +{ + // create a polygon and put some points in it + Polygon_2 p; + p.push_back(Point(0,0)); + p.push_back(Point(4,0)); + p.push_back(Point(4,4)); + p.push_back(Point(2,2)); + p.push_back(Point(0,4)); + + CGAL::draw(p); + + return EXIT_SUCCESS; +} diff --git a/Polygon/include/CGAL/draw_polygon_2.h b/Polygon/include/CGAL/draw_polygon_2.h new file mode 100644 index 00000000000..0088deefd58 --- /dev/null +++ b/Polygon/include/CGAL/draw_polygon_2.h @@ -0,0 +1,131 @@ +// Copyright (c) 1997 +// Utrecht University (The Netherlands), +// ETH Zurich (Switzerland), +// INRIA Sophia-Antipolis (France), +// Max-Planck-Institute Saarbruecken (Germany), +// and Tel-Aviv University (Israel). All rights reserved. +// +// This file is part of CGAL (www.cgal.org); you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 3 of the License, +// or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: LGPL-3.0+ +// +// +// Author(s) : Guillaume Damiand + +#ifndef CGAL_DRAW_POLYGON_2_H +#define CGAL_DRAW_POLYGON_2_H + +#include + +#ifdef CGAL_USE_BASIC_VIEWER + +#include +#include + +namespace CGAL +{ + +// Viewer class for Polygon_2 +template +class SimplePolygon2ViewerQt : public Basic_viewer_qt +{ + typedef Basic_viewer_qt Base; + typedef typename P2::Point_2 Point; + +public: + /// Construct the viewer. + /// @param ap2 the polygon to view + /// @param title the title of the window + SimplePolygon2ViewerQt(QWidget* parent, const P2& ap2, + const char* title="Basic Polygon_2 Viewer") : + // First draw: vertices; edges, faces; multi-color; no inverse normal + Base(parent, title, true, true, true, false, false), + p2(ap2) + { + compute_elements(); + } + +protected: + void compute_elements() + { + clear(); + + if (p2.is_empty()) return; + + Point prev=p2.vertex(p2.size()-1); + + CGAL::Color c(75,160,255); + face_begin(c); + + for (typename P2::Vertex_const_iterator i=p2.vertices_begin(); + i!=p2.vertices_end(); ++i) + { + add_point(*i); // Add vertex + add_segment(prev, *i); // Add segment with previous point + add_point_in_face(*i); // Add point in face + prev=*i; + } + + face_end(); + } + + 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 P2& p2; +}; + +// Specialization of draw function. +template +void draw(const CGAL::Polygon_2& ap2, + const char* title="Polygon_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) + { + int argc=1; + const char* argv[2]={"t2_viewer","\0"}; + QApplication app(argc,const_cast(argv)); + SimplePolygon2ViewerQt > + mainwindow(app.activeWindow(), ap2, title); + mainwindow.show(); + app.exec(); + } +} + +} // End namespace CGAL + +#endif // CGAL_USE_BASIC_VIEWER + +#endif // CGAL_DRAW_POLYGON_2_H