/* $Id: DTWCalculatorType2.java 125 2011-03-09 09:49:51Z ohura $ */
/*
 * 作成日： 2006/10/30
 */
package jp.ac.hokudai.meme.core_smart_gs.searcher;


/**
 * @author hajime
 * mod done 
 */
public class DTWCalculatorType2 extends AbstractDTWCalculator {

    
//    public DistanceAndIndex calcDTWDist(double[][] text, double[][] pattern, int startIndex, double[][] dtwArray) {
    public DistanceAndIndex calcDTWDist(int startIndex, int endIndex, double[][] pattern, double[][] dtwArray){
        int textLength = endIndex - startIndex + 1;
        int patLength = pattern.length;
    
        initDTWArray(dtwArray, startIndex);
        for (int i = 1; i < patLength; i++) {
            double d = getDist(startIndex + 1, i);
//            double d = calcDist(text[1], pattern[i]);
            dtwArray[i][1] = Math.min(dtwArray[i - 1][1] + d, dtwArray[i - 1][0] + d);
        }
        
        for (int i = 1; i < patLength; i++) {
            for (int j = 2; j < textLength; j++) {
                double d = getDist(startIndex + j, i);
                double d1 = dtwArray[i - 1][j];
                double d2 = dtwArray[i - 1][j - 1];
                double d3 = dtwArray[i - 1][j - 2];
                dtwArray[i][j] = min(d1, d2, d3) + d;
            }
        }
        double minDist = Double.POSITIVE_INFINITY;
        int resEndIndex = 0;
        for (int i = 0; i < textLength; i++) {
            if (dtwArray[patLength - 1][i] < minDist) {
                minDist = dtwArray[patLength - 1][i];
                resEndIndex = startIndex + i;
            }
        }
        return new DistanceAndIndex(minDist, startIndex, resEndIndex);
    }
    
    
    /**
     * <p></p>
     * @param args
     */
    public static void main(String[] args) {
        // TODO 自動生成されたメソッド・スタブ

    }

}
