Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdeclarativegeomapitemutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 Paolo Angelelli <paolo.angelelli@gmail.com>
2// Copyright (C) 2022 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
6
7#include <QPointF>
8#include <QMatrix4x4>
9#include <QPainterPath>
10#include <QPainterPathStroker>
11#include <QtPositioning/QGeoCoordinate>
12
13#include <QtPositioning/private/qclipperutils_p.h>
14
16
18
19double distanceSqrPointLine(double p0_x
20 , double p0_y
21 , double p1_x
22 , double p1_y
23 , double p2_x
24 , double p2_y)
25{
26 const double t_x = p2_x - p1_x;
27 const double t_y = p2_y - p1_y;
28 const double p_x = p0_x - p1_x;
29 const double p_y = p0_y - p1_y;
30 const double tsqr = t_x * t_x + t_y * t_y;
31
32 if (tsqr == 0)
33 return qInf();
34
35 double alpha = (p_x * t_x + p_y * t_y) / tsqr;
36 alpha = qBound<double>(0, alpha, 1);
37
38 const double dx = p_x - t_x * alpha;
39 const double dy = p_y - t_y * alpha;
40
41 return dx * dx + dy * dy;
42}
43
44void wrapPath(const QList<QGeoCoordinate> &perimeter,
45 const QGeoCoordinate &geoLeftBound,
47 QList<QDoubleVector2D> &wrappedPath,
48 QList<QDoubleVector2D> &wrappedPathMinus1,
49 QList<QDoubleVector2D> &wrappedPathPlus1,
50 QDoubleVector2D *leftBoundWrapped)
51{
52 QList<QDoubleVector2D> path;
53 for (const QGeoCoordinate &c : perimeter)
54 path << p.geoToMapProjection(c);
55 const QDoubleVector2D leftBound = p.geoToMapProjection(geoLeftBound);
56 wrappedPath.clear();
57 wrappedPathPlus1.clear();
58 wrappedPathMinus1.clear();
59 // compute 3 sets of "wrapped" coordinates: one w regular mercator, one w regular mercator +- 1.0
60 for (QDoubleVector2D coord : path) {
61 // We can get NaN if the map isn't set up correctly, or the projection
62 // is faulty -- probably best thing to do is abort
63 if (!qIsFinite(coord.x()) || !qIsFinite(coord.y()))
64 return;
65
66 const bool isPointLessThanUnwrapBelowX = (coord.x() < leftBound.x());
67 // unwrap x to preserve geometry if moved to border of map
68 if (isPointLessThanUnwrapBelowX)
69 coord.setX(coord.x() + 1.0);
70
71 QDoubleVector2D coordP1(coord.x() + 1.0, coord.y());
72 QDoubleVector2D coordM1(coord.x() - 1.0, coord.y());
73
74 wrappedPath.append(coord);
75 wrappedPathPlus1.append(coordP1);
76 wrappedPathMinus1.append(coordM1);
77 }
78 if (leftBoundWrapped)
79 *leftBoundWrapped = leftBound;
80}
81
82void wrapPath(const QList<QGeoCoordinate> &perimeter,
83 const QGeoCoordinate &geoLeftBound,
85 QList<QDoubleVector2D> &wrappedPath,
86 QDoubleVector2D *leftBoundWrapped)
87{
88 QList<QDoubleVector2D> path;
89 for (const QGeoCoordinate &c : perimeter)
90 path << p.geoToMapProjection(c);
91 const QDoubleVector2D leftBound = p.geoToMapProjection(geoLeftBound);
92 wrapPath(path, leftBound,wrappedPath);
93 if (leftBoundWrapped)
94 *leftBoundWrapped = leftBound;
95}
96
97void wrapPath(const QList<QDoubleVector2D> &path,
98 const QDoubleVector2D &geoLeftBound,
99 QList<QDoubleVector2D> &wrappedPath)
100{
101 wrappedPath.clear();
102 // compute 3 sets of "wrapped" coordinates: one w regular mercator, one w regular mercator +- 1.0
103 for (QDoubleVector2D coord : path) {
104 // We can get NaN if the map isn't set up correctly, or the projection
105 // is faulty -- probably best thing to do is abort
106 if (!qIsFinite(coord.x()) || !qIsFinite(coord.y()))
107 return;
108
109 const bool isPointLessThanUnwrapBelowX = (coord.x() < geoLeftBound.x());
110 // unwrap x to preserve geometry if moved to border of map
111 if (isPointLessThanUnwrapBelowX)
112 coord.setX(coord.x() + 1.0);
113
114 wrappedPath.append(coord);
115 }
116}
117
118void clipPolygon(const QList<QDoubleVector2D> &wrappedPath,
120 QList<QList<QDoubleVector2D>> &clippedPaths,
121 QDoubleVector2D *leftBoundWrapped,
122 bool closed)
123{
124 // 2) Clip bounding box
125 clippedPaths.clear();
126 const QList<QDoubleVector2D> &visibleRegion = p.projectableGeometry();
127 if (visibleRegion.size()) {
128 QClipperUtils clipper;
129 clipper.addSubjectPath(wrappedPath, closed);
130 clipper.addClipPolygon(visibleRegion);
131 clippedPaths = clipper.execute(QClipperUtils::Intersection, QClipperUtils::pftEvenOdd,
133
134 if (leftBoundWrapped) {
135 // 2.1) update srcOrigin_ and leftBoundWrapped with the point with minimum X
136 QDoubleVector2D lb(qInf(), qInf());
137 for (const QList<QDoubleVector2D> &path : clippedPaths) {
138 for (const QDoubleVector2D &p : path) {
139 if (p.x() < lb.x() || (p.x() == lb.x() && p.y() < lb.y()))
140 // y-minimization needed to find the same point on polygon and border
141 lb = p;
142 }
143 }
144
145 if (qIsInf(lb.x())) // e.g., when the polygon is clipped entirely
146 return;
147
148 // 2.2) Prevent the conversion to and from clipper from introducing tiny negative offsets which,
149 // in turn will make the geometry wrap around.
150 lb.setX(qMax(leftBoundWrapped->x(), lb.x()));
151
152 *leftBoundWrapped = lb;
153 // srcOrigin_ = p.mapProjectionToGeo(p.unwrapMapProjection(lb));
154 }
155 } else {
156 clippedPaths.append(wrappedPath);
157 }
158}
159
160void projectBbox(const QList<QDoubleVector2D> &clippedBbox,
162 QPainterPath &projectedBbox)
163{
164 projectedBbox.clear();
165 bool first = true;
166 for (const auto &coord : clippedBbox) {
167 QDoubleVector2D point = p.wrappedMapProjectionToItemPosition(coord);
168 if (first) {
169 first = false;
170 projectedBbox.moveTo(point.toPointF());
171 } else {
172 projectedBbox.lineTo(point.toPointF());
173 }
174 }
175 projectedBbox.closeSubpath();
176}
177
178
179QRectF boundingRectangleFromList(const QList<QDoubleVector2D> &list)
180{
181 double xMin = qInf();
182 double xMax = -qInf();
183 double yMin = qInf();
184 double yMax = -qInf();
185 for (const auto &coord : list) {
186 xMin = qMin(xMin, coord.x());
187 xMax = qMax(xMax, coord.x());
188 yMin = qMin(yMin, coord.y());
189 yMax = qMax(yMax, coord.y());
190 }
191 return QRectF(xMin, yMin, xMax - xMin, yMax - yMin);
192}
193
194QList<QGeoCoordinate> greaterCirclePath(const QList<QGeoCoordinate> &cornerPoints,
196{
197 QList<QGeoCoordinate> path;
198 path.reserve(N);
199 //If the path has to be closed we include the first coordinate again at the end of the procedure
200 qsizetype lineCount = cornerPoints.size() - ((form == OpenPath) ? 1 : 0);
201 for (qsizetype i = 0; i < lineCount; i++) {
202 path.append(cornerPoints.at(i));
203 const double p_lat = cornerPoints.at(i).latitude() / 180.0 * M_PI; //latitude point p in rad
204 const double p_lon = cornerPoints.at(i).longitude() / 180.0 * M_PI; //longitude point p in rad
205 const double q_lat = cornerPoints.at((i + 1) % cornerPoints.size()).latitude() / 180.0 * M_PI; //latitude point q in rad
206 const double q_lon = cornerPoints.at((i + 1) % cornerPoints.size()).longitude() / 180.0 * M_PI;//longitude point q in rad
207
208 const double c_p_lat = cos(p_lat);
209 const double c_p_lon = cos(p_lon);
210 const double s_p_lat = sin(p_lat);
211 const double s_p_lon = sin(p_lon);
212 const double c_q_lat = cos(q_lat);
213 const double c_q_lon = cos(q_lon);
214 const double s_q_lat = sin(q_lat);
215 const double s_q_lon = sin(q_lon);
216
217 const QDoubleVector3D p(c_p_lat * c_p_lon,
218 c_p_lat * s_p_lon,
219 s_p_lat);
220 const QDoubleVector3D q(c_q_lat * c_q_lon,
221 c_q_lat * s_q_lon,
222 s_q_lat);
223 const double qp = QDoubleVector3D::dotProduct(q, p); //scalar product of q p
224
225 if (qp <= -1.0 || qp >= 1.0)
226 continue;
227
228 // the path from q to p will be parametrized as a combination of points p and q:
229 // x = s*p + t*q.
230 // We will then directly calculate the latitude and longitude of the interpolated point x
231 // from the latitude and longitude of cornerpoints q and p.
232 // The parameters s and t depend on each other to ensure that x is located on the
233 // surface of the earth: s*s + 2*s*t*qp + t*t = 1
234 // Their minimum value is 0, as negative values indicate a path that goes around earth
235 // long way. Their maximum value follows as
236 const double paramMax = sqrt(1 / (1 - qp * qp));
237
238 double t = 0.0;
239 for (int sign = 1; sign > -2.0; sign -= 2.0) {
240 for (; t <= paramMax && t >= 0.0; t += sign * paramMax / N / 2) {
241 // dependence between s and t requires a plus/minus
242 // therefore we solve this equation two times with sign = -1/+1
243 const double s = - t * qp + sign * sqrt(t * t * (qp * qp - 1.0) + 1.0);
244 if (s < 0.0) //s > paramMax will never happen. If s < 0 we are done.
245 break;
246 const double lat = asin(s * s_p_lat + t * s_q_lat);
247 const double lon = atan2(s * c_p_lat * s_p_lon + t * c_q_lat * s_q_lon,
248 s * c_p_lat * c_p_lon + t * c_q_lat * c_q_lon);
249 path.append(QGeoCoordinate(lat * 180.0 / M_PI, lon * 180.0 / M_PI));
250 }
251 t -= paramMax / N / 2;
252 }
253 }
254 if (form == OpenPath)
255 path.append(cornerPoints.last());
256 path.squeeze();
257 return path;
258}
259
260} // namespace QDeclarativeGeoMapItemUtils
261
void addSubjectPath(const QList< QDoubleVector2D > &path, bool closed)
Q_DECL_CONSTEXPR QPointF toPointF() const
void setX(double x)
Q_DECL_CONSTEXPR double x() const
static Q_DECL_CONSTEXPR double dotProduct(const QDoubleVector3D &v1, const QDoubleVector3D &v2)
\inmodule QtPositioning
Definition qlist.h:75
\inmodule QtGui
\inmodule QtCore\reentrant
Definition qrect.h:484
void wrapPath(const QList< QGeoCoordinate > &perimeter, const QGeoCoordinate &geoLeftBound, const QGeoProjectionWebMercator &p, QList< QDoubleVector2D > &wrappedPath, QList< QDoubleVector2D > &wrappedPathMinus1, QList< QDoubleVector2D > &wrappedPathPlus1, QDoubleVector2D *leftBoundWrapped)
QList< QGeoCoordinate > greaterCirclePath(const QList< QGeoCoordinate > &cornerPoints, greaterCirclePathForm form, int N)
QRectF boundingRectangleFromList(const QList< QDoubleVector2D > &list)
void clipPolygon(const QList< QDoubleVector2D > &wrappedPath, const QGeoProjectionWebMercator &p, QList< QList< QDoubleVector2D > > &clippedPaths, QDoubleVector2D *leftBoundWrapped, bool closed)
double distanceSqrPointLine(double p0_x, double p0_y, double p1_x, double p1_y, double p2_x, double p2_y)
void projectBbox(const QList< QDoubleVector2D > &clippedBbox, const QGeoProjectionWebMercator &p, QPainterPath &projectedBbox)
Combined button and popup list for selecting options.
bool qIsFinite(qfloat16 f) noexcept
Definition qfloat16.h:285
bool qIsInf(qfloat16 f) noexcept
Definition qfloat16.h:283
#define M_PI
Definition qmath.h:209
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION double qInf()
GLint first
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLuint coord
GLdouble GLdouble t
Definition qopenglext.h:243
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLsizei const GLchar *const * path
GLfloat GLfloat p
[1]
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
ptrdiff_t qsizetype
Definition qtypes.h:165
static int sign(int x)
QList< int > list
[14]