# 2.1.18.19 ocmath_tps_fit

## Description

This function perform 2D interpolation using thin plate spline method.

## Syntax

int ocmath_tps_fit(int nNode, const double *x, const double *y, const double *z, double dSmooth, ocmath_strTPS *strTPS, double* dRegularization)

## Parameters

nNode
[input] the number of scatters.
x
[input] the position and the x-coordinates of each points.
y
[input] the position and the y-coordinates of each points.
z
[input] the position and the z-coordinates of each points.
dSmooth
[input] the smooth factor.
strTPS
[output] contains the interpolation coefficients of the fitting.
dRegularization
[output] the regularization factor defined by the value of dsmooth.

## Return

OE_NOERROR for successful or

OE_INVALID_POINT Not enough points (n<3) or all the points are colinear;

OE_NULL_POINTER if pointer is null.

OE_INVALID_SMOOTH Parameter dSmooth should not be less than zero.

## Examples

EX1

#include <wks2mat.h>
int ocmath_tps_fit_ex1()
{
// tabulated data on a 3x3 regular grid, points of form (x,y,f(x,y))
const int iQuantity = 9;
double afX[] = {0.0, 0.5, 1.0,0.0, 0.5, 1.0,0.0, 0.5, 1.0};
double afY[] = {0.0, 0.0, 0.0,0.5, 0.5, 0.5,1.0, 1.0, 1.0};
double afF[] = {1.0, 2.0, 3.0,3.0, 2.0, 1.0,1.0, 2.0, 3.0};

// resample on a 7x7 regular grid
const int iResample = 6;
const double fInv = 1.0/(double)iResample;
int i, j;

// no smoothing, exact interpolation at grid points
ocmath_strTPS strTPS;
ocmath_tps_initial(&strTPS);

double dSmooth = 0.1;
double dReg;

int nRe = ocmath_tps_fit(iQuantity, afX, afY, afF,
dSmooth, &strTPS, &dReg);

if(nRe)
{
printf("intp error  %d\n", nRe);
return -1;
}
else
{
printf("The regularization parameter is %g\n\n",dReg);
}

// Output the results
printf( "smooth parameter is %g\n", dSmooth);
double x, y, z;
for (j = 0; j <= iResample; j++)
{
y = fInv*j;
for (i = 0; i <= iResample; i++)
{
x = fInv*i;
ocmath_tps_eval(1, &x, &y, &z, &strTPS);
printf("%9.6f\t", z);
}
printf("\n");
}

ocmath_tps_free(&strTPS);
return 0;
}

EX2

// This example demonstrates the usage of thin plate spline fitting and XY gridding.
// To test, create an Origin workshhet with three columns that contain XYZ scatters.
// The result will be stored in a newly created Origin matrix window.
#include <wks2mat.h>
void ocmath_tps_fit_ex2()
{
int nRows = 100;
int nCols = 100;

Worksheet wks = Project.ActiveLayer();
wks.SetSize(-1, 3);
Dataset dsX(wks, 0), dsY(wks, 1), dsZ(wks, 2);

vector vXsrc(dsX), vYsrc(dsY), vZsrc(dsZ);
vector vXdest(nRows * nCols), vYdest(nRows * nCols), vZdest(nRows * nCols);

int nSize = vXsrc.GetSize();

double dXmin, dXmax, dYmin, dYmax;
vXsrc.GetMinMax(dXmin, dXmax);
vYsrc.GetMinMax(dYmin, dYmax);

MatrixLayer ml;
ml.Create();
MatrixObject mo = ml.MatrixObjects(0);

mo.SetNumRows(nRows);
mo.SetNumCols(nCols);
mo.SetXY(dXmin, dYmin, dXmax, dYmax);

Matrix& mat = mo.GetDataObject();

ocmath_strTPS strTPS;
ocmath_tps_initial(&strTPS);

double dSmooth = 0.0; // no smoothing, exact interpolation at grid points
double dReg;

int iRet;
// Initialization
iRet = ocmath_tps_fit(nSize, vXsrc, vYsrc, vZsrc, dSmooth, &strTPS, &dReg);

if(iRet) {
printf("intp error  %d\n", iRet);
return;
}

// Create XY gridding.
iRet = ocmath_mat_to_regular_xyz(NULL, nRows, nCols, dXmin, dXmax, dYmin, dYmax, vXdest, vYdest, NULL);

if (iRet <= 0) {
printf("Failed to create XY gridding!\n");
return;
}

// Interpolation
ocmath_tps_eval(nRows * nCols, vXdest, vYdest, mat, &strTPS);

ocmath_tps_free(&strTPS);
}