// DialogforParameter.cpp : implementation file
//

#include "stdafx.h"
#include "SingleViewModeling.h"
#include "DialogforParameter.h"
#include "Matrix.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogforParameter dialog


CDialogforParameter::CDialogforParameter(CSingleViewModelingView *pView,CWnd* pParent /*=NULL*/)
	: CDialog(CDialogforParameter::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogforParameter)
	m_bu = 0.0;
	m_bv = 0.0;
	m_A = 0.0;
	m_a = 0.0;
	m_b = 0.0;
	m_x = 0.0;
	m_y = 0.0;
	m_tu = 0.0;
	m_tv = 0.0;
	m_Z = 0.0;
	m_u = 0.0;
	m_v = 0.0;
	m_uo = 0.0;
	m_vo = 0.0;
	//}}AFX_DATA_INIT
	m_pViewptr = pView;
}


void CDialogforParameter::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogforParameter)
	DDX_Text(pDX, IDC_B_U, m_bu);
	DDX_Text(pDX, IDC_B_V, m_bv);
	DDX_Text(pDX, IDC_POINT_A, m_A);
	DDX_Text(pDX, IDC_POINT_SMALL_A, m_a);
	DDX_Text(pDX, IDC_POINT_SMALL_B, m_b);
	DDX_Text(pDX, IDC_POINT_X, m_x);
	DDX_Text(pDX, IDC_POINT_Y, m_y);
	DDX_Text(pDX, IDC_T_U, m_tu);
	DDX_Text(pDX, IDC_T_V, m_tv);
	DDX_Text(pDX, IDC_Z, m_Z);
	DDX_Text(pDX, IDC_POINT_U, m_u);
	DDX_Text(pDX, IDC_POINT_V, m_v);
	DDX_Text(pDX, IDC_POINT_UO, m_uo);
	DDX_Text(pDX, IDC_POINT_VO, m_vo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogforParameter, CDialog)
	//{{AFX_MSG_MAP(CDialogforParameter)
	ON_BN_CLICKED(IDC_Caculate, OnCaculate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogforParameter message handlers

void CDialogforParameter::OnCaculate() 
{
	// TODO: Add your control notification handler code here

    CSingleViewModelingDoc *pDoc;
	pDoc = (CSingleViewModelingDoc *)m_pViewptr->GetDocument( );
	CSingleImage *pImageptr;
	pImageptr = (CSingleImage *)pDoc->GetSingleImage( );
	BOOL getdata;
	getdata = UpdateData(TRUE);

	double u0,v0;
	u0 = pImageptr->GetWidth( )/2.0;
	v0 = pImageptr->GetHeight( )/2.0;

	double ab[2];
	double luv[2];
	double ** caculatematrix;

	caculatematrix = (double **)malloc(2*sizeof(double *));
	for(int k=0;k<2;k++)
		caculatematrix[k] = (double*)malloc(2*sizeof(double));

	CMatrix MatrixItem;

	caculatematrix[0][0]=pImageptr->m_Vx[0]*m_x-pImageptr->m_Vx[2]*m_x*(m_u-u0);
	caculatematrix[0][1]=pImageptr->m_Vy[0]*m_y-pImageptr->m_Vy[2]*m_y*(m_u-u0);
	caculatematrix[1][0]=pImageptr->m_Vx[1]*m_x-pImageptr->m_Vx[2]*m_x*(m_v-v0);
	caculatematrix[1][1]=pImageptr->m_Vy[1]*m_y-pImageptr->m_Vy[2]*m_y*(m_v-v0);

	luv[0] = 1*(m_u-u0)-(m_uo-u0);
	luv[1] = 1*(m_v-v0)-(m_vo-v0);

	MatrixItem.getequationanswer(caculatematrix,luv,2);
	ab[0] = luv[0];
	ab[1] = luv[1];

	double b[3];
	double t[3];
    double bcrosst[3];
	double vzcrosst[3];
	double lstandard[3];
	double ldotb;
	double btmo;
	double vztmo;

	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);

	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(lstandard,b);
    
	m_A = -btmo/(ldotb*vztmo*m_Z);
	m_a = ab[0];
	m_b = ab[1];

	getdata = UpdateData(FALSE);



    
	for(k=0;k<2;k++)
		delete caculatematrix[k];
	delete caculatematrix;



	//continue from here. caculate then save...then free.

	
}

/*void CDialogforParameter::OnOK() 
{
	// TODO: Add extra validation here

	CSingleViewModelingDoc *pDoc;
	pDoc = (CSingleViewModelingDoc *)m_pViewptr->GetDocument( );
	CSingleImage *pImageptr;
	pImageptr = (CSingleImage *)pDoc->GetSingleImage( );
	BOOL getdata;
	getdata = UpdateData(TRUE);

    pImageptr->A = m_A;
	pImageptr->a = m_a;
	pImageptr->b = m_b;

    double lstandard[3];
	CMatrix MatrixItem;
	MatrixItem.getvectorcrossproduct(pImageptr->m_Vx,pImageptr->m_Vy,lstandard);
	MatrixItem.getstandardvector(lstandard,3);

	for(int i=0;i<3;i++){
		pImageptr->m_lScale[i] = lstandard[i];
	};



	pImageptr->m_l[0] = m_uo;
	pImageptr->m_l[1] = m_vo;
	pImageptr->m_l[2] = 1;

	for(i=0;i<3;i++){
		pImageptr->HMatrix[i][0] = pImageptr->a*pImageptr->m_Vx[i];
		pImageptr->HMatrix[i][1] = pImageptr->b*pImageptr->m_Vy[i];
        pImageptr->HMatrix[i][2] = pImageptr->m_l[i];
        
	};


	double ** HCMatrix,** HCSMatrix,** HCIMatrix,** Displacement;


	HCMatrix = (double **)malloc(3*sizeof(double *));
	HCSMatrix = (double **)malloc(3*sizeof(double *));
	HCIMatrix = (double **)malloc(3*sizeof(double *));
	Displacement = (double **)malloc(3*sizeof(double *));
	for(int k=0;k<3;k++){
		HCMatrix[k] = (double*)malloc(3*sizeof(double));
		HCSMatrix[k] = (double*)malloc(3*sizeof(double));
	    HCIMatrix[k] = (double*)malloc(3*sizeof(double));
	    Displacement[k] = (double *)malloc(3*sizeof(double));
	}
	for( i=0;i<3;i++)
		for(int j=0;j<3;j++){
			HCMatrix[i][j] = 0;
			HCSMatrix[i][j] = 0;
			HCIMatrix[i][j] = 0;
			Displacement[i][j] = 0;
		};

     for( i=0;i<3;i++)
	   	for(int j=0;j<3;j++){
		   HCMatrix[i][j] = pImageptr->HMatrix[i][j];
		};

     MatrixItem.inversematrix(HCMatrix,HCIMatrix,3);

	 double standardl[3];
	 for(i=0;i<3;i++){
		standardl[i] = pImageptr->m_lScale[i];
	 };

	 double x0y0[3];
	 MatrixItem.matrixvectormultiply(HCIMatrix,standardl,x0y0,3,3);

	 pImageptr->x0 = x0y0[0]/x0y0[2];
	 pImageptr->y0 = x0y0[1]/x0y0[2];


     Displacement[0][0] =1;
	 Displacement[1][1] =1;
	 Displacement[2][2] =1;

	 Displacement[0][2] = pImageptr->x0;
	 Displacement[1][2] = pImageptr->y0;

	 MatrixItem.matrixmultiply(HCMatrix,Displacement,HCSMatrix,3,3,3);

	 double  tempvector[3];
	 for(i=0;i<3;i++){
		tempvector[i] = HCSMatrix[i][2];
	 };

	 double tempmol;
	 tempmol = MatrixItem.getmol(tempvector,3);
	 double scale;
	 scale = 1;
	 for(i=0;i<3;i++){
		double mul;
		mul = tempvector[i]*pImageptr->m_lScale[i];
		if(mul<0){
			scale= -1;
			break;
		};
	 };
	 scale = scale/tempmol;

	 for(i=0;i<3;i++)
		 for(int j=0;j<3;j++){
			HCSMatrix[i][j] = scale*HCSMatrix[i][j];
		 };

		
	 
    for(i=0;i<3;i++)
		 for(int j=0;j<3;j++){
			pImageptr->HStandardMatrix[i][j]= HCSMatrix[i][j]; 
		 };


  
		 for(k=0;k<3;k++){
					
			delete HCMatrix[k];
			delete HCSMatrix[k];
			delete HCIMatrix[k];
			delete Displacement[k];
		 };

		 	delete HCMatrix;
			delete HCSMatrix;
			delete HCIMatrix;
			delete Displacement;

	CDialog::OnOK();


} */
