#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	outlier_extrap,
	outlier_missing,
	outlier_zero,
	outlier_min,
	outlier_max,
	outlier_mean,
	outlier_custom,
};


#include <wks2mat.h> // ocmath_mat_to_regular_xyz and 
#include <ocmath.h> //ocmath_d_sum


void xyz_tps(const XYZRange& iz, const int& rows, const int& cols, const double& s, const int& outlier, const double& outside_val, const double& tolerance, MatrixObject& om, double& r)
{
	// XYZ to matrix using TPS method
	vector vx, vy, vz;
	if(!iz.IsValid() || ! iz.GetData(vz, vy, vx))
	{
	   XF_THROW(XFERR_INVALID_XYZ_RANGE);
	}
	
	int iQuantity = vx.GetSize();
	ocmath_strTPS strTPS;
	double dReg;
	
	int nRet = ocmath_tps_fit(iQuantity, vx, vy, vz, s, &strTPS, &dReg);
	if(nRet)
	{
	   XF_THROW(XFERR_COVERT_TO_MATRIX);
	}
	else
	{
	   r = dReg;
	}
	
	Matrix& mat = om.GetDataObject();
	mat.SetSize(rows, cols);
	// Output the results
	double xx, yy, zz;
	// need to get XY range from vx and vy, for now
	//
	double xmin, xmax, ymin, ymax;
	ocmath_d_sum(vx, vx.GetSize(), &xmin, &xmax);
	ocmath_d_sum(vy, vy.GetSize(), &ymin, &ymax);
	om.SetXY(xmin, ymin, xmax, ymax);
	// Create XY arrays using matrix XY mapping
	int nSize = rows * cols;
	vector vxm(nSize), vym(nSize);
	int iRet = ocmath_mat_to_regular_xyz(NULL, rows, cols, xmin, xmax, ymin, ymax, vxm, vym, NULL, true);
	
	if (iRet <= 0) 
	{ 
	   XF_THROW(CER_FAILED_TO_CREATE_XY_GRIDDING_DATA);
	}
	// Extrapolation
	bool bExtrapolation = false;
	double dValue = NANUM;
	
	switch(outlier)
	{
	case outlier_extrap:
	   bExtrapolation = true;
	   break;
	case outlier_zero:
	   dValue = 0;
	   break;
	case outlier_mean:
	   dValue = ocmath_d_sum(vz, vz.GetSize()) / vz.GetSize();
	   break;
	case outlier_min:
	   ocmath_d_sum(vz, vz.GetSize(), &dValue);
	   break;
	case outlier_max:
	   ocmath_d_sum(vz, vz.GetSize(), NULL, &dValue);
	   break;
	case outlier_missing:
	   break;
	case outlier_custom:
	   dValue = outside_val;
	}
	
	// Interpolation
	ocmath_tps_eval(nSize, vxm, vym, mat, &strTPS, bExtrapolation, dValue, tolerance);
	
	ocmath_tps_free(&strTPS);
	
	MatrixLayer ml;
	om.GetParent(ml);
	if(ml)
	{
	   ml.SetViewImage(true);
	   MatrixPage pg = ml.GetPage();
	   if(pg)
	   {
	      string strLabel;
	      strLabel.Format("TPS. smooth = %g", s); //CPY 1/31/06, this was smooth, I changed it to s, but I don't see s used anywere
	      pg.SetLongName(strLabel);
	      pg.TitleShow = WIN_TITLE_SHOW_BOTH;
	   }
	}
	
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int xyz_tps_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return xyz_tps_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	if (trGetN.outlier.nVal == outlier_custom)
		trGetN.outside_val.Show = true;
	else
		trGetN.outside_val.Show = false;
	
	if (trGetN.outlier.nVal != outlier_extrap)
		trGetN.tolerance.Show = true;
	else
		trGetN.tolerance.Show = false;
	
	return true;
	

}

int xyz_tps_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void xyz_tps_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int xyz_tps_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
