diff --git a/.gitattributes b/.gitattributes index 7107b38aa81..1be87909883 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1577,6 +1577,7 @@ GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.qrc -text GraphicsView/demo/Triangulation_2/Constrained_Delaunay_triangulation_2.ui -text GraphicsView/demo/Triangulation_2/Delaunay_triangulation_2.qrc -text GraphicsView/demo/Triangulation_2/Delaunay_triangulation_2.ui -text +GraphicsView/demo/Triangulation_2/Regular_triangulation_2.ui -text GraphicsView/demo/Triangulation_2/about_Constrained_Delaunay_triangulation_2.html svneol=native#text/html GraphicsView/demo/Triangulation_2/about_Delaunay_triangulation_2.html svneol=native#text/html GraphicsView/demo/Triangulation_2/data/clusters.poly -text svneol=unset#application/octet-stream diff --git a/GraphicsView/demo/Triangulation_2/RegularTriangulationRemoveVertex.h b/GraphicsView/demo/Triangulation_2/RegularTriangulationRemoveVertex.h new file mode 100644 index 00000000000..39d1af4702c --- /dev/null +++ b/GraphicsView/demo/Triangulation_2/RegularTriangulationRemoveVertex.h @@ -0,0 +1,80 @@ + +#ifndef CGAL_QT_REGULAR_TRIANGULATION_REMOVE_VERTEX_H +#define CGAL_QT_REGULAR_TRIANGULATION_REMOVE_VERTEX_H + +#include +#include +#include +#include +#include + + + +namespace CGAL { +namespace Qt { + +template +class RegularTriangulationRemoveVertex : public GraphicsViewInput +{ +public: + typedef typename DT::Vertex_handle Vertex_handle; + + RegularTriangulationRemoveVertex(DT * dt_, QObject* parent); + +protected: + + void mousePressEvent(QGraphicsSceneMouseEvent *event); + + bool eventFilter(QObject *obj, QEvent *event); + + DT * dt; +}; + + +template +RegularTriangulationRemoveVertex::RegularTriangulationRemoveVertex(T * dt_, + QObject* parent) + : GraphicsViewInput(parent), dt(dt_) +{} + + + +template +void +RegularTriangulationRemoveVertex::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if((event->modifiers() & ::Qt::ShiftModifier) + && (! (event->modifiers() & ::Qt::ControlModifier))){ + if(dt->number_of_vertices() == 0){ + dt->clear(); + }else { + typedef Kernel_traits::Kernel K; + Converter convert; + typename T::Vertex_handle selected_vertex = dt->nearest_power_vertex(convert(event->scenePos())); + dt->remove(selected_vertex); + } + emit (modelChanged()); + } +} + + + +template +bool +RegularTriangulationRemoveVertex::eventFilter(QObject *obj, QEvent *event) +{ + if (event->type() == QEvent::GraphicsSceneMousePress) { + QGraphicsSceneMouseEvent *mouseEvent = static_cast(event); + mousePressEvent(mouseEvent); + return false; + } else{ + // standard event processing + return QObject::eventFilter(obj, event); + } +} + + +} // namespace Qt +} // namespace CGAL + +#endif // CGAL_QT_REGULAR_TRIANGULATION_REMOVE_VERTEX_H diff --git a/GraphicsView/demo/Triangulation_2/Regular_triangulation_2.cpp b/GraphicsView/demo/Triangulation_2/Regular_triangulation_2.cpp new file mode 100644 index 00000000000..ba835970970 --- /dev/null +++ b/GraphicsView/demo/Triangulation_2/Regular_triangulation_2.cpp @@ -0,0 +1,303 @@ +#include + +// CGAL headers +#include +#include +#include + +#include + +// Qt headers +#include +#include +#include +#include +#include + +// GraphicsView items and event filters (input classes) + +#include "RegularTriangulationRemoveVertex.h" +#include +#include +#include + +// for viewportsBbox +#include +// the two base classes +#include "ui_Regular_triangulation_2.h" +#include + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K; +typedef K::Point_2 Point_2; +typedef K::Point_2 Circle_2; +typedef K::Iso_rectangle_2 Iso_rectangle_2; + +typedef double Weight; +typedef CGAL::Regular_triangulation_euclidean_traits_2 Gt; +typedef CGAL::Regular_triangulation_2 Regular; + +class MainWindow : + public CGAL::Qt::DemosMainWindow, + public Ui::Regular_triangulation_2 +{ + Q_OBJECT + +private: + Regular dt; + QGraphicsScene scene; + + CGAL::Qt::RegularTriangulationGraphicsItem * dgi; + CGAL::Qt::PowerdiagramGraphicsItem * vgi; + + CGAL::Qt::RegularTriangulationRemoveVertex * trv; + CGAL::Qt::GraphicsViewCircleInput * pi; +public: + MainWindow(); + +public slots: + + void processInput(CGAL::Object o); + + void on_actionShowRegular_toggled(bool checked); + + void on_actionShowPowerdiagram_toggled(bool checked); + + void on_actionInsertPoint_toggled(bool checked); + + void on_actionInsertRandomPoints_triggered(); + + void on_actionLoadPoints_triggered(); + + void on_actionSavePoints_triggered(); + + void on_actionClear_triggered(); + + void on_actionRecenter_triggered(); + + +signals: + void changed(); +}; + + +MainWindow::MainWindow() + : DemosMainWindow() +{ + setupUi(this); + + // Add a GraphicItem for the Regular triangulation + dgi = new CGAL::Qt::RegularTriangulationGraphicsItem(&dt); + + QObject::connect(this, SIGNAL(changed()), + dgi, SLOT(modelChanged())); + + dgi->setVerticesPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + scene.addItem(dgi); + + // Add a GraphicItem for the Powerdiagram diagram + vgi = new CGAL::Qt::PowerdiagramGraphicsItem(&dt); + + QObject::connect(this, SIGNAL(changed()), + vgi, SLOT(modelChanged())); + + vgi->setEdgesPen(QPen(Qt::blue, 0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + scene.addItem(vgi); + vgi->hide(); + + // Setup input handlers. They get events before the scene gets them + // and the input they generate is passed to the triangulation with + // the signal/slot mechanism + pi = new CGAL::Qt::GraphicsViewCircleInput(this, &scene, 1); // emits center/radius + + + QObject::connect(pi, SIGNAL(generate(CGAL::Object)), + this, SLOT(processInput(CGAL::Object))); + + trv = new CGAL::Qt::RegularTriangulationRemoveVertex(&dt, this); + QObject::connect(trv, SIGNAL(modelChanged()), + this, SIGNAL(changed())); + + // + // Manual handling of actions + // + QObject::connect(this->actionQuit, SIGNAL(triggered()), + this, SLOT(close())); + + // We put mutually exclusive actions in an QActionGroup + QActionGroup* ag = new QActionGroup(this); + ag->addAction(this->actionInsertPoint); + + // Check two actions + this->actionInsertPoint->setChecked(true); + this->actionShowRegular->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_Regular_triangulation_2.html"); + this->addAboutCGAL(); +} + + +void +MainWindow::processInput(CGAL::Object o) +{ + std::pair center_sqr; + if(CGAL::assign(center_sqr, o)){ + Regular::Point wp(center_sqr.first, center_sqr.second); + dt.insert(wp); + } + + 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__" + */ +void +MainWindow::on_actionInsertPoint_toggled(bool checked) +{ + if(checked){ + scene.installEventFilter(pi); + scene.installEventFilter(trv); + } else { + scene.removeEventFilter(pi); + scene.removeEventFilter(trv); + } +} + + + +void +MainWindow::on_actionShowRegular_toggled(bool checked) +{ + dgi->setVisibleEdges(checked); +} + + +void +MainWindow::on_actionShowPowerdiagram_toggled(bool checked) +{ + vgi->setVisible(checked); +} + + +void +MainWindow::on_actionClear_triggered() +{ + dt.clear(); + emit(changed()); +} + + +void +MainWindow::on_actionInsertRandomPoints_triggered() +{ + QRectF rect = CGAL::Qt::viewportsBbox(&scene); + CGAL::Qt::Converter convert; + Iso_rectangle_2 isor = convert(rect); + CGAL::Random_points_in_iso_rectangle_2 pg(isor.min(), isor.max()); + const int number_of_points = + QInputDialog::getInteger(this, + tr("Number of random points"), + tr("Enter number of random points"), 100, 0); + + // wait cursor + QApplication::setOverrideCursor(Qt::WaitCursor); + std::vector points; + points.reserve(number_of_points); + for(int i = 0; i < number_of_points; ++i){ + points.push_back(*pg++); + } + dt.insert(points.begin(), points.end()); + // default cursor + QApplication::setOverrideCursor(Qt::ArrowCursor); + emit(changed()); +} + + +void +MainWindow::on_actionLoadPoints_triggered() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open Points file"), + "."); + if(! fileName.isEmpty()){ + std::ifstream ifs(qPrintable(fileName)); + + K::Point_2 p; + std::vector points; + while(ifs >> p) { + points.push_back(p); + } + dt.insert(points.begin(), points.end()); + + actionRecenter->trigger(); + emit(changed()); + } +} + + +void +MainWindow::on_actionSavePoints_triggered() +{ + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save points"), + "."); + if(! fileName.isEmpty()){ + std::ofstream ofs(qPrintable(fileName)); + for(Regular::Finite_vertices_iterator + vit = dt.finite_vertices_begin(), + end = dt.finite_vertices_end(); + vit!= end; ++vit) + { + ofs << vit->point() << std::endl; + } + } +} + + +void +MainWindow::on_actionRecenter_triggered() +{ + this->graphicsView->setSceneRect(dgi->boundingRect()); + this->graphicsView->fitInView(dgi->boundingRect(), Qt::KeepAspectRatio); +} + + +#include "Regular_triangulation_2.moc" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + // 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(); +} diff --git a/GraphicsView/demo/Triangulation_2/Regular_triangulation_2.ui b/GraphicsView/demo/Triangulation_2/Regular_triangulation_2.ui new file mode 100644 index 00000000000..9e49fd40e45 --- /dev/null +++ b/GraphicsView/demo/Triangulation_2/Regular_triangulation_2.ui @@ -0,0 +1,247 @@ + + GeometryFactory + Regular_triangulation_2 + + + + 0 + 0 + 400 + 300 + + + + CGAL Regular Triangulation + + + + :/cgal/logos/cgal_icon:/cgal/logos/cgal_icon + + + + + + + Qt::StrongFocus + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAlwaysOn + + + QGraphicsView::NoAnchor + + + + + + + + + File Tools + + + TopToolBarArea + + + false + + + + + + + + Visualization Tools + + + TopToolBarArea + + + false + + + + + + + + + + 0 + 0 + 400 + 19 + + + + + &File + + + + + + + + + + + &Edit + + + + + + &Tools + + + + + + + + + + + + + + &About + + + + + About &CGAL + + + + + &Quit + + + Ctrl+Q + + + + + &Insert random points + + + Ctrl+I + + + + + true + + + false + + + + :/cgal/Input/inputPoint.png:/cgal/Input/inputPoint.png + + + &Insert Point + + + Insert Point + + + Left: Insert vtx + + + + + + :/cgal/fileToolbar/fileNew.png:/cgal/fileToolbar/fileNew.png + + + &Clear + + + Ctrl+C + + + + + true + + + false + + + + :/cgal/Triangulation_2/Voronoi_diagram_2.png:/cgal/Triangulation_2/Voronoi_diagram_2.png + + + Show &Voronoi Diagram + + + Ctrl+V + + + + + true + + + + :/cgal/Triangulation_2/Delaunay_triangulation_2.png:/cgal/Triangulation_2/Delaunay_triangulation_2.png + + + Show &Regular Triangulation + + + Ctrl+D + + + + + + :/cgal/fileToolbar/fileOpen.png:/cgal/fileToolbar/fileOpen.png + + + &Load Points... + + + Ctrl+L + + + + + + :/cgal/fileToolbar/fileSave.png:/cgal/fileToolbar/fileSave.png + + + &Save Points... + + + Ctrl+S + + + + + + :/cgal/Input/zoom-best-fit:/cgal/Input/zoom-best-fit + + + Re&center the viewport + + + Ctrl+R + + + + + + + + + + + + diff --git a/GraphicsView/demo/Triangulation_2/about_Regular_triangulation_2.html b/GraphicsView/demo/Triangulation_2/about_Regular_triangulation_2.html new file mode 100644 index 00000000000..02e8fc29ae0 --- /dev/null +++ b/GraphicsView/demo/Triangulation_2/about_Regular_triangulation_2.html @@ -0,0 +1,11 @@ + + +

Delaunay Triangulation

+

Copyright © 2008 GeometryFactory

+

This application illustrates the 2D Regular triangulation + of CGAL, and its dual Power + diagram.

+

See also the online + manual.

+ +