From 0170671dcd91a1b59e0789b100fa65d7843ba36c Mon Sep 17 00:00:00 2001 From: uclaros Date: Fri, 10 Jan 2025 17:28:07 +0200 Subject: [PATCH] Allow closed polygon shapes in 3d rubber bands --- src/3d/qgsrubberband3d.cpp | 25 +++++++++++++++++-------- src/3d/qgsrubberband3d.h | 2 ++ src/3d/symbols/qgslinevertexdata_p.cpp | 7 ++++++- src/3d/symbols/qgslinevertexdata_p.h | 2 +- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/3d/qgsrubberband3d.cpp b/src/3d/qgsrubberband3d.cpp index 9aaa4ccf42aa..d326e7c12c15 100644 --- a/src/3d/qgsrubberband3d.cpp +++ b/src/3d/qgsrubberband3d.cpp @@ -50,7 +50,7 @@ QgsRubberBand3D::QgsRubberBand3D( Qgs3DMapSettings &map, QgsWindow3DEngine *engi , mEngine( engine ) , mGeometryType( geometryType ) { - if ( mGeometryType == Qgis::GeometryType::Line ) + if ( mGeometryType == Qgis::GeometryType::Line || mGeometryType == Qgis::GeometryType::Polygon ) { // Rubberband line mLineEntity = new Qt3DCore::QEntity( parentEntity ); @@ -110,7 +110,7 @@ void QgsRubberBand3D::setWidth( float width ) { mWidth = width; - if ( mGeometryType == Qgis::GeometryType::Line ) + if ( mGeometryType == Qgis::GeometryType::Line || mGeometryType == Qgis::GeometryType::Polygon ) { // when highlighting lines, the vertex markers should be wider mLineMaterial->setLineWidth( width ); @@ -130,7 +130,7 @@ void QgsRubberBand3D::setColor( QColor color ) { mColor = color; - if ( mGeometryType == Qgis::GeometryType::Line ) + if ( mGeometryType == Qgis::GeometryType::Line || mGeometryType == Qgis::GeometryType::Polygon ) { mLineMaterial->setLineColor( color ); mMarkerSymbol->setColor( color.lighter( 130 ) ); @@ -151,12 +151,14 @@ void QgsRubberBand3D::setMarkerType( MarkerType marker ) { mMarkerType = marker; + const bool lineOrPolygon = mGeometryType == Qgis::GeometryType::Line || mGeometryType == Qgis::GeometryType::Polygon; + const QVariantMap props { - { QStringLiteral( "color" ), mGeometryType == Qgis::GeometryType::Line ? mColor.lighter( 130 ).name() : mColor.name() }, + { QStringLiteral( "color" ), lineOrPolygon ? mColor.lighter( 130 ).name() : mColor.name() }, { QStringLiteral( "size_unit" ), QStringLiteral( "pixel" ) }, - { QStringLiteral( "size" ), QString::number( mGeometryType == Qgis::GeometryType::Line ? mWidth * 3.f : mWidth ) }, + { QStringLiteral( "size" ), QString::number( lineOrPolygon ? mWidth * 3.f : mWidth ) }, { QStringLiteral( "outline_color" ), mColor.name() }, - { QStringLiteral( "outline_width" ), QString::number( mGeometryType == Qgis::GeometryType::Line ? 0.5 : 1 ) }, + { QStringLiteral( "outline_width" ), QString::number( lineOrPolygon ? 0.5 : 1 ) }, { QStringLiteral( "name" ), mMarkerType == Square ? QStringLiteral( "square" ) : QStringLiteral( "circle" ) } }; @@ -181,6 +183,12 @@ void QgsRubberBand3D::addPoint( const QgsPoint &pt ) updateGeometry(); } +void QgsRubberBand3D::setPoints( const QgsLineString &points ) +{ + mLineString = points; + updateGeometry(); +} + void QgsRubberBand3D::removeLastPoint() { const int lastVertexIndex = mLineString.numPoints() - 1; @@ -200,9 +208,10 @@ void QgsRubberBand3D::updateGeometry() QgsLineVertexData lineData; lineData.withAdjacency = true; lineData.init( Qgis::AltitudeClamping::Absolute, Qgis::AltitudeBinding::Vertex, 0, Qgs3DRenderContext::fromMapSettings( mMapSettings ), mMapSettings->origin() ); - lineData.addLineString( mLineString ); + const bool closed = mGeometryType == Qgis::GeometryType::Polygon; + lineData.addLineString( mLineString, 0, closed ); - if ( mGeometryType == Qgis::GeometryType::Line ) + if ( mGeometryType == Qgis::GeometryType::Line || mGeometryType == Qgis::GeometryType::Polygon ) { mPositionAttribute->buffer()->setData( lineData.createVertexBuffer() ); mIndexAttribute->buffer()->setData( lineData.createIndexBuffer() ); diff --git a/src/3d/qgsrubberband3d.h b/src/3d/qgsrubberband3d.h index 609e6117ac1e..51b33e14268e 100644 --- a/src/3d/qgsrubberband3d.h +++ b/src/3d/qgsrubberband3d.h @@ -120,6 +120,8 @@ class _3D_EXPORT QgsRubberBand3D void addPoint( const QgsPoint &pt ); + void setPoints( const QgsLineString &points ); + void removeLastPoint(); void moveLastPoint( const QgsPoint &pt ); diff --git a/src/3d/symbols/qgslinevertexdata_p.cpp b/src/3d/symbols/qgslinevertexdata_p.cpp index 5636fff7ec9c..d453815417c5 100644 --- a/src/3d/symbols/qgslinevertexdata_p.cpp +++ b/src/3d/symbols/qgslinevertexdata_p.cpp @@ -117,7 +117,7 @@ Qt3DQGeometry *QgsLineVertexData::createGeometry( Qt3DCore::QNode *parent ) return geom; } -void QgsLineVertexData::addLineString( const QgsLineString &lineString, float extraHeightOffset ) +void QgsLineVertexData::addLineString( const QgsLineString &lineString, float extraHeightOffset, bool closePolygon ) { if ( withAdjacency ) indexes << vertices.count(); // add the following vertex (for adjacency) @@ -132,6 +132,8 @@ void QgsLineVertexData::addLineString( const QgsLineString &lineString, float ex break; } + const int firstIndex = vertices.count(); + for ( int i = 0; i < lineString.vertexCount(); ++i ) { QgsPoint p = lineString.pointN( i ); @@ -141,6 +143,9 @@ void QgsLineVertexData::addLineString( const QgsLineString &lineString, float ex indexes << vertices.count() - 1; } + if ( closePolygon ) + indexes << firstIndex; // repeat the first vertex + if ( withAdjacency ) indexes << vertices.count() - 1; // add the last vertex (for adjacency) diff --git a/src/3d/symbols/qgslinevertexdata_p.h b/src/3d/symbols/qgslinevertexdata_p.h index 186f51c0db18..d1b338c073c5 100644 --- a/src/3d/symbols/qgslinevertexdata_p.h +++ b/src/3d/symbols/qgslinevertexdata_p.h @@ -95,7 +95,7 @@ struct QgsLineVertexData Qt3DCore::QGeometry *createGeometry( Qt3DCore::QNode *parent ); #endif - void addLineString( const QgsLineString &lineString, float extraHeightOffset = 0 ); + void addLineString( const QgsLineString &lineString, float extraHeightOffset = 0, bool closePolygon = false ); void addVerticalLines( const QgsLineString &lineString, float verticalLength, float extraHeightOffset = 0 ); };