/
qgstracer.cpp
809 lines (654 loc) · 20.6 KB
/
qgstracer.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/***************************************************************************
qgstracer.cpp
--------------------------------------
Date : January 2016
Copyright : (C) 2016 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 "qgstracer.h"
#include "qgsfeatureiterator.h"
#include "qgsgeometry.h"
#include "qgsgeometryutils.h"
#include "qgsgeos.h"
#include "qgslogger.h"
#include "qgsvectorlayer.h"
#include "qgsexception.h"
#include "qgsrenderer.h"
#include "qgssettings.h"
#include <queue>
#include <vector>
typedef std::pair<int, double> DijkstraQueueItem; // first = vertex index, second = distance
// utility comparator for queue items based on distance
struct comp
{
bool operator()( DijkstraQueueItem a, DijkstraQueueItem b )
{
return a.second > b.second;
}
};
// TODO: move to geometry utils
double distance2D( const QgsPolylineXY &coords )
{
int np = coords.count();
if ( np == 0 )
return 0;
double x0 = coords[0].x(), y0 = coords[0].y();
double x1, y1;
double dist = 0;
for ( int i = 1; i < np; ++i )
{
x1 = coords[i].x();
y1 = coords[i].y();
dist += std::sqrt( ( x1 - x0 ) * ( x1 - x0 ) + ( y1 - y0 ) * ( y1 - y0 ) );
x0 = x1;
y0 = y1;
}
return dist;
}
// TODO: move to geometry utils
double closestSegment( const QgsPolylineXY &pl, const QgsPointXY &pt, int &vertexAfter, double epsilon )
{
double sqrDist = std::numeric_limits<double>::max();
const QgsPointXY *pldata = pl.constData();
int plcount = pl.count();
double prevX = pldata[0].x(), prevY = pldata[0].y();
double segmentPtX, segmentPtY;
for ( int i = 1; i < plcount; ++i )
{
double currentX = pldata[i].x();
double currentY = pldata[i].y();
double testDist = QgsGeometryUtils::sqrDistToLine( pt.x(), pt.y(), prevX, prevY, currentX, currentY, segmentPtX, segmentPtY, epsilon );
if ( testDist < sqrDist )
{
sqrDist = testDist;
vertexAfter = i;
}
prevX = currentX;
prevY = currentY;
}
return sqrDist;
}
/////
//! Simple graph structure for shortest path search
struct QgsTracerGraph
{
QgsTracerGraph() = default;
struct E // bidirectional edge
{
//! vertices that the edge connects
int v1, v2;
//! coordinates of the edge (including endpoints)
QVector<QgsPointXY> coords;
int otherVertex( int v0 ) const { return v1 == v0 ? v2 : v1; }
double weight() const { return distance2D( coords ); }
};
struct V
{
//! location of the vertex
QgsPointXY pt;
//! indices of adjacent edges (used in Dijkstra algorithm)
QVector<int> edges;
};
//! Vertices of the graph
QVector<V> v;
//! Edges of the graph
QVector<E> e;
//! Temporarily removed edges
QSet<int> inactiveEdges;
//! Temporarily added vertices (for each there are two extra edges)
int joinedVertices{ 0 };
};
QgsTracerGraph *makeGraph( const QVector<QgsPolylineXY> &edges )
{
QgsTracerGraph *g = new QgsTracerGraph();
g->joinedVertices = 0;
QHash<QgsPointXY, int> point2vertex;
Q_FOREACH ( const QgsPolylineXY &line, edges )
{
QgsPointXY p1( line[0] );
QgsPointXY p2( line[line.count() - 1] );
int v1 = -1, v2 = -1;
// get or add vertex 1
if ( point2vertex.contains( p1 ) )
v1 = point2vertex.value( p1 );
else
{
v1 = g->v.count();
QgsTracerGraph::V v;
v.pt = p1;
g->v.append( v );
point2vertex[p1] = v1;
}
// get or add vertex 2
if ( point2vertex.contains( p2 ) )
v2 = point2vertex.value( p2 );
else
{
v2 = g->v.count();
QgsTracerGraph::V v;
v.pt = p2;
g->v.append( v );
point2vertex[p2] = v2;
}
// add edge
QgsTracerGraph::E e;
e.v1 = v1;
e.v2 = v2;
e.coords = line;
g->e.append( e );
// link edge to vertices
int eIdx = g->e.count() - 1;
g->v[v1].edges << eIdx;
g->v[v2].edges << eIdx;
}
return g;
}
QVector<QgsPointXY> shortestPath( const QgsTracerGraph &g, int v1, int v2 )
{
if ( v1 == -1 || v2 == -1 )
return QVector<QgsPointXY>(); // invalid input
// priority queue to drive Dijkstra:
// first of the pair is vertex index, second is distance
std::priority_queue< DijkstraQueueItem, std::vector< DijkstraQueueItem >, comp > Q;
// shortest distances to each vertex
QVector<double> D( g.v.count(), std::numeric_limits<double>::max() );
D[v1] = 0;
// whether vertices have been already processed
QVector<bool> F( g.v.count() );
// using which edge there is shortest path to each vertex
QVector<int> S( g.v.count(), -1 );
int u = -1;
Q.push( DijkstraQueueItem( v1, 0 ) );
while ( !Q.empty() )
{
u = Q.top().first; // new vertex to visit
Q.pop();
if ( u == v2 )
break; // we can stop now, there won't be a shorter path
if ( F[u] )
continue; // ignore previously added path which is actually longer
const QgsTracerGraph::V &vu = g.v[u];
const int *vuEdges = vu.edges.constData();
int count = vu.edges.count();
for ( int i = 0; i < count; ++i )
{
const QgsTracerGraph::E &edge = g.e[ vuEdges[i] ];
int v = edge.otherVertex( u );
double w = edge.weight();
if ( !F[v] && D[u] + w < D[v] )
{
// found a shorter way to the vertex
D[v] = D[u] + w;
S[v] = vuEdges[i];
Q.push( DijkstraQueueItem( v, D[v] ) );
}
}
F[u] = true; // mark the vertex as processed (we know the fastest path to it)
}
if ( u != v2 ) // there's no path to the end vertex
return QVector<QgsPointXY>();
//qDebug("dist %f", D[u]);
QVector<QgsPointXY> points;
QList<int> path;
while ( S[u] != -1 )
{
path << S[u];
const QgsTracerGraph::E &e = g.e[S[u]];
QVector<QgsPointXY> edgePoints = e.coords;
if ( edgePoints[0] != g.v[u].pt )
std::reverse( edgePoints.begin(), edgePoints.end() );
if ( !points.isEmpty() )
points.remove( points.count() - 1 ); // chop last one (will be used from next edge)
points << edgePoints;
u = e.otherVertex( u );
}
std::reverse( path.begin(), path.end() );
//Q_FOREACH (int x, path)
// qDebug("e: %d", x);
std::reverse( points.begin(), points.end() );
return points;
}
int point2vertex( const QgsTracerGraph &g, const QgsPointXY &pt, double epsilon = 1e-6 )
{
// TODO: use spatial index
for ( int i = 0; i < g.v.count(); ++i )
{
const QgsTracerGraph::V &v = g.v.at( i );
if ( v.pt == pt || ( std::fabs( v.pt.x() - pt.x() ) < epsilon && std::fabs( v.pt.y() - pt.y() ) < epsilon ) )
return i;
}
return -1;
}
int point2edge( const QgsTracerGraph &g, const QgsPointXY &pt, int &lineVertexAfter, double epsilon = 1e-6 )
{
int vertexAfter;
for ( int i = 0; i < g.e.count(); ++i )
{
if ( g.inactiveEdges.contains( i ) )
continue; // ignore temporarily disabled edges
const QgsTracerGraph::E &e = g.e.at( i );
double dist = closestSegment( e.coords, pt, vertexAfter, epsilon );
if ( dist == 0 )
{
lineVertexAfter = vertexAfter; //NOLINT
return i;
}
}
return -1;
}
void splitLinestring( const QgsPolylineXY &points, const QgsPointXY &pt, int lineVertexAfter, QgsPolylineXY &pts1, QgsPolylineXY &pts2 )
{
int count1 = lineVertexAfter;
int count2 = points.count() - lineVertexAfter;
for ( int i = 0; i < count1; ++i )
pts1 << points[i];
if ( points[lineVertexAfter - 1] != pt )
pts1 << pt; // repeat if not split exactly at that point
if ( pt != points[lineVertexAfter] )
pts2 << pt; // repeat if not split exactly at that point
for ( int i = 0; i < count2; ++i )
pts2 << points[i + lineVertexAfter];
}
int joinVertexToGraph( QgsTracerGraph &g, const QgsPointXY &pt )
{
// find edge where the point is
int lineVertexAfter;
int eIdx = point2edge( g, pt, lineVertexAfter );
//qDebug("e: %d", eIdx);
if ( eIdx == -1 )
return -1;
const QgsTracerGraph::E &e = g.e[eIdx];
QgsTracerGraph::V &v1 = g.v[e.v1];
QgsTracerGraph::V &v2 = g.v[e.v2];
QgsPolylineXY out1, out2;
splitLinestring( e.coords, pt, lineVertexAfter, out1, out2 );
int vIdx = g.v.count();
int e1Idx = g.e.count();
int e2Idx = e1Idx + 1;
// prepare new vertex and edges
QgsTracerGraph::V v;
v.pt = pt;
v.edges << e1Idx << e2Idx;
QgsTracerGraph::E e1;
e1.v1 = e.v1;
e1.v2 = vIdx;
e1.coords = out1;
QgsTracerGraph::E e2;
e2.v1 = vIdx;
e2.v2 = e.v2;
e2.coords = out2;
// update edge connectivity of existing vertices
v1.edges.replace( v1.edges.indexOf( eIdx ), e1Idx );
v2.edges.replace( v2.edges.indexOf( eIdx ), e2Idx );
g.inactiveEdges << eIdx;
// add new vertex and edges to the graph
g.v.append( v );
g.e.append( e1 );
g.e.append( e2 );
g.joinedVertices++;
return vIdx;
}
int pointInGraph( QgsTracerGraph &g, const QgsPointXY &pt )
{
// try to use existing vertex in the graph
int v = point2vertex( g, pt );
if ( v != -1 )
return v;
// try to add the vertex to an edge (may fail if point is not on edge)
return joinVertexToGraph( g, pt );
}
void resetGraph( QgsTracerGraph &g )
{
// remove extra vertices and edges
g.v.resize( g.v.count() - g.joinedVertices );
g.e.resize( g.e.count() - g.joinedVertices * 2 );
g.joinedVertices = 0;
// fix vertices of deactivated edges
Q_FOREACH ( int eIdx, g.inactiveEdges )
{
if ( eIdx >= g.e.count() )
continue;
const QgsTracerGraph::E &e = g.e[eIdx];
QgsTracerGraph::V &v1 = g.v[e.v1];
for ( int i = 0; i < v1.edges.count(); ++i )
{
if ( v1.edges[i] >= g.e.count() )
v1.edges.remove( i-- );
}
v1.edges << eIdx;
QgsTracerGraph::V &v2 = g.v[e.v2];
for ( int i = 0; i < v2.edges.count(); ++i )
{
if ( v2.edges[i] >= g.e.count() )
v2.edges.remove( i-- );
}
v2.edges << eIdx;
}
g.inactiveEdges.clear();
}
void extractLinework( const QgsGeometry &g, QgsMultiPolylineXY &mpl )
{
QgsGeometry geom = g;
// segmentize curved geometries - we will use noding algorithm from GEOS
// to find all intersections a bit later (so we need them segmentized anyway)
if ( QgsWkbTypes::isCurvedType( g.wkbType() ) )
{
QgsAbstractGeometry *segmentizedGeomV2 = g.constGet()->segmentize();
if ( !segmentizedGeomV2 )
return;
geom = QgsGeometry( segmentizedGeomV2 );
}
switch ( QgsWkbTypes::flatType( geom.wkbType() ) )
{
case QgsWkbTypes::LineString:
mpl << geom.asPolyline();
break;
case QgsWkbTypes::Polygon:
Q_FOREACH ( const QgsPolylineXY &ring, geom.asPolygon() )
mpl << ring;
break;
case QgsWkbTypes::MultiLineString:
Q_FOREACH ( const QgsPolylineXY &linestring, geom.asMultiPolyline() )
mpl << linestring;
break;
case QgsWkbTypes::MultiPolygon:
Q_FOREACH ( const QgsPolygonXY &polygon, geom.asMultiPolygon() )
Q_FOREACH ( const QgsPolylineXY &ring, polygon )
mpl << ring;
break;
default:
break; // unknown type - do nothing
}
}
// -------------
QgsTracer::QgsTracer() = default;
bool QgsTracer::initGraph()
{
if ( mGraph )
return true; // already initialized
mHasTopologyProblem = false;
QgsFeature f;
QgsMultiPolylineXY mpl;
// extract linestrings
// TODO: use QgsPointLocator as a source for the linework
QTime t1, t2, t2a, t3;
t1.start();
int featuresCounted = 0;
bool enableInvisibleFeature = QgsSettings().value( QStringLiteral( "/qgis/digitizing/snap_invisible_feature" ), false ).toBool();
for ( QgsVectorLayer *vl : qgis::as_const( mLayers ) )
{
QgsFeatureRequest request;
bool filter = false;
std::unique_ptr< QgsFeatureRenderer > renderer;
QgsRenderContext *ctx = nullptr;
if ( !enableInvisibleFeature && mRenderContext )
{
renderer.reset( vl->renderer() ? vl->renderer()->clone() : nullptr );
mRenderContext->expressionContext() << QgsExpressionContextUtils::layerScope( vl );
ctx = mRenderContext.get();
if ( renderer )
{
// setup scale for scale dependent visibility (rule based)
renderer->startRender( *ctx, vl->fields() );
filter = renderer->capabilities() & QgsFeatureRenderer::Filter;
request.setSubsetOfAttributes( renderer->usedAttributes( *ctx ), vl->fields() );
}
}
else
{
request.setSubsetOfAttributes( QgsAttributeList() );
}
request.setDestinationCrs( mCRS, mTransformContext );
if ( !mExtent.isEmpty() )
request.setFilterRect( mExtent );
QgsFeatureIterator fi = vl->getFeatures( request );
while ( fi.nextFeature( f ) )
{
if ( !f.hasGeometry() )
continue;
if ( filter && ctx && renderer )
{
ctx->expressionContext().setFeature( f );
if ( !renderer->willRenderFeature( f, *ctx ) )
{
continue;
}
}
extractLinework( f.geometry(), mpl );
++featuresCounted;
if ( mMaxFeatureCount != 0 && featuresCounted >= mMaxFeatureCount )
return false;
}
if ( ctx && renderer )
{
renderer->stopRender( *ctx );
}
}
int timeExtract = t1.elapsed();
// resolve intersections
t2.start();
int timeNodingCall = 0;
#if 0
// without noding - if data are known to be noded beforehand
#else
QgsGeometry allGeom = QgsGeometry::fromMultiPolylineXY( mpl );
try
{
t2a.start();
// GEOSNode_r may throw an exception
geos::unique_ptr allGeomGeos( QgsGeos::asGeos( allGeom ) );
geos::unique_ptr allNoded( GEOSNode_r( QgsGeos::getGEOSHandler(), allGeomGeos.get() ) );
timeNodingCall = t2a.elapsed();
QgsGeometry noded = QgsGeos::geometryFromGeos( allNoded.release() );
mpl = noded.asMultiPolyline();
}
catch ( GEOSException &e )
{
// no big deal... we will just not have nicely noded linework, potentially
// missing some intersections
mHasTopologyProblem = true;
QgsDebugMsg( QString( "Tracer Noding Exception: %1" ).arg( e.what() ) );
}
#endif
int timeNoding = t2.elapsed();
t3.start();
mGraph.reset( makeGraph( mpl ) );
int timeMake = t3.elapsed();
Q_UNUSED( timeExtract );
Q_UNUSED( timeNoding );
Q_UNUSED( timeNodingCall );
Q_UNUSED( timeMake );
QgsDebugMsg( QString( "tracer extract %1 ms, noding %2 ms (call %3 ms), make %4 ms" )
.arg( timeExtract ).arg( timeNoding ).arg( timeNodingCall ).arg( timeMake ) );
return true;
}
QgsTracer::~QgsTracer()
{
invalidateGraph();
}
void QgsTracer::setLayers( const QList<QgsVectorLayer *> &layers )
{
if ( mLayers == layers )
return;
Q_FOREACH ( QgsVectorLayer *layer, mLayers )
{
disconnect( layer, &QgsVectorLayer::featureAdded, this, &QgsTracer::onFeatureAdded );
disconnect( layer, &QgsVectorLayer::featureDeleted, this, &QgsTracer::onFeatureDeleted );
disconnect( layer, &QgsVectorLayer::geometryChanged, this, &QgsTracer::onGeometryChanged );
disconnect( layer, &QgsVectorLayer::attributeValueChanged, this, &QgsTracer::onAttributeValueChanged );
disconnect( layer, &QgsVectorLayer::dataChanged, this, &QgsTracer::onDataChanged );
disconnect( layer, &QgsVectorLayer::styleChanged, this, &QgsTracer::onStyleChanged );
disconnect( layer, &QObject::destroyed, this, &QgsTracer::onLayerDestroyed );
}
mLayers = layers;
Q_FOREACH ( QgsVectorLayer *layer, mLayers )
{
connect( layer, &QgsVectorLayer::featureAdded, this, &QgsTracer::onFeatureAdded );
connect( layer, &QgsVectorLayer::featureDeleted, this, &QgsTracer::onFeatureDeleted );
connect( layer, &QgsVectorLayer::geometryChanged, this, &QgsTracer::onGeometryChanged );
connect( layer, &QgsVectorLayer::attributeValueChanged, this, &QgsTracer::onAttributeValueChanged );
connect( layer, &QgsVectorLayer::dataChanged, this, &QgsTracer::onDataChanged );
connect( layer, &QObject::destroyed, this, &QgsTracer::onLayerDestroyed );
}
invalidateGraph();
}
void QgsTracer::setDestinationCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext )
{
mCRS = crs;
mTransformContext = transformContext;
invalidateGraph();
}
void QgsTracer::setRenderContext( const QgsRenderContext *renderContext )
{
mRenderContext = std::unique_ptr<QgsRenderContext>( new QgsRenderContext( *renderContext ) );
invalidateGraph();
}
void QgsTracer::setExtent( const QgsRectangle &extent )
{
if ( mExtent == extent )
return;
mExtent = extent;
invalidateGraph();
}
void QgsTracer::setOffset( double offset )
{
mOffset = offset;
}
void QgsTracer::offsetParameters( int &quadSegments, int &joinStyle, double &miterLimit )
{
quadSegments = mOffsetSegments;
joinStyle = mOffsetJoinStyle;
miterLimit = mOffsetMiterLimit;
}
void QgsTracer::setOffsetParameters( int quadSegments, int joinStyle, double miterLimit )
{
mOffsetSegments = quadSegments;
mOffsetJoinStyle = joinStyle;
mOffsetMiterLimit = miterLimit;
}
bool QgsTracer::init()
{
if ( mGraph )
return true;
// configuration from derived class?
configure();
return initGraph();
}
void QgsTracer::invalidateGraph()
{
mGraph.reset( nullptr );
}
void QgsTracer::onFeatureAdded( QgsFeatureId fid )
{
Q_UNUSED( fid );
invalidateGraph();
}
void QgsTracer::onFeatureDeleted( QgsFeatureId fid )
{
Q_UNUSED( fid );
invalidateGraph();
}
void QgsTracer::onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom )
{
Q_UNUSED( fid );
Q_UNUSED( geom );
invalidateGraph();
}
void QgsTracer::onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
{
Q_UNUSED( fid );
Q_UNUSED( idx );
Q_UNUSED( value );
if ( mRenderContext )
invalidateGraph();
}
void QgsTracer::onDataChanged( )
{
invalidateGraph();
}
void QgsTracer::onStyleChanged( )
{
if ( mRenderContext )
invalidateGraph();
}
void QgsTracer::onLayerDestroyed( QObject *obj )
{
// remove the layer before it is completely invalid (static_cast should be the safest cast)
mLayers.removeAll( static_cast<QgsVectorLayer *>( obj ) );
invalidateGraph();
}
QVector<QgsPointXY> QgsTracer::findShortestPath( const QgsPointXY &p1, const QgsPointXY &p2, PathError *error )
{
init(); // does nothing if the graph exists already
if ( !mGraph )
{
if ( error ) *error = ErrTooManyFeatures;
return QVector<QgsPointXY>();
}
QTime t;
t.start();
int v1 = pointInGraph( *mGraph, p1 );
int v2 = pointInGraph( *mGraph, p2 );
int tPrep = t.elapsed();
if ( v1 == -1 )
{
if ( error ) *error = ErrPoint1;
return QVector<QgsPointXY>();
}
if ( v2 == -1 )
{
if ( error ) *error = ErrPoint2;
return QVector<QgsPointXY>();
}
QTime t2;
t2.start();
QgsPolylineXY points = shortestPath( *mGraph, v1, v2 );
int tPath = t2.elapsed();
Q_UNUSED( tPrep );
Q_UNUSED( tPath );
QgsDebugMsg( QString( "path timing: prep %1 ms, path %2 ms" ).arg( tPrep ).arg( tPath ) );
resetGraph( *mGraph );
if ( !points.isEmpty() && mOffset != 0 )
{
QVector<QgsPointXY> pointsInput( points );
QgsLineString linestring( pointsInput );
std::unique_ptr<QgsGeometryEngine> linestringEngine( QgsGeometry::createGeometryEngine( &linestring ) );
std::unique_ptr<QgsAbstractGeometry> linestringOffset( linestringEngine->offsetCurve( mOffset, mOffsetSegments, mOffsetJoinStyle, mOffsetMiterLimit ) );
if ( QgsLineString *ls2 = qgsgeometry_cast<QgsLineString *>( linestringOffset.get() ) )
{
points.clear();
for ( int i = 0; i < ls2->numPoints(); ++i )
points << QgsPointXY( ls2->pointN( i ) );
// sometimes (with negative offset?) the resulting curve is reversed
if ( points.count() >= 2 )
{
QgsPointXY res1 = points.first(), res2 = points.last();
double diffNormal = res1.distance( p1 ) + res2.distance( p2 );
double diffReversed = res1.distance( p2 ) + res2.distance( p1 );
if ( diffReversed < diffNormal )
std::reverse( points.begin(), points.end() );
}
}
}
if ( error )
*error = points.isEmpty() ? ErrNoPath : ErrNone;
return points;
}
bool QgsTracer::isPointSnapped( const QgsPointXY &pt )
{
init(); // does nothing if the graph exists already
if ( !mGraph )
return false;
if ( point2vertex( *mGraph, pt ) != -1 )
return true;
int lineVertexAfter;
int e = point2edge( *mGraph, pt, lineVertexAfter );
return e != -1;
}