ACF $AcfVersion:0$
CBox3d.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
17class CBox3d
18{
19public:
24 CBox3d();
25
31 CBox3d(const CVector3d& min, const CVector3d& max);
32
36 bool IsEmpty() const;
37
41 const CVector3d& GetMin() const;
42
46 void SetMin(const CVector3d& min);
47
51 const CVector3d& GetMax() const;
52
56 void SetMax(const CVector3d& max);
57
61 CVector3d GetCenter() const;
62
66 CVector3d GetSize() const;
67
71 CVector3d GetExtent() const;
72
76 double GetVolume() const;
77
81 double GetSurfaceArea() const;
82
88 bool Contains(const CVector3d& point, double tolerance = 0.0) const;
89
93 bool Intersects(const CBox3d& other) const;
94
98 void Include(const CVector3d& point);
99
103 void Include(const CBox3d& other);
104
109
113 void Reset();
114
118 bool Serialize(iser::IArchive& archive);
119
120 bool operator==(const CBox3d& box) const;
121 bool operator!=(const CBox3d& box) const;
122
123private:
124 CVector3d m_min;
125 CVector3d m_max;
126};
127
128
129// inline methods
130
132{
133 Reset();
134}
135
136
137inline CBox3d::CBox3d(const CVector3d& min, const CVector3d& max)
138: m_min(min)
139, m_max(max)
140{
141}
142
143
144inline bool CBox3d::IsEmpty() const
145{
146 return m_min.GetX() > m_max.GetX() ||
147 m_min.GetY() > m_max.GetY() ||
148 m_min.GetZ() > m_max.GetZ();
149}
150
151
152inline const CVector3d& CBox3d::GetMin() const
153{
154 return m_min;
155}
156
157
158inline void CBox3d::SetMin(const CVector3d& min)
159{
160 m_min = min;
161}
162
163
164inline const CVector3d& CBox3d::GetMax() const
165{
166 return m_max;
167}
168
169
170inline void CBox3d::SetMax(const CVector3d& max)
171{
172 m_max = max;
173}
174
175
177{
178 return (m_min + m_max) * 0.5;
179}
180
181
183{
184 if (IsEmpty()){
185 return CVector3d(0.0, 0.0, 0.0);
186 }
187 return m_max - m_min;
188}
189
190
192{
193 return GetSize() * 0.5;
194}
195
196
197inline double CBox3d::GetVolume() const
198{
199 if (IsEmpty()){
200 return 0.0;
201 }
202 CVector3d size = GetSize();
203 return size.GetX() * size.GetY() * size.GetZ();
204}
205
206
207inline double CBox3d::GetSurfaceArea() const
208{
209 if (IsEmpty()){
210 return 0.0;
211 }
212 CVector3d size = GetSize();
213 return 2.0 * (size.GetX() * size.GetY() +
214 size.GetX() * size.GetZ() +
215 size.GetY() * size.GetZ());
216}
217
218
219inline bool CBox3d::Contains(const CVector3d& point, double tolerance) const
220{
221 if (IsEmpty()){
222 return false;
223 }
224
225 return point.GetX() >= m_min.GetX() - tolerance &&
226 point.GetX() <= m_max.GetX() + tolerance &&
227 point.GetY() >= m_min.GetY() - tolerance &&
228 point.GetY() <= m_max.GetY() + tolerance &&
229 point.GetZ() >= m_min.GetZ() - tolerance &&
230 point.GetZ() <= m_max.GetZ() + tolerance;
231}
232
233
234inline bool CBox3d::Intersects(const CBox3d& other) const
235{
236 if (IsEmpty() || other.IsEmpty()){
237 return false;
238 }
239
240 return m_min.GetX() <= other.m_max.GetX() &&
241 m_max.GetX() >= other.m_min.GetX() &&
242 m_min.GetY() <= other.m_max.GetY() &&
243 m_max.GetY() >= other.m_min.GetY() &&
244 m_min.GetZ() <= other.m_max.GetZ() &&
245 m_max.GetZ() >= other.m_min.GetZ();
246}
247
248
249inline void CBox3d::Include(const CVector3d& point)
250{
251 if (IsEmpty()){
252 m_min = point;
253 m_max = point;
254 }
255 else{
256 m_min.SetX(qMin(m_min.GetX(), point.GetX()));
257 m_min.SetY(qMin(m_min.GetY(), point.GetY()));
258 m_min.SetZ(qMin(m_min.GetZ(), point.GetZ()));
259
260 m_max.SetX(qMax(m_max.GetX(), point.GetX()));
261 m_max.SetY(qMax(m_max.GetY(), point.GetY()));
262 m_max.SetZ(qMax(m_max.GetZ(), point.GetZ()));
263 }
264}
265
266
267inline void CBox3d::Include(const CBox3d& other)
268{
269 if (!other.IsEmpty()){
270 Include(other.m_min);
271 Include(other.m_max);
272 }
273}
274
275
276inline void CBox3d::Reset()
277{
278 // Set to invalid state (min > max)
279 m_min = CVector3d(qInf(), qInf(), qInf());
280 m_max = CVector3d(-qInf(), -qInf(), -qInf());
281}
282
283
284inline bool CBox3d::operator==(const CBox3d& box) const
285{
286 return m_min == box.m_min && m_max == box.m_max;
287}
288
289
290inline bool CBox3d::operator!=(const CBox3d& box) const
291{
292 return !(*this == box);
293}
294
295
296} // namespace i3d
297
298
Definition of an axis-aligned bounding box (AABB) in 3D space.
Definition CBox3d.h:18
CBox3d()
Default constructor.
Definition CBox3d.h:131
void Include(const CVector3d &point)
Expand box to include point.
Definition CBox3d.h:249
void Reset()
Reset box to empty state.
Definition CBox3d.h:276
const CVector3d & GetMax() const
Get maximum corner of the box.
Definition CBox3d.h:164
CVector3d GetCenter() const
Get center of the box.
Definition CBox3d.h:176
void SetMax(const CVector3d &max)
Set maximum corner of the box.
Definition CBox3d.h:170
bool IsEmpty() const
Returns true if the box is in invalid/empty state.
Definition CBox3d.h:144
bool Serialize(iser::IArchive &archive)
Serialize this box to specified archive.
CVector3d GetExtent() const
Get extent (half-size) of the box.
Definition CBox3d.h:191
CVector3d GetClosestPoint(const CVector3d &point) const
Get closest point on or inside box to given point.
const CVector3d & GetMin() const
Get minimum corner of the box.
Definition CBox3d.h:152
double GetSurfaceArea() const
Calculate surface area of the box.
Definition CBox3d.h:207
double GetVolume() const
Calculate volume of the box.
Definition CBox3d.h:197
bool Intersects(const CBox3d &other) const
Check if this box intersects another box.
Definition CBox3d.h:234
CVector3d GetSize() const
Get size (dimensions) of the box.
Definition CBox3d.h:182
bool operator==(const CBox3d &box) const
Definition CBox3d.h:284
bool Contains(const CVector3d &point, double tolerance=0.0) const
Check if point is inside the box.
Definition CBox3d.h:219
bool operator!=(const CBox3d &box) const
Definition CBox3d.h:290
void SetMin(const CVector3d &min)
Set minimum corner of the box.
Definition CBox3d.h:158
Represents a position or mathematical vector in 3D space with double precision.
Definition CVector3d.h:74
void SetZ(double value)
Sets the Z coordinate of the vector.
Definition CVector3d.h:320
double GetX() const
Gets the X coordinate of the vector.
Definition CVector3d.h:290
void SetX(double value)
Sets the X coordinate of the vector.
Definition CVector3d.h:296
double GetY() const
Gets the Y coordinate of the vector.
Definition CVector3d.h:302
double GetZ() const
Gets the Z coordinate of the vector.
Definition CVector3d.h:314
void SetY(double value)
Sets the Y coordinate of the vector.
Definition CVector3d.h:308
Represents an input/output persistence archive for object serialization.
Definition IArchive.h:164
#define qInf
Definition imath.h:9
Contains the 3D objects.
Definition CAffine3d.h:12