001package org.opencv.core;
002
003//javadoc:TermCriteria
004public class TermCriteria {
005
006    /**
007     * The maximum number of iterations or elements to compute
008     */
009    public static final int COUNT = 1;
010    /**
011     * The maximum number of iterations or elements to compute
012     */
013    public static final int MAX_ITER = COUNT;
014    /**
015     * The desired accuracy threshold or change in parameters at which the iterative algorithm is terminated.
016     */
017    public static final int EPS = 2;
018
019    public int type;
020    public int maxCount;
021    public double epsilon;
022
023    /**
024     * Termination criteria for iterative algorithms.
025     *
026     * @param type
027     *            the type of termination criteria: COUNT, EPS or COUNT + EPS.
028     * @param maxCount
029     *            the maximum number of iterations/elements.
030     * @param epsilon
031     *            the desired accuracy.
032     */
033    public TermCriteria(int type, int maxCount, double epsilon) {
034        this.type = type;
035        this.maxCount = maxCount;
036        this.epsilon = epsilon;
037    }
038
039    /**
040     * Termination criteria for iterative algorithms.
041     */
042    public TermCriteria() {
043        this(0, 0, 0.0);
044    }
045
046    public TermCriteria(double[] vals) {
047        set(vals);
048    }
049
050    public void set(double[] vals) {
051        if (vals != null) {
052            type = vals.length > 0 ? (int) vals[0] : 0;
053            maxCount = vals.length > 1 ? (int) vals[1] : 0;
054            epsilon = vals.length > 2 ? (double) vals[2] : 0;
055        } else {
056            type = 0;
057            maxCount = 0;
058            epsilon = 0;
059        }
060    }
061
062    public TermCriteria clone() {
063        return new TermCriteria(type, maxCount, epsilon);
064    }
065
066    @Override
067    public int hashCode() {
068        final int prime = 31;
069        int result = 1;
070        long temp;
071        temp = Double.doubleToLongBits(type);
072        result = prime * result + (int) (temp ^ (temp >>> 32));
073        temp = Double.doubleToLongBits(maxCount);
074        result = prime * result + (int) (temp ^ (temp >>> 32));
075        temp = Double.doubleToLongBits(epsilon);
076        result = prime * result + (int) (temp ^ (temp >>> 32));
077        return result;
078    }
079
080    @Override
081    public boolean equals(Object obj) {
082        if (this == obj) return true;
083        if (!(obj instanceof TermCriteria)) return false;
084        TermCriteria it = (TermCriteria) obj;
085        return type == it.type && maxCount == it.maxCount && epsilon == it.epsilon;
086    }
087
088    @Override
089    public String toString() {
090        return "{ type: " + type + ", maxCount: " + maxCount + ", epsilon: " + epsilon + "}";
091    }
092}