39#include <Qt3DCore/QAttribute>
40#include <Qt3DCore/QBuffer>
41#include <Qt3DCore/QTransform>
42#include <Qt3DExtras/QPhongMaterial>
43#include <Qt3DRender/QGeometryRenderer>
45using namespace Qt::StringLiterals;
52class QgsBufferedLine3DSymbolHandler :
public QgsFeature3DHandler
55 QgsBufferedLine3DSymbolHandler(
const QgsLine3DSymbol *symbol,
const QgsFeatureIds &selectedIds )
56 : mSymbol( static_cast<QgsLine3DSymbol *>( symbol->clone() ) )
57 , mSelectedIds( selectedIds )
60 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames,
const QgsBox3D &chunkExtent )
override;
61 void processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
62 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
68 std::unique_ptr<QgsTessellator> tessellator;
69 QVector<QgsFeatureId> triangleIndexFids;
70 QVector<uint> triangleIndexStartingIndices;
73 void processPolygon( QgsPolygon *polyBuffered,
QgsFeatureId fid,
float height,
float extrusionHeight,
const Qgs3DRenderContext &context, LineData &lineData );
75 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, LineData &lineData,
bool selected );
78 std::unique_ptr<QgsLine3DSymbol> mSymbol;
82 LineData mLineDataNormal;
83 LineData mLineDataSelected;
87bool QgsBufferedLine3DSymbolHandler::prepare(
const Qgs3DRenderContext &, QSet<QString> &attributeNames,
const QgsBox3D &chunkExtent )
89 Q_UNUSED( attributeNames )
91 mChunkOrigin = chunkExtent.
center();
92 mChunkOrigin.
setZ( 0. );
93 mChunkExtent = chunkExtent;
95 const bool requiresTextureCoordinates = mSymbol->materialSettings() ? mSymbol->materialSettings()->requiresTextureCoordinates() :
false;
97 auto lineDataNormalTessellator = std::make_unique<QgsTessellator>();
98 lineDataNormalTessellator->setOrigin( mChunkOrigin );
99 lineDataNormalTessellator->setAddNormals(
true );
100 lineDataNormalTessellator->setAddTextureUVs( requiresTextureCoordinates );
104 mLineDataNormal.tessellator = std::move( lineDataNormalTessellator );
106 auto lineDataSelectedTessellator = std::make_unique<QgsTessellator>();
107 lineDataSelectedTessellator->setOrigin( mChunkOrigin );
108 lineDataSelectedTessellator->setAddNormals(
true );
109 lineDataSelectedTessellator->setAddTextureUVs( requiresTextureCoordinates );
113 mLineDataSelected.tessellator = std::move( lineDataSelectedTessellator );
123 LineData &lineData = mSelectedIds.contains( feature.
id() ) ? mLineDataSelected : mLineDataNormal;
126 clipGeometryIfTooLarge( geom );
141 const int nSegments = 4;
144 const double mitreLimit = 0;
146 const QgsGeos engine( abstractGeom );
148 double width = mSymbol->width();
156 QgsAbstractGeometry *buffered = engine.buffer( width / 2., nSegments, endCapStyle, joinStyle, mitreLimit );
163 processPolygon( polyBuffered, feature.
id(), mSymbol->offset(), mSymbol->extrusionHeight(), context, lineData );
171 processPolygon( polyBuffered, feature.
id(), mSymbol->offset(), mSymbol->extrusionHeight(), context, lineData );
182 Q_ASSERT( lineData.tessellator->dataVerticesCount() % 3 == 0 );
183 const uint startingTriangleIndex =
static_cast<uint
>( lineData.tessellator->dataVerticesCount() / 3 );
184 lineData.triangleIndexStartingIndices.append( startingTriangleIndex );
185 lineData.triangleIndexFids.append( fid );
186 lineData.tessellator->addPolygon( *polyBuffered, extrusionHeight );
187 if ( !lineData.tessellator->error().isEmpty() )
195void QgsBufferedLine3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
198 makeEntity( parent, context, mLineDataNormal,
false );
199 makeEntity( parent, context, mLineDataSelected,
true );
201 mZMin = std::min( mLineDataNormal.tessellator->zMinimum(), mLineDataSelected.tessellator->zMinimum() );
202 mZMax = std::max( mLineDataNormal.tessellator->zMaximum(), mLineDataSelected.tessellator->zMaximum() );
206void QgsBufferedLine3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, LineData &lineData,
bool selected )
208 if ( lineData.tessellator->dataVerticesCount() == 0 )
219 const QByteArray vertexBuffer = lineData.tessellator->vertexBuffer();
220 const QByteArray indexBuffer = lineData.tessellator->indexBuffer();
221 const int vertexCount = vertexBuffer.count() / lineData.tessellator->stride();
222 const size_t indexCount = lineData.tessellator->dataVerticesCount();
225 geometry->
setVertexBufferData( vertexBuffer, vertexCount, lineData.triangleIndexFids, lineData.triangleIndexStartingIndices );
228 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
229 renderer->setGeometry( geometry );
232 QgsGeoTransform *transform =
new QgsGeoTransform;
233 transform->setGeoTranslation( mChunkOrigin );
236 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
237 entity->addComponent( renderer );
238 entity->addComponent( material );
239 entity->addComponent( transform );
240 entity->setParent( parent );
253class QgsThickLine3DSymbolHandler :
public QgsFeature3DHandler
256 QgsThickLine3DSymbolHandler(
const QgsLine3DSymbol *symbol,
const QgsFeatureIds &selectedIds )
257 : mSymbol( static_cast<QgsLine3DSymbol *>( symbol->clone() ) )
258 , mSelectedIds( selectedIds )
261 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames,
const QgsBox3D &chunkExtent )
override;
262 void processFeature(
const QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
263 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
266 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, QgsLineVertexData &lineVertexData,
bool selected );
267 Qt3DExtras::QPhongMaterial *material(
const QgsLine3DSymbol &symbol )
const;
268 void processMaterialDatadefined( uint verticesCount,
const QgsExpressionContext &context, QgsLineVertexData &lineVertexData );
271 std::unique_ptr<QgsLine3DSymbol> mSymbol;
275 QgsLineVertexData mLineDataNormal;
276 QgsLineVertexData mLineDataSelected;
280bool QgsThickLine3DSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames,
const QgsBox3D &chunkExtent )
282 Q_UNUSED( attributeNames )
284 mChunkOrigin = chunkExtent.
center();
285 mChunkExtent = chunkExtent;
287 mLineDataNormal.withAdjacency =
true;
288 mLineDataSelected.withAdjacency =
true;
289 mLineDataNormal.init( mSymbol->altitudeClamping(), mSymbol->altitudeBinding(), mSymbol->offset(), context, mChunkOrigin );
290 mLineDataSelected.init( mSymbol->altitudeClamping(), mSymbol->altitudeBinding(), mSymbol->offset(), context, mChunkOrigin );
292 QSet<QString> attrs = mSymbol->dataDefinedProperties().referencedFields( context.
expressionContext() );
293 attributeNames.unite( attrs );
294 attrs = mSymbol->materialSettings()->dataDefinedProperties().referencedFields( context.
expressionContext() );
295 attributeNames.unite( attrs );
299 processMaterialDatadefined( mLineDataNormal.vertices.size(), context.
expressionContext(), mLineDataNormal );
300 processMaterialDatadefined( mLineDataSelected.vertices.size(), context.
expressionContext(), mLineDataSelected );
312 QgsLineVertexData &lineVertexData = mSelectedIds.
contains( feature.
id() ) ? mLineDataSelected : mLineDataNormal;
314 const int oldVerticesCount = lineVertexData.vertices.size();
317 ( void ) clipGeometryIfTooLarge( geom );
333 lineVertexData.addLineString( *lineString );
337 for (
int nGeom = 0; nGeom < multiLineString->numGeometries(); ++nGeom )
339 const QgsLineString *lineString = multiLineString->lineStringN( nGeom );
340 lineVertexData.addLineString( *lineString );
345 processMaterialDatadefined( lineVertexData.vertices.size() - oldVerticesCount, context.
expressionContext(), lineVertexData );
350void QgsThickLine3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
353 makeEntity( parent, context, mLineDataNormal,
false );
354 makeEntity( parent, context, mLineDataSelected,
true );
356 updateZRangeFromPositions( mLineDataNormal.vertices );
357 updateZRangeFromPositions( mLineDataSelected.vertices );
361void QgsThickLine3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, QgsLineVertexData &lineVertexData,
bool selected )
363 if ( lineVertexData.indexes.isEmpty() )
378 if ( QgsLineMaterial *lineMaterial =
dynamic_cast<QgsLineMaterial *
>( material ) )
380 float width = mSymbol->width();
381 if ( mHighlightingEnabled )
385 lineMaterial->setLineColor( color );
392 lineMaterial->setLineWidth( width );
395 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
398 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
399 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
400 Qt3DCore::QGeometry *geometry = lineVertexData.createGeometry( entity );
407 renderer->setGeometry( geometry );
409 renderer->setVertexCount( lineVertexData.indexes.count() );
410 renderer->setPrimitiveRestartEnabled(
true );
411 renderer->setRestartIndexValue( 0 );
414 QgsGeoTransform *transform =
new QgsGeoTransform;
415 transform->setGeoTranslation( mChunkOrigin );
418 entity->addComponent( renderer );
419 entity->addComponent( material );
420 entity->addComponent( transform );
421 entity->setParent( parent );
424void QgsThickLine3DSymbolHandler::processMaterialDatadefined( uint verticesCount,
const QgsExpressionContext &context, QgsLineVertexData &lineVertexData )
427 lineVertexData.materialDataDefined.append( bytes.repeated(
static_cast<int>( verticesCount ) ) );
434namespace Qgs3DSymbolImpl
437 QgsFeature3DHandler *handlerForLine3DSymbol( QgsVectorLayer *layer,
const QgsAbstract3DSymbol *symbol )
439 const QgsLine3DSymbol *lineSymbol =
dynamic_cast<const QgsLine3DSymbol *
>( symbol );
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
JoinStyle
Join styles for buffers.
@ Round
Use rounded joins.
EndCapStyle
End cap styles for buffers.
@ Triangles
Triangle based rendering (default).
@ Lines
Line based rendering, requires line data.
@ MultiPolygon
MultiPolygon.
Rendering context for preparation of 3D entities.
QColor selectionColor() const
Returns color used for selected features.
QgsExpressionContext & expressionContext()
Gets the expression context.
@ Main3DRenderer
Renderer for normal entities.
static const char * PROP_NAME_3D_RENDERER_FLAG
Qt property name to hold the 3D geometry renderer flag.
static void clampAltitudes(QgsLineString *lineString, Qgis::AltitudeClamping altClamp, Qgis::AltitudeBinding altBind, const QgsPoint ¢roid, float offset, const Qgs3DRenderContext &context)
Clamps altitude of vertices of a linestring according to the settings.
static QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context)
Creates a new QgsMaterial object representing the material settings.
static QByteArray materialDataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext)
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
static void applyMaterialDataDefinedToGeometry(const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &dataDefinedBytes)
Applies the data defined bytes, dataDefinedBytes, on the geometry by filling a specific vertex buffer...
Abstract base class for all geometries.
virtual const QgsAbstractGeometry * simplifiedTypeRef() const
Returns a reference to the simplest lossless representation of this geometry, e.g.
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
@ Ambient
Ambient color (phong material).
A 3-dimensional box composed of x, y, z coordinates.
bool contains(const QgsBox3D &other) const
Returns true when box contains other box.
QgsVector3D center() const
Returns the center of the box as a vector.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
int numGeometries() const
Returns the number of geometries within the collection.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Does vector analysis using the GEOS library and handles import, export, and exception handling.
static int silhouetteWidth()
Returns the width of the generated silhouette effect in pixels.
bool renderAsSimpleLines() const
Returns whether the renderer will render data with simple lines (otherwise it uses buffer).
Line string geometry type, with support for z-dimension and m-values.
Context settings for a material.
void setIsSelected(bool isSelected)
Sets whether the material should represent a selected state.
void setSelectionColor(const QColor &color)
Sets the color for representing materials in a selected state.
void setIsHighlighted(bool isHighlighted)
Sets whether the material should represent a highlighted state.
Base class for all materials used within QGIS 3D views.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Multi line string geometry collection.
Multi polygon geometry collection.
QgsPolygon * polygonN(int index)
Returns the polygon with the specified index.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Basic shading material used for rendering simple lines as solid line components.
Qt3DRender::QGeometry subclass that represents polygons tessellated into 3D geometry.
void setVertexBufferData(const QByteArray &vertexBufferData, int vertexCount, const QVector< QgsFeatureId > &triangleIndexFids, const QVector< uint > &triangleIndexStartingIndices)
Initializes vertex buffer (and other members) from data that were already tessellated.
void setIndexBufferData(const QByteArray &indexBufferData, size_t indexCount)
Sets index buffer data.
void setZ(double z)
Sets Z coordinate.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
static Q_INVOKABLE bool isCurvedType(Qgis::WkbType type)
Returns true if the WKB type is a curved type or can contain curved geometries.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features