-
Notifications
You must be signed in to change notification settings - Fork 93
Add BSplineSE3 class. #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1f28014
Add BSpline_SE3 class
myeatman-bdai 4c21989
formatting
myeatman-bdai 043b217
Clean up. Doc strings. Etc..
myeatman-bdai 74fd97a
nit
myeatman-bdai e065e5e
nit
myeatman-bdai 20d491e
Add unit test.
myeatman-bdai f04e0dd
Fix jien's review comments.
myeatman-bdai 6ca54b4
Run black formatter.
myeatman-bdai 279c1ca
Properly generalize uniformly spaced knots with degree of input.
myeatman-bdai f81893a
Better comment.
myeatman-bdai 350a25c
...
myeatman-bdai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
# Copyright (c) 2024 Boston Dynamics AI Institute LLC. | ||
# MIT Licence, see details in top-level file: LICENCE | ||
|
||
""" | ||
Classes for parameterizing a trajectory in SE3 with B-splines. | ||
|
||
Copies parts of the API from scipy's B-spline class. | ||
""" | ||
|
||
from typing import Any, Dict, List, Optional | ||
from scipy.interpolate import BSpline | ||
from spatialmath import SE3 | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from spatialmath.base.transforms3d import tranimate, trplot | ||
|
||
|
||
class BSplineSE3: | ||
"""A class to parameterize a trajectory in SE3 with a 6-dimensional B-spline. | ||
|
||
The SE3 control poses are converted to se3 twists (the lie algebra) and a B-spline | ||
is created for each dimension of the twist, using the corresponding element of the twists | ||
as the control point for the spline. | ||
|
||
For detailed information about B-splines, please see this wikipedia article. | ||
https://en.wikipedia.org/wiki/Non-uniform_rational_B-spline | ||
""" | ||
|
||
def __init__( | ||
self, | ||
control_poses: List[SE3], | ||
degree: int = 3, | ||
knots: Optional[List[float]] = None, | ||
) -> None: | ||
"""Construct BSplineSE3 object. The default arguments generate a cubic B-spline | ||
with uniformly spaced knots. | ||
|
||
- control_poses: list of SE3 objects that govern the shape of the spline. | ||
- degree: int that controls degree of the polynomial that governs any given point on the spline. | ||
- knots: list of floats that govern which control points are active during evaluating the spline | ||
at a given t input. | ||
""" | ||
|
||
self.control_poses = control_poses | ||
|
||
# a matrix where each row is a control pose as a twist | ||
# (so each column is a vector of control points for that dim of the twist) | ||
self.control_pose_matrix = np.vstack( | ||
[np.array(element.twist()) for element in control_poses] | ||
) | ||
|
||
self.degree = degree | ||
|
||
if knots is None: | ||
knots = np.linspace(0, 1, len(control_poses) - 2, endpoint=True) | ||
knots = np.append( | ||
[0.0] * degree, knots | ||
) # ensures the curve starts on the first control pose | ||
knots = np.append( | ||
knots, [1] * degree | ||
) # ensures the curve ends on the last control pose | ||
self.knots = knots | ||
|
||
self.splines = [ | ||
BSpline(knots, self.control_pose_matrix[:, i], degree) for i in range(0, 6) | ||
] | ||
|
||
def __call__(self, t: float) -> SE3: | ||
"""Returns pose of spline at t. | ||
|
||
t: Normalized time value [0,1] to evaluate the spline at. | ||
""" | ||
twist = np.hstack([spline(t) for spline in self.splines]) | ||
return SE3.Exp(twist) | ||
|
||
def visualize( | ||
self, | ||
num_samples: int, | ||
length: float = 1.0, | ||
repeat: bool = False, | ||
ax: Optional[plt.Axes] = None, | ||
kwargs_trplot: Dict[str, Any] = {"color": "green"}, | ||
kwargs_tranimate: Dict[str, Any] = {"wait": True}, | ||
kwargs_plot: Dict[str, Any] = {}, | ||
) -> None: | ||
"""Displays an animation of the trajectory with the control poses.""" | ||
out_poses = [self(t) for t in np.linspace(0, 1, num_samples)] | ||
x = [pose.x for pose in out_poses] | ||
y = [pose.y for pose in out_poses] | ||
z = [pose.z for pose in out_poses] | ||
|
||
if ax is None: | ||
fig = plt.figure(figsize=(10, 10)) | ||
ax = fig.add_subplot(projection="3d") | ||
|
||
trplot( | ||
[np.array(self.control_poses)], ax=ax, length=length, **kwargs_trplot | ||
) # plot control points | ||
ax.plot(x, y, z, **kwargs_plot) # plot x,y,z trajectory | ||
|
||
tranimate( | ||
out_poses, repeat=repeat, length=length, **kwargs_tranimate | ||
) # animate pose along trajectory |
This file contains hidden or 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,32 @@ | ||
import numpy.testing as nt | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import unittest | ||
import sys | ||
import pytest | ||
|
||
from spatialmath import BSplineSE3, SE3 | ||
|
||
|
||
class TestBSplineSE3(unittest.TestCase): | ||
control_poses = [ | ||
SE3.Trans([e, 2 * np.cos(e / 2 * np.pi), 2 * np.sin(e / 2 * np.pi)]) | ||
* SE3.Ry(e / 8 * np.pi) | ||
for e in range(0, 8) | ||
] | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
plt.close("all") | ||
|
||
def test_constructor(self): | ||
BSplineSE3(self.control_poses) | ||
|
||
def test_evaluation(self): | ||
spline = BSplineSE3(self.control_poses) | ||
nt.assert_almost_equal(spline(0).A, self.control_poses[0].A) | ||
nt.assert_almost_equal(spline(1).A, self.control_poses[-1].A) | ||
|
||
def test_visualize(self): | ||
spline = BSplineSE3(self.control_poses) | ||
spline.visualize(num_samples=100, repeat=False) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.