Skip to content

Commit

Permalink
Allow closed polygon shapes in 3d rubber bands
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros committed Jan 10, 2025
1 parent a2c3471 commit 0170671
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
25 changes: 17 additions & 8 deletions src/3d/qgsrubberband3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -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 ) );
Expand All @@ -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" ) }
};

Expand All @@ -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;
Expand All @@ -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() );
Expand Down
2 changes: 2 additions & 0 deletions src/3d/qgsrubberband3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
7 changes: 6 additions & 1 deletion src/3d/symbols/qgslinevertexdata_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 );
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/3d/symbols/qgslinevertexdata_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
};

Expand Down

0 comments on commit 0170671

Please sign in to comment.