/
qgsgeometryutils.cpp
1882 lines (1626 loc) · 57.7 KB
/
qgsgeometryutils.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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsgeometryutils.cpp
-------------------------------------------------------------------
Date : 21 Nov 2014
Copyright : (C) 2014 by Marco Hugentobler
email : marco.hugentobler at sourcepole 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 "qgsgeometryutils.h"
#include "qgscurve.h"
#include "qgscurvepolygon.h"
#include "qgsgeometrycollection.h"
#include "qgslinestring.h"
#include "qgswkbptr.h"
#include "qgslogger.h"
#include <memory>
#include <QStringList>
#include <QVector>
#include <QRegularExpression>
#include <nlohmann/json.hpp>
QVector<QgsLineString *> QgsGeometryUtils::extractLineStrings( const QgsAbstractGeometry *geom )
{
QVector< QgsLineString * > linestrings;
if ( !geom )
return linestrings;
QVector< const QgsAbstractGeometry * > geometries;
geometries << geom;
while ( ! geometries.isEmpty() )
{
const QgsAbstractGeometry *g = geometries.takeFirst();
if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( g ) )
{
linestrings << static_cast< QgsLineString * >( curve->segmentize() );
}
else if ( const QgsGeometryCollection *collection = qgsgeometry_cast< const QgsGeometryCollection * >( g ) )
{
for ( int i = 0; i < collection->numGeometries(); ++i )
{
geometries.append( collection->geometryN( i ) );
}
}
else if ( const QgsCurvePolygon *curvePolygon = qgsgeometry_cast< const QgsCurvePolygon * >( g ) )
{
if ( curvePolygon->exteriorRing() )
linestrings << static_cast< QgsLineString * >( curvePolygon->exteriorRing()->segmentize() );
for ( int i = 0; i < curvePolygon->numInteriorRings(); ++i )
{
linestrings << static_cast< QgsLineString * >( curvePolygon->interiorRing( i )->segmentize() );
}
}
}
return linestrings;
}
QgsPoint QgsGeometryUtils::closestVertex( const QgsAbstractGeometry &geom, const QgsPoint &pt, QgsVertexId &id )
{
double minDist = std::numeric_limits<double>::max();
double currentDist = 0;
QgsPoint minDistPoint;
id = QgsVertexId(); // set as invalid
if ( geom.isEmpty() || pt.isEmpty() )
return minDistPoint;
QgsVertexId vertexId;
QgsPoint vertex;
while ( geom.nextVertex( vertexId, vertex ) )
{
currentDist = QgsGeometryUtils::sqrDistance2D( pt, vertex );
// The <= is on purpose: for geometries with closing vertices, this ensures
// that the closing vertex is returned. For the vertex tool, the rubberband
// of the closing vertex is above the opening vertex, hence with the <=
// situations where the covered opening vertex rubberband is selected are
// avoided.
if ( currentDist <= minDist )
{
minDist = currentDist;
minDistPoint = vertex;
id.part = vertexId.part;
id.ring = vertexId.ring;
id.vertex = vertexId.vertex;
id.type = vertexId.type;
}
}
return minDistPoint;
}
QgsPoint QgsGeometryUtils::closestPoint( const QgsAbstractGeometry &geometry, const QgsPoint &point )
{
QgsPoint closestPoint;
QgsVertexId vertexAfter;
geometry.closestSegment( point, closestPoint, vertexAfter, nullptr, DEFAULT_SEGMENT_EPSILON );
if ( vertexAfter.isValid() )
{
const QgsPoint pointAfter = geometry.vertexAt( vertexAfter );
if ( vertexAfter.vertex > 0 )
{
QgsVertexId vertexBefore = vertexAfter;
vertexBefore.vertex--;
const QgsPoint pointBefore = geometry.vertexAt( vertexBefore );
const double length = pointBefore.distance( pointAfter );
const double distance = pointBefore.distance( closestPoint );
if ( qgsDoubleNear( distance, 0.0 ) )
closestPoint = pointBefore;
else if ( qgsDoubleNear( distance, length ) )
closestPoint = pointAfter;
else
{
if ( QgsWkbTypes::hasZ( geometry.wkbType() ) && length )
closestPoint.addZValue( pointBefore.z() + ( pointAfter.z() - pointBefore.z() ) * distance / length );
if ( QgsWkbTypes::hasM( geometry.wkbType() ) )
closestPoint.addMValue( pointBefore.m() + ( pointAfter.m() - pointBefore.m() ) * distance / length );
}
}
}
return closestPoint;
}
double QgsGeometryUtils::distanceToVertex( const QgsAbstractGeometry &geom, QgsVertexId id )
{
double currentDist = 0;
QgsVertexId vertexId;
QgsPoint vertex;
while ( geom.nextVertex( vertexId, vertex ) )
{
if ( vertexId == id )
{
//found target vertex
return currentDist;
}
currentDist += geom.segmentLength( vertexId );
}
//could not find target vertex
return -1;
}
bool QgsGeometryUtils::verticesAtDistance( const QgsAbstractGeometry &geometry, double distance, QgsVertexId &previousVertex, QgsVertexId &nextVertex )
{
double currentDist = 0;
previousVertex = QgsVertexId();
nextVertex = QgsVertexId();
QgsPoint point;
QgsPoint previousPoint;
if ( qgsDoubleNear( distance, 0.0 ) )
{
geometry.nextVertex( previousVertex, point );
nextVertex = previousVertex;
return true;
}
bool first = true;
while ( currentDist < distance && geometry.nextVertex( nextVertex, point ) )
{
if ( !first && nextVertex.part == previousVertex.part && nextVertex.ring == previousVertex.ring )
{
currentDist += std::sqrt( QgsGeometryUtils::sqrDistance2D( previousPoint, point ) );
}
if ( qgsDoubleNear( currentDist, distance ) )
{
// exact hit!
previousVertex = nextVertex;
return true;
}
if ( currentDist > distance )
{
return true;
}
previousVertex = nextVertex;
previousPoint = point;
first = false;
}
//could not find target distance
return false;
}
double QgsGeometryUtils::sqrDistance2D( const QgsPoint &pt1, const QgsPoint &pt2 )
{
return ( pt1.x() - pt2.x() ) * ( pt1.x() - pt2.x() ) + ( pt1.y() - pt2.y() ) * ( pt1.y() - pt2.y() );
}
double QgsGeometryUtils::sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double &minDistX, double &minDistY, double epsilon )
{
minDistX = x1;
minDistY = y1;
double dx = x2 - x1;
double dy = y2 - y1;
if ( !qgsDoubleNear( dx, 0.0 ) || !qgsDoubleNear( dy, 0.0 ) )
{
const double t = ( ( ptX - x1 ) * dx + ( ptY - y1 ) * dy ) / ( dx * dx + dy * dy );
if ( t > 1 )
{
minDistX = x2;
minDistY = y2;
}
else if ( t > 0 )
{
minDistX += dx * t;
minDistY += dy * t;
}
}
dx = ptX - minDistX;
dy = ptY - minDistY;
const double dist = dx * dx + dy * dy;
//prevent rounding errors if the point is directly on the segment
if ( qgsDoubleNear( dist, 0.0, epsilon ) )
{
minDistX = ptX;
minDistY = ptY;
return 0.0;
}
return dist;
}
bool QgsGeometryUtils::lineIntersection( const QgsPoint &p1, QgsVector v1, const QgsPoint &p2, QgsVector v2, QgsPoint &intersection )
{
const double d = v1.y() * v2.x() - v1.x() * v2.y();
if ( qgsDoubleNear( d, 0 ) )
return false;
const double dx = p2.x() - p1.x();
const double dy = p2.y() - p1.y();
const double k = ( dy * v2.x() - dx * v2.y() ) / d;
intersection = QgsPoint( p1.x() + v1.x() * k, p1.y() + v1.y() * k );
// z and m support for intersection point
QgsGeometryUtils::transferFirstZOrMValueToPoint( QgsPointSequence() << p1 << p2, intersection );
return true;
}
bool QgsGeometryUtils::segmentIntersection( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &q1, const QgsPoint &q2, QgsPoint &intersectionPoint, bool &isIntersection, const double tolerance, bool acceptImproperIntersection )
{
isIntersection = false;
QgsVector v( p2.x() - p1.x(), p2.y() - p1.y() );
QgsVector w( q2.x() - q1.x(), q2.y() - q1.y() );
const double vl = v.length();
const double wl = w.length();
if ( qgsDoubleNear( vl, 0.0, tolerance ) || qgsDoubleNear( wl, 0.0, tolerance ) )
{
return false;
}
v = v / vl;
w = w / wl;
if ( !QgsGeometryUtils::lineIntersection( p1, v, q1, w, intersectionPoint ) )
{
return false;
}
isIntersection = true;
if ( acceptImproperIntersection )
{
if ( ( p1 == q1 ) || ( p1 == q2 ) )
{
intersectionPoint = p1;
return true;
}
else if ( ( p2 == q1 ) || ( p2 == q2 ) )
{
intersectionPoint = p2;
return true;
}
double x, y;
if (
// intersectionPoint = p1
qgsDoubleNear( QgsGeometryUtils::sqrDistToLine( p1.x(), p1.y(), q1.x(), q1.y(), q2.x(), q2.y(), x, y, tolerance ), 0.0, tolerance ) ||
// intersectionPoint = p2
qgsDoubleNear( QgsGeometryUtils::sqrDistToLine( p2.x(), p2.y(), q1.x(), q1.y(), q2.x(), q2.y(), x, y, tolerance ), 0.0, tolerance ) ||
// intersectionPoint = q1
qgsDoubleNear( QgsGeometryUtils::sqrDistToLine( q1.x(), q1.y(), p1.x(), p1.y(), p2.x(), p2.y(), x, y, tolerance ), 0.0, tolerance ) ||
// intersectionPoint = q2
qgsDoubleNear( QgsGeometryUtils::sqrDistToLine( q2.x(), q2.y(), p1.x(), p1.y(), p2.x(), p2.y(), x, y, tolerance ), 0.0, tolerance )
)
{
return true;
}
}
const double lambdav = QgsVector( intersectionPoint.x() - p1.x(), intersectionPoint.y() - p1.y() ) * v;
if ( lambdav < 0. + tolerance || lambdav > vl - tolerance )
return false;
const double lambdaw = QgsVector( intersectionPoint.x() - q1.x(), intersectionPoint.y() - q1.y() ) * w;
return !( lambdaw < 0. + tolerance || lambdaw >= wl - tolerance );
}
bool QgsGeometryUtils::lineCircleIntersection( const QgsPointXY ¢er, const double radius,
const QgsPointXY &linePoint1, const QgsPointXY &linePoint2,
QgsPointXY &intersection )
{
// formula taken from http://mathworld.wolfram.com/Circle-LineIntersection.html
const double x1 = linePoint1.x() - center.x();
const double y1 = linePoint1.y() - center.y();
const double x2 = linePoint2.x() - center.x();
const double y2 = linePoint2.y() - center.y();
const double dx = x2 - x1;
const double dy = y2 - y1;
const double dr2 = std::pow( dx, 2 ) + std::pow( dy, 2 );
const double d = x1 * y2 - x2 * y1;
const double disc = std::pow( radius, 2 ) * dr2 - std::pow( d, 2 );
if ( disc < 0 )
{
//no intersection or tangent
return false;
}
else
{
// two solutions
const int sgnDy = dy < 0 ? -1 : 1;
const double sqrDisc = std::sqrt( disc );
const double ax = center.x() + ( d * dy + sgnDy * dx * sqrDisc ) / dr2;
const double ay = center.y() + ( -d * dx + std::fabs( dy ) * sqrDisc ) / dr2;
const QgsPointXY p1( ax, ay );
const double bx = center.x() + ( d * dy - sgnDy * dx * sqrDisc ) / dr2;
const double by = center.y() + ( -d * dx - std::fabs( dy ) * sqrDisc ) / dr2;
const QgsPointXY p2( bx, by );
// snap to nearest intersection
if ( intersection.sqrDist( p1 ) < intersection.sqrDist( p2 ) )
{
intersection.set( p1.x(), p1.y() );
}
else
{
intersection.set( p2.x(), p2.y() );
}
return true;
}
}
// based on public domain work by 3/26/2005 Tim Voght
// see http://paulbourke.net/geometry/circlesphere/tvoght.c
int QgsGeometryUtils::circleCircleIntersections( const QgsPointXY ¢er1, const double r1, const QgsPointXY ¢er2, const double r2, QgsPointXY &intersection1, QgsPointXY &intersection2 )
{
// determine the straight-line distance between the centers
const double d = center1.distance( center2 );
// check for solvability
if ( d > ( r1 + r2 ) )
{
// no solution. circles do not intersect.
return 0;
}
else if ( d < std::fabs( r1 - r2 ) )
{
// no solution. one circle is contained in the other
return 0;
}
else if ( qgsDoubleNear( d, 0 ) && ( qgsDoubleNear( r1, r2 ) ) )
{
// no solutions, the circles coincide
return 0;
}
/* 'point 2' is the point where the line through the circle
* intersection points crosses the line between the circle
* centers.
*/
// Determine the distance from point 0 to point 2.
const double a = ( ( r1 * r1 ) - ( r2 * r2 ) + ( d * d ) ) / ( 2.0 * d ) ;
/* dx and dy are the vertical and horizontal distances between
* the circle centers.
*/
const double dx = center2.x() - center1.x();
const double dy = center2.y() - center1.y();
// Determine the coordinates of point 2.
const double x2 = center1.x() + ( dx * a / d );
const double y2 = center1.y() + ( dy * a / d );
/* Determine the distance from point 2 to either of the
* intersection points.
*/
const double h = std::sqrt( ( r1 * r1 ) - ( a * a ) );
/* Now determine the offsets of the intersection points from
* point 2.
*/
const double rx = dy * ( h / d );
const double ry = dx * ( h / d );
// determine the absolute intersection points
intersection1 = QgsPointXY( x2 + rx, y2 - ry );
intersection2 = QgsPointXY( x2 - rx, y2 + ry );
// see if we have 1 or 2 solutions
if ( qgsDoubleNear( d, r1 + r2 ) )
return 1;
return 2;
}
// Using https://stackoverflow.com/a/1351794/1861260
// and inspired by http://csharphelper.com/blog/2014/11/find-the-tangent-lines-between-a-point-and-a-circle-in-c/
bool QgsGeometryUtils::tangentPointAndCircle( const QgsPointXY ¢er, double radius, const QgsPointXY &p, QgsPointXY &pt1, QgsPointXY &pt2 )
{
// distance from point to center of circle
const double dx = center.x() - p.x();
const double dy = center.y() - p.y();
const double distanceSquared = dx * dx + dy * dy;
const double radiusSquared = radius * radius;
if ( distanceSquared < radiusSquared )
{
// point is inside circle!
return false;
}
// distance from point to tangent point, using pythagoras
const double distanceToTangent = std::sqrt( distanceSquared - radiusSquared );
// tangent points are those where the original circle intersects a circle centered
// on p with radius distanceToTangent
circleCircleIntersections( center, radius, p, distanceToTangent, pt1, pt2 );
return true;
}
// inspired by http://csharphelper.com/blog/2014/12/find-the-tangent-lines-between-two-circles-in-c/
int QgsGeometryUtils::circleCircleOuterTangents( const QgsPointXY ¢er1, double radius1, const QgsPointXY ¢er2, double radius2, QgsPointXY &line1P1, QgsPointXY &line1P2, QgsPointXY &line2P1, QgsPointXY &line2P2 )
{
if ( radius1 > radius2 )
return circleCircleOuterTangents( center2, radius2, center1, radius1, line1P1, line1P2, line2P1, line2P2 );
const double radius2a = radius2 - radius1;
if ( !tangentPointAndCircle( center2, radius2a, center1, line1P2, line2P2 ) )
{
// there are no tangents
return 0;
}
// get the vector perpendicular to the
// first tangent with length radius1
QgsVector v1( -( line1P2.y() - center1.y() ), line1P2.x() - center1.x() );
const double v1Length = v1.length();
v1 = v1 * ( radius1 / v1Length );
// offset the tangent vector's points
line1P1 = center1 + v1;
line1P2 = line1P2 + v1;
// get the vector perpendicular to the
// second tangent with length radius1
QgsVector v2( line2P2.y() - center1.y(), -( line2P2.x() - center1.x() ) );
const double v2Length = v2.length();
v2 = v2 * ( radius1 / v2Length );
// offset the tangent vector's points
line2P1 = center1 + v2;
line2P2 = line2P2 + v2;
return 2;
}
// inspired by http://csharphelper.com/blog/2014/12/find-the-tangent-lines-between-two-circles-in-c/
int QgsGeometryUtils::circleCircleInnerTangents( const QgsPointXY ¢er1, double radius1, const QgsPointXY ¢er2, double radius2, QgsPointXY &line1P1, QgsPointXY &line1P2, QgsPointXY &line2P1, QgsPointXY &line2P2 )
{
if ( radius1 > radius2 )
return circleCircleInnerTangents( center2, radius2, center1, radius1, line1P1, line1P2, line2P1, line2P2 );
// determine the straight-line distance between the centers
const double d = center1.distance( center2 );
const double radius1a = radius1 + radius2;
// check for solvability
if ( d <= radius1a || qgsDoubleNear( d, radius1a ) )
{
// no solution. circles intersect or touch.
return 0;
}
if ( !tangentPointAndCircle( center1, radius1a, center2, line1P2, line2P2 ) )
{
// there are no tangents
return 0;
}
// get the vector perpendicular to the
// first tangent with length radius2
QgsVector v1( ( line1P2.y() - center2.y() ), -( line1P2.x() - center2.x() ) );
const double v1Length = v1.length();
v1 = v1 * ( radius2 / v1Length );
// offset the tangent vector's points
line1P1 = center2 + v1;
line1P2 = line1P2 + v1;
// get the vector perpendicular to the
// second tangent with length radius2
QgsVector v2( -( line2P2.y() - center2.y() ), line2P2.x() - center2.x() );
const double v2Length = v2.length();
v2 = v2 * ( radius2 / v2Length );
// offset the tangent vector's points in opposite direction
line2P1 = center2 + v2;
line2P2 = line2P2 + v2;
return 2;
}
QVector<QgsGeometryUtils::SelfIntersection> QgsGeometryUtils::selfIntersections( const QgsAbstractGeometry *geom, int part, int ring, double tolerance )
{
QVector<SelfIntersection> intersections;
const int n = geom->vertexCount( part, ring );
const bool isClosed = geom->vertexAt( QgsVertexId( part, ring, 0 ) ) == geom->vertexAt( QgsVertexId( part, ring, n - 1 ) );
// Check every pair of segments for intersections
for ( int i = 0, j = 1; j < n; i = j++ )
{
const QgsPoint pi = geom->vertexAt( QgsVertexId( part, ring, i ) );
const QgsPoint pj = geom->vertexAt( QgsVertexId( part, ring, j ) );
if ( QgsGeometryUtils::sqrDistance2D( pi, pj ) < tolerance * tolerance ) continue;
// Don't test neighboring edges
const int start = j + 1;
const int end = i == 0 && isClosed ? n - 1 : n;
for ( int k = start, l = start + 1; l < end; k = l++ )
{
const QgsPoint pk = geom->vertexAt( QgsVertexId( part, ring, k ) );
const QgsPoint pl = geom->vertexAt( QgsVertexId( part, ring, l ) );
QgsPoint inter;
bool intersection = false;
if ( !QgsGeometryUtils::segmentIntersection( pi, pj, pk, pl, inter, intersection, tolerance ) ) continue;
SelfIntersection s;
s.segment1 = i;
s.segment2 = k;
if ( s.segment1 > s.segment2 )
{
std::swap( s.segment1, s.segment2 );
}
s.point = inter;
intersections.append( s );
}
}
return intersections;
}
int QgsGeometryUtils::leftOfLine( const QgsPoint &point, const QgsPoint &p1, const QgsPoint &p2 )
{
return leftOfLine( point.x(), point.y(), p1.x(), p1.y(), p2.x(), p2.y() );
}
int QgsGeometryUtils::leftOfLine( const double x, const double y, const double x1, const double y1, const double x2, const double y2 )
{
const double f1 = x - x1;
const double f2 = y2 - y1;
const double f3 = y - y1;
const double f4 = x2 - x1;
const double test = ( f1 * f2 - f3 * f4 );
// return -1, 0, or 1
return qgsDoubleNear( test, 0.0 ) ? 0 : ( test < 0 ? -1 : 1 );
}
QgsPoint QgsGeometryUtils::pointOnLineWithDistance( const QgsPoint &startPoint, const QgsPoint &directionPoint, double distance )
{
double x, y;
pointOnLineWithDistance( startPoint.x(), startPoint.y(), directionPoint.x(), directionPoint.y(), distance, x, y );
return QgsPoint( x, y );
}
void QgsGeometryUtils::pointOnLineWithDistance( double x1, double y1, double x2, double y2, double distance, double &x, double &y, double *z1, double *z2, double *z, double *m1, double *m2, double *m )
{
const double dx = x2 - x1;
const double dy = y2 - y1;
const double length = std::sqrt( dx * dx + dy * dy );
if ( qgsDoubleNear( length, 0.0 ) )
{
x = x1;
y = y1;
if ( z && z1 )
*z = *z1;
if ( m && m1 )
*m = *m1;
}
else
{
const double scaleFactor = distance / length;
x = x1 + dx * scaleFactor;
y = y1 + dy * scaleFactor;
if ( z && z1 && z2 )
*z = *z1 + ( *z2 - *z1 ) * scaleFactor;
if ( m && m1 && m2 )
*m = *m1 + ( *m2 - *m1 ) * scaleFactor;
}
}
void QgsGeometryUtils::perpendicularOffsetPointAlongSegment( double x1, double y1, double x2, double y2, double proportion, double offset, double *x, double *y )
{
// calculate point along segment
const double mX = x1 + ( x2 - x1 ) * proportion;
const double mY = y1 + ( y2 - y1 ) * proportion;
const double pX = x1 - x2;
const double pY = y1 - y2;
double normalX = -pY;
double normalY = pX; //#spellok
const double normalLength = sqrt( ( normalX * normalX ) + ( normalY * normalY ) ); //#spellok
normalX /= normalLength;
normalY /= normalLength; //#spellok
*x = mX + offset * normalX;
*y = mY + offset * normalY; //#spellok
}
QgsPoint QgsGeometryUtils::interpolatePointOnArc( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double distance )
{
double centerX, centerY, radius;
circleCenterRadius( pt1, pt2, pt3, radius, centerX, centerY );
const double theta = distance / radius; // angle subtended
const double anglePt1 = std::atan2( pt1.y() - centerY, pt1.x() - centerX );
const double anglePt2 = std::atan2( pt2.y() - centerY, pt2.x() - centerX );
const double anglePt3 = std::atan2( pt3.y() - centerY, pt3.x() - centerX );
const bool isClockwise = circleClockwise( anglePt1, anglePt2, anglePt3 );
const double angleDest = anglePt1 + ( isClockwise ? -theta : theta );
const double x = centerX + radius * ( std::cos( angleDest ) );
const double y = centerY + radius * ( std::sin( angleDest ) );
const double z = pt1.is3D() ?
interpolateArcValue( angleDest, anglePt1, anglePt2, anglePt3, pt1.z(), pt2.z(), pt3.z() )
: 0;
const double m = pt1.isMeasure() ?
interpolateArcValue( angleDest, anglePt1, anglePt2, anglePt3, pt1.m(), pt2.m(), pt3.m() )
: 0;
return QgsPoint( pt1.wkbType(), x, y, z, m );
}
double QgsGeometryUtils::ccwAngle( double dy, double dx )
{
const double angle = std::atan2( dy, dx ) * 180 / M_PI;
if ( angle < 0 )
{
return 360 + angle;
}
else if ( angle > 360 )
{
return 360 - angle;
}
return angle;
}
void QgsGeometryUtils::circleCenterRadius( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double &radius, double ¢erX, double ¢erY )
{
double dx21, dy21, dx31, dy31, h21, h31, d;
//closed circle
if ( qgsDoubleNear( pt1.x(), pt3.x() ) && qgsDoubleNear( pt1.y(), pt3.y() ) )
{
centerX = ( pt1.x() + pt2.x() ) / 2.0;
centerY = ( pt1.y() + pt2.y() ) / 2.0;
radius = std::sqrt( std::pow( centerX - pt1.x(), 2.0 ) + std::pow( centerY - pt1.y(), 2.0 ) );
return;
}
// Using Cartesian circumcenter eguations from page https://en.wikipedia.org/wiki/Circumscribed_circle
dx21 = pt2.x() - pt1.x();
dy21 = pt2.y() - pt1.y();
dx31 = pt3.x() - pt1.x();
dy31 = pt3.y() - pt1.y();
h21 = std::pow( dx21, 2.0 ) + std::pow( dy21, 2.0 );
h31 = std::pow( dx31, 2.0 ) + std::pow( dy31, 2.0 );
// 2*Cross product, d<0 means clockwise and d>0 counterclockwise sweeping angle
d = 2 * ( dx21 * dy31 - dx31 * dy21 );
// Check colinearity, Cross product = 0
if ( qgsDoubleNear( std::fabs( d ), 0.0, 0.00000000001 ) )
{
radius = -1.0;
return;
}
// Calculate centroid coordinates and radius
centerX = pt1.x() + ( h21 * dy31 - h31 * dy21 ) / d;
centerY = pt1.y() - ( h21 * dx31 - h31 * dx21 ) / d;
radius = std::sqrt( std::pow( centerX - pt1.x(), 2.0 ) + std::pow( centerY - pt1.y(), 2.0 ) );
}
bool QgsGeometryUtils::circleClockwise( double angle1, double angle2, double angle3 )
{
if ( angle3 >= angle1 )
{
return !( angle2 > angle1 && angle2 < angle3 );
}
else
{
return !( angle2 > angle1 || angle2 < angle3 );
}
}
bool QgsGeometryUtils::circleAngleBetween( double angle, double angle1, double angle2, bool clockwise )
{
if ( clockwise )
{
if ( angle2 < angle1 )
{
return ( angle <= angle1 && angle >= angle2 );
}
else
{
return ( angle <= angle1 || angle >= angle2 );
}
}
else
{
if ( angle2 > angle1 )
{
return ( angle >= angle1 && angle <= angle2 );
}
else
{
return ( angle >= angle1 || angle <= angle2 );
}
}
}
bool QgsGeometryUtils::angleOnCircle( double angle, double angle1, double angle2, double angle3 )
{
const bool clockwise = circleClockwise( angle1, angle2, angle3 );
return circleAngleBetween( angle, angle1, angle3, clockwise );
}
double QgsGeometryUtils::circleLength( double x1, double y1, double x2, double y2, double x3, double y3 )
{
double centerX, centerY, radius;
circleCenterRadius( QgsPoint( x1, y1 ), QgsPoint( x2, y2 ), QgsPoint( x3, y3 ), radius, centerX, centerY );
double length = M_PI / 180.0 * radius * sweepAngle( centerX, centerY, x1, y1, x2, y2, x3, y3 );
if ( length < 0 )
{
length = -length;
}
return length;
}
double QgsGeometryUtils::sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 )
{
const double p1Angle = QgsGeometryUtils::ccwAngle( y1 - centerY, x1 - centerX );
const double p2Angle = QgsGeometryUtils::ccwAngle( y2 - centerY, x2 - centerX );
const double p3Angle = QgsGeometryUtils::ccwAngle( y3 - centerY, x3 - centerX );
if ( p3Angle >= p1Angle )
{
if ( p2Angle > p1Angle && p2Angle < p3Angle )
{
return ( p3Angle - p1Angle );
}
else
{
return ( - ( p1Angle + ( 360 - p3Angle ) ) );
}
}
else
{
if ( p2Angle < p1Angle && p2Angle > p3Angle )
{
return ( -( p1Angle - p3Angle ) );
}
else
{
return ( p3Angle + ( 360 - p1Angle ) );
}
}
}
bool QgsGeometryUtils::segmentMidPoint( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &result, double radius, const QgsPoint &mousePos )
{
const QgsPoint midPoint( ( p1.x() + p2.x() ) / 2.0, ( p1.y() + p2.y() ) / 2.0 );
const double midDist = std::sqrt( sqrDistance2D( p1, midPoint ) );
if ( radius < midDist )
{
return false;
}
const double centerMidDist = std::sqrt( radius * radius - midDist * midDist );
const double dist = radius - centerMidDist;
const double midDx = midPoint.x() - p1.x();
const double midDy = midPoint.y() - p1.y();
//get the four possible midpoints
QVector<QgsPoint> possibleMidPoints;
possibleMidPoints.append( pointOnLineWithDistance( midPoint, QgsPoint( midPoint.x() - midDy, midPoint.y() + midDx ), dist ) );
possibleMidPoints.append( pointOnLineWithDistance( midPoint, QgsPoint( midPoint.x() - midDy, midPoint.y() + midDx ), 2 * radius - dist ) );
possibleMidPoints.append( pointOnLineWithDistance( midPoint, QgsPoint( midPoint.x() + midDy, midPoint.y() - midDx ), dist ) );
possibleMidPoints.append( pointOnLineWithDistance( midPoint, QgsPoint( midPoint.x() + midDy, midPoint.y() - midDx ), 2 * radius - dist ) );
//take the closest one
double minDist = std::numeric_limits<double>::max();
int minDistIndex = -1;
for ( int i = 0; i < possibleMidPoints.size(); ++i )
{
const double currentDist = sqrDistance2D( mousePos, possibleMidPoints.at( i ) );
if ( currentDist < minDist )
{
minDistIndex = i;
minDist = currentDist;
}
}
if ( minDistIndex == -1 )
{
return false;
}
result = possibleMidPoints.at( minDistIndex );
// add z and m support if necessary
QgsGeometryUtils::transferFirstZOrMValueToPoint( QgsPointSequence() << p1 << p2, result );
return true;
}
QgsPoint QgsGeometryUtils::segmentMidPointFromCenter( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint ¢er, const bool useShortestArc )
{
double midPointAngle = averageAngle( lineAngle( center.x(), center.y(), p1.x(), p1.y() ),
lineAngle( center.x(), center.y(), p2.x(), p2.y() ) );
if ( !useShortestArc )
midPointAngle += M_PI;
return center.project( center.distance( p1 ), midPointAngle * 180 / M_PI );
}
double QgsGeometryUtils::circleTangentDirection( const QgsPoint &tangentPoint, const QgsPoint &cp1,
const QgsPoint &cp2, const QgsPoint &cp3 )
{
//calculate circle midpoint
double mX, mY, radius;
circleCenterRadius( cp1, cp2, cp3, radius, mX, mY );
const double p1Angle = QgsGeometryUtils::ccwAngle( cp1.y() - mY, cp1.x() - mX );
const double p2Angle = QgsGeometryUtils::ccwAngle( cp2.y() - mY, cp2.x() - mX );
const double p3Angle = QgsGeometryUtils::ccwAngle( cp3.y() - mY, cp3.x() - mX );
double angle = 0;
if ( circleClockwise( p1Angle, p2Angle, p3Angle ) )
{
angle = lineAngle( tangentPoint.x(), tangentPoint.y(), mX, mY ) - M_PI_2;
}
else
{
angle = lineAngle( mX, mY, tangentPoint.x(), tangentPoint.y() ) - M_PI_2;
}
if ( angle < 0 )
angle += 2 * M_PI;
return angle;
}
// Ported from PostGIS' pt_continues_arc
bool QgsGeometryUtils::pointContinuesArc( const QgsPoint &a1, const QgsPoint &a2, const QgsPoint &a3, const QgsPoint &b, double distanceTolerance, double pointSpacingAngleTolerance )
{
double centerX = 0;
double centerY = 0;
double radius = 0;
circleCenterRadius( a1, a2, a3, radius, centerX, centerY );
// Co-linear a1/a2/a3
if ( radius < 0.0 )
return false;
// distance of candidate point to center of arc a1-a2-a3
const double bDistance = std::sqrt( ( b.x() - centerX ) * ( b.x() - centerX ) +
( b.y() - centerY ) * ( b.y() - centerY ) );
double diff = std::fabs( radius - bDistance );
auto arcAngle = []( const QgsPoint & a, const QgsPoint & b, const QgsPoint & c )->double
{
const double abX = b.x() - a.x();
const double abY = b.y() - a.y();
const double cbX = b.x() - c.x();
const double cbY = b.y() - c.y();
const double dot = ( abX * cbX + abY * cbY ); /* dot product */
const double cross = ( abX * cbY - abY * cbX ); /* cross product */
const double alpha = std::atan2( cross, dot );
return alpha;
};
// Is the point b on the circle?
if ( diff < distanceTolerance )
{
const double angle1 = arcAngle( a1, a2, a3 );
const double angle2 = arcAngle( a2, a3, b );
// Is the sweep angle similar to the previous one?
// We only consider a segment replaceable by an arc if the points within
// it are regularly spaced
diff = std::fabs( angle1 - angle2 );
if ( diff > pointSpacingAngleTolerance )
{
return false;
}
const int a2Side = leftOfLine( a2.x(), a2.y(), a1.x(), a1.y(), a3.x(), a3.y() );
const int bSide = leftOfLine( b.x(), b.y(), a1.x(), a1.y(), a3.x(), a3.y() );
// Is the point b on the same side of a1/a3 as the mid-point a2 is?
// If not, it's in the unbounded part of the circle, so it continues the arc, return true.
if ( bSide != a2Side )
return true;
}
return false;
}
void QgsGeometryUtils::segmentizeArc( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &p3, QgsPointSequence &points, double tolerance, QgsAbstractGeometry::SegmentationToleranceType toleranceType, bool hasZ, bool hasM )
{
bool reversed = false;
const int segSide = segmentSide( p1, p3, p2 );
QgsPoint circlePoint1;
const QgsPoint &circlePoint2 = p2;
QgsPoint circlePoint3;
if ( segSide == -1 )
{
// Reverse !
circlePoint1 = p3;
circlePoint3 = p1;
reversed = true;
}
else
{
circlePoint1 = p1;
circlePoint3 = p3;
}
//adapted code from PostGIS
double radius = 0;
double centerX = 0;
double centerY = 0;
circleCenterRadius( circlePoint1, circlePoint2, circlePoint3, radius, centerX, centerY );
if ( circlePoint1 != circlePoint3 && ( radius < 0 || qgsDoubleNear( segSide, 0.0 ) ) ) //points are colinear
{
points.append( p1 );
points.append( p2 );
points.append( p3 );
return;
}
double increment = tolerance; //one segment per degree
if ( toleranceType == QgsAbstractGeometry::MaximumDifference )
{
// Ensure tolerance is not higher than twice the radius
tolerance = std::min( tolerance, radius * 2 );
const double halfAngle = std::acos( -tolerance / radius + 1 );
increment = 2 * halfAngle;
}
//angles of pt1, pt2, pt3
const double a1 = std::atan2( circlePoint1.y() - centerY, circlePoint1.x() - centerX );