SHiP Geometry
SHiP experiment geometry implementation using GeoModel.
Loading...
Searching...
No Matches
StaticChecks.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// Copyright (C) CERN for the benefit of the SHiP Collaboration
3
4#pragma once
5
7
8#include <array>
9#include <cstddef>
10#include <limits>
11#include <utility>
12
25
26using units::LengthMm;
27
33
35[[nodiscard]] constexpr Span span(LengthMm centre, LengthMm half) {
36 return {.lo = centre - half, .hi = centre + half};
37}
38
40[[nodiscard]] constexpr bool contains(Span outer, Span inner, LengthMm tol = 0.0 * units::mm) {
41 return outer.lo - tol <= inner.lo && inner.hi <= outer.hi + tol;
42}
43
45[[nodiscard]] constexpr bool overlaps(Span a, Span b) {
46 return a.lo < b.hi && b.lo < a.hi;
47}
48
50struct Box {
54};
55
57[[nodiscard]] constexpr Box box(LengthMm cx, LengthMm cy, LengthMm cz, LengthMm hx, LengthMm hy,
58 LengthMm hz) {
59 return {.x = span(cx, hx), .y = span(cy, hy), .z = span(cz, hz)};
60}
61
63[[nodiscard]] constexpr Box box(LengthMm hx, LengthMm hy, LengthMm hz) {
64 constexpr auto zero = 0.0 * units::mm;
65 return box(zero, zero, zero, hx, hy, hz);
66}
67
68[[nodiscard]] constexpr bool contains(const Box& outer, const Box& inner,
69 LengthMm tol = 0.0 * units::mm) {
70 return contains(outer.x, inner.x, tol) && contains(outer.y, inner.y, tol) &&
71 contains(outer.z, inner.z, tol);
72}
73
75[[nodiscard]] constexpr bool overlaps(const Box& a, const Box& b) {
76 return overlaps(a.x, b.x) && overlaps(a.y, b.y) && overlaps(a.z, b.z);
77}
78
80[[nodiscard]] constexpr Box bounds(double x0, double x1, double y0, double y1, double z0,
81 double z1) {
82 return {.x = {.lo = x0 * units::mm, .hi = x1 * units::mm},
83 .y = {.lo = y0 * units::mm, .hi = y1 * units::mm},
84 .z = {.lo = z0 * units::mm, .hi = z1 * units::mm}};
85}
86
88[[nodiscard]] constexpr Box intersection(const Box& a, const Box& b) {
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};
91 };
92 return {.x = meet(a.x, b.x), .y = meet(a.y, b.y), .z = meet(a.z, b.z)};
93}
94
97[[nodiscard]] constexpr bool overlapsWithin(const Box& a, const Box& b, const Box& region,
98 LengthMm tol = 1e-6 * units::mm) {
99 return overlaps(a, b) && contains(region, intersection(a, b), tol);
100}
101
104[[nodiscard]] constexpr Box shrunk(const Box& b, LengthMm d) {
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}};
108}
109
111inline constexpr std::size_t npos = std::numeric_limits<std::size_t>::max();
112
119template <typename Range, typename Pred>
120[[nodiscard]] constexpr std::size_t firstFailure(const Range& range, Pred ok) {
121 std::size_t i = 0;
122 for (const auto& element : range) {
123 if (!ok(element)) {
124 return i;
125 }
126 ++i;
127 }
128 return npos;
129}
130
133template <typename Range, typename Pred>
134[[nodiscard]] constexpr std::size_t firstPairFailure(const Range& range, Pred ok) {
135 for (std::size_t i = 0; i + 1 < std::size(range); ++i) {
136 if (!ok(range[i], range[i + 1])) {
137 return i;
138 }
139 }
140 return npos;
141}
142
150template <std::size_t N>
151[[nodiscard]] constexpr bool coveredBy(const Box& inner, const std::array<Box, N>& outers) {
152 struct Cuts {
153 std::array<LengthMm, (2 * N) + 2> at{};
154 std::size_t n = 0;
155 };
156 const auto cuts = [&](Span Box::* axis) {
157 const Span s = inner.*axis;
158 Cuts c;
159 c.at[c.n++] = s.lo;
160 c.at[c.n++] = s.hi;
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) {
164 c.at[c.n++] = v;
165 }
166 }
167 }
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]);
171 }
172 }
173 return c;
174 };
175 const auto inside = [](Span s, LengthMm v) { return s.lo <= v && v <= s.hi; };
176
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) {
188 covered =
189 covered || (inside(outer.x, x) && inside(outer.y, y) && inside(outer.z, z));
190 }
191 if (!covered) {
192 return false;
193 }
194 }
195 }
196 }
197 return true;
198}
199
200} // namespace SHiP::geometry::checks
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 &region, 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