QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgscircle.h
Go to the documentation of this file.
1/***************************************************************************
2 qgscircle.h
3 --------------
4 begin : March 2017
5 copyright : (C) 2017 by Loîc Bartoletti
6 email : lbartoletti at tuxfamily dot org
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#ifndef QGSCIRCLE_H
19#define QGSCIRCLE_H
20
21#include <cmath>
22
23#include "qgis_core.h"
24#include "qgscircularstring.h"
25#include "qgsellipse.h"
26#include "qgspolygon.h"
27#include "qgsrectangle.h"
28
29#include <QString>
30
31class QgsPoint;
32
41
42
43class CORE_EXPORT QgsCircle : public QgsEllipse
44{
45 public:
46 QgsCircle();
47
54 QgsCircle( const QgsPoint &center, double radius, double azimuth = 0 ) SIP_HOLDGIL;
55
66 static QgsCircle from2Points( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL;
67
80 static QgsCircle from3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon = 1E-8 ) SIP_HOLDGIL;
81
89 static QgsCircle fromCenterDiameter( const QgsPoint &center, double diameter, double azimuth = 0 ) SIP_HOLDGIL;
90
91
100 static QgsCircle fromCenterPoint( const QgsPoint &center, const QgsPoint &pt1 ) SIP_HOLDGIL; // cppcheck-suppress duplInheritedMember
101
102
131 static QgsCircle from3Tangents( const QgsPoint &pt1_tg1, const QgsPoint &pt2_tg1, const QgsPoint &pt1_tg2, const QgsPoint &pt2_tg2, const QgsPoint &pt1_tg3, const QgsPoint &pt2_tg3, double epsilon = 1E-8, const QgsPoint &pos = QgsPoint() ) SIP_HOLDGIL;
132
173 static QVector<QgsCircle> from3TangentsMulti( const QgsPoint &pt1_tg1, const QgsPoint &pt2_tg1, const QgsPoint &pt1_tg2, const QgsPoint &pt2_tg2, const QgsPoint &pt1_tg3, const QgsPoint &pt2_tg3, double epsilon = 1E-8, const QgsPoint &pos = QgsPoint() ) SIP_HOLDGIL;
174
185 static QgsCircle fromExtent( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL; // cppcheck-suppress duplInheritedMember
186
197 static QgsCircle minimalCircleFrom3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon = 1E-8 ) SIP_HOLDGIL;
198
213 int intersections( const QgsCircle &other, QgsPoint &intersection1 SIP_OUT, QgsPoint &intersection2 SIP_OUT, bool useZ = false ) const;
214
228 bool tangentToPoint( const QgsPointXY &p, QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const;
229
251 int outerTangents( const QgsCircle &other, QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT, QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) const;
252
274 int innerTangents( const QgsCircle &other, QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT, QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) const;
275
276 double area() const override SIP_HOLDGIL;
277 double perimeter() const override SIP_HOLDGIL;
278
279 //inherited
280 // void setAzimuth(const double azimuth);
281 // double azimuth() const {return mAzimuth; }
282
283
289 void setSemiMajorAxis( double semiMajorAxis ) override SIP_HOLDGIL;
290
296 void setSemiMinorAxis( double semiMinorAxis ) override SIP_HOLDGIL;
297
299 double radius() const SIP_HOLDGIL { return mSemiMajorAxis; }
302 {
303 mSemiMajorAxis = std::fabs( radius );
305 }
306
313 QVector<QgsPoint> northQuadrant() const SIP_FACTORY;
314
319 QgsCircularString *toCircularString( bool oriented = false ) const;
320
322 bool contains( const QgsPoint &point, double epsilon = 1E-8 ) const;
323
324 QgsRectangle boundingBox() const override;
325
326 QString toString( int pointPrecision = 17, int radiusPrecision = 17, int azimuthPrecision = 2 ) const override;
327
337 QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;
338
356 QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;
357
374 static int calculateSegments( double radius, double parameter, int minSegments, Qgis::SegmentCalculationMethod method );
375
376
377#ifdef SIP_RUN
378 SIP_PYOBJECT __repr__();
379 % MethodCode
380 QString str
381 = QStringLiteral( "<QgsCircle: %1>" ).arg( sipCpp->toString() );
382 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
383 % End
384#endif
385
386 private :
387
414 static int calculateSegmentsStandard( double radius, double tolerance, int minSegments )
415 {
416 if ( tolerance >= radius )
417 {
418 return minSegments;
419 }
420
421 // Using the sagitta formula: s = r(1 - cos(θ/2))
422 const double halfAngle = std::acos( 1.0 - tolerance / radius );
423 const int segments = std::ceil( M_PI / halfAngle );
424
425 return std::max( segments, minSegments );
426 }
427
461 static int calculateSegmentsAdaptive( double radius, double tolerance, int minSegments )
462 {
463 // Compute adaptive tolerance that varies with radius
464 const double adaptiveTolerance = tolerance * std::sqrt( radius ) / std::log10( radius + 1.0 );
465
466 if ( adaptiveTolerance >= radius )
467 {
468 return minSegments;
469 }
470
471 const double halfAngle = std::acos( 1.0 - adaptiveTolerance / radius );
472 const int segments = std::ceil( M_PI / halfAngle );
473
474 return std::max( segments, minSegments );
475 }
476
518 static int calculateSegmentsByAreaError( double radius, double baseTolerance, int minSegments )
519 {
520 Q_UNUSED( radius );
521 // Convert tolerance from percentage to decimal
522 const double decimalTolerance = baseTolerance / 100.0;
523
524 // Avoid division by zero or extremely small tolerance
525 const double tolerance = std::max( decimalTolerance, 1.0e-8 );
526
527 // Calculate required segments using the area error formula
528 const double requiredSegments = M_PI * std::sqrt( 2.0 / ( 3.0 * tolerance ) );
529
530 return std::max( static_cast<int>( std::ceil( requiredSegments ) ), minSegments );
531 }
532
553 static int calculateSegmentsByConstant( double radius, double constant, int minSegments )
554 {
555 return std::max( minSegments, static_cast<int>( std::ceil( constant * radius ) ) );
556 }
557};
558
559#endif // QGSCIRCLE_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:56
Abstract base class for all geometries.
static QgsCircle from2Points(const QgsPoint &pt1, const QgsPoint &pt2)
Constructs a circle by 2 points on the circle.
Definition qgscircle.cpp:37
int intersections(const QgsCircle &other, QgsPoint &intersection1, QgsPoint &intersection2, bool useZ=false) const
Calculates the intersections points between this circle and an other circle.
double radius() const
Returns the radius of the circle.
Definition qgscircle.h:299
int innerTangents(const QgsCircle &other, QgsPointXY &line1P1, QgsPointXY &line1P2, QgsPointXY &line2P1, QgsPointXY &line2P2) const
Calculates the inner tangent points between this circle and an other circle.
int outerTangents(const QgsCircle &other, QgsPointXY &line1P1, QgsPointXY &line1P2, QgsPointXY &line2P1, QgsPointXY &line2P2) const
Calculates the outer tangent points between this circle and an other circle.
void setRadius(double radius)
Sets the radius of the circle.
Definition qgscircle.h:301
static QgsCircle from3Tangents(const QgsPoint &pt1_tg1, const QgsPoint &pt2_tg1, const QgsPoint &pt1_tg2, const QgsPoint &pt2_tg2, const QgsPoint &pt1_tg3, const QgsPoint &pt2_tg3, double epsilon=1E-8, const QgsPoint &pos=QgsPoint())
Constructs a circle by 3 tangents on the circle (aka inscribed circle of a triangle).
static QVector< QgsCircle > from3TangentsMulti(const QgsPoint &pt1_tg1, const QgsPoint &pt2_tg1, const QgsPoint &pt1_tg2, const QgsPoint &pt2_tg2, const QgsPoint &pt1_tg3, const QgsPoint &pt2_tg3, double epsilon=1E-8, const QgsPoint &pos=QgsPoint())
Returns an array of circle constructed by 3 tangents on the circle (aka inscribed circle of a triangl...
static QgsCircle fromCenterDiameter(const QgsPoint &center, double diameter, double azimuth=0)
Constructs a circle by a center point and a diameter.
bool tangentToPoint(const QgsPointXY &p, QgsPointXY &pt1, QgsPointXY &pt2) const
Calculates the tangent points between this circle and the point p.
static QgsCircle from3Points(const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon=1E-8)
Constructs a circle by 3 points on the circle.
Definition qgscircle.cpp:82
static QgsCircle minimalCircleFrom3Points(const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon=1E-8)
Constructs the smallest circle from 3 points.
Circular string geometry type.
QgsPoint center() const
Returns the center point.
Definition qgsellipse.h:120
double semiMajorAxis() const
Returns the semi-major axis.
Definition qgsellipse.h:126
static QgsEllipse fromCenterPoint(const QgsPoint &ptc, const QgsPoint &pt1)
Constructs an ellipse by a center point and a another point.
double mSemiMajorAxis
Definition qgsellipse.h:252
double azimuth() const
Returns the azimuth.
Definition qgsellipse.h:138
QgsEllipse()=default
Constructor for QgsEllipse.
virtual double perimeter() const
The circumference of the ellipse using first approximation of Ramanujan.
virtual void setSemiMinorAxis(double semiMinorAxis)
Sets the semi-minor axis.
static QgsEllipse fromExtent(const QgsPoint &pt1, const QgsPoint &pt2)
Constructs an ellipse by an extent (aka bounding box / QgsRectangle).
virtual double area() const
The area of the ellipse.
double mSemiMinorAxis
Definition qgsellipse.h:253
double semiMinorAxis() const
Returns the semi-minor axis.
Definition qgsellipse.h:132
virtual void setSemiMajorAxis(double semiMajorAxis)
Sets the semi-major axis.
Represents a 2D point.
Definition qgspointxy.h:60
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
A rectangle specified with double values.
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_HOLDGIL
Definition qgis_sip.h:179
#define SIP_FACTORY
Definition qgis_sip.h:84