QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
ObstacleDistanceOverlayMap.qml
Go to the documentation of this file.
1import QtQuick
2
3import QGroundControl
4
5Item {
6 id: root
7 anchors.fill: parent
8 property var showText: obstacleDistance._showText
9
10 function paintObstacleOverlay(ctx) {
11 const vehiclePoint = _root.fromCoordinate(_activeVehicleCoordinate, false)
12 const centerX = vehiclePoint.x
13 const centerY = vehiclePoint.y
14 const maxRadiusPixels = 0.9 * root.height / 2 // Max pixels to center
15 const minRadiusPixels = maxRadiusPixels * 0.2
16 const metersPerPixelInCycle = (maxRadiusPixels - minRadiusPixels) / obstacleDistance._maxRadiusMeters
17
18 const leftCoord = mapControl.toCoordinate(Qt.point(0, root.y), false)
19 const rightCoord = mapControl.toCoordinate(Qt.point(100, root.y), false)
20 const metersIn100Pixels = leftCoord.distanceTo(rightCoord)
21 const metersPerPixel = 100.0 / metersIn100Pixels
22
23 var minGradPixels = minRadiusPixels
24 var maxGradPixels = maxRadiusPixels
25 var metersToPixels = metersPerPixelInCycle
26 if (metersIn100Pixels < 4) {
27 minGradPixels = 0
28 maxGradPixels = obstacleDistance._maxRadiusMeters * metersPerPixel
29 metersToPixels = metersPerPixel
30 }
31
32 var grad = ctx.createRadialGradient(centerX, centerY, minGradPixels, centerX, centerY, maxGradPixels)
33 grad.addColorStop(0, Qt.rgba(1, 0, 0, 1))
34 grad.addColorStop(0.1, Qt.rgba(1, 0, 0, 0.7))
35 grad.addColorStop(0.5, Qt.rgba(1, 0.64, 0, 0.7))
36 grad.addColorStop(0.65, Qt.rgba(1, 0.64, 0, 0.3))
37 grad.addColorStop(0.95, Qt.rgba(0, 1, 0, 0.3))
38 grad.addColorStop(1, Qt.rgba(0, 1, 0, 0))
39
40 var points = []
41 const height = minRadiusPixels / 8
42 for (var i = 0; i < obstacleDistance._rangesLen; ++i) {
43 const deg = i * obstacleDistance._incrementDeg
44 const rad = deg * Math.PI / 180.0
45 const m = obstacleDistance._ranges[obstacleDistance._degToRangeIdx(deg, true)] / 100.0
46 const pixels = minGradPixels + m * metersToPixels
47 const outerX = centerX + pixels * Math.sin(rad)
48 const outerY = centerY - pixels * Math.cos(rad)
49 const innerX = centerX + (pixels - height) * Math.sin(rad)
50 const innerY = centerY - (pixels - height) * Math.cos(rad)
51
52 points.push({'outer_x': outerX, 'outer_y': outerY, 'inner_x': innerX, 'inner_y': innerY, 'range': m})
53 }
54
55 ctx.strokeStyle = Qt.rgba(0, 0, 0, 0.8)
56 ctx.font = "bold 22px sans-serif"
57 ctx.lineWidth = 2;
58 var mPrev = -1
59 for (var i = 0; i < points.length; i += 3) {
60 const i3 = (i + 3) % points.length // catch the line from the last to the first point
61
62 ctx.beginPath()
63 ctx.fillStyle = grad
64 ctx.moveTo(points[i].inner_x, points[i].inner_y)
65 ctx.lineTo(points[i].outer_x, points[i].outer_y)
66 ctx.bezierCurveTo(
67 points[i + 1].outer_x, points[i + 1].outer_y,
68 points[i + 2].outer_x, points[i + 2].outer_y,
69 points[i3].outer_x, points[i3].outer_y)
70 ctx.lineTo(points[i3].inner_x, points[i3].inner_y)
71 ctx.bezierCurveTo(
72 points[i3].inner_x, points[i3].inner_y,
73 points[i + 2].inner_x, points[i + 2].inner_y,
74 points[i + 1].inner_x, points[i + 1].inner_y)
75 ctx.fill()
76
77 if (showText) {
78 var iMin = i
79 for (var k = iMin + 1; k < iMin + 2; ++k) {
80 const idx = k % points.length
81 if (points[idx].range < points[iMin].range)
82 iMin = idx
83 }
84
85 var m = points[iMin].range
86 if (m < obstacleDistance._maxRadiusMeters && Math.abs(m - mPrev) > 2.0) {
87 const textX = points[iMin].inner_x
88 const textY = points[iMin].inner_y
89
90 ctx.fillStyle = Qt.rgba(1, 1, 1, 0.9)
91 const text = obstacleDistance._rangeToShow(m)
92 ctx.strokeText(text, textX, textY)
93 ctx.fillText(text, textX, textY)
94 mPrev = m
95 }
96 }
97 }
98 }
99
100 ObstacleDistanceOverlay {
101 id: obstacleDistance
102 }
103}