QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsgeometryutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometryutils.h
3  -------------------------------------------------------------------
4 Date : 21 Nov 2014
5 Copyright : (C) 2014 by Marco Hugentobler
6 email : marco.hugentobler at sourcepole dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSGEOMETRYUTILS_H
17 #define QGSGEOMETRYUTILS_H
18 
19 #include <limits>
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
23 #include "qgspoint.h"
24 #include "qgsabstractgeometry.h"
25 #include "qgsvector3d.h"
26 
27 
28 class QgsLineString;
29 
36 class CORE_EXPORT QgsGeometryUtils
37 {
38  public:
39 
44  static QVector<QgsLineString *> extractLineStrings( const QgsAbstractGeometry *geom ) SIP_FACTORY;
45 
50  static QgsPoint closestVertex( const QgsAbstractGeometry &geom, const QgsPoint &pt, QgsVertexId &id SIP_OUT );
51 
57  static QgsPoint closestPoint( const QgsAbstractGeometry &geometry, const QgsPoint &point );
58 
66  static double distanceToVertex( const QgsAbstractGeometry &geom, QgsVertexId id );
67 
79  static bool verticesAtDistance( const QgsAbstractGeometry &geometry,
80  double distance,
81  QgsVertexId &previousVertex SIP_OUT,
82  QgsVertexId &nextVertex SIP_OUT );
83 
87  static double sqrDistance2D( const QgsPoint &pt1, const QgsPoint &pt2 );
88 
92  static double sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double &minDistX SIP_OUT, double &minDistY SIP_OUT, double epsilon );
93 
105  static bool lineIntersection( const QgsPoint &p1, QgsVector v1, const QgsPoint &p2, QgsVector v2, QgsPoint &intersection SIP_OUT );
106 
141  static bool segmentIntersection( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &q1, const QgsPoint &q2, QgsPoint &intersectionPoint SIP_OUT, bool &isIntersection SIP_OUT, double tolerance = 1e-8, bool acceptImproperIntersection = false );
142 
154  static bool lineCircleIntersection( const QgsPointXY &center, double radius,
155  const QgsPointXY &linePoint1, const QgsPointXY &linePoint2,
156  QgsPointXY &intersection SIP_INOUT );
157 
168  static int circleCircleIntersections( QgsPointXY center1, double radius1,
169  QgsPointXY center2, double radius2,
170  QgsPointXY &intersection1 SIP_OUT, QgsPointXY &intersection2 SIP_OUT );
171 
180  static bool tangentPointAndCircle( const QgsPointXY &center, double radius,
181  const QgsPointXY &p, QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT );
182 
200  static int circleCircleOuterTangents(
201  const QgsPointXY &center1, double radius1, const QgsPointXY &center2, double radius2,
202  QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT,
203  QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT );
204 
212  static QgsPoint projectPointOnSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 )
213  {
214  double nx = s2.y() - s1.y();
215  double ny = -( s2.x() - s1.x() );
216  double t = ( p.x() * ny - p.y() * nx - s1.x() * ny + s1.y() * nx ) / ( ( s2.x() - s1.x() ) * ny - ( s2.y() - s1.y() ) * nx );
217  return t < 0. ? s1 : t > 1. ? s2 : QgsPoint( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
218  }
219 
222  {
223  int segment1;
224  int segment2;
226  };
227 
238  static QVector<SelfIntersection> selfIntersections( const QgsAbstractGeometry *geom, int part, int ring, double tolerance ) SIP_SKIP;
239 
247  static int leftOfLine( double x, double y, double x1, double y1, double x2, double y2 );
248 
252  static QgsPoint pointOnLineWithDistance( const QgsPoint &startPoint, const QgsPoint &directionPoint, double distance );
253 
255  static double ccwAngle( double dy, double dx );
256 
258  static void circleCenterRadius( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double &radius SIP_OUT,
259  double &centerX SIP_OUT, double &centerY SIP_OUT );
260 
262  static bool circleClockwise( double angle1, double angle2, double angle3 );
263 
265  static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise );
266 
271  static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 );
272 
274  static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 );
275 
277  static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 );
278 
285  static bool segmentMidPoint( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &result SIP_OUT, double radius, const QgsPoint &mousePos );
286 
298  static QgsPoint segmentMidPointFromCenter( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &center, bool useShortestArc = true );
299 
301  static double circleTangentDirection( const QgsPoint &tangentPoint, const QgsPoint &cp1, const QgsPoint &cp2, const QgsPoint &cp3 );
302 
307  static void segmentizeArc( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &p3,
308  QgsPointSequence SIP_PYALTERNATIVETYPE( QVector<QgsPoint> ) &points SIP_OUT, double tolerance = M_PI_2 / 90,
310  bool hasZ = false, bool hasM = false );
311 
317  static int segmentSide( const QgsPoint &pt1, const QgsPoint &pt3, const QgsPoint &pt2 );
318 
323  static double interpolateArcValue( double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3 );
324 
329  static QgsPointSequence pointsFromWKT( const QString &wktCoordinateList, bool is3D, bool isMeasure ) SIP_SKIP;
330 
335  static void pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequence &points, bool is3D, bool isMeasure ) SIP_SKIP;
336 
341  static QString pointsToWKT( const QgsPointSequence &points, int precision, bool is3D, bool isMeasure ) SIP_SKIP;
342 
347  static QDomElement pointsToGML2( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, const QgsAbstractGeometry::AxisOrder &axisOrder = QgsAbstractGeometry::AxisOrder::XY ) SIP_SKIP;
348 
353  static QDomElement pointsToGML3( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, bool is3D, const QgsAbstractGeometry::AxisOrder &axisOrder = QgsAbstractGeometry::AxisOrder::XY ) SIP_SKIP;
354 
359  static QString pointsToJSON( const QgsPointSequence &points, int precision ) SIP_SKIP;
360 
366  static double normalizedAngle( double angle );
367 
376  static double lineAngle( double x1, double y1, double x2, double y2 );
377 
389  static double angleBetweenThreePoints( double x1, double y1, double x2, double y2,
390  double x3, double y3 );
391 
401  static double linePerpendicularAngle( double x1, double y1, double x2, double y2 );
402 
407  static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 );
408 
415  static double averageAngle( double a1, double a2 );
416 
421  static QPair<QgsWkbTypes::Type, QString> wktReadBlock( const QString &wkt ) SIP_SKIP;
422 
430  static QStringList wktGetChildBlocks( const QString &wkt, const QString &defaultType = QString() ) SIP_SKIP;
431 
453  static QgsPoint midpoint( const QgsPoint &pt1, const QgsPoint &pt2 );
454 
468  static QgsPointXY interpolatePointOnLine( double x1, double y1, double x2, double y2, double fraction );
469 
486  static QgsPoint interpolatePointOnLine( const QgsPoint &p1, const QgsPoint &p2, double fraction );
487 
500  static QgsPointXY interpolatePointOnLineByValue( double x1, double y1, double v1, double x2, double y2, double v2, double value );
501 
509  static double gradient( const QgsPoint &pt1, const QgsPoint &pt2 );
510 
520  static void coefficients( const QgsPoint &pt1, const QgsPoint &pt2,
521  double &a SIP_OUT, double &b SIP_OUT, double &c SIP_OUT );
522 
530  static QgsLineString perpendicularSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 );
531 
532 
541  static double skewLinesDistance( const QgsVector3D &P1, const QgsVector3D &P12,
542  const QgsVector3D &P2, const QgsVector3D &P22 );
543 
554  static bool skewLinesProjection( const QgsVector3D &P1, const QgsVector3D &P12,
555  const QgsVector3D &P2, const QgsVector3D &P22,
556  QgsVector3D &X1 SIP_OUT,
557  double epsilon = 0.0001 );
558 
593  static bool linesIntersection3D( const QgsVector3D &La1, const QgsVector3D &La2,
594  const QgsVector3D &Lb1, const QgsVector3D &Lb2,
595  QgsVector3D &intersection SIP_OUT );
596 
607  static bool setZValueFromPoints( const QgsPointSequence &points, QgsPoint &point );
608 
610  enum ComponentType SIP_SKIP
611  {
614  Part
615  };
616 
618  template<class T> static double closestSegmentFromComponents( T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon ) SIP_SKIP
619  {
620  double minDist = std::numeric_limits<double>::max();
621  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
622  QgsVertexId minDistVertexAfter;
623  int minDistLeftOf = 0;
624  double sqrDist = 0.0;
625  int vertexOffset = 0;
626  int ringOffset = 0;
627  int partOffset = 0;
628 
629  for ( int i = 0; i < container.size(); ++i )
630  {
631  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
632  if ( sqrDist >= 0 && sqrDist < minDist )
633  {
634  minDist = sqrDist;
635  minDistSegmentX = segmentPt.x();
636  minDistSegmentY = segmentPt.y();
637  minDistVertexAfter = vertexAfter;
638  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
639  minDistVertexAfter.part = vertexAfter.part + partOffset;
640  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
641  if ( leftOf )
642  {
643  minDistLeftOf = *leftOf;
644  }
645  }
646 
647  if ( ctype == Vertex )
648  {
649  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
650  vertexOffset += container.at( i )->nCoordinates() - 1;
651  }
652  else if ( ctype == Ring )
653  {
654  ringOffset += 1;
655  }
656  else if ( ctype == Part )
657  {
658  partOffset += 1;
659  }
660  }
661 
662  if ( minDist == std::numeric_limits<double>::max() )
663  return -1; // error: no segments
664 
665  segmentPt.setX( minDistSegmentX );
666  segmentPt.setY( minDistSegmentY );
667  vertexAfter = minDistVertexAfter;
668  if ( leftOf )
669  {
670  *leftOf = minDistLeftOf;
671  }
672  return minDist;
673  }
674 };
675 
676 #endif // QGSGEOMETRYUTILS_H
3 Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double preci...
Definition: qgsvector3d.h:29
double y
Definition: qgspoint.h:42
Maximum angle between generating radii (lines from arc center to output vertices) ...
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool ANALYSIS_EXPORT lineIntersection(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Returns true, if line1 (p1 to p2) and line2 (p3 to p4) intersect. If the lines have an endpoint in co...
Definition: MathUtils.cpp:311
#define SIP_INOUT
Definition: qgis_sip.h:64
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Contains various geometry utility functions.
Utility class for identifying a unique vertex within a geometry.
#define SIP_SKIP
Definition: qgis_sip.h:119
#define SIP_FACTORY
Definition: qgis_sip.h:69
Abstract base class for all geometries.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
AxisOrder
Axis order for GML generation.
A class to represent a vector.
Definition: qgsvector.h:28
QVector< QgsPoint > QgsPointSequence
#define SIP_OUT
Definition: qgis_sip.h:51
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
static QgsPoint projectPointOnSegment(const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2)
Project the point on a segment.
double ANALYSIS_EXPORT leftOf(const QgsPoint &thepoint, const QgsPoint *p1, const QgsPoint *p2)
Returns whether &#39;thepoint&#39; is left or right of the line from &#39;p1&#39; to &#39;p2&#39;. Negativ values mean left a...
Definition: MathUtils.cpp:292
static double closestSegmentFromComponents(T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon)
#define SIP_PYALTERNATIVETYPE(type)
Definition: qgis_sip.h:129
double x
Definition: qgspoint.h:41