package ac.essex.ooechs.facedetection.evolved.gp.nodes;

import ac.essex.gp.tree.Node;
import ac.essex.gp.params.NodeConstraints;
import ac.essex.gp.problems.DataStack;
import ac.essex.ooechs.imaging.commons.fast.IntegralImage;

/**
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version,
 * provided that any use properly credits the author.
 * 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 General Public License for more details at http://www.gnu.org
 * </p>
 *
 * @author Olly Oechsle, University of Essex, Date: 13-Jun-2008
 * @version 1.0
 */
public class Rectangle extends Node {

   public Rectangle() {
        super(4);
    }

    /**
     * Tells what return type each of the node's children should have
     */
    public int getChildType(int index) {
        return NodeConstraints.PARAMETER;
    }

    public int getReturnType() {
        return NodeConstraints.FEATURE;
    }

    private int x, y, width, height;

    public double execute(DataStack data) {

        IntegralImage image = data.getIntegralImage();

        x = (int) child[0].execute(data);
        y = (int) child[1].execute(data);
        width = (int) child[2].execute(data);
        height = (int) child[3].execute(data);

        if (x < 0) x = 0;
        if (y < 0) y = 0;
        if (width < 1) width = 1;
        if (height < 1) height = 1;

        data.usesImaging = true;
        data.value = image.getSquaredSum(x,y,width,height);
        return debugger.record(data.value);
    }

    public String toJava() {
        return "image.getSum(" + x + ", " + y + ", " + width + ", " + height + ")";
    }

}

