QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsrubberband3d.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrubberband3d.cpp
3 --------------------------------------
4 Date : June 2021
5 Copyright : (C) 2021 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsrubberband3d.h"
17
18#include "qgscameracontroller.h"
19#include "qgslinevertexdata_p.h"
20#include "qgslinematerial_p.h"
21#include "qgsvertexid.h"
22#include "qgslinestring.h"
23
24#include <Qt3DCore/QEntity>
25
26#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
27#include <Qt3DRender/QAttribute>
28#include <Qt3DRender/QBuffer>
29#include <Qt3DRender/QGeometry>
30#else
31#include <Qt3DCore/QAttribute>
32#include <Qt3DCore/QBuffer>
33#include <Qt3DCore/QGeometry>
34#endif
35
36#include <Qt3DRender/QGeometryRenderer>
37#include <Qt3DRender/QMaterial>
38
39
41
42
43QgsRubberBand3D::QgsRubberBand3D( Qgs3DMapSettings &map, QgsCameraController *cameraController, Qt3DCore::QEntity *parentEntity )
44{
45 mMapSettings = &map;
46
47 mEntity = new Qt3DCore::QEntity( parentEntity );
48
49 QgsLineVertexData dummyLineData;
50 mGeometry = dummyLineData.createGeometry( mEntity );
51
52 Q_ASSERT( mGeometry->attributes().count() == 2 );
53 mPositionAttribute = mGeometry->attributes()[0];
54 mIndexAttribute = mGeometry->attributes()[1];
55
56 mGeomRenderer = new Qt3DRender::QGeometryRenderer;
57 mGeomRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
58 mGeomRenderer->setGeometry( mGeometry );
59 mGeomRenderer->setPrimitiveRestartEnabled( true );
60 mGeomRenderer->setRestartIndexValue( 0 );
61
62 mEntity->addComponent( mGeomRenderer );
63
64 mLineMaterial = new QgsLineMaterial;
65 mLineMaterial->setLineWidth( 3 );
66 mLineMaterial->setLineColor( Qt::red );
67
68 QObject::connect( cameraController, &QgsCameraController::viewportChanged, mLineMaterial, [this, cameraController]
69 {
70 mLineMaterial->setViewportSize( cameraController->viewport().size() );
71 } );
72 mLineMaterial->setViewportSize( cameraController->viewport().size() );
73
74 mEntity->addComponent( mLineMaterial );
75}
76
77QgsRubberBand3D::~QgsRubberBand3D()
78{
79 delete mEntity;
80}
81
82float QgsRubberBand3D::width() const
83{
84 return mLineMaterial->lineWidth();
85}
86
87void QgsRubberBand3D::setWidth( float width )
88{
89 mLineMaterial->setLineWidth( width );
90}
91
92QColor QgsRubberBand3D::color() const
93{
94 return mLineMaterial->lineColor();
95}
96
97void QgsRubberBand3D::setColor( QColor color )
98{
99 mLineMaterial->setLineColor( color );
100}
101
102void QgsRubberBand3D::reset()
103{
104 mLineString.clear();
105 updateGeometry();
106}
107
108void QgsRubberBand3D::addPoint( const QgsPoint &pt )
109{
110 mLineString.addVertex( pt );
111 updateGeometry();
112}
113
114void QgsRubberBand3D::removeLastPoint()
115{
116 const int lastVertexIndex = mLineString.numPoints() - 1;
117 mLineString.deleteVertex( QgsVertexId( 0, 0, lastVertexIndex ) );
118 updateGeometry();
119}
120
121void QgsRubberBand3D::updateGeometry()
122{
123 QgsLineVertexData lineData;
124 lineData.withAdjacency = true;
125 lineData.init( Qgis::AltitudeClamping::Absolute, Qgis::AltitudeBinding::Vertex, 0, mMapSettings );
126 lineData.addLineString( mLineString );
127
128 mPositionAttribute->buffer()->setData( lineData.createVertexBuffer() );
129 mIndexAttribute->buffer()->setData( lineData.createIndexBuffer() );
130 mGeomRenderer->setVertexCount( lineData.indexes.count() );
131}
132
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
@ Vertex
Clamp every vertex of feature.
void viewportChanged()
Emitted when viewport rectangle has been updated.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Utility class for identifying a unique vertex within a geometry.
Definition: qgsvertexid.h:31