Skip to content

[WIP] GraphicsView: Add General_polygon_2 to QPainterPath converter. … #2059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions GraphicsView/include/CGAL/Qt/Converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
#include <CGAL/license/GraphicsView.h>


#include <QtMath>
#include <QPointF>
#include <QLineF>
#include <QRectF>
#include <QPainterPath>
#include <QPolygonF>
#include <list>

Expand Down Expand Up @@ -183,6 +185,52 @@ class Converter {
}
}

template < typename T >
QPainterPath operator()(const typename CGAL::General_polygon_2< T > &pgn) const
{
typedef typename CGAL::General_polygon_2< T > CGAL_Polygon_2;
typedef typename CGAL_Polygon_2::Curve_const_iterator CGAL_Curve_const_iterator;
typedef typename CGAL_Polygon_2::X_monotone_curve_2 CGAL_X_monotone_curve_2;

QPainterPath result;

CGAL_Curve_const_iterator current = pgn.curves_begin();
CGAL_Curve_const_iterator end = pgn.curves_end();

result.moveTo(QPointF(CGAL::to_double(current->source().x()),
CGAL::to_double(current->source().y())));

do {
const CGAL_X_monotone_curve_2 &curve = *current;
const QPointF target = QPointF(CGAL::to_double(curve.target().x()),
CGAL::to_double(curve.target().y()));

if (curve.is_linear()) {
result.lineTo(target);
}
else if (curve.is_circular()) {
const bool isClockwise = (curve.supporting_circle().orientation() == CGAL::CLOCKWISE);
const QRectF rect = operator()(curve.supporting_circle().bbox());
const QPointF center = QPointF(CGAL::to_double(curve.supporting_circle().center().x()),
CGAL::to_double(curve.supporting_circle().center().y()));
const QPointF source = QPointF(CGAL::to_double(curve.source().x()),
CGAL::to_double(curve.source().y()));
const QPointF p1 = source - center;
const QPointF p2 = target - center;
const double asource = qAtan2(p1.y(), p1.x());
const double atarget = qAtan2(p2.y(), p2.x());
double aspan = atarget - asource;
if (aspan < -CGAL_PI || (qFuzzyCompare(aspan, -CGAL_PI) && !isClockwise))
aspan += 2.0*CGAL_PI;
else if (aspan > CGAL_PI || (qFuzzyCompare(aspan, CGAL_PI) && isClockwise))
aspan -= 2.0*CGAL_PI;
result.arcTo(rect.normalized(), qRadiansToDegrees(-asource), qRadiansToDegrees(-aspan));
}
} while (++current != end);

return result;
}

};

} // namespace Qt
Expand Down