Skip to content

Commit ae3b3ec

Browse files
committedSep 30, 2017
Introduced Shape enum for 3D point symbols (from Nyall's review)
1 parent 982cfef commit ae3b3ec

File tree

5 files changed

+164
-84
lines changed

5 files changed

+164
-84
lines changed
 

‎src/3d/symbols/qgspoint3dsymbol.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ void QgsPoint3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &c
2121
mMaterial.writeXml( elemMaterial );
2222
elem.appendChild( elemMaterial );
2323

24+
elem.setAttribute( QStringLiteral( "shape" ), shapeToString( mShape ) );
25+
2426
QVariantMap shapePropertiesCopy( mShapeProperties );
2527
shapePropertiesCopy["model"] = QVariant( context.pathResolver().writePath( shapePropertiesCopy["model"].toString() ) );
2628

@@ -38,10 +40,48 @@ void QgsPoint3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteConte
3840
QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
3941
mMaterial.readXml( elemMaterial );
4042

43+
mShape = shapeFromString( elem.attribute( QStringLiteral( "shape" ) ) );
44+
4145
QDomElement elemShapeProperties = elem.firstChildElement( QStringLiteral( "shape-properties" ) );
4246
mShapeProperties = QgsXmlUtils::readVariant( elemShapeProperties.firstChildElement() ).toMap();
4347
mShapeProperties["model"] = QVariant( context.pathResolver().readPath( mShapeProperties["model"].toString() ) );
4448

4549
QDomElement elemTransform = elem.firstChildElement( QStringLiteral( "transform" ) );
4650
mTransform = Qgs3DUtils::stringToMatrix4x4( elemTransform.attribute( QStringLiteral( "matrix" ) ) );
4751
}
52+
53+
QgsPoint3DSymbol::Shape QgsPoint3DSymbol::shapeFromString( const QString &shape )
54+
{
55+
if ( shape == QStringLiteral( "sphere" ) )
56+
return Sphere;
57+
else if ( shape == QStringLiteral( "cone" ) )
58+
return Cone;
59+
else if ( shape == QStringLiteral( "cube" ) )
60+
return Cube;
61+
else if ( shape == QStringLiteral( "torus" ) )
62+
return Torus;
63+
else if ( shape == QStringLiteral( "plane" ) )
64+
return Plane;
65+
else if ( shape == QStringLiteral( "extruded-text" ) )
66+
return ExtrudedText;
67+
else if ( shape == QStringLiteral( "model" ) )
68+
return Model;
69+
else // "cylinder" (default)
70+
return Cylinder;
71+
}
72+
73+
QString QgsPoint3DSymbol::shapeToString( QgsPoint3DSymbol::Shape shape )
74+
{
75+
switch ( shape )
76+
{
77+
case Cylinder: return QStringLiteral( "cylinder" );
78+
case Sphere: return QStringLiteral( "sphere" );
79+
case Cone: return QStringLiteral( "cone" );
80+
case Cube: return QStringLiteral( "cube" );
81+
case Torus: return QStringLiteral( "torus" );
82+
case Plane: return QStringLiteral( "plane" );
83+
case ExtrudedText: return QStringLiteral( "extruded-text" );
84+
case Model: return QStringLiteral( "model" );
85+
default: Q_ASSERT( false ); return QString();
86+
}
87+
}

‎src/3d/symbols/qgspoint3dsymbol.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ class _3D_EXPORT QgsPoint3DSymbol : public QgsAbstract3DSymbol
2929
//! Sets material used for shading of the symbol
3030
void setMaterial( const QgsPhongMaterialSettings &material ) { mMaterial = material; }
3131

32+
//! 3D shape types supported by the symbol
33+
enum Shape
34+
{
35+
Cylinder,
36+
Sphere,
37+
Cone,
38+
Cube,
39+
Torus,
40+
Plane,
41+
ExtrudedText, //!< Supported in Qt 5.9+
42+
Model,
43+
};
44+
45+
//! Returns shape enum value from a string
46+
static Shape shapeFromString( const QString &shape );
47+
//! Returns string from a shape enum value
48+
static QString shapeToString( Shape shape );
49+
50+
//! Returns 3D shape for points
51+
Shape shape() const { return mShape; }
52+
//! Sets 3D shape for points
53+
void setShape( Shape shape ) { mShape = shape; }
54+
3255
//! Returns a key-value dictionary of point shape properties
3356
QVariantMap shapeProperties() const { return mShapeProperties; }
3457
//! Sets a key-value dictionary of point shape properties
@@ -41,7 +64,8 @@ class _3D_EXPORT QgsPoint3DSymbol : public QgsAbstract3DSymbol
4164

4265
private:
4366
QgsPhongMaterialSettings mMaterial; //!< Defines appearance of objects
44-
QVariantMap mShapeProperties; //!< What kind of shape to use and what
67+
Shape mShape = Cylinder; //!< What kind of shape to use
68+
QVariantMap mShapeProperties; //!< Key-value dictionary of shape's properties (different keys for each shape)
4569
QMatrix4x4 mTransform; //!< Transform of individual instanced models
4670
};
4771

‎src/3d/symbols/qgspoint3dsymbol_p.cpp

Lines changed: 87 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
QgsPoint3DSymbolEntity::QgsPoint3DSymbolEntity( const Qgs3DMapSettings &map, QgsVectorLayer *layer, const QgsPoint3DSymbol &symbol, Qt3DCore::QNode *parent )
3838
: Qt3DCore::QEntity( parent )
3939
{
40-
if ( symbol.shapeProperties()["shape"].toString() == "model" )
40+
if ( symbol.shape() == QgsPoint3DSymbol::Model )
4141
{
4242
QgsPoint3DSymbolModelEntityFactory::addEntitiesForSelectedPoints( map, layer, symbol, this );
4343
QgsPoint3DSymbolModelEntityFactory::addEntitiesForNotSelectedPoints( map, layer, symbol, this );
@@ -199,78 +199,7 @@ Qt3DRender::QGeometryRenderer *QgsPoint3DSymbolInstancedEntityNode::renderer( co
199199
instanceDataAttribute->setCount( count );
200200
instanceDataAttribute->setByteStride( 3 * sizeof( float ) );
201201

202-
Qt3DRender::QGeometry *geometry = nullptr;
203-
QVariantMap shapeProperties = symbol.shapeProperties();
204-
QString shape = shapeProperties["shape"].toString();
205-
if ( shape == "sphere" )
206-
{
207-
float radius = shapeProperties["radius"].toFloat();
208-
Qt3DExtras::QSphereGeometry *g = new Qt3DExtras::QSphereGeometry;
209-
g->setRadius( radius ? radius : 10 );
210-
geometry = g;
211-
}
212-
else if ( shape == "cone" )
213-
{
214-
float length = shapeProperties["length"].toFloat();
215-
float bottomRadius = shapeProperties["bottomRadius"].toFloat();
216-
float topRadius = shapeProperties["topRadius"].toFloat();
217-
Qt3DExtras::QConeGeometry *g = new Qt3DExtras::QConeGeometry;
218-
g->setLength( length ? length : 10 );
219-
g->setBottomRadius( bottomRadius );
220-
g->setTopRadius( topRadius );
221-
//g->setHasBottomEndcap(hasBottomEndcap);
222-
//g->setHasTopEndcap(hasTopEndcap);
223-
geometry = g;
224-
}
225-
else if ( shape == "cube" )
226-
{
227-
float size = shapeProperties["size"].toFloat();
228-
Qt3DExtras::QCuboidGeometry *g = new Qt3DExtras::QCuboidGeometry;
229-
g->setXExtent( size ? size : 10 );
230-
g->setYExtent( size ? size : 10 );
231-
g->setZExtent( size ? size : 10 );
232-
geometry = g;
233-
}
234-
else if ( shape == "torus" )
235-
{
236-
float radius = shapeProperties["radius"].toFloat();
237-
float minorRadius = shapeProperties["minorRadius"].toFloat();
238-
Qt3DExtras::QTorusGeometry *g = new Qt3DExtras::QTorusGeometry;
239-
g->setRadius( radius ? radius : 10 );
240-
g->setMinorRadius( minorRadius ? minorRadius : 5 );
241-
geometry = g;
242-
}
243-
else if ( shape == "plane" )
244-
{
245-
float size = shapeProperties["size"].toFloat();
246-
Qt3DExtras::QPlaneGeometry *g = new Qt3DExtras::QPlaneGeometry;
247-
g->setWidth( size ? size : 10 );
248-
g->setHeight( size ? size : 10 );
249-
geometry = g;
250-
}
251-
#if QT_VERSION >= 0x050900
252-
else if ( shape == "extrudedText" )
253-
{
254-
float depth = shapeProperties["depth"].toFloat();
255-
QString text = shapeProperties["text"].toString();
256-
Qt3DExtras::QExtrudedTextGeometry *g = new Qt3DExtras::QExtrudedTextGeometry;
257-
g->setDepth( depth ? depth : 1 );
258-
g->setText( text );
259-
geometry = g;
260-
}
261-
#endif
262-
else // shape == "cylinder" or anything else
263-
{
264-
float radius = shapeProperties["radius"].toFloat();
265-
float length = shapeProperties["length"].toFloat();
266-
Qt3DExtras::QCylinderGeometry *g = new Qt3DExtras::QCylinderGeometry;
267-
//g->setRings(2); // how many vertices vertically
268-
//g->setSlices(8); // how many vertices on circumference
269-
g->setRadius( radius ? radius : 10 );
270-
g->setLength( length ? length : 10 );
271-
geometry = g;
272-
}
273-
202+
Qt3DRender::QGeometry *geometry = symbolGeometry( symbol.shape(), symbol.shapeProperties() );
274203
geometry->addAttribute( instanceDataAttribute );
275204
geometry->setBoundingVolumePositionAttribute( instanceDataAttribute );
276205

@@ -281,6 +210,91 @@ Qt3DRender::QGeometryRenderer *QgsPoint3DSymbolInstancedEntityNode::renderer( co
281210
return renderer;
282211
}
283212

213+
Qt3DRender::QGeometry *QgsPoint3DSymbolInstancedEntityNode::symbolGeometry( QgsPoint3DSymbol::Shape shape, const QVariantMap &shapeProperties ) const
214+
{
215+
switch ( shape )
216+
{
217+
case QgsPoint3DSymbol::Cylinder:
218+
{
219+
float radius = shapeProperties["radius"].toFloat();
220+
float length = shapeProperties["length"].toFloat();
221+
Qt3DExtras::QCylinderGeometry *g = new Qt3DExtras::QCylinderGeometry;
222+
//g->setRings(2); // how many vertices vertically
223+
//g->setSlices(8); // how many vertices on circumference
224+
g->setRadius( radius ? radius : 10 );
225+
g->setLength( length ? length : 10 );
226+
return g;
227+
}
228+
229+
case QgsPoint3DSymbol::Sphere:
230+
{
231+
float radius = shapeProperties["radius"].toFloat();
232+
Qt3DExtras::QSphereGeometry *g = new Qt3DExtras::QSphereGeometry;
233+
g->setRadius( radius ? radius : 10 );
234+
return g;
235+
}
236+
237+
case QgsPoint3DSymbol::Cone:
238+
{
239+
float length = shapeProperties["length"].toFloat();
240+
float bottomRadius = shapeProperties["bottomRadius"].toFloat();
241+
float topRadius = shapeProperties["topRadius"].toFloat();
242+
Qt3DExtras::QConeGeometry *g = new Qt3DExtras::QConeGeometry;
243+
g->setLength( length ? length : 10 );
244+
g->setBottomRadius( bottomRadius );
245+
g->setTopRadius( topRadius );
246+
//g->setHasBottomEndcap(hasBottomEndcap);
247+
//g->setHasTopEndcap(hasTopEndcap);
248+
return g;
249+
}
250+
251+
case QgsPoint3DSymbol::Cube:
252+
{
253+
float size = shapeProperties["size"].toFloat();
254+
Qt3DExtras::QCuboidGeometry *g = new Qt3DExtras::QCuboidGeometry;
255+
g->setXExtent( size ? size : 10 );
256+
g->setYExtent( size ? size : 10 );
257+
g->setZExtent( size ? size : 10 );
258+
return g;
259+
}
260+
261+
case QgsPoint3DSymbol::Torus:
262+
{
263+
float radius = shapeProperties["radius"].toFloat();
264+
float minorRadius = shapeProperties["minorRadius"].toFloat();
265+
Qt3DExtras::QTorusGeometry *g = new Qt3DExtras::QTorusGeometry;
266+
g->setRadius( radius ? radius : 10 );
267+
g->setMinorRadius( minorRadius ? minorRadius : 5 );
268+
return g;
269+
}
270+
271+
case QgsPoint3DSymbol::Plane:
272+
{
273+
float size = shapeProperties["size"].toFloat();
274+
Qt3DExtras::QPlaneGeometry *g = new Qt3DExtras::QPlaneGeometry;
275+
g->setWidth( size ? size : 10 );
276+
g->setHeight( size ? size : 10 );
277+
return g;
278+
}
279+
280+
#if QT_VERSION >= 0x050900
281+
case QgsPoint3DSymbol::ExtrudedText:
282+
{
283+
float depth = shapeProperties["depth"].toFloat();
284+
QString text = shapeProperties["text"].toString();
285+
Qt3DExtras::QExtrudedTextGeometry *g = new Qt3DExtras::QExtrudedTextGeometry;
286+
g->setDepth( depth ? depth : 1 );
287+
g->setText( text );
288+
return g;
289+
}
290+
#endif
291+
292+
default:
293+
Q_ASSERT( false );
294+
return nullptr;
295+
}
296+
}
297+
284298
//* 3D MODEL RENDERING *//
285299

286300
static Qt3DExtras::QPhongMaterial *phongMaterial( const QgsPoint3DSymbol &symbol )

‎src/3d/symbols/qgspoint3dsymbol_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <Qt3DRender/QGeometryRenderer>
1818
#include <Qt3DCore/QTransform>
1919

20+
#include "qgspoint3dsymbol.h"
21+
2022
class Qgs3DMapSettings;
2123
class QgsPoint3DSymbol;
2224

@@ -48,6 +50,7 @@ class QgsPoint3DSymbolInstancedEntityNode : public Qt3DCore::QEntity
4850

4951
private:
5052
Qt3DRender::QGeometryRenderer *renderer( const QgsPoint3DSymbol &symbol, const QList<QVector3D> &positions ) const;
53+
Qt3DRender::QGeometry *symbolGeometry( QgsPoint3DSymbol::Shape shape, const QVariantMap &shapeProperties ) const;
5154
};
5255

5356
class QgsPoint3DSymbolModelEntityFactory

‎src/app/3d/qgspoint3dsymbolwidget.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ QgsPoint3DSymbolWidget::QgsPoint3DSymbolWidget( QWidget *parent )
1010
{
1111
setupUi( this );
1212

13-
cboShape->addItem( tr( "Sphere" ), "sphere" );
14-
cboShape->addItem( tr( "Cylinder" ), "cylinder" );
15-
cboShape->addItem( tr( "Cube" ), "cube" );
16-
cboShape->addItem( tr( "Cone" ), "cone" );
17-
cboShape->addItem( tr( "Plane" ), "plane" );
18-
cboShape->addItem( tr( "Torus" ), "torus" );
19-
cboShape->addItem( tr( "3D Model" ), "model" );
13+
cboShape->addItem( tr( "Sphere" ), QgsPoint3DSymbol::Sphere );
14+
cboShape->addItem( tr( "Cylinder" ), QgsPoint3DSymbol::Cylinder );
15+
cboShape->addItem( tr( "Cube" ), QgsPoint3DSymbol::Cube );
16+
cboShape->addItem( tr( "Cone" ), QgsPoint3DSymbol::Cone );
17+
cboShape->addItem( tr( "Plane" ), QgsPoint3DSymbol::Plane );
18+
cboShape->addItem( tr( "Torus" ), QgsPoint3DSymbol::Torus );
19+
cboShape->addItem( tr( "3D Model" ), QgsPoint3DSymbol::Model );
2020

2121
setSymbol( QgsPoint3DSymbol() );
2222
onShapeChanged();
@@ -72,7 +72,7 @@ void QgsPoint3DSymbolWidget::onOverwriteMaterialChecked( int state )
7272
void QgsPoint3DSymbolWidget::setSymbol( const QgsPoint3DSymbol &symbol )
7373
{
7474
QVariantMap vm = symbol.shapeProperties();
75-
int index = cboShape->findData( vm["shape"] );
75+
int index = cboShape->findData( symbol.shape() );
7676
cboShape->setCurrentIndex( index != -1 ? index : 1 ); // use cylinder by default if shape is not set
7777

7878
widgetMaterial->setEnabled( true );
@@ -141,8 +141,6 @@ void QgsPoint3DSymbolWidget::setSymbol( const QgsPoint3DSymbol &symbol )
141141
QgsPoint3DSymbol QgsPoint3DSymbolWidget::symbol() const
142142
{
143143
QVariantMap vm;
144-
vm["shape"] = cboShape->itemData( cboShape->currentIndex() );
145-
146144
switch ( cboShape->currentIndex() )
147145
{
148146
case 0: // sphere
@@ -183,6 +181,7 @@ QgsPoint3DSymbol QgsPoint3DSymbolWidget::symbol() const
183181
tr.rotate( rot );
184182

185183
QgsPoint3DSymbol sym;
184+
sym.setShape( ( QgsPoint3DSymbol::Shape ) cboShape->itemData( cboShape->currentIndex() ).toInt() );
186185
sym.setShapeProperties( vm );
187186
sym.setMaterial( widgetMaterial->material() );
188187
sym.setTransform( tr );

0 commit comments

Comments
 (0)
Please sign in to comment.