QGIS API Documentation  3.0.2-Girona (307d082)
qgsline3dsymbol_p.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsline3dsymbol_p.cpp
3  --------------------------------------
4  Date : July 2017
5  Copyright : (C) 2017 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 "qgsline3dsymbol_p.h"
17 
18 #include "qgsline3dsymbol.h"
20 #include "qgs3dmapsettings.h"
21 //#include "qgsterraingenerator.h"
22 #include "qgs3dutils.h"
23 
24 #include "qgsvectorlayer.h"
25 #include "qgsmultipolygon.h"
26 #include "qgsgeos.h"
27 
29 
30 QgsLine3DSymbolEntity::QgsLine3DSymbolEntity( const Qgs3DMapSettings &map, QgsVectorLayer *layer, const QgsLine3DSymbol &symbol, Qt3DCore::QNode *parent )
31  : Qt3DCore::QEntity( parent )
32 {
33  addEntityForSelectedLines( map, layer, symbol );
34  addEntityForNotSelectedLines( map, layer, symbol );
35 }
36 
37 Qt3DExtras::QPhongMaterial *QgsLine3DSymbolEntity::material( const QgsLine3DSymbol &symbol ) const
38 {
39  Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
40 
41  material->setAmbient( symbol.material().ambient() );
42  material->setDiffuse( symbol.material().diffuse() );
43  material->setSpecular( symbol.material().specular() );
44  material->setShininess( symbol.material().shininess() );
45 
46  return material;
47 }
48 
49 void QgsLine3DSymbolEntity::addEntityForSelectedLines( const Qgs3DMapSettings &map, QgsVectorLayer *layer, const QgsLine3DSymbol &symbol )
50 {
51  // build the default material
52  Qt3DExtras::QPhongMaterial *mat = material( symbol );
53 
54  // update the material with selection colors
55  mat->setDiffuse( map.selectionColor() );
56  mat->setAmbient( map.selectionColor().darker() );
57 
58  // build the feature request to select features
60  req.setDestinationCrs( map.crs(), map.transformContext() );
61  req.setFilterFids( layer->selectedFeatureIds() );
62 
63  // build the entity
64  QgsLine3DSymbolEntityNode *entity = new QgsLine3DSymbolEntityNode( map, layer, symbol, req );
65  entity->addComponent( mat );
66  entity->setParent( this );
67 }
68 
69 void QgsLine3DSymbolEntity::addEntityForNotSelectedLines( const Qgs3DMapSettings &map, QgsVectorLayer *layer, const QgsLine3DSymbol &symbol )
70 {
71  // build the default material
72  Qt3DExtras::QPhongMaterial *mat = material( symbol );
73 
74  // build the feature request to select features
76  req.setDestinationCrs( map.crs(), map.transformContext() );
77 
78  QgsFeatureIds notSelected = layer->allFeatureIds();
79  notSelected.subtract( layer->selectedFeatureIds() );
80  req.setFilterFids( notSelected );
81 
82  // build the entity
83  QgsLine3DSymbolEntityNode *entity = new QgsLine3DSymbolEntityNode( map, layer, symbol, req );
84  entity->addComponent( mat );
85  entity->setParent( this );
86 }
87 
88 QgsLine3DSymbolEntityNode::QgsLine3DSymbolEntityNode( const Qgs3DMapSettings &map, QgsVectorLayer *layer, const QgsLine3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent )
89  : Qt3DCore::QEntity( parent )
90 {
91  addComponent( renderer( map, symbol, layer, req ) );
92 }
93 
94 Qt3DRender::QGeometryRenderer *QgsLine3DSymbolEntityNode::renderer( const Qgs3DMapSettings &map, const QgsLine3DSymbol &symbol, const QgsVectorLayer *layer, const QgsFeatureRequest &request )
95 {
96  QgsPointXY origin( map.origin().x(), map.origin().y() );
97 
98  // TODO: configurable
99  int nSegments = 4;
102  double mitreLimit = 0;
103 
104  QList<QgsPolygon *> polygons;
105  QgsFeature f;
106  QgsFeatureIterator fi = layer->getFeatures( request );
107  while ( fi.nextFeature( f ) )
108  {
109  if ( f.geometry().isNull() )
110  continue;
111 
112  QgsGeometry geom = f.geometry();
113 
114  // segmentize curved geometries if necessary
115  if ( QgsWkbTypes::isCurvedType( geom.constGet()->wkbType() ) )
116  geom = QgsGeometry( geom.constGet()->segmentize() );
117 
118  const QgsAbstractGeometry *g = geom.constGet();
119 
120  QgsGeos engine( g );
121  QgsAbstractGeometry *buffered = engine.buffer( symbol.width() / 2., nSegments, endCapStyle, joinStyle, mitreLimit ); // factory
122 
123  if ( QgsWkbTypes::flatType( buffered->wkbType() ) == QgsWkbTypes::Polygon )
124  {
125  QgsPolygon *polyBuffered = static_cast<QgsPolygon *>( buffered );
126  Qgs3DUtils::clampAltitudes( polyBuffered, symbol.altitudeClamping(), symbol.altitudeBinding(), symbol.height(), map );
127  polygons.append( polyBuffered );
128  }
129  else if ( QgsWkbTypes::flatType( buffered->wkbType() ) == QgsWkbTypes::MultiPolygon )
130  {
131  QgsMultiPolygon *mpolyBuffered = static_cast<QgsMultiPolygon *>( buffered );
132  for ( int i = 0; i < mpolyBuffered->numGeometries(); ++i )
133  {
134  QgsAbstractGeometry *partBuffered = mpolyBuffered->geometryN( i );
135  Q_ASSERT( QgsWkbTypes::flatType( partBuffered->wkbType() ) == QgsWkbTypes::Polygon );
136  QgsPolygon *polyBuffered = static_cast<QgsPolygon *>( partBuffered )->clone(); // need to clone individual geometry parts
137  Qgs3DUtils::clampAltitudes( polyBuffered, symbol.altitudeClamping(), symbol.altitudeBinding(), symbol.height(), map );
138  polygons.append( polyBuffered );
139  }
140  delete buffered;
141  }
142  }
143 
144  mGeometry = new QgsTessellatedPolygonGeometry;
145  mGeometry->setPolygons( polygons, origin, symbol.extrusionHeight() );
146 
147  Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
148  renderer->setGeometry( mGeometry );
149 
150  return renderer;
151 }
152 
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature&#39;s geometries.
AltitudeBinding altitudeBinding() const
Returns method that determines how altitude is bound to individual vertices.
Wrapper for iterator of features from vector data provider or vector layer.
float shininess() const
Returns shininess of the surface.
QColor selectionColor() const
Returns color used for selected features.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
bool isNull() const
Returns true if the geometry is null (ie, contains no underlying geometry accessible via geometry() )...
static void clampAltitudes(QgsLineString *lineString, AltitudeClamping altClamp, AltitudeBinding altBind, const QgsPoint &centroid, float height, const Qgs3DMapSettings &map)
Clamps altitude of vertices of a linestring according to the settings.
Definition: qgs3dutils.cpp:110
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
QColor specular() const
Returns specular color component.
A class to represent a 2D point.
Definition: qgspointxy.h:43
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
EndCapStyle
End cap styles for buffers.
Definition: qgsgeometry.h:905
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:44
3 Definition of the world
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0)
float height() const
Returns height (altitude) of the symbol (in map units)
QgsAbstract3DSymbol * clone() const override
Returns a new instance of the symbol with the same settings.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
float width() const
Returns width of the line symbol (in map units)
const QgsFeatureIds & selectedFeatureIds() const
Return reference to identifiers of selected features.
void setPolygons(const QList< QgsPolygon *> &polygons, const QgsPointXY &origin, float extrusionHeight, const QList< float > &extrusionHeightPerPolygon=QList< float >())
Initializes vertex buffer from given polygons. Takes ownership of passed polygon geometries.
3 Class derived from Qt3DRender::QGeometry that represents polygons tessellated into 3D geometry...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsPhongMaterialSettings material() const
Returns material used for shading of the symbol.
3 3D symbol that draws linestring geometries as planar polygons (created from lines using a buffer wi...
Abstract base class for all geometries.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
Does vector analysis using the geos library and handles import, export, exception handling*...
Definition: qgsgeos.h:98
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
int numGeometries() const
Returns the number of geometries within the collection.
Use rounded joins.
Definition: qgsgeometry.h:916
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Set feature IDs that should be fetched.
Multi polygon geometry collection.
float extrusionHeight() const
Returns extrusion height (in map units)
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
static bool isCurvedType(Type type)
Returns true if the WKB type is a curved type or can contain curved geometries.
Definition: qgswkbtypes.h:606
QColor ambient() const
Returns ambient color component.
AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain)
QColor diffuse() const
Returns diffuse color component.
bool nextFeature(QgsFeature &f)
Polygon geometry type.
Definition: qgspolygon.h:31
JoinStyle
Join styles for buffers.
Definition: qgsgeometry.h:914
Represents a vector layer which manages a vector based data sets.
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:427
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
void setAmbient(const QColor &ambient)
Sets ambient color component.
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:42