package nxm.sys.libg;

/** A class to define a Pixel Buffer for {@link MPlot}, {@link View}, and {@link Layer}. This is
    used for vector line drawing and is not a replacement a buffered image.

    @author Jeff Schoen / Jeanette McCall
 */
public class DPix extends Object {

  int max,off,n,flags;
  double[] x,y;
  double[] z;

  // This is package-private, only used by MPlot and View
  boolean save;               // Any saved?
  double[]   sx = new double[2];    // Save values X
  double[]   sy = new double[2];    // Save values Y
  double[] sz = new double[2];  // Save values Z

  /** DPix Constructor
      @param maxsize maximum size of x,y,z pixel buffers
      @param flags pixel flags
   */
  public DPix (int maxsize, int flags) {
    max = maxsize+1;
    x = new double[max];
    y = new double[max];
    z = new double[max];
    setFlags(flags);
  }

  /** Convert double version of Pix to a regular int version
   *  @return int version of Pix
   */
  Pix toPix () {
    Pix pix = new Pix(max-1,flags);
    for (int i = 0; i < n; i++) {
      pix.x[i] = (int)x[i];
      pix.y[i] = (int)y[i];
      pix.z[i] = (float)z[i];
    }

    if (save) {
      for (int i = 0; i < 2; i++) {
        pix.sx[i] = (int)sx[i];
        pix.sy[i] = (int)sy[i];
        pix.sz[i] = (int)sz[i];
      }
    }

    pix.n = n;
    pix.off = off;

    return pix;
  }

  void setX (double[] x) {
    this.x = x;
  }

  void setY (double[] y) {
    this.y = y;
  }

  void setOffset (int offset) {
    this.off = offset;
  }

  int getLengthX () {
    return x.length;
  }

  int getLengthY () {
    return y.length;
  }


  /** return how many points to do this pass and offset for next pass
   *  @param npts number of points requested
   *  @return npts done
   */
  public int setSize (int npts) {
    if (npts>max) { n=max; off=max-1; }
    else { n=npts; off=npts; }
    return n;
  }
  /** Sets the pixel flags
      @param mask pixel flags
   */
  public void setFlags (int mask) { flags = mask; }
  /** Adds a pixel flag
      @param mask pixel flag
   */
  public void setFlag (int mask) { flags |= mask; }
  /** Clears a pixel flag
      @param mask pixel flag
   */
  public void clrFlag (int mask) { flags &= (~mask); }

  double[] getX () {
    return x;
  }

  double[] getY () {
    return y;
  }

  int getSize () {
    return n;
  }

  int getFlags () {
    return flags;
  }

  int getOffset () {
    return off;
  }

  /** Returns the min and max x and y values
      Calls {@link #getXYMinMax(double[], int)} with a default initial-value array of all zeros
      @param num number of elements in x and y pixel arrays
      @return min and max x and y values
   */
  public double[] getXYMinMax(int num) {
    double r[] = {0,0,0,0};
    return (getXYMinMax(r,num));
  }

  /** Returns the min and max x and y values
      @param ini array of initial min and max x and y values
      @param num number of elements in x and y pixel arrays
      @return min and max x and y values
   */
  public double[] getXYMinMax(double[] ini, int num) {
    double xmin,xmax,ymin,ymax;
    xmin = ini[0];
    ymin = ini[1];
    xmax = ini[2];
    ymax = ini[3];

    for (int i = 0; i < num; i++) {
      xmax = Math.max(xmax, x[i]);
      xmin = Math.min(xmin, x[i]);
      ymax = Math.max(ymax, y[i]);
      ymin = Math.min(ymin, y[i]);
    }

    double[] retVal = new double[4];
    retVal[0] = xmin;
    retVal[1] = ymin;
    retVal[2] = xmax;
    retVal[3] = ymax;

    return (retVal);
  }

  /** Creates a string of the object. This is useful for debugging purposes. The format of the
      string returned by this method may change in future versions.

      @return A string describing the object.
   */
  @Override
  public String toString() {
    final StringBuffer str = new StringBuffer();

    str.append("Pix: {");

    for(int i = 0; i < n; i++) {
      str.append(" (");
      str.append(x[i]);
      str.append(',');
      str.append(y[i]);
      str.append(',');
      str.append(z[i]);
      str.append(')');
    }
    str.append(" }");

    return str.toString();
  }
}
