File indexing completed on 2025-09-17 08:02:26
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Acts/Surfaces/detail/VerticesHelper.hpp"
0010
0011 #include <algorithm>
0012 #include <cmath>
0013 #include <cstddef>
0014 #include <numbers>
0015
0016 namespace Acts {
0017
0018 std::vector<double> detail::VerticesHelper::phiSegments(
0019 double phiMin, double phiMax, const std::vector<double>& phiRefs,
0020 unsigned int quarterSegments) {
0021
0022 if (phiMin > phiMax) {
0023 throw std::invalid_argument(
0024 "VerticesHelper::phiSegments ... Minimum phi must be smaller than "
0025 "maximum phi");
0026 }
0027
0028
0029 for (double phiRef : phiRefs) {
0030 if (phiRef < phiMin || phiRef > phiMax) {
0031 throw std::invalid_argument(
0032 "VerticesHelper::phiSegments ... Reference phi is outside the range "
0033 "of the segment");
0034 }
0035 }
0036 if (quarterSegments == 0u) {
0037 throw std::invalid_argument(
0038 "VerticesHelper::phiSegments ... Number of segments must be larger "
0039 "than 0.");
0040 }
0041 std::vector<double> phiSegments = {phiMin, phiMax};
0042
0043
0044 for (unsigned int i = 0; i < 4 * quarterSegments + 1; ++i) {
0045 double phiExt =
0046 -std::numbers::pi + i * 2 * std::numbers::pi / (4 * quarterSegments);
0047 if (phiExt > phiMin && phiExt < phiMax &&
0048 std::ranges::none_of(phiSegments, [&phiExt](double phi) {
0049 return std::abs(phi - phiExt) <
0050 std::numeric_limits<double>::epsilon();
0051 })) {
0052 phiSegments.push_back(phiExt);
0053 }
0054 }
0055
0056 for (const auto& phiRef : phiRefs) {
0057 if (phiRef > phiMin && phiRef < phiMax) {
0058 if (std::ranges::none_of(phiSegments, [&phiRef](double phi) {
0059 return std::abs(phi - phiRef) <
0060 std::numeric_limits<double>::epsilon();
0061 })) {
0062 phiSegments.push_back(phiRef);
0063 }
0064 }
0065 }
0066
0067
0068 std::ranges::sort(phiSegments);
0069 return phiSegments;
0070 }
0071
0072 std::vector<Vector2> detail::VerticesHelper::ellipsoidVertices(
0073 double innerRx, double innerRy, double outerRx, double outerRy,
0074 double avgPhi, double halfPhi, unsigned int quarterSegments) {
0075
0076
0077 std::vector<Vector2> rvertices;
0078 std::vector<Vector2> ivertices;
0079 std::vector<Vector2> overtices;
0080
0081 bool innerExists = (innerRx > 0. && innerRy > 0.);
0082 bool closed = std::abs(halfPhi - std::numbers::pi) < s_onSurfaceTolerance;
0083
0084 std::vector<double> refPhi = {};
0085 if (avgPhi != 0.) {
0086 refPhi.push_back(avgPhi);
0087 }
0088
0089
0090 if (innerExists) {
0091 ivertices = segmentVertices<Vector2, Transform2>(
0092 {innerRx, innerRy}, avgPhi - halfPhi, avgPhi + halfPhi, refPhi,
0093 quarterSegments);
0094 }
0095 overtices = segmentVertices<Vector2, Transform2>(
0096 {outerRx, outerRy}, avgPhi - halfPhi, avgPhi + halfPhi, refPhi,
0097 quarterSegments);
0098
0099
0100 if (!innerExists) {
0101 if (!closed) {
0102
0103 rvertices.push_back(Vector2(0., 0.));
0104 }
0105 rvertices.insert(rvertices.end(), overtices.begin(), overtices.end());
0106 } else if (!closed) {
0107 rvertices.insert(rvertices.end(), overtices.begin(), overtices.end());
0108 rvertices.insert(rvertices.end(), ivertices.rbegin(), ivertices.rend());
0109 } else {
0110 rvertices.insert(rvertices.end(), overtices.begin(), overtices.end());
0111 rvertices.insert(rvertices.end(), ivertices.begin(), ivertices.end());
0112 }
0113 return rvertices;
0114 }
0115
0116 std::vector<Vector2> detail::VerticesHelper::circularVertices(
0117 double innerR, double outerR, double avgPhi, double halfPhi,
0118 unsigned int quarterSegments) {
0119 return ellipsoidVertices(innerR, innerR, outerR, outerR, avgPhi, halfPhi,
0120 quarterSegments);
0121 }
0122
0123 bool detail::VerticesHelper::onHyperPlane(const std::vector<Vector3>& vertices,
0124 double tolerance) {
0125
0126 if (vertices.size() < 4) {
0127 return true;
0128 }
0129
0130 auto hyperPlane = Eigen::Hyperplane<double, 3>::Through(
0131 vertices[0], vertices[1], vertices[2]);
0132 for (std::size_t ip = 3; ip < vertices.size(); ++ip) {
0133 if (hyperPlane.absDistance(vertices[ip]) > tolerance) {
0134 return false;
0135 }
0136 }
0137 return true;
0138 }
0139
0140 Vector2 detail::VerticesHelper::computeClosestPointOnPolygon(
0141 const Vector2& point, std::span<const Vector2> vertices,
0142 const SquareMatrix2& metric) {
0143 auto squaredNorm = [&](const Vector2& x) {
0144 return (x.transpose() * metric * x).value();
0145 };
0146
0147
0148
0149 auto closestOnSegment = [&](auto&& ll0, auto&& ll1) {
0150
0151 auto n = ll1 - ll0;
0152 auto n_transformed = metric * n;
0153 auto f = n.dot(n_transformed);
0154 auto u = std::isnormal(f)
0155 ? (point - ll0).dot(n_transformed) / f
0156 : 0.5;
0157
0158 return ll0 + std::clamp(u, 0.0, 1.0) * n;
0159 };
0160
0161 auto iv = std::begin(vertices);
0162 Vector2 l0 = *iv;
0163 Vector2 l1 = *(++iv);
0164 Vector2 closest = closestOnSegment(l0, l1);
0165 auto closestDist = squaredNorm(closest - point);
0166
0167 for (++iv; iv != std::end(vertices); ++iv) {
0168 l0 = l1;
0169 l1 = *iv;
0170 Vector2 current = closestOnSegment(l0, l1);
0171 auto currentDist = squaredNorm(current - point);
0172 if (currentDist < closestDist) {
0173 closest = current;
0174 closestDist = currentDist;
0175 }
0176 }
0177
0178 Vector2 last = closestOnSegment(l1, *std::begin(vertices));
0179 if (squaredNorm(last - point) < closestDist) {
0180 closest = last;
0181 }
0182 return closest;
0183 }
0184
0185 Vector2 detail::VerticesHelper::computeEuclideanClosestPointOnRectangle(
0186 const Vector2& point, const Vector2& lowerLeft, const Vector2& upperRight) {
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205 double l0 = point[0];
0206 double l1 = point[1];
0207 double loc0Min = lowerLeft[0];
0208 double loc0Max = upperRight[0];
0209 double loc1Min = lowerLeft[1];
0210 double loc1Max = upperRight[1];
0211
0212
0213 if (loc0Min <= l0 && l0 < loc0Max && loc1Min <= l1 && l1 < loc1Max) {
0214
0215 double dist = std::abs(loc0Max - l0);
0216 Vector2 cls(loc0Max, l1);
0217
0218 double test = std::abs(loc0Min - l0);
0219 if (test <= dist) {
0220 dist = test;
0221 cls = {loc0Min, l1};
0222 }
0223
0224 test = std::abs(loc1Max - l1);
0225 if (test <= dist) {
0226 dist = test;
0227 cls = {l0, loc1Max};
0228 }
0229
0230 test = std::abs(loc1Min - l1);
0231 if (test <= dist) {
0232 return {l0, loc1Min};
0233 }
0234 return cls;
0235 } else {
0236
0237 if (l0 > loc0Max) {
0238 if (l1 > loc1Max) {
0239 return {loc0Max, loc1Max};
0240 } else if (l1 <= loc1Min) {
0241 return {loc0Max, loc1Min};
0242 } else {
0243 return {loc0Max, l1};
0244 }
0245 } else if (l0 < loc0Min) {
0246 if (l1 > loc1Max) {
0247 return {loc0Min, loc1Max};
0248 } else if (l1 <= loc1Min) {
0249 return {loc0Min, loc1Min};
0250 } else {
0251 return {loc0Min, l1};
0252 }
0253 } else {
0254 if (l1 > loc1Max) {
0255 return {l0, loc1Max};
0256 } else {
0257 return {l0, loc1Min};
0258 }
0259
0260 }
0261 }
0262 }
0263
0264 Vector2 detail::VerticesHelper::computeClosestPointOnAlignedBox(
0265 const Vector2& lowerLeft, const Vector2& upperRight, const Vector2& point,
0266 const SquareMatrix2& metric) {
0267 Vector2 closestPoint;
0268
0269 if (metric.isIdentity()) {
0270 closestPoint =
0271 detail::VerticesHelper::computeEuclideanClosestPointOnRectangle(
0272 point, lowerLeft, upperRight);
0273 } else {
0274
0275
0276
0277 std::array<Vector2, 4> vertices = {{lowerLeft,
0278 {upperRight[0], lowerLeft[1]},
0279 upperRight,
0280 {lowerLeft[0], upperRight[1]}}};
0281
0282 closestPoint = detail::VerticesHelper::computeClosestPointOnPolygon(
0283 point, vertices, metric);
0284 }
0285
0286 return closestPoint;
0287 }
0288
0289 }