Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
TRAJECTORY: cubic Hermite spline dead reckoning and movement simulation for 2D/3D

Data Structures

struct  TRAJECTORY
 structure holding all data for trajectory interpolation / extrapolation More...
 
struct  TRAJECTORY_POINT
 a single waypoint in a multi-point trajectory path More...
 

Macros

#define TRAJECTORY_2D   2
 use 2 components (x,y) for trajectory computation
 
#define TRAJECTORY_3D   3
 use 3 components (x,y,z) for trajectory computation
 
#define TRAJECTORY_BEFORE   3
 trajectory is extrapolating before the start state using quadratic motion
 
#define TRAJECTORY_EXTRAP   2
 trajectory is extrapolating beyond the end state using quadratic motion
 
#define TRAJECTORY_INTERP   1
 trajectory is interpolating along the cubic Hermite spline between start and end
 

Functions

int trajectory_add_point (TRAJECTORY *traj, const PHYSICS *state, double time_val)
 add a waypoint to the multi-point path (times must be strictly increasing)
 
int trajectory_clear_points (TRAJECTORY *traj)
 clear all waypoints from the multi-point path
 
int trajectory_compute (TRAJECTORY *traj, double time_val)
 compute full state (position, speed, acceleration, orientation) at given time
 
void trajectory_delete (TRAJECTORY **traj)
 free a trajectory
 
double trajectory_distance (TRAJECTORY *traj, double time_a, double time_b, int steps)
 distance along the trajectory between two times
 
int trajectory_get_acceleration (TRAJECTORY *traj, double time_val, VECTOR3D out)
 compute only acceleration at given time, stores result in out
 
int trajectory_get_orientation (TRAJECTORY *traj, double time_val, VECTOR3D out)
 compute only orientation at given time, stores result in out
 
int trajectory_get_position (TRAJECTORY *traj, double time_val, VECTOR3D out)
 compute only position at given time, stores result in out
 
int trajectory_get_speed (TRAJECTORY *traj, double time_val, VECTOR3D out)
 compute only speed at given time, stores result in out
 
TRAJECTORYtrajectory_new (int nb_components)
 allocate a new trajectory for 2D or 3D use
 
int trajectory_set_end (TRAJECTORY *traj, const PHYSICS *state, double time_val)
 set the terminal state and time of the trajectory, recomputes Hermite tangents
 
int trajectory_set_start (TRAJECTORY *traj, const PHYSICS *state, double time_val)
 set the initial state and time of the trajectory
 
int trajectory_update (TRAJECTORY *traj, const PHYSICS *new_end, double time_val)
 shift: old end becomes new start, set a new end state (for continuous dead reckoning updates)
 

Detailed Description


Data Structure Documentation

◆ TRAJECTORY

struct TRAJECTORY

structure holding all data for trajectory interpolation / extrapolation

Examples
ex_iso_astar.c, and ex_trajectory.c.

Definition at line 82 of file n_trajectory.h.

+ Collaboration diagram for TRAJECTORY:
Data Fields
PHYSICS current current computed state at current_time
int current_segment index of the currently loaded segment (points[i] -> points[i+1]), -1 if none
double current_time last computed time
double duration duration = end_time - start_time
PHYSICS end state at trajectory end (terminal known state)
double end_time timestamp of end state
VECTOR3D m0 Hermite tangent at start for position (start.speed * duration)
VECTOR3D m0_orient Hermite tangent at start for orientation (start.angular_speed * duration)
VECTOR3D m1 Hermite tangent at end for position (end.speed * duration)
VECTOR3D m1_orient Hermite tangent at end for orientation (end.angular_speed * duration)
int mode current computation mode: TRAJECTORY_INTERP, TRAJECTORY_EXTRAP, or TRAJECTORY_BEFORE
int nb_components number of components to process: TRAJECTORY_2D (2) or TRAJECTORY_3D (3)
int nb_points number of waypoints in the points array
int nb_points_allocated allocated capacity of the points array
TRAJECTORY_POINT * points array of waypoints for multi-point paths (NULL if single segment)
PHYSICS start state at trajectory start (initial known state)
double start_time timestamp of start state

◆ TRAJECTORY_POINT

struct TRAJECTORY_POINT

a single waypoint in a multi-point trajectory path

Definition at line 74 of file n_trajectory.h.

+ Collaboration diagram for TRAJECTORY_POINT:
Data Fields
PHYSICS state physics state (position, speed, etc.) at this waypoint
double time_val timestamp of this waypoint

Macro Definition Documentation

◆ TRAJECTORY_2D

#define TRAJECTORY_2D   2

use 2 components (x,y) for trajectory computation

Examples
ex_iso_astar.c, and ex_trajectory.c.

Definition at line 69 of file n_trajectory.h.

◆ TRAJECTORY_3D

#define TRAJECTORY_3D   3

use 3 components (x,y,z) for trajectory computation

Examples
ex_trajectory.c.

Definition at line 71 of file n_trajectory.h.

◆ TRAJECTORY_BEFORE

#define TRAJECTORY_BEFORE   3

trajectory is extrapolating before the start state using quadratic motion

Examples
ex_trajectory.c.

Definition at line 66 of file n_trajectory.h.

◆ TRAJECTORY_EXTRAP

#define TRAJECTORY_EXTRAP   2

trajectory is extrapolating beyond the end state using quadratic motion

Examples
ex_trajectory.c.

Definition at line 64 of file n_trajectory.h.

◆ TRAJECTORY_INTERP

#define TRAJECTORY_INTERP   1

trajectory is interpolating along the cubic Hermite spline between start and end

Examples
ex_trajectory.c.

Definition at line 62 of file n_trajectory.h.

Function Documentation

◆ trajectory_add_point()

int trajectory_add_point ( TRAJECTORY traj,
const PHYSICS state,
double  time_val 
)

add a waypoint to the multi-point path (times must be strictly increasing)

add a waypoint to the multi-point path (times must be strictly increasing)

Waypoints must be added in strictly increasing chronological order. When 2+ waypoints exist, trajectory_compute and trajectory_get_* functions automatically find and interpolate the correct segment.

Parameters
trajThe trajectory
stateThe physics state at this waypoint
time_valThe timestamp of this waypoint (must be > previous point's time)
Returns
TRUE or FALSE

Definition at line 616 of file n_trajectory.c.

References __n_assert, current_segment, LOG_ERR, n_log, nb_points, nb_points_allocated, points, Realloc, TRAJECTORY_POINT::state, TRAJECTORY_POINT::time_val, and trajectory_load_segment().

Referenced by demo_dead_reckoning(), init_3d(), and rebuild_2d_trajectory().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ trajectory_clear_points()

int trajectory_clear_points ( TRAJECTORY traj)

clear all waypoints from the multi-point path

clear all waypoints from the multi-point path

Keeps the allocated memory for reuse. Resets start/end/duration.

Parameters
trajThe trajectory
Returns
TRUE or FALSE

Definition at line 655 of file n_trajectory.c.

References __n_assert, current_segment, duration, end, end_time, nb_points, start, and start_time.

Referenced by rebuild_2d_trajectory().

+ Here is the caller graph for this function:

◆ trajectory_compute()

int trajectory_compute ( TRAJECTORY traj,
double  time_val 
)

compute full state (position, speed, acceleration, orientation) at given time

compute full state (position, speed, acceleration, orientation) at given time

Parameters
trajThe trajectory
time_valThe time at which to compute the state
Returns
TRUE or FALSE

Definition at line 329 of file n_trajectory.c.

References __n_assert, PHYSICS::acceleration, PHYSICS::angular_acceleration, PHYSICS::angular_speed, current, current_time, duration, end, end_time, hermite_d2h00(), hermite_d2h01(), hermite_d2h10(), hermite_d2h11(), hermite_dh00(), hermite_dh01(), hermite_dh10(), hermite_dh11(), hermite_h00(), hermite_h01(), hermite_h10(), hermite_h11(), m0, m0_orient, m1, m1_orient, mode, nb_components, PHYSICS::orientation, PHYSICS::position, PHYSICS::speed, start, start_time, TRAJECTORY_BEFORE, TRAJECTORY_EXTRAP, TRAJECTORY_INTERP, and trajectory_load_segment().

Referenced by draw_2d_panel(), draw_3d_panel(), main(), and update_3d_logic().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ trajectory_delete()

void trajectory_delete ( TRAJECTORY **  traj)

free a trajectory

free a trajectory

Parameters
trajPointer to the TRAJECTORY pointer to free

Definition at line 234 of file n_trajectory.c.

References __n_assert, and Free.

Referenced by demo_dead_reckoning(), main(), and reset_2d().

+ Here is the caller graph for this function:

◆ trajectory_distance()

double trajectory_distance ( TRAJECTORY traj,
double  time_a,
double  time_b,
int  steps 
)

distance along the trajectory between two times

distance along the trajectory between two times

Parameters
trajThe trajectory
time_aStart time
time_bEnd time
stepsNumber of integration steps (higher = more precise, 100 is usually enough)
Returns
The approximate distance, or -1.0 on error

Definition at line 678 of file n_trajectory.c.

References __n_assert, nb_components, and trajectory_get_position().

+ Here is the call graph for this function:

◆ trajectory_get_acceleration()

int trajectory_get_acceleration ( TRAJECTORY traj,
double  time_val,
VECTOR3D  out 
)

compute only acceleration at given time, stores result in out

compute only acceleration at given time, stores result in out

Parameters
trajThe trajectory
time_valThe time at which to compute
outVECTOR3D to store the result
Returns
TRUE or FALSE

Definition at line 526 of file n_trajectory.c.

References __n_assert, PHYSICS::acceleration, duration, end, end_time, hermite_d2h00(), hermite_d2h01(), hermite_d2h10(), hermite_d2h11(), m0, m1, nb_components, PHYSICS::position, start, start_time, and trajectory_load_segment().

+ Here is the call graph for this function:

◆ trajectory_get_orientation()

int trajectory_get_orientation ( TRAJECTORY traj,
double  time_val,
VECTOR3D  out 
)

compute only orientation at given time, stores result in out

compute only orientation at given time, stores result in out

Parameters
trajThe trajectory
time_valThe time at which to compute
outVECTOR3D to store the result
Returns
TRUE or FALSE

Definition at line 568 of file n_trajectory.c.

References __n_assert, PHYSICS::angular_acceleration, PHYSICS::angular_speed, duration, end, end_time, hermite_h00(), hermite_h01(), hermite_h10(), hermite_h11(), m0_orient, m1_orient, nb_components, PHYSICS::orientation, start, start_time, and trajectory_load_segment().

+ Here is the call graph for this function:

◆ trajectory_get_position()

int trajectory_get_position ( TRAJECTORY traj,
double  time_val,
VECTOR3D  out 
)

compute only position at given time, stores result in out

compute only position at given time, stores result in out

This is a lightweight version of trajectory_compute that only computes position.

Parameters
trajThe trajectory
time_valThe time at which to compute
outVECTOR3D to store the result
Returns
TRUE or FALSE

Definition at line 435 of file n_trajectory.c.

References __n_assert, PHYSICS::acceleration, duration, end, end_time, hermite_h00(), hermite_h01(), hermite_h10(), hermite_h11(), m0, m1, nb_components, PHYSICS::position, PHYSICS::speed, start, start_time, and trajectory_load_segment().

Referenced by demo_dead_reckoning(), draw_full_path_2d(), draw_full_path_3d(), and trajectory_distance().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ trajectory_get_speed()

int trajectory_get_speed ( TRAJECTORY traj,
double  time_val,
VECTOR3D  out 
)

compute only speed at given time, stores result in out

compute only speed at given time, stores result in out

Parameters
trajThe trajectory
time_valThe time at which to compute
outVECTOR3D to store the result
Returns
TRUE or FALSE

Definition at line 480 of file n_trajectory.c.

References __n_assert, PHYSICS::acceleration, duration, end, end_time, hermite_dh00(), hermite_dh01(), hermite_dh10(), hermite_dh11(), m0, m1, nb_components, PHYSICS::position, PHYSICS::speed, start, start_time, and trajectory_load_segment().

Referenced by demo_dead_reckoning().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ trajectory_new()

TRAJECTORY * trajectory_new ( int  nb_components)

allocate a new trajectory for 2D or 3D use

allocate a new trajectory for 2D or 3D use

Parameters
nb_componentsTRAJECTORY_2D (2) or TRAJECTORY_3D (3)
Returns
A new TRAJECTORY or NULL on error

Definition at line 206 of file n_trajectory.c.

References __n_assert, current_segment, current_time, duration, end_time, LOG_ERR, Malloc, mode, n_log, nb_components, nb_points, nb_points_allocated, points, start_time, TRAJECTORY_2D, TRAJECTORY_3D, and TRAJECTORY_BEFORE.

Referenced by demo_dead_reckoning(), init_3d(), and reset_2d().

+ Here is the caller graph for this function:

◆ trajectory_set_end()

int trajectory_set_end ( TRAJECTORY traj,
const PHYSICS state,
double  time_val 
)

set the terminal state and time of the trajectory, recomputes Hermite tangents

set the terminal state and time of the trajectory, recomputes Hermite tangents

Recomputes the Hermite tangent vectors.

Parameters
trajThe trajectory to configure
stateThe physics state at the end point
time_valThe timestamp of this state (must be > start_time)
Returns
TRUE or FALSE

Definition at line 270 of file n_trajectory.c.

References __n_assert, duration, end, end_time, LOG_ERR, n_log, start_time, and trajectory_compute_tangents().

+ Here is the call graph for this function:

◆ trajectory_set_start()

int trajectory_set_start ( TRAJECTORY traj,
const PHYSICS state,
double  time_val 
)

set the initial state and time of the trajectory

set the initial state and time of the trajectory

Parameters
trajThe trajectory to configure
stateThe physics state at the start point
time_valThe timestamp of this state
Returns
TRUE or FALSE

Definition at line 247 of file n_trajectory.c.

References __n_assert, duration, end_time, start, start_time, and trajectory_compute_tangents().

+ Here is the call graph for this function:

◆ trajectory_update()

int trajectory_update ( TRAJECTORY traj,
const PHYSICS new_end,
double  time_val 
)

shift: old end becomes new start, set a new end state (for continuous dead reckoning updates)

shift: old end becomes new start, set a new end state (for continuous dead reckoning updates)

The previous end state becomes the new start state, and the new state becomes the end. This is the core function for continuous dead reckoning: call it each time a new network update arrives to create a smooth spline from the current endpoint to the new one.

Parameters
trajThe trajectory to update
new_endThe new physics state received (e.g. from a network packet)
time_valThe timestamp of the new state
Returns
TRUE or FALSE

Definition at line 298 of file n_trajectory.c.

References __n_assert, duration, end, end_time, LOG_ERR, n_log, start, start_time, and trajectory_compute_tangents().

+ Here is the call graph for this function: