// GetPointDialog.cpp : implementation file
//

#include "stdafx.h"
#include "SingleViewModeling.h"
#include "GetPointDialog.h"
#include "Matrix.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGetPointDialog dialog


CGetPointDialog::CGetPointDialog(CSingleViewModelingView * pView,CWnd* pParent /*=NULL*/)
	: CDialog(CGetPointDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGetPointDialog)
	m_bu = 0.0;
	m_bv = 0.0;
	m_u = 0.0;
	m_v = 0.0;
	m_pointid = 0;
	m_tu = 0.0;
	m_tv = 0.0;
	m_wx = 0.0;
	m_wy = 0.0;
	m_wz = 0.0;
	m_wxrefid = -1;
	m_wyrefid = -1;
	m_wzrefid = -1;
	//}}AFX_DATA_INIT

    m_pView = pView;
}


void CGetPointDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGetPointDialog)
	DDX_Text(pDX, IDC_B_U, m_bu);
	DDX_Text(pDX, IDC_B_V, m_bv);
	DDX_Text(pDX, IDC_IMAGE_U, m_u);
	DDX_Text(pDX, IDC_IMAGE_V, m_v);
	DDX_Text(pDX, IDC_POINT_ID, m_pointid);
	DDX_Text(pDX, IDC_T_U, m_tu);
	DDX_Text(pDX, IDC_T_V, m_tv);
	DDX_Text(pDX, IDC_W_X, m_wx);
	DDX_Text(pDX, IDC_W_Y, m_wy);
	DDX_Text(pDX, IDC_W_Z, m_wz);
	DDX_Text(pDX, IDC_W_X_REF, m_wxrefid);
	DDX_Text(pDX, IDC_W_Y_REF, m_wyrefid);
	DDX_Text(pDX, IDC_W_Z_REF, m_wzrefid);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CGetPointDialog, CDialog)
	//{{AFX_MSG_MAP(CGetPointDialog)
	ON_BN_CLICKED(IDC_CACULATE_B, OnCaculateB)
	ON_BN_CLICKED(IDC_CACULATE_XandY, OnCACULATEXandY)
	ON_BN_CLICKED(IDC_CACULATE_Z, OnCaculateZ)
	ON_BN_CLICKED(IDC_GET_REF, OnGetRef)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGetPointDialog message handlers

void CGetPointDialog::OnCaculateB() 
{
	// TODO: Add your control notification handler code here
	 CSingleViewModelingDoc *pDoc;
	pDoc = (CSingleViewModelingDoc *)m_pView->GetDocument( );
	CSingleImage *pImageptr;
	pImageptr = (CSingleImage *)pDoc->GetSingleImage( );
	BOOL getdata;
	getdata = UpdateData(TRUE);
	double ** HZStandard;

	HZStandard = (double **)malloc(3*sizeof(double *));
	for(int k=0;k<3;k++)
		HZStandard[k] = (double*)malloc(3*sizeof(double));


	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++){
			HZStandard[i][j] = pImageptr->HStandardMatrix[i][j];
		
		};

	for( i=0;i<3;i++){
	
		HZStandard[i][2] = HZStandard[i][2] +pImageptr->A*m_wz*pImageptr->m_Vz[i];
		};

    double uvw[3];
	uvw[0] = 0;
	uvw[1] = 0;
	uvw[2] = 0;

	double xyw[3];
	xyw[0] = m_wx-pImageptr->x0;
	xyw[1] = m_wy-pImageptr->y0;
	xyw[2] = 1;

	CMatrix matrixitem;
	matrixitem.matrixvectormultiply(HZStandard,xyw,uvw,3,3);
    double u0,v0;
	u0 = pImageptr->GetWidth( )/2.0;
	v0 = pImageptr->GetHeight( )/2.0;
	m_bu = uvw[0]/uvw[2]+u0 ;
	m_bv = uvw[1]/uvw[2]+v0 ;

	getdata = UpdateData(FALSE);

	for(k=0;k<3;k++)
	   delete	HZStandard[k];


	
	delete HZStandard;


	
}

void CGetPointDialog::OnCACULATEXandY() 
{
	// TODO: Add your control notification handler code here
	 CSingleViewModelingDoc *pDoc;
	pDoc = (CSingleViewModelingDoc *)m_pView->GetDocument( );
	CSingleImage *pImageptr;
	pImageptr = (CSingleImage *)pDoc->GetSingleImage( );
    BOOL getdata;
	getdata = UpdateData(TRUE);
	double ** HZStandard,** HZStandardInverse;

	HZStandard = (double **)malloc(3*sizeof(double *));
	for(int k=0;k<3;k++)
		HZStandard[k] = (double*)malloc(3*sizeof(double));

	HZStandardInverse = (double **)malloc(3*sizeof(double *));
	for(k=0;k<3;k++)
		HZStandardInverse[k] = (double*)malloc(3*sizeof(double));
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++){
			HZStandard[i][j] = pImageptr->HStandardMatrix[i][j];
		
		};

	for( i=0;i<3;i++){
	
		HZStandard[i][2] = HZStandard[i][2] +pImageptr->A*m_wz*pImageptr->m_Vz[i];
		};

	double u0,v0;
	u0 = pImageptr->GetWidth( )/2.0;
	v0 = pImageptr->GetHeight( )/2.0;
    double uvw[3];
	uvw[0] = m_u-u0;
	uvw[1] = m_v-v0;
	uvw[2] = 1;

	double xyw[3];
	xyw[0] = 0;
	xyw[1] = 0;
	xyw[2] = 0;

	CMatrix matrixitem;
	matrixitem.inversematrix(HZStandard,HZStandardInverse,3);
	matrixitem.matrixvectormultiply(HZStandardInverse,uvw,xyw,3,3);
	m_wx = xyw[0]/xyw[2] + pImageptr->x0;
	m_wy = xyw[1]/xyw[2] + pImageptr->y0;
	getdata = UpdateData(FALSE);

	for(k=0;k<3;k++)
	   delete	HZStandard[k];


	for(k=0;k<3;k++)
		delete HZStandardInverse[k];
	
	delete HZStandard;
	delete HZStandardInverse;

	
	
}

void CGetPointDialog::OnCaculateZ() 
{
	// TODO: Add your control notification handler code here

    CSingleViewModelingDoc *pDoc;
	pDoc = (CSingleViewModelingDoc *)m_pView->GetDocument( );
	CSingleImage *pImageptr;
	pImageptr = (CSingleImage *)pDoc->GetSingleImage( );
    BOOL getdata;
	getdata = UpdateData(TRUE);
    double b[3];
	double t[3];
    double bcrosst[3];
	double vzcrosst[3];
//	double lstandard[3];
	double ldotb;
	double btmo;
	double vztmo;
	double aa;

	double u0,v0;
	u0 = pImageptr->GetWidth()/2.0;
	v0 = pImageptr->GetHeight( )/2.0;
	b[0] = m_bu-u0;
	b[1] = m_bv-v0;
	b[2] = 1;

	t[0] = m_tu-u0;
	t[1] = m_tv-v0;
	t[2] = 1;


//	MatrixItem.getvectorcrossproduct(pImageptr->m_Vx,pImageptr->m_Vy,lstandard);
//	MatrixItem.getstandardvector(lstandard,3);
    CMatrix MatrixItem;
	MatrixItem.getvectorcrossproduct(b,t,bcrosst);
	btmo = MatrixItem.getmol(bcrosst,3);

	MatrixItem.getvectorcrossproduct(pImageptr->m_Vz,t,vzcrosst);
	vztmo = MatrixItem.getmol(vzcrosst,3);

	ldotb = MatrixItem.getdotproduct(pImageptr->m_lScale,b);
    
	aa = pImageptr->A;

	m_wz = -btmo/(ldotb*vztmo*aa);

	

	getdata = UpdateData(FALSE);


	
}

void CGetPointDialog::OnGetRef() 
{
	// TODO: Add your control notification handler code here
	CSingleViewModelingDoc *pDoc;
	pDoc = (CSingleViewModelingDoc *)m_pView->GetDocument( );
	CSingleImage *pImageptr;
	pImageptr = (CSingleImage *)pDoc->GetSingleImage( );
    BOOL getdata;
	getdata = UpdateData(TRUE);
    int pointnumber;
	pointnumber = pImageptr->m_ViewPointListptr.GetSize( );

	if(m_wxrefid!=(-1)){
	
		for(int i=0;i<pointnumber;i++){
			if(pImageptr->m_ViewPointListptr[i]->m_pointid == m_wxrefid){
				m_wx = pImageptr->m_ViewPointListptr[i]->m_wx;
				break;
			}
		};
	};

	if(m_wyrefid!=(-1)){
	
		for(int i=0;i<pointnumber;i++){
			if(pImageptr->m_ViewPointListptr[i]->m_pointid == m_wyrefid){
				m_wy = pImageptr->m_ViewPointListptr[i]->m_wy;
				break;
			}
		};
	};

	if(m_wzrefid!=(-1)){
	
		for(int i=0;i<pointnumber;i++){
			if(pImageptr->m_ViewPointListptr[i]->m_pointid == m_wzrefid){
				m_wz = pImageptr->m_ViewPointListptr[i]->m_wz;
				break;
			}
		};
	};



	getdata = UpdateData(FALSE);

	
}
