QGIS API Documentation 3.41.0-Master (652554233d6)
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 <QString>
22
23#include <cmath>
24
25#include "qgis_core.h"
26#include "qgsellipse.h"
27#include "qgspolygon.h"
28#include "qgsrectangle.h"
29#include "qgscircularstring.h"
30
31
32class QgsPoint;
33
44class CORE_EXPORT QgsCircle : public QgsEllipse
45{
46 public:
47 QgsCircle();
48
55 QgsCircle( const QgsPoint &center, double radius, double azimuth = 0 ) SIP_HOLDGIL;
56
67 static QgsCircle from2Points( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL;
68
81 static QgsCircle from3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon = 1E-8 ) SIP_HOLDGIL;
82
90 static QgsCircle fromCenterDiameter( const QgsPoint &center, double diameter, double azimuth = 0 ) SIP_HOLDGIL;
91
92
101 static QgsCircle fromCenterPoint( const QgsPoint &center, const QgsPoint &pt1 ) SIP_HOLDGIL; // cppcheck-suppress duplInheritedMember
102
103
132 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;
133
174 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;
175
186 static QgsCircle fromExtent( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL; // cppcheck-suppress duplInheritedMember
187
198 static QgsCircle minimalCircleFrom3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon = 1E-8 ) SIP_HOLDGIL;
199
214 int intersections( const QgsCircle &other, QgsPoint &intersection1 SIP_OUT, QgsPoint &intersection2 SIP_OUT, bool useZ = false ) const;
215
229 bool tangentToPoint( const QgsPointXY &p, QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const;
230
252 int outerTangents( const QgsCircle &other, QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT, QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) const;
253
275 int innerTangents( const QgsCircle &other, QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT, QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) const;
276
277 double area() const override SIP_HOLDGIL;
278 double perimeter() const override SIP_HOLDGIL;
279
280 //inherited
281 // void setAzimuth(const double azimuth);
282 // double azimuth() const {return mAzimuth; }
283
284
290 void setSemiMajorAxis( double semiMajorAxis ) override SIP_HOLDGIL;
291
297 void setSemiMinorAxis( double semiMinorAxis ) override SIP_HOLDGIL;
298
300 double radius() const SIP_HOLDGIL { return mSemiMajorAxis; }
302 void setRadius( double radius ) SIP_HOLDGIL
303 {
304 mSemiMajorAxis = std::fabs( radius );
305 mSemiMinorAxis = mSemiMajorAxis;
306 }
307
314 QVector<QgsPoint> northQuadrant() const SIP_FACTORY;
315
320 QgsCircularString *toCircularString( bool oriented = false ) const;
321
323 bool contains( const QgsPoint &point, double epsilon = 1E-8 ) const;
324
325 QgsRectangle boundingBox() const override;
326
327 QString toString( int pointPrecision = 17, int radiusPrecision = 17, int azimuthPrecision = 2 ) const override;
328
338 QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;
339
357 QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const;
358
375 static int calculateSegments( double radius, double parameter, int minSegments, Qgis::SegmentCalculationMethod method );
376
377
378#ifdef SIP_RUN
379 SIP_PYOBJECT __repr__();
380 % MethodCode
381 QString str
382 = QStringLiteral( "<QgsCircle: %1>" ).arg( sipCpp->toString() );
383 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
384 % End
385#endif
386
387 private :
388
415 static int calculateSegmentsStandard( double radius, double tolerance, int minSegments )
416 {
417 if ( tolerance >= radius )
418 {
419 return minSegments;
420 }
421
422 // Using the sagitta formula: s = r(1 - cos(θ/2))
423 const double halfAngle = std::acos( 1.0 - tolerance / radius );
424 const int segments = std::ceil( M_PI / halfAngle );
425
426 return std::max( segments, minSegments );
427 }
428
462 static int calculateSegmentsAdaptive( double radius, double tolerance, int minSegments )
463 {
464 // Compute adaptive tolerance that varies with radius
465 const double adaptiveTolerance = tolerance * std::sqrt( radius ) / std::log10( radius + 1.0 );
466
467 if ( adaptiveTolerance >= radius )
468 {
469 return minSegments;
470 }
471
472 const double halfAngle = std::acos( 1.0 - adaptiveTolerance / radius );
473 const int segments = std::ceil( M_PI / halfAngle );
474
475 return std::max( segments, minSegments );
476 }
477
519 static int calculateSegmentsByAreaError( double radius, double baseTolerance, int minSegments )
520 {
521 Q_UNUSED( radius );
522 // Convert tolerance from percentage to decimal
523 const double decimalTolerance = baseTolerance / 100.0;
524
525 // Avoid division by zero or extremely small tolerance
526 const double tolerance = std::max( decimalTolerance, 1.0e-8 );
527
528 // Calculate required segments using the area error formula
529 const double requiredSegments = M_PI * std::sqrt( 2.0 / ( 3.0 * tolerance ) );
530
531 return std::max( static_cast<int>( std::ceil( requiredSegments ) ), minSegments );
532 }
533
554 static int calculateSegmentsByConstant( double radius, double constant, int minSegments )
555 {
556 return std::max( minSegments, static_cast<int>( std::ceil( constant * radius ) ) );
557 }
558};
559
560#endif // QGSCIRCLE_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
Abstract base class for all geometries.
Circle geometry type.
Definition qgscircle.h:45
double radius() const
Returns the radius of the circle.
Definition qgscircle.h:300
void setRadius(double radius)
Sets the radius of the circle.
Definition qgscircle.h:302
Circular string geometry type.
Ellipse geometry type.
Definition qgsellipse.h:39
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
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.
virtual void setSemiMajorAxis(double semiMajorAxis)
Sets the semi-major axis.
A class to represent 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:171
#define SIP_FACTORY
Definition qgis_sip.h:76
int precision