#include "stdafx.h" #include "DistDotProcess.h" #include "VisionBufferPro.h" CDistDotProcess::CDistDotProcess(){ /* code */ m_id = 0; m_eDir = DIMENSION_A; m_strName.Format(_T("P%d"), m_id); m_idxRefer = 0; m_nRef = 100; m_nRef_ = 100; m_distMode = 1; m_minValue = 0; m_maxValue = 0; m_distMode2 = 5; m_minValue2 = 0; m_maxValue2 = 0; m_xPos = 0.0f; m_yPos = 0.0f; m_cx = 0.0f; m_cy = 0.0f; m_angle = 0.0f; m_len1 = 0.0f; m_len2 = 0.0f; m_xMmvsp = 0.0f; m_yMmvsp = 0.0f; m_ngType = DefectLoc_Grind; m_ngType2 = DefectLoc_Dist_Cut; m_incValue = 0; m_incValue2 = 0; ClsVision::GenEmptyObject(m_hRegion); ClsVision::GenEmptyObject(m_hLineRegion); } CDistDotProcess::~CDistDotProcess(){ /* code */ } CDistDotProcess::CDistDotProcess(DimensionDir eDir, int id) { /* code */ m_id = id; m_eDir = eDir; switch (eDir){ case GLOBAL_DEFINE::DIMENSION_A: m_strName.Format(_T("TopA%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B: m_strName.Format(_T("TopB%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C: m_strName.Format(_T("TopC%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D: m_strName.Format(_T("TopD%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_A_DN: m_strName.Format(_T("BotA%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_B_DN: m_strName.Format(_T("BotB%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_C_DN: m_strName.Format(_T("BotC%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_D_DN: m_strName.Format(_T("BotD%d"), m_id); break; case GLOBAL_DEFINE::DIMENSION_NONE: m_strName.Format(_T("P%d"), m_id); break; default: m_strName.Format(_T("P%d"), m_id); break; } m_idxRefer = 0; m_nRef = 100; m_nRef_ = 100; m_distMode = 1; m_minValue = 0; m_maxValue = 0; m_distMode2 = 5; m_minValue2 = 0; m_maxValue2 = 0; m_xPos = 0.0f; m_yPos = 0.0f; m_cx = 0.0f; m_cy = 0.0f; m_angle = 0.0f; m_len1 = 0.0f; m_len2 = 0.0f; m_xMmvsp = 0.0f; m_yMmvsp = 0.0f; m_incValue = 0; m_incValue2 = 0; ClsVision::GenEmptyObject(m_hRegion); ClsVision::GenEmptyObject(m_hLineRegion); m_ngType = DefectLoc_Grind; m_ngType2 = DefectLoc_Dist_Cut; } int CDistDotProcess::getID(void) { /* code */ return m_id; } int CDistDotProcess::Execute(DimensionDir eDir, MeasureInfo inf, Point2I offset) { /* code */ m_nNgInfo.clear(); m_ptMatchResult.x = 0.0f; m_ptMatchResult.y = 0.0f; m_ptNearResult.x = 0.0f; m_ptNearResult.y = 0.0f; m_ptMeasureResult.x = 0.0f; m_ptMeasureResult.y = 0.0f; m_locPointResult.x = 0; m_locPointResult.y = 0; getMmvsp(eDir); getKeyPose(offset); //²éÕұ߽ç int nRet = findPairEdge(eDir, inf, offset); // if (0 != nRet) return nRet; JudgeResult(); return nRet; } void CDistDotProcess::JudgeResult(void) { /* code */ JudgeFirstResult(); JudgeSecondResult(); } void CDistDotProcess::JudgeFirstResult(void) { /* code */ if (0 == m_distMode) return; NgDotInfo ng; ng.eDir = m_eDir; ng.id = m_id; ng.isRes = true; ng.minValue = m_minValue; ng.maxValue = m_maxValue; ng.strName = m_strName; ng.ngType = DefectLoc_OK; //ȱÏÝÀàÐÍ ng.eVision = VISION_NONE; // VISION_DIST; 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; if (1 == m_distMode) { //Grind Size ng.ngType = m_ngType; double dx = m_xMmvsp *(m_ptMeasureResult.x - m_ptNearResult.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - m_ptNearResult.y); ng.result = sqrt(dx * dx + dy * dy); } else if (2 == m_distMode) { //Top Point ng.ngType = m_ngType; double dx = m_xMmvsp *(m_ptMeasureResult.x - m_topPoint.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - m_topPoint.y); ng.result = sqrt(dx * dx + dy * dy); } else if (3 == m_distMode) { //Bot Point ng.ngType = m_ngType; double dx = m_xMmvsp *(m_ptMeasureResult.x - m_botPoint.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - m_botPoint.y); ng.result = sqrt(dx * dx + dy * dy); } else if (4 == m_distMode) { //Key Dist ng.ngType = m_ngType; double dx = m_xMmvsp *(m_ptMeasureResult.x - m_ptMatchResult.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - m_ptMatchResult.y); ng.result = sqrt(dx * dx + dy * dy); } else if (5 == m_distMode) { //Mark Line Line2D line; line.pt0.x = m_topMark.x; line.pt0.y = m_topMark.y; line.pt1.x = m_botMark.x; line.pt1.y = m_botMark.y; Point2D pose = ClsVision::ProjectionPL(m_ptMeasureResult, line); double dx = m_xMmvsp *(m_ptMeasureResult.x - pose.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - pose.y); ng.result = sqrt(dx * dx + dy * dy); ng.ngType = m_ngType; } if (ng.result + m_incValue > 0.0f) { ng.result += m_incValue; } if (m_maxValue < 0.01f || m_maxValue <= m_minValue + 0.001) { m_nNgInfo.push_back(ng); return; } if (ng.result < m_minValue || ng.result > m_maxValue) { ng.isRes = false; } m_nNgInfo.push_back(ng); } void CDistDotProcess::JudgeSecondResult(void) { /* code */ if (0 == m_distMode2) return; NgDotInfo ng; ng.eDir = m_eDir; ng.id = m_id; ng.isRes = true; ng.minValue = m_minValue2; ng.maxValue = m_maxValue2; ng.strName = m_strName; ng.ngType = DefectLoc_OK; //ȱÏÝÀàÐÍ ng.eVision = VISION_NONE; // VISION_DIST; 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; if (1 == m_distMode2) { //Grind Size ng.ngType = m_ngType2; double dx = m_xMmvsp *(m_ptMeasureResult.x - m_ptNearResult.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - m_ptNearResult.y); ng.result = sqrt(dx * dx + dy * dy); } else if (2 == m_distMode2) { //Top Point ng.ngType = m_ngType2; double dx = m_xMmvsp *(m_ptMeasureResult.x - m_topPoint.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - m_topPoint.y); ng.result = sqrt(dx * dx + dy * dy); } else if (3 == m_distMode2) { //Bot Point ng.ngType = m_ngType2; double dx = m_xMmvsp *(m_ptMeasureResult.x - m_botPoint.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - m_botPoint.y); ng.result = sqrt(dx * dx + dy * dy); } else if (4 == m_distMode2) { //Key Dist ng.ngType = m_ngType2; double dx = m_xMmvsp *(m_ptMeasureResult.x - m_ptMatchResult.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - m_ptMatchResult.y); ng.result = sqrt(dx * dx + dy * dy); } else if (5 == m_distMode2) { //Mark Line Line2D line; line.pt0.x = m_topMark.x; line.pt0.y = m_topMark.y; line.pt1.x = m_botMark.x; line.pt1.y = m_botMark.y; Point2D pose = ClsVision::ProjectionPL(m_ptMeasureResult, line); double dx = m_xMmvsp *(m_ptMeasureResult.x - pose.x); double dy = m_yMmvsp *(m_ptMeasureResult.y - pose.y); ng.result = sqrt(dx * dx + dy * dy); ng.ngType = m_ngType2; } if (ng.result + m_incValue2 > 0.0f) { ng.result += m_incValue2; } if (m_maxValue2 < 0.01f || m_maxValue2 <= m_minValue2 + 0.001) { m_nNgInfo.push_back(ng); return; } if (ng.result < m_minValue2 || ng.result > m_maxValue2) { ng.isRes = false; } m_nNgInfo.push_back(ng); } void CDistDotProcess::getMmvsp(DimensionDir eDir) { /* code */ CBlSideData *pSideData = CVisionRecipe::getInstance()->getSideData(eDir); m_xMmvsp = pSideData->m_dPixelSizeX; m_yMmvsp = pSideData->m_dPixelSizeY; } int CDistDotProcess::getKeyPose(Point2I offset) { /* code */ if (fabs(m_xPos) < 0.01 || fabs(m_yPos) < 0.01) return -1; m_ptMatchResult.x = m_xPos + offset.x; m_ptMatchResult.y = m_yPos + offset.y; return 0; } int CDistDotProcess::findPairEdge(DimensionDir eDir, MeasureInfo infMeasure, Point2I offset) { /* code */ //1. ͼÏñÔ¤´¦Àí if (m_len1 < 0.01 || m_len2 < 0.01) return -3; Line2D line = GetTargeLine(); line.pt0.x += offset.x; line.pt0.y += offset.y; line.pt1.x += offset.x; line.pt1.y += offset.y; int x1 = (int)(line.pt0.x - 30); if (line.pt0.x > line.pt1.x) x1 = (int)(line.pt1.x - 30); int x2 = (int)(line.pt0.x + 30); if (line.pt0.x < line.pt1.x) x2 = (int)(line.pt1.x + 30); int y1 = (int)(line.pt0.y - 30); if (line.pt0.y > line.pt1.y) y1 = (int)(line.pt1.y - 30); int y2 = (int)(line.pt0.y + 30); if (line.pt0.y < line.pt1.y) y2 = (int)(line.pt1.y + 30); int leftPos = x1; int topPos = y1; m_locPointResult.x = (int)(m_cx + offset.x); m_locPointResult.y = (int)(m_cy + offset.y); HalconCpp::HObject hImage; if (!CVisionBufferPro::getImageROI(eDir, hImage, x1, y1, x2, y2)) return -3; line.pt0.x -= leftPos; line.pt0.y -= topPos; line.pt1.x -= leftPos; line.pt1.y -= topPos; // HalconCpp::HObject hProcImage; // HalconCpp::MedianImage(hImage, &hProcImage, "circle", 1, "mirrored"); bool valid = false; if (0 == infMeasure.typePoint) { //valid = CVisionBufferPro::GetMeasurePos(hImage, line, // infMeasure.typePoint, // infMeasure.pointThres, infMeasure.modePoint, // infMeasure.nearThres, infMeasure.modeNear, // 5, m_ptMeasureResult, m_ptNearResult); } else { //valid = CVisionBufferPro::GetMeasurePos(hImage, line, // infMeasure.typePoint, // infMeasure.pointThres, infMeasure.modePoint, // infMeasure.nearThres, infMeasure.modeNear, // 5, m_ptNearResult, m_ptMeasureResult); } if (!valid) { m_ptMeasureResult.x = 0.0f; m_ptMeasureResult.y = 0.0f; m_ptNearResult.x = 0.0f; m_ptNearResult.y = 0.0f; } else { m_ptMeasureResult.x += leftPos; m_ptMeasureResult.y += topPos; m_ptNearResult.x += leftPos; m_ptNearResult.y += topPos; } if (!valid) { return -4; } m_locPointResult.x = (int)(m_ptMeasureResult.x); m_locPointResult.y = (int)(m_ptMeasureResult.y); return 0; } Line2D CDistDotProcess::GetTargeLine(void) { /* code */ Line2D lineResult; double angle = m_angle; double xa = m_len1; double xb = m_len2; double x = m_cx; double y = m_cy; Point2D ptCenter; ptCenter.x = x; ptCenter.y = y; double phi = ClsVision::DegreeToRad(angle); double cosx = ClsVision::GetCos(phi); double sinx = ClsVision::GetSin(phi); lineResult.pt0.x = ptCenter.x + xa * cosx; lineResult.pt0.y = ptCenter.y - xa * sinx; lineResult.pt1.x = 2 * ptCenter.x - lineResult.pt0.x; lineResult.pt1.y = 2 * ptCenter.y - lineResult.pt0.y; return lineResult; } int CDistDotProcess::Serialized(void* pBuffer, int size) { /* code */ char* pTemp = (char*)pBuffer; int remainSize = size; int useSize = 0; int nRet = 0; #if 0 //1. ±£´æIDºÍÃû³Æ int iValue = (int)(m_eDir); SERIALIZER_INT_32(iValue, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_id, pTemp, remainSize, useSize); std::string strName(CT2A(m_strName.GetString())); SERIALIZER_STRING(strName, pTemp, remainSize, useSize); //2. ±£´æÇøÓò·¶Î§ SERIALIZER_DOUBLE(m_xPos, pTemp, remainSize, useSize); SERIALIZER_DOUBLE(m_yPos, pTemp, remainSize, useSize); SERIALIZER_DOUBLE(m_cx, pTemp, remainSize, useSize); SERIALIZER_DOUBLE(m_cy, pTemp, remainSize, useSize); SERIALIZER_DOUBLE(m_angle, pTemp, remainSize, useSize); SERIALIZER_DOUBLE(m_len1, pTemp, remainSize, useSize); SERIALIZER_DOUBLE(m_len2, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_idxRefer, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_nRef, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_nRef_, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_distMode, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_minValue, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_maxValue, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_ngType, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_incValue, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_distMode2, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_minValue2, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_maxValue2, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_ngType2, pTemp, remainSize, useSize); SERIALIZER_INT_32(m_incValue2, pTemp, remainSize, useSize); #endif return useSize; } int CDistDotProcess::Deserialized(void* pBuffer, int size) { /* code */ char* pTemp = (char*)pBuffer; int remainSize = size; int useSize = 0; int nRet = 0; #if 0 //1. ±£´æIDºÍÃû³Æ int iValue = 0; DESERIALIZER_INT_32(iValue, pTemp, remainSize, useSize); m_eDir = (DimensionDir)(iValue); DESERIALIZER_INT_32(m_id, pTemp, remainSize, useSize); std::string strName; DESERIALIZER_STRING(strName, pTemp, remainSize, useSize); m_strName = CA2T(strName.c_str()); //2. ±£´æÇøÓò·¶Î§ DESERIALIZER_DOUBLE(m_xPos, pTemp, remainSize, useSize); DESERIALIZER_DOUBLE(m_yPos, pTemp, remainSize, useSize); DESERIALIZER_DOUBLE(m_cx, pTemp, remainSize, useSize); DESERIALIZER_DOUBLE(m_cy, pTemp, remainSize, useSize); DESERIALIZER_DOUBLE(m_angle, pTemp, remainSize, useSize); DESERIALIZER_DOUBLE(m_len1, pTemp, remainSize, useSize); DESERIALIZER_DOUBLE(m_len2, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_idxRefer, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_nRef, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_nRef_, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_distMode, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_minValue, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_maxValue, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_ngType, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_incValue, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_distMode2, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_minValue2, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_maxValue2, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_ngType2, pTemp, remainSize, useSize); DESERIALIZER_INT_32(m_incValue2, pTemp, remainSize, useSize); if (m_xPos <= 0.001 && m_yPos <= 0.001) { HalconCpp::GenEmptyObj(&m_hRegion); } else { HalconCpp::GenCircle(&m_hRegion, m_yPos, m_xPos, 25); } if (m_len1 < 0.01 || m_len2 < 0.01) { HalconCpp::GenEmptyObj(&m_hRegion); } else { HalconCpp::GenRectangle2(&m_hLineRegion, m_cy, m_cx, ClsVision::DegreeToRad(m_angle), m_len1, m_len2); } #endif return useSize; } void CDistDotProcess::setRoiRegion(HalconCpp::HObject &hRegion) { /* code */ m_xPos = 0.0f; m_yPos = 0.0f; if (!ClsVision::RegionExist(hRegion)){ HalconCpp::GenEmptyObj(&m_hRegion); return; } int nArea = 0; ClsVision::AreaCenter(hRegion, nArea, m_xPos, m_yPos); m_hRegion = hRegion; } void CDistDotProcess::setLineRegion(HalconCpp::HObject &hRegion) { /* code */ m_cx = 0.0f; m_cy = 0.0f; m_angle = 0.0f; m_len1 = 0.0f; m_len2 = 0.0f; if (!ClsVision::RegionExist(hRegion)) { HalconCpp::GenEmptyObj(&m_hLineRegion); return; } ClsVision::SmallestRectangle2(hRegion, m_cx, m_cy, m_angle, m_len1, m_len2); m_hLineRegion = hRegion; } Point2I CDistDotProcess::getPose(void) { /* code */ Point2I result; result.x = (int)(m_cx); result.y = (int)(m_cy); return result; } void CDistDotProcess::setPoint(Point2I pt, int nType) { /* code */ if (0 == nType) { m_topMark = pt; } else if (1 == nType) { m_botMark = pt; } else if (2 == nType) { m_topPoint = pt; } else if (3 == nType) { m_botPoint = pt; } }