interpolation
Trajectory initialization utilities for generating initial guesses.
This module provides interpolation methods for constructing initial trajectory guesses from keyframe values at specified nodes. Keyframes define values at specific nodes, and the interpolation fills in values for all intermediate nodes.
linspace(keyframes: Sequence[Keyframe], nodes: Sequence[int]) -> np.ndarray
¶
Generate trajectory guess via piecewise linear interpolation between keyframes.
Keyframes specify values at particular nodes, and linear interpolation fills in the values for all nodes in between. This generalizes np.linspace to support multiple waypoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keyframes
|
Sequence[Keyframe]
|
Sequence of values at keyframe nodes. Each keyframe should be array-like with the same shape, or a scalar. |
required |
nodes
|
Sequence[int]
|
Sequence of node indices where keyframes occur. Must be sorted in ascending order and have the same length as keyframes. The last node determines the output size (N = nodes[-1] + 1). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray of shape (N, *keyframe_shape) containing interpolated values |
ndarray
|
for all nodes, where N = nodes[-1] + 1. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If keyframes and nodes have different lengths, or nodes are not sorted. |
Note
The first node should typically be 0 and the last node should be N-1 (where N is your trajectory length). Nodes before the first keyframe or after the last will be left as zeros.
Example
Interpolate position through three waypoints::
import openscvx as ox
position_guess = ox.init.linspace(
keyframes=[[0, 0, 0], [5, 10, 5], [10, 0, 0]],
nodes=[0, 25, 49],
) # Returns shape (50, 3)
position.guess = position_guess
Simple start-to-end interpolation (equivalent to np.linspace)::
position_guess = ox.init.linspace(
keyframes=[[0, 0, 0], [10, 0, 0]],
nodes=[0, 49],
) # Returns shape (50, 3)
Source code in openscvx/init/interpolation.py
nlerp(keyframes: Sequence[Quaternion], nodes: Sequence[int]) -> np.ndarray
¶
Generate quaternion trajectory guess via normalized linear interpolation.
NLERP performs linear interpolation between quaternions followed by normalization. This is faster than SLERP but produces non-constant angular velocity. For initial guesses this is typically sufficient.
Quaternion convention: [w, x, y, z] (scalar-first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keyframes
|
Sequence[Quaternion]
|
Sequence of unit quaternions at keyframe nodes. Each quaternion should be array-like with shape (4,) in [w, x, y, z] order. |
required |
nodes
|
Sequence[int]
|
Sequence of node indices where keyframes occur. Must be sorted in ascending order and have the same length as keyframes. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray of shape (N, 4) containing normalized interpolated quaternions |
ndarray
|
for all nodes, where N = nodes[-1] + 1. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If keyframes and nodes have different lengths, nodes are not sorted, or quaternions don't have shape (4,). |
Note
The first node should typically be 0 and the last node should be N-1. Nodes outside the keyframe range will be left as zeros.
Example
Interpolate attitude from identity to 180-degree rotation about z-axis::
import openscvx as ox
attitude_guess = ox.init.nlerp(
keyframes=[
[1, 0, 0, 0], # Identity quaternion
[0, 0, 0, 1], # 180 deg about z
],
nodes=[0, 49],
) # Returns shape (50, 4)
attitude.guess = attitude_guess
Interpolate through intermediate attitudes::
attitude_guess = ox.init.nlerp(
keyframes=[
[1, 0, 0, 0], # Identity
[0.707, 0, 0, 0.707], # 90 deg about z
[0, 0, 0, 1], # 180 deg about z
],
nodes=[0, 25, 49],
)
Source code in openscvx/init/interpolation.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
slerp(keyframes: Sequence[Quaternion], nodes: Sequence[int]) -> np.ndarray
¶
Generate quaternion trajectory guess via spherical linear interpolation.
SLERP interpolates along the great arc on the unit quaternion sphere, producing constant angular velocity between keyframes. This is more accurate than NLERP but slightly more expensive to compute.
Quaternion convention: [w, x, y, z] (scalar-first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keyframes
|
Sequence[Quaternion]
|
Sequence of unit quaternions at keyframe nodes. Each quaternion should be array-like with shape (4,) in [w, x, y, z] order. |
required |
nodes
|
Sequence[int]
|
Sequence of node indices where keyframes occur. Must be sorted in ascending order and have the same length as keyframes. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray of shape (N, 4) containing interpolated quaternions for all nodes, |
ndarray
|
where N = nodes[-1] + 1. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If keyframes and nodes have different lengths, nodes are not sorted, or quaternions don't have shape (4,). |
Note
The first node should typically be 0 and the last node should be N-1. Nodes outside the keyframe range will be left as zeros.
Example
Interpolate attitude with constant angular velocity::
import openscvx as ox
attitude_guess = ox.init.slerp(
keyframes=[
[1, 0, 0, 0], # Identity quaternion
[0, 0, 0, 1], # 180 deg about z
],
nodes=[0, 49],
) # Returns shape (50, 4)
attitude.guess = attitude_guess
Source code in openscvx/init/interpolation.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |