ROICircle.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /// circular ROI. ROICircle 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 ROICircle : ROI
  14. {
  15. [XmlElement(ElementName = "Row")]
  16. public double Row
  17. {
  18. get { return this.midR; }
  19. set { this.midR = value; }
  20. }
  21. [XmlElement(ElementName = "Column")]
  22. public double Column
  23. {
  24. get { return this.midC; }
  25. set { this.midC = value; }
  26. }
  27. [XmlElement(ElementName = "Radius")]
  28. public double Radius
  29. {
  30. get { return this.radius; }
  31. set { this.radius = value; }
  32. }
  33. private double radius;
  34. private double row1, col1; // first handle
  35. private double midR, midC; // second handle
  36. public ROICircle()
  37. {
  38. NumHandles = 2; // one at corner of circle + midpoint
  39. activeHandleIdx = 1;
  40. }
  41. public ROICircle(double row, double col, double radius)
  42. {
  43. createCircle(row, col, radius);
  44. }
  45. public override void createCircle(double row, double col, double radius)
  46. {
  47. base.createCircle(row, col, radius);
  48. midR = row;
  49. midC = col;
  50. this.radius = radius;
  51. row1 = midR;
  52. col1 = midC + radius;
  53. }
  54. /// <summary>Creates a new ROI instance at the mouse position</summary>
  55. public override void createROI(double midX, double midY)
  56. {
  57. midR = midY;
  58. midC = midX;
  59. radius = 100;
  60. row1 = midR;
  61. col1 = midC + radius;
  62. }
  63. /// <summary>Paints the ROI into the supplied window</summary>
  64. /// <param name="window">HALCON window</param>
  65. public override void draw(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
  66. {
  67. HOperatorSet.SetDraw(window, "margin");
  68. window.DispCircle(midR, midC, radius);
  69. double littleRecSize = 0;
  70. if (imageHeight < 300) littleRecSize = 1;
  71. else if (imageHeight < 600) littleRecSize = 2;
  72. else if (imageHeight < 900) littleRecSize = 3;
  73. else if (imageHeight < 1200) littleRecSize = 4;
  74. else if (imageHeight < 1500) littleRecSize = 5;
  75. else if (imageHeight < 1800) littleRecSize = 6;
  76. else if (imageHeight < 2100) littleRecSize = 7;
  77. else if (imageHeight < 2400) littleRecSize = 8;
  78. else if (imageHeight < 2700) littleRecSize = 9;
  79. else if (imageHeight < 3000) littleRecSize = 10;
  80. else if (imageHeight < 3300) littleRecSize = 11;
  81. else if (imageHeight < 3600) littleRecSize = 12;
  82. else if (imageHeight < 3900) littleRecSize = 13;
  83. else if (imageHeight < 4200) littleRecSize = 14;
  84. else if (imageHeight < 4500) littleRecSize = 15;
  85. else if (imageHeight < 4800) littleRecSize = 16;
  86. else if (imageHeight < 5100) littleRecSize = 17;
  87. else littleRecSize = 18;
  88. if (littleRecSize % 2 != 0)
  89. littleRecSize++;
  90. HOperatorSet.SetDraw(window, "fill");
  91. window.DispRectangle2(row1, col1, 0, littleRecSize, littleRecSize);
  92. window.DispRectangle2(midR, midC, 0, littleRecSize, littleRecSize);
  93. }
  94. /// <summary>
  95. /// Returns the distance of the ROI handle being
  96. /// closest to the image point(x,y)
  97. /// </summary>
  98. public override double distToClosestHandle(double x, double y)
  99. {
  100. double max = 10000;
  101. double[] val = new double[NumHandles];
  102. val[0] = HMisc.DistancePp(y, x, row1, col1); // border handle
  103. val[1] = HMisc.DistancePp(y, x, midR, midC); // midpoint
  104. for (int i = 0; i < NumHandles; i++)
  105. {
  106. if (val[i] < max)
  107. {
  108. max = val[i];
  109. activeHandleIdx = i;
  110. }
  111. }// end of for
  112. return val[activeHandleIdx];
  113. }
  114. /// <summary>
  115. /// Paints the active handle of the ROI object into the supplied window
  116. /// </summary>
  117. public override void displayActive(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
  118. {
  119. double littleRecSize = 0;
  120. if (imageHeight < 300) littleRecSize = 1;
  121. else if (imageHeight < 600) littleRecSize = 2;
  122. else if (imageHeight < 900) littleRecSize = 3;
  123. else if (imageHeight < 1200) littleRecSize = 4;
  124. else if (imageHeight < 1500) littleRecSize = 5;
  125. else if (imageHeight < 1800) littleRecSize = 6;
  126. else if (imageHeight < 2100) littleRecSize = 7;
  127. else if (imageHeight < 2400) littleRecSize = 8;
  128. else if (imageHeight < 2700) littleRecSize = 9;
  129. else if (imageHeight < 3000) littleRecSize = 10;
  130. else if (imageHeight < 3300) littleRecSize = 11;
  131. else if (imageHeight < 3600) littleRecSize = 12;
  132. else if (imageHeight < 3900) littleRecSize = 13;
  133. else if (imageHeight < 4200) littleRecSize = 14;
  134. else if (imageHeight < 4500) littleRecSize = 15;
  135. else if (imageHeight < 4800) littleRecSize = 16;
  136. else if (imageHeight < 5100) littleRecSize = 17;
  137. else littleRecSize = 18;
  138. if (littleRecSize % 2 != 0)
  139. littleRecSize++;
  140. //HOperatorSet.SetDraw(window, "margin");
  141. HOperatorSet.SetDraw(window, "fill");
  142. switch (activeHandleIdx)
  143. {
  144. case 0:
  145. window.DispRectangle2(row1, col1, 0, littleRecSize, littleRecSize);
  146. break;
  147. case 1:
  148. window.DispRectangle2(midR, midC, 0, littleRecSize, littleRecSize);
  149. break;
  150. }
  151. }
  152. /// <summary>Gets the HALCON region described by the ROI</summary>
  153. public override HRegion getRegion()
  154. {
  155. HRegion region = new HRegion();
  156. region.GenCircle(midR, midC, radius);
  157. return region;
  158. }
  159. public override double getDistanceFromStartPoint(double row, double col)
  160. {
  161. double sRow = midR; // assumption: we have an angle starting at 0.0
  162. double sCol = midC + 1 * radius;
  163. double angle = HMisc.AngleLl(midR, midC, sRow, sCol, midR, midC, row, col);
  164. if (angle < 0)
  165. angle += 2 * Math.PI;
  166. return (radius * angle);
  167. }
  168. /// <summary>
  169. /// Gets the model information described by
  170. /// the ROI
  171. /// </summary>
  172. public override HTuple getModelData()
  173. {
  174. return new HTuple(new double[] { midR, midC, radius });
  175. }
  176. /// <summary>
  177. /// Recalculates the shape of the ROI. Translation is
  178. /// performed at the active handle of the ROI object
  179. /// for the image coordinate (x,y)
  180. /// </summary>
  181. public override void moveByHandle(double newX, double newY, HWindowControl window)
  182. {
  183. HTuple distance;
  184. double shiftX, shiftY;
  185. switch (activeHandleIdx)
  186. {
  187. case 0: // handle at circle border
  188. row1 = newY;
  189. col1 = newX;
  190. HOperatorSet.DistancePp(new HTuple(row1), new HTuple(col1),
  191. new HTuple(midR), new HTuple(midC),
  192. out distance);
  193. radius = distance[0].D;
  194. window.Cursor = System.Windows.Forms.Cursors.Hand ;
  195. break;
  196. case 1: // midpoint
  197. shiftY = midR - newY;
  198. shiftX = midC - newX;
  199. midR = newY;
  200. midC = newX;
  201. row1 -= shiftY;
  202. col1 -= shiftX;
  203. window.Cursor = System.Windows.Forms.Cursors.SizeAll ;
  204. break;
  205. }
  206. }
  207. }//end of class
  208. }//end of namespace