Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions python/ouroboros/helpers/spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
71 changes: 70 additions & 1 deletion python/test/helpers/test_spline.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion python/test/pipeline/test_round_trip_accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading