QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgssphere.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssphere.cpp
3 --------------
4 begin : July 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
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#include "qgssphere.h"
19#include "qgspoint.h"
20#include "qgscircle.h"
21#include "qgsbox3d.h"
22#include "qgsvector3d.h"
23
24QgsSphere::QgsSphere( double x, double y, double z, double radius )
25 : mCenterX( x )
26 , mCenterY( y )
27 , mCenterZ( z )
28 , mRadius( radius )
29{
30
31}
32
34{
35 return std::isnan( mCenterX ) || std::isnan( mCenterY ) || std::isnan( mCenterZ );
36}
37
39{
40 return qgsDoubleNear( mRadius, 0 );
41}
42
44{
45 return QgsPoint( mCenterX, mCenterY, mCenterZ );
46}
47
49{
50 return QgsVector3D( mCenterX, mCenterY, mCenterZ );
51}
52
53void QgsSphere::setCenter( const QgsPoint &center )
54{
55 mCenterX = center.x();
56 mCenterY = center.y();
57 mCenterZ = center.z();
58}
59
60double QgsSphere::volume() const
61{
62 return 4.0 / 3.0 * M_PI * std::pow( mRadius, 3 );
63}
64
66{
67 return 4.0 * M_PI * std::pow( mRadius, 2 );
68}
69
71{
72 if ( isNull() )
73 return QgsCircle();
74
75 return QgsCircle( QgsPoint( mCenterX, mCenterY ), mRadius );
76}
77
79{
80 if ( isNull() )
81 return QgsBox3D();
82
83 return QgsBox3D( mCenterX - mRadius, mCenterY - mRadius, mCenterZ - mRadius,
84 mCenterX + mRadius, mCenterY + mRadius, mCenterZ + mRadius );
85}
86
A 3-dimensional box composed of x, y, z coordinates.
Definition: qgsbox3d.h:43
Circle geometry type.
Definition: qgscircle.h:43
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Q_GADGET double x
Definition: qgspoint.h:52
double z
Definition: qgspoint.h:54
double y
Definition: qgspoint.h:53
QgsCircle toCircle() const
Converts the sphere to a 2-dimensional circle.
Definition: qgssphere.cpp:70
void setCenter(const QgsPoint &center)
Sets the center point of the sphere.
Definition: qgssphere.cpp:53
double surfaceArea() const
Returns the surface area of the sphere.
Definition: qgssphere.cpp:65
QgsVector3D centerVector() const
Returns the vector to the center of the sphere.
Definition: qgssphere.cpp:48
bool isNull() const
Returns true if the sphere is a null (default constructed) sphere.
Definition: qgssphere.cpp:33
QgsPoint center() const
Returns the center point of the sphere.
Definition: qgssphere.cpp:43
QgsSphere()=default
Constructor for an invalid QgsSphere.
QgsBox3D boundingBox() const
Returns the 3-dimensional bounding box containing the sphere.
Definition: qgssphere.cpp:78
bool isEmpty() const
Returns true if the sphere is considered empty, i.e.
Definition: qgssphere.cpp:38
double volume() const
Returns the volume of the sphere.
Definition: qgssphere.cpp:60
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition: qgsvector3d.h:31
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:5207