#ifndef _COORD_WARPING_H
#define _COORD_WARPING_H

#include "ofMain.h"
#include "ofAddons.h"
#include "guiQuad.h"

#include "laserUtils.h"
#include "baseGui.h"

//--------------------------
// constants
#include "ofCvConstants.h"

//--------------------------
// images
#include "ofCvGrayscaleImage.h"
#include "ofCvColorImage.h"

//--------------------------
// contours

#include "ofCvContourFinder.h"


//our other app objects

class coordWarping{


	public:
	
		//---------------------------
		coordWarping(){
			translate = cvCreateMat(3,3,CV_32FC1);
		}

		//---------------------------
		void calculateMatrix(ofPoint2f src[4], ofPoint2f dst[4]){
		
			cvSetZero(translate);
			for (int i = 0; i < 4; i++){
				cvsrc[i].x = src[i].x;
				cvsrc[i].y = src[i].y;
				cvdst[i].x = dst[i].x;
				cvdst[i].y = dst[i].y;
			}
			
			cvWarpPerspectiveQMatrix(cvsrc, cvdst, translate);  // calculate homography
			
			int n       = translate->cols;
			float *data = translate->data.fl;
/*
			printf("\n");

			for(int i = 0; i < 9; i++){
				
				if(i%3==0){
					printf("\n");
				}
				
				printf("[%f]", data[i]);
			}
			
			printf("\n");
*/
		}

		
		//---------------------------		
		ofPoint2f transform(float xIn, float yIn){

			ofPoint2f out;
		
			float *data = translate->data.fl;
 			
 			float a = data[0];
 			float b = data[1];
 			float c = data[2];
 			float d = data[3];
 			
 			float e = data[4];
 			float f = data[5];
 			float i = data[6];
 			float j = data[7];
 			
 			//from Myler & Weeks - so fingers crossed!
			out.x = (a*xIn + b*yIn + c) / (i*xIn + j*xIn + 1);
			out.y = (d*xIn + e*yIn + f) / (i*xIn + j*xIn + 1);
			
			return out;
		}		
		
	
	protected:
		
		CvPoint2D32f cvsrc[4];
		CvPoint2D32f cvdst[4];
		CvMat *translate;
				
};

#endif