|
| 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 | + |
0 commit comments