#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	method_renka,
	method_shepard,
	method_tps,
	method_kriging,
	method_wave,
};



#include <wks2mat.h>
#include <OC_nag8.h>


void xyz_resample(const XYZRange& iz, const XYRange& y, const int& edge_pts, const int& method, const int& nq, const int& nw, const double& kriging_radius, const double& average_radius, const int& min_pts, const double& tps_smooth, const double& kriging_smooth, XYZRange& oz)
{
	int iRet;
	
	vector vxBdr, vyBdr;
	vector vX, vY, vZ;
	vector vxMesh, vyMesh, vzMesh;
	iz.GetData(vZ, vY, vX);	
	if (y == NULL)
	{
		double dxMin, dxMax, dyMin, dyMax;
		vX.GetMinMax(dxMin, dxMax);
		vY.GetMinMax(dyMin, dyMax);
		vxBdr.Add(dxMin);
		vxBdr.Add(dxMax);
		vxBdr.Add(dxMax);
		vxBdr.Add(dxMin);
		vyBdr.Add(dyMin);
		vyBdr.Add(dyMin);
		vyBdr.Add(dyMax);
		vyBdr.Add(dyMax);
	}
	else
	{
		y.GetData( vyBdr, vxBdr);
	}
	
	int nSize = vX.GetSize();
	int nBdrSize = vxBdr.GetSize();
	int nMshSize = edge_pts;
	
	nSize = ocmath_xyz_remove_duplicates(nSize, vX, vY, vZ);
		
	//try to allocate suitable MeshSize
	do
	{
		nMshSize = nMshSize*edge_pts;
		if(nMshSize>0x0100000)
			break;
		vxMesh.SetSize(nMshSize);
		vyMesh.SetSize(nMshSize);
		vzMesh.SetSize(nMshSize);
		iRet = ocmath_mesh_in_polygon(nBdrSize, vxBdr, vyBdr, edge_pts, &nMshSize, vxMesh, vyMesh);
	}while(iRet==NE_MESH_ERROR);
	
	if (OE_NOERROR != iRet)
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, iRet);
	
	vxMesh.SetSize(nMshSize);
	vyMesh.SetSize(nMshSize);
	vzMesh.SetSize(nMshSize);
	
	iRet = ocmath_xyz_gridding(nSize, vX, vY, vZ, nMshSize, vxMesh, vyMesh, vzMesh, 
				method, nq, nw, kriging_radius, average_radius, min_pts, 
				tps_smooth, kriging_smooth);
				
	if( iRet!=NE_NOERROR )
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, iRet);
	
	// Put result data to result mat
	oz.SetData(&vzMesh, &vyMesh, &vxMesh);	
	
}

//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_resample_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_resample_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	bool bRenkaCline, bShepard, bTPS, bKriging, bWeightAve;
	switch(trGetN.method.nVal)
	{
	case GRIDDING_METHOD_RANDOM_RENKA_CLINE:
		bRenkaCline = true;
		break;
	case GRIDDING_METHOD_RANDOM_SHEPARD:
		bShepard = true;
		break;
	case GRIDDING_METHOD_RANDOM_TPS:
		bTPS = true;
		break;
	case GRIDDING_METHOD_RANDOM_KRIGING:
		bKriging = true;
		break;
	case GRIDDING_METHOD_WEIGHTED_AVERAGE:
		bWeightAve = true;
		break;
	}
	
	trGetN.nq.Show = bShepard;
	trGetN.nw.Show = bShepard;
	trGetN.kriging_radius.Show = bKriging;
	trGetN.average_radius.Show = bWeightAve;
	trGetN.min_pts.Show = bKriging;
	trGetN.tps_smooth.Show = bTPS;
	trGetN.kriging_smooth.Show = bKriging;
	
	return true;

}

int xyz_resample_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_resample_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_resample_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
