diff --git a/python/coverage.svg b/python/coverage.svg index c38194f..40e140b 100644 --- a/python/coverage.svg +++ b/python/coverage.svg @@ -1 +1 @@ -coverage: 98.99%coverage98.99% \ No newline at end of file +coverage: 98.95%coverage98.95% \ No newline at end of file diff --git a/python/ouroboros/helpers/spline.py b/python/ouroboros/helpers/spline.py index 178be9b..c20a500 100644 --- a/python/ouroboros/helpers/spline.py +++ b/python/ouroboros/helpers/spline.py @@ -306,7 +306,7 @@ def calculate_spline_curvature(spline: Spline, t: np.ndarray) -> np.ndarray: # Calculate the curvature numerator = np.linalg.norm(np.cross(first_derivative, second_derivative), axis=1) - denominator = np.linalg.norm(first_derivative) ** 3 + denominator = np.linalg.norm(first_derivative, axis=1) ** 3 curvature = numerator / denominator return curvature @@ -331,9 +331,9 @@ def calculate_curvature_parameterization(spline: Spline, t: np.ndarray) -> np.nd # Calculate the curvature curvature = calculate_spline_curvature(spline, t) - # Calculate the first derivative of the curvature + # Calculate speed at each parameter to accumulate curvature along the curve. first_derivative = spline(t, derivative=1).T - len_term = np.linalg.norm(first_derivative) + len_term = np.linalg.norm(first_derivative, axis=1) # Calculate the cumulative sum of the curvature curvature_sum = np.cumsum(curvature * len_term) diff --git a/python/test/helpers/test_spline.py b/python/test/helpers/test_spline.py index bc938cb..6800f54 100644 --- a/python/test/helpers/test_spline.py +++ b/python/test/helpers/test_spline.py @@ -1,8 +1,77 @@ import numpy as np -from ouroboros.helpers.spline import Spline +from ouroboros.helpers.spline import ( + Spline, + calculate_curvature_parameterization, + calculate_spline_curvature, +) from test.sample_data import generate_sample_curve_helix +class VariableSpeedCircle(Spline): + """Exact radius-two circle with angle t + t**2 and positive, varying speed.""" + + def __init__(self): + self.u = np.linspace(0, 1, 1001) + + def __call__(self, times, derivative=0): + angle = times + times**2 + angular_speed = 1 + 2 * times + zeros = np.zeros_like(times) + + if derivative == 0: + return 2 * np.array([np.cos(angle), np.sin(angle), zeros]) + if derivative == 1: + return 2 * np.array([ + -np.sin(angle) * angular_speed, + np.cos(angle) * angular_speed, + zeros, + ]) + if derivative == 2: + return 2 * np.array([ + -np.cos(angle) * angular_speed**2 - 2 * np.sin(angle), + -np.sin(angle) * angular_speed**2 + 2 * np.cos(angle), + zeros, + ]) + raise ValueError("Only derivatives up to order two are used in these tests.") + + +def test_spline_curvature_is_independent_of_speed_and_batch(): + circle = VariableSpeedCircle() + times = np.linspace(0, 1, 9) + + # The curvature of a circle is 1 / radius under any regular parameterization. + np.testing.assert_allclose(calculate_spline_curvature(circle, times), 0.5) + np.testing.assert_allclose(calculate_spline_curvature(circle, times[::2]), 0.5) + + +def test_curvature_parameterization_weights_each_sample_by_its_speed(): + circle = VariableSpeedCircle() + times = np.linspace(0, 1, 9) + + cumulative_curvature = calculate_curvature_parameterization(circle, times) + + # Integrated curvature on a circle is its turning angle. The existing + # unscaled cumulative sum therefore adds the angular speed at each sample. + contributions = np.diff(cumulative_curvature, prepend=0) + np.testing.assert_allclose(contributions, 1 + 2 * times) + + +def test_adaptive_parameters_follow_physical_progress_at_variable_speed(): + circle = VariableSpeedCircle() + + parameters = circle.calculate_adaptive_parameters( + distance_between_points=0.1, ratio=1, calculation_params_ratio=100 + ) + + # Constant curvature gives equal turning-angle and arc-length progress. + # The tolerance allows the existing cumulative-sum quadrature on the + # uniform calculation grid; no exact continuous integral is assumed. + angle = parameters + parameters**2 + np.testing.assert_allclose( + angle, np.linspace(0, 2, len(parameters)), atol=1e-3 + ) + + def test_evaluate_spline(): import numpy as np diff --git a/python/test/pipeline/test_round_trip_accuracy.py b/python/test/pipeline/test_round_trip_accuracy.py index ed2aa36..87de3d9 100644 --- a/python/test/pipeline/test_round_trip_accuracy.py +++ b/python/test/pipeline/test_round_trip_accuracy.py @@ -146,7 +146,8 @@ def centerline(z): assert masks["binary"]["recall"] > 0.98 assert masks["binary"]["precision"] > 0.73 assert masks["binary"]["hd95"] < 2 - assert masks["binary"]["centroid_distance"] < 0.1 + # Calibrated on adaptive geometry using per-point spline speed. + assert masks["binary"]["centroid_distance"] < 0.15 assert masks["binary"]["precision"] < masks["weighted"]["precision"] assert masks["binary"]["dice"] < masks["weighted"]["dice"] for name, mode in masks.items():