36 return {.lo = centre - half, .hi = centre + half};
41 return outer.
lo - tol <= inner.
lo && inner.
hi <= outer.
hi + tol;
59 return {.x =
span(cx, hx), .y =
span(cy, hy), .z =
span(cz, hz)};
65 return box(zero, zero, zero, hx, hy, hz);
80[[nodiscard]]
constexpr Box bounds(
double x0,
double x1,
double y0,
double y1,
double z0,
89 const auto meet = [](
Span s,
Span t) {
90 return Span{.lo = s.
lo < t.lo ? t.lo : s.
lo, .hi = s.
hi < t.hi ? s.
hi : t.hi};
92 return {.x = meet(a.
x, b.
x), .y = meet(a.
y, b.
y), .z = meet(a.
z, b.
z)};
105 return {.x = {.lo = b.
x.
lo + d, .hi = b.
x.
hi - d},
106 .y = {.lo = b.
y.
lo + d, .hi = b.
y.
hi - d},
107 .z = {.lo = b.
z.
lo + d, .hi = b.
z.
hi - d}};
111inline constexpr std::size_t
npos = std::numeric_limits<std::size_t>::max();
119template <
typename Range,
typename Pred>
120[[nodiscard]]
constexpr std::size_t
firstFailure(
const Range& range, Pred ok) {
122 for (
const auto& element : range) {
133template <
typename Range,
typename Pred>
135 for (std::size_t i = 0; i + 1 < std::size(range); ++i) {
136 if (!ok(range[i], range[i + 1])) {
150template <std::
size_t N>
151[[nodiscard]]
constexpr bool coveredBy(
const Box& inner,
const std::array<Box, N>& outers) {
153 std::array<
LengthMm, (2 * N) + 2> at{};
156 const auto cuts = [&](
Span Box::* axis) {
157 const Span s = inner.*axis;
161 for (
const auto& outer : outers) {
162 for (
const LengthMm v : {(outer.*axis).lo, (outer.*axis).hi}) {
163 if (s.
lo < v && v < s.
hi) {
168 for (std::size_t i = 1; i < c.n; ++i) {
169 for (std::size_t j = i; j > 0 && c.at[j] < c.at[j - 1]; --j) {
170 std::swap(c.at[j], c.at[j - 1]);
175 const auto inside = [](
Span s,
LengthMm v) {
return s.
lo <= v && v <= s.
hi; };
177 const Cuts cx = cuts(&
Box::x);
178 const Cuts cy = cuts(&
Box::y);
179 const Cuts cz = cuts(&
Box::z);
180 for (std::size_t i = 0; i + 1 < cx.n; ++i) {
181 for (std::size_t j = 0; j + 1 < cy.n; ++j) {
182 for (std::size_t k = 0; k + 1 < cz.n; ++k) {
183 const LengthMm x = (cx.at[i] + cx.at[i + 1]) / 2.0;
184 const LengthMm y = (cy.at[j] + cy.at[j + 1]) / 2.0;
185 const LengthMm z = (cz.at[k] + cz.at[k + 1]) / 2.0;
186 bool covered =
false;
187 for (
const auto& outer : outers) {
189 covered || (inside(outer.x, x) && inside(outer.y, y) && inside(outer.z, z));
Constexpr predicates for compile-time geometry consistency checks.
Definition StaticChecks.h:24
constexpr std::size_t firstPairFailure(const Range &range, Pred ok)
Index i of the first neighbouring pair (range[i], range[i+1]) for which ok is false,...
Definition StaticChecks.h:134
constexpr Box shrunk(const Box &b, LengthMm d)
b with every face moved inwards by d.
Definition StaticChecks.h:104
constexpr bool overlaps(Span a, Span b)
The intervals share more than a boundary. Touching ends do not overlap.
Definition StaticChecks.h:45
constexpr bool coveredBy(const Box &inner, const std::array< Box, N > &outers)
Whether inner lies within the union of outers.
Definition StaticChecks.h:151
constexpr std::size_t firstFailure(const Range &range, Pred ok)
Index of the first element of range for which ok is false, or npos.
Definition StaticChecks.h:120
constexpr bool contains(Span outer, Span inner, LengthMm tol=0.0 *units::mm)
inner lies within outer, allowing tol of slack at either end.
Definition StaticChecks.h:40
constexpr Box intersection(const Box &a, const Box &b)
The region two boxes share. Only meaningful if they overlap.
Definition StaticChecks.h:88
constexpr Box bounds(double x0, double x1, double y0, double y1, double z0, double z1)
Box from its bounds in mm, for writing a known region down literally.
Definition StaticChecks.h:80
constexpr Span span(LengthMm centre, LengthMm half)
Interval of a volume with the given centre and half-length.
Definition StaticChecks.h:35
constexpr std::size_t npos
Returned by the first*() searches when every element passes.
Definition StaticChecks.h:111
constexpr bool overlapsWithin(const Box &a, const Box &b, const Box ®ion, LengthMm tol=1e-6 *units::mm)
The boxes overlap, and only within region.
Definition StaticChecks.h:97
constexpr Box box(LengthMm cx, LengthMm cy, LengthMm cz, LengthMm hx, LengthMm hy, LengthMm hz)
Box with centre (cx, cy, cz) and the given half-lengths.
Definition StaticChecks.h:57
mp_units::quantity< mm > LengthMm
Spellings for struct members and function parameters.
Definition Units.h:39
constexpr auto mm
Definition Units.h:27
Axis-aligned box.
Definition StaticChecks.h:50
Span z
Definition StaticChecks.h:53
Span y
Definition StaticChecks.h:52
Span x
Definition StaticChecks.h:51
Closed interval along one axis.
Definition StaticChecks.h:29
LengthMm hi
Definition StaticChecks.h:31
LengthMm lo
Definition StaticChecks.h:30