ROIRectangle2.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using System;
  2. using HalconDotNet;
  3. using System.Xml.Serialization;
  4. using System.Windows.Forms;
  5. namespace ViewWindow.Model
  6. {
  7. /// <summary>
  8. /// This class demonstrates one of the possible implementations for a
  9. /// (simple) rectangularly shaped ROI. To create this rectangle we use
  10. /// a center point (midR, midC), an orientation 'phi' and the half
  11. /// edge lengths 'length1' and 'length2', similar to the HALCON
  12. /// operator gen_rectangle2().
  13. /// The class ROIRectangle2 inherits from the base class ROI and
  14. /// implements (besides other auxiliary methods) all virtual methods
  15. /// defined in ROI.cs.
  16. /// </summary>
  17. [Serializable]
  18. public class ROIRectangle2 : ROI
  19. {
  20. [XmlElement(ElementName = "Row")]
  21. public double Row
  22. {
  23. get { return this.midR; }
  24. set { this.midR = value; }
  25. }
  26. [XmlElement(ElementName = "Column")]
  27. public double Column
  28. {
  29. get { return this.midC; }
  30. set { this.midC = value; }
  31. }
  32. [XmlElement(ElementName = "Phi")]
  33. public double Phi
  34. {
  35. get { return this.phi; }
  36. set { this.phi = value; }
  37. }
  38. [XmlElement(ElementName = "Length1")]
  39. public double Lenth1
  40. {
  41. get { return this.length1; }
  42. set { this.length1 = value; }
  43. }
  44. [XmlElement(ElementName = "Length2")]
  45. public double Lenth2
  46. {
  47. get { return this.length2; }
  48. set { this.length2 = value; }
  49. }
  50. /// <summary>Half length of the rectangle side, perpendicular to phi</summary>
  51. private double length1;
  52. /// <summary>Half length of the rectangle side, in direction of phi</summary>
  53. private double length2;
  54. /// <summary>Row coordinate of midpoint of the rectangle</summary>
  55. private double midR;
  56. /// <summary>Column coordinate of midpoint of the rectangle</summary>
  57. private double midC;
  58. /// <summary>Orientation of rectangle defined in radians.</summary>
  59. private double phi;
  60. //auxiliary variables
  61. HTuple rowsInit;
  62. HTuple colsInit;
  63. public HTuple rows;
  64. public HTuple cols;
  65. HHomMat2D hom2D, tmp;
  66. /// <summary>Constructor</summary>
  67. public ROIRectangle2()
  68. {
  69. NumHandles = 10; // 4 corners + 1 midpoint + 1 rotationpoint
  70. activeHandleIdx = 4;
  71. }
  72. public ROIRectangle2(double row, double col, double phi, double length1, double length2)
  73. {
  74. createRectangle2(row, col, phi, length1, length2);
  75. }
  76. public override void createRectangle2(double row, double col, double phi, double length1, double length2)
  77. {
  78. base.createRectangle2(row, col, phi, length1, length2);
  79. this.midR = row;
  80. this.midC = col;
  81. this.length1 = length1;
  82. this.length2 = length2;
  83. this.phi = phi;
  84. rowsInit = new HTuple(new double[] {-1.0, -1.0, 1.0,
  85. 1.0, 0.0, 0.0 ,0,-1,0,1});
  86. colsInit = new HTuple(new double[] {-1.0, 1.0, 1.0,
  87. -1.0, 0.0, 1.8 ,-1,0,1,0});
  88. //order ul , ur, lr, ll, mp, arrowMidpoint
  89. hom2D = new HHomMat2D();
  90. tmp = new HHomMat2D();
  91. updateHandlePos();
  92. }
  93. public override void createInitRectangle2(double imageHeight)
  94. {
  95. double size = 0;
  96. if (imageHeight < 300) size = 10;
  97. else if (imageHeight < 600) size = 20;
  98. else if (imageHeight < 900) size = 30;
  99. else if (imageHeight < 1200) size = 40;
  100. else if (imageHeight < 1500) size = 50;
  101. else if (imageHeight < 1800) size = 60;
  102. else if (imageHeight < 2100) size = 70;
  103. else if (imageHeight < 2400) size = 80;
  104. else if (imageHeight < 2700) size = 90;
  105. else if (imageHeight < 3000) size = 100;
  106. else if (imageHeight < 3300) size = 110;
  107. else if (imageHeight < 3600) size = 120;
  108. else if (imageHeight < 3900) size = 130;
  109. else if (imageHeight < 4200) size = 140;
  110. else if (imageHeight < 4500) size = 150;
  111. else if (imageHeight < 4800) size = 160;
  112. else if (imageHeight < 5100) size = 170;
  113. else size = 180;
  114. double length1 = size * 3;
  115. double length2 = size * 4;
  116. base.createRectangle2(midR , midC , phi, length1, length2);
  117. this.midR = midR ;
  118. this.midC = midC ;
  119. this.length1 = length1;
  120. this.length2 = length2;
  121. this.phi = phi;
  122. rowsInit = new HTuple(new double[] {-1.0, -1.0, 1.0,
  123. 1.0, 0.0, 0.0 ,0,-1,0,1});
  124. colsInit = new HTuple(new double[] {-1.0, 1.0, 1.0,
  125. -1.0, 0.0, 1.8 ,-1,0,1,0});
  126. //order ul , ur, lr, ll, mp, arrowMidpoint
  127. hom2D = new HHomMat2D();
  128. tmp = new HHomMat2D();
  129. updateHandlePos();
  130. }
  131. /// <summary>Creates a new ROI instance at the mouse position</summary>
  132. /// <param name="midX">
  133. /// x (=column) coordinate for interactive ROI
  134. /// </param>
  135. /// <param name="midY">
  136. /// y (=row) coordinate for interactive ROI
  137. /// </param>
  138. public override void createROI(double midX, double midY)
  139. {
  140. midR = midY;
  141. midC = midX;
  142. length1 = 100;
  143. length2 = 50;
  144. phi = 0.0;
  145. rowsInit = new HTuple(new double[] {-1.0, -1.0, 1.0,
  146. 1.0, 0.0, 0.0 });
  147. colsInit = new HTuple(new double[] {-1.0, 1.0, 1.0,
  148. -1.0, 0.0, 1.8 });
  149. //order ul , ur, lr, ll, mp, arrowMidpoint
  150. hom2D = new HHomMat2D();
  151. tmp = new HHomMat2D();
  152. updateHandlePos();
  153. }
  154. /// <summary>Paints the ROI into the supplied window</summary>
  155. /// <param name="window">HALCON window</param>
  156. public override void draw(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
  157. {
  158. double littleRecSize = 0;
  159. if (imageHeight < 300) littleRecSize = 1;
  160. else if (imageHeight < 600) littleRecSize = 2;
  161. else if (imageHeight < 900) littleRecSize = 3;
  162. else if (imageHeight < 1200) littleRecSize = 4;
  163. else if (imageHeight < 1500) littleRecSize = 5;
  164. else if (imageHeight < 1800) littleRecSize = 6;
  165. else if (imageHeight < 2100) littleRecSize = 7;
  166. else if (imageHeight < 2400) littleRecSize = 8;
  167. else if (imageHeight < 2700) littleRecSize = 9;
  168. else if (imageHeight < 3000) littleRecSize = 10;
  169. else if (imageHeight < 3300) littleRecSize = 11;
  170. else if (imageHeight < 3600) littleRecSize = 12;
  171. else if (imageHeight < 3900) littleRecSize = 13;
  172. else if (imageHeight < 4200) littleRecSize = 14;
  173. else if (imageHeight < 4500) littleRecSize = 15;
  174. else if (imageHeight < 4800) littleRecSize = 16;
  175. else if (imageHeight < 5100) littleRecSize = 17;
  176. else littleRecSize = 18;
  177. if (littleRecSize % 2 != 0)
  178. littleRecSize++;
  179. HOperatorSet.SetDraw(window, "margin");
  180. window.DispRectangle2(midR, midC, -phi, length1, length2);
  181. HOperatorSet.SetDraw(window, "fill");
  182. for (int i = 0; i < NumHandles; i++)
  183. {
  184. window.DispRectangle2(rows[i].D, cols[i].D, -phi, littleRecSize, littleRecSize);
  185. Application.DoEvents();
  186. }
  187. window.DispArrow(midR, midC, midR + (Math.Sin(phi) * length1 * 1.8),
  188. midC + (Math.Cos(phi) * length1 * 1.8), littleRecSize);
  189. }
  190. /// <summary>
  191. /// Returns the distance of the ROI handle being
  192. /// closest to the image point(x,y)
  193. /// </summary>
  194. /// <param name="x">x (=column) coordinate</param>
  195. /// <param name="y">y (=row) coordinate</param>
  196. /// <returns>
  197. /// Distance of the closest ROI handle.
  198. /// </returns>
  199. public override double distToClosestHandle(double x, double y)
  200. {
  201. double max = 10000;
  202. double [] val = new double[NumHandles];
  203. for (int i=0; i < NumHandles; i++)
  204. val[i] = HMisc.DistancePp(y, x, rows[i].D, cols[i].D);
  205. for (int i=0; i < NumHandles; i++)
  206. {
  207. if (val[i] < max)
  208. {
  209. max = val[i];
  210. activeHandleIdx = i;
  211. }
  212. }
  213. return val[activeHandleIdx];
  214. }
  215. /// <summary>
  216. /// Paints the active handle of the ROI object into the supplied window
  217. /// </summary>
  218. /// <param name="window">HALCON window</param>
  219. public override void displayActive(HalconDotNet.HWindow window, int imageWidth, int imageHeight)
  220. {
  221. double littleRecSize = 0;
  222. if (imageHeight < 300) littleRecSize = 1;
  223. else if (imageHeight < 600) littleRecSize = 2;
  224. else if (imageHeight < 900) littleRecSize = 3;
  225. else if (imageHeight < 1200) littleRecSize = 4;
  226. else if (imageHeight < 1500) littleRecSize = 5;
  227. else if (imageHeight < 1800) littleRecSize = 6;
  228. else if (imageHeight < 2100) littleRecSize = 7;
  229. else if (imageHeight < 2400) littleRecSize = 8;
  230. else if (imageHeight < 2700) littleRecSize = 9;
  231. else if (imageHeight < 3000) littleRecSize = 10;
  232. else if (imageHeight < 3300) littleRecSize = 11;
  233. else if (imageHeight < 3600) littleRecSize = 12;
  234. else if (imageHeight < 3900) littleRecSize = 13;
  235. else if (imageHeight < 4200) littleRecSize = 14;
  236. else if (imageHeight < 4500) littleRecSize = 15;
  237. else if (imageHeight < 4800) littleRecSize = 16;
  238. else if (imageHeight < 5100) littleRecSize = 17;
  239. else littleRecSize = 18;
  240. if (littleRecSize % 2 != 0)
  241. littleRecSize++;
  242. window.DispRectangle2(rows[activeHandleIdx].D,
  243. cols[activeHandleIdx].D,
  244. -phi, littleRecSize, littleRecSize);
  245. if (activeHandleIdx == 5)
  246. window.DispArrow(midR, midC,
  247. midR + (Math.Sin(phi) * length1 * 1.8),
  248. midC + (Math.Cos(phi) * length1 * 1.8),
  249. littleRecSize);
  250. }
  251. /// <summary>Gets the HALCON region described by the ROI</summary>
  252. public override HRegion getRegion()
  253. {
  254. HRegion region = new HRegion();
  255. region.GenRectangle2(midR, midC, -phi, length1, length2);
  256. return region;
  257. }
  258. /// <summary>
  259. /// Gets the model information described by
  260. /// the interactive ROI
  261. /// </summary>
  262. public override HTuple getModelData()
  263. {
  264. return new HTuple(new double[] { midR, midC, phi, length1, length2 });
  265. }
  266. public override HTuple getRowsData()
  267. {
  268. return new HTuple(rows);
  269. }
  270. public override HTuple getColsData()
  271. {
  272. return new HTuple(cols );
  273. }
  274. /// <summary>
  275. /// Recalculates the shape of the ROI instance. Translation is
  276. /// performed at the active handle of the ROI object
  277. /// for the image coordinate (x,y)
  278. /// </summary>
  279. /// <param name="newX">x mouse coordinate</param>
  280. /// <param name="newY">y mouse coordinate</param>
  281. public override void moveByHandle(double newX, double newY, HWindowControl window)
  282. {
  283. double vX, vY, x=0, y=0;
  284. switch (activeHandleIdx)
  285. {
  286. case 0:
  287. case 1:
  288. case 2:
  289. case 3:
  290. tmp = hom2D.HomMat2dInvert();
  291. x = tmp.AffineTransPoint2d(newX, newY, out y);
  292. length2 = Math.Abs(y);
  293. length1 = Math.Abs(x);
  294. checkForRange(x, y);
  295. window.Cursor = System.Windows.Forms.Cursors.Hand ;
  296. break;
  297. case 4:
  298. midC = newX;
  299. midR = newY;
  300. window.Cursor = System.Windows.Forms.Cursors.SizeAll ;
  301. break;
  302. case 5:
  303. vY = newY - rows[4].D;
  304. vX = newX - cols[4].D;
  305. phi = Math.Atan2(vY, vX);
  306. window.Cursor = System.Windows.Forms.Cursors.Hand;
  307. break;
  308. case 7:
  309. case 9:
  310. tmp = hom2D.HomMat2dInvert();
  311. x = tmp.AffineTransPoint2d(newX, newY, out y);
  312. length2 = Math.Abs(y);
  313. checkForRange(x, y);
  314. window.Cursor = System.Windows.Forms.Cursors.Hand;
  315. break;
  316. case 6:
  317. case 8:
  318. tmp = hom2D.HomMat2dInvert();
  319. x = tmp.AffineTransPoint2d(newX, newY, out y);
  320. length1 = Math.Abs(x);
  321. checkForRange(x, y);
  322. window.Cursor = System.Windows.Forms.Cursors.Hand;
  323. break;
  324. }
  325. updateHandlePos();
  326. }//end of method
  327. /// <summary>
  328. /// Auxiliary method to recalculate the contour points of
  329. /// the rectangle by transforming the initial row and
  330. /// column coordinates (rowsInit, colsInit) by the updated
  331. /// homography hom2D
  332. /// </summary>
  333. private void updateHandlePos()
  334. {
  335. hom2D.HomMat2dIdentity();
  336. hom2D = hom2D.HomMat2dTranslate(midC, midR);
  337. hom2D = hom2D.HomMat2dRotateLocal(phi);
  338. tmp = hom2D.HomMat2dScaleLocal(length1, length2);
  339. cols = tmp.AffineTransPoint2d(colsInit, rowsInit, out rows);
  340. }
  341. /* This auxiliary method checks the half lengths
  342. * (length1, length2) using the coordinates (x,y) of the four
  343. * rectangle corners (handles 0 to 3) to avoid 'bending' of
  344. * the rectangular ROI at its midpoint, when it comes to a
  345. * 'collapse' of the rectangle for length1=length2=0.
  346. * */
  347. private void checkForRange(double x, double y)
  348. {
  349. switch (activeHandleIdx)
  350. {
  351. case 0:
  352. if ((x < 0) && (y < 0))
  353. return;
  354. if (x >= 0) length1 = 0.01;
  355. if (y >= 0) length2 = 0.01;
  356. break;
  357. case 1:
  358. if ((x > 0) && (y < 0))
  359. return;
  360. if (x <= 0) length1 = 0.01;
  361. if (y >= 0) length2 = 0.01;
  362. break;
  363. case 2:
  364. if ((x > 0) && (y > 0))
  365. return;
  366. if (x <= 0) length1 = 0.01;
  367. if (y <= 0) length2 = 0.01;
  368. break;
  369. case 3:
  370. if ((x < 0) && (y > 0))
  371. return;
  372. if (x >= 0) length1 = 0.01;
  373. if (y <= 0) length2 = 0.01;
  374. break;
  375. default:
  376. break;
  377. }
  378. }
  379. }//end of class
  380. }//end of namespace