ROIPoint.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "ROIPoint.h"
  2. ROIPoint::ROIPoint()
  3. {
  4. using namespace HalconCpp;
  5. numHandles = 1;
  6. activeHandleIdx = 1;
  7. roiType = ROIType::Point;
  8. midRow = 0;
  9. midCol = 0;
  10. }
  11. void ROIPoint::createROI(double midX, double midY)
  12. {
  13. midRow = midY;
  14. midCol = midX;
  15. updateArrowHandle();
  16. }
  17. void ROIPoint::drawROI(HTuple winID, double scaleFactor)
  18. {
  19. Q_UNUSED(scaleFactor);
  20. int width=handleWidth;
  21. HObject ho_Rectangle;
  22. //DispRectangle2(winID, HTuple(midRow), HTuple(midCol), 0, width, width);
  23. GenRectangle2ContourXld(&ho_Rectangle, HTuple(midRow), HTuple(midCol), 0, width, width);
  24. DispObj(ho_Rectangle, winID);
  25. // if (arrowHandleXLD.IsInitialized()==false)
  26. {
  27. updateArrowHandle();
  28. }
  29. DispObj(arrowHandleXLD, winID); //window.DispRectangle2( row2, col2, 0, 5, 5);
  30. HObject ho_Cross;
  31. GenCrossContourXld(&ho_Cross, midRow, midCol, 40, 0);
  32. DispObj(ho_Cross, winID);
  33. //DispCircle(winID, midRow, midCol, 20);
  34. HObject ho_ContCircle;
  35. GenCircleContourXld(&ho_ContCircle, midRow, midCol, 20, 0, 6.28318, "positive", 1);
  36. DispObj(ho_ContCircle, winID);
  37. }
  38. double ROIPoint::distToClosestHandle(double x, double y)
  39. {
  40. double max = 10000;
  41. double val[3] = {0};
  42. val[0] = HMisc::DistancePp(y, x, midRow, midCol); // upper left
  43. for (int i = 0; i < numHandles; i++)
  44. {
  45. if (val[i] < max)
  46. {
  47. max = val[i];
  48. activeHandleIdx = i;
  49. }
  50. }// end of for
  51. return val[activeHandleIdx];
  52. }
  53. void ROIPoint::displayActive(HTuple winID, double scaleFactor)
  54. {
  55. Q_UNUSED(scaleFactor);
  56. HObject ho_Rectangle;
  57. int width=handleWidth;
  58. switch (activeHandleIdx)
  59. {
  60. case 0:
  61. //DispRectangle2(winID, HTuple(midRow), HTuple(midCol), 0, width / 2, width / 2 );
  62. GenRectangle2ContourXld(&ho_Rectangle, HTuple(midRow), HTuple(midCol), 0, width / 2, width / 2);
  63. DispObj(ho_Rectangle, winID);
  64. break;
  65. case 1:
  66. DispObj(arrowHandleXLD, winID); //window.DispRectangle2(row2, col2, 0, 5, 5);
  67. break;
  68. }
  69. }
  70. void ROIPoint::moveByHandle(double newX, double newY)
  71. {
  72. switch (activeHandleIdx)
  73. {
  74. case 0: // first end point
  75. midRow = newY;
  76. midCol = newX;
  77. break;
  78. }
  79. updateArrowHandle();
  80. }
  81. QCursor ROIPoint::showByHandle()
  82. {
  83. QCursor cursor;
  84. cursor = Qt::SizeBDiagCursor;
  85. return cursor;
  86. }
  87. HRegion ROIPoint::getRegion()
  88. {
  89. HRegion region;
  90. region.GenRectangle2((midRow), (midCol),0.0 , (handleWidth), (handleWidth));
  91. return region;
  92. }
  93. HTuple ROIPoint::getROIData()
  94. {
  95. HTuple dat;
  96. dat.Clear();
  97. dat[0] = midRow;
  98. dat[1] = midCol;
  99. return dat;
  100. }
  101. void ROIPoint::setROIData(HTuple dat)
  102. {
  103. if (dat.TupleLength() == 2)
  104. {
  105. midRow = dat[0];
  106. midCol = dat[1];
  107. }
  108. }
  109. void ROIPoint::save(QDataStream& dataStream)
  110. {
  111. ROI::save(dataStream);
  112. int paranum;//参数数量
  113. paranum = 3;
  114. dataStream << paranum;//先保存参数数量
  115. dataStream << (int)1 << midRow;
  116. dataStream << (int)2 << midCol;
  117. dataStream << (int)3 << m_strTitle;
  118. }
  119. void ROIPoint::load(QDataStream& dataStream)
  120. {
  121. ROI::load(dataStream);
  122. int paranum;//参数数量
  123. int para;
  124. dataStream >> paranum;//读取参数数量
  125. for (int i = 0; i < paranum; i++)
  126. {
  127. dataStream >> para;
  128. switch (para)
  129. {
  130. case 1: dataStream >> midRow; break;
  131. case 2: dataStream >> midCol; break;
  132. case 3: dataStream >> m_strTitle; break;
  133. default:
  134. {
  135. qWarning() << "配置文档错误!";
  136. }
  137. break;
  138. }
  139. }
  140. }
  141. void ROIPoint::updateArrowHandle()
  142. {
  143. using namespace HalconCpp;
  144. arrowHandleXLD.GenEmptyObj();
  145. //GenRectangle2(&arrowHandleXLD, HTuple(midRow),HTuple(midCol), 0, handleWidth, handleWidth);
  146. GenCrossContourXld(&arrowHandleXLD, HTuple(midRow), HTuple(midCol), handleWidth * 2, 0);
  147. }