package yi.iu.processor.cannyox;

public class EdgelChain
{
    // -- Default constructor
    public EdgelChain()
    {
        init(1);//just not to cause trouble
    }
    
    // -- Constructor for n length chain.
    public EdgelChain(int n)
    {
        init(n);
    }


    // Data Access---------------------------------------------------------------
    public int length()  { return _nedgels; }

    public float []GetX()      { return _x; }
    public float []GetY()      { return _y; }
    //public float []GetZ()      { return _z; }
    public float []GetGrad()   { return _grad; }
    public float []GetTheta()  { return _theta; }

    public float GetX(int i)      { return _x[i]; }
    public float GetY(int i)      { return _y[i]; }
    //public float GetZ(int i)      { return _z[i]; }
    public float GetGrad(int i)   { return _grad[i]; }
    public float GetTheta(int i)  { return _theta[i]; }

    
    float MeanGrad()
    {
        float sum=0;
        for(int i=0;i<_nedgels;i++) sum += _grad[i];
        return(sum/_nedgels);
    }
    
    // Data Control--------------------------------------------------------------
    public void SetX(float x, int i)          { _x[i] = x; }
    public void SetY(float y, int i)          { _y[i] = y; }
    //public void SetZ(float z, int i)          { _z[i] = z; }
    public void SetX(float[] x) { _x = x; }
    public void SetY(float[] y) { _y = y; }
    //public void SetZ(float[] z) { _z = z; }
    public void SetGrad(float grad, int i)    { _grad[i] = grad; }
    public void SetTheta(float theta, int i)  { _theta[i] = theta; }
    public void SetLength(int i) { _nedgels = i; }

    // Utility Methods-----------------------------------------------------------
    public void get_min_max(float[] min, float[] max)
    {
        int endx=_nedgels;
        int x=0,y=0;
        float minx=_x[x], maxx=_x[x++], miny=_y[y], maxy=_y[y++];

        while (x < endx)
        {
            if (_x[x] < minx) minx = _x[x];
            else if (_x[x] > maxx) maxx = _x[x];
            if (_y[y] < miny) miny = _y[y];
            else if (_y[y] > maxy) maxy = _y[y];
            x++; y++;
        }
        min[0] = minx; min[1] = miny; 
        max[0] = maxx; max[1] = maxy; 
    }    
    
    public String toString()
    {
        String s="";
        s+= "EdgelChain:\n";
        for(int i=0;i<_nedgels;i++)
            s+= _x[i] + " " + _y[i] + "\n ";
        s+= "\n";
        return s;
    }

    //--------------------------------------------------------------
    //
    // -- Reverses the order of the data in the EdgelChain.
    //
    public void Reverse()
    {
        int down,up;
        float tmp;

        for(down=0,up=_nedgels-1;down<up;down++,up--)
        {
            tmp = _x[down];      _x[down] = _x[up];         _x[up] = tmp;
            tmp = _y[down];      _y[down] = _y[up];         _y[up] = tmp;
            //tmp = _z[down];      _z[down] = _z[up];         _z[up] = tmp;
            tmp = _grad[down];   _grad[down] = _grad[up];   _grad[up] = tmp;
            tmp = _theta[down];  _theta[down] = _theta[up]; _theta[up] = tmp;
        }
    }

  // INTERNALS-----------------------------------------------------------------

  // Data Members--------------------------------------------------------------

    void init(int n_edgels)
    {
        _nedgels = n_edgels;
        _x = new float [n_edgels];
        _y = new float [n_edgels];
        //_z = new float [n_edgels];
        _grad =new float [n_edgels];
        _theta = new float [n_edgels];
    }  

    //  void clear();
    int _nedgels;                      // Number of edgels in the chain
    public float[] _x, _y,  _grad, _theta;  // (x,y,z), step intensity and orientation
}