File indexing completed on 2025-01-18 09:11:14
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "Acts/Definitions/Algebra.hpp"
0012
0013 #include <unsupported/Eigen/Splines>
0014
0015 namespace Acts::Interpolation3D {
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 template <typename trajectory_type>
0033 trajectory_type spline(const trajectory_type& inputsRaw, std::size_t nPoints,
0034 bool keepOriginalHits = false) {
0035 trajectory_type output;
0036 if (inputsRaw.empty()) {
0037 return output;
0038 }
0039
0040 using InputVectorType = typename trajectory_type::value_type;
0041
0042 std::vector<Vector3> inputs;
0043
0044 if constexpr (std::is_same_v<trajectory_type, std::vector<Vector3>>) {
0045 inputs = inputsRaw;
0046 } else {
0047 inputs.reserve(inputsRaw.size());
0048 for (const auto& input : inputsRaw) {
0049 inputs.push_back(Vector3(input[0], input[1], input[2]));
0050 }
0051 }
0052
0053
0054
0055 if (inputsRaw.size() < 3 || nPoints <= inputsRaw.size()) {
0056 return inputsRaw;
0057 } else {
0058 Eigen::MatrixXd points(3, inputs.size());
0059 for (std::size_t i = 0; i < inputs.size(); ++i) {
0060 points.col(i) = inputs[i].transpose();
0061 }
0062 Eigen::Spline<double, 3> spline3D =
0063 Eigen::SplineFitting<Eigen::Spline<double, 3>>::Interpolate(points, 2);
0064
0065 double step = 1. / (nPoints - 1);
0066 for (std::size_t i = 0; i < nPoints; ++i) {
0067 double t = i * step;
0068 InputVectorType point;
0069 point[0] = spline3D(t)[0];
0070 point[1] = spline3D(t)[1];
0071 point[2] = spline3D(t)[2];
0072 output.push_back(point);
0073 }
0074 }
0075
0076
0077 if (keepOriginalHits) {
0078 output.insert(output.begin(), inputsRaw.begin() + 1, inputsRaw.end() - 1);
0079
0080 std::sort(output.begin(), output.end(),
0081 [&inputs](const auto& a, const auto& b) {
0082 const auto ifront = inputs.front();
0083 double da2 = (a[0] - ifront[0]) * (a[0] - ifront[0]) +
0084 (a[1] - ifront[1]) * (a[1] - ifront[1]) +
0085 (a[2] - ifront[2]) * (a[2] - ifront[2]);
0086 double db2 = (b[0] - ifront[0]) * (b[0] - ifront[0]) +
0087 (b[1] - ifront[1]) * (b[1] - ifront[1]) +
0088 (b[2] - ifront[2]) * (b[2] - ifront[2]);
0089 return da2 < db2;
0090 });
0091 }
0092
0093 return output;
0094 }
0095
0096 }