package yi.iu.region;

import yi.graph.polygon.*;
import java.awt.*;
import java.io.*;

/**
 * PolygonRegion keeps a region as a polygon.  It also includes the statistical
 * information of the region.
 * @see yi.iu.region.Regoin
 */

public class PolygonRegion implements Serializable{

  /**
   * Store the statistical information of the region
   */
  Region m_region;

  /**
   * Store the region as a polygon
   */
  GenericPolygon m_polygon;

  public PolygonRegion() {
  }

  public PolygonRegion(GenericPolygon polygon, Region region) {
    m_polygon = polygon;
    m_region = region;
  }

  public Region getRegion() {
    return m_region;
  }

  public void setRegion(Region region) {
    m_region = region;
  }

  public GenericPolygon getPolygon() {
    return m_polygon;
  }

  public void setPolygon(GenericPolygon polygon) {
    m_polygon = polygon;
  }

  /**
   * Rescale the polygon.  The coordinates of each points will be multiplied by
   * scale
   * @param scale
   */
  public void scale(double scale) {
    m_region.scale(scale);
    m_polygon.scale(scale);
  }


  public static PolygonRegion[] getPolygonRegions(int[] labels, int width, int height, boolean ignore_zero) {
    // the number of labels
    int n = yi.stat.StatUtil.maximum(labels) + 1;

    // get the region statistical information first
    Region[] regions = Region.getRegions(labels, n - 1,
                                         new Dimension(width, height));

    // get the polygons
    GenericPolygon[] polygons = GenericPolygon.getPolygons(labels, width, height, ignore_zero);

    PolygonRegion[] pgs = new PolygonRegion[n];
    for (int i=0; i<n; i++) {
      pgs[i] = new PolygonRegion(polygons[i], regions[i]);
    }
    return pgs;
  }

}