1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
#include <fstream>
#include <boost/config.hpp>
#include <boost/version.hpp>
// CGAL headers
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Regular_triangulation_2.h>
#include <CGAL/IO/WKT.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 "RegularTriangulationRemoveVertex.h"
#include <CGAL/Qt/GraphicsViewCircleInput.h>
#include <CGAL/Qt/RegularTriangulationGraphicsItem.h>
#include <CGAL/Qt/PowerdiagramGraphicsItem.h>
// for viewportsBbox
#include <CGAL/Qt/utility.h>
// the two base classes
#include "ui_Regular_triangulation_2.h"
#include <CGAL/Qt/DemosMainWindow.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef K::Weighted_point_2 Weighted_point_2;
typedef K::Point_2 Circle_2;
typedef K::Iso_rectangle_2 Iso_rectangle_2;
typedef CGAL::Regular_triangulation_2<K> Regular;
class MainWindow :
public CGAL::Qt::DemosMainWindow,
public Ui::Regular_triangulation_2
{
Q_OBJECT
private:
Regular dt;
QGraphicsScene scene;
CGAL::Qt::RegularTriangulationGraphicsItem<Regular> * dgi;
CGAL::Qt::PowerdiagramGraphicsItem<Regular> * vgi;
CGAL::Qt::RegularTriangulationRemoveVertex<Regular> * trv;
CGAL::Qt::GraphicsViewCircleInput<K> * pi;
public:
MainWindow();
public Q_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();
Q_SIGNALS:
void changed();
};
MainWindow::MainWindow()
: DemosMainWindow()
{
setupUi(this);
// Add a GraphicItem for the regular triangulation
dgi = new CGAL::Qt::RegularTriangulationGraphicsItem<Regular>(&dt);
QObject::connect(this, SIGNAL(changed()),
dgi, SLOT(modelChanged()));
dgi->setVerticesPen(QPen(Qt::red, 0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
dgi->setEdgesPen(QPen(Qt::black, 0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
scene.addItem(dgi);
// Add a GraphicItem for the Powerdiagram diagram
vgi = new CGAL::Qt::PowerdiagramGraphicsItem<Regular>(&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<K>(this, &scene, 1); // emits center/radius
QObject::connect(pi, SIGNAL(generate(CGAL::Object)),
this, SLOT(processInput(CGAL::Object)));
trv = new CGAL::Qt::RegularTriangulationRemoveVertex<Regular>(&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->transform().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<Point_2, K::FT > center_sqr;
if(CGAL::assign(center_sqr, o)){
Regular::Point wp(center_sqr.first, center_sqr.second);
dt.insert(wp);
}
Q_EMIT( changed());
}
/*
* Qt Automatic Connections
* https://doc.qt.io/qt-5/designer-using-a-ui-file.html#automatic-connections
*
* setupUi(this) generates connections to the slots named
* "on_<action_name>_<signal_name>"
*/
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();
Q_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)());
CGAL::Random rnd(CGAL::get_default_random());
const int number_of_points =
QInputDialog::getInt(this,
tr("Number of random points"),
tr("Enter number of random points"), 100, 0);
// wait cursor
QApplication::setOverrideCursor(Qt::WaitCursor);
std::vector<Weighted_point_2> points;
points.reserve(number_of_points);
for(int i = 0; i < number_of_points; ++i){
Weighted_point_2 wp(*pg++, rnd.get_double(0, 500));
points.push_back(wp);
}
dt.insert(points.begin(), points.end());
// default cursor
QApplication::setOverrideCursor(Qt::ArrowCursor);
Q_EMIT( changed());
}
void
MainWindow::on_actionLoadPoints_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Points file"),
".",
tr("Weighted Points (*.wpts.cgal);;"
"WKT files (*.wkt *.WKT);;"
"All (*)"));
if(! fileName.isEmpty()){
std::ifstream ifs(qPrintable(fileName));
std::vector<Weighted_point_2> points;
if(fileName.endsWith(".wkt",Qt::CaseInsensitive))
{
std::vector<K::Point_3> points_3;
CGAL::IO::read_multi_point_WKT(ifs, points_3);
for(const K::Point_3& p : points_3)
{
points.push_back(Weighted_point_2(K::Point_2(p.x(), p.y()), p.z()));
}
}
else
{
Weighted_point_2 p;
while(ifs >> p) {
points.push_back(p);
}
}
dt.insert(points.begin(), points.end());
actionRecenter->trigger();
Q_EMIT( changed());
}
}
void
MainWindow::on_actionSavePoints_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save points"),
".reg.cgal",
tr("Weighted Points (*.wpts.cgal);;"
"WKT files (*.wkt *.WKT);;"
"All (*)"));
if(! fileName.isEmpty()){
std::ofstream ofs(qPrintable(fileName));
if(fileName.endsWith(".wkt",Qt::CaseInsensitive))
{
std::vector<K::Point_3> points_3;
for(Regular::Finite_vertices_iterator
vit = dt.finite_vertices_begin(),
end = dt.finite_vertices_end();
vit!= end; ++vit)
{
points_3.push_back(K::Point_3(vit->point().x(),
vit->point().y(),
vit->point().weight()));
}
CGAL::IO::write_multi_point_WKT(ofs, points_3);
}
else
{
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"
#include <CGAL/Qt/resources.h>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
app.setOrganizationDomain("geometryfactory.com");
app.setOrganizationName("GeometryFactory");
app.setApplicationName("Regular_triangulation_2 demo");
// Import resources from libCGAL (Qt6).
CGAL_QT_INIT_RESOURCES;
MainWindow mainWindow;
mainWindow.show();
QStringList args = app.arguments();
args.removeAt(0);
Q_FOREACH(QString filename, args) {
mainWindow.open(filename);
}
return app.exec();
}
|