Robotran C Documentation
Functions
cubic_spline.c File Reference
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "cubic_spline.h"
#include "mbs_1D_array.h"
#include "mbs_matrix.h"
#include "mbs_message.h"

Functions

CubicSplinecubic_spline_new_from_file (const char *filename)
 Load the coefficient of the cubic splines from a file. More...
 
int cubic_spline_get_index (CubicSpline *cs, double x_in)
 Retrieve the index (starting at zero) in the spline coefficient table corresponding to the specified coordinate. More...
 
double cubic_spline_comp_value (CubicSpline *cs, double x_in, int index)
 Compute the value of the cubic spline at the specified coordinate. More...
 
double cubic_spline_comp_firstder (CubicSpline *cs, double x_in, int index)
 Compute the value of the first derivative of the cubic spline at the specified coordinate. More...
 
double cubic_spline_comp_secondder (CubicSpline *cs, double x_in, int index)
 Compute the value of the second derivative of the cubic spline at the specified coordinate. More...
 
void cubic_spline_comp_val_and_der (CubicSpline *cs, double x_in, int index, double *y, double *yp, double *ypp)
 Compute the value of the cubic spline and its first and second derivatives at the specified coordinate. More...
 
void cubic_spline_delete (CubicSpline *cs)
 Free the memory of cubic spline. More...
 

Detailed Description

This c file implements utilities functions about cubic spline interpolation.

Creation date: 2022

Author
Robotran team

(c) Universite catholique de Louvain

Function Documentation

◆ cubic_spline_comp_firstder()

double cubic_spline_comp_firstder ( CubicSpline cs,
double  x_in,
int  index 
)

Compute the value of the first derivative of the cubic spline at the specified coordinate.

Parameters
cscubic spline.
x_inevaluated coordinate.
indexcorresponding to the evaluated coordinate. Use cubic_spline_get_index to get the index corresponding to the coordinate or use -99 to let the function evaluating the index by itself. Other negative values are invalid index.
Returns
the value of the cubic spline.

◆ cubic_spline_comp_secondder()

double cubic_spline_comp_secondder ( CubicSpline cs,
double  x_in,
int  index 
)

Compute the value of the second derivative of the cubic spline at the specified coordinate.

Parameters
cscubic spline.
x_inevaluated coordinate.
indexcorresponding to the evaluated coordinate. Use cubic_spline_get_index to get the index corresponding to the coordinate or use -99 to let the function evaluating the index by itself. Other negative values are invalid index.
Returns
the value of the cubic spline.

◆ cubic_spline_comp_val_and_der()

void cubic_spline_comp_val_and_der ( CubicSpline cs,
double  x_in,
int  index,
double *  y,
double *  yp,
double *  ypp 
)

Compute the value of the cubic spline and its first and second derivatives at the specified coordinate.

Parameters
cscubic spline.
x_inevaluated coordinate.
indexcorresponding to the evaluated coordinate. Use cubic_spline_get_index to get the index corresponding to the coordinate or use -99 to let the function evaluating the index by itself. Ohter negative value are invalid index.
ya pointer to the memory where to store the value of the cubic spline.
ypa pointer to the memory where to store the 1st derivative.
yppa pointer to the memory where to store the 2nd derivative.

◆ cubic_spline_comp_value()

double cubic_spline_comp_value ( CubicSpline cs,
double  x_in,
int  index 
)

Compute the value of the cubic spline at the specified coordinate.

Parameters
cscubic spline.
x_inevaluated coordinate.
indexcorresponding to the evaluated coordinate. Use cubic_spline_get_index to get the index corresponding to the coordinate or use -99 to let the function evaluating the index by itself. Other negative values are invalid index.
Returns
the value of the cubic spline.

◆ cubic_spline_delete()

void cubic_spline_delete ( CubicSpline cs)

Free the memory of cubic spline.

Parameters
cscubic spline to be deleted.

◆ cubic_spline_get_index()

int cubic_spline_get_index ( CubicSpline cs,
double  x_in 
)

Retrieve the index (starting at zero) in the spline coefficient table corresponding to the specified coordinate.

The index is the lower value of the interval.

Parameters
cscubic spline.
x_incoordinate for which to retrieve the index.
Returns
the correct value of the index where coefficients corresponding to the value x_in are located.

◆ cubic_spline_new_from_file()

CubicSpline* cubic_spline_new_from_file ( const char *  filename)

Load the coefficient of the cubic splines from a file.

Allocate also the memory corresponding to this spline structure.

The file contains 5 columns, denoted: $x_{beg}$, $a$, $b$, $c$, $d$. Each column must be separated by a space character. Each line defines a segment. The signification of each column is:

  • $x_{beg}$: Value of the interpolation variable at the beginning of the current segment;
  • $a$, $b$, $c$, $d$: coefficients of the cubic spline for the current segment;

The spline is computed as: $spline(x) = a y^3 + b y^2 + c y + d$ with $y = (x-x_{beg})$

The last line of the file must contains five column, even if only the first value is used as the end of the cubic spline interpolation range.

To generate the input file, an example of Python script is provided with the sources of MBsysC. The file named cubic_spline_compute_coefficients.py is located in the folder MBsysC/scripts.

Parameters
[in]filenameFile name (with the path and extension) that contains the coefficients of the spline
Returns
an allocated structure