QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GeoMapItem.cc
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * (c) 2009-2024 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 *
5 * QGroundControl is licensed according to the terms in the file
6 * COPYING.md in the root of the source code directory.
7 *
8 ****************************************************************************/
9
10#include "GeoMapItem.h"
11
12#include <QtQml/QQmlComponent>
13#include <QtQuick3D/QQuick3DObject>
14
15#include <algorithm>
16#include <cmath>
17
18#include "GeoMapCamera.h"
19#include "GeoScene.h"
20#include "QGCLoggingCategory.h"
21#include "SurfacePatchModel.h"
22#include "TileMath.h"
23
24QGC_LOGGING_CATEGORY(GeoMapItemLog, "GeoMap.GeoMapItem")
25
26// Far enough off-screen for any plausible viewport
27static constexpr QPointF kParkedPosition{-1e6, -1e6};
28
29GeoMapItem::GeoMapItem(QQuickItem* parent) : QQuickItem(parent)
30{
31 connect(this, &QQuickItem::visibleChanged, this, &GeoMapItem::_updateNode3DVisibility);
32}
33
35{
36 delete _node3D;
37}
38
39void GeoMapItem::setCoordinate(const QGeoCoordinate& coordinate)
40{
41 if (coordinate == _coordinate) {
42 return;
43 }
44 _coordinate = coordinate;
45 _updatePosition();
46 _updateNode3DVisibility();
47 emit coordinateChanged();
48}
49
51{
52 if (anchorPoint == _anchorPoint) {
53 return;
54 }
55 _anchorPoint = anchorPoint;
56 _updatePosition();
57 emit anchorPointChanged();
58}
59
61{
62 if (scene == _scene) {
63 return;
64 }
65 qCDebug(GeoMapItemLog) << "scene" << (scene ? "set" : "cleared");
66 if (_scene) {
67 disconnect(_scene, nullptr, this, nullptr);
68 }
69 _scene = scene;
70 if (_scene) {
71 connect(_scene, &GeoScene::sceneOriginChanged, this, &GeoMapItem::_updatePosition);
72 connect(_scene, &GeoScene::terrainScaleChanged, this, &GeoMapItem::_updatePosition);
73 connect(_scene, &GeoScene::terrainScaleChanged, this, &GeoMapItem::_updateCrossfade);
74 connect(_scene, &GeoScene::cameraChanged, this, &GeoMapItem::_connectCamera);
75 connect(_scene, &GeoScene::content3DChanged, this, &GeoMapItem::_rebuildNode3D);
76 connect(_scene, &QObject::destroyed, this, [this] {
77 _scene = nullptr;
78 _connectCamera();
79 _rebuildNode3D();
80 emit sceneChanged();
81 });
82 }
83 _connectCamera();
84 _rebuildNode3D();
85 emit sceneChanged();
86}
87
89{
90 if (surfaceModel == _surfaceModel) {
91 return;
92 }
93 if (_surfaceModel) {
94 disconnect(_surfaceModel, nullptr, this, nullptr);
95 }
96 _surfaceModel = surfaceModel;
97 if (_surfaceModel) {
98 connect(_surfaceModel, &SurfacePatchModel::terrainHeightsChanged, this, &GeoMapItem::_terrainHeightsChanged);
99 connect(_surfaceModel, &QObject::destroyed, this, [this] {
100 _surfaceModel = nullptr;
101 _updatePosition();
102 emit surfaceModelChanged();
103 });
104 }
105 _updatePosition();
106 emit surfaceModelChanged();
107}
108
110{
111 if (mode == _altitudeMode) {
112 return;
113 }
114 _altitudeMode = mode;
115 _updatePosition();
116 emit altitudeModeChanged();
117}
118
120{
121 if (delegate3D == _delegate3D) {
122 return;
123 }
124 qCDebug(GeoMapItemLog) << "delegate3D" << (delegate3D ? "set" : "cleared");
125 if (_delegate3D) {
126 disconnect(_delegate3D, nullptr, this, nullptr);
127 }
128 _delegate3D = delegate3D;
129 if (_delegate3D) {
130 connect(_delegate3D, &QObject::destroyed, this, [this] {
131 _delegate3D = nullptr;
132 _rebuildNode3D();
133 emit delegate3DChanged();
134 });
135 }
136 _rebuildNode3D();
137 emit delegate3DChanged();
138}
139
141{
142 if (crossfade3D == _crossfade3D) {
143 return;
144 }
145 _crossfade3D = crossfade3D;
146 _updateCrossfade();
147 emit crossfade3DChanged();
148}
149
151{
152 node3DOpacity = std::clamp(node3DOpacity, 0.0, 1.0);
153 if (node3DOpacity == _node3DOpacity) {
154 return;
155 }
156 _node3DOpacity = node3DOpacity;
157 _updateCrossfade();
159}
160
161void GeoMapItem::_connectCamera()
162{
163 GeoMapCamera* const camera = _scene ? _scene->camera() : nullptr;
164 if (camera == _connectedCamera) {
165 _updatePosition();
166 return;
167 }
168 if (_connectedCamera) {
169 disconnect(_connectedCamera, nullptr, this, nullptr);
170 }
171 _connectedCamera = camera;
172 if (_connectedCamera) {
173 connect(_connectedCamera, &GeoMapCamera::scenePoseChanged, this, &GeoMapItem::_updatePosition);
174 connect(_connectedCamera, &GeoMapCamera::viewportSizeChanged, this, &GeoMapItem::_updatePosition);
175 connect(_connectedCamera, &GeoMapCamera::fieldOfViewChanged, this, &GeoMapItem::_updatePosition);
176 connect(_connectedCamera, &QObject::destroyed, this, [this] {
177 _connectedCamera = nullptr;
178 _updatePosition();
179 });
180 }
181 _updatePosition();
182}
183
184void GeoMapItem::_terrainHeightsChanged()
185{
186 if (_altitudeMode == AltitudeMode::ClampToGround) {
187 _updatePosition();
188 }
189}
190
191void GeoMapItem::_setProjected(bool projected)
192{
193 if (projected == _projected) {
194 return;
195 }
196 _projected = projected;
197 if (!_projected) {
198 // Park off-screen rather than toggling visible, which would fight
199 // user bindings on this item's own visible property
200 setPosition(kParkedPosition);
201 }
202 emit projectedChanged();
203}
204
205void GeoMapItem::_setScenePosition(const QVector3D& scenePosition)
206{
207 if (scenePosition == _scenePosition) {
208 return;
209 }
210 _scenePosition = scenePosition;
211 if (_node3D) {
212 _node3D->setProperty("position", _scenePosition);
213 }
215}
216
217void GeoMapItem::_rebuildNode3D()
218{
219 const bool hadNode = _node3D != nullptr;
220 if (_node3D) {
221 // Unhook from the scene immediately; deleteLater is safer than delete
222 // here since this can run from the container's destroyed signal
223 if (auto* const object3D = qobject_cast<QQuick3DObject*>(_node3D)) {
224 object3D->setParentItem(nullptr);
225 }
226 _node3D->deleteLater();
227 _node3D = nullptr;
228 }
229
230 QQuick3DObject* const container = _scene ? qobject_cast<QQuick3DObject*>(_scene->content3D()) : nullptr;
231 if (_delegate3D && container) {
232 QObject* const node = _delegate3D->create(_delegate3D->creationContext());
233 if (!node) {
234 qCWarning(GeoMapItemLog) << "delegate3D failed to create:" << _delegate3D->errorString();
235 } else if (auto* const object3D = qobject_cast<QQuick3DObject*>(node)) {
236 node->setParent(container);
237 object3D->setParentItem(container);
238 node->setProperty("position", _scenePosition);
239 _node3D = node;
240 _updateNode3DVisibility();
241 } else {
242 // Caller contract: delegate3D must produce a QQuick3DNode
243 qCWarning(GeoMapItemLog) << "delegate3D did not produce a QQuick3DObject";
244 delete node;
245 }
246 }
247
248 _updateCrossfade();
249 if (hadNode || _node3D) {
250 emit node3DChanged();
251 }
252}
253
254void GeoMapItem::_updateNode3DVisibility()
255{
256 if (_node3D) {
257 _node3D->setProperty("visible", isVisible() && _coordinate.isValid());
258 }
259}
260
261void GeoMapItem::_updateCrossfade()
262{
263 if (!_crossfade3D) {
264 if (_node3D) {
265 _node3D->setProperty("opacity", _node3DOpacity);
266 }
267 if (_contentOpacity2D != 1.0) {
268 _contentOpacity2D = 1.0;
270 }
271 return;
272 }
273 const qreal terrainScale = _scene ? std::clamp(_scene->terrainScale(), 0.0, 1.0) : 0.0;
274 if (_node3D) {
275 _node3D->setProperty("opacity", terrainScale * _node3DOpacity);
276 }
277 const qreal contentOpacity2D = _node3D ? (1.0 - terrainScale) : 1.0;
278 if (contentOpacity2D != _contentOpacity2D) {
279 _contentOpacity2D = contentOpacity2D;
281 }
282}
283
284void GeoMapItem::_updatePosition()
285{
286 if (!_scene || !_coordinate.isValid()) {
287 _setScenePosition(QVector3D());
288 _setProjected(false);
289 return;
290 }
291
292 double altitude = 0.0;
293 if (_altitudeMode == AltitudeMode::Absolute) {
294 altitude = std::isfinite(_coordinate.altitude()) ? _coordinate.altitude() : 0.0;
295 } else if (_surfaceModel) {
296 altitude = _surfaceModel->terrainHeightAt(_coordinate);
297 }
298
299 const QPointF world = TileMath::geoToWorld(_coordinate);
300 const double sceneZ = altitude * _scene->verticalScale() * _scene->terrainScale();
301 const QPointF origin = _scene->sceneOrigin();
302 _setScenePosition(QVector3D(static_cast<float>(world.x() - origin.x()), static_cast<float>(world.y() - origin.y()),
303 static_cast<float>(sceneZ)));
304
305 GeoMapCamera* const camera = _scene->camera();
306 if (!camera) {
307 _setProjected(false);
308 return;
309 }
310 const auto screen = camera->worldToScreen(world, sceneZ);
311 if (!screen) {
312 _setProjected(false);
313 return;
314 }
315 setPosition(*screen - _anchorPoint);
316 _setProjected(true);
317}
static constexpr QPointF kParkedPosition
Definition GeoMapItem.cc:27
#define QGC_LOGGING_CATEGORY(name, categoryStr)
std::optional< QPointF > worldToScreen(const QPointF &worldGround, double worldZ=0.0) const
void scenePoseChanged()
void fieldOfViewChanged()
void viewportSizeChanged()
SurfacePatchModel * surfaceModel() const
Definition GeoMapItem.h:81
void crossfade3DChanged()
void setAnchorPoint(const QPointF &anchorPoint)
Definition GeoMapItem.cc:50
void setNode3DOpacity(qreal node3DOpacity)
void setCrossfade3D(bool crossfade3D)
void surfaceModelChanged()
qreal contentOpacity2D() const
Definition GeoMapItem.h:127
AltitudeMode
How the coordinate's vertical position is resolved.
Definition GeoMapItem.h:61
@ ClampToGround
sit on the rendered terrain surface (default)
Definition GeoMapItem.h:62
@ Absolute
use coordinate.altitude (NaN treated as 0)
Definition GeoMapItem.h:63
void projectedChanged()
void setAltitudeMode(AltitudeMode mode)
void setDelegate3D(QQmlComponent *delegate3D)
void node3DChanged()
QVector3D scenePosition() const
Definition GeoMapItem.h:96
void scenePositionChanged()
GeoMapItem(QQuickItem *parent=nullptr)
Definition GeoMapItem.cc:29
void setScene(GeoScene *scene)
Definition GeoMapItem.cc:60
void coordinateChanged()
QPointF anchorPoint() const
Definition GeoMapItem.h:73
bool projected() const
Definition GeoMapItem.h:91
void anchorPointChanged()
void setSurfaceModel(SurfacePatchModel *surfaceModel)
Definition GeoMapItem.cc:88
void delegate3DChanged()
void sceneChanged()
bool crossfade3D() const
Definition GeoMapItem.h:114
GeoScene * scene() const
Definition GeoMapItem.h:77
void node3DOpacityChanged()
QGeoCoordinate coordinate() const
Definition GeoMapItem.h:67
~GeoMapItem() override
Definition GeoMapItem.cc:34
void altitudeModeChanged()
QQmlComponent * delegate3D() const
Definition GeoMapItem.h:103
void setCoordinate(const QGeoCoordinate &coordinate)
Definition GeoMapItem.cc:39
void contentOpacity2DChanged()
qreal node3DOpacity() const
Definition GeoMapItem.h:120
void sceneOriginChanged()
void terrainScaleChanged()
void cameraChanged()
GeoMapCamera * camera() const
Definition GeoScene.h:49
void content3DChanged()
void terrainHeightsChanged()
terrainHeightAt answers changed somewhere: consumers re-query
QPointF geoToWorld(const QGeoCoordinate &coord)
Geo -> world meters. Latitude is clamped to +/-kMaxLatitude.
Definition TileMath.cc:23