/**
 **
 ** ItemPatternMatch.java - Pattern match quality record for detected ground objects
 **
 ** Copyright (C) 2024 Elphel, Inc.
 **
 ** -----------------------------------------------------------------------------**
 **
 **  ItemPatternMatch.java 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 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 General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ** -----------------------------------------------------------------------------**
 **
 */
package com.elphel.imagej.orthomosaic;

import java.util.Arrays;

public class ItemPatternMatch {
//	public String pattern_path;
	public double [] sub_matches; // array of sub-pattern match qualities. [0] - is usually combo, [1] - full pattern, [2+] - obscured
	public int       best_sub = -1; // best subpattern
	public GroundObjectPattern groundObjectPattern;
	public ItemPatternMatch(GroundObjectPattern groundObjectPattern) {
		this.groundObjectPattern = groundObjectPattern;
	}
	public void setMatches(double [] matches) {
		sub_matches = matches;
	}
	
	public void setMatch(int indx, double match_value) { // not used
		if (sub_matches == null) {
			sub_matches = new double [indx+1];
			Arrays.fill(sub_matches, Double.NaN);
		} else if (sub_matches.length <= indx) {
			double [] old_matches = sub_matches;
			sub_matches = new double [indx+1];
			Arrays.fill(sub_matches, Double.NaN);
			System.arraycopy(old_matches, 0, sub_matches, 0, old_matches.length);
		}
		sub_matches[indx] = match_value;
	}

	public void setBestSub(int sub) {
		best_sub = sub;
	}
	
	public int getBestSub() { // 1 - full, > 1 - partial
		return best_sub;
	}
	
	public double getMatch(int indx) {
		if ((sub_matches == null) || (sub_matches.length <= indx)) {
			return Double.NaN;	
		}
		return sub_matches[indx];
	}
	/**
	 * Return actual largest value of full and all half patterns, regardless of best_sub which can point
	 * to full pattern even if the correlation value is larger for partial 
	 * @return
	 */
	public double getBestMatchValue() { //
		if (sub_matches == null) {
			return Double.NaN;
		}
		double best = 0;
		for (int i = 1; i < sub_matches.length; i++) {
			if (sub_matches[i] > best) {
				best = sub_matches[i];
			}
		}
		return best;
	}
	
	
	
	public double [] getMatches() {
		return sub_matches;
	}
	public GroundObjectPattern getGroundObjectPattern() {
		return groundObjectPattern;
	}
}


