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
|
#include <fstream>
// CGAL headers
#include <CGAL/Cartesian.h>
#include <CGAL/MP_Float.h>
#include <CGAL/Algebraic_kernel_for_circles_2_2.h>
#include <CGAL/Circular_kernel_2.h>
#include <CGAL/intersections.h>
#include <CGAL/Circular_kernel_2.h>
#include <CGAL/Object.h>
// Qt headers
#include <QtGui>
#include <QString>
#include <QActionGroup>
#include <QFileDialog>
#include <QInputDialog>
// GraphicsView items and event filters (input classes)
#include <CGAL/Qt/GraphicsViewCircularArcInput.h>
#include "ArcsGraphicsItem.h"
// the two base classes
#include "ui_Circular_kernel_2.h"
#include <CGAL/Qt/DemosMainWindow.h>
typedef CGAL::Quotient<CGAL::MP_Float> NT;
typedef CGAL::Cartesian<NT> Linear_k;
typedef CGAL::Algebraic_kernel_for_circles_2_2<NT> Algebraic_k;
typedef CGAL::Circular_kernel_2<Linear_k,Algebraic_k> CircularKernel;
typedef CircularKernel::Point_2 Point_2;
typedef CircularKernel::Segment_2 Segment_2;
typedef CircularKernel::Line_arc_2 Line_arc_2;
typedef CircularKernel::Circular_arc_2 Circular_arc_2;
typedef CircularKernel::Circular_arc_point_2 Circular_arc_point_2;
typedef CGAL::Qt::ArcsGraphicsItem<CircularKernel> ArcsGraphicsItem;
typedef std::vector<CGAL::Object> ArcContainer;
class MainWindow :
public CGAL::Qt::DemosMainWindow,
public Ui::Circular_kernel_2
{
Q_OBJECT
private:
ArcContainer arcs;
ArcContainer intersections;
QGraphicsScene scene;
ArcsGraphicsItem * agi;
CGAL::Qt::GraphicsViewCircularArcInput<CircularKernel> * cai;
public:
MainWindow();
public Q_SLOTS:
virtual void open(QString);
void processInput(CGAL::Object o);
void on_actionInsertCircularArc_toggled(bool checked);
void on_actionClear_triggered();
void on_actionLoadLineAndCircularArcs_triggered();
void on_actionRecenter_triggered();
Q_SIGNALS:
void changed();
};
MainWindow::MainWindow()
: DemosMainWindow()
{
setupUi(this);
// Add a GraphicItem for the Circular triangulation
agi = new CGAL::Qt::ArcsGraphicsItem<CircularKernel>(arcs, intersections);
agi->setIntersectionsPen(QPen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
QObject::connect(this, SIGNAL(changed()),
agi, SLOT(modelChanged()));
agi->setInputPen(QPen(Qt::black, 0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
scene.addItem(agi);
agi->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
cai = new CGAL::Qt::GraphicsViewCircularArcInput<CircularKernel>(this, &scene);
QObject::connect(cai, SIGNAL(generate(CGAL::Object)),
this, SLOT(processInput(CGAL::Object)));
// Manual handling of actions
//
QObject::connect(this->actionQuit, SIGNAL(triggered()),
qApp, SLOT(quit()));
// Check two actions
this->actionInsertCircularArc->setChecked(true);
//
// 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_Circular_kernel_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)
{
Circular_arc_2 ca;
Line_arc_2 la;
bool is_circular = false;
if(assign(ca, o)){
is_circular = true;
} else if(! assign(la, o)){
std::cerr << "unknown object" << std::endl;
return;
}
for(std::vector<CGAL::Object>::iterator it = arcs.begin(); it != arcs.end(); ++it){
Circular_arc_2 vca;
Line_arc_2 vla;
if(assign(vca, *it)){
if(is_circular){
CGAL::intersection(ca, vca, std::back_inserter(intersections));
} else {
CGAL::intersection(la, vca, std::back_inserter(intersections));
}
} else if(assign(vla, *it)){
if(is_circular){
CGAL::intersection(ca, vla, std::back_inserter(intersections));
} else {
CGAL::intersection(la, vla, std::back_inserter(intersections));
}
}
}
arcs.push_back(o);
Q_EMIT( changed());
}
/*
* Qt Automatic Connections
* http://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_actionInsertCircularArc_toggled(bool checked)
{
if(checked){
scene.installEventFilter(cai);
} else {
scene.removeEventFilter(cai);
}
}
void
MainWindow::on_actionClear_triggered()
{
arcs.clear();
intersections.clear();
Q_EMIT( changed());
}
void
MainWindow::on_actionLoadLineAndCircularArcs_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Line and Circular Arc File"),
".",
tr("Edge files (*.arc)\n"));
if(! fileName.isEmpty()){
open(fileName);
this->addToRecentFiles(fileName);
}
}
void
MainWindow::open(QString fileName)
{
std::ifstream ifs(qPrintable(fileName));
char c;
double x,y;
Segment_2 s;
while(ifs >> c){
if(c == 's'){
ifs >> x >> y;
Point_2 p(x,y);
ifs >> x >> y;
Point_2 q(x,y);
Line_arc_2 la(Segment_2(p,q));
for(std::vector<CGAL::Object>::iterator it = arcs.begin(); it != arcs.end(); ++it){
Circular_arc_2 vca;
Line_arc_2 vla;
if(assign(vca, *it)){
CGAL::intersection(la, vca, std::back_inserter(intersections));
} else if(assign(vla, *it)){
CGAL::intersection(la, vla, std::back_inserter(intersections));
}
}
arcs.push_back(make_object(la));
} else if(c == 'c'){
ifs >> x >> y;
Point_2 p(x,y);
ifs >> x >> y;
Point_2 q(x,y);
ifs >> x >> y;
Point_2 r(x,y);
Circular_arc_2 ca(p,q,r);
for(std::vector<CGAL::Object>::iterator it = arcs.begin(); it != arcs.end(); ++it){
Circular_arc_2 vca;
Line_arc_2 vla;
if(assign(vca, *it)){
CGAL::intersection(ca, vca, std::back_inserter(intersections));
} else if(assign(vla, *it)){
CGAL::intersection(ca, vla, std::back_inserter(intersections));
}
}
arcs.push_back(make_object(ca));
}
}
Q_EMIT( changed());
}
void
MainWindow::on_actionRecenter_triggered()
{
this->graphicsView->setSceneRect(agi->boundingRect());
this->graphicsView->fitInView(agi->boundingRect(), Qt::KeepAspectRatio);
}
#include "Circular_kernel_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("Circular_kernel_2 demo");
// Import resources from libCGAL (Qt5).
CGAL_QT_INIT_RESOURCES;
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
|