Skip to content

Commit c0e732b

Browse files
committed
[processing] Add QGIS 3d provider
Adds a new QGIS processing provider for 3d algorithms, available only when QGIS is built WITH_3D Currently includes only a single algorithm for tesselating geometries, which exposes the functionality of QgsTesselator to processing. Like the native c++ algorithm provider, algorithms in the 3d provider are transparently merged with the other QGIS providers (i.e. they aren't separated into their own group)
1 parent 8c211f2 commit c0e732b

File tree

8 files changed

+329
-7
lines changed

8 files changed

+329
-7
lines changed

python/plugins/processing/gui/ProcessingToolbox.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,14 @@ def fillTreeUsingProviders(self):
370370

371371
# first add qgis/native providers, since they create top level groups
372372
for provider in QgsApplication.processingRegistry().providers():
373-
if provider.id() in ('qgis', 'native'):
373+
if provider.id() in ('qgis', 'native', '3d'):
374374
self.addAlgorithmsFromProvider(provider, self.algorithmTree.invisibleRootItem())
375375
else:
376376
continue
377377
self.algorithmTree.sortItems(0, Qt.AscendingOrder)
378378

379379
for provider in QgsApplication.processingRegistry().providers():
380-
if provider.id() in ('qgis', 'native'):
380+
if provider.id() in ('qgis', 'native', '3d'):
381381
# already added
382382
continue
383383
else:
@@ -422,7 +422,7 @@ def addAlgorithmsFromProvider(self, provider, parent):
422422
groupItem = TreeGroupItem(alg.group())
423423
if not active:
424424
groupItem.setInactive()
425-
if provider.id() in ('qgis', 'native'):
425+
if provider.id() in ('qgis', 'native', '3d'):
426426
groupItem.setIcon(0, provider.icon())
427427
groups[alg.group()] = groupItem
428428
algItem = TreeAlgorithmItem(alg)
@@ -444,7 +444,7 @@ def addAlgorithmsFromProvider(self, provider, parent):
444444

445445
text = provider.name()
446446

447-
if not provider.id() in ('qgis', 'native'):
447+
if not provider.id() in ('qgis', 'native', '3d'):
448448
if not active:
449449
def activateProvider():
450450
self.activateProvider(provider.id())
@@ -459,7 +459,7 @@ def activateProvider():
459459
for group, groupItem in sorted(groups.items(), key=operator.itemgetter(1)):
460460
parent.addChild(groupItem)
461461

462-
if not provider.id() in ('qgis', 'native'):
462+
if not provider.id() in ('qgis', 'native', '3d'):
463463
parent.setHidden(parent.childCount() == 0)
464464

465465

python/plugins/processing/modeler/ModelerDialog.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -655,15 +655,15 @@ def fillAlgorithmTreeUsingProviders(self):
655655
if show:
656656
if alg.group() in groups:
657657
groupItem = groups[alg.group()]
658-
elif provider.id() in ('qgis', 'native') and alg.group() in qgis_groups:
658+
elif provider.id() in ('qgis', 'native', '3d') and alg.group() in qgis_groups:
659659
groupItem = qgis_groups[alg.group()]
660660
else:
661661
groupItem = QTreeWidgetItem()
662662
name = alg.group()
663663
groupItem.setText(0, name)
664664
groupItem.setToolTip(0, name)
665665
groupItem.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
666-
if provider.id() in ('qgis', 'native'):
666+
if provider.id() in ('qgis', 'native', '3d'):
667667
groupItem.setIcon(0, provider.icon())
668668
qgis_groups[alg.group()] = groupItem
669669
else:

src/3d/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ SET(QGIS_3D_SRCS
1919
chunks/qgschunknode_p.cpp
2020
chunks/qgschunkqueuejob_p.cpp
2121

22+
processing/qgs3dalgorithms.cpp
23+
processing/qgsalgorithmtesselate.cpp
24+
2225
symbols/qgsabstract3dsymbol.cpp
2326
symbols/qgsline3dsymbol.cpp
2427
symbols/qgsline3dsymbol_p.cpp
@@ -56,6 +59,8 @@ SET(QGIS_3D_MOC_HDRS
5659
chunks/qgschunkloader_p.h
5760
chunks/qgschunkqueuejob_p.h
5861

62+
processing/qgs3dalgorithms.h
63+
5964
terrain/qgsdemterraintileloader_p.h
6065
terrain/qgsterrainentity_p.h
6166
terrain/qgsterraintexturegenerator_p.h

src/3d/processing/qgs3dalgorithms.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/***************************************************************************
2+
qgs3dalgorithms.cpp
3+
---------------------
4+
begin : November 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgs3dalgorithms.h"
19+
#include "qgsalgorithmtesselate.h"
20+
#include "qgsapplication.h"
21+
22+
///@cond PRIVATE
23+
24+
Qgs3DAlgorithms::Qgs3DAlgorithms( QObject *parent )
25+
: QgsProcessingProvider( parent )
26+
{}
27+
28+
QIcon Qgs3DAlgorithms::icon() const
29+
{
30+
return QgsApplication::getThemeIcon( QStringLiteral( "/providerQgis.svg" ) );
31+
}
32+
33+
QString Qgs3DAlgorithms::svgIconPath() const
34+
{
35+
return QgsApplication::iconPath( QStringLiteral( "providerQgis.svg" ) );
36+
}
37+
38+
QString Qgs3DAlgorithms::id() const
39+
{
40+
return QStringLiteral( "3d" );
41+
}
42+
43+
QString Qgs3DAlgorithms::name() const
44+
{
45+
return tr( "QGIS (3D)" );
46+
}
47+
48+
bool Qgs3DAlgorithms::supportsNonFileBasedOutput() const
49+
{
50+
return true;
51+
}
52+
53+
void Qgs3DAlgorithms::loadAlgorithms()
54+
{
55+
addAlgorithm( new QgsTesselateAlgorithm() );
56+
}
57+
58+
59+
///@endcond
60+
61+
62+

src/3d/processing/qgs3dalgorithms.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/***************************************************************************
2+
qgs3dalgorithms.h
3+
---------------------
4+
begin : November 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGS3DALGORITHMS_H
19+
#define QGS3DALGORITHMS_H
20+
21+
#include "qgis_3d.h"
22+
#include "qgis.h"
23+
#include "processing/qgsprocessingprovider.h"
24+
25+
/**
26+
* \ingroup analysis
27+
* \class Qgs3DAlgorithms
28+
* \since QGIS 3.0
29+
* \brief QGIS 3D processing algorithm provider.
30+
*/
31+
class _3D_EXPORT Qgs3DAlgorithms: public QgsProcessingProvider
32+
{
33+
Q_OBJECT
34+
35+
public:
36+
37+
/**
38+
* Constructor for Qgs3DAlgorithms.
39+
*/
40+
Qgs3DAlgorithms( QObject *parent = nullptr );
41+
42+
QIcon icon() const override;
43+
QString svgIconPath() const override;
44+
QString id() const override;
45+
QString name() const override;
46+
bool supportsNonFileBasedOutput() const override;
47+
48+
protected:
49+
50+
void loadAlgorithms() override;
51+
52+
};
53+
54+
#endif // QGS3DALGORITHMS_H
55+
56+
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/***************************************************************************
2+
qgsalgorithmtesselate.cpp
3+
---------------------
4+
begin : November 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsalgorithmtesselate.h"
19+
#include "qgstessellator.h"
20+
#include "qgsmultipolygon.h"
21+
#include "qgstriangle.h"
22+
///@cond PRIVATE
23+
24+
QString QgsTesselateAlgorithm::name() const
25+
{
26+
return QStringLiteral( "tesselate" );
27+
}
28+
29+
QString QgsTesselateAlgorithm::displayName() const
30+
{
31+
return QObject::tr( "Tesselate geometry" );
32+
}
33+
34+
QStringList QgsTesselateAlgorithm::tags() const
35+
{
36+
return QObject::tr( "3d,triangle" ).split( ',' );
37+
}
38+
39+
QString QgsTesselateAlgorithm::group() const
40+
{
41+
return QObject::tr( "Vector geometry" );
42+
}
43+
44+
QString QgsTesselateAlgorithm::outputName() const
45+
{
46+
return QObject::tr( "Tesselated" );
47+
}
48+
49+
QgsProcessing::SourceType QgsTesselateAlgorithm::outputLayerType() const
50+
{
51+
return QgsProcessing::TypeVectorPolygon;
52+
}
53+
54+
QgsWkbTypes::Type QgsTesselateAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
55+
{
56+
Q_UNUSED( inputWkbType );
57+
return QgsWkbTypes::MultiPolygonZ;
58+
}
59+
60+
QString QgsTesselateAlgorithm::shortHelpString() const
61+
{
62+
return QObject::tr( "This algorithm tesselates a polygon geometry layer, dividing the geometries into triangular components." )
63+
+ QStringLiteral( "\n\n" )
64+
+ QObject::tr( "The output layer consists of multipolygon geometries for each input feature, with each multipolygon consisting of multiple triangle component polygons." );
65+
}
66+
67+
QList<int> QgsTesselateAlgorithm::inputLayerTypes() const
68+
{
69+
return QList<int>() << QgsProcessing::TypeVectorPolygon;
70+
}
71+
72+
QgsTesselateAlgorithm *QgsTesselateAlgorithm::createInstance() const
73+
{
74+
return new QgsTesselateAlgorithm();
75+
}
76+
77+
QgsPoint getPointFromData( QVector< float >::const_iterator &it )
78+
{
79+
// tesselator geometry is x, z, -y
80+
double x = *it;
81+
++it;
82+
double z = *it;
83+
++it;
84+
double y = -( *it );
85+
++it;
86+
return QgsPoint( x, y, z );
87+
}
88+
89+
void tesselatePolygon( const QgsPolygon *polygon, QgsMultiPolygon *destination )
90+
{
91+
QgsTessellator t( 0, 0, false );
92+
t.addPolygon( *polygon, 0 );
93+
94+
QVector<float> data = t.data();
95+
for ( auto it = data.constBegin(); it != data.constEnd(); )
96+
{
97+
QgsPoint p1 = getPointFromData( it );
98+
QgsPoint p2 = getPointFromData( it );
99+
QgsPoint p3 = getPointFromData( it );
100+
destination->addGeometry( new QgsTriangle( p1, p2, p3 ) );
101+
}
102+
}
103+
104+
QgsFeature QgsTesselateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingFeedback * )
105+
{
106+
QgsFeature f = feature;
107+
if ( f.hasGeometry() )
108+
{
109+
if ( QgsWkbTypes::geometryType( f.geometry().wkbType() ) != QgsWkbTypes::PolygonGeometry )
110+
f.clearGeometry();
111+
else
112+
{
113+
std::unique_ptr< QgsMultiPolygon > mp = qgis::make_unique< QgsMultiPolygon >();
114+
if ( f.geometry().isMultipart() )
115+
{
116+
const QgsMultiSurface *ms = qgsgeometry_cast< const QgsMultiSurface * >( f.geometry().constGet() );
117+
for ( int i = 0; i < ms->numGeometries(); ++i )
118+
{
119+
std::unique_ptr< QgsPolygon > p( qgsgeometry_cast< QgsPolygon * >( ms->geometryN( i )->segmentize() ) );
120+
tesselatePolygon( p.get(), mp.get() );
121+
}
122+
}
123+
else
124+
{
125+
std::unique_ptr< QgsPolygon > p( qgsgeometry_cast< QgsPolygon * >( f.geometry().constGet()->segmentize() ) );
126+
tesselatePolygon( p.get(), mp.get() );
127+
}
128+
f.setGeometry( QgsGeometry( std::move( mp ) ) );
129+
}
130+
}
131+
return f;
132+
}
133+
134+
///@endcond
135+
136+
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/***************************************************************************
2+
qgsalgorithmtesselate.h
3+
---------------------
4+
begin : November 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSALGORITHMTESSELATE_H
19+
#define QGSALGORITHMTESSELATE_H
20+
21+
#define SIP_NO_FILE
22+
23+
#include "qgis.h"
24+
#include "processing/qgsprocessingalgorithm.h"
25+
26+
///@cond PRIVATE
27+
28+
/**
29+
* Native tesselate algorithm.
30+
*/
31+
class QgsTesselateAlgorithm : public QgsProcessingFeatureBasedAlgorithm
32+
{
33+
34+
public:
35+
36+
QgsTesselateAlgorithm() = default;
37+
QString name() const override;
38+
QString displayName() const override;
39+
virtual QStringList tags() const override;
40+
QString group() const override;
41+
QString shortHelpString() const override;
42+
QList<int> inputLayerTypes() const override;
43+
QgsTesselateAlgorithm *createInstance() const override SIP_FACTORY;
44+
45+
protected:
46+
QString outputName() const override;
47+
QgsProcessing::SourceType outputLayerType() const override;
48+
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override;
49+
50+
QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override;
51+
52+
};
53+
54+
55+
///@endcond PRIVATE
56+
57+
#endif // QGSALGORITHMTESSELATE_H
58+
59+

0 commit comments

Comments
 (0)