ROIRectangle1.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /// (simple) rectangularly shaped ROI. ROIRectangle1 inherits
  9. /// from the base class ROI and implements (besides other auxiliary
  10. /// methods) all virtual methods defined in ROI.cs.
  11. /// Since a simple rectangle is defined by two data points, by the upper
  12. /// left corner and the lower right corner, we use four values (row1/col1)
  13. /// and (row2/col2) as class members to hold these positions at
  14. /// any time of the program. The four corners of the rectangle can be taken
  15. /// as handles, which the user can use to manipulate the size of the ROI.
  16. /// Furthermore, we define a midpoint as an additional handle, with which
  17. /// the user can grab and drag the ROI. Therefore, we declare NumHandles
  18. /// to be 5 and set the activeHandle to be 0, which will be the upper left
  19. /// corner of our ROI.
  20. /// </summary>
  21. [Serializable]
  22. public class ROIRectangle1 : ROI
  23. {
  24. [XmlElement(ElementName = "Row1")]
  25. public double Row1
  26. {
  27. get { return this.row1; }
  28. set { this.row1 = value; }
  29. }
  30. [XmlElement(ElementName = "Column1")]
  31. public double Column1
  32. {
  33. get { return this.col1; }
  34. set { this.col1 = value; }
  35. }
  36. [XmlElement(ElementName = "Row2")]
  37. public double Row2
  38. {
  39. get { return this.row2; }
  40. set { this.row2 = value; }
  41. }
  42. [XmlElement(ElementName = "Column2")]
  43. public double Column2
  44. {
  45. get { return this.col2; }
  46. set { this.col2 = value; }
  47. }
  48. private string color = "blue";
  49. private double row1, col1; // upper left
  50. private double row2, col2; // lower right
  51. private double midR, midC; // midpoint
  52. /// <summary>Constructor</summary>
  53. public ROIRectangle1()
  54. {
  55. NumHandles = 9; // 4 corner points + midpoint
  56. activeHandleIdx = 4;
  57. }
  58. public ROIRectangle1(double row1, double col1, double row2, double col2)
  59. {
  60. createRectangle1(row1, col1, row2, col2);
  61. }
  62. public override void createRectangle1(double row1, double col1, double row2, double col2)
  63. {
  64. base.createRectangle1(row1, col1, row2, col2);
  65. this.row1 = row1;
  66. this.col1 = col1;
  67. this.row2 = row2;
  68. this.col2 = col2;
  69. midR = (this.row1 + this.row2) / 2.0;
  70. midC = (this.col1 + this.col2) / 2.0;
  71. }
  72. public override void createInitRectangle1(double imageHeight)
  73. {
  74. double size = 0;
  75. if (imageHeight < 300) size = 10;
  76. else if (imageHeight < 600) size = 20;
  77. else if (imageHeight < 900) size = 30;
  78. else if (imageHeight < 1200) size = 40;
  79. else if (imageHeight < 1500) size = 50;
  80. else if (imageHeight < 1800) size = 60;
  81. else if (imageHeight < 2100) size = 70;
  82. else if (imageHeight < 2400) size = 80;
  83. else if (imageHeight < 2700) size = 90;
  84. else if (imageHeight < 3000) size = 100;
  85. else if (imageHeight < 3300) size = 110;
  86. else if (imageHeight < 3600) size = 120;
  87. else if (imageHeight < 3900) size = 130;
  88. else if (imageHeight < 4200) size = 140;
  89. else if (imageHeight < 4500) size = 150;
  90. else if (imageHeight < 4800) size = 160;
  91. else if (imageHeight < 5100) size = 170;
  92. else size = 180;
  93. double length1 = size * 3;
  94. double length2 = size * 4;
  95. base.createRectangle1(row1, col1, row1 + length1, col1+ length2);
  96. this.row1 = row1;
  97. this.col1 = col1;
  98. this.row2 = row1 + length1;
  99. this.col2 = col1 + length2;
  100. midR = (this.row1 + this.row2) / 2.0;
  101. midC = (this.col1 + this.col2) / 2.0;
  102. }
  103. /// <summary>Creates a new ROI instance at the mouse position</summary>
  104. /// <param name="midX">
  105. /// x (=column) coordinate for interactive ROI
  106. /// </param>
  107. /// <param name="midY">
  108. /// y (=row) coordinate for interactive ROI
  109. /// </param>
  110. public override void createROI(double midX, double midY)
  111. {
  112. midR = midY;
  113. midC = midX;
  114. row1 = midR - 25;
  115. col1 = midC - 25;
  116. row2 = midR + 25;
  117. col2 = midC + 25;
  118. }
  119. /// <summary>Paints the ROI into the supplied window</summary>
  120. /// <param name="window">HALCON window</param>
  121. public override void draw(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
  122. {
  123. double littleRecSize = 0;
  124. if (imageHeight < 300) littleRecSize = 1;
  125. else if (imageHeight < 600) littleRecSize = 2;
  126. else if (imageHeight < 900) littleRecSize = 3;
  127. else if (imageHeight < 1200) littleRecSize = 4;
  128. else if (imageHeight < 1500) littleRecSize = 5;
  129. else if (imageHeight < 1800) littleRecSize = 6;
  130. else if (imageHeight < 2100) littleRecSize = 7;
  131. else if (imageHeight < 2400) littleRecSize = 8;
  132. else if (imageHeight < 2700) littleRecSize = 9;
  133. else if (imageHeight < 3000) littleRecSize = 10;
  134. else if (imageHeight < 3300) littleRecSize = 11;
  135. else if (imageHeight < 3600) littleRecSize = 12;
  136. else if (imageHeight < 3900) littleRecSize = 13;
  137. else if (imageHeight < 4200) littleRecSize = 14;
  138. else if (imageHeight < 4500) littleRecSize = 15;
  139. else if (imageHeight < 4800) littleRecSize = 16;
  140. else if (imageHeight < 5100) littleRecSize = 17;
  141. else littleRecSize = 18;
  142. if (littleRecSize % 2 != 0)
  143. littleRecSize++;
  144. HOperatorSet.SetDraw(window,"margin");
  145. window.DispRectangle1(row1, col1, row2, col2);
  146. HOperatorSet.SetDraw(window, "fill");
  147. window.DispRectangle2(row1, col1, 0, littleRecSize, littleRecSize);
  148. window.DispRectangle2(row1, col2, 0, littleRecSize, littleRecSize);
  149. window.DispRectangle2(row2, col2, 0, littleRecSize, littleRecSize);
  150. window.DispRectangle2(row2, col1, 0, littleRecSize, littleRecSize);
  151. window.DispRectangle2(midR, midC, 0, littleRecSize, littleRecSize);
  152. window.DispRectangle2((row1 + row2) / 2, col1, 0, littleRecSize, littleRecSize);
  153. window.DispRectangle2((row1 + row2) / 2, col2, 0, littleRecSize, littleRecSize);
  154. window.DispRectangle2(row1, (col1 + col2) / 2, 0, littleRecSize, littleRecSize);
  155. window.DispRectangle2(row2, (col1 + col2) / 2, 0, littleRecSize, littleRecSize);
  156. }
  157. /// <summary>
  158. /// Returns the distance of the ROI handle being
  159. /// closest to the image point(x,y)
  160. /// </summary>
  161. /// <param name="x">x (=column) coordinate</param>
  162. /// <param name="y">y (=row) coordinate</param>
  163. /// <returns>
  164. /// Distance of the closest ROI handle.
  165. /// </returns>
  166. public override double distToClosestHandle(double x, double y)
  167. {
  168. double max = 10000;
  169. double[] val = new double[NumHandles];
  170. midR = ((row2 - row1) / 2) + row1;
  171. midC = ((col2 - col1) / 2) + col1;
  172. val[0] = HMisc.DistancePp(y, x, row1, col1); // upper left
  173. val[1] = HMisc.DistancePp(y, x, row1, col2); // upper right
  174. val[2] = HMisc.DistancePp(y, x, row2, col2); // lower right
  175. val[3] = HMisc.DistancePp(y, x, row2, col1); // lower left
  176. val[4] = HMisc.DistancePp(y, x, midR, midC); // midpoint
  177. val[5] = HMisc.DistancePp(y, x, (row1 + row2) / 2, col1);
  178. val[6] = HMisc.DistancePp(y, x, (row1 + row2) / 2, col2);
  179. val[7] = HMisc.DistancePp(y, x, row1, (col1 + col2) / 2);
  180. val[8] = HMisc.DistancePp(y, x, row2, (col1 + col2) / 2);
  181. for (int i = 0; i < NumHandles; i++)
  182. {
  183. if (val[i] < max)
  184. {
  185. max = val[i];
  186. activeHandleIdx = i;
  187. }
  188. }// end of for
  189. return val[activeHandleIdx];
  190. }
  191. /// <summary>
  192. /// Paints the active handle of the ROI object into the supplied window
  193. /// </summary>
  194. /// <param name="window">HALCON window</param>
  195. public override void displayActive(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
  196. {
  197. double littleRecSize = 0;
  198. if (imageHeight < 300) littleRecSize = 1;
  199. else if (imageHeight < 600) littleRecSize = 2;
  200. else if (imageHeight < 900) littleRecSize = 3;
  201. else if (imageHeight < 1200) littleRecSize = 4;
  202. else if (imageHeight < 1500) littleRecSize = 5;
  203. else if (imageHeight < 1800) littleRecSize = 6;
  204. else if (imageHeight < 2100) littleRecSize = 7;
  205. else if (imageHeight < 2400) littleRecSize = 8;
  206. else if (imageHeight < 2700) littleRecSize = 9;
  207. else if (imageHeight < 3000) littleRecSize = 10;
  208. else if (imageHeight < 3300) littleRecSize = 11;
  209. else if (imageHeight < 3600) littleRecSize = 12;
  210. else if (imageHeight < 3900) littleRecSize = 13;
  211. else if (imageHeight < 4200) littleRecSize = 14;
  212. else if (imageHeight < 4500) littleRecSize = 15;
  213. else if (imageHeight < 4800) littleRecSize = 16;
  214. else if (imageHeight < 5100) littleRecSize = 17;
  215. else littleRecSize = 18;
  216. if (littleRecSize % 2 != 0)
  217. littleRecSize++;
  218. switch (activeHandleIdx)
  219. {
  220. case 0:
  221. window.DispRectangle2(row1, col1, 0, littleRecSize, littleRecSize);
  222. break;
  223. case 1:
  224. window.DispRectangle2(row1, col2, 0, littleRecSize, littleRecSize);
  225. break;
  226. case 2:
  227. window.DispRectangle2(row2, col2, 0, littleRecSize, littleRecSize);
  228. break;
  229. case 3:
  230. window.DispRectangle2(row2, col1, 0, littleRecSize, littleRecSize);
  231. break;
  232. case 4:
  233. window.DispRectangle2(midR, midC, 0, littleRecSize, littleRecSize);
  234. break;
  235. case 5:
  236. window.DispRectangle2((row1 + row2) / 2, col1, 0, littleRecSize, littleRecSize);
  237. break;
  238. case 6:
  239. window.DispRectangle2((row1 + row2) / 2, col2, 0, littleRecSize, littleRecSize);
  240. break;
  241. case 7:
  242. window.DispRectangle2(row1, (col1 + col2) / 2, 0, littleRecSize, littleRecSize);
  243. break;
  244. case 8:
  245. window.DispRectangle2(row2, (col1 + col2) / 2, 0, littleRecSize, littleRecSize);
  246. break;
  247. }
  248. }
  249. /// <summary>Gets the HALCON region described by the ROI</summary>
  250. public override HRegion getRegion()
  251. {
  252. HRegion region = new HRegion();
  253. region.GenRectangle1(row1, col1, row2, col2);
  254. return region;
  255. }
  256. /// <summary>
  257. /// Gets the model information described by
  258. /// the interactive ROI
  259. /// </summary>
  260. public override HTuple getModelData()
  261. {
  262. return new HTuple(new double[] { row1, col1, row2, col2 });
  263. }
  264. /// <summary>
  265. /// Recalculates the shape of the ROI instance. Translation is
  266. /// performed at the active handle of the ROI object
  267. /// for the image coordinate (x,y)
  268. /// </summary>
  269. /// <param name="newX">x mouse coordinate</param>
  270. /// <param name="newY">y mouse coordinate</param>
  271. public override void moveByHandle(double newX, double newY,HWindowControl window)
  272. {
  273. double len1, len2;
  274. double tmp;
  275. switch (activeHandleIdx)
  276. {
  277. case 0: // upper left
  278. row1 = newY;
  279. col1 = newX;
  280. window.Cursor = System.Windows.Forms.Cursors.SizeNWSE ;
  281. break;
  282. case 1: // upper right
  283. row1 = newY;
  284. col2 = newX;
  285. window.Cursor = System.Windows.Forms.Cursors.SizeNESW ;
  286. break;
  287. case 2: // lower right
  288. row2 = newY;
  289. col2 = newX;
  290. window.Cursor = System.Windows.Forms.Cursors.SizeNWSE ;
  291. break;
  292. case 3: // lower left
  293. row2 = newY;
  294. col1 = newX;
  295. window.Cursor = System.Windows.Forms.Cursors.SizeNESW;
  296. break;
  297. case 4: // midpoint
  298. len1 = ((row2 - row1) / 2);
  299. len2 = ((col2 - col1) / 2);
  300. row1 = newY - len1;
  301. row2 = newY + len1;
  302. col1 = newX - len2;
  303. col2 = newX + len2;
  304. window.Cursor = System.Windows.Forms.Cursors.SizeAll ;
  305. break;
  306. case 5: // upper right
  307. col1 = newX;
  308. window.Cursor = System.Windows.Forms.Cursors.SizeWE ;
  309. break;
  310. case 6: // lower right
  311. col2 = newX;
  312. window.Cursor = System.Windows.Forms.Cursors.SizeWE ;
  313. break;
  314. case 7: // lower left
  315. row1 = newY;
  316. window.Cursor = System.Windows.Forms.Cursors.SizeNS ;
  317. break;
  318. case 8: // midpoint
  319. row2 = newY;
  320. window.Cursor = System.Windows.Forms.Cursors.SizeNS ;
  321. break;
  322. }
  323. if (row2 <= row1)
  324. {
  325. tmp = row1;
  326. row1 = row2;
  327. row2 = tmp;
  328. }
  329. if (col2 <= col1)
  330. {
  331. tmp = col1;
  332. col1 = col2;
  333. col2 = tmp;
  334. }
  335. midR = ((row2 - row1) / 2) + row1;
  336. midC = ((col2 - col1) / 2) + col1;
  337. }//end of method
  338. }//end of class
  339. }//end of namespace