/*
   * 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;
  }
  /*
   * find corners with maximal cornerness
   */
  bool harrisCorners::findCornerMaxima(const channel& cornerness,
                                             channel& cornersOnly,
                                             pointList& cornerMax) const {
    if (cornerness.empty()) {
      cornersOnly.clear();
      cornerMax.clear();
      return true;
    }

    const parameters& par = getParameters();

    const float corner = par.cornerValue/255.0f;
    const float noCorner = par.noCornerValue/255.0f;

    localMaxima<float> lmax;
    localMaxima<float>::parameters lmaxPar(par.localMaximaParameters);
    lmaxPar.noMaxValue = noCorner;
    lmaxPar.maxNumber = par.maximumCorners;
    lmax.setParameters(lmaxPar);

    if (lmax.apply(cornerness,cornersOnly,cornerMax)) {
      pointList::iterator it;
      int i;
      for (it=cornerMax.begin(),i=0;
           (it!=cornerMax.end());
           ++it) {
        cornersOnly.at(*it) = corner;
      }

      for (;it!=cornerMax.end();++it) {
        cornersOnly.at(*it) = noCorner;
      }

      return true;
    }
    return false;
  }