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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "stdafx.h"
#include "CutDotProcess.h"
#include "VisionRecipe.h"
#include "VisionBufferPro.h"
#include "FileRecipe.h"
 
CCutDotProcess::CCutDotProcess(){
    /* code */
    m_poseType = 0;
    m_eDir = DIMENSION_NONE;
    m_id = 0;
 
    m_nUse = 0;
    m_strName.Format(_T("P%d"), m_id);
 
    m_nPosX = 0;
    m_nPosY = 0;
    m_nEdgeThres = 120;
 
    m_nInc = 0;
    m_nMin = 0;
    m_nMax = 0;
    m_distResult = 0.0f;
}
 
CCutDotProcess::CCutDotProcess(DimensionDir eDir, int id) {
    /* code */
    m_poseType = 0;
    m_eDir = eDir;
    m_id = id;
 
    m_nUse = 0;
    switch (eDir) {
    case GLOBAL_DEFINE::DIMENSION_A:
        m_strName.Format(_T("TopA_L%d"), m_id);
        break;
    case GLOBAL_DEFINE::DIMENSION_B:
        m_strName.Format(_T("TopB_L%d"), m_id);
        break;
    case GLOBAL_DEFINE::DIMENSION_C:
        m_strName.Format(_T("TopC_L%d"), m_id);
        break;
    case GLOBAL_DEFINE::DIMENSION_D:
        m_strName.Format(_T("TopD_L%d"), m_id);
        break;
    case GLOBAL_DEFINE::DIMENSION_A_DN:
        m_strName.Format(_T("BotA_L%d"), m_id);
        break;
    case GLOBAL_DEFINE::DIMENSION_B_DN:
        m_strName.Format(_T("BotB_L%d"), m_id);
        break;
    case GLOBAL_DEFINE::DIMENSION_C_DN:
        m_strName.Format(_T("BotC_L%d"), m_id);
        break;
    case GLOBAL_DEFINE::DIMENSION_D_DN:
        m_strName.Format(_T("BotD_L%d"), m_id);
        break;
    case GLOBAL_DEFINE::DIMENSION_NONE:
        m_strName.Format(_T("P_L%d"), m_id);
        break;
    default:
        m_strName.Format(_T("P_Cut_Dot%d"), m_id);
        break;
    }
 
    m_nPosX = 0;
    m_nPosY = 0;
    m_nEdgeThres = 120;
 
    m_nInc = 0;
    m_nMin = 0;
    m_nMax = 0;
 
    m_distResult = 0.0f;
}
 
CCutDotProcess::~CCutDotProcess(){
    /* code */
}
 
int CCutDotProcess::Execute(DimensionDir eDir, Point2I offset) {
    /* code */
    m_nNgInfo.clear();
    m_ptNearResult.x = 0.0f;
    m_ptNearResult.y = 0.0f;
    m_ptMeasureResult.x = 0.0f;
    m_ptMeasureResult.y = 0.0f;
    m_distResult = 0.0f;
    if (1 != m_nUse) return 0;
    if (0 == m_nPosX && 0 == m_nPosY)  return 0;
 
    //1. »ñÈ¡½âÊͶÈ
    getMmvsp(eDir);
 
    //2. ²éÕÒleft
    if (!FindLeftLine(eDir, offset)) return -1;
 
    //3. ½á¹ûÅжÏ
    judgeResult();
 
    return 0;
}
 
bool CCutDotProcess::FindLeftLine(DimensionDir eDir, Point2I offset) {
    /* code */
    CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir);
    if (NULL == pSideData)  return false;
    if (0 == m_poseType) {
        if (!pSideData->m_bTopMark_Find)  return false;
        if (!pSideData->m_bBotMark_Find)  return false;
    }
    else {
        if (!pSideData->m_bTopPoint_Find)  return false;
        if (!pSideData->m_bBotPoint_Find)  return false;
    }
 
    Point2I ptOffset;
    ptOffset.x = -1 * m_nPosX;
    ptOffset.y = m_nPosY;
    Point2I result;
    if (!CVisionBufferPro::TransformToReal(m_eDir, ptOffset, m_poseType, result))  return false;
    m_locPointResult = result;
 
    double fzSearchRange = 500.0f;
    int nSearchRange = (int)(fzSearchRange / pSideData->m_dPixelSizeX);
 
    Point2I cutResult;
    if (!CVisionBufferPro::findCutLine(m_eDir, result, nSearchRange, m_nEdgeThres, cutResult)) return false;
 
    m_ptMeasureResult.x = cutResult.x;
    m_ptMeasureResult.y = cutResult.y;
    m_ptNearResult = m_ptMeasureResult;
 
    Line2D line;
    line.pt0.x = 0.0f;  // m_Points[0].x;
    line.pt0.y = 0.0f;  // m_Points[0].y;
    line.pt1.x = m_xMmvsp * (m_Points[1].x - m_Points[0].x);
    line.pt1.y = m_yMmvsp * (m_Points[1].y - m_Points[0].y);
    Point2D ptEdge;
    ptEdge.x = m_xMmvsp * (m_ptMeasureResult.x - m_Points[0].x);
    ptEdge.y = m_yMmvsp * (m_ptMeasureResult.y - m_Points[0].y);
 
    Point2D pose = ClsVision::ProjectionPL(ptEdge, line);
    Point2D posProject;
    posProject.x = m_Points[0].x + pose.x / pSideData->m_dPixelSizeX;
    posProject.y = m_Points[0].y + pose.y / pSideData->m_dPixelSizeY;
 
    if (nullptr != pSideData) {  //ÇøÓòµÄÏÔʾ½á¹û
        DispVisionResult inf;
        inf.nType = 3;
        inf.eDir = (int)(m_eDir);
        inf.strName = m_strName;
        inf.eVision = (int)(VISION_CUT);
        inf.rectX1 = (int)(cutResult.x);
        inf.rectY1 = (int)(cutResult.y);
        inf.rectX2 = (int)(posProject.x);
        inf.rectY2 = (int)(posProject.y);
        pSideData->m_vDispVisionResult.push_back(inf);
    }
 
    return true;
}
 
int CCutDotProcess::getID(void) {
    /* code */
    return m_id;
}
 
void CCutDotProcess::getMmvsp(DimensionDir eDir) {
    /* code */
    CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(eDir);
    m_xMmvsp = pSideData->m_dPixelSizeX;
    m_yMmvsp = pSideData->m_dPixelSizeY;
}
 
void CCutDotProcess::judgeResult(void) {
    /* code */
    m_nNgInfo.clear();
    CBlSideData* pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir);
    if (NULL == pSideData)  return;
    if (NULL != pSideData) {
        DispVisionResult inf;
        inf.nType = 4;
        inf.eDir = (int)(m_eDir);
        inf.strName = m_strName;
        inf.eVision = (int)(VISION_CUT);
        inf.pointX = (int)(m_ptMeasureResult.x);
        inf.pointY = (int)(m_ptMeasureResult.y);
        pSideData->m_vDispVisionResult.push_back(inf);
    }
 
    NgDotInfo ng;
    ng.eDir = m_eDir;
    ng.id = m_id;
    ng.isRes = true;
    ng.minValue = m_nMin;
    ng.maxValue = m_nMax;
    ng.strName = m_strName;
    ng.ngType = DefectLoc_OK;   //ȱÏÝÀàÐÍ
    ng.eVision = VISION_KDIST;
 
    ng.xPosPxl = m_locPointResult.x;
    ng.yPosPxl = m_locPointResult.y;
    ng.xResult = 0.0f;
    ng.yResult = 0.0f;
    ng.x1 = m_locPointResult.x - 40;
    ng.y1 = m_locPointResult.y - 20;
    ng.x2 = m_locPointResult.x + 40;
    ng.y2 = m_locPointResult.y + 20;
 
    ng.ngType = DefectLoc_Dist_Cut;
    Line2D line;
    line.pt0.x = 0.0f;  // m_Points[0].x;
    line.pt0.y = 0.0f;  // m_Points[0].y;
    line.pt1.x = m_xMmvsp * (m_Points[1].x - m_Points[0].x);
    line.pt1.y = m_yMmvsp * (m_Points[1].y - m_Points[0].y);
 
    Point2D ptEdge;
    ptEdge.x = m_xMmvsp * (m_ptMeasureResult.x - m_Points[0].x);
    ptEdge.y = m_yMmvsp * (m_ptMeasureResult.y - m_Points[0].y);
 
    Point2D pose = ClsVision::ProjectionPL(ptEdge, line);
    double dx = (ptEdge.x - pose.x);
    double dy = (ptEdge.y - pose.y);
    m_distResult = sqrt(dx * dx + dy * dy);
 
    m_distResult += m_nInc;
    ng.result = m_distResult;
 
    if (m_nMax < 0.5) {
        m_nNgInfo.push_back(ng);
        return;
    }
 
    double dDist = m_distResult - abs(m_nPosX);
    if (dDist < m_nMin || dDist > m_nMax) {
        ng.isRes = false;
    }
    m_nNgInfo.push_back(ng);
}
 
void CCutDotProcess::setPoint(Point2I pose, int nType) {
    /* code */
    if (0 == nType) {
        m_Points[0] = pose;
    }
    else if (1 == nType) {
        m_Points[1] = pose;
    }
}
 
Json::Value CCutDotProcess::WriteToJson(void) {
    /* code */
    std::string strName = CFileRecipe::toString(m_strName);
    Json::Value jsValue;
    jsValue["alg type"] = CUT_VISION_TYPE;
    jsValue["side"] = (int)(m_eDir);
    jsValue["id"] = m_id;
    jsValue["name"] = strName;
    jsValue["use"] = m_nUse;
    jsValue["pos x"] = m_nPosX;
    jsValue["pos y"] = m_nPosY;
    jsValue["inc"] = m_nInc;
    jsValue["min"] = m_nMin;
    jsValue["max"] = m_nMax;
    jsValue["pos type"] = m_poseType;
    jsValue["line thres"] = m_nEdgeThres;
 
    return jsValue;
}
 
void CCutDotProcess::DecodeJson(Json::Value &jsValue) {
    /* code */
    int num = (int)(jsValue.size());
    if (num < 1) return;
 
    //1. side
    std::string strName = "side";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_eDir = (DimensionDir)(jsValue[strName.c_str()].asInt());
    }
 
    //2. id
    strName = "id";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_id = jsValue[strName.c_str()].asInt();
    }
 
    //3. name
    strName = "name";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isString()) {
        m_strName = CFileRecipe::toCString(jsValue[strName.c_str()].asString());
    }
 
    //4. use
    strName = "use";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_nUse = jsValue[strName.c_str()].asInt();
    }
 
    //5. pos x
    strName = "pos x";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_nPosX = jsValue[strName.c_str()].asInt();
    }
 
    //6. pos y
    strName = "pos y";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_nPosY = jsValue[strName.c_str()].asInt();
    }
 
    //7. inc
    strName = "inc";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_nInc = jsValue[strName.c_str()].asInt();
    }
 
    //8. min
    strName = "min";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_nMin = jsValue[strName.c_str()].asInt();
    }
 
    //9. max
    strName = "max";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_nMax = jsValue[strName.c_str()].asInt();
    }
 
    //10. pos type
    strName = "pos type";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_poseType = jsValue[strName.c_str()].asInt();
    }
 
    //11. line width
    strName = "line thres";
    if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) {
        m_nEdgeThres = jsValue[strName.c_str()].asInt();
    }
}