File indexing completed on 2025-12-16 09:24:39
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Acts/Definitions/Algebra.hpp"
0010 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0011 #include "Acts/Surfaces/ConvexPolygonBounds.hpp"
0012 #include "ActsTests/CommonHelpers/BenchmarkTools.hpp"
0013
0014 #include <algorithm>
0015 #include <iostream>
0016 #include <random>
0017 #include <vector>
0018
0019 using namespace Acts;
0020 using namespace ActsTests;
0021
0022 int main(int , char** ) {
0023
0024
0025
0026 ConvexPolygonBounds<PolygonDynamic> poly(
0027 {{0.4, 0.25}, {0.6, 0.25}, {0.8, 0.75}, {0.2, 0.75}});
0028
0029
0030 SquareMatrix2 cov;
0031 cov << 0.2, 0.02, 0.15, 0.02;
0032
0033
0034
0035
0036 std::mt19937 rng(42);
0037 std::uniform_real_distribution<double> axis(0, 1);
0038 auto random_point = [&]() -> Vector2 {
0039 return Vector2(axis(rng), axis(rng));
0040 };
0041
0042
0043 const Vector2 center(0.5, 0.5);
0044
0045 const Vector2 edge_inside(0.401, 0.251);
0046
0047 const Vector2 edge_outside(0.399, 0.249);
0048
0049 const Vector2 far_away(-1000., -1000.);
0050
0051
0052
0053
0054 constexpr int NTESTS = 5'000;
0055
0056 // Some checks are much slower, so we tune down benchmark iterations
0057 constexpr int NTESTS_SLOW = NTESTS / 10;
0058
0059 // Conversely, no-op tests are so fast that we need to tune up iterations
0060 constexpr int NTESTS_NOOP = NTESTS * 10;
0061
0062 // We use this to switch between iteration counts
0063 enum class Mode { None, FastOutside, SlowOutside };
0064
0065 // Benchmark output display
0066 auto print_bench_header = [](const std::string& check_name) {
0067 std::cout << check_name << ":" << std::endl;
0068 };
0069 auto print_bench_result = [](const std::string& bench_name,
0070 const MicroBenchmarkResult& res) {
0071 std::cout << "- " << bench_name << ": " << res << std::endl;
0072 };
0073
0074 // Benchmark runner
0075 auto run_bench = [&](auto&& iteration, int num_iters,
0076 const std::string& bench_name) {
0077 auto bench_result = microBenchmark(iteration, num_iters);
0078 print_bench_result(bench_name, bench_result);
0079 };
0080 auto run_bench_with_inputs = [&](auto&& iterationWithArg, auto&& inputs,
0081 const std::string& bench_name) {
0082 auto bench_result = microBenchmark(iterationWithArg, inputs);
0083 print_bench_result(bench_name, bench_result);
0084 };
0085 auto run_all_benches = [&](const BoundaryTolerance& check,
0086 const std::string& check_name, const Mode mode) {
0087 // Announce a set of benchmarks
0088 print_bench_header(check_name);
0089
0090 // Pre-determined "interesting" test points
0091 int num_inside_points = 0;
0092 int num_outside_points = 0;
0093 switch (mode) {
0094 case Mode::None:
0095 num_inside_points = NTESTS_NOOP;
0096 num_outside_points = NTESTS_NOOP;
0097 break;
0098 case Mode::FastOutside:
0099 num_inside_points = NTESTS;
0100 num_outside_points = NTESTS;
0101 break;
0102 case Mode::SlowOutside:
0103 num_inside_points = NTESTS;
0104 num_outside_points = NTESTS_SLOW;
0105 default: // do nothing
0106 break;
0107 };
0108 run_bench([&] { return poly.inside(center, check); }, num_inside_points,
0109 "Center");
0110 run_bench([&] { return poly.inside(edge_inside, check); },
0111 num_inside_points, "Inside edge");
0112 run_bench([&] { return poly.inside(edge_outside, check); },
0113 num_outside_points, "Outside edge");
0114 run_bench([&] { return poly.inside(far_away, check); }, num_outside_points,
0115 "Far away");
0116
0117 // Pre-rolled random points
0118 std::vector<Vector2> points(num_outside_points);
0119 std::generate(points.begin(), points.end(), random_point);
0120 run_bench_with_inputs(
0121 [&](const auto& point) { return poly.inside(point, check); }, points,
0122 "Random");
0123 };
0124
0125 // Benchmark scenarios
0126 run_all_benches(BoundaryTolerance::Infinite(), "No check", Mode::None);
0127 run_all_benches(BoundaryTolerance::None(), "No tolerance", Mode::FastOutside);
0128 run_all_benches(BoundaryTolerance::AbsoluteEuclidean(0.6), "Abs. tolerance",
0129 Mode::SlowOutside);
0130 run_all_benches(BoundaryTolerance::Chi2Bound(cov, 3.0), "Cov. tolerance",
0131 Mode::SlowOutside);
0132
0133 return 0;
0134 }