package yi.iu.processor.texture.reg;

import yi.iu.region.*;
import yi.util.io.output.Csvable;
import yi.iu.ft.local.RegionBased;
import yi.iu.processor.texture.gabor.Gabor;

public class GaborTextureRegion implements Csvable, java.io.Serializable, RegionBased{
  public double[] m_gabor;
  Region m_region;

  /**
   * This constructor is for castor only.  Don't call it directly
   */
  public GaborTextureRegion() {
  }

  public GaborTextureRegion(double[] gabor, Region rg) {
    m_gabor = gabor;
    m_region = rg;
  }

  public void setGabor(double[] gabor) {m_gabor = gabor;}
  public double[] getGabor() {return m_gabor;}

  public void setRegion(Region r) {m_region = r;}
  public Region getRegion() {return m_region;}


  public double[][] toCsv() {
    double[] region = new double[] {
      m_region.m_centroidX,
      m_region.m_centroidY,
      m_region.m_majAngle,
      m_region.m_majLength,
      m_region.m_minAngle,
      m_region.m_minLength,
      m_region.m_momentX,
      m_region.m_momentY,
      m_region.m_momentXY,
      m_region.m_size,
      m_region.m_imgWidth,
      m_region.m_imgHeight
    };

    double[] data = new double[m_gabor.length + region.length];
    for (int i=0; i<m_gabor.length; i++) data[i] = m_gabor[i];
    for (int i=0; i<region.length; i++) data[m_gabor.length + i] = region[i];
    return new double[][] {data};
  }

  public static GaborTextureRegion fromCsv(double[] csv) {
    // read in the gabor texture part
    int gabor_length = Gabor.numScales * Gabor.numOrients;
    double[] gabor = new double[gabor_length];
    for (int i=0; i<gabor_length; i++) gabor[i] = csv[i];

    // read in the region part
    Region r = new Region();
    r.m_centroidX  = (float)csv[gabor_length + 0];
    r.m_centroidY  = (float)csv[gabor_length + 1];
    r.m_majAngle   = csv[gabor_length + 2];
    r.m_majLength  = csv[gabor_length + 3];
    r.m_minAngle   = csv[gabor_length + 4];
    r.m_minLength  = csv[gabor_length + 5];
    r.m_momentX    = (float)csv[gabor_length + 6];
    r.m_momentY    = (float)csv[gabor_length + 7];
    r.m_momentXY   = (float)csv[gabor_length + 8];
    r.m_size       = (int)Math.round(csv[gabor_length + 9]);
    r.m_imgWidth   = (int)Math.round(csv[gabor_length + 10]);
    r.m_imgHeight  = (int)Math.round(csv[gabor_length + 11]);
    return new GaborTextureRegion(gabor, r);
  }
}