#include "stdafx.h" #include "GrindDotProcess.h" #include "VisionRecipe.h" #include "VisionBufferPro.h" #include "FileRecipe.h" CGrindDotProcess::CGrindDotProcess() { /* code */ m_poseType = 0; m_eDir = DIMENSION_NONE; m_id = 0; m_nUse = 0; m_strName.Format(_T("P%d"), m_id); m_nPosY = 0; m_nInc = 0; m_nMin = 0; m_nMax = 0; m_distResult = 0.0f; m_nEdgeThres = 20; m_nLeftLineDist = 0; } CGrindDotProcess::CGrindDotProcess(DimensionDir eDir, int id) { /* code */ m_poseType = 0; m_eDir = eDir; m_id = id; m_nLeftLineDist = 0; m_nUse = 0; switch (eDir) { case GLOBAL_DEFINE::DIMENSION_A: m_strName.Format(_T("TopA_G%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B: m_strName.Format(_T("TopB_G%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C: m_strName.Format(_T("TopC_G%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D: m_strName.Format(_T("TopD_G%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_A_DN: m_strName.Format(_T("BotA_G%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B_DN: m_strName.Format(_T("BotB_G%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C_DN: m_strName.Format(_T("BotC_G%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D_DN: m_strName.Format(_T("BotD_G%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_NONE: m_strName.Format(_T("P_G%d"), m_id); break; default: m_strName.Format(_T("P_G%d"), m_id); break; } m_nPosY = 0; m_nInc = 0; m_nMin = 0; m_nMax = 0; m_distResult = 0.0f; m_nEdgeThres = 20; } CGrindDotProcess::~CGrindDotProcess(){ /* code */ } int CGrindDotProcess::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_ptLeftResult.x = 0; m_ptLeftResult.y = 0; m_leftLineResult = 0; if (1 != m_nUse) return 0; if (0 == m_nLeftLineDist) return 0; //1. »ñÈ¡½âÊÍ¶È getMmvsp(eDir); //2. ²éÕÒleft if (!FindLeftLine(eDir, offset)) return -1; //3. ½á¹ûÅÐ¶Ï judgeResult(); return 0; } bool CGrindDotProcess::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 = m_nLeftLineDist; ptOffset.y = m_nPosY; Point2I result; if (!CVisionBufferPro::TransformToReal(m_eDir, ptOffset, m_poseType, result)) return false; m_locPointResult = result; int x1 = result.x - 200; int y1 = result.y - 31; int x2 = result.x + 200; int y2 = result.y + 31; Point2I leftTop; leftTop.x = x1; leftTop.y = y1; HalconCpp::HObject hImage; if (!CVisionBufferPro::getImageROI(eDir, hImage, x1, y1, x2, y2)) return false; HalconCpp::HObject hMeanImage; HalconCpp::MeanImage(hImage, &hMeanImage, 1, 20); int width = 0; int height = 0; ClsVision::GetImageSize(hImage, width, height); if (width < 10 || height < 25) return false; Line2D line; line.pt0.x = 5; line.pt0.y = 0.5 * height - 1; line.pt1.x = width - 6; line.pt1.y = 0.5 * height - 1; Point2D ptStart, ptEnd; if (!CVisionBufferPro::GetMeasureNearPos(hMeanImage, line, m_nEdgeThres, 0, 10, ptStart, ptEnd)) return false; m_ptMeasureResult = ptStart; m_ptMeasureResult.x += leftTop.x; m_ptMeasureResult.y += leftTop.y; m_ptNearResult = ptEnd; m_ptNearResult.x += leftTop.x; m_ptNearResult.y += leftTop.y; m_ptLeftResult.x = (int)(m_ptMeasureResult.x); m_ptLeftResult.y = (int)(m_ptMeasureResult.y); return true; } void CGrindDotProcess::setLeftLine(int nLeftDist, int leftThres) { /* code */ m_nLeftLineDist = nLeftDist; m_nLeftThres = leftThres; } int CGrindDotProcess::getID(void) { /* code */ return m_id; } void CGrindDotProcess::getMmvsp(DimensionDir eDir) { /* code */ CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(eDir); m_xMmvsp = pSideData->m_dPixelSizeX; m_yMmvsp = pSideData->m_dPixelSizeY; } void CGrindDotProcess::judgeResult(void) { /* code */ CBlSideData* pSideData = CVisionRecipe::getInstance()->getSideData(m_eDir); if (NULL == pSideData) return; if (nullptr != pSideData) { //ÇøÓòµÄÏÔʾ½á¹û //1. Êä³ö±ß½çµã1 DispVisionResult inf; inf.nType = 0; inf.strName = m_strName; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_GRIND); inf.pointX = (int)(m_ptMeasureResult.x); inf.pointY = (int)(m_ptMeasureResult.y); pSideData->m_vDispVisionResult.push_back(inf); //2. Êä³öÖ±Ïß inf.nType = 3; inf.strName = m_strName; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_GRIND); inf.rectX1 = (int)(m_ptMeasureResult.x); inf.rectY1 = (int)(m_ptMeasureResult.y); inf.rectX2 = (int)(m_ptNearResult.x); inf.rectY2 = (int)(m_ptNearResult.y); pSideData->m_vDispVisionResult.push_back(inf); //4. Êä³öλÖà inf.nType = 4; inf.strName = m_strName; inf.eDir = (int)(m_eDir); inf.eVision = (int)(VISION_GRIND); inf.pointX = (int)(m_ptMeasureResult.x); inf.pointY = (int)(m_ptMeasureResult.y); pSideData->m_vDispVisionResult.push_back(inf); } m_nNgInfo.clear(); 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_Grind; double dx = m_xMmvsp *(m_ptMeasureResult.x - m_ptNearResult.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - m_ptNearResult.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; } if (ng.result < m_nMin || ng.result > m_nMax) { ng.isRes = false; } m_nNgInfo.push_back(ng); } Json::Value CGrindDotProcess::WriteToJson(void) { /* code */ std::string strName = CFileRecipe::toString(m_strName); Json::Value jsValue; jsValue["alg type"] = GRIND_VISION_TYPE; jsValue["side"] = (int)(m_eDir); jsValue["id"] = m_id; jsValue["name"] = strName; jsValue["use"] = m_nUse; jsValue["pos y"] = m_nPosY; jsValue["end Thres"] = m_nEdgeThres; jsValue["inc"] = m_nInc; jsValue["min"] = m_nMin; jsValue["max"] = m_nMax; jsValue["pos type"] = m_poseType; return jsValue; } void CGrindDotProcess::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(); } //7. pos y strName = "pos y"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nPosY = jsValue[strName.c_str()].asInt(); } //8. end Thres strName = "end Thres"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nEdgeThres = jsValue[strName.c_str()].asInt(); } //9. inc strName = "inc"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nInc = jsValue[strName.c_str()].asInt(); } //10. min strName = "min"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nMin = jsValue[strName.c_str()].asInt(); } //11. max strName = "max"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_nMax = jsValue[strName.c_str()].asInt(); } //12. pos type strName = "pos type"; if (jsValue.isMember(strName.c_str()) && jsValue[strName.c_str()].isInt()) { m_poseType = jsValue[strName.c_str()].asInt(); } }