#include "ROILine.h" #include "HalconCpp.h" #include"HMisc.h" #include "HRegion.h" ROILine::ROILine() :ROI() { using namespace HalconCpp; numHandles = 3; activeHandleIdx = 2; midRow = 0; midCol = 0; startRow = 0; startCol = 0; endRow = 0; endCol = 0; roiType = ROIType::Line; } void ROILine::createROI(double midX, double midY) { midRow = midY; midCol = midX; startRow = midRow; startCol = midCol - 50; endRow = midRow; endCol = midCol + 50; updateArrowHandle(); } void ROILine::drawROI(HTuple winID, double scaleFactor) { Q_UNUSED(scaleFactor); DispLine(winID, startRow, startCol, endRow, endCol); int width=handleWidth; //DispRectangle2(winID, startRow, startCol, 0, width, width); HObject ho_Rectangle; GenRectangle2ContourXld(&ho_Rectangle, startRow, startCol, 0, width, width); DispObj(ho_Rectangle, winID); // if (arrowHandleXLD.IsInitialized()==false) { updateArrowHandle(); } DispObj(arrowHandleXLD, winID); //window.DispRectangle2( row2, col2, 0, 5, 5); //DispRectangle2(winID, midRow, midCol, 0, width, width); GenRectangle2ContourXld(&ho_Rectangle, midRow, midCol, 0, width, width); DispObj(ho_Rectangle, winID); } double ROILine::distToClosestHandle(double x, double y) { double max = 10000; double val[3]; val[0] = HMisc::DistancePp(y, x, startRow, startCol); // upper left val[1] = HMisc::DistancePp(y, x, endRow, endCol); // upper right val[2] = HMisc::DistancePp(y, x, midRow, midCol); // midpoint for (int i = 0; i < numHandles; i++) { if (val[i] < max) { max = val[i]; activeHandleIdx = i; } }// end of for return val[activeHandleIdx]; } void ROILine::displayActive(HTuple winID, double scaleFactor) { Q_UNUSED(scaleFactor); HObject ho_Rectangle; int width=handleWidth; switch (activeHandleIdx) { case 0: //DispRectangle2(winID, startRow, startCol, 0, width, width); GenRectangle2ContourXld(&ho_Rectangle, startRow, startCol, 0, width, width); DispObj(ho_Rectangle, winID); break; case 1: DispObj(arrowHandleXLD, winID); //window.DispRectangle2(row2, col2, 0, 5, 5); break; case 2: //DispRectangle2(winID, midRow, midCol, 0, width, width); GenRectangle2ContourXld(&ho_Rectangle, midRow, midCol, 0, width, width); DispObj(ho_Rectangle, winID); break; } } void ROILine::moveByHandle(double newX, double newY) { double lenR, lenC; switch (activeHandleIdx) { case 0: // first end point startRow = newY; startCol = newX; midRow = (startRow + endRow) / 2; midCol = (startCol + endCol) / 2; break; case 1: // last end point endRow = newY; endCol = newX; midRow = (startRow + endRow) / 2; midCol = (startCol + endCol) / 2; break; case 2: // midpoint lenR = startRow - midRow; lenC = startCol - midCol; midRow = newY; midCol = newX; startRow = midRow + lenR; startCol = midCol + lenC; endRow = midRow - lenR; endCol = midCol - lenC; break; } updateArrowHandle(); } QCursor ROILine::showByHandle() { QCursor cursor; switch (activeHandleIdx) { case 0: // 鼠标在圆上的一点时 cursor= Qt::SizeAllCursor; break; case 1: // 鼠标在圆心上时 cursor= Qt::SizeBDiagCursor; break; } return cursor; } HRegion ROILine::getRegion() { HRegion region; region.GenRegionLine(HTuple(startRow), HTuple(startCol), HTuple(endRow), HTuple(endCol)); return region; } HTuple ROILine::getROIData() { HTuple dat; dat.Clear(); dat[0] = startRow; dat[1] = startCol; dat[2] = endRow; dat[3] = endCol; return dat; } void ROILine::setROIData(HTuple dat) { Q_UNUSED(dat); } void ROILine::save(QDataStream &dataStream) { ROI::save(dataStream); int paranum;//参数数量 paranum = 7; dataStream << paranum;//先保存参数数量 dataStream << (int)1 << startRow; dataStream << (int)2 << startCol; dataStream << (int)3 << endRow; dataStream << (int)4 << endCol; dataStream << (int)5 << midRow; dataStream << (int)6 << midCol; dataStream << (int)7 << m_strTitle; } void ROILine::load(QDataStream &dataStream) { ROI::load(dataStream); int paranum;//参数数量 int para; dataStream >> paranum;//读取参数数量 for (int i = 0; i < paranum; i++) { dataStream >> para; switch (para) { case 1: dataStream >> startRow; break; case 2: dataStream >> startCol; break; case 3: dataStream >> endRow; break; case 4: dataStream >> endCol; break; case 5: dataStream >> midRow; break; case 6: dataStream >> midCol; break; case 7: dataStream >> m_strTitle; break; default: { qWarning() << "配置文档错误!"; } break; } } } void ROILine::updateArrowHandle() { using namespace HalconCpp; double length, dr, dc, halfHW; double rrow1, ccol1, rowP1, colP1, rowP2, colP2; double headLength =handleWidth; double headWidth = handleWidth; arrowHandleXLD.GenEmptyObj(); rrow1 = startRow + (endRow - startRow) * 0.8; ccol1 = startCol + (endCol - startCol) * 0.8; // DistancePp(rrow1, ccol1, endRow, endCol, &length); length = HMisc::DistancePp(rrow1, ccol1, endRow, endCol); if (length == 0) length = -1; dr = (endRow - rrow1) / length; dc = (endCol - ccol1) / length; halfHW = headWidth / 2.0; rowP1 = rrow1 + (length - headLength) * dr + halfHW * dc; rowP2 = rrow1 + (length - headLength) * dr - halfHW * dc; colP1 = ccol1 + (length - headLength) * dc - halfHW * dr; colP2 = ccol1 + (length - headLength) * dc + halfHW * dr; HTuple tupleR, tupleC; tupleR.Clear(); tupleC.Clear(); if (length == -1) { tupleR[0] = rrow1; tupleC[0] = ccol1; } else { tupleR[0] = rrow1; tupleR[1] = endRow; tupleR[2] = rowP1; tupleR[3] = endRow; tupleR[4] = rowP2; tupleR[5] = endRow; tupleC[0] = ccol1; tupleC[1] = endCol; tupleC[2] = colP1; tupleC[3] = endCol; tupleC[4] = colP2; tupleC[5] = endCol; } GenContourPolygonXld(&arrowHandleXLD, tupleR, tupleC); }