Add basic viewer for polygon.

This commit is contained in:
Guillaume Damiand 2018-09-07 14:23:14 +02:00
parent 4afa5e9cb5
commit eb6354425f
6 changed files with 175 additions and 1 deletions

View File

@ -70,6 +70,18 @@ and 3D Linear Geometric %Kernel.
\cgalExample{Polygon/projected_polygon.cpp} \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 \section secPolygonWithHole Polygons with Holes

View File

@ -3,4 +3,5 @@
\example Polygon/polygon_algorithms.cpp \example Polygon/polygon_algorithms.cpp
\example Polygon/Polygon.cpp \example Polygon/Polygon.cpp
\example Polygon/projected_polygon.cpp \example Polygon/projected_polygon.cpp
\example Polygon/draw_polygon.cpp
*/ */

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -6,7 +6,11 @@ project( Polygon_Examples )
cmake_minimum_required(VERSION 2.8.10) 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 ) if ( CGAL_FOUND )
@ -22,6 +26,10 @@ if ( CGAL_FOUND )
create_single_source_cgal_program( "${cppfile}" ) create_single_source_cgal_program( "${cppfile}" )
endforeach() endforeach()
if(CGAL_Qt5_FOUND)
target_link_libraries(draw_polygon PUBLIC CGAL::CGAL_Qt5)
endif()
else() else()
message(STATUS "This program requires the CGAL library, and will not be compiled.") message(STATUS "This program requires the CGAL library, and will not be compiled.")

View File

@ -0,0 +1,22 @@
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/draw_polygon_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Point_2<K> 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;
}

View File

@ -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 <guillaume.damiand@liris.cnrs.fr>
#ifndef CGAL_DRAW_POLYGON_2_H
#define CGAL_DRAW_POLYGON_2_H
#include <CGAL/Qt/Basic_viewer_qt.h>
#ifdef CGAL_USE_BASIC_VIEWER
#include <CGAL/Polygon_2.h>
#include <CGAL/Random.h>
namespace CGAL
{
// Viewer class for Polygon_2
template<class P2>
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<class T, class C>
void draw(const CGAL::Polygon_2<T, C>& 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<char**>(argv));
SimplePolygon2ViewerQt<CGAL::Polygon_2<T, C> >
mainwindow(app.activeWindow(), ap2, title);
mainwindow.show();
app.exec();
}
}
} // End namespace CGAL
#endif // CGAL_USE_BASIC_VIEWER
#endif // CGAL_DRAW_POLYGON_2_H