QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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 "qgswindow3dengine.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#include <QColor>
39
40
42
43
44QgsRubberBand3D::QgsRubberBand3D( Qgs3DMapSettings &map, QgsWindow3DEngine *engine, Qt3DCore::QEntity *parentEntity )
45{
46 mMapSettings = &map;
47
48 mEntity = new Qt3DCore::QEntity( parentEntity );
49
50 QgsLineVertexData dummyLineData;
51 mGeometry = dummyLineData.createGeometry( mEntity );
52
53 Q_ASSERT( mGeometry->attributes().count() == 2 );
54 mPositionAttribute = mGeometry->attributes()[0];
55 mIndexAttribute = mGeometry->attributes()[1];
56
57 mGeomRenderer = new Qt3DRender::QGeometryRenderer;
58 mGeomRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
59 mGeomRenderer->setGeometry( mGeometry );
60 mGeomRenderer->setPrimitiveRestartEnabled( true );
61 mGeomRenderer->setRestartIndexValue( 0 );
62
63 mEntity->addComponent( mGeomRenderer );
64
65 mLineMaterial = new QgsLineMaterial;
66 mLineMaterial->setLineWidth( 3 );
67 mLineMaterial->setLineColor( Qt::red );
68
69 QObject::connect( engine, &QgsAbstract3DEngine::sizeChanged, mLineMaterial, [this, engine]
70 {
71 mLineMaterial->setViewportSize( engine->size() );
72 } );
73 mLineMaterial->setViewportSize( engine->size() );
74
75 mEntity->addComponent( mLineMaterial );
76}
77
78QgsRubberBand3D::~QgsRubberBand3D()
79{
80 delete mEntity;
81}
82
83float QgsRubberBand3D::width() const
84{
85 return mLineMaterial->lineWidth();
86}
87
88void QgsRubberBand3D::setWidth( float width )
89{
90 mLineMaterial->setLineWidth( width );
91}
92
93QColor QgsRubberBand3D::color() const
94{
95 return mLineMaterial->lineColor();
96}
97
98void QgsRubberBand3D::setColor( QColor color )
99{
100 mLineMaterial->setLineColor( color );
101}
102
103void QgsRubberBand3D::reset()
104{
105 mLineString.clear();
106 updateGeometry();
107}
108
109void QgsRubberBand3D::addPoint( const QgsPoint &pt )
110{
111 mLineString.addVertex( pt );
112 updateGeometry();
113}
114
115void QgsRubberBand3D::removeLastPoint()
116{
117 const int lastVertexIndex = mLineString.numPoints() - 1;
118 mLineString.deleteVertex( QgsVertexId( 0, 0, lastVertexIndex ) );
119 updateGeometry();
120}
121
122void QgsRubberBand3D::updateGeometry()
123{
124 QgsLineVertexData lineData;
125 lineData.withAdjacency = true;
126 lineData.init( Qgis::AltitudeClamping::Absolute, Qgis::AltitudeBinding::Vertex, 0, mMapSettings );
127 lineData.addLineString( mLineString );
128
129 mPositionAttribute->buffer()->setData( lineData.createVertexBuffer() );
130 mIndexAttribute->buffer()->setData( lineData.createIndexBuffer() );
131 mGeomRenderer->setVertexCount( lineData.indexes.count() );
132}
133
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
@ Vertex
Clamp every vertex of feature.
void sizeChanged()
Emitted after a call to setSize()
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:31