-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move spline to helper in Visualization (#3950)
This PR moves the spline-interpolation from `Examples/Plugins/Obj` to `Core/Visualization` because it can be used in other visualisation areas as well. I've added also a UnitTest since this is from some `experimetnal/usupported` Eigen directory, but it works nicely!
- Loading branch information
1 parent
5c41085
commit a646e06
Showing
6 changed files
with
211 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// This file is part of the ACTS project. | ||
// | ||
// Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "Acts/Definitions/Algebra.hpp" | ||
|
||
#include <unsupported/Eigen/Splines> | ||
|
||
namespace Acts::Interpolation3D { | ||
|
||
/// @brief Helper function to interpolate points using a spline | ||
/// from Eigen | ||
/// | ||
/// The only requirement is that the input trajectory type has | ||
/// a method empty() and size() and that the elements can be | ||
/// accessed with operator[] and have themselves a operator[] to | ||
/// access the coordinates. | ||
/// | ||
/// @tparam input_trajectory_type input trajectory type | ||
/// | ||
/// @param inputsRaw input vector points | ||
/// @param nPoints number of interpolation points | ||
/// @param keepOriginalHits keep the original hits in the trajectory | ||
/// | ||
/// @return std::vector<Acts::Vector3> interpolated points | ||
template <typename trajectory_type> | ||
trajectory_type spline(const trajectory_type& inputsRaw, std::size_t nPoints, | ||
bool keepOriginalHits = false) { | ||
trajectory_type output; | ||
if (inputsRaw.empty()) { | ||
return output; | ||
} | ||
|
||
using InputVectorType = typename trajectory_type::value_type; | ||
|
||
std::vector<Vector3> inputs; | ||
// If input type is a vector of Vector3 we can use it directly | ||
if constexpr (std::is_same_v<trajectory_type, std::vector<Vector3>>) { | ||
inputs = inputsRaw; | ||
} else { | ||
inputs.reserve(inputsRaw.size()); | ||
for (const auto& input : inputsRaw) { | ||
inputs.push_back(Vector3(input[0], input[1], input[2])); | ||
} | ||
} | ||
|
||
// Don't do anything if we have less than 3 points or less interpolation | ||
// points than input points | ||
if (inputsRaw.size() < 3 || nPoints <= inputsRaw.size()) { | ||
return inputsRaw; | ||
} else { | ||
Eigen::MatrixXd points(3, inputs.size()); | ||
for (std::size_t i = 0; i < inputs.size(); ++i) { | ||
points.col(i) = inputs[i].transpose(); | ||
} | ||
Eigen::Spline<double, 3> spline3D = | ||
Eigen::SplineFitting<Eigen::Spline<double, 3>>::Interpolate(points, 2); | ||
|
||
double step = 1. / (nPoints - 1); | ||
for (std::size_t i = 0; i < nPoints; ++i) { | ||
double t = i * step; | ||
InputVectorType point; | ||
point[0] = spline3D(t)[0]; | ||
point[1] = spline3D(t)[1]; | ||
point[2] = spline3D(t)[2]; | ||
output.push_back(point); | ||
} | ||
} | ||
// If we want to keep the original hits, we add them to the output | ||
// (first and last are there anyway) | ||
if (keepOriginalHits) { | ||
output.insert(output.begin(), inputsRaw.begin() + 1, inputsRaw.end() - 1); | ||
// We need to sort the output in distance to first | ||
std::sort(output.begin(), output.end(), | ||
[&inputs](const auto& a, const auto& b) { | ||
const auto ifront = inputs.front(); | ||
double da2 = (a[0] - ifront[0]) * (a[0] - ifront[0]) + | ||
(a[1] - ifront[1]) * (a[1] - ifront[1]) + | ||
(a[2] - ifront[2]) * (a[2] - ifront[2]); | ||
double db2 = (b[0] - ifront[0]) * (b[0] - ifront[0]) + | ||
(b[1] - ifront[1]) * (b[1] - ifront[1]) + | ||
(b[2] - ifront[2]) * (b[2] - ifront[2]); | ||
return da2 < db2; | ||
}); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
} // namespace Acts::Interpolation3D |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Tests/UnitTests/Core/Visualization/Interpolation3DTests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// This file is part of the ACTS project. | ||
// | ||
// Copyright (C) 2016 CERN for the benefit of the ACTS project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
#include "Acts/Tests/CommonHelpers/FloatComparisons.hpp" | ||
#include "Acts/Visualization/Interpolation3D.hpp" | ||
|
||
#include <numbers> | ||
|
||
namespace Acts::Test { | ||
|
||
BOOST_AUTO_TEST_SUITE(Visualization) | ||
|
||
BOOST_AUTO_TEST_CASE(SplineInterpolationEigen) { | ||
/// Define the input vector | ||
double R = 10.; | ||
std::vector<Acts::Vector3> inputs; | ||
|
||
// Interpolate the points options | ||
std::vector<Acts::Vector3> trajectory; | ||
|
||
// Empty in empty out check | ||
trajectory = Acts::Interpolation3D::spline(inputs, 10); | ||
BOOST_CHECK(trajectory.empty()); | ||
|
||
for (double phi = 0; phi < 2 * std::numbers::pi; | ||
phi += std::numbers::pi / 4) { | ||
inputs.push_back(Acts::Vector3(R * cos(phi), R * sin(phi), 0.)); | ||
} | ||
|
||
// (0) - nothing happens | ||
trajectory = Acts::Interpolation3D::spline(inputs, 1); | ||
// Check input and output size are the same | ||
BOOST_CHECK_EQUAL(trajectory.size(), inputs.size()); | ||
|
||
// (1) - interpolate between the points with 12 points in total | ||
trajectory = Acts::Interpolation3D::spline(inputs, 12); | ||
// Check the output size is correct | ||
BOOST_CHECK_EQUAL(trajectory.size(), 12); | ||
|
||
for (const auto& point : trajectory) { | ||
// Check the interpolated points are on the circle | ||
// with a tolerance of course | ||
CHECK_CLOSE_ABS(point.norm(), R, 0.1); | ||
// Verify points remain in the XY plane | ||
CHECK_CLOSE_ABS(point.z(), 0., 0.1); | ||
} | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(SplineInterpolationArray) { | ||
/// Define the input vector | ||
std::vector<std::array<double, 3u>> inputs; | ||
|
||
for (double x = 0; x < 10; x += 1) { | ||
inputs.push_back({x, x * x, 0.}); | ||
} | ||
|
||
// This time we keep the original hits | ||
auto trajectory = Acts::Interpolation3D::spline(inputs, 100, true); | ||
|
||
// Check the outpu type is correct | ||
constexpr bool isOutput = | ||
std::is_same_v<decltype(trajectory), decltype(inputs)>; | ||
BOOST_CHECK(isOutput); | ||
|
||
// Check the output size is correct | ||
BOOST_CHECK_EQUAL(trajectory.size(), 108); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(SplineInterpolationErrors) { | ||
std::vector<std::array<double, 3u>> inputs; | ||
|
||
// Test with single point | ||
inputs.push_back({0., 0., 0.}); | ||
auto result = Acts::Interpolation3D::spline(inputs, 10); | ||
BOOST_CHECK_EQUAL(result.size(), 1); | ||
|
||
// Test with two points | ||
inputs.push_back({1., 1., 1.}); | ||
result = Acts::Interpolation3D::spline(inputs, 10); | ||
BOOST_CHECK_EQUAL(result.size(), 2); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() | ||
|
||
} // namespace Acts::Test |