QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsgeometryutils_base.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsgeometryutils_base.h
3 -------------------------------------------------------------------
4Date : 14 september 2023
5Copyright : (C) 2023 by Loïc Bartoletti
6email : loic dot bartoletti at oslandia 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#pragma once
17
18#include "qgis_core.h"
19#include "qgis_sip.h"
20#include "qgsvector3d.h"
21#include "qgsvector.h"
22#include <iterator>
23
30class CORE_EXPORT QgsGeometryUtilsBase
31{
32 public:
33
40 static double sqrDistance3D( double x1, double y1, double z1, double x2, double y2, double z2 ) SIP_HOLDGIL {return ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) + ( z1 - z2 ) * ( z1 - z2 ); }
41
48 static double distance3D( double x1, double y1, double z1, double x2, double y2, double z2 ) SIP_HOLDGIL {return std::sqrt( sqrDistance3D( x1, y1, z1, x2, y2, z2 ) ); }
49
53 static double sqrDistance2D( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL {return ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ); }
54
58 static double distance2D( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL {return std::sqrt( sqrDistance2D( x1, y1, x2, y2 ) ); }
59
63 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 ) SIP_HOLDGIL;
64
72 static int leftOfLine( const double x, const double y, const double x1, const double y1, const double x2, const double y2 ) SIP_HOLDGIL;
73
83 static void pointOnLineWithDistance( double x1, double y1, double x2, double y2, double distance, double &x, double &y,
84 double *z1 = nullptr, double *z2 = nullptr, double *z = nullptr,
85 double *m1 = nullptr, double *m2 = nullptr, double *m = nullptr ) SIP_SKIP;
86
114 static void perpendicularOffsetPointAlongSegment( double x1, double y1, double x2, double y2, double proportion, double offset, double *x SIP_OUT, double *y SIP_OUT );
115
117 static double ccwAngle( double dy, double dx ) SIP_HOLDGIL;
118
120 static void circleCenterRadius( double x1, double y1, double x2, double y2, double x3, double y3, double &radius SIP_OUT,
121 double &centerX SIP_OUT, double &centerY SIP_OUT ) SIP_HOLDGIL;
122
129 static bool circleClockwise( double angle1, double angle2, double angle3 ) SIP_HOLDGIL;
130
132 static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise ) SIP_HOLDGIL;
133
138 static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 ) SIP_HOLDGIL;
139
141 static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 ) SIP_HOLDGIL;
142
144 static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 ) SIP_HOLDGIL;
145
149 static double interpolateArcValue( double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3 ) SIP_HOLDGIL;
150
156 static double normalizedAngle( double angle ) SIP_HOLDGIL;
157
166 static double lineAngle( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL;
167
179 static double angleBetweenThreePoints( double x1, double y1, double x2, double y2,
180 double x3, double y3 ) SIP_HOLDGIL;
181
191 static double linePerpendicularAngle( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL;
192
197 static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 ) SIP_HOLDGIL;
198
205 static double averageAngle( double a1, double a2 ) SIP_HOLDGIL;
206
229 static int closestSideOfRectangle( double right, double bottom, double left, double top, double x, double y );
230
254 static void perpendicularCenterSegment( double centerPointX, double centerPointY,
255 double segmentPoint1x, double segmentPoint1y,
256 double segmentPoint2x, double segmentPoint2y,
257 double &perpendicularSegmentPoint1x SIP_OUT, double &perpendicularSegmentPoint1y SIP_OUT,
258 double &perpendicularSegmentPoint2x SIP_OUT, double &perpendicularSegmentPoint2y SIP_OUT,
259 double segmentLength = 0
260 ) SIP_HOLDGIL;
261
270 static double skewLinesDistance( const QgsVector3D &P1, const QgsVector3D &P12,
271 const QgsVector3D &P2, const QgsVector3D &P22 ) SIP_HOLDGIL;
272
283 static bool skewLinesProjection( const QgsVector3D &P1, const QgsVector3D &P12,
284 const QgsVector3D &P2, const QgsVector3D &P22,
286 double epsilon = 0.0001 ) SIP_HOLDGIL;
287
324 static bool linesIntersection3D( const QgsVector3D &La1, const QgsVector3D &La2,
325 const QgsVector3D &Lb1, const QgsVector3D &Lb2,
326 QgsVector3D &intersection SIP_OUT ) SIP_HOLDGIL;
327
334 static double triangleArea( double aX, double aY, double bX, double bY, double cX, double cY ) SIP_HOLDGIL;
335
344 static double pointFractionAlongLine( double x1, double y1, double x2, double y2, double px, double py );
345
363 static void weightedPointInTriangle( double aX, double aY, double bX, double bY, double cX, double cY,
364 double weightB, double weightC, double &pointX SIP_OUT, double &pointY SIP_OUT ) SIP_HOLDGIL;
365
372 static bool pointsAreCollinear( double x1, double y1, double x2, double y2, double x3, double y3, double epsilon );
373
395 static bool angleBisector( double aX, double aY, double bX, double bY, double cX, double cY, double dX, double dY,
396 double &pointX SIP_OUT, double &pointY SIP_OUT, double &angle SIP_OUT ) SIP_HOLDGIL;
397
414 static bool bisector( double aX, double aY, double bX, double bY, double cX, double cY,
415 double &pointX SIP_OUT, double &pointY SIP_OUT ) SIP_HOLDGIL;
416
417
432 static bool lineIntersection( double p1x, double p1y, QgsVector v1, double p2x, double p2y, QgsVector v2, double &intersectionX SIP_OUT, double &intersectionY SIP_OUT ) SIP_HOLDGIL;
433
452 static bool segmentIntersection( double p1x, double p1y, double p2x, double p2y, double q1x, double q1y, double q2x, double q2y, double &intersectionPointX SIP_OUT, double &intersectionPointY SIP_OUT, bool &isIntersection SIP_OUT, double tolerance = 1e-8, bool acceptImproperIntersection = false ) SIP_HOLDGIL;
453
472 static void project( double aX, double aY, double aZ, double distance, double azimuth, double inclination, double &resultX SIP_OUT, double &resultY SIP_OUT, double &resultZ SIP_OUT ) SIP_HOLDGIL;
473
482 static double azimuth( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL;
483
484#ifndef SIP_RUN
485
505 template<typename T, typename... Args>
506 static bool fuzzyEqual( T epsilon, const Args &... args ) noexcept
507 {
508 static_assert( ( sizeof...( args ) % 2 == 0 || sizeof...( args ) != 0 ), "The number of arguments must be greater than 0 and even" );
509 constexpr size_t numArgs = sizeof...( args );
510 bool result = true;
511 T values[] = {static_cast<T>( args )...};
512
513 for ( size_t i = 0; i < numArgs / 2; ++i )
514 {
515 result = result && qgsNumberNear( values[i], values[i + numArgs / 2], epsilon );
516 }
517
518 return result;
519 }
520
541 template<typename T, typename... Args>
542 static bool fuzzyDistanceEqual( T epsilon, const Args &... args ) noexcept
543 {
544 static_assert( ( sizeof...( args ) % 2 == 0 || sizeof...( args ) >= 4 ), "The number of arguments must be greater than 4 and even" );
545 constexpr size_t numArgs = sizeof...( args );
546 const T squaredEpsilon = epsilon * epsilon;
547 T sum = 0;
548
549 T values[] = {static_cast<T>( args )...};
550
551 for ( size_t i = 0; i < numArgs / 2; ++i )
552 {
553 const T diff = values[i] - values[i + numArgs / 2];
554 sum += diff * diff;
555 }
556
557 return sum < squaredEpsilon;
558 }
559#endif
560
561};
Convenience functions for geometry utils.
static double sqrDistance2D(double x1, double y1, double x2, double y2)
Returns the squared 2D distance between (x1, y1) and (x2, y2).
static double distance2D(double x1, double y1, double x2, double y2)
Returns the 2D distance between (x1, y1) and (x2, y2).
static bool fuzzyEqual(T epsilon, const Args &... args) noexcept
Performs fuzzy comparison between pairs of values within a specified epsilon.
static double distance3D(double x1, double y1, double z1, double x2, double y2, double z2)
Returns the 3D distance between (x1, y1), (x2, y2) and (z2, z2).
static double sqrDistance3D(double x1, double y1, double z1, double x2, double y2, double z2)
Returns the squared 3D distance between (x1, y1), (x2, y2) and (z2, z2).
static bool fuzzyDistanceEqual(T epsilon, const Args &... args) noexcept
Compare equality between multiple pairs of values with a specified epsilon.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition: qgsvector3d.h:31
A class to represent a vector.
Definition: qgsvector.h:30
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:716
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:241
bool qgsNumberNear(T a, T b, T epsilon=std::numeric_limits< T >::epsilon() *4)
Compare two numbers of type T (but allow some difference)
Definition: qgis.h:5189
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_HOLDGIL
Definition: qgis_sip.h:171