Added a Qt4 based 2D Alpha Shape demo

This commit is contained in:
Andreas Fabri 2010-08-13 14:49:39 +00:00
parent eba2d33e3d
commit e88b07fd02
5 changed files with 546 additions and 0 deletions

3
.gitattributes vendored
View File

@ -1413,6 +1413,9 @@ Geomview/demo/Geomview/CMakeLists.txt -text
Geomview/doc_tex/Geomview/geomview.gif -text
GraphicsView/GraphicsView.odp -text
GraphicsView/TODO -text
GraphicsView/demo/Alpha_shapes_2/Alpha_shapes_2.qrc -text
GraphicsView/demo/Alpha_shapes_2/Alpha_shapes_2.ui -text
GraphicsView/demo/Alpha_shapes_2/about_Alpha_shapes_2.html svneol=native#text/html
GraphicsView/demo/Bounding_volumes/Bounding_volumes.qrc -text
GraphicsView/demo/Bounding_volumes/Bounding_volumes.ui -text
GraphicsView/demo/Bounding_volumes/about_Bounding_volumes.html svneol=native#text/html

View File

@ -0,0 +1,311 @@
#include <fstream>
// CGAL headers
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/Alpha_shape_2.h>
#include <CGAL/point_generators_2.h>
// Qt headers
#include <QtGui>
#include <QString>
#include <QActionGroup>
#include <QFileDialog>
#include <QInputDialog>
// GraphicsView items and event filters (input classes)
#include <CGAL/Qt/AlphaShapeGraphicsItem.h>
#include <CGAL/Qt/GraphicsViewPolylineInput.h>
// for viewportsBbox
#include <CGAL/Qt/utility.h>
// the two base classes
#include "ui_Alpha_shapes_2.h"
#include <CGAL/Qt/DemosMainWindow.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef K::Iso_rectangle_2 Iso_rectangle_2;
typedef CGAL::Alpha_shape_vertex_base_2<K> Vb;
typedef CGAL::Alpha_shape_face_base_2<K> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb,Fb> Tds;
typedef CGAL::Delaunay_triangulation_2<K, Tds> Delaunay;
typedef CGAL::Alpha_shape_2<Delaunay> Alpha_shape_2;
typedef Alpha_shape_2::Alpha_iterator Alpha_iterator;
class MainWindow :
public CGAL::Qt::DemosMainWindow,
public Ui::Alpha_shapes_2
{
Q_OBJECT
private:
double alpha;
std::vector<Point_2> points;
Alpha_shape_2 as;
QGraphicsScene scene;
CGAL::Qt::AlphaShapeGraphicsItem<Alpha_shape_2> * agi;
CGAL::Qt::GraphicsViewPolylineInput<K> * pi;
public:
MainWindow();
public slots:
void processInput(CGAL::Object o);
void alphaChanged(int i);
void on_actionInsertRandomPoints_triggered();
void on_actionLoadPoints_triggered();
void on_actionClear_triggered();
void on_actionRecenter_triggered();
void open(QString fileName);
signals:
void changed();
};
MainWindow::MainWindow()
: DemosMainWindow()
{
setupUi(this);
this->graphicsView->setAcceptDrops(false);
// Add a GraphicItem for the alpha shape
agi = new CGAL::Qt::AlphaShapeGraphicsItem<Alpha_shape_2>(&as);
QObject::connect(this, SIGNAL(changed()),
agi, SLOT(modelChanged()));
agi->setVerticesPen(QPen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
agi->setEdgesPen(QPen(Qt::lightGray, 0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
agi->setRegularEdgesPen(QPen(Qt::blue, 0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
agi->setSingularEdgesPen(QPen(Qt::cyan, 0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
agi->setRegularFacesBrush(QBrush(Qt::cyan));
scene.addItem(agi);
//
// Manual handling of actions
//
QObject::connect(this->alphaSlider, SIGNAL(valueChanged(int)),
this, SLOT(alphaChanged(int)));
QObject::connect(this->alphaBox, SIGNAL(valueChanged(int)),
this, SLOT(alphaChanged(int)));
QObject::connect(this->alphaSlider, SIGNAL(valueChanged(int)),
this->alphaBox, SLOT(setValue(int)));
QObject::connect(this->alphaBox, SIGNAL(valueChanged(int)),
this->alphaSlider, SLOT(setValue(int)));
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);
//this->actionShowAlphaShape->setChecked(true);
//
// Setup the scene and the view
//
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
scene.setSceneRect(-100, -100, 100, 100);
this->graphicsView->setScene(&scene);
this->graphicsView->setMouseTracking(true);
// Turn the vertical axis upside down
this->graphicsView->matrix().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_Alpha_shape_2.html");
this->addAboutCGAL();
this->addRecentFiles(this->menuFile, this->actionQuit);
connect(this, SIGNAL(openRecentFile(QString)),
this, SLOT(open(QString)));
}
void
MainWindow::processInput(CGAL::Object o)
{
std::list<Point_2> input;
if(CGAL::assign(input, o)){
if(input.size() == 1) {
points.push_back(input.front());
as.make_alpha_shape(points.begin(), points.end());
as.set_alpha(alpha);
}
emit(changed());
}
}
void MainWindow::alphaChanged(int i)
{
if (as.number_of_alphas() > 0){
if(i < 100){
int n = static_cast<int>((i * as.number_of_alphas())/ 100);
if(n == 0) n++;
alpha = as.get_nth_alpha(n);
as.set_alpha(alpha);
} else {
Alpha_iterator alpha_end_it = as.alpha_end();
alpha = (*(--alpha_end_it))+1;
as.set_alpha(alpha);
}
} else {
alpha = 0;
as.set_alpha(0);
}
emit (changed());
}
/*
* 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()
{
as.clear();
points.clear();
emit(changed());
}
void
MainWindow::on_actionInsertRandomPoints_triggered()
{
QRectF rect = CGAL::Qt::viewportsBbox(&scene);
CGAL::Qt::Converter<K> convert;
Iso_rectangle_2 isor = convert(rect);
CGAL::Random_points_in_iso_rectangle_2<Point_2> pg(isor.min(), isor.max());
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++);
}
as.make_alpha_shape(points.begin(), points.end());
as.set_alpha(alpha);
// default cursor
QApplication::restoreOverrideCursor();
emit(changed());
}
void
MainWindow::on_actionLoadPoints_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Points file"),
".");
if(! fileName.isEmpty()){
open(fileName);
}
}
void
MainWindow::open(QString fileName)
{
std::cerr << "open " << std::endl;
std::cerr << qPrintable(fileName) << std::endl;
// wait cursor
QApplication::setOverrideCursor(Qt::WaitCursor);
std::ifstream ifs(qPrintable(fileName));
K::Point_2 p;
while(ifs >> p) {
points.push_back(p);
}
as.make_alpha_shape(points.begin(), points.end());
as.set_alpha(alpha);
// default cursor
QApplication::restoreOverrideCursor();
this->addToRecentFiles(fileName);
actionRecenter->trigger();
emit(changed());
}
void
MainWindow::on_actionRecenter_triggered()
{
this->graphicsView->setSceneRect(agi->boundingRect());
this->graphicsView->fitInView(agi->boundingRect(), Qt::KeepAspectRatio);
}
#include "Alpha_shapes_2.moc"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
app.setOrganizationDomain("geometryfactory.com");
app.setOrganizationName("GeometryFactory");
app.setApplicationName("Alpha_shape_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(Triangulation_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_Alpha_shapes_2.html</file>
</qresource>
</RCC>

View File

@ -0,0 +1,216 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author>GeometryFactory</author>
<class>Alpha_shapes_2</class>
<widget class="QMainWindow" name="Alpha_shapes_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>CGAL Alpha Shape</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="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>&amp;Alpha:</string>
</property>
<property name="buddy">
<cstring>alphaBox</cstring>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="alphaBox">
<property name="suffix">
<string>%</string>
</property>
<property name="maximum">
<number>100</number>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="alphaSlider">
<property name="maximum">
<number>100</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QGraphicsView" name="graphicsView">
<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>
</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"/>
<addaction name="actionLoadPoints"/>
</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="separator"/>
<addaction name="action_InsertPoint"/>
<addaction name="actionRecenter"/>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</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="actionLoadPoints"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
<widget class="QMenu" name="menuEdit">
<property name="title">
<string>&amp;Edit</string>
</property>
<addaction name="actionInsertRandomPoints"/>
</widget>
<widget class="QMenu" name="menuTools">
<property name="title">
<string>&amp;Tools</string>
</property>
<addaction name="separator"/>
<addaction name="actionRecenter"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
<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="actionLoadPoints">
<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 Points...</string>
</property>
<property name="shortcut">
<string>Ctrl+L</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="actionInsertRandomPoints">
<property name="text">
<string>Insert Random Points</string>
</property>
</action>
<action name="action_InsertPoint">
<property name="icon">
<iconset resource="../icons/Input.qrc">
<normaloff>:/cgal/Input/inputPoint.png</normaloff>:/cgal/Input/inputPoint.png</iconset>
</property>
<property name="text">
<string>Insert Point</string>
</property>
</action>
</widget>
<resources>
<include location="Alpha_shapes_2.qrc"/>
<include location="../icons/File.qrc"/>
<include location="../resources/CGAL.qrc"/>
<include location="../icons/Triangulation_2.qrc"/>
<include location="../icons/Input.qrc"/>
</resources>
<connections/>
</ui>

View File

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