QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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
20#include "qgsmarkersymbol.h"
21#include "qgswindow3dengine.h"
22#include "qgslinevertexdata_p.h"
23#include "qgslinematerial_p.h"
24#include "qgsvertexid.h"
25#include "qgslinestring.h"
26#include "qgssymbollayer.h"
27#include "qgs3dmapsettings.h"
28
29#include <Qt3DCore/QEntity>
30
31#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
32#include <Qt3DRender/QAttribute>
33#include <Qt3DRender/QBuffer>
34#include <Qt3DRender/QGeometry>
35#else
36#include <Qt3DCore/QAttribute>
37#include <Qt3DCore/QBuffer>
38#include <Qt3DCore/QGeometry>
39#endif
40
41#include <Qt3DRender/QGeometryRenderer>
42#include <QColor>
43
44
46
47
48QgsRubberBand3D::QgsRubberBand3D( Qgs3DMapSettings &map, QgsWindow3DEngine *engine, Qt3DCore::QEntity *parentEntity )
49{
50 mMapSettings = &map;
51 mEngine = engine;
52
53 // Rubberband line
54 mLineEntity = new Qt3DCore::QEntity( parentEntity );
55
56 QgsLineVertexData dummyLineData;
57 mGeometry = dummyLineData.createGeometry( mLineEntity );
58
59 Q_ASSERT( mGeometry->attributes().count() == 2 );
60 mPositionAttribute = mGeometry->attributes()[0];
61 mIndexAttribute = mGeometry->attributes()[1];
62
63 mGeomRenderer = new Qt3DRender::QGeometryRenderer;
64 mGeomRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
65 mGeomRenderer->setGeometry( mGeometry );
66 mGeomRenderer->setPrimitiveRestartEnabled( true );
67 mGeomRenderer->setRestartIndexValue( 0 );
68
69 mLineEntity->addComponent( mGeomRenderer );
70
71 mLineMaterial = new QgsLineMaterial;
72 mLineMaterial->setLineWidth( 3 );
73 mLineMaterial->setLineColor( Qt::red );
74
75 QObject::connect( engine, &QgsAbstract3DEngine::sizeChanged, mLineMaterial, [this, engine]
76 {
77 mLineMaterial->setViewportSize( engine->size() );
78 } );
79 mLineMaterial->setViewportSize( engine->size() );
80
81 mLineEntity->addComponent( mLineMaterial );
82
83 Qt3DCore::QTransform *lineTransform = new Qt3DCore::QTransform;
84 mLineEntity->addComponent( lineTransform );
85
86 // Rubberband vertex markers
87 mMarkerEntity = new Qt3DCore::QEntity( parentEntity );
88 mMarkerGeometry = new QgsBillboardGeometry();
89 mMarkerGeometryRenderer = new Qt3DRender::QGeometryRenderer;
90 mMarkerGeometryRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::Points );
91 mMarkerGeometryRenderer->setGeometry( mMarkerGeometry );
92 mMarkerGeometryRenderer->setVertexCount( mMarkerGeometry->count() );
93
94 const QVariantMap props
95 {
96 {QStringLiteral( "color" ), QStringLiteral( "red" ) },
97 {QStringLiteral( "size" ), 6 },
98 {QStringLiteral( "outline_color" ), QStringLiteral( "green" ) },
99 {QStringLiteral( "outline_width" ), 0.5 }
100 };
101
102 mMarkerSymbol = QgsMarkerSymbol::createSimple( props );
103 updateMarkerMaterial();
104 mMarkerEntity->addComponent( mMarkerGeometryRenderer );
105
106 Qt3DCore::QTransform *markerTransform = new Qt3DCore::QTransform;
107 mMarkerEntity->addComponent( markerTransform );
108}
109
110QgsRubberBand3D::~QgsRubberBand3D()
111{
112 delete mLineEntity;
113 delete mMarkerEntity;
114 delete mMarkerSymbol;
115}
116
117float QgsRubberBand3D::width() const
118{
119 return mLineMaterial->lineWidth();
120}
121
122void QgsRubberBand3D::setWidth( float width )
123{
124 mLineMaterial->setLineWidth( width );
125 mMarkerSymbol->setSize( width );
126 updateMarkerMaterial();
127}
128
129QColor QgsRubberBand3D::color() const
130{
131 return mLineMaterial->lineColor();
132}
133
134void QgsRubberBand3D::setColor( QColor color )
135{
136 mLineMaterial->setLineColor( color );
137 mMarkerSymbol->setColor( color.lighter( 130 ) );
138 if ( mMarkerSymbol->symbolLayerCount() > 0 && mMarkerSymbol->symbolLayer( 0 )->layerType() == QLatin1String( "SimpleMarker" ) )
139 {
140 static_cast<QgsMarkerSymbolLayer *>( mMarkerSymbol->symbolLayer( 0 ) )->setStrokeColor( color );
141 }
142 updateMarkerMaterial();
143}
144
145void QgsRubberBand3D::reset()
146{
147 mLineString.clear();
148 mShowLastMarker = false;
149 updateGeometry();
150}
151
152void QgsRubberBand3D::addPoint( const QgsPoint &pt )
153{
154 mLineString.addVertex( pt );
155 updateGeometry();
156}
157
158void QgsRubberBand3D::removeLastPoint()
159{
160 const int lastVertexIndex = mLineString.numPoints() - 1;
161 mLineString.deleteVertex( QgsVertexId( 0, 0, lastVertexIndex ) );
162 updateGeometry();
163}
164
165void QgsRubberBand3D::moveLastPoint( const QgsPoint &pt )
166{
167 const int lastVertexIndex = mLineString.numPoints() - 1;
168 mLineString.moveVertex( QgsVertexId( 0, 0, lastVertexIndex ), pt );
169 updateGeometry();
170}
171
172void QgsRubberBand3D::updateGeometry()
173{
174 QgsLineVertexData lineData;
175 lineData.withAdjacency = true;
177 lineData.addLineString( mLineString );
178
179 mPositionAttribute->buffer()->setData( lineData.createVertexBuffer() );
180 mIndexAttribute->buffer()->setData( lineData.createIndexBuffer() );
181 mGeomRenderer->setVertexCount( lineData.indexes.count() );
182
183 // first entry is empty for primitive restart
184 lineData.vertices.pop_front();
185
186 // we may not want a marker on the last point as it's tracked by the mouse cursor
187 if ( !mShowLastMarker && !lineData.vertices.isEmpty() )
188 lineData.vertices.pop_back();
189
190 mMarkerGeometry->setPoints( lineData.vertices );
191 mMarkerGeometryRenderer->setVertexCount( lineData.vertices.count() );
192}
193
194void QgsRubberBand3D::updateMarkerMaterial()
195{
196 delete mMarkerMaterial;
197 mMarkerMaterial = new QgsPoint3DBillboardMaterial();
198 mMarkerMaterial->setTexture2DFromSymbol( mMarkerSymbol, Qgs3DRenderContext::fromMapSettings( mMapSettings ) );
199 mMarkerEntity->addComponent( mMarkerMaterial );
200
201 //TODO: QgsAbstract3DEngine::sizeChanged should have const QSize &size param
202 QObject::connect( mEngine, &QgsAbstract3DEngine::sizeChanged, mMarkerMaterial, [this]
203 {
204 mMarkerMaterial->setViewportSize( mEngine->size() );
205 } );
206 mMarkerMaterial->setViewportSize( mEngine->size() );
207}
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
@ Vertex
Clamp every vertex of feature.
static Qgs3DRenderContext fromMapSettings(const Qgs3DMapSettings *mapSettings)
Creates an initialized Qgs3DRenderContext instance from given Qgs3DMapSettings.
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0)
void sizeChanged()
Emitted after a call to setSize()
Abstract base class for marker symbol layers.
static QgsMarkerSymbol * createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
QSize size() const override
Returns size of the engine's rendering area in pixels.
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30