#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#endif

#ifndef _TRAINING_H
#define _TRAINING_H

#include <vector>
#include "feature.h"

class training; //forward reference

struct trainingRecord{
	short sign;
	int featValue;

	trainingRecord(int sign1, int featVal) {	//sign must be +1 or -1
		sign=(short)sign1;
		featValue=featVal;
	}

	inline void printRecord() {
		if (sign==1)
			printf("+ %i \n", featValue);
		else 
			printf("- %i \n", featValue);
	}

	inline int getSign() {return sign;}
	inline int getVal() {return featValue;}
};

struct rect{
	CvPoint p1;
	CvPoint p2;
	float decision;

	rect(CvPoint &i, CvPoint &j, float dec) {	//sign must be +1 or -1
		p1=i;
		p2=j;
		decision=dec;
	}

	bool isTooClose(struct rect *p) {
		float f=sqrt((p1.x-p->p1.x)*(p1.x-p->p1.x)+(p1.y-p->p1.y)*(p1.y-p->p1.y));
		float f2=sqrt((p2.x-p->p2.x)*(p2.x-p->p2.x)+(p2.y-p->p2.y)*(p2.y-p->p2.y));
		//float f3=sqrt((p2.x-p->p2.x)*(p2.x-p->p2.x)+(p1.y-p->p1.y)*(p1.y-p->p1.y));
		if ((f<=60)||(f2<=60)) 
			return true;
		else
			return false;
	}

	inline CvPoint getP1() {return p1;}
	inline CvPoint getP2() {return p2;}
};

class training {

protected:
	std::vector<trainingRecord*> trainRecs;
	std::vector<trainingRecord*> unsortedtrainRecs;	//used to find bestWC. Avoids recalculation

public:
	training();
	~training();
	training(training *m);	//copy constructor;

	int numpositives;
	int numnegatives;

	float pm;
	float nm;
	short featureNumber;	//the number of the feature used in the featurelist vector in main
	short featoffsetX;	//specifics of the feature used in this training
	short featoffsetY;
	int featScaleX;
	int featScaleY;
	float theta;	//threshold theta
	short s;	//sign
	int falsepos;	//number of false positives in the threshold finding procedure.
	int missed;		//number of missed (false negatives)
	double alpha;	//weight of the weak classifier.
	float ce;		//combined error

	void settraining(int featno, int offX, int offY, int scaX, int scaY);
	void sortRecs(std::vector<trainingRecord*>& set, int fst, int lst);
	void sortRecs();
	void addRec(int sign1, int featVal);
	void findWeakClassifier();
	std::vector<trainingRecord*>& getTrainRecs();
	std::vector<trainingRecord*>& getUnsortedTrainRecs();

	//training operator= (training T1);

};

#endif // !defined(AFX_TRAINING_H__F919129F_D056_4EE1_8C45_1E17E5CD52EB__INCLUDED_)

#ifdef _EiC
main(1,"training.cpp");
#endif