QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
Viewer3DInstancing.cc
Go to the documentation of this file.
2
4
5#include <cmath>
6
7QGC_LOGGING_CATEGORY(Viewer3DInstancingLog, "Viewer3d.Viewer3DInstancing")
8
9Viewer3DInstancing::Viewer3DInstancing(QQuick3DObject *parent)
10 : QQuick3DInstancing(parent)
11{
12}
13
15{
16 qCDebug(Viewer3DInstancingLog) << "Clearing" << _entries.size() << "entries";
17 _entries.clear();
18 _dirty = true;
19 markDirty();
20 emit countChanged();
21}
22
23void Viewer3DInstancing::addEntry(const QVector3D &position,
24 const QVector3D &scale,
25 const QQuaternion &rotation,
26 const QColor &color)
27{
28 _entries.append({position, scale, rotation, color});
29 _dirty = true;
30 markDirty();
31 emit countChanged();
32}
33
34void Viewer3DInstancing::addLineSegment(const QVector3D &p1,
35 const QVector3D &p2,
36 float lineWidth,
37 const QColor &color)
38{
39 const QVector3D delta = p2 - p1;
40 const float distance = delta.length();
41 if (distance < 1e-6f) {
42 qCDebug(Viewer3DInstancingLog) << "Skipping degenerate line segment (length < 1e-6)";
43 return;
44 }
45
46 // #Cylinder: 100 units tall on Y, radius 50
47 const float radius = lineWidth * 0.1f;
48 const QVector3D position = (p1 + p2) * 0.5f;
49 const QVector3D scale(radius / 50.0f, distance / 100.0f, radius / 50.0f);
50
51 const QVector3D yAxis(0.0f, 1.0f, 0.0f);
52 const QQuaternion rotation = QQuaternion::rotationTo(yAxis, delta.normalized());
53
54 _entries.append({position, scale, rotation, color});
55 _dirty = true;
56 markDirty();
57 emit countChanged();
58}
59
61{
62 return _entries.size();
63}
64
66{
67 return _selectedIndex;
68}
69
71{
72 if (_selectedIndex == index) return;
73 _selectedIndex = index;
74 _dirty = true;
75 markDirty();
77}
78
79QByteArray Viewer3DInstancing::getInstanceBuffer(int *instanceCount)
80{
81 if (_dirty) {
82 _instanceData.clear();
83 for (int i = 0; i < _entries.size(); ++i) {
84 const auto &entry = _entries[i];
85 const QColor color = (i == _selectedIndex) ? kHighlightColor : entry.color;
86 auto tableEntry = calculateTableEntry(entry.position, entry.scale, entry.rotation.toEulerAngles(), color);
87 _instanceData.append(reinterpret_cast<const char *>(&tableEntry), sizeof(tableEntry));
88 }
89 _dirty = false;
90 }
91
92 if (instanceCount) {
93 *instanceCount = _entries.size();
94 }
95 return _instanceData;
96}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QByteArray getInstanceBuffer(int *instanceCount) override
void addEntry(const QVector3D &position, const QVector3D &scale, const QQuaternion &rotation, const QColor &color)
void selectedIndexChanged()
void setSelectedIndex(int index)
void addLineSegment(const QVector3D &p1, const QVector3D &p2, float lineWidth, const QColor &color)