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