#ifndef SEARCHANGLESADAM_H
#define SEARCHANGLESADAM_H

#include "recognition.h"
#include "../microadam.h"
#include "canny.h"
#include "correlation.h"
#include "../opencvext.h"
#include "../polarline.h"
#include <iostream>

using namespace std;

namespace seemicro
{

const int MAX_MAX_MAXIMA = 30;
const int MAXPL=40;

struct SearchAnglesAdam : public RecognitionModule
{
  MicroAdam& adam;
  CannyModule& canny;
  CorrelationModule& correlation;

  /**
    * Intermediate values of searchAngles().
    * storage: for hough lines
    */
  CvMemStorage* storage;
  CvSeq* lines;
  polarLine polarLines[MAXPL], l1, l2, mline;
  mypoint p1, p2;
  float dr2;
  myrect rect;

  /**
    * Saved return value of searchAngles().
    * Used by Output module to decide what to draw.
    */
  int searchStatus;

  /**
    * for hough transform: number of accumulator cells
    */
  int angres, distres;
  int hough_threshold;
  int houghMaxGap;

  SearchAnglesAdam(MicroAdam& Am, CorrelationModule& Aco, CannyModule& Ac) :
    adam(Am), canny(Ac), correlation(Aco)
  {
    distres = 200;
    angres = 200;
    hough_threshold = 15;
    lines = NULL;
    storage = cvCreateMemStorage(0);
    rect = cvRect(0, 0, canny.img->width, canny.img->height);
    searchStatus = -2;
    houghMaxGap = 20;
  }

  ~SearchAnglesAdam()
  {
    if(lines) cvClearSeq(lines);
    cvReleaseMemStorage(&storage);
  }

  void processFrame(myrect *ROI) { searchStatus = searchAngles(); }
  void processKeyFrame(void) { searchStatus = searchAngles(); }

  /**
    * Takes inside of wheel by copying / masking out the rest
    * then does hough transform to find main lines.
    * Assumes adam.radius & adam.mitte are valid.
    *
    * @returns 0 = OK, -1 = weniger als 2 Geraden gefunden,
    * -2 = keine passenden Geraden im Bild
    */
  int searchAngles();


}; // SearchAnglesAdam

} // seemicro

#endif
