ROINurbs.cs 13 KB

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