package ac.essex.ooechs.facedetection.evolved.ga;

import ga.core.GAIndividual;
import ac.essex.ooechs.imaging.commons.fast.IntegralImage;
import ac.essex.ooechs.facedetection.evolved.ga.genes.*;

/**
 * This individual defines the parameters for a Haar Imaging function.
 *
 * @author Olly Oechsle, University of Essex, Date: 21-May-2007
 * @version 1.0
 */
public class GAFeatureIndividual extends GAIndividual {

    public static final int TWO_RECT = 2;
    public static final int THREE_RECT = 3;
    public static final int FOUR_RECT = 4;

    public int mode;

    public static int startX, startY;

    public int x, y, width, height, adjacency;

    public GAFeatureIndividual(int mode) {
        super(5);
        this.mode = mode;
        genes[0] = new XGene();
        genes[1] = new YGene();
        genes[2] = new WidthGene();
        genes[3] = new HeightGene();
        genes[4] = new AdjacencyGene();
    }

    public double execute(Object o) {

        x = startX + (int) genes[0].value;
        y = startY + (int) genes[1].value;
        width = (int) genes[2].value;
        height = (int) genes[3].value;
        adjacency = (int) genes[4].value;

        IntegralImage image = (IntegralImage) o;

        switch (mode) {
            case TWO_RECT:
                return image.getHaarlikeFeaturesVariance().getTwoRectangleFeature(x,y,width,height,adjacency);
            case THREE_RECT:
                return image.getHaarlikeFeaturesVariance().getThreeRectangleFeature(x,y,width,height,adjacency);
            case FOUR_RECT:
                return image.getHaarlikeFeaturesVariance().getFourRectangleFeature(x,y,width,height,adjacency);
        }

        return 0;

    }

    public String toJava() {

        switch (mode) {
            case TWO_RECT:
                return "image.getHaarlikeFeatures().getTwoRectangleFeature(" + x + "," + y + ", " +  width + ", " +  height + ", " + adjacency +  ")";
            case THREE_RECT:
                return "image.getHaarlikeFeatures().getThreeRectangleFeature(" + x + "," + y + ", " +  width + ", " +  height + ", " + adjacency + ")";
            case FOUR_RECT:
                return "image.getHaarlikeFeatures().getFourRectangleFeature(" + x + "," + y + ", " +  width + ", " +  height + ", " + adjacency + ")";
        }

        return "";

    }

}
