ROILine.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using HalconDotNet;
  3. using System.Xml.Serialization;
  4. namespace ViewWindow.Model
  5. {
  6. /// <summary>
  7. /// This class demonstrates one of the possible implementations for a
  8. /// linear ROI. ROILine inherits from the base class ROI and
  9. /// implements (besides other auxiliary methods) all virtual methods
  10. /// defined in ROI.cs.
  11. /// </summary>
  12. [Serializable]
  13. public class ROILine : ROI
  14. {
  15. [XmlElement(ElementName = "RowBegin")]
  16. public double RowBegin
  17. {
  18. get { return this.row1; }
  19. set { this.row1 = value; }
  20. }
  21. [XmlElement(ElementName = "ColumnBegin")]
  22. public double ColumnBegin
  23. {
  24. get { return this.col1; }
  25. set { this.col1 = value; }
  26. }
  27. [XmlElement(ElementName = "RowEnd")]
  28. public double RowEnd
  29. {
  30. get { return this.row2; }
  31. set { this.row2 = value; }
  32. }
  33. [XmlElement(ElementName = "ColumnEnd")]
  34. public double ColumnEnd
  35. {
  36. get { return this.col2; }
  37. set { this.col2 = value; }
  38. }
  39. private double row1, col1; // first end point of line
  40. private double row2, col2; // second end point of line
  41. private double midR, midC; // midPoint of line
  42. private HObject arrowHandleXLD;
  43. public ROILine()
  44. {
  45. NumHandles = 3; // two end points of line
  46. activeHandleIdx = 2;
  47. arrowHandleXLD = new HXLDCont();
  48. arrowHandleXLD.GenEmptyObj();
  49. }
  50. public ROILine(double beginRow, double beginCol, double endRow, double endCol)
  51. {
  52. createLine(beginRow, beginCol, endRow, endCol);
  53. }
  54. public override void createLine(double beginRow, double beginCol, double endRow, double endCol)
  55. {
  56. base.createLine(beginRow, beginCol, endRow, endCol);
  57. row1 = beginRow;
  58. col1 = beginCol;
  59. row2 = endRow;
  60. col2 = endCol;
  61. midR = (row1 + row2) / 2.0;
  62. midC = (col1 + col2) / 2.0;
  63. updateArrowHandle();
  64. }
  65. /// <summary>Creates a new ROI instance at the mouse position.</summary>
  66. public override void createROI(double midX, double midY)
  67. {
  68. midR = midY;
  69. midC = midX;
  70. row1 = midR;
  71. col1 = midC - 50;
  72. row2 = midR;
  73. col2 = midC + 50;
  74. updateArrowHandle();
  75. }
  76. /// <summary>Paints the ROI into the supplied window.</summary>
  77. public override void draw(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
  78. {
  79. window.DispLine(row1, col1, row2, col2);
  80. window.DispRectangle2(row1, col1, 0, 8, 8);
  81. window.DispObj(arrowHandleXLD); //window.DispRectangle2( row2, col2, 0, 25, 25);
  82. window.DispRectangle2(midR, midC, 0, 8, 8);
  83. }
  84. /// <summary>
  85. /// Returns the distance of the ROI handle being
  86. /// closest to the image point(x,y).
  87. /// </summary>
  88. public override double distToClosestHandle(double x, double y)
  89. {
  90. double max = 10000;
  91. double [] val = new double[NumHandles];
  92. val[0] = HMisc.DistancePp(y, x, row1, col1); // upper left
  93. val[1] = HMisc.DistancePp(y, x, row2, col2); // upper right
  94. val[2] = HMisc.DistancePp(y, x, midR, midC); // midpoint
  95. for (int i=0; i < NumHandles; i++)
  96. {
  97. if (val[i] < max)
  98. {
  99. max = val[i];
  100. activeHandleIdx = i;
  101. }
  102. }// end of for
  103. return val[activeHandleIdx];
  104. }
  105. /// <summary>
  106. /// Paints the active handle of the ROI object into the supplied window.
  107. /// </summary>
  108. public override void displayActive(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
  109. {
  110. switch (activeHandleIdx)
  111. {
  112. case 0:
  113. window.DispRectangle2(row1, col1, 0, 8, 8);
  114. break;
  115. case 1:
  116. window.DispObj(arrowHandleXLD); //window.DispRectangle2(row2, col2, 0, 25, 25);
  117. break;
  118. case 2:
  119. window.DispRectangle2(midR, midC, 0, 8, 8);
  120. break;
  121. }
  122. }
  123. /// <summary>Gets the HALCON region described by the ROI.</summary>
  124. public override HRegion getRegion()
  125. {
  126. HRegion region = new HRegion();
  127. region.GenRegionLine(row1, col1, row2, col2);
  128. return region;
  129. }
  130. public override double getDistanceFromStartPoint(double row, double col)
  131. {
  132. double distance = HMisc.DistancePp(row, col, row1, col1);
  133. return distance;
  134. }
  135. /// <summary>
  136. /// Gets the model information described by
  137. /// the ROI.
  138. /// </summary>
  139. public override HTuple getModelData()
  140. {
  141. return new HTuple(new double[] { row1, col1, row2, col2 });
  142. }
  143. /// <summary>
  144. /// Recalculates the shape of the ROI. Translation is
  145. /// performed at the active handle of the ROI object
  146. /// for the image coordinate (x,y).
  147. /// </summary>
  148. public override void moveByHandle(double newX, double newY, HWindowControl window)
  149. {
  150. double lenR, lenC;
  151. switch (activeHandleIdx)
  152. {
  153. case 0: // first end point
  154. row1 = newY;
  155. col1 = newX;
  156. midR = (row1 + row2) / 2;
  157. midC = (col1 + col2) / 2;
  158. break;
  159. case 1: // last end point
  160. row2 = newY;
  161. col2 = newX;
  162. midR = (row1 + row2) / 2;
  163. midC = (col1 + col2) / 2;
  164. break;
  165. case 2: // midpoint
  166. lenR = row1 - midR;
  167. lenC = col1 - midC;
  168. midR = newY;
  169. midC = newX;
  170. row1 = midR + lenR;
  171. col1 = midC + lenC;
  172. row2 = midR - lenR;
  173. col2 = midC - lenC;
  174. break;
  175. }
  176. updateArrowHandle();
  177. }
  178. /// <summary> Auxiliary method </summary>
  179. private void updateArrowHandle()
  180. {
  181. double length,dr,dc, halfHW;
  182. double rrow1, ccol1,rowP1, colP1, rowP2, colP2;
  183. double headLength = 16;
  184. double headWidth = 16;
  185. arrowHandleXLD.Dispose();
  186. arrowHandleXLD.GenEmptyObj();
  187. rrow1 = row1 + (row2 - row1) * 0.9;
  188. ccol1 = col1 + (col2 - col1) * 0.9;
  189. length = HMisc.DistancePp(rrow1, ccol1, row2, col2);
  190. if (length == 0)
  191. length = -1;
  192. dr = (row2 - rrow1) / length;
  193. dc = (col2 - ccol1) / length;
  194. halfHW = headWidth / 2.0;
  195. rowP1 = rrow1 + (length - headLength) * dr + halfHW * dc;
  196. rowP2 = rrow1 + (length - headLength) * dr - halfHW * dc;
  197. colP1 = ccol1 + (length - headLength) * dc - halfHW * dr;
  198. colP2 = ccol1 + (length - headLength) * dc + halfHW * dr;
  199. if (length == -1)
  200. HOperatorSet.GenContourPolygonXld(out arrowHandleXLD,rrow1, ccol1);
  201. else
  202. HOperatorSet.GenContourPolygonXld(out arrowHandleXLD, new HTuple(new double[] { rrow1, row2, rowP1, row2, rowP2, row2 }),
  203. new HTuple(new double[] { ccol1, col2, colP1, col2, colP2, col2 }));
  204. }
  205. }//end of class
  206. }//end of namespace