#ifndef CPROCESS_KERF_H
#define CPROCESS_KERF_H

#include "cmprocess.h"
#include "cimagecontainer.h"
#include "cshape.h"
#include <QDataStream>
#include "cprecisetime.h"
#include "ckerf_base.h"
/*
enum Kerf_POS
{
    CENTER_KERF,    //中心
    SIDE_KERF,      //边缘
    SPEC_KERF,      //特殊位置
	PLATFORM_KERF	//加工盘上检查
};
typedef struct _tagKerfParam
{
    int		iLineCount;
    int		iPixelThres;
    int		iLight1;
    int		iLight2;
    int     iExposure;
    int     iGain;
    double	dRange;
    double	dWidthMax;
    double	dWidthMin;
    double	dGAPMax;
    double	dGAPMin;
    double	dOffsetMax;
    double	dOffsetMin;
    double	dRangeMin;
    int		ikerfPos;   //kerf检查的位置
    double  dSpecKerfPos;
    int     iBinaryThres;//灰度阈值
    _tagKerfParam()
    {
        iLineCount=1;
        iLight1=0;
        iLight2=0;
        iExposure=7000;
        iGain=1000;
        dRange=0;
        dWidthMax=0;
        dWidthMin=0;
        dGAPMax=0;
        dGAPMin=0;
        dOffsetMax=0;
        dOffsetMin=0;
        iPixelThres=0;
        dRangeMin=0;
        ikerfPos=SIDE_KERF;
        dSpecKerfPos=0;
        iBinaryThres=128;
    }
}KERFPARAM,*PKERFPARAM;
QDataStream &operator<<(QDataStream &out,const KERFPARAM& param);
QDataStream &operator>>(QDataStream &in, KERFPARAM& param);
ConfigStreamWriter &operator<<(ConfigStreamWriter &out, const KERFPARAM &param);
ConfigStreamReader &operator >> (ConfigStreamReader &in, KERFPARAM &param);
*/
class CProcess_Kerf : public CMProcess
{
public:
    enum msg_id
    {
        PROCESS_Kerf_MSG_ID=MAX_PROCESS_MSG_ID,
        CALCUKERFPOS,   //计算Kerf的位置
        CALCUKERFPOS1, //计算Kerf的位置
        MOVEKERFPOS,    //移动到Kerf的位置
        KERF,
        SETKERFPOSMANUAL,   //手动测量Kerf位置
        TESTKERF,       //手动测试
  //      CALCCUTKERFPOSCUT, //计算Kerf切割后检测位置
  //      CALCANDCUTKERFLINE, //计算Kerf切割并进行切割
		//MOVEKKERFPOS_CUT, //移动到检测位置
		//KERF_CUT, //切割后Kerf
		KERF_WATCH, //切割后仅观察
        MAX_PROCESS_Kerf_MSG_ID,
    };
    CProcess_Kerf(const QString& name,int32_t id);
    QMMutexVector<KERFPARAM> m_vKerfParam;
    CMPointd m_CheckPos;    //检查的点位
    bool m_bHasSet;
	
	//计算Kerf检查的位置
    int CalcuKerfPos(int iSideID,int iLineID,int iLayerID);
	//手动切割时的检查位置
    int CalcuKerfPos1(double dX, double dY, int iSideID, int iLayerID);
	//移动到Kerf的位置
    int MoveKerfPos();
	

    //kerf检查
    int Kerf(int iSideID, int iLayerID);
	//仅观察
	int KerfWatch(int iSideID, int iLayerID);

#if 0
	//在膜上切割Kerf
	int CutKerf(int iSideID, int iLineID, int iLayerID,
				double dStartPosX, double dEndPosX, double dPosY);
	//计算切割位置
	int CalcuKerfPosCut(int iSideID,int iLineID,int iLayerID);
	//计算并切割
	int CalcAndCutKerfLine(int iSideID,int iLineID,int iLayerID);
	//移动到切割后Kerf的位置
	int MoveKerfPosCut();
	//检查自己切割的线
	int KerfCut(int iSideID, int iLayerID);
#endif
	
	//手动更新Kerf的偏移位置
    int SetKerfPosManual(int iSideID,int iLayerID);

	//设置关联的Kerf的参数
    void SetKerfParam(int iSideID,int iLayerID);
	//分析识别的结果
    void AnalyseKerfResult(int iSideID,int iLayerID,CImageContainer* pContainer);

    void CaptureImage(int iSideID, int iLayerID, QString &str);
    void TestKerf(int iSideID,int iLayerID);

    int32_t m_iKerfCamID;

    CImageContainer m_imageContainer[2];	//Kerf检测的相机显示控件0 SideA
    void ClearContainer();

    void ReadRecipe(ConfigStreamReader& streamReader, int iVersion) override;


    void WriteRecipe(ConfigStreamWriter& streamWriter, int iVersion) override;

public:
    virtual void do_msg(int iID,QVariantList& vlInput,V_MsgOutput* pvlOutput) override;
    virtual void Serialize(QDataStream &stream, bool bwrite, int iVersion) override;
public:
    KERFPARAM *GetKerfParam( int iSideID,int iLayerID );
    QVector<int>  VectorResult;
    void SearchLine(int iSideID,int iLayerID ,bool_t bAutoThres);

	//dRealOffset 切割道中心与相机中心点的偏移尺寸 在中心下方为负
	//dRealHeight 第一条线的尺寸mm
	//dRealHeight1 第二条线的尺寸mm
	//dRealGAP 两条线的中心点距离 只有一条线无效
    void GetKerfResult(double& dRealOffset,
                   double& dRealHeight ,
                   double& dRealHeight1,
                   double&  dRealGAP);
	//显示Kerf结果
    void ShowKerfResult(double  dRealOffset,
                         double  dRealHeight ,
                         double  dRealHeight1 ,
                         double  dRealGAP,
                         CImageContainer *pContainer);

    void SetKerfLight(int iSideID, int iLayerID);

	//根据WPDP的坐标，计算出线的位置的偏移并应用到激光的Offset
    double SetHairLineOffset(int iSideID, int iLayerID, const CMPointd& WPDP, bool bApplyOffset, bool bManualSet=false);

    CPreciseTime m_KerfTime;
	double m_dKerfPower;
	double m_dKerfWidth;
};

#endif // CPROCESS_KERF_H
