#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>


#include <wks2mat.h>
#include <ocTriContour.h>
#define NE_MESH_ERROR 2099

enum {
	ERR_CANNOT_GENERATE_MESH  = XF_LOCAL_ERR_BEGIN,
};


void xy_resample(const XYRange& iy, const XYRange& b, const int& e, XYRange& oy)
{
	int iRet;
	int edge_pts = e;
	
	vector vxBdr, vyBdr;
	vector vX, vY;
	vector vxMesh, vyMesh;
	iy.GetData(vY, vX);	
	if (b == NULL)
	{
		/// Leo 2006-4-7 USE_DEFAULT_BOUNDARY
		/*
		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);
		*/
		vector vzdumb(vX.GetSize());
		TRIANGULATION strTriCnt;
		ocmath_triangulation(vX.GetSize(), vX, vY, vzdumb, &strTriCnt);
	
		int nBorder = strTriCnt.npb;
		vxBdr.SetSize(nBorder);
		vyBdr.SetSize(nBorder);
		vzdumb.SetSize(nBorder);
		ocmath_triangulation_boundary(&strTriCnt, &nBorder, vxBdr, vyBdr, vzdumb);
		ocmath_triangulation_free(&strTriCnt);	
		///end USE_DEFAULT_BOUNDARY
	}
	else
	{
		b.GetData( vyBdr, vxBdr);
	}
	
	int nSize = vX.GetSize();
	int nBdrSize = vxBdr.GetSize();
	int nMshSize = edge_pts;
	
	//try to allocate suitable MeshSize
	int nn = 1;
	do
	{
		nMshSize = nMshSize*e;
		if(nn > 50)
			throw(1);
		vxMesh.SetSize(nMshSize);
		vyMesh.SetSize(nMshSize);
		printf("%d: trying %d mesh points\n", nn++, nMshSize);
		iRet = ocmath_mesh_in_polygon(nBdrSize, vxBdr, vyBdr, edge_pts, &nMshSize, vxMesh, vyMesh);
		/// Leo 2006-4-6 TOO_FEW_EDGE_PTS
		if (iRet == OE_SIZE_LT)
		{
			XF_THROW(CER_CANNOT_GENERATE_MESH);
		}
	}while(iRet == NE_MESH_ERROR);
	
	if (OE_NOERROR != iRet)
		XF_THROW_EX("Xfunction failed, Code = %d", iRet);
	
	
	vxMesh.SetSize(nMshSize);
	vyMesh.SetSize(nMshSize);
	
	// Put result data to result mat
	oy.SetData(&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 xy_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 xy_resample_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	return false;

}

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