// CameraFreeMoveDialog.cpp : implementation file
//

#include "stdafx.h"
#include "ViewTransformation.h"
#include "CameraFreeMoveDialog.h"
#include "ViewTransformationView.h"
#include "Matrix.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCameraFreeMoveDialog dialog


CCameraFreeMoveDialog::CCameraFreeMoveDialog(CViewTransformationView* pView, CWnd* pParent)//=NULL//)
	: CDialog(CCameraFreeMoveDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCameraFreeMoveDialog)
	m_dWValue = 0.0;
	m_dXPosition = 0.0;
	m_dXRotation = 0.0;
	m_dYPosition = 0.0;
	m_dYRotation = 0.0;
	m_dZPosition = 0.0;
	m_dZRotation = 0.0;
	m_dZone = 0;
	m_x_value = 0;
	m_y_value = 0;
	//}}AFX_DATA_INIT
	this->m_pViewptr = pView;
}


void CCameraFreeMoveDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCameraFreeMoveDialog)
	DDX_Text(pDX, IDC_W_VALUE, m_dWValue);
	DDX_Text(pDX, IDC_X_Position, m_dXPosition);
	DDX_Text(pDX, IDC_X_Rotation, m_dXRotation);
	DDX_Text(pDX, IDC_Y_Position, m_dYPosition);
	DDX_Text(pDX, IDC_Y_Rotation, m_dYRotation);
	DDX_Text(pDX, IDC_Z_Position, m_dZPosition);
	DDX_Text(pDX, IDC_Z_Rotation, m_dZRotation);
	DDX_Text(pDX, IDC_ZONE, m_dZone);
	DDX_Text(pDX, IDC_X_VALUE, m_x_value);
	DDX_Text(pDX, IDC_Y_VALUE, m_y_value);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCameraFreeMoveDialog, CDialog)
	//{{AFX_MSG_MAP(CCameraFreeMoveDialog)
	ON_BN_CLICKED(IDC_HINT, OnHint)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCameraFreeMoveDialog message handlers

void CCameraFreeMoveDialog::OnHint() 
{
	// TODO: Add your control notification handler code here

	CViewTransformationDoc *pDoc;
	pDoc = (CViewTransformationDoc *) m_pViewptr->GetDocument( );

	BOOL getdata;
	getdata = UpdateData(TRUE);
    
/*	double **xrotatematrix,**yrotatematrix,**zrotatematrix;
	xrotatematrix = (double **)malloc(3*sizeof(double *));
	for(int k=0;k<3;k++)
		xrotatematrix[k] = (double*)malloc(3*sizeof(double));

	yrotatematrix = (double **)malloc(3*sizeof(double *));
	for( k=0;k<3;k++)
		yrotatematrix[k] = (double*)malloc(3*sizeof(double));

	zrotatematrix = (double **)malloc(3*sizeof(double *));
	for( k=0;k<3;k++)
		zrotatematrix[k] = (double*)malloc(3*sizeof(double));
 */       
	double * c1c2vector,*eyevector;
	c1c2vector = (double *) malloc(3*sizeof(double));
    eyevector = (double *)malloc(3*sizeof(double));

//	double ** P2matrix,**R1matrix,**R2matrix,**P1matrix,**P2inversematrix;

	double **P1matrix,**P1inversematrix;

/*	P2matrix = (double **)malloc(3*sizeof(double *));
	for( k=0;k<3;k++)
		P2matrix[k] = (double*)malloc(3*sizeof(double));
*/	
	P1inversematrix = (double **)malloc(3*sizeof(double *));
	for( int k=0;k<3;k++)
		P1inversematrix[k] = (double*)malloc(3*sizeof(double));

	P1matrix = (double **)malloc(3*sizeof(double *));
	for( k=0;k<3;k++)
		P1matrix[k] = (double*)malloc(3*sizeof(double));

/*	R1matrix = (double **)malloc(3*sizeof(double *));
	for( k=0;k<3;k++)
		R1matrix[k] = (double*)malloc(3*sizeof(double));

	R2matrix = (double **)malloc(3*sizeof(double *));
	for( k=0;k<3;k++)
		R2matrix[k] = (double*)malloc(3*sizeof(double));

*/
	//initialize
	for(int i = 0;i<3;i++)
		for(int j = 0;j<3;j++){
//			xrotatematrix[i][j] = 0.0;
//			yrotatematrix[i][j] = 0.0;
//			zrotatematrix[i][j] = 0.0;
			P1matrix[i][j] = 0.0;
//			P2matrix[i][j] = 0.0;
			P1inversematrix[i][j] = 0.0;
//			R1matrix[i][j] = 0.0;
//			R2matrix[i][j] = 0.0;
		};

	for(i =0;i<3;i++){
			c1c2vector[i] = 0.0;
			eyevector[i] = 0.0;
		};
	
/*	double xangle,yangle,zangle;
	xangle = m_dXRotation*2*PIE/360.0;
	yangle = m_dYRotation*2*PIE/360.0;
	zangle = m_dZRotation*2*PIE/360.0;
*/
    CMatrix MatrixItem;

	

	if(getdata){
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++){
				P1matrix[i][j] = pDoc->m_pSourceImage->m_dPMatrix[i][j]; 
				
			};
            //caculate p2 =R*p1
/*			xrotatematrix[0][0] = 1;
			xrotatematrix[1][1] = cos(xangle);
			xrotatematrix[1][2] = -sin(xangle);
			xrotatematrix[2][1] = sin(xangle);
			xrotatematrix[2][2] = cos(xangle);

			yrotatematrix[0][0] = cos(yangle);
			yrotatematrix[0][2] = sin(yangle);
			yrotatematrix[1][1] = 1;
			yrotatematrix[2][0] = -sin(yangle);
			yrotatematrix[2][2] = cos(yangle);

			zrotatematrix[0][0] = cos(zangle);
			zrotatematrix[0][1] = -sin(zangle);
			zrotatematrix[1][0] = sin(zangle);
			zrotatematrix[1][1] = cos(zangle);
			zrotatematrix[2][2] = 1;


            MatrixItem.matrixmultiply(xrotatematrix,yrotatematrix,R1matrix,3,3,3);
			MatrixItem.matrixmultiply(R1matrix,zrotatematrix,R2matrix,3,3,3);

			MatrixItem.matrixmultiply(R2matrix,P1matrix,P2matrix,3,3,3);
            MatrixItem.inversematrix(P2matrix,P2inversematrix,3);
*/
			MatrixItem.inversematrix(P1matrix,P1inversematrix,3);
                    
			c1c2vector[0] = m_dXPosition-pDoc->m_pSourceImage->m_dCAM[0];
			c1c2vector[1] = m_dYPosition-pDoc->m_pSourceImage->m_dCAM[1];
			c1c2vector[2] = m_dZPosition-pDoc->m_pSourceImage->m_dCAM[2];

			MatrixItem.matrixvectormultiply(P1inversematrix,c1c2vector,eyevector,3,3);
		    m_dWValue = eyevector[2];
			double xvalue,yvalue;
			ASSERT(fabs(m_dWValue)>TINY);
			xvalue = eyevector[0]/eyevector[2];
			yvalue = eyevector[1]/eyevector[2];
            
			int size;
			size = INPUTIMAGESIZE-1;
			int xdvalue,ydvalue;
            xdvalue = (int)(xvalue+0.5);
			ydvalue = (int)(yvalue+0.5);

			m_x_value = xdvalue;
			m_y_value = ydvalue;

			if((xdvalue<0)&&(ydvalue>=size))
				m_dZone = 1;
			if((xdvalue>=0)&&(xdvalue<size)&&(ydvalue>=size))
				m_dZone = 2;
			if((xdvalue>=size)&&(ydvalue>=size))
				m_dZone = 3;
			if((xdvalue<0)&&(ydvalue>=0)&&(ydvalue<size))
				m_dZone = 4;
			if((xdvalue>=0)&&(xdvalue<size)&&(ydvalue>=0)&&(ydvalue<size))
				m_dZone = 5;
			if((xdvalue>=size)&&(ydvalue>=0)&&(ydvalue<size))
				m_dZone = 6;
			if((xdvalue<0)&&(ydvalue<0))
				m_dZone = 7;
			if((xdvalue>=0)&&(xdvalue<size)&&(ydvalue<0))
				m_dZone = 8;
			if((xdvalue>=size)&&(ydvalue<0))
				m_dZone = 9;

			if(m_dWValue<0) m_dZone = -m_dZone;
			getdata = UpdateData(FALSE);


	}else{
	}

	for(i=0;i<3;i++){
		//delete xrotatematrix[i];
		//delete yrotatematrix[i];
		//delete zrotatematrix[i];
        //delete P2matrix[i];
		delete P1matrix[i];
		delete P1inversematrix[i];
		//delete R1matrix[i];
		//delete R2matrix[i];
	};

	delete c1c2vector;
	delete eyevector;
//	delete xrotatematrix;
//	delete yrotatematrix;
//	delete zrotatematrix;
//	delete P2matrix;
	delete P1matrix;
	delete P1inversematrix;
//	delete R1matrix;
//	delete R2matrix;

}
