#include "ROIRect1.h" ROIRect1::ROIRect1() { numHandles = 5; // 4 corner points + midpoint activeHandleIdx = 4; roiType = ROIType::Rectangle1; midR = 0; midC = 0; row1 = 0; col1 = 0; row2 = 0; col2 = 0; } void ROIRect1::createROI(double midX, double midY) { midR = midY; midC = midX; row1 = midR - 50; col1 = midC - 50; row2 = midR + 50; col2 = midC + 50; } void ROIRect1::drawROI(HTuple winID, double scaleFactor) { Q_UNUSED(scaleFactor); int width = handleWidth; HObject ho_Rectangle; DispRectangle1(winID, row1, col1, row2, col2); //DispRectangle2(winID, row1, col1, 0, width, width); //DispRectangle2(winID, row1, col2, 0, width, width); //DispRectangle2(winID, row2, col2, 0, width, width); //DispRectangle2(winID, row2, col1, 0, width, width); //DispRectangle2(winID, midR, midC, 0, width, width); //GenRectangle2ContourXld(&ho_Rectangle, row1, col1, 0, row2, col2); //DispObj(ho_Rectangle, winID); GenRectangle2ContourXld(&ho_Rectangle, row1, col1, 0, width, width); DispObj(ho_Rectangle, winID); GenRectangle2ContourXld(&ho_Rectangle, row1, col2, 0, width, width); DispObj(ho_Rectangle, winID); GenRectangle2ContourXld(&ho_Rectangle, row2, col2, 0, width, width); DispObj(ho_Rectangle, winID); GenRectangle2ContourXld(&ho_Rectangle, row2, col1, 0, width, width); DispObj(ho_Rectangle, winID); GenRectangle2ContourXld(&ho_Rectangle, midR, midC, 0, width, width); DispObj(ho_Rectangle, winID); SetTposition(winID, row1, col1 + width); WriteString(winID, HTuple(m_strTitle.toStdString().c_str())); } double ROIRect1::distToClosestHandle(double x, double y) { double max = 10000; double val[5]; midR = ((row2 - row1) / 2) + row1; midC = ((col2 - col1) / 2) + col1; val[0] = HMisc::DistancePp(y, x, row1, col1); // upper left val[1] = HMisc::DistancePp(y, x, row1, col2); // upper right val[2] = HMisc::DistancePp(y, x, row2, col2); // lower right val[3] = HMisc::DistancePp(y, x, row2, col1); // lower left val[4] = HMisc::DistancePp(y, x, midR, midC); // midpoint for (int i = 0; i < numHandles; i++) { if (val[i] < max) { max = val[i]; activeHandleIdx = i; } }// end of for return val[activeHandleIdx]; } void ROIRect1::displayActive(HTuple winID, double scaleFactor) { Q_UNUSED(scaleFactor); HObject ho_Rectangle; int width = handleWidth; switch (activeHandleIdx) { case 0: //DispRectangle2(winID, row1, col1, 0, width, width); GenRectangle2ContourXld(&ho_Rectangle, row1, col1, 0, width, width); DispObj(ho_Rectangle, winID); break; case 1: //DispRectangle2(winID, row1, col2, 0, width, width); GenRectangle2ContourXld(&ho_Rectangle, row1, col2, 0, width, width); DispObj(ho_Rectangle, winID); break; case 2: //DispRectangle2(winID, row2, col2, 0, width, width); GenRectangle2ContourXld(&ho_Rectangle, row2, col2, 0, width, width); DispObj(ho_Rectangle, winID); break; case 3: //DispRectangle2(winID, row2, col1, 0, width, width); GenRectangle2ContourXld(&ho_Rectangle, row2, col1, 0, width, width); DispObj(ho_Rectangle, winID); break; case 4: //DispRectangle2(winID, midR, midC, 0, width, width); GenRectangle2ContourXld(&ho_Rectangle, midR, midC, 0, width, width); DispObj(ho_Rectangle, winID); break; } } void ROIRect1::moveByHandle(double newX, double newY) { double len1, len2; double tmp; switch (activeHandleIdx) { case 0: // upper left row1 = newY; col1 = newX; break; case 1: // upper right row1 = newY; col2 = newX; break; case 2: // lower right row2 = newY; col2 = newX; break; case 3: // lower left row2 = newY; col1 = newX; break; case 4: // midpoint len1 = ((row2 - row1) / 2); len2 = ((col2 - col1) / 2); row1 = newY - len1; row2 = newY + len1; col1 = newX - len2; col2 = newX + len2; break; } if (row2 <= row1) { tmp = row1; row1 = row2; row2 = tmp; } if (col2 <= col1) { tmp = col1; col1 = col2; col2 = tmp; } midR = ((row2 - row1) / 2) + row1; midC = ((col2 - col1) / 2) + col1; } QCursor ROIRect1::showByHandle() { QCursor cursor; switch (activeHandleIdx) { case 0: // 鼠标在圆上的一点时 cursor = Qt::SizeAllCursor; break; case 1: // 鼠标在圆心上时 cursor = Qt::SizeBDiagCursor; break; } return cursor; } HRegion ROIRect1::getRegion() { HRegion region; region.GenRectangle1(row1, col1, row2, col2); return region; } HTuple ROIRect1::getROIData() { HTuple dat; dat.Clear(); dat[0] = row1; dat[1] = col1; dat[2] = row2; dat[3] = col2; return dat; } void ROIRect1::setROIData(HTuple dat) { if (dat.TupleLength() == 4) { row1 = dat[0]; col1 = dat[1]; row2 = dat[2]; col2 = dat[3]; midR = ((row2 - row1) / 2) + row1; midC = ((col2 - col1) / 2) + col1; } } void ROIRect1::save(QDataStream &dataStream) { ROI::save(dataStream); int paranum;//参数数量 paranum = 7; dataStream << paranum;//先保存参数数量 dataStream << (int)1 << row1; dataStream << (int)2 << col1; dataStream << (int)3 << row2; dataStream << (int)4 << col2; dataStream << (int)5 << midR; dataStream << (int)6 << midC; dataStream << (int)7 << m_strTitle; } void ROIRect1::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 >> row1; break; case 2: dataStream >> col1; break; case 3: dataStream >> row2; break; case 4: dataStream >> col2; break; case 5: dataStream >> midR; break; case 6: dataStream >> midC; break; case 7: dataStream >> m_strTitle; break; default: { qWarning() << "配置文档错误!"; } break; } } } void ROIRect1::setTitle(QString title) { m_strTitle = title; }