/** 
* @file CachedQuadraticDistances.cpp
* @brief kernel function which needs quadratic distances between feature vectors
* @author Erik Rodner
* @date 01/20/2010

*/
#include <iostream>

#include "CachedQuadraticDistances.h"

using namespace std;
using namespace NICE;
using namespace OBJREC;

void CachedQuadraticDistances::calcKernelData ( const VVector & vecSet, KernelData *kernelData ) const
{
	//cerr << "CachedQuadraticDistances::calcKernelData()" << endl;
	Matrix *quadraticDistances = new Matrix ( vecSet.size(), vecSet.size() );;
	// FIXME: memory leak -> only if vecSet.size() is to big (ca. >>5000)
	int ii = 0;
	for ( VVector::const_iterator i  = vecSet.begin();
						   i != vecSet.end();
						   i++, ii++ )
	{
		const NICE::Vector & x = *i;
		int jj = ii;
		for ( VVector::const_iterator j  = i;
						   j != vecSet.end();
						   j++, jj++ )
		{
			const NICE::Vector & y = *j;
			double distance = 0.0;
			for ( size_t k = 0 ; k < x.size() ; k++ )
			{
				double diff = x[k] - y[k];
				distance += diff*diff;
			}
			(*quadraticDistances)(ii, jj) = distance;
			(*quadraticDistances)(jj, ii) = distance;
		}
	}

	kernelData->setCachedMatrix ( KernelData::QUADRATIC_DISTANCES, quadraticDistances );

	Kernel::calcKernelData ( vecSet, kernelData );
}
