-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
sgrubberband.cpp
152 lines (134 loc) · 4.84 KB
/
sgrubberband.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
/***************************************************************************
sgrubberband.cpp
---------------------
begin : March 2020
copyright : (C) 2020 by David Signer
email : david at opengis dot ch
***************************************************************************
* *
* 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 "sgrubberband.h"
#include <qgscurvepolygon.h>
#include <qgsgeometry.h>
#include <qgsgeos.h>
#include <qgslinestring.h>
#include <qgspolygon.h>
#include <qgssurface.h>
#include <qgstessellator.h>
SGRubberband::SGRubberband( const QVector<QgsPoint> &points, Qgis::GeometryType type, const QColor &color, float width )
: QSGNode()
{
mMaterial.setColor( color );
if ( points.isEmpty() )
return;
switch ( type )
{
case Qgis::GeometryType::Point:
// TODO: Implement
break;
case Qgis::GeometryType::Line:
{
appendChildNode( createLineGeometry( points, width ) );
break;
}
case Qgis::GeometryType::Polygon:
{
appendChildNode( createLineGeometry( points, width ) );
if ( points.size() > 2 )
{
QString error;
QgsPolygon polygon = QgsPolygon( new QgsLineString( points ) );
if ( !polygon.isValid( error ) )
{
QgsGeos geos( &polygon );
std::unique_ptr<QgsAbstractGeometry> validPolygon = geos.makeValid();
QgsGeometryPartIterator parts = validPolygon->parts();
while ( parts.hasNext() )
{
QgsPolygon *p = qgsgeometry_cast<QgsPolygon *>( parts.next() );
if ( p )
{
QgsCurve *l = p->exteriorRing();
if ( l )
{
QgsPointSequence pts;
l->points( pts );
appendChildNode( createPolygonGeometry( pts ) );
}
}
}
}
else
{
appendChildNode( createPolygonGeometry( points ) );
}
}
break;
}
case Qgis::GeometryType::Unknown:
case Qgis::GeometryType::Null:
break;
}
}
QSGGeometryNode *SGRubberband::createLineGeometry( const QVector<QgsPoint> &points, float width )
{
QSGGeometryNode *node = new QSGGeometryNode;
QSGGeometry *sgGeom = new QSGGeometry( QSGGeometry::defaultAttributes_Point2D(), points.count() );
QSGGeometry::Point2D *vertices = sgGeom->vertexDataAsPoint2D();
int i = 0;
for ( const QgsPoint &pt : points )
{
vertices[i++].set( static_cast<float>( pt.x() ), static_cast<float>( pt.y() ) );
}
sgGeom->setLineWidth( static_cast<float>( width ) );
sgGeom->setDrawingMode( QSGGeometry::DrawLineStrip );
node->setGeometry( sgGeom );
node->setMaterial( &mMaterial );
node->setFlag( QSGNode::OwnsGeometry );
node->setFlag( QSGNode::OwnedByParent );
return node;
}
QSGGeometryNode *SGRubberband::createPolygonGeometry( const QVector<QgsPoint> &points )
{
QgsGeometry geom( new QgsPolygon( new QgsLineString( points ) ) );
geom = geom.buffer( 0.0000001, 1, Qgis::EndCapStyle::Flat, Qgis::JoinStyle::Miter, 5 );
// QgsTesselator doesn't allow for coordinates distance smaller than 0.001
geom.removeDuplicateNodes( 0.001 );
QgsPolygon *polygon = qgsgeometry_cast<QgsPolygon *>( geom.constGet() );
QgsTessellator t( 0, 0, false, false, false, true );
if ( points.size() > 2 && polygon )
{
t.addPolygon( *polygon, 0 );
}
QSGGeometryNode *node = new QSGGeometryNode;
QSGGeometry *sgGeom = new QSGGeometry( QSGGeometry::defaultAttributes_Point2D(), t.dataVerticesCount() );
QSGGeometry::Point2D *vertices = sgGeom->vertexDataAsPoint2D();
const QVector<float> triangleData = t.data();
int currentVertex = 0;
for ( auto it = triangleData.constBegin(); it != triangleData.constEnd(); )
{
vertices[currentVertex].x = ( *it++ );
( void ) it++; // z
vertices[currentVertex].y = -( *it++ );
currentVertex++;
vertices[currentVertex].x = ( *it++ );
( void ) it++; // z
vertices[currentVertex].y = -( *it++ );
currentVertex++;
vertices[currentVertex].x = ( *it++ );
( void ) it++; // z
vertices[currentVertex].y = -( *it++ );
currentVertex++;
}
sgGeom->setDrawingMode( QSGGeometry::DrawTriangles );
node->setGeometry( sgGeom );
node->setMaterial( &mMaterial );
node->setFlag( QSGNode::OwnsGeometry );
node->setFlag( QSGNode::OwnedByParent );
return node;
}