/*
 *  Brno University of Technology
 *
 *  Copyright (C) 2009-2010 by Brno University of Technology and the contributors
 *
 *  Complete list of developers available at our web site:
 *
 *       http://spl.utko.feec.vutbr.cz
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */
package cz.vutbr.feec.utko.opencv.operators.imageProcessing;

import java.util.List;

import com.googlecode.javacv.cpp.opencv_core.CvMat;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.OperatorDescription;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.ports.InputPort;
import com.rapidminer.operator.ports.OutputPort;
import com.rapidminer.operator.ports.metadata.GenerateNewMDRule;
import com.rapidminer.operator.ports.metadata.MetaData;
import com.rapidminer.operator.ports.metadata.SimplePrecondition;
import com.rapidminer.parameter.ParameterType;
import com.rapidminer.parameter.ParameterTypeInt;
import com.sun.jna.ptr.DoubleByReference;
import com.sun.jna.ptr.LongByReference;

import cz.vutbr.feec.imageprocessing.IOObject.PointsIOObject;
import cz.vutbr.feec.imageprocessing.IOObject.innerObject.InterrestArea;
import cz.vutbr.feec.utko.opencv.io.OpenCVIOObject;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;


public class KMeans extends Operator {

	protected InputPort inPoints = getInputPorts().createPort("points");
	protected OutputPort outPointsImg = getOutputPorts().createPort("image");
	protected OutputPort outPoints = getOutputPorts().createPort("points");	

	
	/*PARAMS*/
	private static final String PARAM1 = "number of clusters";
	
	public KMeans(OperatorDescription description) {
		super(description);
		
		inPoints.addPrecondition(new SimplePrecondition(inPoints, new MetaData(
				PointsIOObject.class)));
		
		getTransformer().addRule(
				new GenerateNewMDRule(outPointsImg, OpenCVIOObject.class));
		
		getTransformer().addRule(
				new GenerateNewMDRule(outPoints, PointsIOObject.class));
		
	}
	

	
	public final void doWork() throws OperatorException
	{
		PointsIOObject original1 = inPoints.getData();

		int cluster_count = getParameterAsInt(PARAM1);
		int sample_count = original1.getSize();
		
		
		CvMat points = CvMat.take(sample_count, 1, CV_MAKETYPE(CV_32F,2));
		CvMat clusters = CvMat.take(sample_count, 1, CV_MAKE_TYPE(CV_32S, 1));//cvCreateMat( sample_count, 1, CV_32SC1 );

		int counter = 0;
		for (InterrestArea interrestArea : original1) {
			double x = interrestArea.getX();
			double y = interrestArea.getY();
			points.put(0,counter, 0, x);
			points.put(0,counter, 1, y);
			counter++;
		}
		
		CvRNG rng = new CvRNG();
		int flags = 0;
		CvMat centers = CvMat.take(cluster_count, 1, CV_MAKETYPE(CV_32F,2));
		double[] compactness = new double[1];
		
		
		cvKMeans2(points, cluster_count, clusters,(cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0)), 1, rng, flags, centers, compactness);

		
		IplImage img = cvCreateImage( cvSize(original1.getWidth(), original1.getHeight()), IPL_DEPTH_8U, 3 );
		
		
		PointsIOObject out1 = new PointsIOObject(img.width(), img.height(), 0,0);
		  for(int i = 0; i < sample_count; i++ )
	      {
			int clustr = (int)clusters.get(0, i);
			int c_clustr = (int)(255.0*clustr/(double)cluster_count);	
			
			CvPoint p = new CvPoint((int)points.get(0, i, 0),(int)points.get(0, i, 1));
			CvScalar s = cvScalar(c_clustr,0,0,0);
			cvCircle(img, p, 2, s, 2, 1, 0);
			
			s = cvScalar(255,255,255,255);
			cvCircle(img, p, 1, s, 1, 1, 0);
			
				  //out.add(new ShapeValue((int)points.get(0, i, 0),(int)points.get(0, i, 1), 1, 1, 1, (byte)3));
			 // out1.addPoint((int)points.get(0, i, 0),(int)points.get(0, i, 1), c_clustr);
	      }

			/*  for(int i = 0; i < cluster_count; i++ )
		      {
				  out1.addPoint(new Point((int)centers.get(0, i, 0),(int)centers.get(0, i, 1), i));
		      }
		  */
		  
		 OpenCVIOObject out = new OpenCVIOObject(img);//(original1.getWidth(), original1.getHeight(), 0, 0);

			
		
		outPointsImg.deliver(out);
		outPoints.deliver(out1);
	}
	
	
	@Override
	public List<ParameterType> getParameterTypes()
	{
		List<ParameterType> types = super.getParameterTypes();

		types.add(new ParameterTypeInt(PARAM1, "number of clusters",1, 100, 5));
		
		return types;
	}

}
