1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#pragma once
 
#include <map>
#include <list>
#include <vector>
#include "SplineInspect.h"
#include "ChamferInspect.h"
#include "EDGE_DIPM.h"
 
using namespace std;
 
enum    EM_NOTCH_MSG_ERR{ERR_NO_MASTERIMG=0,ERR_NO_ORGIMG,ERR_FIND_POS,NORMAL_EXE};
 
typedef struct  _NOTCH_Parm_STU
{
    BOOL        bChipIns;
    BOOL        bSizeIns;
    int            nChipDiff;    
    int            nSizeDiff;
    int            nDetThres;    
    int            nEdgeThres;
    int            nSizeThres;
    int            nChipThres;
    int            nSkipSize;
    double        dBlank2EdgeRatio;
    BOOL        bSplineChip[2];    
    int            nSpInsSize[2];    
    int            nChamferSize;
    int            nChamferDiff;
    int            nNoDetSkipSize;
    int            nDetSkipSize;
    double        dCamRes;
    int            nPointInsAveCount;
 
    _NOTCH_Parm_STU()
    {
        Reset();
    }
    void Reset()
    {
        bChipIns = bSizeIns = bSplineChip[0] = bSplineChip[1] = FALSE;
        nChipDiff = nDetThres = nEdgeThres = nChipThres = nSkipSize = 0;
        nSpInsSize[0] = nSpInsSize[1] = nSizeDiff = nChamferSize = nChamferDiff = nNoDetSkipSize = nDetSkipSize = 0;
        dCamRes = dBlank2EdgeRatio = 0.;
        nPointInsAveCount = 5;
    }
}NOTCH_Parm_STU;
 
typedef struct  _NOTCH_Master_STU
{
    BOOL            bLoad;
    COwnerBuffer    imgBuf;
    int                nPosX;
    int                nPosY;    
    int                nMaxX;
    int                nMaxY;
 
    _NOTCH_Master_STU()
    {
        Reset();
    }
    void Reset()
    {
        bLoad = FALSE;
        imgBuf.ReleaseSpace();        
        nPosX = nPosY = nMaxX = nMaxY = 0;
    }        
}NOTCH_Master_STU;
 
typedef struct  _PointD
{
    double        X,Y;
 
    _PointD()
    {
        Reset();
    }
    _PointD(double dX,double dY)
    {
        X = dX;
        Y = dY;
    }
    void Reset()
    {
        X = Y = 0.;
    }        
}PointD;
 
typedef struct  _NOTCH_ObjFind_STU
{
    double        dMatPosX;
    double        dMatPosY;
    int            nEdgeTop;
    int            nEdgeBot;
 
    _NOTCH_ObjFind_STU()
    {
        Reset();
    }
    void Reset()
    {
        dMatPosX = dMatPosY = 0.;
        nEdgeTop = nEdgeBot = 0;
    }
}NOTCH_ObjFind_STU;
 
typedef struct  _NOTCH_Merge_STU
{
    int            nStart;
    int            nEnd;
    int            nSize;
 
    _NOTCH_Merge_STU()
    {
        Reset();
    }
    void Reset()
    {
        nStart = nEnd = -1;
        nSize = 0;
    }
}NOTCH_Merge_STU;
 
typedef struct  _NOTCH_Circle_STU
{
    PointD        pointF;
    PointD        pointS;
 
    _NOTCH_Circle_STU()
    {
        Reset();
    }
    void Reset()
    {
        pointF = pointS = PointD(0,0);
    }
}NOTCH_Circle_STU;
 
typedef struct  _NOTCH_RangePos_STU
{
    CPoint        pointCAD;
    CPoint        pointImg;
    double        dDist;
    int            nInsCount;//20140528
 
    _NOTCH_RangePos_STU()
    {
        Reset();
    }
    void Reset()
    {
        pointCAD = pointImg = CPoint(0,0);
        dDist = 0.;
        nInsCount = 0;//20140528
    }
}NOTCH_RangePos_STU,*pNOTCH_RangePos_STU;
 
struct SortYPos : public std::binary_function<pair<int,CPoint>, pair<int,CPoint>, bool>
{
    bool operator()(const pair<int,CPoint> & lhs, const pair<int,CPoint> & rhs)
    {
        return lhs.second.y > rhs.second.y;
    }
};
struct ResSortYPos : public std::binary_function<pair<int,CPoint>, pair<int,CPoint>, bool>
{
    bool operator()(const pair<int,CPoint> & lhs, const pair<int,CPoint> & rhs)
    {
        return lhs.second.y < rhs.second.y;
    }
};
struct SortSPYPos : public std::binary_function<pair<int,CSplinePoint>, pair<int,CSplinePoint>, bool>
{
    bool operator()(const pair<int,CSplinePoint> & lhs, const pair<int,CSplinePoint> & rhs)
    {
        return lhs.second.rotate.y < rhs.second.rotate.y;
    }
};
#define MAX_NOTCH_SCAN_COUNT 4
#define MAX_NOTCH_DEFECT_COUNT    1000
 
class CSISBuffer;
 
class AFX_EXT_CLASS CNotchCut
{
public:
    CNotchCut(void);    
    virtual ~CNotchCut(void);
 
public:
    void        Reset();
    BOOL        LoadData(CString strFile);    
    BOOL        CopyModelData(CNotchCut *pNotch);
    BOOL        AdjustTilt(int index,double dTheta,double baseCenY);
    void        SetParm(NOTCH_Parm_STU &parm);
    int            NotchExecute(CSISBuffer &pOrg,double *dResult,double *dResult_Dy,pNOTCH_RangePos_STU pRangeRes,pNOTCH_RangePos_STU pRangeRes_Dy,pNOTCH_RangePos_STU pRangeRes_Cham,int nRangeCnt,COwnerBuffer &pRes,COwnerBuffer &pRes_Dy,COwnerBuffer &pRes_Cham,double *dAvgThick,int iDimension
        , double *dFeedbackXY, int icam,double *dGlassXY,double dAlign2GlassX,int iLeftLine,BOOL bSaveDebug,CString strSavePath,double dTheta);    //20140528
    BOOL        WriteModelData(CString strFile,CString strRecipe,CString strCut,std::multimap<int, CSplinePoint> *pPoint);
    BOOL        ReadModelData(CString strFile,int index);
    CChipBlob    *GetResultDefect(){return m_ResultDefect;}
    int            GetDefectCount(){return m_nDefectCount;}
    void        SetNotchInsPoint(std::vector<CPoint> *InsPoint, int index);//20140528
    void        FindCenterPos(CSISBuffer &pImg,int &nPosX,int &nPosY,int nSkipSize=10);//20140528
 
protected:
    BOOL        CalDiffXY(std::multimap<int,CSplinePoint>    *mapDist,double *dDx,double *dDy);
    void        CalRangeVal(std::multimap<int,CSplinePoint>    *mapDist,double *dResDist,pNOTCH_RangePos_STU dRangeRes,int nRangeCnt,int nCenX,int nCenY,int nStartX,int nFirstY,int nLastY);
    void        CalRangeVal_New(std::multimap<int,CSplinePoint>    *mapDist,std::multimap<int,CSplinePoint> *mapDist_Dy,double *dResDist,double *dResDist_Dy,pNOTCH_RangePos_STU dRangeRes,pNOTCH_RangePos_STU dRangeRes_Dy,pNOTCH_RangePos_STU dRangeRes_Cham,int nRangeCnt,int nCenX,int nCenY,int nStartX,int nFirstY,int nLastY,std::multimap<int,CSplinePoint> *mapChamfer);//20140528
    int            CropDataXY2(std::multimap<int,CSplinePoint> *mapCrop,std::multimap<int,CSplinePoint> *mapDist,int nStartX,int nFirstY,int nLastY,double *dResDist);
    int            CropDataXY2_Chamfer(std::multimap<int,CSplinePoint> *mapCrop,std::multimap<int,CSplinePoint> *mapDist,int nStartX,int nFirstY,int nLastY);//20140528
    CSplinePoint    FindCenter2Vec(std::multimap<int,CSplinePoint> *mapCrop,int nCenY,int *nPosition);
    void        GetRangeAvgThick(std::multimap<int,CSplinePoint> *mapCrop,pNOTCH_RangePos_STU pReangeRes,CSplinePoint spCenter,int nRangeCnt);
    void        GetRangeAvgThick_New(std::multimap<int,CSplinePoint> *mapCrop,pNOTCH_RangePos_STU pReangeRes,CPoint point,int nRangeCnt);//20140528
    void        GetRangeAvgThick_New_Cham(std::multimap<int,CSplinePoint> *mapCropChamfer,pNOTCH_RangePos_STU pReangeRes,CPoint point,int nRangeCnt);//20140528
    void        Inspection_Size(CSISBuffer &pOrg,COwnerBuffer &pRes,COwnerBuffer &pRes_Dy,COwnerBuffer &pRes_Cham,CSISBuffer &pBinEdge,CSISBuffer &pBin,double *dChamferAvgThick,double *dChipAvgThick,int nCenX,int nCenY,int nGlass2Edge,double *dFeedbackXY
        ,int iDimension, int icam, std::multimap<int,CSplinePoint> *mapDist,std::multimap<int,CSplinePoint> *mapDist_Dy,double *dGlassXY,double *dResult,std::multimap<int,CSplinePoint> *mapChamfer,double dTheta);    //20140528
    void        ThinImage(CSISBuffer &pBinThin,int iscan, int icam);    
    BOOL        DistInspection(CSISBuffer &pTgt,CSISBuffer &pOrg,double *dResult,double *dRangeRes,double *dAvgThick,int nCenX,int nCenY,int nGlass2Edge, double *dFeedbackXY,int iscan, int icam);
    BOOL        DistInspection_New(CSISBuffer &pBin,CSISBuffer &pOrg,double *dChamferThick,double *dChipAvgThick,int nCenX,int nCenY,int nGlass2Edge
        , double *dFeedbackXY,int iDimension, int icam, std::multimap<int,CSplinePoint> *mapDist,std::multimap<int,CSplinePoint> *mapDist_Dy,double *dGlassXY,int *nUpVPos,int *nDwVPos,std::multimap<int,CSplinePoint> *mapChamfer,double dTheta);    //20140528
    BOOL        Inspection_SpChip(CSISBuffer &pBin,CSISBuffer &pOrg,int nGlass2Edge,int nUpVPos,int nDwVPos);
    int            Defect_Analyze(std::multimap<double,CDetectPoint> *mapDist,double dAvgThick);
    void        GetSpChipData(vector<pair<int,CPoint>> *pVec,CRect *rectIns);
    double        Detect_Position(CSISBuffer &pBin,CSISBuffer &pOrg,std::multimap<double,CDetectPoint> *mapDist
        ,vector<pair<int,CPoint>> *vecData);
    void        DrawImgRect(CSISBuffer &pOrg,double dX,double dY,int nValue=128);
    int            AnalysisThick(CRect rectImg,double dRes,int nJudgeThick,double dAvgThick);
    int            GetRangeDivThick(std::multimap<int, CSplinePoint> *mapCrop,CSplinePoint    spCenter,int nCenterPos,pNOTCH_RangePos_STU pRangeRes,int nRangeCnt,BOOL bOpt,int nDivRange);
    void        SetBrokenDefect(CPoint pointBroken);
 
protected:    
    void        AssertRect(CRect &rect,CSISBuffer pOrg);    
    int            FindLeftLine(CSISBuffer &pImg,CRect rect,int nThres);
    int            FindLeftLineReal(CSISBuffer &pImg,CRect rect,int nThres);
    void        SetNotchImgResPoint(COwnerBuffer *pRes,pNOTCH_RangePos_STU RangeVal,int nRangeCnt);
    void        bresenham_Line(COwnerBuffer *pRes,CPoint posStart,CPoint posEnd,BYTE color);
 
protected:
    void        FindRealPos(CSISBuffer &pOrg,CSISBuffer &pBin,int iLeftLine,int &nCenX,int &nCenY
        ,int *nStartX,int *nFisrtY,int *nLastY,int iCam,int iDem,int nSkipSize=10);    
    void        FindYPosMerge(std::vector<int> *vList,std::vector<NOTCH_Merge_STU> *vMergeList);
    void        FindYPosPixel(CSISBuffer &pImg,CRect rect,std::vector<int> *vList,int nThres,int u,BOOL bDir=TRUE);
    int            FindAccumYPos(CSISBuffer &pImg,CRect rect,int nThres);
    void        FindRealAccumXY(CSISBuffer &pImg,CRect rect,int nThres,int *nStartX,int *nFirstY,int *nLastY);
    double        FindRealAccumYPos(CSISBuffer &pImg,int nSkipSize,int nThres,int *iLeftLine);    
    double        FindRealCenter(CSISBuffer &pImg,CRect rect,int nThres,int *nStartX,int *nFirstY,int *nLastY);    
    void        FindRealYPosMerge(std::vector<int> *vList,std::vector<NOTCH_Merge_STU> *vMergeList);
    void        MakeFoldImage(CSISBuffer &pImg,int nPosY);
    void        MakeReal2Cad(CSISBuffer &pOrg,int nCenX,int nCenY,int iDimension=0, int icam=0);    
    void        MakeReal2AlignCad(CSISBuffer &pOrg,int nCenY,int nForeX,int iDimension=0, int icam=0);    
    void        MakeReal2Cad_Image(CSISBuffer &pOrg,COwnerBuffer &pRes,int nValue=128);
    void        ResetData();
    PointD        GetCenterPointFrom3Points(const PointD& S, const PointD& M, const PointD& E);
    void        Exe_ChipInspect(CSISBuffer &pBin,CSISBuffer &pOrg,int iCam);
    double        FindCenterThickness(CSISBuffer &pOrg,CSISBuffer &pBin,int nCenX,int nCenY,int icam,int iDimension);
 
public:    
    vector<pair<int,CPoint>> m_mapMaster;
    std::multimap<int, CPoint> m_mapOrgMaster[MAX_NOTCH_SCAN_COUNT];
    std::multimap<int, CPoint> m_mapAlignMaster[MAX_NOTCH_SCAN_COUNT];
    vector<pair<int,CPoint>> m_mapMaster_InsPos;//20140528
    std::multimap<int, CPoint> m_mapOrgMaster_InsPos[MAX_NOTCH_SCAN_COUNT];//20140528
    std::multimap<int, CPoint> m_mapAlignMaster_InsPos[MAX_NOTCH_SCAN_COUNT];//20140528
    vector<pair<int,CSplinePoint>> m_mapChipData;
    CChipBlob        m_ResultDefect[MAX_NOTCH_DEFECT_COUNT];
    int                m_nDefectCount;    
 
public:
    NOTCH_Parm_STU        m_InsParm;
    NOTCH_Master_STU    m_MasterParm[MAX_NOTCH_SCAN_COUNT];
    NOTCH_Master_STU    m_AlignMasterParm[MAX_NOTCH_SCAN_COUNT];
    NOTCH_ObjFind_STU    m_ObjectInfo;    
    BOOL                m_bSaveDebug;
    CString                m_strSavePath;
    int                    m_nNoDetCount,m_nTotalCount;
};