Commit f5a3f7f8 authored by Dilson Guimarães's avatar Dilson Guimarães Committed by Caio Tonetti
Browse files

Fixed edge crossing test in the quality benchmark.

parent 497c98df
......@@ -2,6 +2,8 @@
#include <QLineF>
#include <QVector2D>
#include <iostream>
#include <iomanip>
void LayoutEvaluator::showMetric(const MetricSummarizer& metric, std::ostream& outputStream) const
{
......@@ -10,6 +12,41 @@ void LayoutEvaluator::showMetric(const MetricSummarizer& metric, std::ostream& o
outputStream << "\taverage: " << metric.average() << std::endl;
}
QPointF LayoutEvaluator::projectOntoSegment(const QLineF& segment, const QPointF& point) const
{
const QPointF p1p2 = segment.p1() - segment.p2();
const QPointF pointp2 = point - segment.p2();
const qreal denominator = QPointF::dotProduct(p1p2, p1p2);
const qreal numerator = QPointF::dotProduct(p1p2, pointp2);
const qreal lambda = qMin(1., qMax(0., numerator / denominator));
return segment.p1() * lambda + segment.p2() * (1. - lambda);
}
qreal LayoutEvaluator::squaredDistance(const QPointF& pointA, const QPointF& pointB) const
{
return QPointF::dotProduct(pointA - pointB, pointA - pointB);
}
bool LayoutEvaluator::doSegmentsIntersect(const QLineF& segmentA, const QLineF& segmentB) const
{
constexpr qreal EPS = 1.e-1;
QPointF point;
if (segmentA.intersects(segmentB, &point) != QLineF::BoundedIntersection) {
return false;
}
if (squaredDistance(point, projectOntoSegment(segmentA, point)) > EPS) {
return false;
}
if (squaredDistance(point, projectOntoSegment(segmentB, point)) > EPS) {
return false;
}
return true;
}
bool LayoutEvaluator::crosses(const EdgePtr a, const EdgePtr b) const
{
if (a->from() == b->from() or a->from() == b->to() or
......@@ -18,11 +55,15 @@ bool LayoutEvaluator::crosses(const EdgePtr a, const EdgePtr b) const
return false;
}
QLineF segmentA(a->from()->x(), a->from()->y(), a->to()->x(), a->to()->y());
QLineF segmentB(b->from()->x(), b->from()->y(), b->to()->x(), b->to()->y());
QPointF aFrom(a->from()->x(), a->from()->y());
QPointF aTo(a->to()->x(), a->to()->y());
QPointF bFrom(b->from()->x(), b->from()->y());
QPointF bTo(b->to()->x(), b->to()->y());
QPointF point;
return segmentA.intersects(segmentB, &point) == QLineF::BoundedIntersection;
QLineF segmentA(aFrom, aTo);
QLineF segmentB(bFrom, bTo);
return doSegmentsIntersect(segmentA, segmentB);
}
int LayoutEvaluator::calculateNumberOfEdgeCrosses(GraphDocumentPtr document)
......@@ -34,7 +75,7 @@ int LayoutEvaluator::calculateNumberOfEdgeCrosses(GraphDocumentPtr document)
numberOfEdgeCrosses++;
}
}
}
}
return numberOfEdgeCrosses / 2;
}
......
......@@ -54,6 +54,9 @@ private:
bool intersects(const NodePtr a, const NodePtr b) const;
int calculateNumberOfNodeIntersections(GraphDocumentPtr document);
int calculateNumberOfNodesWithIntersections(GraphDocumentPtr document);
QPointF projectOntoSegment(const QLineF& segment, const QPointF& point) const;
qreal squaredDistance(const QPointF& pointA, const QPointF& pointB) const;
bool doSegmentsIntersect(const QLineF& segmentA, const QLineF& segmentB) const;
public:
/*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment