/
qgspolygon3dsymbol_p.cpp
246 lines (198 loc) · 9.26 KB
/
qgspolygon3dsymbol_p.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/***************************************************************************
qgspolygon3dsymbol_p.cpp
--------------------------------------
Date : July 2017
Copyright : (C) 2017 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgspolygon3dsymbol_p.h"
#include "qgspolygon3dsymbol.h"
#include "qgstessellatedpolygongeometry.h"
#include "qgs3dmapsettings.h"
#include "qgs3dutils.h"
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QEffect>
#include <Qt3DRender/QTechnique>
#include <Qt3DRender/QCullFace>
#include <Qt3DRender/QGeometryRenderer>
#include "qgsvectorlayer.h"
#include "qgsmultipolygon.h"
/// @cond PRIVATE
class QgsPolygon3DSymbolHandler : public QgsFeature3DHandler
{
public:
QgsPolygon3DSymbolHandler( const QgsPolygon3DSymbol &symbol, const QgsFeatureIds &selectedIds )
: mSymbol( symbol ), mSelectedIds( selectedIds ) {}
bool prepare( const Qgs3DRenderContext &context, QSet<QString> &attributeNames ) override;
void processFeature( QgsFeature &feature, const Qgs3DRenderContext &context ) override;
void finalize( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context ) override;
private:
//! temporary data we will pass to the tessellator
struct PolygonData
{
QList<QgsPolygon *> polygons;
QList<QgsFeatureId> fids;
QList<float> extrusionHeightPerPolygon; // will stay empty if not needed per polygon
};
void processPolygon( QgsPolygon *polyClone, QgsFeatureId fid, float height, bool hasDDExtrusion, float extrusionHeight, const Qgs3DRenderContext &context, PolygonData &out );
void makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PolygonData &out, bool selected );
Qt3DExtras::QPhongMaterial *material( const QgsPolygon3DSymbol &symbol ) const;
// input specific for this class
const QgsPolygon3DSymbol &mSymbol;
// inputs - generic
QgsFeatureIds mSelectedIds;
// outputs
PolygonData outNormal; //!< Features that are not selected
PolygonData outSelected; //!< Features that are selected
};
bool QgsPolygon3DSymbolHandler::prepare( const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
{
QSet<QString> attrs = mSymbol.dataDefinedProperties().referencedFields( context.expressionContext() );
attributeNames.unite( attrs );
return true;
}
void QgsPolygon3DSymbolHandler::processPolygon( QgsPolygon *polyClone, QgsFeatureId fid, float height, bool hasDDExtrusion, float extrusionHeight, const Qgs3DRenderContext &context, PolygonData &out )
{
Qgs3DUtils::clampAltitudes( polyClone, mSymbol.altitudeClamping(), mSymbol.altitudeBinding(), height, context.map() );
out.polygons.append( polyClone );
out.fids.append( fid );
if ( hasDDExtrusion )
out.extrusionHeightPerPolygon.append( extrusionHeight );
}
void QgsPolygon3DSymbolHandler::processFeature( QgsFeature &f, const Qgs3DRenderContext &context )
{
if ( f.geometry().isNull() )
return;
PolygonData &out = mSelectedIds.contains( f.id() ) ? outSelected : outNormal;
QgsGeometry geom = f.geometry();
// segmentize curved geometries if necessary
if ( QgsWkbTypes::isCurvedType( geom.constGet()->wkbType() ) )
geom = QgsGeometry( geom.constGet()->segmentize() );
const QgsAbstractGeometry *g = geom.constGet();
const QgsPropertyCollection &ddp = mSymbol.dataDefinedProperties();
bool hasDDHeight = ddp.isActive( QgsAbstract3DSymbol::PropertyHeight );
bool hasDDExtrusion = ddp.isActive( QgsAbstract3DSymbol::PropertyExtrusionHeight );
float height = mSymbol.height();
float extrusionHeight = mSymbol.extrusionHeight();
if ( hasDDHeight )
height = ddp.valueAsDouble( QgsAbstract3DSymbol::PropertyHeight, context.expressionContext(), height );
if ( hasDDExtrusion )
extrusionHeight = ddp.valueAsDouble( QgsAbstract3DSymbol::PropertyExtrusionHeight, context.expressionContext(), extrusionHeight );
if ( const QgsPolygon *poly = qgsgeometry_cast< const QgsPolygon *>( g ) )
{
QgsPolygon *polyClone = poly->clone();
processPolygon( polyClone, f.id(), height, hasDDExtrusion, extrusionHeight, context, out );
}
else if ( const QgsMultiPolygon *mpoly = qgsgeometry_cast< const QgsMultiPolygon *>( g ) )
{
for ( int i = 0; i < mpoly->numGeometries(); ++i )
{
const QgsAbstractGeometry *g2 = mpoly->geometryN( i );
Q_ASSERT( QgsWkbTypes::flatType( g2->wkbType() ) == QgsWkbTypes::Polygon );
QgsPolygon *polyClone = static_cast< const QgsPolygon *>( g2 )->clone();
processPolygon( polyClone, f.id(), height, hasDDExtrusion, extrusionHeight, context, out );
}
}
else if ( const QgsGeometryCollection *gc = qgsgeometry_cast< const QgsGeometryCollection *>( g ) )
{
for ( int i = 0; i < gc->numGeometries(); ++i )
{
const QgsAbstractGeometry *g2 = gc->geometryN( i );
if ( QgsWkbTypes::flatType( g2->wkbType() ) == QgsWkbTypes::Polygon )
{
QgsPolygon *polyClone = static_cast< const QgsPolygon *>( g2 )->clone();
processPolygon( polyClone, f.id(), height, hasDDExtrusion, extrusionHeight, context, out );
}
}
}
else
qDebug() << "not a polygon";
}
void QgsPolygon3DSymbolHandler::finalize( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context )
{
// create entity for selected and not selected
makeEntity( parent, context, outNormal, false );
makeEntity( parent, context, outSelected, true );
}
void QgsPolygon3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent, const Qgs3DRenderContext &context, PolygonData &out, bool selected )
{
if ( out.polygons.isEmpty() )
return; // nothing to show - no need to create the entity
Qt3DExtras::QPhongMaterial *mat = material( mSymbol );
if ( selected )
{
// update the material with selection colors
mat->setDiffuse( context.map().selectionColor() );
mat->setAmbient( context.map().selectionColor().darker() );
}
QgsPointXY origin( context.map().origin().x(), context.map().origin().y() );
QgsTessellatedPolygonGeometry *geometry = new QgsTessellatedPolygonGeometry;
geometry->setInvertNormals( mSymbol.invertNormals() );
geometry->setAddBackFaces( mSymbol.addBackFaces() );
geometry->setPolygons( out.polygons, out.fids, origin, mSymbol.extrusionHeight(), out.extrusionHeightPerPolygon );
Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
renderer->setGeometry( geometry );
// make entity
Qt3DCore::QEntity *entity = new Qt3DCore::QEntity;
entity->addComponent( renderer );
entity->addComponent( mat );
entity->setParent( parent );
if ( !selected )
entity->findChild<Qt3DRender::QGeometryRenderer *>()->setObjectName( QStringLiteral( "main" ) ); // temporary measure to distinguish between "selected" and "main"
}
static Qt3DRender::QCullFace::CullingMode _qt3DcullingMode( Qgs3DTypes::CullingMode mode )
{
switch ( mode )
{
case Qgs3DTypes::NoCulling: return Qt3DRender::QCullFace::NoCulling;
case Qgs3DTypes::Front: return Qt3DRender::QCullFace::Front;
case Qgs3DTypes::Back: return Qt3DRender::QCullFace::Back;
case Qgs3DTypes::FrontAndBack: return Qt3DRender::QCullFace::FrontAndBack;
}
return Qt3DRender::QCullFace::NoCulling;
}
Qt3DExtras::QPhongMaterial *QgsPolygon3DSymbolHandler::material( const QgsPolygon3DSymbol &symbol ) const
{
Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
// front/back side culling
auto techniques = material->effect()->techniques();
for ( auto tit = techniques.constBegin(); tit != techniques.constEnd(); ++tit )
{
auto renderPasses = ( *tit )->renderPasses();
for ( auto rpit = renderPasses.begin(); rpit != renderPasses.end(); ++rpit )
{
Qt3DRender::QCullFace *cullFace = new Qt3DRender::QCullFace;
cullFace->setMode( _qt3DcullingMode( symbol.cullingMode() ) );
( *rpit )->addRenderState( cullFace );
}
}
material->setAmbient( symbol.material().ambient() );
material->setDiffuse( symbol.material().diffuse() );
material->setSpecular( symbol.material().specular() );
material->setShininess( symbol.material().shininess() );
return material;
}
// --------------
namespace Qgs3DSymbolImpl
{
QgsFeature3DHandler *handlerForPolygon3DSymbol( QgsVectorLayer *layer, const QgsPolygon3DSymbol &symbol )
{
return new QgsPolygon3DSymbolHandler( symbol, layer->selectedFeatureIds() );
}
Qt3DCore::QEntity *entityForPolygon3DSymbol( const Qgs3DMapSettings &map, QgsVectorLayer *layer, const QgsPolygon3DSymbol &symbol )
{
QgsFeature3DHandler *handler = handlerForPolygon3DSymbol( layer, symbol );
Qt3DCore::QEntity *e = entityFromHandler( handler, map, layer );
delete handler;
return e;
}
}
/// @endcond