// ViewTransformationView.cpp : implementation of the CViewTransformationView class
//

#include "stdafx.h"
#include "ViewTransformation.h"
#include "Matrix.h"

#include "ViewTransformationDoc.h"
#include "ViewTransformationView.h"
#include "CameraFreeMoveDialog.h"
#include "FocusCameraMoveDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CViewTransformationView

IMPLEMENT_DYNCREATE(CViewTransformationView, CView)

BEGIN_MESSAGE_MAP(CViewTransformationView, CView)
	//{{AFX_MSG_MAP(CViewTransformationView)
	ON_WM_DESTROY()
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	ON_WM_SIZE()
	ON_WM_CREATE()
	ON_COMMAND(ID_LOAD_SOURCE_IMAGE, OnLoadSourceImage)
	ON_COMMAND(ID_SAVE_SOURCE, OnSaveSource)
	ON_COMMAND(ID_Free_Camera_Move, OnFreeCameraMove)
	ON_COMMAND(ID_Camera_Focus, OnCameraFocus)
	ON_COMMAND(ID_SAVE_DEST, OnSaveDest)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CViewTransformationView construction/destruction

CViewTransformationView::CViewTransformationView()
{
	// TODO: add construction code here

}

CViewTransformationView::~CViewTransformationView()
{
}

BOOL CViewTransformationView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
    cs.style |= WS_CLIPSIBLINGS|WS_CLIPCHILDREN;  
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CViewTransformationView drawing

void CViewTransformationView::OnDraw(CDC* pDC)
{
	CViewTransformationDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CViewTransformationView printing

BOOL CViewTransformationView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CViewTransformationView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CViewTransformationView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CViewTransformationView diagnostics

#ifdef _DEBUG
void CViewTransformationView::AssertValid() const
{
	CView::AssertValid();
}

void CViewTransformationView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CViewTransformationDoc* CViewTransformationView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CViewTransformationDoc)));
	return (CViewTransformationDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CViewTransformationView message handlers

void CViewTransformationView::OnDestroy() 
{
	CView::OnDestroy();
	m_GLView.Destroy( );
	
	// TODO: Add your message handler code here
	
}

BOOL CViewTransformationView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	
	return CView::OnEraseBkgnd(pDC);
}

void CViewTransformationView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	
	// TODO: Add your message handler code here
	
	BeginPaint(NULL);
		::glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		::glShadeModel(GL_FLAT);
		::glPixelStorei(GL_UNPACK_ALIGNMENT,1);
		int GLView[4];
		::glGetIntegerv(GL_VIEWPORT,GLView);
		::glMatrixMode(GL_MODELVIEW);
		::glLoadIdentity( );
		::gluOrtho2D(0,GLView[2],0,GLView[3] );
        if(m_CurrentView == SOURCEVIEW){
			ASSERT(m_ShowImage!=NULL);
				::glClear(GL_COLOR_BUFFER_BIT);
			::glRasterPos2i(0,0);
			::glDrawPixels(m_iWidthShowImage,m_iHeightShowImage,GL_RGB,GL_UNSIGNED_BYTE,m_ShowImage);
		    
		  }else if(m_CurrentView ==DESTVIEW){
			ASSERT(m_ShowImage!=NULL);
			::glClear(GL_COLOR_BUFFER_BIT);
			::glRasterPos2i(0,0);
			::glDrawPixels(m_iWidthShowImage,m_iHeightShowImage,GL_RGB,GL_UNSIGNED_BYTE,m_ShowImage);

		
			
		};
		

//		::glTranslated(0.0,0.0,1.0);
		m_GLView.FinishAndSwap ( );
	EndPaint( NULL);
	// Do not call CView::OnPaint() for painting messages
}

void CViewTransformationView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(cx<=0||cy<=0)
		return;
	::glViewport(0,0,cx,cy);
	
}

void CViewTransformationView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	m_GLView.Initialize( );
    m_CurrentView = INVALIDVIEW;
	m_ShowImage = NULL;
	m_iWidthShowImage = 0;
	m_iHeightShowImage = 0;
	
	
	// TODO: Add your specialized code here and/or call the base class
	
}

int CViewTransformationView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	m_GLView.Create(this);
	return 0;
}

void CViewTransformationView::OnLoadSourceImage() 
{
	// TODO: Add your command handler code here

	CFileDialog LoadSourceImageDialog(TRUE );
	if ( LoadSourceImageDialog.DoModal()  == IDOK )
	{    
	
		CString FileName;
		FileName.Empty( );
		FileName = LoadSourceImageDialog.GetPathName( );
		m_CurrentView = SOURCEVIEW;

    

	
		CViewImage * viewimageptr;
		
		viewimageptr = GetDocument( )->m_pDestImage;
		if(viewimageptr != NULL) delete viewimageptr;
		viewimageptr = GetDocument( )->m_pSourceImage;

		if(viewimageptr !=NULL) delete viewimageptr;
		viewimageptr = new CViewImage(FileName);
		GetDocument( )->m_pSourceImage = viewimageptr;

		if(m_ShowImage != NULL) delete m_ShowImage;
		
		int imagewidth;
		int imageheight;
        imagewidth = viewimageptr->GetImageWidth( );
		imageheight = viewimageptr->GetImageHeight( );

		m_iWidthShowImage = imagewidth;
		m_iHeightShowImage = imageheight;

		m_ShowImage = new GLubyte[imagewidth*imageheight*RGB];
        double * tempptr;
		tempptr = viewimageptr->GetImageRGBDptr( );
		for(int i =0;i<imagewidth;i++)
			for(int j=0;j<imagewidth;j++)
			{
				double *p;
				p = tempptr+j*imagewidth*RGBD+i*RGBD;
				GLubyte *q;
				q = m_ShowImage+j*imagewidth*RGB+i*RGB;
				q[R] = (GLubyte )p[R];
				q[G] = (GLubyte )p[G];
				q[B] = (GLubyte )p[B];
				
			};
		
	      SendMessage(WM_PAINT);
	}



	//test begin
/*	FILE *fp;

	
	if ( ( fp = fopen( "E:\\testfile", "w" ) ) == NULL ) 
	{
		TRACE( "can't open imagefile %s!", "E:\testfile.txt" );
		return;
	}

	BOOL istri;
	
   CMatrix matrixitem;
   istri = matrixitem.isvalidtriangle(2.3,3.3,7.6,8.7,9.8,1.4);
   for(int m=0;m<10;m++)
	   for(int n=0;n<10;n++){
		BOOL ispointin;
		
		ispointin = matrixitem.IsPointInTriangle(m,n,2.3,3.3,7.6,8.7,9.8,1.4);
		if(ispointin){
			double pvalue;
			pvalue = matrixitem.getaveragevalue(-10,100,10,m,n,2.3,3.3,7.6,8.7,9.8,1.4);
			fprintf(fp,"i=%d,j=%d,p=%lf\n",m,n,pvalue);


		};
	   };

	   fclose(fp);
*/	//test off
	
}

void CViewTransformationView::OnSaveSource() 
{
	static char BASED_CODE szFilter[] = "bmp";
 

	CFileDialog SaveFinalImageDialog(FALSE,szFilter );
	if ( SaveFinalImageDialog.DoModal()  == IDOK )
	{    
	
		CString FileName;
		FileName.Empty( );
		FileName = SaveFinalImageDialog.GetPathName( );
		ASSERT(GetDocument( )->m_pSourceImage!=NULL);
	    GetDocument( )->m_pSourceImage->StoreToBMP(FileName);
 


	}
	// TODO: Add your command handler code here
	
}

void CViewTransformationView::OnFreeCameraMove() 
{
	// TODO: Add your command handler code here
	CCameraFreeMoveDialog cameradialog(this);
	CViewImage * pViewSourceImage,* pViewDestImage;
	CViewTransformationDoc * pDoc;
	pDoc = GetDocument( );
    pViewSourceImage = pDoc->m_pSourceImage;
	pViewDestImage = pDoc->m_pDestImage;
	double camera2[3];
	double xangle,yangle,zangle;
	if ( cameradialog.DoModal()  == IDOK ){
	// construct dest in document.
     camera2[0] = cameradialog.m_dXPosition;
	 camera2[1] = cameradialog.m_dYPosition;
	 camera2[2] = cameradialog.m_dZPosition;
     
	 xangle = cameradialog.m_dXRotation;
	 yangle = cameradialog.m_dYRotation;
	 zangle = cameradialog.m_dZRotation;

	 if(pViewDestImage!= NULL) delete pViewDestImage;
	 
     pViewDestImage = new CViewImage(xangle,yangle,zangle,camera2,pViewSourceImage);
	 pDoc->m_pDestImage = pViewDestImage;

	 m_CurrentView = DESTVIEW;

    

	
	if(m_ShowImage != NULL) delete m_ShowImage;
	
	    int imagewidth;
		int imageheight;
        imagewidth = pViewDestImage->GetImageWidth( );
		imageheight = pViewDestImage->GetImageHeight( );

		m_iWidthShowImage = imagewidth;
		m_iHeightShowImage = imageheight;

		m_ShowImage = new GLubyte[imagewidth*imageheight*RGB];
        double * tempptr;
		tempptr = pViewDestImage->GetImageRGBDptr( );
		for(int i =0;i<imagewidth;i++)
			for(int j=0;j<imageheight;j++)
			{
				double *p;
				p = tempptr+j*imagewidth*RGBD+i*RGBD;
				GLubyte *q;
				q = m_ShowImage+j*imagewidth*RGB+i*RGB;
				q[R] = (GLubyte )p[R];
				q[G] = (GLubyte )p[G];
				q[B] = (GLubyte )p[B];
				
			};
		
	      SendMessage(WM_PAINT);
	}

     //get imagedata in View
	 //sent WM_PAINT





    //construct data in Veiw
		// send message for paining.

		



	
/*	CMatrix matrixitem;
	double a[3],b[3],c[3];
	a[0] = 1;
	a[1] = 0.5;
	a[2] = 0.5;
	b[0] = 2;
	b[1] = 3;
	b[2] = 5;
	matrixitem.getvectorcrossproduct(a,b,c);
*/

}

void CViewTransformationView::OnCameraFocus() 
{
	// TODO: Add your command handler code here
	CFocusCameraMoveDialog cameradialog(this);
	CViewImage * pViewSourceImage,* pViewDestImage;
	CViewTransformationDoc * pDoc;
	pDoc = GetDocument( );
    pViewSourceImage = pDoc->m_pSourceImage;
	pViewDestImage = pDoc->m_pDestImage;
	double xangle,yangle,zangle,zoom_value;
	if ( cameradialog.DoModal()  == IDOK ){
	// construct dest in document.
     zoom_value = cameradialog.m_zoom_value;
	 xangle = cameradialog.m_rotate_x;
	 yangle = cameradialog.m_rotate_y;
	 zangle = cameradialog.m_rotate_z;

	 if(pViewDestImage!= NULL) delete pViewDestImage;
	 
     pViewDestImage = new CViewImage(xangle,yangle,zangle,zoom_value,pViewSourceImage);
	 pDoc->m_pDestImage = pViewDestImage;

	 m_CurrentView = DESTVIEW;

    

	
	if(m_ShowImage != NULL) delete m_ShowImage;
	
	    int imagewidth;
		int imageheight;
        imagewidth = pViewDestImage->GetImageWidth( );
		imageheight = pViewDestImage->GetImageHeight( );

		m_iWidthShowImage = imagewidth;
		m_iHeightShowImage = imageheight;

		m_ShowImage = new GLubyte[imagewidth*imageheight*RGB];
        double * tempptr;
		tempptr = pViewDestImage->GetImageRGBDptr( );
		for(int i =0;i<imagewidth;i++)
			for(int j=0;j<imageheight;j++)
			{
				double *p;
				p = tempptr+j*imagewidth*RGBD+i*RGBD;
				GLubyte *q;
				q = m_ShowImage+j*imagewidth*RGB+i*RGB;
				q[R] = (GLubyte )p[R];
				q[G] = (GLubyte )p[G];
				q[B] = (GLubyte )p[B];
				
			};
		
	      SendMessage(WM_PAINT);
	}

	
}

void CViewTransformationView::OnSaveDest() 
{
	// TODO: Add your command handler code here
	static char BASED_CODE szFilter[] = "bmp";
 

	CFileDialog SaveFinalImageDialog(FALSE,szFilter );
	if ( SaveFinalImageDialog.DoModal()  == IDOK )
	{    
	
		CString FileName;
		FileName.Empty( );
		FileName = SaveFinalImageDialog.GetPathName( );
		ASSERT(GetDocument( )->m_pDestImage!=NULL);
	    GetDocument( )->m_pDestImage->StoreToBMP(FileName);
	}
	
}
