QGIS API Documentation 3.99.0-Master (d270888f95f)
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 : lituus at free dot fr
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
31using namespace Qt::StringLiterals;
32
33class QgsPoint;
34
43
44
45class CORE_EXPORT QgsCircle : public QgsEllipse
46{
47 public:
48 QgsCircle();
49
56 QgsCircle( const QgsPoint &center, double radius, double azimuth = 0 ) SIP_HOLDGIL;
57
68 static QgsCircle from2Points( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL;
69
82 static QgsCircle from3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon = 1E-8 ) SIP_HOLDGIL;
83
91 static QgsCircle fromCenterDiameter( const QgsPoint &center, double diameter, double azimuth = 0 ) SIP_HOLDGIL;
92
93
102 static QgsCircle fromCenterPoint( const QgsPoint &center, const QgsPoint &pt1 ) SIP_HOLDGIL; // cppcheck-suppress duplInheritedMember
103
104
133 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;
134
175 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;
176
187 static QgsCircle fromExtent( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL; // cppcheck-suppress duplInheritedMember
188
199 static QgsCircle minimalCircleFrom3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon = 1E-8 ) SIP_HOLDGIL;
200
215 int intersections( const QgsCircle &other, QgsPoint &intersection1 SIP_OUT, QgsPoint &intersection2 SIP_OUT, bool useZ = false ) const;
216
230 bool tangentToPoint( const QgsPointXY &p, QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const;
231
253 int outerTangents( const QgsCircle &other, QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT, QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) const;
254
276 int innerTangents( const QgsCircle &other, QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT, QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) const;
277
278 double area() const override SIP_HOLDGIL;
279 double perimeter() const override SIP_HOLDGIL;
280
281 //inherited
282 // void setAzimuth(const double azimuth);
283 // double azimuth() const {return mAzimuth; }
284
285
291 void setSemiMajorAxis( double semiMajorAxis ) override SIP_HOLDGIL;
292
298 void setSemiMinorAxis( double semiMinorAxis ) override SIP_HOLDGIL;
299
301 double radius() const SIP_HOLDGIL { return mSemiMajorAxis; }
304 {
305 mSemiMajorAxis = std::fabs( radius );
307 }
308
315 QVector<QgsPoint> northQuadrant() const SIP_FACTORY;
316
321 QgsCircularString *toCircularString( bool oriented = false ) const;
322
324 bool contains( const QgsPoint &point, double epsilon = 1E-8 ) const;
325
326 QgsRectangle boundingBox() const override;
327
328 QString toString( int pointPrecision = 17, int radiusPrecision = 17, int azimuthPrecision = 2 ) const override;
329
339 QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;
340
358 QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;
359
376 static int calculateSegments( double radius, double parameter, int minSegments, Qgis::SegmentCalculationMethod method );
377
378
379#ifdef SIP_RUN
380 SIP_PYOBJECT __repr__();
381 % MethodCode
382 QString str
383 = u"<QgsCircle: %1>"_s.arg( sipCpp->toString() );
384 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
385 % End
386#endif
387
388 private :
389
416 static int calculateSegmentsStandard( double radius, double tolerance, int minSegments )
417 {
418 if ( tolerance >= radius )
419 {
420 return minSegments;
421 }
422
423 // Using the sagitta formula: s = r(1 - cos(θ/2))
424 const double halfAngle = std::acos( 1.0 - tolerance / radius );
425 const int segments = std::ceil( M_PI / halfAngle );
426
427 return std::max( segments, minSegments );
428 }
429
463 static int calculateSegmentsAdaptive( double radius, double tolerance, int minSegments )
464 {
465 // Compute adaptive tolerance that varies with radius
466 const double adaptiveTolerance = tolerance * std::sqrt( radius ) / std::log10( radius + 1.0 );
467
468 if ( adaptiveTolerance >= radius )
469 {
470 return minSegments;
471 }
472
473 const double halfAngle = std::acos( 1.0 - adaptiveTolerance / radius );
474 const int segments = std::ceil( M_PI / halfAngle );
475
476 return std::max( segments, minSegments );
477 }
478
520 static int calculateSegmentsByAreaError( double radius, double baseTolerance, int minSegments )
521 {
522 Q_UNUSED( radius );
523 // Convert tolerance from percentage to decimal
524 const double decimalTolerance = baseTolerance / 100.0;
525
526 // Avoid division by zero or extremely small tolerance
527 const double tolerance = std::max( decimalTolerance, 1.0e-8 );
528
529 // Calculate required segments using the area error formula
530 const double requiredSegments = M_PI * std::sqrt( 2.0 / ( 3.0 * tolerance ) );
531
532 return std::max( static_cast<int>( std::ceil( requiredSegments ) ), minSegments );
533 }
534
555 static int calculateSegmentsByConstant( double radius, double constant, int minSegments )
556 {
557 return std::max( minSegments, static_cast<int>( std::ceil( constant * radius ) ) );
558 }
559};
560
561#endif // QGSCIRCLE_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
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:41
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:301
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:303
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:86
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:122
double semiMajorAxis() const
Returns the semi-major axis.
Definition qgsellipse.h:128
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:254
double azimuth() const
Returns the azimuth.
Definition qgsellipse.h:140
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:255
double semiMinorAxis() const
Returns the semi-minor axis.
Definition qgsellipse.h:134
virtual void setSemiMajorAxis(double semiMajorAxis)
Sets the semi-major axis.
Represents a 2D point.
Definition qgspointxy.h:62
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
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