/*
   * compute the second order.
   */
  bool harrisCorners::getSecondOrder(channel& gx,
                                     channel& gy,
                                     channel& fxy) const {

    const parameters& par = getParameters();

    fxy.resize(gx.size(),false,false);
    gaussKernel2D<float> gk(par.kernelSize,par.variance);
    convolution filter;
    convolution::parameters filterPar;
    filterPar.boundaryType = lti::Constant;
    filterPar.setKernel(gk);
    filter.setParameters(filterPar);

    channel::iterator igx=gx.begin();
    channel::iterator igxend=gx.end();
    channel::iterator igy=gy.begin();
    channel::iterator ifxy=fxy.begin();
    float tx, ty;

    while (igx!=igxend) {
      tx=(*igx);
      ty=(*igy);
      (*igx)=tx*tx;
      (*igy)=ty*ty;
      (*ifxy)=tx*ty;
      ++igx; ++igy; ++ifxy;
    }

    return (filter.apply(gx) && filter.apply(gy) && filter.apply(fxy));
  }
  void distanceTransform::sedFiltering(channel &chnl, 
                                        bool useEightSED) const {

    const float fv  = 0.0f;
    const int undef = -2;

    matrix<point> dist(chnl.size());

    int row, 
        col;

    //init
    for(row = 0; row < chnl.rows(); ++row){
      for(col = 0; col < chnl.columns(); ++col){
        if(chnl.at(row, col) == fv)
          dist.at(row, col) = point(0, 0);
        else
          dist.at(row, col) = point(undef, undef);
      }
    }

    if(useEightSED) 
      eightSEDFiltering(chnl, dist);
    else            
      fourSEDFiltering(chnl, dist);

    //set the distances 
    for(row = 0; row < chnl.rows(); ++row)
      for(col = 0; col < chnl.columns(); ++col)
        chnl.at(row, col) = static_cast<float>(dist.at(row, col).distanceSqr(point(0,0)));
  }
  /*
   * compute cornerness
   */
  bool harrisCorners::getCornerness(const channel& fxx,
                                    const channel& fxy,
                                    const channel& fyy,
                                    const float scale,
                                    channel& cornerness,
                                    float& maxCornerness) const {
    // we can assume that all channels are connected, but try it out if not
    if ((fxx.getMode() != channel::Connected) ||
        (fxy.getMode() != channel::Connected) ||
        (fyy.getMode() != channel::Connected)) {
      setStatusString("Channels not contigous in getCornerness");
      return false;
    }
    
    if (fxx.empty() || fxy.empty() || fyy.empty()) {
      cornerness.clear();
      maxCornerness = 0.0f;
      return false;
    }
    
    int i;
    const int end = fxx.rows()*fxx.columns();
    const float *const pfxx = &fxx.at(0);
    const float *const pfxy = &fxy.at(0);
    const float *const pfyy = &fyy.at(0);

    cornerness.resize(fxx.size(),0,false,false);
    float* pcor = &cornerness.at(0);

    float det,trace,txx,txy,tyy,c;
    float maxc = 0.0f;

    for (i=0;i<end;++i) {
      txx=pfxx[i];
      txy=pfxy[i];
      tyy=pfyy[i];
      det=txx*tyy - txy*txy;
      trace=txx+tyy;
      c = det-scale*trace*trace;
      pcor[i]=c;
      if (c>maxc) {
        maxc=c;
      }
    }

    maxCornerness = maxc;
    return true;
  }