/*
 *  Alex Mathews, amathews@jhu.edu
 *  Pranav Lakshminarayanan, plakshm1@jhu.edu
 *  Computer Integrated Surgery I - Fall 2014
 */
package iterativeclosestpoint;

import java.util.ArrayList;

/**
 * Converges when error is beneath a threshold value.
 * @author Pranav
 */
public class ThresholdConvergence implements Convergence {

    /**
     * Maximum error threshold.
     */
    private final double thresh;

    /**
     * Desired number of iterations to be run.
     */
    private final int numIter;
    
    /**
     * Number of iterations completed.
     */
    private int iter = 0;
    
    /**
     * List of errors from each iteration.
     */
    private final ArrayList<Double> errors = new ArrayList<>();
    
    /**
     * Construct convergence object that converges to a threshold.
     * @param th convergence threshold
     * @param ni maximum number of iterations to be run
     */
    public ThresholdConvergence(double th, int ni) {
        this.thresh = th;
        this.numIter = ni;
    }

    @Override
    public ArrayList<Double> getErrors() {
        return this.errors;
    }

    @Override
    public boolean isConverged(double err) {
        this.errors.add(err);
        if (this.iter <= this.numIter) {
            this.iter++;
            return err < this.thresh;
        }
        return true;
    }
    
    
}
