#ifdef NOVISUAL
#warning "testKMeans needs ICE with visualization !!"
int main(int argc, char **argv) {};

#else

#include "core/vector/VectorT.h"
#include "core/vector/MatrixT.h"
#include "core/image/ImageT.h"
#include "core/imagedisplay/ImageDisplay.h"
#include <iostream>

#include <core/image/CrossT.h>
#include <core/imagedisplay/SimpleSelector.h>

using namespace std;

using namespace NICE;

#include <vislearning/math/cluster/KMeans.h>
#include <vislearning/math/cluster/KMeansOnline.h>
#include <vislearning/math/cluster/KMeansMatlab.h>
#include <vislearning/math/cluster/KMeansHeuristic.h>
#include <vislearning/math/cluster/SpectralCluster.h>
#include <vislearning/math/cluster/HierarchicalCluster.h>

#include <vislearning/math/cluster/GMM.h>

using namespace OBJREC;

int main(int argc, char **argv)
{
#ifndef __clang__
#ifndef __llvm__
	std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
#endif
#endif

	NICE::Image panel(200, 200);
	NICE::Image overlay(panel.width(), panel.height());
	panel.set(255);
	overlay.set(0);

	VVector x;
	VVector prototypes;
	std::vector<double> weights;
	std::vector<int> assignments;

	char c;
	const int noClasses = 2;

	do
	{
		vector<CoordT<double> > points;
		NICE::selectPoints(panel, points, "Select some points!");

		x.clear();

		for (vector<CoordT<double> >::const_iterator i = points.begin();
							i != points.end(); i++)
		{
			NICE::Vector xvec(2);
			xvec[0] = i->x;
			xvec[1] = i->y;
			x.push_back(xvec);
		}


		ClusterAlgorithm *clusteralg = new GMM(noClasses);

//		ClusterAlgorithm *clusteralg = new KMeans ( noClasses);
//		ClusterAlgorithm *clusteralg = new HierarchicalCluster( 0); //automatic cluster size estimation
//		ClusterAlgorithm *clusteralg = new KMeansHeuristic ( noClasses);


		//ClusterAlgorithm *clusteralg = new SpectralCluster ( noClasses, alpha );
		//ClusterAlgorithm *clusteralg = new KMeansOnline ( noClasses );

		clusteralg->cluster(x, prototypes, weights, assignments);
		delete clusteralg;

		overlay.set(0);

		for (size_t i = 0 ; i < prototypes.size() ; i++)
		{
			fprintf(stderr, "prototype %d (%f, %f)\n", (int)i, prototypes[i][0], prototypes[i][1]);
			Cross cross(Coord((int)prototypes[i][0], (int)prototypes[i][1]), 7);
			overlay.draw(cross, i + 1);
		}

		fprintf(stderr, "assignments: %d\n", (int)assignments.size());

		for (size_t i = 0 ; i < assignments.size() ; i++)
		{
			Cross cross(Coord((int)x[i][0], (int)x[i][1]) , 7);
			overlay.draw(cross, assignments[i] + 1);
		}

		NICE::showImageOverlay(panel, overlay, "Clustering results");

		c = getchar();
	}
	while (c != 'q');

	return 0;
}

#endif
