Files
qt-creator/src/libs/3rdparty/modeling/qmt/infrastructure/geometryutilities.cpp

293 lines
11 KiB
C++
Raw Normal View History

/***************************************************************************
**
** Copyright (C) 2015 Jochen Becher
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "geometryutilities.h"
#include "qmt/infrastructure/qmtassert.h"
#include <QPolygonF>
#include <QLineF>
#include <QPointF>
#include <QPair>
#include <QList>
#include <QVector2D>
#include <qmath.h>
#include <qdebug.h>
#include <limits>
template <typename T>
inline int sgn(T val)
{
return (T(0) < val) - (val < T(0));
}
namespace qmt {
GeometryUtilities::GeometryUtilities()
{
}
QLineF GeometryUtilities::stretch(const QLineF &line, double p1Extension, double p2Extension)
{
QLineF direction = line.unitVector();
QPointF stretchedP1 = line.p1() - (direction.p2() - direction.p1()) * p1Extension;
QPointF stretchedP2 = line.p2() + (direction.p2() - direction.p1()) * p2Extension;
return QLineF(stretchedP1, stretchedP2);
}
bool GeometryUtilities::intersect(const QPolygonF &polygon, const QLineF &line, QPointF *intersectionPoint, QLineF *intersectionLine)
{
for (int i = 0; i <= polygon.size() - 2; ++i) {
QLineF polygonLine(polygon.at(i), polygon.at(i+1));
QLineF::IntersectType intersectionType = polygonLine.intersect(line, intersectionPoint);
if (intersectionType == QLineF::BoundedIntersection) {
if (intersectionLine) {
*intersectionLine = polygonLine;
}
return true;
}
}
return false;
}
namespace {
struct Candidate {
Candidate() : third(GeometryUtilities::SIDE_UNSPECIFIED) { }
Candidate(const QVector2D &f, const QPointF &s, GeometryUtilities::Side t) : first(f), second(s), third(t) { }
QVector2D first;
QPointF second;
GeometryUtilities::Side third;
};
}
bool GeometryUtilities::placeRectAtLine(const QRectF &rect, const QLineF &line, double lineOffset, double distance, const QLineF &intersectionLine, QPointF *placement, Side *horizontalAlignedSide)
{
QMT_CHECK(placement);
QList<Candidate> candidates;
candidates << Candidate(QVector2D(rect.topRight() - rect.topLeft()), QPointF(0.0, 0.0), SIDE_TOP)
<< Candidate(QVector2D(rect.topLeft() - rect.topRight()), rect.topRight() - rect.topLeft(), SIDE_TOP)
<< Candidate(QVector2D(rect.bottomLeft() - rect.topLeft()), QPointF(0.0, 0.0), SIDE_LEFT)
<< Candidate(QVector2D(rect.topLeft() - rect.bottomLeft()), rect.bottomLeft() - rect.topLeft(), SIDE_LEFT)
<< Candidate(QVector2D(rect.bottomRight() - rect.bottomLeft()), rect.bottomLeft() - rect.topLeft(), SIDE_BOTTOM)
<< Candidate(QVector2D(rect.bottomLeft() - rect.bottomRight()), rect.bottomRight() - rect.topLeft(), SIDE_BOTTOM)
<< Candidate(QVector2D(rect.bottomRight() - rect.topRight()), rect.topRight() - rect.topLeft(), SIDE_RIGHT)
<< Candidate(QVector2D(rect.topRight() - rect.bottomRight()), rect.bottomRight() - rect.topLeft(), SIDE_RIGHT);
QVector<QVector2D> rectEdgeVectors;
rectEdgeVectors << QVector2D(rect.topLeft() - rect.topLeft())
<< QVector2D(rect.topRight() - rect.topLeft())
<< QVector2D(rect.bottomLeft() - rect.topLeft())
<< QVector2D(rect.bottomRight() -rect.topLeft());
QVector2D directionVector(line.p2() - line.p1());
directionVector.normalize();
QVector2D sideVector(directionVector.y(), -directionVector.x());
QVector2D intersectionVector(intersectionLine.p2() - intersectionLine.p1());
intersectionVector.normalize();
QVector2D outsideVector = QVector2D(intersectionVector.y(), -intersectionVector.x());
double p = QVector2D::dotProduct(directionVector, outsideVector);
if (p < 0.0) {
outsideVector = outsideVector * -1.0;
}
double smallestA = -1.0;
QPointF rectTranslation;
Side side = SIDE_UNSPECIFIED;
int bestSign = 0;
foreach (const Candidate &candidate, candidates) {
// solve equation a * directionVector + candidate.first = b * intersectionVector to find smallest a
double r = directionVector.x() * intersectionVector.y() - directionVector.y() * intersectionVector.x();
if (r <= -1e-5 || r >= 1e-5) {
double a = (candidate.first.y() * intersectionVector.x() - candidate.first.x() * intersectionVector.y()) / r;
if (a >= 0.0 && (smallestA < 0.0 || a < smallestA)) {
// verify that all rectangle edges lay outside of shape (by checking for positiv projection to intersection)
bool ok = true;
int sign = 0;
QVector2D rectOriginVector = directionVector * a - QVector2D(candidate.second);
foreach (const QVector2D &rectEdgeVector, rectEdgeVectors) {
QVector2D edgeVector = rectOriginVector + rectEdgeVector;
double aa = QVector2D::dotProduct(outsideVector, edgeVector);
if (aa < 0.0) {
ok = false;
break;
}
int s = sgn(QVector2D::dotProduct(sideVector, edgeVector));
if (s) {
if (sign) {
if (s != sign) {
ok = false;
break;
}
} else {
sign = s;
}
}
}
if (ok) {
smallestA = a;
rectTranslation = candidate.second;
side = candidate.third;
bestSign = sign;
}
}
}
}
if (horizontalAlignedSide) {
// convert side into a horizontal side depending on placement relative to direction vector
switch (side) {
case SIDE_TOP:
side = bestSign == -1 ? SIDE_RIGHT : SIDE_LEFT;
break;
case SIDE_BOTTOM:
side = bestSign == -1 ? SIDE_LEFT : SIDE_RIGHT;
break;
default:
break;
}
*horizontalAlignedSide = side;
}
if (smallestA < 0.0) {
return false;
}
*placement = line.p1() + (directionVector * (smallestA + lineOffset)).toPointF() + (sideVector * (bestSign * distance)).toPointF() - rectTranslation;
return true;
}
double GeometryUtilities::calcAngle(const QLineF &line)
{
QVector2D directionVector(line.p2() - line.p1());
directionVector.normalize();
double angle = qAcos(directionVector.x()) * 180.0 / 3.1415926535;
if (directionVector.y() > 0.0) {
angle = -angle;
}
return angle;
}
namespace {
// scalar product
static qreal operator&(const QVector2D &lhs, const QVector2D &rhs) {
return lhs.x() * rhs.x() + lhs.y() * rhs.y();
}
}
double GeometryUtilities::calcDistancePointToLine(const QPointF &point, const QLineF &line)
{
QVector2D p(point);
QVector2D a(line.p1());
QVector2D directionVector(line.p2() - line.p1());
qreal r = -((a - p) & directionVector) / directionVector.lengthSquared();
if (r < 0.0 || r > 1.0) {
return std::numeric_limits<float>::quiet_NaN();
}
qreal d = (a + r * directionVector - p).length();
return d;
}
QPointF GeometryUtilities::calcProjection(const QLineF &line, const QPointF &point)
{
QVector2D p(point);
QVector2D a(line.p1());
QVector2D directionVector(line.p2() - line.p1());
qreal r = -((a - p) & directionVector) / directionVector.lengthSquared();
return (a + r * directionVector).toPointF();
}
QPointF GeometryUtilities::calcPrimaryAxisDirection(const QLineF &line)
{
qreal xAbs = qAbs(line.dx());
qreal yAbs = qAbs(line.dy());
if (yAbs > xAbs) {
if (line.dy() >= 0.0) {
return QPointF(0.0, 1.0);
} else {
return QPointF(0.0, -1.0);
}
} else {
if (line.dx() >= 0.0) {
return QPointF(1.0, 0.0);
} else {
return QPointF(-1.0, 0.0);
}
}
}
QPointF GeometryUtilities::calcSecondaryAxisDirection(const QLineF &line)
{
qreal xAbs = qAbs(line.dx());
qreal yAbs = qAbs(line.dy());
if (yAbs > xAbs) {
if (line.dx() >= 0.0) {
return QPointF(1.0, 0.0);
} else {
return QPointF(-1.0, 0.0);
}
} else {
if (line.dy() >= 0.0) {
return QPointF(0.0, 1.0);
} else {
return QPointF(0.0, -1.0);
}
}
}
void GeometryUtilities::adjustPosAndRect(QPointF *pos, QRectF *rect, const QPointF &topLeftDelta, const QPointF &bottomRightDelta, const QPointF &relativeAlignment)
{
*pos += QPointF(topLeftDelta.x() * (1.0 - relativeAlignment.x()) + bottomRightDelta.x() * relativeAlignment.x(),
topLeftDelta.y() * (1.0 - relativeAlignment.y()) + bottomRightDelta.y() * relativeAlignment.y());
rect->adjust(topLeftDelta.x() * relativeAlignment.x() - bottomRightDelta.x() * relativeAlignment.x(),
topLeftDelta.y() * relativeAlignment.y() - bottomRightDelta.y() * relativeAlignment.y(),
bottomRightDelta.x() * (1.0 - relativeAlignment.x()) - topLeftDelta.x() * (1.0 - relativeAlignment.x()),
bottomRightDelta.y() * (1.0 - relativeAlignment.y()) - topLeftDelta.y() * (1.0 - relativeAlignment.y()));
}
QSizeF GeometryUtilities::ensureMinimumRasterSize(const QSizeF &size, double rasterWidth, double rasterHeight)
{
double width = int(size.width() / rasterWidth + 0.99999) * rasterWidth;
double height = int(size.height() / rasterHeight + 0.99999) * rasterHeight;
return QSizeF(width, height);
}
}