QGIS API Documentation 4.1.0-Master (60fea48833c)
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
50 // clang-format off
57 QgsCircle( const QgsPoint &center, double radius, double azimuth = 0 ) SIP_HOLDGIL;
58 // clang-format on
59
70 static QgsCircle from2Points( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL;
71
84 static QgsCircle from3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon = 1E-8 ) SIP_HOLDGIL;
85
93 static QgsCircle fromCenterDiameter( const QgsPoint &center, double diameter, double azimuth = 0 ) SIP_HOLDGIL;
94
95
104 static QgsCircle fromCenterPoint( const QgsPoint &center, const QgsPoint &pt1 ) SIP_HOLDGIL; // cppcheck-suppress duplInheritedMember
105
106
135 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;
136
177 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;
178
189 static QgsCircle fromExtent( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL; // cppcheck-suppress duplInheritedMember
190
201 static QgsCircle minimalCircleFrom3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon = 1E-8 ) SIP_HOLDGIL;
202
217 int intersections( const QgsCircle &other, QgsPoint &intersection1 SIP_OUT, QgsPoint &intersection2 SIP_OUT, bool useZ = false ) const;
218
232 bool tangentToPoint( const QgsPointXY &p, QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const;
233
255 int outerTangents( const QgsCircle &other, QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT, QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) const;
256
278 int innerTangents( const QgsCircle &other, QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT, QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) const;
279
280 double area() const override SIP_HOLDGIL;
281 double perimeter() const override SIP_HOLDGIL;
282
283 //inherited
284 // void setAzimuth(const double azimuth);
285 // double azimuth() const {return mAzimuth; }
286
287
293 void setSemiMajorAxis( double semiMajorAxis ) override SIP_HOLDGIL;
294
300 void setSemiMinorAxis( double semiMinorAxis ) override SIP_HOLDGIL;
301
303 double radius() const SIP_HOLDGIL { return mSemiMajorAxis; }
306 {
307 mSemiMajorAxis = std::fabs( radius );
309 }
310
317 QVector<QgsPoint> northQuadrant() const SIP_FACTORY;
318
323 QgsCircularString *toCircularString( bool oriented = false ) const;
324
326 bool contains( const QgsPoint &point, double epsilon = 1E-8 ) const;
327
328 QgsRectangle boundingBox() const override;
329
330 QString toString( int pointPrecision = 17, int radiusPrecision = 17, int azimuthPrecision = 2 ) const override;
331
341 QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;
342
360 QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;
361
378 static int calculateSegments( double radius, double parameter, int minSegments, Qgis::SegmentCalculationMethod method );
379
380// clang-format off
381#ifdef SIP_RUN
382 SIP_PYOBJECT __repr__();
383 % MethodCode
384 QString str
385 = u"<QgsCircle: %1>"_s.arg( sipCpp->toString() );
386 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
387 % End
388#endif
389
390 private:
391// clang-format on
392
419 static int calculateSegmentsStandard( double radius, double tolerance, int minSegments )
420 {
421 if ( tolerance >= radius )
422 {
423 return minSegments;
424 }
425
426 // Using the sagitta formula: s = r(1 - cos(θ/2))
427 const double halfAngle = std::acos( 1.0 - tolerance / radius );
428 const int segments = std::ceil( M_PI / halfAngle );
429
430 return std::max( segments, minSegments );
431 }
432
466 static int calculateSegmentsAdaptive( double radius, double tolerance, int minSegments )
467 {
468 // Compute adaptive tolerance that varies with radius
469 const double adaptiveTolerance = tolerance * std::sqrt( radius ) / std::log10( radius + 1.0 );
470
471 if ( adaptiveTolerance >= radius )
472 {
473 return minSegments;
474 }
475
476 const double halfAngle = std::acos( 1.0 - adaptiveTolerance / radius );
477 const int segments = std::ceil( M_PI / halfAngle );
478
479 return std::max( segments, minSegments );
480 }
481
523 static int calculateSegmentsByAreaError( double radius, double baseTolerance, int minSegments )
524 {
525 Q_UNUSED( radius );
526 // Convert tolerance from percentage to decimal
527 const double decimalTolerance = baseTolerance / 100.0;
528
529 // Avoid division by zero or extremely small tolerance
530 const double tolerance = std::max( decimalTolerance, 1.0e-8 );
531
532 // Calculate required segments using the area error formula
533 const double requiredSegments = M_PI * std::sqrt( 2.0 / ( 3.0 * tolerance ) );
534
535 return std::max( static_cast<int>( std::ceil( requiredSegments ) ), minSegments );
536 }
537
558 static int calculateSegmentsByConstant( double radius, double constant, int minSegments )
559 {
560 return std::max( minSegments, static_cast<int>( std::ceil( constant * radius ) ) );
561 }
562};
563
564#endif // QGSCIRCLE_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
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:39
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:303
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:305
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:84
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:256
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:257
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:57
#define SIP_HOLDGIL
Definition qgis_sip.h:178
#define SIP_FACTORY
Definition qgis_sip.h:83