#include "hausdorff.h"
#include "../ModelViewer/ColorMap.h"
extern CColorMap g_colorMap;

int searchInPari(vector<pair<int,int>> pairs,pair<int,int> target)
{
	int index = -1;
	for(int i=0;i<pairs.size();i++)
	{
		pair<int,int> ipari = pairs[i];
		if(ipari.first = target.first&&ipari.second==target.second)
			return i;	
	}
	return index;
}

void matchShapes(char srcImgname[255],char dstImgname[255])
{
	
	vector<vector<int>> srcRowset;
	vector<vector<int>> dstRowset;

	cv::Mat srcImage,dstImage;
	vector<Node> dstRegions = loadImage(dstImgname,dstRowset,dstImage,false);

	vector<Node> srcRegions = loadImage(srcImgname,srcRowset,srcImage,true);
	
	
	cv::Mat rimg(512,512,CV_8UC3);	
	for(int j=0;j<rimg.cols;j++)
		for(int k=0;k<rimg.rows;k++)
			rimg.at<cv::Vec3b>(j,k) =cv::Vec3b(255,255,255);

	//for(int i=0;i<nodes.size();i++)
	//{

	//}



	vector<pair<int,int>> pairs;
	float  cost = matchRegions(srcRegions,dstRegions,pairs);
	
	vector<vector<int>> dstToSrc(dstRegions.size());
	vector<vector<int>> srcToDst(srcRegions.size());
	for(int i=0;i<pairs.size();i++)
	{
	
		int src = pairs[i].first;
		int dst = pairs[i].second;
		cout<<src<<'\t'<<dst<<endl;
		dstToSrc[dst].push_back(src);
		srcToDst[src].push_back(dst);
		int start = srcRegions[src].start;
		int end=srcRegions[src].end;
		for(int j=start;j<=end;j++)
			for(int k=0;k<srcImage.rows;k++)
			{
				cv::Vec3b intensity = srcImage.at<cv::Vec3b>(j,k);
				if(intensity[0]!=255||intensity[1]!=255||intensity[2]!=255)				
				//if(rimg.at<UCHAR>(j,k)==0)
				{
					Color rgb = g_colorMap.GetClassColor((dst+2)%92);
					UCHAR r = rgb[0]*255;
					UCHAR g = rgb[1]*255;
					UCHAR b = rgb[2]*255;
					rimg.at<cv::Vec3b>(j,k) =cv::Vec3b(r,g,b);
				}	
			}
	}
	
	vector<bool> pairProcessed(pairs.size(),false);
	vector<float> ratios;
	for(int i=0;i<pairs.size();i++)
	{
		if(pairProcessed[i])continue;
		int src = pairs[i].first;
		int dst = pairs[i].second;
		int numSrcToDst = srcToDst[src].size();
		int numDstToSrc = dstToSrc[dst].size();
		float ratio = 0;
		if(numDstToSrc>numSrcToDst)
		{
			float sumHeight = 0;
			int startRegion = INT_MAX;
			for(int j=0;j<dstToSrc[dst].size();j++)
			{
				int jsrc = dstToSrc[dst][j];
				sumHeight += srcRegions[jsrc].height;
				int index = searchInPari(pairs,pair<int,int>(jsrc,dst));
				if(index!=-1)
					pairProcessed[index] = true;	
				if(jsrc<startRegion)
					startRegion = jsrc;
			}
			ratio = dstRegions[dst].height/sumHeight;
			for(int j=0;j<sumHeight;j++)
			{
				int srcRow = srcRegions[startRegion].start + j;
				int dstRow = (int)(ratio*j) + dstRegions[dst].start;
				vector<int> matchedPoints;
					float shiftcost = compute_dist(srcRowset[srcRow],dstRowset[dstRow],matchedPoints);
				for(int k=0;k<srcRowset[srcRow].size();k++)
				{
					int xSrc = srcRow;
					int ySrc = srcRowset[srcRow][k];
					int xDst = dstRow;
					int yDst = matchedPoints[k];
					cv::Vec3b intensity = dstImage.at<cv::Vec3b>(xDst,yDst);
					if(intensity[0]==255&&intensity[1]==255&&intensity[2]==255)
						printf("debug\n");
					srcImage.at<cv::Vec3b>(xSrc,ySrc) = dstImage.at<cv::Vec3b>(xDst,yDst);
				}
				//imwrite("d:/tests.bmp",srcImage);

			}

		}
		else if(numDstToSrc<=numSrcToDst)
		{
			float sumHeight = 0;
			int startRegion = INT_MAX;
			for(int j=0;j<srcToDst[src].size();j++)
			{
				float jdst = srcToDst[src][j];
				sumHeight += dstRegions[jdst].height;
				int index = searchInPari(pairs,pair<int,int>(src,jdst));
				if(index!=-1)
					pairProcessed[index] = true;	
				if(jdst<startRegion)
					startRegion = jdst;
			}
			ratio = sumHeight/srcRegions[src].height;
			for(int j=0;j<srcRegions[src].height;j++)
			{
				int srcRow = srcRegions[src].start + j;
				int dstRow = (int)(ratio*j) + dstRegions[startRegion].start;
				vector<int> matchedPoints;
		
				float shiftcost = compute_dist(srcRowset[srcRow],dstRowset[dstRow],matchedPoints);
				for(int k=0;k<srcRowset[srcRow].size();k++)
				{
					int xSrc = srcRow;
					int ySrc = srcRowset[srcRow][k];
					int xDst = dstRow;
					int yDst = matchedPoints[k];
					cv::Vec3b intensity = dstImage.at<cv::Vec3b>(xDst,yDst);
					if(intensity[0]==255&&intensity[1]==255&&intensity[2]==255)
						printf("debug\n");
					srcImage.at<cv::Vec3b>(xSrc,ySrc) = dstImage.at<cv::Vec3b>(xDst,yDst);
				}
				
				
			}


		}
		/*else
		{
			ratio = (float)dstRegions[dst].height/(float)srcRegions[src].height;
			for(int j=0;j<srcRegions[src].height;j++)
			{
				int srcRow = srcRegions[src].start + j;
				int dstRow = (int)(ratio*j) + dstRegions[startRegion].start;
				vector<int> matchedPoints;
				
				float shiftcost = compute_dist(srcRowset[srcRow],dstRowset[dstRow],matchedPoints);
				for(int k=0;k<srcRowset[srcRow].size();k++)
				{
					int xSrc = srcRow;
					int ySrc = srcRowset[srcRow][k];
					int xDst = dstRow;
					int yDst = matchedPoints[k];
					cv::Vec3b intensity = dstImage.at<cv::Vec3b>(xDst,yDst);
					
					srcImage.at<cv::Vec3b>(xSrc,ySrc) = dstImage.at<cv::Vec3b>(xDst,yDst);
				}
				imwrite("d:/tests.bmp",srcImage);

			}
		}*/
		ratios.push_back(ratio);
	}
	imwrite("E:/data/Large-Chairs/test/testdesc/tests.bmp",srcImage);


	imwrite("E:/data/Large-Chairs/test/testdesc/matched.bmp",rimg);


}