/** 
 * @file IDKLTSampling.cpp
 * @brief random interest point sampling
 * @author Erik Rodner
 * @date 02/05/2008

 */
#include <iostream>

#include "vislearning/features/localfeatures/IDKLTSampling.h"
#include "core/image/ImageTools.h"

using namespace OBJREC;

using namespace std;

using namespace NICE;

IDKLTSampling::IDKLTSampling(const Config *conf, int _numSamples) :
	numSamples(_numSamples)
{
	minScale = conf->gD("IDKLTSampling", "min_scale", 1.0);
	fixedOrientation = conf->gB("IDKLTSampling", "fixed_orientation", true);

	srand(time(NULL));
}

IDKLTSampling::~IDKLTSampling()
{
}

int IDKLTSampling::getInterests(const Image & img,
		std::vector<Vector> & positions) const
{
	std::auto_ptr<Matrix> corners(KLTCornerDetector(img, numSamples, 100, 1,2));
	for (int j = 0; j < corners->rows(); j++)
	{
		int x, y;
		x = (*corners)(j, 0);
		y = (*corners)(j, 1);
		Vector pos(3);
		pos[0] = x;
		pos[1] = y;
		pos[2] = 0;
		positions.push_back(pos);
	}
	fprintf(stderr, "IDKLTSampling finished\n");
	return 0;
}
int IDKLTSampling::getInterests(const ImagePyramid & imp,
		std::vector<Vector> & positions) const
{
	int numLevels = imp.getNumLevels();
	std::vector<Matrix *> cornersAtLevel;
	for (int level = 0; level < numLevels; level++)
	{
		const NICE::Image & img = imp.getLevel(level);
		cornersAtLevel.push_back(KLTCornerDetector(img, numSamples, 100, 1, 2));
	}
	for (int level = 0; level < numLevels; level++)
	{
		for (int j = 0; j < cornersAtLevel[level]->rows(); j++)
		{
			const NICE::Image & img = imp.getLevel(level);
			int x, y;
			x = (*cornersAtLevel[level])(j, 0);
			y = (*cornersAtLevel[level])(j, 1);
			double xo, yo;
			imp.getOriginalCoordinates(x, y, level, xo, yo);

			Vector pos(3);
			pos[0] = xo;
			pos[1] = yo;
			pos[2] = level;
			positions.push_back(pos);

		}
	}
	for (int level = 0; level < numLevels; level++)
	{
		delete cornersAtLevel[level];
	}
	fprintf(stderr, "IDKLTSampling finished\n");
	return 0;
}
