// FocusCameraMoveDialog.cpp : implementation file
//

#include "stdafx.h"
#include "ViewTransformation.h"
#include "FocusCameraMoveDialog.h"
#include "Matrix.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFocusCameraMoveDialog dialog


CFocusCameraMoveDialog::CFocusCameraMoveDialog(CViewTransformationView *pView,CWnd* pParent /*=NULL*/)
	: CDialog(CFocusCameraMoveDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFocusCameraMoveDialog)
	m_zoom_value = 0.0;
	m_rotate_x = 0.0;
	m_rotate_y = 0.0;
	m_rotate_z = 0.0;
	m_w_value = 0.0;
	m_x_value = 0;
	m_y_value = 0;
	m_zone_no = 0;
	//}}AFX_DATA_INIT
	this->m_pViewPtr = pView;
}


void CFocusCameraMoveDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFocusCameraMoveDialog)
	DDX_Text(pDX, ID_ZOOM_VALUE, m_zoom_value);
	DDX_Text(pDX, IDC_ROTATE_X, m_rotate_x);
	DDX_Text(pDX, IDC_ROTATE_Y, m_rotate_y);
	DDX_Text(pDX, IDC_ROTATE_Z, m_rotate_z);
	DDX_Text(pDX, IDC_W_VALUE, m_w_value);
	DDX_Text(pDX, IDC_X_VALUE, m_x_value);
	DDX_Text(pDX, IDC_YVALUE, m_y_value);
	DDX_Text(pDX, IDC_ZONE_NO, m_zone_no);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFocusCameraMoveDialog, CDialog)
	//{{AFX_MSG_MAP(CFocusCameraMoveDialog)
	ON_BN_CLICKED(IDC_HINT, OnHint)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFocusCameraMoveDialog message handlers

void CFocusCameraMoveDialog::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,*WtoOriginVector,*ChangedVector;
	c1c2vector = (double *) malloc(3*sizeof(double));
    eyevector = (double *)malloc(3*sizeof(double));
	WtoOriginVector = (double *) malloc(3*sizeof(double));
    ChangedVector = (double *)malloc(3*sizeof(double));


	double ** P2matrix,**R1matrix,**R2matrix,**P1matrix;
	double **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( 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;
			WtoOriginVector[i] = 0.0;
            ChangedVector[i] = 0.0;
		};
	
	double xangle,yangle,zangle;
	xangle = m_rotate_x*2*PIE/360.0;
	yangle = m_rotate_y*2*PIE/360.0;
	zangle = m_rotate_z*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]; 
				
			};
            
			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);


			double w3[3];
			w3[0] = (pDoc->m_pSourceImage->m_dBWXmax+pDoc->m_pSourceImage->m_dBWXmin)/2.0;
			w3[1] = (pDoc->m_pSourceImage->m_dBWYmax+pDoc->m_pSourceImage->m_dBWYmin)/2.0;
			w3[2] = (pDoc->m_pSourceImage->m_dBWZmax+pDoc->m_pSourceImage->m_dBWZmin)/2.0;

			WtoOriginVector[0] = pDoc->m_pSourceImage->m_dCAM[0]-w3[0];
            WtoOriginVector[1] = pDoc->m_pSourceImage->m_dCAM[1]-w3[1];
			WtoOriginVector[2] = pDoc->m_pSourceImage->m_dCAM[2]-w3[2];

			MatrixItem.matrixvectormultiply(R2matrix,WtoOriginVector,ChangedVector,3,3);
			for(i=0;i<3;i++){
				ChangedVector[i] = ChangedVector[i]*m_zoom_value;
			};

			double cameraposition[3];
			for(i = 0;i<3;i++){
				cameraposition[i]=ChangedVector[i]+w3[i];
			};
		//	MatrixItem.matrixmultiply(R2matrix,P1matrix,P2matrix,3,3,3);
//            MatrixItem.inversematrix(P2matrix,P2inversematrix,3);

			MatrixItem.inversematrix(P1matrix,P1inversematrix,3);
            
			for(i =0;i<3;i++){
				c1c2vector[i] = cameraposition[i]-pDoc->m_pSourceImage->m_dCAM[i];
			};
			

			MatrixItem.matrixvectormultiply(P1inversematrix,c1c2vector,eyevector,3,3);
		    m_w_value = eyevector[2];
			double xvalue,yvalue;
			ASSERT(fabs(m_w_value)>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_zone_no = 1;
			if((xdvalue>=0)&&(xdvalue<size)&&(ydvalue>=size))
				m_zone_no = 2;
			if((xdvalue>=size)&&(ydvalue>=size))
				m_zone_no = 3;
			if((xdvalue<0)&&(ydvalue>=0)&&(ydvalue<size))
				m_zone_no = 4;
			if((xdvalue>=0)&&(xdvalue<size)&&(ydvalue>=0)&&(ydvalue<size))
				m_zone_no = 5;
			if((xdvalue>=size)&&(ydvalue>=0)&&(ydvalue<size))
				m_zone_no = 6;
			if((xdvalue<0)&&(ydvalue<0))
				m_zone_no = 7;
			if((xdvalue>=0)&&(xdvalue<size)&&(ydvalue<0))
				m_zone_no = 8;
			if((xdvalue>=size)&&(ydvalue<0))
				m_zone_no = 9;

			if(m_w_value<0) m_zone_no = -m_zone_no;
			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 ChangedVector;
	delete WtoOriginVector;
	delete xrotatematrix;
	delete yrotatematrix;
	delete zrotatematrix;
	delete P2matrix;
	delete P1matrix;
	delete P1inversematrix;
	delete R1matrix;
	delete R2matrix;
	
}
