Add a Qt4 based demo for the Generator package

This commit is contained in:
Andreas Fabri 2010-06-16 09:46:51 +00:00
parent e17399e9cd
commit 169964dede
6 changed files with 620 additions and 0 deletions

3
.gitattributes vendored
View File

@ -1297,6 +1297,9 @@ GraphicsView/TODO -text
GraphicsView/demo/Circular_kernel_2/Circular_kernel_2.qrc -text
GraphicsView/demo/Circular_kernel_2/Circular_kernel_2.ui -text
GraphicsView/demo/Circular_kernel_2/arcs.arc -text
GraphicsView/demo/Generator/Generator_2.qrc -text
GraphicsView/demo/Generator/Generator_2.ui -text
GraphicsView/demo/Generator/about_Generator_2.html svneol=native#text/html
GraphicsView/demo/Polygon/Polygon_2.qrc -text
GraphicsView/demo/Polygon/Polygon_2.ui -text
GraphicsView/demo/Segment_Delaunay_graph_2/Segment_voronoi_2.qrc -text

View File

@ -0,0 +1,56 @@
# Created by the script cgal_create_cmake_script
# This is the CMake script for compiling a CGAL application.
project (GeneratorDemo)
cmake_minimum_required(VERSION 2.4.5)
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
if ( COMMAND cmake_policy )
cmake_policy( SET CMP0003 NEW )
endif()
find_package(CGAL COMPONENTS Qt4)
include(${CGAL_USE_FILE})
set( QT_USE_QTXML TRUE )
set( QT_USE_QTMAIN TRUE )
set( QT_USE_QTSCRIPT TRUE )
set( QT_USE_QTOPENGL TRUE )
find_package(Qt4)
include_directories (BEFORE ../../include)
if ( CGAL_FOUND AND CGAL_Qt4_FOUND AND QT4_FOUND )
include(${QT_USE_FILE})
#--------------------------------
# Demo: Generator_2
#--------------------------------
# UI files (Qt Designer files)
qt4_wrap_ui( DT_UI_FILES Generator_2.ui )
# qrc files (resources files, that contain icons, at least)
qt4_add_resources ( DT_RESOURCE_FILES ./Generator_2.qrc )
# use the Qt MOC preprocessor on classes that derives from QObject
qt4_generate_moc( Generator_2.cpp Generator_2.moc )
# The executable itself.
add_executable ( Generator_2 Generator_2.cpp Generator_2.moc ${DT_UI_FILES} ${DT_RESOURCE_FILES} )
add_to_cached_list( CGAL_EXECUTABLE_TARGETS Generator_2 )
# Link with Qt libraries
target_link_libraries( Generator_2 ${QT_LIBRARIES} )
# And with CGAL libraries
target_link_libraries( Generator_2 ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} )
else()
message(STATUS "NOTICE: This demo requires CGAL, and Qt4, and will not be compiled.")
endif()

View File

@ -0,0 +1,293 @@
#include <fstream>
// CGAL headers
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/point_generators_2.h>
#include <CGAL/function_objects.h>
#include <CGAL/Join_input_iterator.h>
#include <CGAL/algorithm.h>
// Qt headers
#include <QtGui>
#include <QString>
#include <QFileDialog>
#include <QGraphicsLineItem>
// GraphicsView items and event filters (input classes)
#include <CGAL/Qt/PointsGraphicsItem.h>
#include <CGAL/Qt/utility.h>
#include <CGAL/Qt/SegmentsGraphicsItem.h>
// the two base classes
#include "ui_Generator_2.h"
#include <CGAL/Qt/DemosMainWindow.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef K::Vector_2 Vector_2;
typedef K::Segment_2 Segment_2;
typedef K::Iso_rectangle_2 Iso_rectangle_2;
class MainWindow :
public CGAL::Qt::DemosMainWindow,
public Ui::Generator_2
{
Q_OBJECT
private:
CGAL::Qt::Converter<K> convert;
std::vector<Point_2> points;
std::vector<Segment_2> segments;
QGraphicsScene scene;
CGAL::Qt::PointsGraphicsItem<std::vector<Point_2> > * pgi;
CGAL::Qt::SegmentsGraphicsItem<std::vector<Segment_2> > * sgi;
template <typename G>
void
on_actionGenerate_triggered()
{
QRectF rect = CGAL::Qt::viewportsBbox(&scene);
CGAL::Qt::Converter<K> convert;
Iso_rectangle_2 isor = convert(rect);
Point_2 center = CGAL::midpoint(isor[0], isor[2]);
Vector_2 offset = center - CGAL::ORIGIN;
double w = isor.xmax() - isor.xmin();
double h = isor.ymax() - isor.ymin();
double radius = (w<h) ? w/2 : h/2;
G pg(radius);
bool ok = false;
const int number_of_points =
QInputDialog::getInteger(this,
tr("Number of random points"),
tr("Enter number of random points"),
100,
0,
std::numeric_limits<int>::max(),
1,
&ok);
if(!ok) {
return;
}
// wait cursor
QApplication::setOverrideCursor(Qt::WaitCursor);
points.reserve(points.size() + number_of_points);
for(int i = 0; i < number_of_points; ++i){
points.push_back(*pg + offset);
++pg;
}
// default cursor
QApplication::restoreOverrideCursor();
emit(changed());
}
public:
MainWindow();
public slots:
void on_actionClear_triggered();
void on_actionRecenter_triggered();
void on_actionGeneratePointsOnCircle_triggered();
void on_actionGeneratePointsInSquare_triggered();
void on_actionGeneratePointsInDisc_triggered();
void on_actionGenerateSegments_triggered();
void on_actionGenerateSegmentFans_triggered();
void clear();
signals:
void changed();
};
MainWindow::MainWindow()
: DemosMainWindow()
{
setupUi(this);
// Add a GraphicItem for the point set
pgi = new CGAL::Qt::PointsGraphicsItem<std::vector<Point_2> >(&points);
sgi = new CGAL::Qt::SegmentsGraphicsItem<std::vector<Segment_2> >(&segments);
QObject::connect(this, SIGNAL(changed()),
pgi, SLOT(modelChanged()));
QObject::connect(this, SIGNAL(changed()),
sgi, SLOT(modelChanged()));
pgi->setVerticesPen(QPen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
sgi->setVerticesPen(QPen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
scene.addItem(pgi);
scene.addItem(sgi);
//
// Manual handling of actions
//
QObject::connect(this->actionQuit, SIGNAL(triggered()),
this, SLOT(close()));
//
// Setup the scene and the view
//
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
scene.setSceneRect(-100, -100, 100, 100);
this->graphicsView->setScene(&scene);
// Uncomment the following line to get antialiasing by default.
// actionUse_Antialiasing->setChecked(true);
// Turn the vertical axis upside down
this->graphicsView->scale(1, -1);
// The navigation adds zooming and translation functionality to the
// QGraphicsView
this->addNavigation(this->graphicsView);
this->setupStatusBar();
this->setupOptionsMenu();
this->addAboutDemo(":/cgal/help/about_Generator_2.html");
this->addAboutCGAL();
}
/*
* Qt Automatic Connections
* http://doc.trolltech.com/4.4/designer-using-a-component.html#automatic-connections
*
* setupUi(this) generates connections to the slots named
* "on_<action_name>_<signal_name>"
*/
void
MainWindow::on_actionClear_triggered()
{
clear();
emit(changed());
}
void
MainWindow::on_actionRecenter_triggered()
{
this->graphicsView->setSceneRect(pgi->boundingRect());
this->graphicsView->fitInView(pgi->boundingRect(), Qt::KeepAspectRatio);
}
void
MainWindow::on_actionGeneratePointsOnCircle_triggered()
{
typedef CGAL::Random_points_on_circle_2<Point_2> Generator;
on_actionGenerate_triggered<Generator>();
}
void
MainWindow::on_actionGeneratePointsInSquare_triggered()
{
typedef CGAL::Random_points_in_square_2<Point_2> Generator;
on_actionGenerate_triggered<Generator>();
}
void
MainWindow::on_actionGeneratePointsInDisc_triggered()
{
typedef CGAL::Random_points_in_disc_2<Point_2> Generator;
on_actionGenerate_triggered<Generator>();
}
void
MainWindow::on_actionGenerateSegments_triggered()
{
segments.reserve(segments.size() + 200);
// Prepare point generator for the horizontal segment, length 200.
typedef CGAL::Random_points_on_segment_2<Point_2> Rpos_generator;
Rpos_generator rpos( Point_2(-100,0), Point_2(100,0));
// Prepare point generator for random points on circle, radius 250.
typedef CGAL::Random_points_on_circle_2<Point_2> Rpoc_generator;
Rpoc_generator rpoc( 250);
// Create 200 segments.
typedef CGAL::Creator_uniform_2< Point_2, Segment_2> Seg_creator;
typedef CGAL::Join_input_iterator_2< Rpos_generator, Rpoc_generator, Seg_creator> Seg_iterator;
Seg_iterator g( rpos, rpoc);
CGAL::copy_n( g, 200, std::back_inserter(segments));
emit(changed());
}
void
MainWindow::on_actionGenerateSegmentFans_triggered()
{
typedef CGAL::Points_on_segment_2<Point_2> PG;
typedef CGAL::Creator_uniform_2< Point_2, Segment_2> Creator;
typedef CGAL::Join_input_iterator_2< PG, PG, Creator> Segm_iterator;
typedef CGAL::Counting_iterator<Segm_iterator,Segment_2> Count_iterator;
segments.reserve(segments.size() + 100);
// A horizontal like fan.
PG p1( Point_2(-250, -50), Point_2(-250, 50),50); // Point generator.
PG p2( Point_2( 250,-250), Point_2( 250,250),50);
Segm_iterator t1( p1, p2); // Segment generator.
Count_iterator t1_begin( t1); // Finite range.
Count_iterator t1_end( 50);
std::copy( t1_begin, t1_end, std::back_inserter(segments));
// A vertical like fan.
PG p3( Point_2( -50,-250), Point_2( 50,-250),50);
PG p4( Point_2(-250, 250), Point_2( 250, 250),50);
Segm_iterator t2( p3, p4);
Count_iterator t2_begin( t2);
Count_iterator t2_end( 50);
std::copy( t2_begin, t2_end, std::back_inserter(segments));
emit(changed());
}
void
MainWindow::clear()
{
points.clear();
segments.clear();
}
#include "Generator_2.moc"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
app.setOrganizationDomain("geometryfactory.com");
app.setOrganizationName("GeometryFactory");
app.setApplicationName("Generator_2 demo");
// Import resources from libCGALQt4.
// See http://doc.trolltech.com/4.4/qdir.html#Q_INIT_RESOURCE
Q_INIT_RESOURCE(File);
Q_INIT_RESOURCE(Generator_2);
Q_INIT_RESOURCE(Input);
Q_INIT_RESOURCE(CGAL);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}

View File

@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/cgal/help" lang="en" >
<file alias="about_CGAL.html" >../resources/about_CGAL.html</file>
<file>about_Generator_2.html</file>
</qresource>
</RCC>

View File

@ -0,0 +1,252 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author>GeometryFactory</author>
<class>Generator_2</class>
<widget class="QMainWindow" name="Generator_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>568</width>
<height>325</height>
</rect>
</property>
<property name="windowTitle">
<string>CGAL 2D Generator</string>
</property>
<property name="windowIcon">
<iconset resource="../resources/CGAL.qrc">
<normaloff>:/cgal/logos/cgal_icon</normaloff>:/cgal/logos/cgal_icon</iconset>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QGraphicsView" name="graphicsView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>2</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="transformationAnchor">
<enum>QGraphicsView::NoAnchor</enum>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<widget class="QToolBar" name="fileToolBar">
<property name="windowTitle">
<string>File Tools</string>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionClear"/>
</widget>
<widget class="QToolBar" name="toolBar">
<property name="windowTitle">
<string>Visualization Tools</string>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionRecenter"/>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>568</width>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>&amp;File</string>
</property>
<addaction name="separator"/>
<addaction name="actionClear"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
<widget class="QMenu" name="menuTools">
<property name="title">
<string>&amp;Algorithms</string>
</property>
<addaction name="separator"/>
<addaction name="actionRecenter"/>
<addaction name="actionGeneratePointsOnCircle"/>
<addaction name="actionGeneratePointsInDisc"/>
<addaction name="actionGeneratePointsInSquare"/>
<addaction name="actionGenerateSegments"/>
<addaction name="actionGenerateSegmentFans"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuTools"/>
</widget>
<action name="actionAbout">
<property name="text">
<string>&amp;About</string>
</property>
</action>
<action name="actionAboutCGAL">
<property name="text">
<string>About &amp;CGAL</string>
</property>
</action>
<action name="actionQuit">
<property name="text">
<string>&amp;Quit</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
</action>
<action name="actionClear">
<property name="icon">
<iconset resource="../icons/File.qrc">
<normaloff>:/cgal/fileToolbar/fileNew.png</normaloff>:/cgal/fileToolbar/fileNew.png</iconset>
</property>
<property name="text">
<string>&amp;Clear</string>
</property>
<property name="shortcut">
<string>Ctrl+C</string>
</property>
</action>
<action name="actionLoadGenerator">
<property name="icon">
<iconset resource="../icons/File.qrc">
<normaloff>:/cgal/fileToolbar/fileOpen.png</normaloff>:/cgal/fileToolbar/fileOpen.png</iconset>
</property>
<property name="text">
<string>&amp;Load Generator</string>
</property>
<property name="shortcut">
<string>Ctrl+L</string>
</property>
</action>
<action name="actionSaveGenerator">
<property name="icon">
<iconset resource="../icons/File.qrc">
<normaloff>:/cgal/fileToolbar/fileSave.png</normaloff>:/cgal/fileToolbar/fileSave.png</iconset>
</property>
<property name="text">
<string>&amp;Save Generator</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action>
<action name="actionRecenter">
<property name="icon">
<iconset resource="../icons/Input.qrc">
<normaloff>:/cgal/Input/zoom-best-fit</normaloff>:/cgal/Input/zoom-best-fit</iconset>
</property>
<property name="text">
<string>Re&amp;center the viewport</string>
</property>
<property name="shortcut">
<string>Ctrl+R</string>
</property>
</action>
<action name="actionYMonotonePartition">
<property name="text">
<string>Y-monotone Partition</string>
</property>
</action>
<action name="actionCreateInputGenerator">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Create Input Generator</string>
</property>
</action>
<action name="actionInnerSkeleton">
<property name="text">
<string>Inner Skeleton</string>
</property>
</action>
<action name="actionOuterOffset">
<property name="text">
<string>Outer Offset</string>
</property>
</action>
<action name="actionOptimalConvexPartition">
<property name="text">
<string>Optimal Convex Partition</string>
</property>
</action>
<action name="actionApproximateConvexPartition">
<property name="text">
<string>Approximate Convex Partition</string>
</property>
</action>
<action name="actionLinearLeastSquaresFitting">
<property name="text">
<string>Linear Least Squares Fitting of Points</string>
</property>
</action>
<action name="actionLinearLeastSquaresFittingOfSegments">
<property name="text">
<string>Linear Least Squares Fitting of Segments</string>
</property>
</action>
<action name="actionGeneratePointsOnCircle">
<property name="text">
<string>Generate Points on Circle</string>
</property>
</action>
<action name="actionGeneratePointsInSquare">
<property name="text">
<string>Generate Points in Square</string>
</property>
</action>
<action name="actionGenerateSegments">
<property name="text">
<string>Generate Segments</string>
</property>
</action>
<action name="actionGeneratePointsInDisc">
<property name="text">
<string>Generate Points in Disc</string>
</property>
</action>
<action name="actionGenerateSegmentFans">
<property name="text">
<string>Generate Segment Fans</string>
</property>
</action>
</widget>
<resources>
<include location="Generator_2.qrc"/>
<include location="../icons/File.qrc"/>
<include location="../resources/CGAL.qrc"/>
<include location="../icons/Input.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -0,0 +1,10 @@
<html>
<body>
<h2>2D Object Generators</h2>
<p>Copyright &copy; 2010 GeometryFactory</p>
<p>This application illustrates the 2D object generators
of <a href="http://www.cgal.org/">CGAL</a></p>
<p>See also <a href="http://www.cgal.org/Pkg/Generator">the online
manual</a>.</p>
</body>
</html>