package yi.iu.imgdb.learn.absrgn;

import java.util.*;

/**
 * Generate overlapping abstract region features from abstract region features
 * (Abstract Region Overlapping feature generator)
 */

public class AbsRgnOverlappingFtGen implements AbsRgnFeatureGenerator {

  int parent1, parent2;

  public AbsRgnOverlappingFtGen(int parent1, int parent2) {
    this.parent1 = parent1;
    this.parent2 = parent2;
  }

  /**
   * Generate a new abstract region cluster using the existing abstract regions.
   * The new abstract region cluster is an instance of AbsRgnProbMapList.
   *
   * @param cluster_rgn_lists
   * @return
   */
  public AbsRgnProbMapList generate(LinkedList cluster_rgn_lists) {
    AbsRgnProbMapList new_cluster = new AbsRgnProbMapList();

    // the parent clusters
    AbsRgnProbMapList p_cluster1 = (AbsRgnProbMapList)cluster_rgn_lists.get(parent1);
    AbsRgnProbMapList p_cluster2 = (AbsRgnProbMapList)cluster_rgn_lists.get(parent2);

    for (int i = 0; i < p_cluster1.size(); i++) {
      // parents of the overlapped regions for image i
      AbsRgnProbMap p1 = p_cluster1.getAbsRgnProbMap(i);
      AbsRgnProbMap p2 = p_cluster2.getAbsRgnProbMap(i);

      AbsRgnProbMap intersect = p1.intersect(p2);
      new_cluster.add(intersect);
    }
    return new_cluster;
  }

  public AbsRgnProbMap[] generate(AbsRgnProbMap[] cluster1, AbsRgnProbMap[] cluster2) {
    AbsRgnProbMap[] new_cluster = new AbsRgnProbMap[cluster1.length];

    for (int i = 0; i < cluster1.length; i++) {
      // parents of the overlapped regions for image i
      AbsRgnProbMap p1 = cluster1[i];
      AbsRgnProbMap p2 = cluster2[i];

      AbsRgnProbMap intersect = p1.intersect(p2);
      new_cluster[i] = intersect;
    }
    return new_cluster;
  }

}