Qt4 based largest empty iso rectangle demo

This commit is contained in:
Andreas Fabri 2010-08-24 17:11:41 +00:00
parent a5f3b43834
commit 4ff8be1faa
6 changed files with 573 additions and 0 deletions

3
.gitattributes vendored
View File

@ -1432,6 +1432,9 @@ 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/Largest_empty_rect_2/Largest_empty_rectangle_2.qrc -text
GraphicsView/demo/Largest_empty_rect_2/Largest_empty_rectangle_2.ui -text
GraphicsView/demo/Largest_empty_rect_2/about_Largest_empty_rectangle_2.html svneol=native#text/html
GraphicsView/demo/Polygon/Polygon_2.qrc -text
GraphicsView/demo/Polygon/Polygon_2.ui -text
GraphicsView/demo/Polygon/unweighted_polygon.poly -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 (Largest_empty_rectangleDemo)
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: Largest_empty_rectangle_2
#--------------------------------
# UI files (Qt Designer files)
qt4_wrap_ui( DT_UI_FILES Largest_empty_rectangle_2.ui )
# qrc files (resources files, that contain icons, at least)
qt4_add_resources ( DT_RESOURCE_FILES ./Largest_empty_rectangle_2.qrc )
# use the Qt MOC preprocessor on classes that derives from QObject
qt4_generate_moc( Largest_empty_rectangle_2.cpp Largest_empty_rectangle_2.moc )
# The executable itself.
add_executable ( Largest_empty_rectangle_2 Largest_empty_rectangle_2.cpp Largest_empty_rectangle_2.moc ${DT_UI_FILES} ${DT_RESOURCE_FILES} )
add_to_cached_list( CGAL_EXECUTABLE_TARGETS Largest_empty_rectangle_2 )
# Link with Qt libraries
target_link_libraries( Largest_empty_rectangle_2 ${QT_LIBRARIES} )
# And with CGAL libraries
target_link_libraries( Largest_empty_rectangle_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,288 @@
#include <fstream>
// CGAL headers
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/point_generators_2.h>
#include <CGAL/Largest_empty_iso_rectangle_2.h>
// Qt headers
#include <QtGui>
#include <QString>
#include <QFileDialog>
#include <QGraphicsRectItem>
#include <QGraphicsLineItem>
// GraphicsView items and event filters (input classes)
#include <CGAL/Qt/PointsGraphicsItem.h>
#include <CGAL/Qt/GraphicsViewPolylineInput.h>
#include <CGAL/Qt/utility.h>
// the two base classes
#include "ui_Largest_empty_rectangle_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;
typedef CGAL::Largest_empty_iso_rectangle_2<K> Largest_empty_iso_rectangle_2;
class MainWindow :
public CGAL::Qt::DemosMainWindow,
public Ui::Largest_empty_rectangle_2
{
Q_OBJECT
private:
Iso_rectangle_2 square;
Largest_empty_iso_rectangle_2 ler;
CGAL::Qt::Converter<K> convert;
std::vector<Point_2> points;
QGraphicsScene scene;
CGAL::Qt::PointsGraphicsItem<std::vector<Point_2> > * pgi;
QGraphicsRectItem * rgi;
QGraphicsLineItem* frame[4];
CGAL::Qt::GraphicsViewPolylineInput<K> * pi;
template <typename G>
void
on_actionGenerate_triggered()
{
Iso_rectangle_2 isor = square;
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);
ler.insert(points.back());
++pg;
}
// default cursor
QApplication::restoreOverrideCursor();
emit(changed());
}
public:
MainWindow();
public slots:
void on_actionClear_triggered();
void processInput(CGAL::Object);
void on_actionRecenter_triggered();
void on_actionGeneratePointsOnCircle_triggered();
void on_actionGeneratePointsInSquare_triggered();
void on_actionGeneratePointsInDisc_triggered();
void clear();
void update_largest_empty_rectangle();
signals:
void changed();
};
MainWindow::MainWindow()
: DemosMainWindow(), square(Point_2(-1, -1), Point_2(1,1)), ler(square)
{
setupUi(this);
// Add a GraphicItem for the point set
pgi = new CGAL::Qt::PointsGraphicsItem<std::vector<Point_2> >(&points);
rgi = new QGraphicsRectItem(convert(square));
Point_2 bl(-1,-1), br(1,-1), tl(-1,1), tr(1,1);
frame[0] = new QGraphicsLineItem(convert(Segment_2(bl, br)));
frame[1] = new QGraphicsLineItem(convert(Segment_2(br, tr)));
frame[2] = new QGraphicsLineItem(convert(Segment_2(tr, tl)));
frame[3] = new QGraphicsLineItem(convert(Segment_2(tl, bl)));
QObject::connect(this, SIGNAL(changed()),
pgi, SLOT(modelChanged()));
QObject::connect(this, SIGNAL(changed()),
this, SLOT(update_largest_empty_rectangle()));
pgi->setVerticesPen(QPen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
rgi->setBrush(QBrush(Qt::cyan));
scene.addItem(pgi);
scene.addItem(rgi);
scene.addItem(frame[0]);
scene.addItem(frame[1]);
scene.addItem(frame[2]);
scene.addItem(frame[3]);
//
// Manual handling of actions
//
QObject::connect(this->actionQuit, SIGNAL(triggered()),
this, SLOT(close()));
pi = new CGAL::Qt::GraphicsViewPolylineInput<K>(this, &scene, 1, false); // inputs a list with one point
QObject::connect(pi, SIGNAL(generate(CGAL::Object)),
this, SLOT(processInput(CGAL::Object)));
scene.installEventFilter(pi);
//
// Setup the scene and the view
//
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
scene.setSceneRect(-1, -1, 1, 1);
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_Largest_empty_rectangle_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::processInput(CGAL::Object o)
{
std::list<Point_2> input;
if(CGAL::assign(input, o)){
if(input.size() == 1) {
Point_2 p = input.front();
if(! square.has_on_unbounded_side(p)){
points.push_back(p);
ler.insert(p);
}
}
emit(changed());
}
}
void
MainWindow::update_largest_empty_rectangle()
{
rgi->setRect(convert(ler.get_largest_empty_iso_rectangle()));
}
void
MainWindow::on_actionClear_triggered()
{
clear();
emit(changed());
}
void
MainWindow::on_actionRecenter_triggered()
{
this->graphicsView->setSceneRect(convert(square));
this->graphicsView->fitInView(convert(square), 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::clear()
{
points.clear();
ler.clear();
rgi->setRect(convert(square));
}
#include "Largest_empty_rectangle_2.moc"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
app.setOrganizationDomain("geometryfactory.com");
app.setOrganizationName("GeometryFactory");
app.setApplicationName("Largest_empty_rectangle_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(Largest_empty_rectangle_2);
Q_INIT_RESOURCE(Input);
Q_INIT_RESOURCE(CGAL);
MainWindow mainWindow;
mainWindow.show();
mainWindow.on_actionRecenter_triggered();
return app.exec();
}

View File

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

View File

@ -0,0 +1,210 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author>GeometryFactory</author>
<class>Largest_empty_rectangle_2</class>
<widget class="QMainWindow" name="Largest_empty_rectangle_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>500</width>
<height>500</height>
</rect>
</property>
<property name="windowTitle">
<string>CGAL 2D Largest Empty Iso Rectangle</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>500</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"/>
</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="actionCreateInputGenerator">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Create Input Generator</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="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="Largest_empty_rectangle_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 Largest Empty Iso Rectangle</h2>
<p>Copyright &copy; 2010 GeometryFactory</p>
<p>This application illustrates the 2D largest empty iso rectangle
of <a href="http://www.cgal.org/">CGAL</a></p>
<p>See also <a href="http://www.cgal.org/Pkg/InscribedAreas">the online
manual</a>.</p>
</body>
</html>