/**------------------------------------------------------------------------
 * @file
 *
 * Copyright 2001 by the University of Toronto (UT).
 *
 *                       All Rights Reserved
 * 
 * Permission to copy and modify this software and its documentation 
 * only for internal use in your organization is hereby granted, 
 * provided that this notice is retained thereon and on all copies.  
 * UT makes no representations as to the suitability and operability 
 * of this software for any purpose.  It is provided "as is" without 
 * express or implied warranty.
 *
 * UT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
 * SHALL UT BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * No other rights, including for example, the right to redistribute 
 * this software and its documentation or the right to prepare 
 * derivative works, are granted unless specifically provided in a 
 * separate license agreement.
 *
 * Author: Diego Macrini
 *
 *-----------------------------------------------------------------------*/
#include <map>
#include <ShockGraphView.h>
#include <BoneGraphView.h>
#include "ShapeMatcher.h"
#include "MatchingResults.h"

void MatchingResults::CopyResults(const DAG* pTarget, const STATINFO& matSt, 
	DAGDatabase& queryDB, DAGDatabase& modelDB, int nQueryID) 
{
	m_dags.push_back(std::list<DAGPtr>());

	std::list<DAGPtr>& dagList = m_dags.back();

	dagList.push_back(queryDB.ReadDAG(nQueryID));

	for (int i = 0, n = 0; i < matSt.matchList.size() && n < m_nMaxModelsPerQuery; i++)
	{
		if (nQueryID != matSt.matchList[i].nDAGId)
		{
			dagList.push_back(modelDB.ReadDAG(matSt.matchList[i].nDAGId));
			n++;
		}
	}
}

void MatchingResults::ShowResultsAsTable()
{
	//ShowError("I AM HERE");

	std::list< std::list<DAGPtr> >::iterator rowIt = m_dags.begin();
	std::list<DAGPtr>::iterator colIt;
	int i, j;

	/*const int numRows = MIN(m_nMaxModelsPerQuery + 1, (int)m_dags.size());

	for (rowIt = m_dags.begin(); rowIt != m_dags.end(); ) // see increment bellow
	{
		SmartMatrix<const DAG*> dagMat(numRows, m_nMaxModelsPerQuery + 1);

		for (i = 0; i < dagMat.NRows() && rowIt != m_dags.end(); rowIt++, i++)
		{
			for (colIt = rowIt->begin(), j = 0; colIt != rowIt->end(); colIt++, j++)
			{
				dagMat[i][j] = (const DAG*) *colIt;
			}
		}

		BoneGraphView bgview;

		bgview.SetBlackShapeMode(true); // true => draw table

		bgview.Create(dagMat);
	}*/

	/*int dagIds[] = {1, 11, 27, 33, 50, 55, 60, 73, 76};
	const int numRows = 9;*/

	//int dagIds[] = {4, 15, 32, 36, 49, 66, 81, 92, 104, 110, 
	//	125, 141, 146, 158, 176, 181, 193, 206};
	
	//int dagIds[] = {0, 1,3,5,8,20,22,23,38,42,47,48,50,56,75,78,
	//	81,120,145,157,165,166,169,171,172,175,185,199,209,210,215};

	int dagIds[] = {0,3,5,6,23,42,45,48,50,56,75,76,78,81,157,165,
		169,171,172,179,199,215};

	const int numRows = sizeof(dagIds) / sizeof(int);

	SmartMatrix<const DAG*> dagMat(numRows, m_nMaxModelsPerQuery + 1);

	for (rowIt = m_dags.begin(); rowIt != m_dags.end(); rowIt++)
	{
		for (i = 0; i < numRows; i++)
		{
			if ((*rowIt->begin())->GetDAGId() == dagIds[i])
			{
				for (colIt = rowIt->begin(), j = 0; colIt != rowIt->end(); colIt++, j++)
				{
					dagMat[i][j] = (const DAG*) *colIt;
				}

				break;
			}
		}
	}

	BoneGraphView bgview;

	bgview.SetBlackShapeMode(true); // true => draw table

	bgview.Create(dagMat);
}
