51 return 2.0 * s * s * s - 3.0 * s * s + 1.0;
60 return s * s * s - 2.0 * s * s + s;
69 return -2.0 * s * s * s + 3.0 * s * s;
78 return s * s * s - s * s;
87 return 6.0 * s * s - 6.0 * s;
96 return 3.0 * s * s - 4.0 * s + 1.0;
105 return -6.0 * s * s + 6.0 * s;
114 return 3.0 * s * s - 2.0 * s;
123 return 12.0 * s - 6.0;
132 return 6.0 * s - 4.0;
141 return -12.0 * s + 6.0;
150 return 6.0 * s - 2.0;
178 if (seg < 0) seg = 0;
181 while (seg < traj->nb_points - 2 &&
186 while (seg > 0 && time_val < traj->points[seg].time_val) {
208 n_log(
LOG_ERR,
"nb_components must be TRAJECTORY_2D (2) or TRAJECTORY_3D (3), got %d", nb_components);
236 Free((*traj)->points);
302 if (time_val <= traj->end_time) {
303 n_log(
LOG_ERR,
"new time (%g) must be greater than current end_time (%g)", time_val, traj->
end_time);
343 if (time_val < traj->start_time) {
347 for (
int i = 0; i < nc; i++) {
355 }
else if (time_val > traj->
end_time) {
358 double dt = time_val - traj->
end_time;
359 for (
int i = 0; i < nc; i++) {
390 double inv_dur = 1.0 / traj->
duration;
391 double inv_dur2 = inv_dur * inv_dur;
393 for (
int i = 0; i < nc; i++) {
415 for (
int i = nc; i < 3; i++) {
443 for (
int i = 0; i < nc; i++) out[i] = traj->
start.
position[i];
444 for (
int i = nc; i < 3; i++) out[i] = 0.0;
448 if (time_val < traj->start_time) {
450 for (
int i = 0; i < nc; i++) {
453 }
else if (time_val > traj->
end_time) {
454 double dt = time_val - traj->
end_time;
455 for (
int i = 0; i < nc; i++) {
464 for (
int i = 0; i < nc; i++) {
469 for (
int i = nc; i < 3; i++) out[i] = 0.0;
488 for (
int i = 0; i < nc; i++) out[i] = traj->
start.
speed[i];
489 for (
int i = nc; i < 3; i++) out[i] = 0.0;
493 if (time_val < traj->start_time) {
495 for (
int i = 0; i < nc; i++) {
498 }
else if (time_val > traj->
end_time) {
499 double dt = time_val - traj->
end_time;
500 for (
int i = 0; i < nc; i++) {
505 double inv_dur = 1.0 / traj->
duration;
510 for (
int i = 0; i < nc; i++) {
515 for (
int i = nc; i < 3; i++) out[i] = 0.0;
535 for (
int i = nc; i < 3; i++) out[i] = 0.0;
539 if (time_val < traj->start_time || time_val > traj->
end_time) {
542 for (
int i = 0; i < nc; i++) {
552 for (
int i = 0; i < nc; i++) {
557 for (
int i = nc; i < 3; i++) out[i] = 0.0;
577 for (
int i = nc; i < 3; i++) out[i] = 0.0;
581 if (time_val < traj->start_time) {
583 for (
int i = 0; i < nc; i++) {
586 }
else if (time_val > traj->
end_time) {
587 double dt = time_val - traj->
end_time;
588 for (
int i = 0; i < nc; i++) {
597 for (
int i = 0; i < nc; i++) {
602 for (
int i = nc; i < 3; i++) out[i] = 0.0;
622 n_log(
LOG_ERR,
"time_val (%g) must be greater than last point time (%g)",
681 if (steps < 1) steps = 1;
684 double dt = (time_b - time_a) / (
double)steps;
690 for (
int step = 1; step <= steps; step++) {
691 double t = time_a + dt * (double)step;
695 for (
int i = 0; i < nc; i++) {
696 double d = curr[i] - prev[i];
701 for (
int i = 0; i < 3; i++) prev[i] = curr[i];
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define Realloc(__ptr, __struct, __size)
Realloc Handler to get errors.
#define Free(__ptr)
Free Handler to get errors.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
VECTOR3D speed
vx,vy,vz actual speed
VECTOR3D orientation
ax,ay,az actual rotation position
VECTOR3D position
x,y,z actual position
VECTOR3D acceleration
ax,ay,az actual acceleration
VECTOR3D angular_acceleration
rax,ray,raz actual angular acceleration
VECTOR3D angular_speed
rvx,rvy,rvz actual angular speed
double VECTOR3D[3]
struct of a point
structure of the physics of an object
VECTOR3D m0_orient
Hermite tangent at start for orientation (start.angular_speed * duration)
PHYSICS current
current computed state at current_time
VECTOR3D m0
Hermite tangent at start for position (start.speed * duration)
VECTOR3D m1_orient
Hermite tangent at end for orientation (end.angular_speed * duration)
int nb_points_allocated
allocated capacity of the points array
int nb_points
number of waypoints in the points array
double current_time
last computed time
PHYSICS end
state at trajectory end (terminal known state)
double time_val
timestamp of this waypoint
double duration
duration = end_time - start_time
TRAJECTORY_POINT * points
array of waypoints for multi-point paths (NULL if single segment)
int current_segment
index of the currently loaded segment (points[i] -> points[i+1]), -1 if none
PHYSICS state
physics state (position, speed, etc.) at this waypoint
int mode
current computation mode: TRAJECTORY_INTERP, TRAJECTORY_EXTRAP, or TRAJECTORY_BEFORE
VECTOR3D m1
Hermite tangent at end for position (end.speed * duration)
PHYSICS start
state at trajectory start (initial known state)
int nb_components
number of components to process: TRAJECTORY_2D (2) or TRAJECTORY_3D (3)
double start_time
timestamp of start state
double end_time
timestamp of end state
int trajectory_get_position(TRAJECTORY *traj, double time_val, VECTOR3D out)
Compute position at a given time.
TRAJECTORY * trajectory_new(int nb_components)
Allocate and initialize a new TRAJECTORY.
int trajectory_set_start(TRAJECTORY *traj, const PHYSICS *state, double time_val)
Set the initial (start) state and time of the trajectory.
int trajectory_add_point(TRAJECTORY *traj, const PHYSICS *state, double time_val)
Add a waypoint to the multi-point trajectory path.
#define TRAJECTORY_EXTRAP
trajectory is extrapolating beyond the end state using quadratic motion
void trajectory_delete(TRAJECTORY **traj)
Free a TRAJECTORY and set the pointer to NULL.
int trajectory_get_orientation(TRAJECTORY *traj, double time_val, VECTOR3D out)
Compute orientation at a given time.
#define TRAJECTORY_3D
use 3 components (x,y,z) for trajectory computation
#define TRAJECTORY_BEFORE
trajectory is extrapolating before the start state using quadratic motion
#define TRAJECTORY_2D
use 2 components (x,y) for trajectory computation
int trajectory_get_acceleration(TRAJECTORY *traj, double time_val, VECTOR3D out)
Compute acceleration at a given time.
#define TRAJECTORY_INTERP
trajectory is interpolating along the cubic Hermite spline between start and end
int trajectory_compute(TRAJECTORY *traj, double time_val)
Compute the full state (position, speed, acceleration, orientation, angular_speed) at a given time us...
int trajectory_set_end(TRAJECTORY *traj, const PHYSICS *state, double time_val)
Set the terminal (end) state and time of the trajectory.
int trajectory_get_speed(TRAJECTORY *traj, double time_val, VECTOR3D out)
Compute velocity at a given time.
int trajectory_clear_points(TRAJECTORY *traj)
Clear all waypoints from the multi-point path.
int trajectory_update(TRAJECTORY *traj, const PHYSICS *new_end, double time_val)
Update the trajectory with a new end state (dead reckoning shift).
double trajectory_distance(TRAJECTORY *traj, double time_a, double time_b, int steps)
Compute the approximate arc length (distance traveled) along the trajectory between two times,...
structure holding all data for trajectory interpolation / extrapolation
a single waypoint in a multi-point trajectory path
static double hermite_d2h01(double s)
Second derivative of h01: d2h01/ds2 = -12s + 6.
static double hermite_h00(double s)
Hermite basis function h00: weights the start position.
static double hermite_dh00(double s)
First derivative of h00: dh00/ds = 6s^2 - 6s.
static double hermite_h10(double s)
Hermite basis function h10: weights the start tangent.
static double hermite_h11(double s)
Hermite basis function h11: weights the end tangent.
static double hermite_d2h00(double s)
Second derivative of h00: d2h00/ds2 = 12s - 6.
static void trajectory_compute_tangents(TRAJECTORY *traj)
Recompute the Hermite tangent vectors from current start/end states.
static double hermite_dh11(double s)
First derivative of h11: dh11/ds = 3s^2 - 2s.
static double hermite_d2h10(double s)
Second derivative of h10: d2h10/ds2 = 6s - 4.
static double hermite_dh10(double s)
First derivative of h10: dh10/ds = 3s^2 - 4s + 1.
static double hermite_h01(double s)
Hermite basis function h01: weights the end position.
static double hermite_d2h11(double s)
Second derivative of h11: d2h11/ds2 = 6s - 2.
static void trajectory_load_segment(TRAJECTORY *traj, double time_val)
Load the correct spline segment for a given time when using multi-point paths.
static double hermite_dh01(double s)
First derivative of h01: dh01/ds = -6s^2 + 6s.
Trajectory interpolation and dead reckoning for 2D/3D networked simulations.