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
|
#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>
#ifndef Q_MOC_RUN
#include <CGAL/random_convex_hull_in_disc_2.h>
#endif
// Qt headers
#include <QtGui>
#include <QString>
#include <QInputDialog>
#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::getInt(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();
Q_EMIT( changed());
}
public:
MainWindow();
public Q_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 on_actionGeneratePolytopeInDisc_triggered();
void clear();
Q_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
* 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_actionClear_triggered()
{
clear();
Q_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);
std::copy_n( g, 200, std::back_inserter(segments));
Q_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(t1, 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(t2, 50);
std::copy( t2_begin, t2_end, std::back_inserter(segments));
Q_EMIT( changed());
}
void
MainWindow::on_actionGeneratePolytopeInDisc_triggered()
{
boost::mt19937 gen;
gen.seed(time(nullptr));
std::vector<Point_2> points;
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::getInt(this,
tr("Number of random points in the disc"),
tr("Enter number of random points.\nThe polytope will be the convex hull of these points."),
100,
0,
(std::numeric_limits<int>::max)(),
1,
&ok);
if(!ok) {
return;
}
// wait cursor
QApplication::setOverrideCursor(Qt::WaitCursor);
segments.reserve(segments.size() + 100);
CGAL::random_convex_hull_in_disc_2(number_of_points,radius,gen,std::back_inserter(points),K());
std::vector<Point_2>::iterator it2=points.begin();
for(std::vector<Point_2>::iterator it=points.begin();it!=points.end();it++){
it2++;
if (it2==points.end()) it2=points.begin();
Segment_2 p(*it+offset,*it2+offset);
segments.push_back(p);
}
// default cursor
QApplication::restoreOverrideCursor();
Q_EMIT( changed());
}
void
MainWindow::clear()
{
points.clear();
segments.clear();
}
#include "Generator_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("Generator_2 demo");
// Import resources from libCGAL (Qt6).
// See https://doc.qt.io/qt-5/qdir.html#Q_INIT_RESOURCE
CGAL_QT_INIT_RESOURCES;
Q_INIT_RESOURCE(Generator_2);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
|