aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp
blob: 2e140731a92c1583762d481533efa3b5e0e81a40 (plain)
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
// Copyright (C) 2016 Jochen Becher
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0 WITH Qt-GPL-exception-1.0

#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 {

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,
                                  int nearestPoint)
{
    bool found = false;
    qreal mindist = 0;
    QPointF ipoint;
    QLineF iline;
    for (int i = 0; i <= polygon.size() - 2; ++i) {
        QLineF polygonLine(polygon.at(i), polygon.at(i+1));
        QPointF point;
        QLineF::IntersectType intersectionType = polygonLine.intersects(line, &point);
        if (intersectionType == QLineF::BoundedIntersection) {
            qreal dist = QLineF(point, nearestPoint <= 0 ? line.p1() : line.p2()).length();
            if (!found || dist < mindist) {
                mindist = dist;
                ipoint = point;
                iline = polygonLine;
                found = true;
            }
        }
    }
    if (found) {
        if (intersectionPoint)
            *intersectionPoint = ipoint;
        if (intersectionLine)
            *intersectionLine = iline;
    }
    return found;
}

bool GeometryUtilities::intersect(const QList<QPolygonF> &polygons, const QLineF &line,
                                  int *intersectionPolygon, QPointF *intersectionPoint,
                                  QLineF *intersectionLine, int nearestPoint)
{
    bool found = false;
    qreal mindist = 0;
    int ipolygon = -1;
    QPointF ipoint;
    QLineF iline;
    for (int p = 0; p < polygons.size(); ++p) {
        const QPolygonF polygon = polygons.at(p);
        for (int i = 0; i <= polygon.size() - 2; ++i) {
            const QLineF polygonLine(polygon.at(i), polygon.at(i + 1));
            QPointF point;
            QLineF::IntersectType intersectionType = polygonLine.intersects(line, &point);
            if (intersectionType == QLineF::BoundedIntersection) {
                qreal dist = QLineF(point, nearestPoint <= 0 ? line.p1() : line.p2()).length();
                if (!found || dist < mindist) {
                    mindist = dist;
                    ipolygon = p;
                    ipoint = point;
                    iline = polygonLine;
                    found = true;
                }
            }
        }
    }
    if (found) {
        if (intersectionPolygon)
            *intersectionPolygon = ipolygon;
        if (intersectionPoint)
            *intersectionPoint = ipoint;
        if (intersectionLine)
            *intersectionLine = iline;
    }
    return found;
}


namespace {

class Candidate
{
public:
    Candidate() = default;
    Candidate(const QVector2D &f, const QPointF &s, GeometryUtilities::Side t) : first(f), second(s), third(t) { }

    QVector2D first;
    QPointF second;
    GeometryUtilities::Side third = GeometryUtilities::SideUnspecified;
};

}

bool GeometryUtilities::placeRectAtLine(const QRectF &rect, const QLineF &line, double lineOffset, double distance,
                                        const QLineF &intersectionLine, QPointF *placement, Side *horizontalAlignedSide)
{
    QMT_ASSERT(placement, return false);

    QList<Candidate> candidates;
    candidates << Candidate(QVector2D(rect.topRight() - rect.topLeft()), QPointF(0.0, 0.0), SideTop)
               << Candidate(QVector2D(rect.topLeft() - rect.topRight()), rect.topRight() - rect.topLeft(), SideTop)
               << Candidate(QVector2D(rect.bottomLeft() - rect.topLeft()), QPointF(0.0, 0.0), SideLeft)
               << Candidate(QVector2D(rect.topLeft() - rect.bottomLeft()), rect.bottomLeft() - rect.topLeft(), SideLeft)
               << Candidate(QVector2D(rect.bottomRight() - rect.bottomLeft()), rect.bottomLeft() - rect.topLeft(), SideBottom)
               << Candidate(QVector2D(rect.bottomLeft() - rect.bottomRight()), rect.bottomRight() - rect.topLeft(), SideBottom)
               << Candidate(QVector2D(rect.bottomRight() - rect.topRight()), rect.topRight() - rect.topLeft(), SideRight)
               << Candidate(QVector2D(rect.topRight() - rect.bottomRight()), rect.bottomRight() - rect.topLeft(), SideRight);

    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 = SideUnspecified;
    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 SideTop:
            side = bestSign == -1 ? SideRight : SideLeft;
            break;
        case SideBottom:
            side = bestSign == -1 ? SideLeft : SideRight;
            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
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);
}

} // namespace qmt