3.2.9.1 XYZ Gridding

Version Info

Minimum Origin Version Required: Origin 8 SR0

Need to do before Running Examples

Prior to running the following example, the gridding_utils.c file need to be loaded and compiled. This can be done from script with the following command:

run.LoadOC(Originlab\gridding_utils.c, 16);

Examples

//--------------------------------------------------------------------------
// File Name:  XYZ Gridding.c
//
// Description:
// This example demonstrates how to convert different type XYZ data to matrix. 
//
// Instructions:
// 1. Add Nag_utils.c to Code Builder Workspace and compile with this file.
// 2. New a worksheet and import \\Matrix Conversion and Gridding\XYZ Random Gaussian.dat,
// then run xyz_gridding in command window, will convert random xyz data to matrix.
// 3. New a worksheet and import  \\Matrix Conversion and Gridding\XYZ Regular.dat,
// then run xyz_gridding in command window, will convert regular xyz data to matrix.
// 4. New a worksheet and import  \\Matrix Conversion and Gridding\Sparse.dat,
// then run xyz_gridding in command window, will convert sparse xyz data to matrix.
//
//--------------------------------------------------------------------------
// For more Origin C examples visit the Origin C Wiki online at:
// http://wikis/ocwiki/index.php?title=Category:Examples
//--------------------------------------------------------------------------

#include <Origin.h>

#include <wks2mat.h>
#include <Nag_utils.h>


void xyz_gridding(int nRows = 10, int nCols = 10)
{
	Worksheet wks = Project.ActiveLayer();
	if( !wks )
	{
		out_str("No active worksheet");
		return;
	}
	
	Column colX(wks, 0);
	Column colY(wks, 1);
	Column colZ(wks, 2);
	if( !colX || !colY || !colZ )
	{
		out_str("No XYZ column");
		return;
	}
	
	XYZRange rng;
	rng.Add(wks, 0, "X");
	rng.Add(wks, 1, "Y");
	rng.Add(wks, 2, "Z");
	
	vector vX, vY, vZ;
	rng.GetData(vZ, vY, vX);
	
	// examine data type
	UINT nVar;
	double xmin, xstep, xmax, ymin, ystep, ymax;
	int nSize = vX.GetSize();
	int nMethod = ocmath_xyz_examine_data(nSize, vX, vY, vZ, 1.0e-8, 1.0e-8, &nVar, &xmin, &xstep, &xmax, &ymin, &ystep, &ymax);

	if( 0 == nMethod || 1 == nMethod ) //regular or Sparse
	{
		if( !is_equal(ystep, 0) )
			nRows = abs(ymax - ymin)/ystep + 1.5;
		
		if( !is_equal(xstep, 0) )
			nCols = abs(xmax - xmin)/xstep + 1.5;
	}
	
	// prepare matrix window
	MatrixPage mp;
	mp.Create("origin");
	MatrixLayer ml = mp.Layers(0);
	MatrixObject mo(ml, 0);
	mo.SetXY(xmin, ymin, xmax, ymax);
	Matrix mat(ml, 0);
	mat.SetSize(nRows, nCols);
	
	// do xyz gridding
	int iRet;
	switch(nMethod)
	{
	case 0: // Regular.
		printf("--- %d: regular conversion ---\n", iRet);	
		iRet = ocmath_convert_regular_xyz_to_matrix(nSize, vX, vY, vZ, mat, xmin, xstep, nCols, ymin, ystep, nRows);
		break;
		
	case 1: // Sparse.
		printf("--- %d: sparse conversion ---\n", iRet);		
		iRet = ocmath_convert_sparse_xyz_to_matrix(nSize, vX, vY, vZ, mat, xmin, xstep, nCols, ymin, ystep, nRows);
		break;
		
	case 2: // Random(Renka Cline).
		printf("--- %d: random conversion ---\n", iRet);
		
		vector vxGrid(nRows*nCols), vyGrid(nRows*nCols);
		iRet = ocmath_mat_to_regular_xyz(NULL, nRows, nCols, xmin, xmax, ymin, ymax, vxGrid, vyGrid);
		if( iRet >= 0 )
		{
	        iRet = xyz_gridding_nag(vX, vY, vZ, vxGrid, vyGrid, mat);
		}
		break;
		
	default: // Error.
		printf("--- Error: %d returned ---\n", iRet);
	}		
}