#pragma once

#include "blobx.h"


 

#define TT_FEATURE_COUNT   3

class BLOBX_API CTripleTrace :	public CAreaCal
{
public:
	enum {
		TT_EMPTY=0,  //无产品
		TT_BACK =1,  //反面
		TT_FRONT=2,	 //正面	
	};

public:
	CTripleTrace(void);
	virtual ~CTripleTrace(void);

	virtual BOOL Serialize(HANDLE device, BOOL bIn ) ;
    virtual CUCObj* Clone( CUCObj* twin) ;

	virtual void Reset();
	virtual void Show(CDelegate* pDelegate, int mode, BOOL bForce);

	BOOL    LearnFace(int face, int* pErrCode);
	void    Clear();

	//如果找到合适的面，返回TRUE.
	virtual RV_BOOL Inspect(int* pErrCode) ;

	BOOL   IsFaceLearned(int face);
	

	float GetMinError(int face);
	void  SetMinError(int face, float minError);
	 
	int   GetStartPos();
	void  SetStartPos(int pos);

	int   GetEndPos();
	void  SetEndPos(int pos);

	BOOL IsVertical();
	void SetVertical(BOOL flag);
	int  GetTolerLen();
public:

	BOOL   IsTragetExist(){ return m_bTragetExist ; };

	//识别结果
	float  GetFaceError(int face);
	int    GetFacePos(int face);
	int  GetBestResult();

	

public:
	virtual void OnEndTrack(CDelegate* pDelegate, HGPD hCur,  GPoint point, int type)  ;
public:
	BOOL  m_bMovingTarget; //静态物体,比如水平仪气泡;动态物体，比如振动盘上料。
	
	float m_nPosToler;      //位置偏移量

protected:
	//0 - 模板，1-模板+允许误差， 2-全部
	UINT* RetrieveFeature(const RvImage image, BOOL bVertical, int subtype, int& size);
	float CompareFeature(UINT* pSceneData , int nSceneSize, UINT* pPattData, int nPattSize, int& bestpos);


private:
	struct __tt_feature_
	{
		BOOL  bLearned;
		float minError;  //最小错误值，必须小于这个值才认为有效。
		int   size;
		UINT  *pData;
	}m_features[TT_FEATURE_COUNT]; 

	struct __tt_result_
	{
		 float error;   //错误
		 int   pos;     //位置
	}m_results[TT_FEATURE_COUNT]; 
	 
	
	BOOL  m_bTragetExist;
	int   m_nBestFace;

	int   m_nStartPos, m_nEndPos;
	BOOL  m_bVertical;  //垂直方向检测
  
};

