forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoundingBox.hh
293 lines (261 loc) · 8.85 KB
/
BoundingBox.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//----------------------------------*-C++-*----------------------------------//
// Copyright 2021-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file geocel/BoundingBox.hh
//---------------------------------------------------------------------------//
#pragma once
#include <type_traits>
#include "corecel/Assert.hh"
#include "corecel/Macros.hh"
#include "corecel/Types.hh"
#include "corecel/cont/Array.hh"
#include "corecel/math/Algorithms.hh"
#include "corecel/math/NumericLimits.hh"
#include "Types.hh"
namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Axis-aligned bounding box.
*
* Bounding boxes "contain" all points inside \em and on their faces. See \c
* is_inside in \c BoundingBoxUtils.hh .
*
* The default bounding box is "null", which has at least one \c lower
* coordinate greater than its \c upper coordinate: it evaluates to \c false .
* A null bounding box still has the ability to be unioned and intersected with
* other bounding boxes with the expected effect, but geometrical operations on
* it (center, surface area, volume) are prohibited.
*
* A "degenerate" bounding box is one that is well-defined but has zero volume
* because at least one lower coordinate is equal to the corresponding upper
* coordinate. Any point on the surface of this bounding box is still "inside".
* It may have nonzero surface area but will have zero volume.
*/
template<class T = ::celeritas::real_type>
class BoundingBox
{
public:
//!@{
//! \name Type aliases
using real_type = T;
using Real3 = Array<real_type, 3>;
//!@}
public:
// Construct from infinite extents
static inline CELER_FUNCTION BoundingBox from_infinite();
// Construct from unchecked lower/upper bounds
static CELER_CONSTEXPR_FUNCTION BoundingBox
from_unchecked(Real3 const& lower, Real3 const& upper);
// Construct in unassigned state
CELER_CONSTEXPR_FUNCTION BoundingBox();
// Construct from upper and lower points
inline CELER_FUNCTION BoundingBox(Real3 const& lower, Real3 const& upper);
//// ACCESSORS ////
//! Lower bbox coordinate
CELER_CONSTEXPR_FUNCTION Real3 const& lower() const
{
return this->point(Bound::lo);
}
//! Upper bbox coordinate
CELER_CONSTEXPR_FUNCTION Real3 const& upper() const
{
return this->point(Bound::hi);
}
//! Access a bounding point
CELER_CONSTEXPR_FUNCTION Real3 const& point(Bound b) const
{
return points_[to_int(b)];
}
// Whether the bbox is non-null
CELER_CONSTEXPR_FUNCTION explicit operator bool() const;
//// MUTATORS ////
// Reduce the bounding box's extent along an axis
CELER_CONSTEXPR_FUNCTION void
shrink(Bound bnd, Axis axis, real_type position);
// Increase the bounding box's extent along an axis
CELER_CONSTEXPR_FUNCTION void
grow(Bound bnd, Axis axis, real_type position);
private:
Array<Real3, 2> points_; //!< lo/hi points
// Implementation of 'from_unchecked' (true type 'tag')
CELER_CONSTEXPR_FUNCTION
BoundingBox(std::true_type, Real3 const& lower, Real3 const& upper);
};
//---------------------------------------------------------------------------//
// TYPE ALIASES
//---------------------------------------------------------------------------//
//! Bounding box for host metadata
using BBox = BoundingBox<>;
//---------------------------------------------------------------------------//
// INLINE FREE FUNCTIONS
//---------------------------------------------------------------------------//
/*!
* Test equality of two bounding boxes.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION bool
operator==(BoundingBox<T> const& lhs, BoundingBox<T> const& rhs)
{
return lhs.lower() == rhs.lower() && lhs.upper() == rhs.upper();
}
//---------------------------------------------------------------------------//
/*!
* Test inequality of two bounding boxes.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION bool
operator!=(BoundingBox<T> const& lhs, BoundingBox<T> const& rhs)
{
return !(lhs == rhs);
}
//---------------------------------------------------------------------------//
/*!
* Determine if a point is contained in a bounding box.
*
* No point is ever contained in a null bounding box. A degenerate bounding
* box will return "true" for any point on its face.
*/
template<class T, class U>
CELER_CONSTEXPR_FUNCTION bool
is_inside(BoundingBox<T> const& bbox, Array<U, 3> const& point)
{
return bbox.lower()[0] <= point[0] && point[0] <= bbox.upper()[0]
&& bbox.lower()[1] <= point[1] && point[1] <= bbox.upper()[1]
&& bbox.lower()[2] <= point[2] && point[2] <= bbox.upper()[2];
}
//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Create a bounding box with infinite extents.
*/
template<class T>
CELER_FUNCTION BoundingBox<T> BoundingBox<T>::from_infinite()
{
constexpr real_type inf = numeric_limits<real_type>::infinity();
return {{-inf, -inf, -inf}, {inf, inf, inf}};
}
//---------------------------------------------------------------------------//
/*!
* Create a bounding box from unchecked lower/upper bounds.
*
* This should be used exclusively for utilities that understand the
* "null" implementation of the bounding box.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION BoundingBox<T>
BoundingBox<T>::from_unchecked(Real3 const& lo, Real3 const& hi)
{
return BoundingBox<T>{std::true_type{}, lo, hi};
}
//---------------------------------------------------------------------------//
/*!
* Create a null bounding box.
*
* This should naturally satisfy
* \code
calc_union(BBox{}, other) = other:
\endcode
* and
* \code
calc_intersection(BBox{}, other) = other;
\endcode
*/
template<class T>
CELER_CONSTEXPR_FUNCTION BoundingBox<T>::BoundingBox()
{
constexpr real_type inf = numeric_limits<real_type>::infinity();
points_[to_int(Bound::lo)] = {inf, inf, inf};
points_[to_int(Bound::hi)] = {-inf, -inf, -inf};
}
//---------------------------------------------------------------------------//
/*!
* Create a non-null bounding box from two points.
*
* The lower and upper points are allowed to be equal (an empty bounding box
* at a single point) but upper must not be less than lower.
*/
template<class T>
CELER_FUNCTION BoundingBox<T>::BoundingBox(Real3 const& lo, Real3 const& hi)
: points_{{lo, hi}}
{
if constexpr (CELERITAS_DEBUG)
{
for (auto ax : {Axis::x, Axis::y, Axis::z})
{
CELER_EXPECT(this->lower()[to_int(ax)]
<= this->upper()[to_int(ax)]);
}
}
CELER_ENSURE(*this);
}
//---------------------------------------------------------------------------//
/*!
* Create a possibly null bounding box from two points.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION
BoundingBox<T>::BoundingBox(std::true_type, Real3 const& lo, Real3 const& hi)
: points_{{lo, hi}}
{
}
//---------------------------------------------------------------------------//
/*!
* Whether the bbox is in a valid state.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION BoundingBox<T>::operator bool() const
{
return this->lower()[0] <= this->upper()[0]
&& this->lower()[1] <= this->upper()[1]
&& this->lower()[2] <= this->upper()[2];
}
//---------------------------------------------------------------------------//
/*!
* Reduce (clip) the bounding box's extent along an axis.
*
* If the point is inside the box, the box is clipped so the given boundary is
* on that point. Otherwise no change is made.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION void
BoundingBox<T>::shrink(Bound bnd, Axis axis, real_type position)
{
real_type p = points_[to_int(bnd)][to_int(axis)];
if (bnd == Bound::lo)
{
p = ::celeritas::max(p, position);
}
else
{
p = ::celeritas::min(p, position);
}
points_[to_int(bnd)][to_int(axis)] = p;
}
//---------------------------------------------------------------------------//
/*!
* Increase (expand) the bounding box's extent along an axis.
*
* If the point is outside the box, the box is expanded so the given boundary
* is on that point. Otherwise no change is made.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION void
BoundingBox<T>::grow(Bound bnd, Axis axis, real_type position)
{
real_type p = points_[to_int(bnd)][to_int(axis)];
if (bnd == Bound::lo)
{
p = ::celeritas::min(p, position);
}
else
{
p = ::celeritas::max(p, position);
}
points_[to_int(bnd)][to_int(axis)] = p;
}
//---------------------------------------------------------------------------//
} // namespace celeritas