Skip to content

Commit

Permalink
Feature selection is also displayed in 3D map view for polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere authored and wonder-sk committed Sep 15, 2017
1 parent c6ff2f6 commit e83ce54
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 14 deletions.
82 changes: 69 additions & 13 deletions src/3d/polygonentity.cpp
Expand Up @@ -6,8 +6,6 @@
#include "terraingenerator.h"
#include "utils.h"

#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QGeometryRenderer>
#include <Qt3DCore/QTransform>

#include "qgsvectorlayer.h"
Expand All @@ -18,19 +16,80 @@
PolygonEntity::PolygonEntity( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, Qt3DCore::QNode *parent )
: Qt3DCore::QEntity( parent )
{
QgsPointXY origin( map.originX, map.originY );
addEntityForSelectedPolygons( map, layer, symbol );
addEntityForNotSelectedPolygons( map, layer, symbol );
}

void PolygonEntity::addEntityForSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol )
{
// build the default material
Qt3DExtras::QPhongMaterial *mat = material( symbol );

// update the material with selection colors
mat->setDiffuse( map.selectionColor() );
mat->setAmbient( map.selectionColor().darker() );

// build a transform function
Qt3DCore::QTransform *tform = new Qt3DCore::QTransform;
tform->setTranslation( QVector3D( 0, 0, 0 ) );

// build the feature request to select features
QgsFeatureRequest req;
req.setDestinationCrs( map.crs );
req.setFilterFids( layer->selectedFeatureIds() );

// build the entity
PolygonEntityNode *entity = new PolygonEntityNode( map, layer, symbol, req );
entity->addComponent( mat );
entity->addComponent( tform );
entity->setParent( this );
}

void PolygonEntity::addEntityForNotSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol )
{
// build the default material
Qt3DExtras::QPhongMaterial *mat = material( symbol );

// build a transform function
Qt3DCore::QTransform *tform = new Qt3DCore::QTransform;
tform->setTranslation( QVector3D( 0, 0, 0 ) );

// build the feature request to select features
QgsFeatureRequest req;
req.setDestinationCrs( map.crs );

QgsFeatureIds notSelected = layer->allFeatureIds();
notSelected.subtract( layer->selectedFeatureIds() );
req.setFilterFids( notSelected );

// build the entity
PolygonEntityNode *entity = new PolygonEntityNode( map, layer, symbol, req );
entity->addComponent( mat );
entity->addComponent( tform );
entity->setParent( this );
}

Qt3DExtras::QPhongMaterial *PolygonEntity::material( const Polygon3DSymbol &symbol ) const
{
Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
material->setAmbient( symbol.material.ambient() );
material->setDiffuse( symbol.material.diffuse() );
material->setSpecular( symbol.material.specular() );
material->setShininess( symbol.material.shininess() );
addComponent( material );
return material;
}

PolygonEntityNode::PolygonEntityNode( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent )
: Qt3DCore::QEntity( parent )
{
addComponent( renderer( map, symbol, layer, req ) );
}

Qt3DRender::QGeometryRenderer *PolygonEntityNode::renderer( const Map3D &map, const Polygon3DSymbol &symbol, const QgsVectorLayer *layer, const QgsFeatureRequest &request )
{
QgsPointXY origin( map.originX, map.originY );
QList<QgsPolygonV2 *> polygons;
QgsFeature f;
QgsFeatureRequest request;
request.setDestinationCrs( map.crs );
QgsFeatureIterator fi = layer->getFeatures( request );
while ( fi.nextFeature( f ) )
{
Expand Down Expand Up @@ -62,14 +121,11 @@ PolygonEntity::PolygonEntity( const Map3D &map, QgsVectorLayer *layer, const Pol
qDebug() << "not a polygon";
}

geometry = new PolygonGeometry;
geometry->setPolygons( polygons, origin, symbol.extrusionHeight );
mGeometry = new PolygonGeometry;
mGeometry->setPolygons( polygons, origin, symbol.extrusionHeight );

Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
renderer->setGeometry( geometry );
addComponent( renderer );
renderer->setGeometry( mGeometry );

Qt3DCore::QTransform *tform = new Qt3DCore::QTransform;
tform->setTranslation( QVector3D( 0, 0, 0 ) );
addComponent( tform );
return renderer;
}
20 changes: 19 additions & 1 deletion src/3d/polygonentity.h
Expand Up @@ -2,13 +2,16 @@
#define POLYGONENTITY_H

#include <Qt3DCore/QEntity>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QGeometryRenderer>

class Map3D;
class PolygonGeometry;
class Polygon3DSymbol;

class QgsPointXY;
class QgsVectorLayer;
class QgsFeatureRequest;


//! Entity that handles rendering of polygons
Expand All @@ -17,7 +20,22 @@ class PolygonEntity : public Qt3DCore::QEntity
public:
PolygonEntity( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, Qt3DCore::QNode *parent = nullptr );

PolygonGeometry *geometry;
private:
void addEntityForSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol );
void addEntityForNotSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol );

Qt3DExtras::QPhongMaterial *material( const Polygon3DSymbol &symbol ) const;
};

class PolygonEntityNode : public Qt3DCore::QEntity
{
public:
PolygonEntityNode( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent = nullptr );

private:
Qt3DRender::QGeometryRenderer *renderer( const Map3D &map, const Polygon3DSymbol &symbol, const QgsVectorLayer *layer, const QgsFeatureRequest &request );

PolygonGeometry *mGeometry;
};

#endif // POLYGONENTITY_H

0 comments on commit e83ce54

Please sign in to comment.