ROIRect1.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "ROIRect1.h"
  2. ROIRect1::ROIRect1()
  3. {
  4. numHandles = 5; // 4 corner points + midpoint
  5. activeHandleIdx = 4;
  6. roiType = ROIType::Rectangle1;
  7. midR = 0;
  8. midC = 0;
  9. row1 = 0;
  10. col1 = 0;
  11. row2 = 0;
  12. col2 = 0;
  13. }
  14. void ROIRect1::createROI(double midX, double midY)
  15. {
  16. midR = midY;
  17. midC = midX;
  18. row1 = midR - 50;
  19. col1 = midC - 50;
  20. row2 = midR + 50;
  21. col2 = midC + 50;
  22. }
  23. void ROIRect1::drawROI(HTuple winID, double scaleFactor)
  24. {
  25. Q_UNUSED(scaleFactor);
  26. int width = handleWidth;
  27. HObject ho_Rectangle;
  28. DispRectangle1(winID, row1, col1, row2, col2);
  29. //DispRectangle2(winID, row1, col1, 0, width, width);
  30. //DispRectangle2(winID, row1, col2, 0, width, width);
  31. //DispRectangle2(winID, row2, col2, 0, width, width);
  32. //DispRectangle2(winID, row2, col1, 0, width, width);
  33. //DispRectangle2(winID, midR, midC, 0, width, width);
  34. //GenRectangle2ContourXld(&ho_Rectangle, row1, col1, 0, row2, col2);
  35. //DispObj(ho_Rectangle, winID);
  36. GenRectangle2ContourXld(&ho_Rectangle, row1, col1, 0, width, width);
  37. DispObj(ho_Rectangle, winID);
  38. GenRectangle2ContourXld(&ho_Rectangle, row1, col2, 0, width, width);
  39. DispObj(ho_Rectangle, winID);
  40. GenRectangle2ContourXld(&ho_Rectangle, row2, col2, 0, width, width);
  41. DispObj(ho_Rectangle, winID);
  42. GenRectangle2ContourXld(&ho_Rectangle, row2, col1, 0, width, width);
  43. DispObj(ho_Rectangle, winID);
  44. GenRectangle2ContourXld(&ho_Rectangle, midR, midC, 0, width, width);
  45. DispObj(ho_Rectangle, winID);
  46. SetTposition(winID, row1, col1 + width);
  47. WriteString(winID, HTuple(m_strTitle.toStdString().c_str()));
  48. }
  49. double ROIRect1::distToClosestHandle(double x, double y)
  50. {
  51. double max = 10000;
  52. double val[5];
  53. midR = ((row2 - row1) / 2) + row1;
  54. midC = ((col2 - col1) / 2) + col1;
  55. val[0] = HMisc::DistancePp(y, x, row1, col1); // upper left
  56. val[1] = HMisc::DistancePp(y, x, row1, col2); // upper right
  57. val[2] = HMisc::DistancePp(y, x, row2, col2); // lower right
  58. val[3] = HMisc::DistancePp(y, x, row2, col1); // lower left
  59. val[4] = HMisc::DistancePp(y, x, midR, midC); // midpoint
  60. for (int i = 0; i < numHandles; i++)
  61. {
  62. if (val[i] < max)
  63. {
  64. max = val[i];
  65. activeHandleIdx = i;
  66. }
  67. }// end of for
  68. return val[activeHandleIdx];
  69. }
  70. void ROIRect1::displayActive(HTuple winID, double scaleFactor)
  71. {
  72. Q_UNUSED(scaleFactor);
  73. HObject ho_Rectangle;
  74. int width = handleWidth;
  75. switch (activeHandleIdx)
  76. {
  77. case 0:
  78. //DispRectangle2(winID, row1, col1, 0, width, width);
  79. GenRectangle2ContourXld(&ho_Rectangle, row1, col1, 0, width, width);
  80. DispObj(ho_Rectangle, winID);
  81. break;
  82. case 1:
  83. //DispRectangle2(winID, row1, col2, 0, width, width);
  84. GenRectangle2ContourXld(&ho_Rectangle, row1, col2, 0, width, width);
  85. DispObj(ho_Rectangle, winID);
  86. break;
  87. case 2:
  88. //DispRectangle2(winID, row2, col2, 0, width, width);
  89. GenRectangle2ContourXld(&ho_Rectangle, row2, col2, 0, width, width);
  90. DispObj(ho_Rectangle, winID);
  91. break;
  92. case 3:
  93. //DispRectangle2(winID, row2, col1, 0, width, width);
  94. GenRectangle2ContourXld(&ho_Rectangle, row2, col1, 0, width, width);
  95. DispObj(ho_Rectangle, winID);
  96. break;
  97. case 4:
  98. //DispRectangle2(winID, midR, midC, 0, width, width);
  99. GenRectangle2ContourXld(&ho_Rectangle, midR, midC, 0, width, width);
  100. DispObj(ho_Rectangle, winID);
  101. break;
  102. }
  103. }
  104. void ROIRect1::moveByHandle(double newX, double newY)
  105. {
  106. double len1, len2;
  107. double tmp;
  108. switch (activeHandleIdx)
  109. {
  110. case 0: // upper left
  111. row1 = newY;
  112. col1 = newX;
  113. break;
  114. case 1: // upper right
  115. row1 = newY;
  116. col2 = newX;
  117. break;
  118. case 2: // lower right
  119. row2 = newY;
  120. col2 = newX;
  121. break;
  122. case 3: // lower left
  123. row2 = newY;
  124. col1 = newX;
  125. break;
  126. case 4: // midpoint
  127. len1 = ((row2 - row1) / 2);
  128. len2 = ((col2 - col1) / 2);
  129. row1 = newY - len1;
  130. row2 = newY + len1;
  131. col1 = newX - len2;
  132. col2 = newX + len2;
  133. break;
  134. }
  135. if (row2 <= row1)
  136. {
  137. tmp = row1;
  138. row1 = row2;
  139. row2 = tmp;
  140. }
  141. if (col2 <= col1)
  142. {
  143. tmp = col1;
  144. col1 = col2;
  145. col2 = tmp;
  146. }
  147. midR = ((row2 - row1) / 2) + row1;
  148. midC = ((col2 - col1) / 2) + col1;
  149. }
  150. QCursor ROIRect1::showByHandle()
  151. {
  152. QCursor cursor;
  153. switch (activeHandleIdx)
  154. {
  155. case 0: // 鼠标在圆上的一点时
  156. cursor = Qt::SizeAllCursor;
  157. break;
  158. case 1: // 鼠标在圆心上时
  159. cursor = Qt::SizeBDiagCursor;
  160. break;
  161. }
  162. return cursor;
  163. }
  164. HRegion ROIRect1::getRegion()
  165. {
  166. HRegion region;
  167. region.GenRectangle1(row1, col1, row2, col2);
  168. return region;
  169. }
  170. HTuple ROIRect1::getROIData()
  171. {
  172. HTuple dat;
  173. dat.Clear();
  174. dat[0] = row1;
  175. dat[1] = col1;
  176. dat[2] = row2;
  177. dat[3] = col2;
  178. return dat;
  179. }
  180. void ROIRect1::setROIData(HTuple dat)
  181. {
  182. if (dat.TupleLength() == 4)
  183. {
  184. row1 = dat[0];
  185. col1 = dat[1];
  186. row2 = dat[2];
  187. col2 = dat[3];
  188. midR = ((row2 - row1) / 2) + row1;
  189. midC = ((col2 - col1) / 2) + col1;
  190. }
  191. }
  192. void ROIRect1::save(QDataStream &dataStream)
  193. {
  194. ROI::save(dataStream);
  195. int paranum;//参数数量
  196. paranum = 7;
  197. dataStream << paranum;//先保存参数数量
  198. dataStream << (int)1 << row1;
  199. dataStream << (int)2 << col1;
  200. dataStream << (int)3 << row2;
  201. dataStream << (int)4 << col2;
  202. dataStream << (int)5 << midR;
  203. dataStream << (int)6 << midC;
  204. dataStream << (int)7 << m_strTitle;
  205. }
  206. void ROIRect1::load(QDataStream &dataStream)
  207. {
  208. ROI::load(dataStream);
  209. int paranum;//参数数量
  210. int para;
  211. dataStream >> paranum;//读取参数数量
  212. for (int i = 0; i < paranum; i++)
  213. {
  214. dataStream >> para;
  215. switch (para)
  216. {
  217. case 1: dataStream >> row1; break;
  218. case 2: dataStream >> col1; break;
  219. case 3: dataStream >> row2; break;
  220. case 4: dataStream >> col2; break;
  221. case 5: dataStream >> midR; break;
  222. case 6: dataStream >> midC; break;
  223. case 7: dataStream >> m_strTitle; break;
  224. default:
  225. {
  226. qWarning() << "配置文档错误!";
  227. }
  228. break;
  229. }
  230. }
  231. }
  232. void ROIRect1::setTitle(QString title)
  233. {
  234. m_strTitle = title;
  235. }