ACF $AcfVersion:0$
CSphere.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// ACF includes
6#include <i3d/CVector3d.h>
7
8
9namespace i3d
10{
11
12
13class CLine3d;
14
15
21{
22public:
27 CSphere();
28
34 CSphere(double radius, const CVector3d& center);
35
39 double GetRadius() const;
40
44 void SetRadius(double radius);
45
49 const CVector3d& GetCenter() const;
50
54 void SetCenter(const CVector3d& center);
55
59 double GetVolume() const;
60
64 double GetSurfaceArea() const;
65
71 bool Contains(const CVector3d& point, double tolerance = 0.0) const;
72
76 bool Intersects(const CSphere& other) const;
77
81 bool Intersects(const CLine3d& line) const;
82
87 double GetSignedDistance(const CVector3d& point) const;
88
92 CVector3d GetClosestPoint(const CVector3d& point) const;
93
97 bool Serialize(iser::IArchive& archive);
98
99 bool operator==(const CSphere& sphere) const;
100 bool operator!=(const CSphere& sphere) const;
101
102private:
103 CVector3d m_center;
104 double m_radius;
105};
106
107
108// inline methods
109
111: m_center(0.0, 0.0, 0.0)
112, m_radius(1.0)
113{
114}
115
116
117inline CSphere::CSphere(double radius, const CVector3d& center)
118: m_center(center)
119, m_radius(radius)
120{
121}
122
123
124inline double CSphere::GetRadius() const
125{
126 return m_radius;
127}
128
129
130inline void CSphere::SetRadius(double radius)
131{
132 m_radius = radius;
133}
134
135
136inline const CVector3d& CSphere::GetCenter() const
137{
138 return m_center;
139}
140
141
142inline void CSphere::SetCenter(const CVector3d& center)
143{
144 m_center = center;
145}
146
147
148inline double CSphere::GetVolume() const
149{
150 return (4.0 / 3.0) * I_PI * m_radius * m_radius * m_radius;
151}
152
153
154inline double CSphere::GetSurfaceArea() const
155{
156 return 4.0 * I_PI * m_radius * m_radius;
157}
158
159
160inline bool CSphere::Contains(const CVector3d& point, double tolerance) const
161{
162 double distSq = (point - m_center).GetLength2();
163 double radiusTol = m_radius + tolerance;
164 return distSq <= radiusTol * radiusTol;
165}
166
167
168inline bool CSphere::Intersects(const CSphere& other) const
169{
170 double centerDist = (m_center - other.m_center).GetLength();
171 return centerDist <= (m_radius + other.m_radius);
172}
173
174
175inline double CSphere::GetSignedDistance(const CVector3d& point) const
176{
177 return (point - m_center).GetLength() - m_radius;
178}
179
180
182{
183 CVector3d direction = (point - m_center).GetNormalized();
184 return m_center + direction * m_radius;
185}
186
187
188inline bool CSphere::operator==(const CSphere& sphere) const
189{
190 return m_center == sphere.m_center && qAbs(m_radius - sphere.m_radius) < I_BIG_EPSILON;
191}
192
193
194inline bool CSphere::operator!=(const CSphere& sphere) const
195{
196 return !(*this == sphere);
197}
198
199
200} // namespace i3d
201
202
Definition of a line in 3D space.
Definition CLine3d.h:18
Definition of a sphere in 3D space.
Definition CSphere.h:21
double GetRadius() const
Get radius of the sphere.
Definition CSphere.h:124
bool Contains(const CVector3d &point, double tolerance=0.0) const
Check if point is inside the sphere.
Definition CSphere.h:160
double GetVolume() const
Calculate volume of the sphere.
Definition CSphere.h:148
void SetCenter(const CVector3d &center)
Set center of the sphere.
Definition CSphere.h:142
bool operator==(const CSphere &sphere) const
Definition CSphere.h:188
CVector3d GetClosestPoint(const CVector3d &point) const
Get closest point on sphere surface to given point.
Definition CSphere.h:181
bool operator!=(const CSphere &sphere) const
Definition CSphere.h:194
bool Intersects(const CSphere &other) const
Check if this sphere intersects another sphere.
Definition CSphere.h:168
double GetSignedDistance(const CVector3d &point) const
Get distance from sphere surface to point.
Definition CSphere.h:175
void SetRadius(double radius)
Set radius of the sphere.
Definition CSphere.h:130
bool Serialize(iser::IArchive &archive)
Serialize this sphere to specified archive.
const CVector3d & GetCenter() const
Get center of the sphere.
Definition CSphere.h:136
bool Intersects(const CLine3d &line) const
Check if this sphere intersects a line segment.
double GetSurfaceArea() const
Calculate surface area of the sphere.
Definition CSphere.h:154
CSphere()
Default constructor.
Definition CSphere.h:110
Represents a position or mathematical vector in 3D space with double precision.
Definition CVector3d.h:74
Represents an input/output persistence archive for object serialization.
Definition IArchive.h:164
static const double I_PI
Mathematical constant value PI.
Definition imath.h:19
static const double I_BIG_EPSILON
Definition istd.h:26
Contains the 3D objects.
Definition CAffine3d.h:12