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