mrDarker
2025-07-14 1c0ac1c7924efb8a2cb6962d3eda4126533a5ac8
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
#pragma once
 
#include "ThreadControl.h"
#include "DefectStorage.h"
#include "Glass_Data.h"
#include "HoleInspector.h"
 
interface IInspect2Sequence
{
public:    
    virtual void            II2S_InspectionEnd(int iCamID,int iScan) = 0;        
};
 
enum    ERR_FIND_GLASSSTARTLINE {ERR_FINDGLASSSTART_FAIL=0, ERR_FINDGLASSSTART_SUCCESS, ERR_FINDGLASSSTART_FIND_FAIL, ERR_FINDGLASSSTART_COUNT};
 
#define        RCUT_PROFILE_CALCULATE_THETA        22.5
typedef struct STU_SPLINE_PROFILE_POSITION_
{    
    int            iCam;
    int            iSide;
    int            iScan;
    int            iIndex;
    int            iPos;
    int            iLoop;
    double        dX;
    double        dY;
    double        dRadius;
    double        dTheta;
    double        ptCenterX;
    double        ptCenterY;
    CvPoint2D32f    ptRealCenter;
    CvPoint2D32f        ptGlass;
    CvPoint2D32f        ptChamfer;
    CvPoint2D32f        ptCadLine;
    BOOL        bFindPos;
    BOOL        bChamferFind;
    double        dDist;
    double        dDistCnt;
    double        dChamfer;
 
    ST_PROFILE_SETTING    *pSettings;
 
public:
    STU_SPLINE_PROFILE_POSITION_()
    {        
        iCam = iSide = iScan = iIndex = iPos = iLoop = 0;
        dX = dY = dRadius = dTheta = 0.;
        ptGlass.x = ptGlass.y = ptChamfer.x = ptChamfer.y = ptRealCenter.x = ptRealCenter.y = 0;
        ptCadLine.x = ptCadLine.y = 0;
        ptCenterX = ptCenterY = -1;
        bFindPos = bChamferFind = FALSE;
        dDist = dChamfer = dDistCnt = 0.;        
        pSettings = NULL;
    }    
}STU_SPLINE_PROFILE_POSITION, *pSTU_SPLINE_PROFILE_POSITION;
 
typedef struct STU_SPLINE_RCut_
{    
    int            width;
    int            x1,x2,y;    
    int            imgX,imgY;
    double        dTheta;
 
public:
    STU_SPLINE_RCut_()
    {        
        reset();
    }    
    void reset()
    {
        x1 = x2 = y = width = 0;
        imgX = imgY = 0;
        dTheta = 0;
    }
}STU_SPLINE_RCut, *pSTU_SPLINE_RCut;
 
class CGlass_Data;
class CGlassRecipe;
class CHardwareSettings;
class CSISBuffer;
class CInspectCamera : public IThreadWorker
{
public:
    CInspectCamera(int iCam=0);
    virtual ~CInspectCamera(void);
 
public:
    virtual BOOL        OnThreadRun(int iThread, CInspectThread *pInspectThread);
    virtual BOOL        OnThreadEnd(int iThread, CInspectThread *pInspectThread);
    virtual BOOL        OnThreadEndAll();
 
public:
    void                ReleaseThread();
    int                    InitInspect(int nThread,int maxDefect=1000);
    int                    ReInitThread(int nThread);
    void                SetParameter(CGlassRecipe *pRecipe,CHardwareSettings *pHW);
    void                SetGlassData(CGlass_Data *pGlassData);
    void                SetSplineModel(CSplineModel *pSplineModel){m_pSplineModel=pSplineModel;}
    void                SetCADData(std::multimap<int, CPoint *> *pProfileMap,CPoint *ptOrigin);
    void                SetGrabber(CGrabberControl *pGrabber);
    void                SetI2S(IInspect2Sequence *pI2S){m_pII2S=pI2S;}
    BOOL                ScanStart(int iScan);    
    void                SetProcessEnd(){m_bExitThread=TRUE;}
    int                    GetDefectCount();
    CDefect                *GetDefect(int iDefect);
    pINSPECTSPLINE_BUFFER    GetSplineBuffer(int iPos){return &m_SplineImage[iPos];}    
    pINSPECTFULLIMAGE_BUFFER GetFullImgBuffer(int iScan){return &m_FullImgBuffer[iScan];}
    void                SetViewScanHWnd(HWND hWnd){m_hWndViewScan=hWnd;}
    std::multimap<int, CPoint*> *GetProfileAlign(int iScan){return &m_mapAlignProfile[iScan];}
 
protected:    
    BOOL                ScanStartThread();        
    DimensionDir        GetDimension(int iScan);
    BOOL                GetCheckFrame(stFrameIndex stFrame);
    void                SetGrabEnd(int iScan);    
 
    BOOL                CheckStartLineFrame(DimensionDir eDim,int iFrame);    
    BOOL                FindGlassStartLine(DimensionDir emDim,stFrameIndex stFrame);    
    int                    FindLeftLine(int iThread,DimensionDir emDim,stFrameIndex stFrame);
    BOOL                FindEndLine(int iThread,DimensionDir emDim,stFrameIndex stFrame);
    
    void                MakeAlignRegion(int iScan);    
    BOOL                FindAlignMark(int iThread,DimensionDir eDim,int iScan,int iFrame,int iPos);
    BOOL                FindAlignProcess(int iThread,DimensionDir eDim,int iScan,int iFrame);
 
    void                PresetChamferPrm(int iScan,DimensionDir eDim);
    void                MakeChamferRegion(int iScan);
    void                GetChamferRect(stFrameIndex stFrame,DimensionDir eDim,std::vector<CRect> &vecIns);
    DIT_RESULT            FindThickness(int iThread,DimensionDir eDim,stFrameIndex stFrame,int iFindLeft);
    CChamferInspect        *GetChamferControl(int iThread){return m_pChamferControl[iThread];}
    void                GetChamferThres(DimensionDir eDim,int &nChamferThres,int &nChipThres,int &nChipThres_White, int &nChipDiffThres, int &nBurrThres,int &nCrackThres,int &nBrokenThres);    
    int                    ChamferInspectProcess(int iThread,stFrameIndex stFrame,DimensionDir eDim,int iFindLeft,CRect rtIns,int nThresChamfer,int nThresChip,int nThresChip_White,int nThresChipDiff,int nThresCrack,int nThresBroken,int nBurrThres,int *nPrePos);
    int                    ChamferExtractResult(int iThread,DimensionDir eDim,stFrameIndex stFrame,CRect &rect);
        
    CSplineInspect        *GetSplineControl(int iThread){return m_pSplineControl[iThread];}
    DIT_RESULT            FindSpline(int iThread,DimensionDir    emDim,stFrameIndex stFrame,int iFindLine);
    CRect                GetSplineInsRect(CSize szMaster,CPoint nOffset,stFrameIndex stFrame,int iFindLine,INS_EDGE_RESULT_INFO *pEdgeInfo,BOOL &bFlip);
    int                    GetSplineFindEdge(CSISBuffer *pSPBuf,CETC_PARM *pEtc,int iFindLine,int nOffset);
    BOOL                SplineChipProcess(CSplineInspect *pInsSpline,CSISBuffer *imgOrg,int iThread,DimensionDir emDim,stFrameIndex stFrame,int iFindLine,BOOL bFlip,int iTopPixel,int iLeftPixel,CString strCutDiv);
    BOOL                SplineChip_Curve(CSplineInspect *pInsSpline,CSISBuffer *imgOrg,int iThread,DimensionDir emDim,stFrameIndex stFrame,CPoint &pointLeft,int nChipThres,BOOL bFlip,int iTopPixel,int iLeftPixel,CString strCutDiv);
    void                SaveSplineImage(CSISBuffer &pSPBuf,CSplineInspect *pSplineIns,pSTU_SPLINE_INS_PARM pSplinePrm, int iThread,DimensionDir emDim,stFrameIndex stFrame,double dConvRes,SPLINE_ERR_MSG    errMsg,BOOL bFlip);
 
    DIT_RESULT            FindSpline_New(int iThread,DimensionDir    emDim,stFrameIndex stFrame,int iFindLine);
    CRect                GetSplineInsRect_New(int iThread,DimensionDir    emDim,CSize szMaster,CPoint nOffset,stFrameIndex stFrame,int iFindLine,BOOL &bFlip);
    CvPoint2D32f        GetSplineCenter(int iThread,DimensionDir emDim,stFrameIndex stFrame,BOOL bBot);
 
    ROI                    GetRCutChipImage(int iThread,DimensionDir emDim,stFrameIndex stFrame,COwnerBuffer &pTgt,double dTheata,CvPoint2D32f ptCen,double dRadius);
    void                SetRCutDisplayImage(int iThread,DimensionDir emDim,stFrameIndex stFrame,CRect rectIns,BOOL bFlip,double dRadius,CvPoint2D32f ptSplineCen);
    BOOL                CopyRectImg(LPBYTE pOrg,LPBYTE pTgt,CSize szImg,CRect &rectIns);
 
    CHoleInspector        *GetHoleControl(int iThread){return m_pHoleInspect[iThread];}
    DIT_RESULT            FindHoleInspection(int iThread,DimensionDir    emDim,stFrameIndex stFrame,int iFindLine);
    void                MakeHoleRegion(int iScan);
    void                GetHoleRegionFrame(int iThread,DimensionDir emDim,stFrameIndex stFrame, std::vector<HOLE_INS_PARM> &vecArray);
 
    BOOL                ProfileRandom(PROFILE_RESULT &ProfileResult);
    BOOL                ChamferRandom(CHAMFER_RESULT &ChamferResult);
    BOOL                RCutRandom(PROFILE_RESULT &ProfileResult);
    BOOL                RCutChamferRandom(CHAMFER_RESULT &ChamferResult);
 
    BOOL                CheckProfile(int iThread,DimensionDir emDim,stFrameIndex stFrame);
    BOOL                FindProfileProcess(int iThread,DimensionDir emDim,stFrameIndex stFrame,int iFindLine);
    void                CalculateStageTheta(DimensionDir emDim,int iScan,CvPoint2D32f *pAlign);
    BOOL                MakeProfilePosition(DimensionDir emDim,stFrameIndex stFrame,CProfileCADProc *pProcess, vector< pair<int,STU_PROFILE_POSITION*> > &vecProfile,CvPoint2D32f *ptAlign);
    BOOL                CalculateProfileChamfer(DimensionDir emDim,stFrameIndex stFrame,CProfileCADProc *pProcess,vector< pair<int,STU_PROFILE_POSITION*> > &vecProfile,CvPoint2D32f ptAlign);
    BOOL                GetAvgProfileNChamfer(DimensionDir emDim,stFrameIndex stFrame,vector< pair<int,STU_PROFILE_POSITION*> > &vecProfile,double &dProfileAvg,double &dChamferAvg);
    STU_THICKPOS        GetThickPosResultPos(stFrameIndex stFrame,int nYPos);
    CProfileCADProc        *GetProfileControl(int iScan,int iThread){return m_pProfileControl[iScan][iThread];}
 
    BOOL                RCutProfileProcess(DimensionDir emDim,stFrameIndex stFrame,CProfileCADProc *pProcess,vector< pair<double,STU_SPLINE_PROFILE_POSITION*> > &vecProfile,CvPoint2D32f *ptAlign);
    BOOL                IsRCutProfileProcess(DimensionDir emDim);
    BOOL                CheckAlignMark(DimensionDir emDim,int &nIndex);
    BOOL                ReFindAlignMark(DimensionDir emDim,stFrameIndex stFrame,int iPos);
    int                    GetRCutProfileIndexTheta(DimensionDir emDim,stFrameIndex stFrame,vector< pair<double,STU_SPLINE_PROFILE_POSITION*> > &vecProfile,CvPoint2D32f *ptAlign);
    int                    GetRCutProfileIndex(DimensionDir eDimension,int *nRcpIndex,int *nRcpSize);
    int                    GetRCutProfileTheta2Vector(int nPosCnt,DimensionDir eDimension,stFrameIndex stFrame,vector< pair<double,STU_SPLINE_PROFILE_POSITION*> > &vecProfile,int *nRcpIndex,INT *nRcpSize,CvPoint2D32f *ptAlign);
    void                GetRCutCalculateCenterPosition(DimensionDir eDimension,stFrameIndex stFrame,pSTU_SPLINE_PROFILE_POSITION pPfPositon,CvPoint2D32f *ptAlign,int iPos);
    BOOL                FindRCutProfilePosition(DimensionDir eDimension,stFrameIndex stFrame,CProfileCADProc *pProcess,vector< pair<double,STU_SPLINE_PROFILE_POSITION*> > &vecProfile,CvPoint2D32f *ptAlign);
    BOOL                FindRCutPosition(STU_SPLINE_PROFILE_POSITION *pProfilePos,DimensionDir eDimension,stFrameIndex stFrame,CProfileCADProc *pProcess);
    ROI                    GetRCutProcessImage(STU_SPLINE_PROFILE_POSITION *pProfilePos,DimensionDir eDimension,stFrameIndex stFrame,COwnerBuffer &pTgt);
    double                GetRadus2Distance(DimensionDir eDimension,stFrameIndex stFrame,double dtheta,double dRadius,double dSin,double dCons);
    BOOL                FindGlassChamfer(int index,DimensionDir eDimension,stFrameIndex stFrame,CSISBuffer &bufTgt,int nGlassThres,int nChamferThres,IMPOS2D &ptGlassEdgePos,IMPOS2D &ptChamferEdgePos);
    BOOL                CalculateProfileSpline(DimensionDir eDimension,stFrameIndex stFrame,vector< pair<double,STU_SPLINE_PROFILE_POSITION*> > &vecProfile);
 
    void                DeleteCADAlign(std::multimap<int, CPoint*> *pSetProfileAlignMap);
 
protected:    
    //13. ÊÓ¾õ¹¦ÄÜ
    void FinallyVisionProc(DimensionDir eDim, stFrameIndex stFrame);
 
    void                SaveFullImageCopy(int iScan);
    int                    GetLeftMargin(int iCam,int iScan);
    void                GetFrameSize(int iCam,int iScan,int &nFrameWidth,int &nFrameHeight);    
    void                SaveGlassLineImage(int iScan,int nEndLine,int nXPos,DimensionDir emDim,CString strName);
    BOOL                GetCheckExit(){return m_bExitThread;}
    BOOL                SaveCvAlignImage(CvRect saveRoi,CString strFile,int iScan,int nFrameWidth,int nFrameHeight);    
    void                AssertFrameRectWidth(CRect &rect,int iScan);
    void                ScanRegionSet(stFrameIndex stFrame);
    BOOL                CheckProcessEnd(int iThread,stFrameIndex stFrame);        
    BOOL                CheckThreadEnd(int iThread,stFrameIndex stFrame,BOOL bProfile);
    ERR_FIND_GLASSSTARTLINE        IsGlassStartLine(int iThread,int &iScan);
    void                ReleaseSplineBuffer();    
    void                ReleaseFullBuffer();    
    int                    GetThreadEndCount(int iScan);
 
    void                ResetFrameFinish(int nCurrentScanIdx);
    void                SetFrameFinishEndFrame(int nCurrentScanIdx, int nFrameIdx);
    void                SetFrameFinish(int nCurrentScanIdx, int nCurrentFrameIdx);
    BOOL                CheckAllFrameFinish(int nCurrentScanIdx);
 
protected:
    CThreadControl        *m_pThreadControl;
    CGlassRecipe        *m_pRecipe;
    CHardwareSettings    *m_pHardparm;
    CGrabberControl        *m_pGrabber;
    CGlass_Data            *m_pGlassData;
    IInspect2Sequence    *m_pII2S;
    CChamferInspect        *m_pChamferControl[MAX_THREAD];
    CDefectControl        *m_pDefectControl;
    CSplineInspect        *m_pSplineControl[MAX_THREAD];
    CSplineModel        *m_pSplineModel;
    CHoleInspector        *m_pHoleInspect[MAX_THREAD];
    CProfileCADProc        *m_pProfileControl[MAX_SCAN_COUNT][MAX_THREAD];
 
protected:
    int                    m_iCamera;
    int                    m_iScan;    
    int                    m_iSideLine[MAX_SCAN_COUNT];
    BOOL                m_bExitThread;
    double                m_dMark2MarkX[MAX_SCAN_COUNT][MAX_MARKER_COUNT];
    double                m_dMark2MarkY[MAX_SCAN_COUNT][MAX_MARKER_COUNT];
    int                    m_iThreadEnd[MAX_SCAN_COUNT][MAX_THREAD];    
    CCriticalSection    m_csThreadEnd;
    CCriticalSection    m_csThreadGlassStart;    
    CCriticalSection    m_csThreadProfile;    
    CCriticalSection    m_csResultThick;
    CCriticalSection    m_csHoleReion;
    CCriticalSection    m_csProfile;
    CCriticalSection    m_csSaveimage;
    BOOL                m_bFindGlassStart[MAX_SCAN_COUNT];
    INSPECTSPLINE_BUFFER m_SplineImage[MAX_PANEL_SIDE];
    INSPECTFULLIMAGE_BUFFER    m_FullImgBuffer[MAX_SCAN_COUNT];
    BOOL                m_bSplineInspect[2];
    BOOL                m_bProfileFind[MAX_SCAN_COUNT];
    int                    m_nProfileFindFrameIdx[MAX_SCAN_COUNT];
    std::vector<STU_THICKPOS> m_resultThickPos[MAX_SCAN_COUNT];
    vector<HOLE_INS_PARM>    m_vecHoleReg[MAX_SCAN_COUNT];
    HWND                m_hWndViewScan;
    PROG_MSG            m_MsgJob;
    std::multimap<int, CPoint*> m_mapAlignProfile[MAX_SCAN_COUNT];
 
    CCriticalSection    m_csFrameFinishCheck;
    int                    m_nFrameFinishIdx[MAX_SCAN_COUNT];
    BOOL                m_bFrameFinish[MAX_SCAN_COUNT][MAX_FRAM_COUNT];
    double m_fInspectLastTime[8];
};