DHB-XR
SE(3) invariant trajectory encoding for robotics and VLA applications
DHB-XR (DHB Extended Representations) provides SE(3) invariant trajectory encoding for robotics and VLA (Vision-Language-Action) applications. The library implements various DHB (Double-Helix Based) methods for encoding trajectories into invariant representations that are independent of global position and orientation changes.
Research Background
This library implements the double-reflection (DHB-DR) and quaternion-relative (DHB-QR) invariant representations for rigid-body motion trajectories on SE(3), as described in the manuscript "Double-Reflection DHB Invariant Representation on SE(3)".
Author: Andy Park (andypark.purdue@gmail.com)
Features
- DHB-DR (Double-Reflection): Euler-based invariants (4 values per component)
- DHB-QR (Quaternion-Relative): Quaternion-based invariants (5 values per component), no gimbal lock
- DHB-TI (Time-Invariant): Speed-independent representations via arc-length reparameterization
- Trajectory adaptation: Resample and decode with new initial pose; optional batched optimization
- Tokenization: VQ-VAE / RVQ for DHB-Token (VLA action representation)
- Motion database: Storage, similarity (L2, DTW), and retrieval
- Imitation learning: Invariant matching, SE(3) geodesic, and hybrid losses
- Robust encoding: Handles reversals, zero-motion segments, and frame alignment issues
Quick start
from dhb_xr import encode_dhb_dr, decode_dhb_dr
from dhb_xr.core.types import DHBMethod, EncodingMethod
import numpy as np
# Create sample trajectory data
n = 50
positions = np.cumsum(np.random.randn(n, 3) * 0.01, axis=0)
quaternions = np.tile(np.array([1.0, 0.0, 0.0, 0.0]), (n, 1)) # identity orientation
# Encode trajectory to invariants
result = encode_dhb_dr(
positions, quaternions,
method=EncodingMethod.POSITION,
use_default_initial_frames=True,
dhb_method=DHBMethod.DOUBLE_REFLECTION
)
# Decode back to poses
decoded = decode_dhb_dr(
result["linear_motion_invariants"],
result["angular_motion_invariants"],
result["initial_pose"],
method=EncodingMethod.POSITION,
dhb_method=DHBMethod.DOUBLE_REFLECTION,
drop_padded=True
)
print(f"Original shape: {positions.shape}")
print(f"Decoded shape: {decoded['positions'].shape}")
Examples
See Examples for a walkthrough of the example scripts and how to run them.
VLA Integration
DHB-XR integrates with Vision-Language-Action (VLA) benchmarks:
- LIBERO: Load trajectories, encode to DHB invariants, run in simulation
- RoboCASA: HDF5 dataset support with motion retrieval
See VLA Integration Guide for setup and usage.
API Reference
dhb_xr
dhb_xr: DHB Extended Representations for SE(3) invariant trajectory encoding.
Classes
DHBMethod
Bases: Enum
DHB encoding method.
Source code in src/dhb_xr/core/types.py
EncodingMethod
Bases: Enum
Encoding method for initial frame computation.
Source code in src/dhb_xr/core/types.py
ProcessedTrajectory
dataclass
Result of trajectory preprocessing.
Source code in src/dhb_xr/preprocessing/trajectory_cleaner.py
Functions
get_inverse_alignment
Get rotation to undo alignment (for decoding).
TrajectoryDiagnostics
dataclass
Comprehensive trajectory quality report.
Source code in src/dhb_xr/preprocessing/diagnostics.py
Functions
summary
Return human-readable summary.
Source code in src/dhb_xr/preprocessing/diagnostics.py
TrajectoryPreprocessor
Prepare trajectories for DHB encoding.
Provides a configurable preprocessing pipeline to handle common issues in real-world trajectory data:
-
align_to_x: Rotate trajectory so initial motion aligns with +x. This ensures use_default_initial_frames=True works correctly.
-
smooth_reversals: Apply smoothing near detected 180° reversals. Prevents Euler singularities during encoding.
-
remove_zero_motion: Remove or interpolate zero-motion segments. Prevents undefined tangent frames.
-
gaussian_sigma: Overall Gaussian smoothing to reduce sensor noise.
Example
preprocessor = TrajectoryPreprocessor( ... align_to_x=True, ... smooth_reversals=True, ... gaussian_sigma=1.0, ... ) result = preprocessor.process(positions, quaternions)
Use result.positions, result.quaternions for encoding
After decoding, apply result.get_inverse_alignment() to restore original frame
Source code in src/dhb_xr/preprocessing/trajectory_cleaner.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 195 196 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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | |
Functions
__init__
__init__(
align_to_x=True,
smooth_reversals=True,
remove_zero_motion=True,
interpolate_zero_motion=True,
gaussian_sigma=0.0,
reversal_threshold=-0.5,
zero_motion_threshold=1e-06,
reversal_smoothing_radius=3,
)
Initialize preprocessor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
align_to_x
|
bool
|
Rotate trajectory to align initial direction with +x. |
True
|
smooth_reversals
|
bool
|
Apply local smoothing near 180° reversals. |
True
|
remove_zero_motion
|
bool
|
Handle zero-motion segments. |
True
|
interpolate_zero_motion
|
bool
|
If True, interpolate zero-motion segments. If False, remove them entirely. |
True
|
gaussian_sigma
|
float
|
Sigma for Gaussian smoothing (0 = no smoothing). |
0.0
|
reversal_threshold
|
float
|
Dot product threshold for reversal detection. |
-0.5
|
zero_motion_threshold
|
float
|
Distance threshold for zero motion. |
1e-06
|
reversal_smoothing_radius
|
int
|
Number of samples to smooth around reversals. |
3
|
Source code in src/dhb_xr/preprocessing/trajectory_cleaner.py
align_to_initial_direction
Rotate trajectory so initial motion aligns with target direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
(N, 3) position trajectory. |
required |
quaternions
|
ndarray
|
(N, 4) wxyz quaternion trajectory. |
required |
target_dir
|
ndarray
|
Target direction (default: +x = [1,0,0]). |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray, ndarray]
|
Tuple of (aligned_positions, aligned_quaternions, rotation_matrix). |
Source code in src/dhb_xr/preprocessing/trajectory_cleaner.py
detect_reversals
Find indices where 180° reversals occur.
detect_zero_motion
Find zero-motion segments as (start, end) tuples.
process
Run full preprocessing pipeline.
Order of operations: 1. Analyze original trajectory 2. Handle zero-motion segments (remove or interpolate) 3. Smooth reversals (if detected) 4. Apply Gaussian smoothing (if sigma > 0) 5. Align to +x direction 6. Analyze final trajectory
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
(N, 3) position trajectory. |
required |
quaternions
|
ndarray
|
(N, 4) wxyz quaternion trajectory. |
required |
return_diagnostics
|
bool
|
Include before/after diagnostics. |
True
|
Returns:
| Type | Description |
|---|---|
ProcessedTrajectory
|
ProcessedTrajectory with cleaned data and metadata. |
Source code in src/dhb_xr/preprocessing/trajectory_cleaner.py
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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
smooth_trajectory
Apply Gaussian smoothing to trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
(N, 3) position trajectory. |
required |
quaternions
|
ndarray
|
(N, 4) quaternion trajectory. |
required |
sigma
|
float
|
Gaussian sigma parameter. |
1.0
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
Smoothed (positions, quaternions). |
Source code in src/dhb_xr/preprocessing/trajectory_cleaner.py
Functions
analyze_trajectory
analyze_trajectory(
positions, quaternions=None, reversal_threshold=-0.5, zero_motion_threshold=1e-06
)
Comprehensive trajectory quality analysis.
Detects potential issues that could cause DHB encoding problems: - 180° direction reversals (cause Euler singularities) - Zero-motion segments (cause undefined tangent frames) - Misaligned initial direction (reconstruction error with default frames)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
(N, 3) position trajectory. |
required |
quaternions
|
Optional[ndarray]
|
Optional (N, 4) wxyz quaternion trajectory. |
None
|
reversal_threshold
|
float
|
Dot product threshold for reversal detection. |
-0.5
|
zero_motion_threshold
|
float
|
Distance threshold for zero motion. |
1e-06
|
Returns:
| Type | Description |
|---|---|
TrajectoryDiagnostics
|
TrajectoryDiagnostics with analysis results and recommendations. |
Source code in src/dhb_xr/preprocessing/diagnostics.py
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
decode_dhb_dr
decode_dhb_dr(
linear_motion_invariants,
angular_motion_invariants,
initial_pose,
method=EncodingMethod.POSITION,
dhb_method=DHBMethod.DOUBLE_REFLECTION,
drop_padded=True,
robust_mode=False,
validate_initial_pose=False,
return_diagnostics=False,
)
Reconstruct poses from DHB invariants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
linear_motion_invariants
|
ndarray
|
(N, k) linear invariants |
required |
angular_motion_invariants
|
ndarray
|
(N, k) angular invariants |
required |
initial_pose
|
Dict[str, ndarray]
|
{'position': (3,), 'quaternion': (4,) wxyz} |
required |
method
|
EncodingMethod
|
'pos' or 'vel' |
POSITION
|
dhb_method
|
DHBMethod
|
DHBMethod.ORIGINAL (3 inv) or DHBMethod.DOUBLE_REFLECTION (4 inv) |
DOUBLE_REFLECTION
|
drop_padded
|
bool
|
if True, return positions/quaternions[2:] to match encoder padding |
True
|
robust_mode
|
bool
|
Enable robustness features (NaN handling, normalization) |
False
|
validate_initial_pose
|
bool
|
Check initial pose validity before decoding |
False
|
return_diagnostics
|
bool
|
Include decoding diagnostics in output |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
dict with 'positions' (N,3), 'quaternions' (N,4) wxyz, |
Dict[str, Any]
|
and optionally 'diagnostics' if return_diagnostics=True. |
Source code in src/dhb_xr/decoder/dhb_dr.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 195 196 197 198 199 200 201 202 203 204 205 206 | |
detect_reversals
Detect indices where motion direction reverses significantly.
A reversal is detected when consecutive tangent vectors have a dot product below the threshold (e.g., -0.5 means > 120° turn).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
(N, 3) position trajectory. |
required |
threshold
|
float
|
Dot product threshold for reversal detection. -1.0 = exact 180° reversal only -0.5 = 120° or larger turn 0.0 = 90° or larger turn |
-0.5
|
Returns:
| Type | Description |
|---|---|
Tuple[List[int], float]
|
Tuple of (reversal_indices, min_dot_product). |
Source code in src/dhb_xr/preprocessing/diagnostics.py
detect_zero_motion
Detect contiguous segments with zero or near-zero motion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
(N, 3) position trajectory. |
required |
threshold
|
float
|
Distance threshold for zero motion. |
1e-06
|
min_segment_length
|
int
|
Minimum number of consecutive frames to report. |
1
|
Returns:
| Type | Description |
|---|---|
List[Tuple[int, int]]
|
List of (start_index, end_index) tuples for zero-motion segments. |
Source code in src/dhb_xr/preprocessing/diagnostics.py
encode_dhb_dr
encode_dhb_dr(
positions,
quaternions,
init_pose=None,
method="pos",
use_default_initial_frames=True,
dhb_method=DHBMethod.DOUBLE_REFLECTION,
robust_mode=False,
reversal_threshold=-0.9,
zero_motion_threshold=1e-06,
validate_frames=False,
return_diagnostics=False,
)
Compute DHB invariants (original or double-reflection).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
(N, 3) position trajectory |
required |
quaternions
|
ndarray
|
(N, 4) wxyz quaternion trajectory |
required |
init_pose
|
Optional[Dict[str, ndarray]]
|
optional {'position': (3,), 'quaternion': (4,) wxyz} |
None
|
method
|
str
|
EncodingMethod for position or velocity-based encoding |
'pos'
|
use_default_initial_frames
|
bool
|
if True, pad and use identity-like frames |
True
|
dhb_method
|
DHBMethod
|
DHBMethod.ORIGINAL (3 inv) or DHBMethod.DOUBLE_REFLECTION (4 inv) |
DOUBLE_REFLECTION
|
robust_mode
|
bool
|
Enable robustness features (reversal detection, zero-motion handling) |
False
|
reversal_threshold
|
float
|
Dot product threshold for 180° detection (default -0.9) |
-0.9
|
zero_motion_threshold
|
float
|
Threshold for zero-motion detection |
1e-06
|
validate_frames
|
bool
|
Check frame orthogonality at each step |
False
|
return_diagnostics
|
bool
|
Include encoding diagnostics in output |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
dict with linear_motion_invariants, angular_motion_invariants, |
Dict[str, Any]
|
linear_frame_initial, angular_frame_initial, initial_pose, |
Dict[str, Any]
|
and optionally 'diagnostics' if return_diagnostics=True. |
Source code in src/dhb_xr/encoder/dhb_dr.py
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
plot_invariants
Plot linear and angular invariant time series.
Source code in src/dhb_xr/visualization/plot.py
plot_se3_trajectories
plot_se3_trajectories(
trajectories,
ax=None,
show_orientation=True,
vis_type="cube",
box_size_scale=0.05,
num_frames=6,
title="SE(3) Trajectories",
show_legend=True,
)
Plot multiple SE(3) trajectories with orientation visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trajectories
|
Union[List[Dict], Dict[str, Dict]]
|
List of dicts with 'positions' and 'quaternions', or dict of name -> trajectory |
required |
ax
|
Optional[Any]
|
Matplotlib 3D axes |
None
|
show_orientation
|
bool
|
Show orientation cubes/frames |
True
|
vis_type
|
str
|
"cube" or "arrow" |
'cube'
|
box_size_scale
|
float
|
Size of cubes |
0.05
|
num_frames
|
int
|
Number of orientation samples per trajectory |
6
|
title
|
str
|
Plot title |
'SE(3) Trajectories'
|
show_legend
|
bool
|
Show legend |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
Matplotlib axes |
Source code in src/dhb_xr/visualization/plot.py
plot_se3_trajectory
plot_se3_trajectory(
positions,
quaternions=None,
ax=None,
title="SE(3) trajectory",
show_orientation=False,
vis_type="arrow",
box_size=(0.03, 0.03, 0.03),
color="b",
alpha=0.7,
num_frames=8,
label=None,
)
Plot 3D position trajectory with optional orientation visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
(N, 3) position array |
required |
quaternions
|
Optional[ndarray]
|
(N, 4) quaternion array (wxyz), optional |
None
|
ax
|
Optional[Any]
|
Matplotlib 3D axes, created if None |
None
|
title
|
str
|
Plot title |
'SE(3) trajectory'
|
show_orientation
|
bool
|
If True, show orientation frames or cubes |
False
|
vis_type
|
str
|
"arrow" for coordinate frames, "cube" for oriented boxes |
'arrow'
|
box_size
|
tuple
|
Size of cubes (if vis_type="cube") |
(0.03, 0.03, 0.03)
|
color
|
str
|
Trajectory line color |
'b'
|
alpha
|
float
|
Transparency for cubes |
0.7
|
num_frames
|
int
|
Number of orientation samples to show along trajectory |
8
|
label
|
Optional[str]
|
Legend label for trajectory |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Matplotlib axes |
Source code in src/dhb_xr/visualization/plot.py
License
MIT License.
Copyright (c) 2026 Andy Park andypark.purdue@gmail.com