forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoundingBox.hh
232 lines (204 loc) · 7.05 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
//----------------------------------*-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 orange/BoundingBox.hh
//---------------------------------------------------------------------------//
#pragma once
#include <type_traits>
#include "corecel/Assert.hh"
#include "corecel/Macros.hh"
#include "corecel/cont/Array.hh"
#include "corecel/math/Algorithms.hh"
#include "corecel/math/NumericLimits.hh"
#include "OrangeTypes.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 coorindate 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 lower_; }
//! Upper bbox coordinate
CELER_CONSTEXPR_FUNCTION Real3 const& upper() const { return upper_; }
// Whether the bbox is non-null
CELER_CONSTEXPR_FUNCTION explicit operator bool() const;
//// MUTATORS ////
// Intersect in place with a half-space
CELER_CONSTEXPR_FUNCTION void
clip(Sense sense, Axis axis, real_type position);
private:
Real3 lower_;
Real3 upper_;
// 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);
}
//---------------------------------------------------------------------------//
// 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();
lower_ = {inf, inf, inf};
upper_ = {-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)
: lower_(lo), upper_(hi)
{
if constexpr (CELERITAS_DEBUG)
{
for (auto ax : {Axis::x, Axis::y, Axis::z})
{
CELER_EXPECT(lower_[to_int(ax)] <= 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)
: lower_(lo), upper_(hi)
{
}
//---------------------------------------------------------------------------//
/*!
* Whether the bbox is in a valid state.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION BoundingBox<T>::operator bool() const
{
return lower_[to_int(Axis::x)] <= upper_[to_int(Axis::x)]
&& lower_[to_int(Axis::y)] <= upper_[to_int(Axis::y)]
&& lower_[to_int(Axis::z)] <= upper_[to_int(Axis::z)];
}
//---------------------------------------------------------------------------//
/*!
* Intersect in place with a half-space.
*/
template<class T>
CELER_CONSTEXPR_FUNCTION void
BoundingBox<T>::clip(Sense sense, Axis axis, real_type position)
{
if (sense == Sense::inside)
{
upper_[to_int(axis)] = ::celeritas::min(upper_[to_int(axis)], position);
}
else
{
lower_[to_int(axis)] = ::celeritas::max(lower_[to_int(axis)], position);
}
}
//---------------------------------------------------------------------------//
} // namespace celeritas