#pragma once

#include "pixelmatch.h"


//查找一个实例，不支持模板
class PATMAT_API CEdgeFit :	public CPixelMatch
{
public:
	CEdgeFit(void);	
	virtual ~CEdgeFit(void);

	virtual BOOL    Learn(const RvImage image=NULL, int* pErrCode = NULL) ;
	virtual int     Recognize(const RvImage image=NULL, int* pErrCode = NULL);

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

 

	virtual void* GetPatternSet();
	virtual int GetPatternCount();
 
	virtual RvPoint_f  GetPatternCenter(int index);

	//最顶层的模板样品图像的宽度和高度
	virtual int  GetPatternWidth(int index);
	virtual int  GetPatternHeight(int index);

	//客户程序应该释放GetPatternImage返回的对象
	virtual RvImage GetPatternImage(int mipLevel, int pattIndex,  int angleIndex);
	//客户程序应该释放GetPatternMask返回的对象
	virtual RvMask  GetPatternMask(int mipLevel, int pattIndex,  int angleIndex);

//内部使用
public:
	void DumpPatterns(const TCHAR* strFilePath);
	virtual RV_HANDLE DrawPatternInstances(HGDC dc, RV_HANDLE hStroke);

protected:
	//void  DrawEdges(BOOL bForce);
	int ChooseResultsE2(PM_MATCH_RESULT*  pMatResultArr,  int resultCount, int expectedCount);

	void ReleaseSpdEdgeSet();
	//void TrasformToSpdEdgeSet(void*  ppPattSet, int size );

	BOOL WriteEdges(HANDLE device,void* pPatternSet, int size);
	BOOL ReadEdges(HANDLE device,void** ppPatternSet, int* pPatterSize);
	void CloneEdges(void* pSourceSet, int nSrcSize, void** ppDestSet, int* pDestSize);

	int DoEdgeMatch(RvImage* pSceneMip, int nImageCount  );
 	


	//int FindPatternInRange(RvImage pImScene  , int pattIndex, int matchindex,   int& x, int& y, int toler,  float minScore,BOOL bCalcActualPos  );
	int FindRotatedPattern(RvImage pImScene ,  PM_SPD_EDGE* pPattSet, int curPattIdx, float initScore,  PM_MATCH_RESULT*  pInitResultArr,  int curInitCount, int maxInitCount );


	BOOL BuildSampleTreeEf(const RvImage image, const RvMask mask, int left, int top);
	//BOOL GenSampleMipsEf(const RvImage image, const RvMask mask,  void* pPatSet, int setCount);

	//void InitScaledPattEf(void* pPatSet, int setCount, const RvImage image, const RvImage imMask, int cols, int rows, double st,   double reso,  int rotateCount, BOOL bRotate);
	//void SetInitPattEf(void* pPat, RvImage image, RvImage imMask, BOOL bRotate);
	 
public:
	//亮度范围
	int m_nBrightLower, m_nBrightUpper;

protected:
	int   m_nSetSize;
	void* m_pSpdEdgeSet;

	

	RvImage m_imPattern;
	RvMask  m_mkPattern;

};

