CommonMethods.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using HalconDotNet;
  8. namespace CommonMethods
  9. {
  10. public class CommonMethods
  11. {
  12. /// <summary>
  13. /// 延时操作
  14. /// </summary>
  15. /// <param name="mm"></param>
  16. public static void Delay(int mm)
  17. {
  18. DateTime delay = DateTime.Now.AddMilliseconds((double)mm);
  19. while (delay > DateTime.Now)
  20. {
  21. Application.DoEvents();
  22. }
  23. }
  24. /// <summary>
  25. /// Creates an arrow shaped XLD contour.
  26. /// </summary>
  27. /// <param name="ho_Arrow">生成的箭头</param>
  28. /// <param name="hv_Row1">箭头起点Row</param>
  29. /// <param name="hv_Column1">箭头起点Column</param>
  30. /// <param name="hv_Row2">箭头终点Row</param>
  31. /// <param name="hv_Column2">箭头终点Column</param>
  32. /// <param name="hv_HeadLength">头长度</param>
  33. /// <param name="hv_HeadWidth">头宽度</param>
  34. public static void gen_arrow_contour_xld(out HObject ho_Arrow, HTuple hv_Row1, HTuple hv_Column1,
  35. HTuple hv_Row2, HTuple hv_Column2, HTuple hv_HeadLength, HTuple hv_HeadWidth)
  36. {
  37. // Stack for temporary objects
  38. HObject[] OTemp = new HObject[20];
  39. // Local iconic variables
  40. HObject ho_TempArrow = null;
  41. // Local control variables
  42. HTuple hv_Length = new HTuple(), hv_ZeroLengthIndices = new HTuple();
  43. HTuple hv_DR = new HTuple(), hv_DC = new HTuple(), hv_HalfHeadWidth = new HTuple();
  44. HTuple hv_RowP1 = new HTuple(), hv_ColP1 = new HTuple();
  45. HTuple hv_RowP2 = new HTuple(), hv_ColP2 = new HTuple();
  46. HTuple hv_Index = new HTuple();
  47. // Initialize local and output iconic variables
  48. HOperatorSet.GenEmptyObj(out ho_Arrow);
  49. HOperatorSet.GenEmptyObj(out ho_TempArrow);
  50. //This procedure generates arrow shaped XLD contours,
  51. //pointing from (Row1, Column1) to (Row2, Column2).
  52. //If starting and end point are identical, a contour consisting
  53. //of a single point is returned.
  54. //
  55. //input parameteres:
  56. //Row1, Column1: Coordinates of the arrows' starting points
  57. //Row2, Column2: Coordinates of the arrows' end points
  58. //HeadLength, HeadWidth: Size of the arrow heads in pixels
  59. //
  60. //output parameter:
  61. //Arrow: The resulting XLD contour
  62. //
  63. //The input tuples Row1, Column1, Row2, and Column2 have to be of
  64. //the same length.
  65. //HeadLength and HeadWidth either have to be of the same length as
  66. //Row1, Column1, Row2, and Column2 or have to be a single element.
  67. //If one of the above restrictions is violated, an error will occur.
  68. //
  69. //
  70. //Init
  71. ho_Arrow.Dispose();
  72. HOperatorSet.GenEmptyObj(out ho_Arrow);
  73. //
  74. //Calculate the arrow length
  75. hv_Length.Dispose();
  76. HOperatorSet.DistancePp(hv_Row1, hv_Column1, hv_Row2, hv_Column2, out hv_Length);
  77. //
  78. //Mark arrows with identical start and end point
  79. //(set Length to -1 to avoid division-by-zero exception)
  80. hv_ZeroLengthIndices.Dispose();
  81. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  82. {
  83. hv_ZeroLengthIndices = hv_Length.TupleFind(
  84. 0);
  85. }
  86. if ((int)(new HTuple(hv_ZeroLengthIndices.TupleNotEqual(-1))) != 0)
  87. {
  88. if (hv_Length == null)
  89. hv_Length = new HTuple();
  90. hv_Length[hv_ZeroLengthIndices] = -1;
  91. }
  92. //
  93. //Calculate auxiliary variables.
  94. hv_DR.Dispose();
  95. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  96. {
  97. hv_DR = (1.0 * (hv_Row2 - hv_Row1)) / hv_Length;
  98. }
  99. hv_DC.Dispose();
  100. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  101. {
  102. hv_DC = (1.0 * (hv_Column2 - hv_Column1)) / hv_Length;
  103. }
  104. hv_HalfHeadWidth.Dispose();
  105. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  106. {
  107. hv_HalfHeadWidth = hv_HeadWidth / 2.0;
  108. }
  109. //
  110. //Calculate end points of the arrow head.
  111. hv_RowP1.Dispose();
  112. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  113. {
  114. hv_RowP1 = (hv_Row1 + ((hv_Length - hv_HeadLength) * hv_DR)) + (hv_HalfHeadWidth * hv_DC);
  115. }
  116. hv_ColP1.Dispose();
  117. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  118. {
  119. hv_ColP1 = (hv_Column1 + ((hv_Length - hv_HeadLength) * hv_DC)) - (hv_HalfHeadWidth * hv_DR);
  120. }
  121. hv_RowP2.Dispose();
  122. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  123. {
  124. hv_RowP2 = (hv_Row1 + ((hv_Length - hv_HeadLength) * hv_DR)) - (hv_HalfHeadWidth * hv_DC);
  125. }
  126. hv_ColP2.Dispose();
  127. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  128. {
  129. hv_ColP2 = (hv_Column1 + ((hv_Length - hv_HeadLength) * hv_DC)) + (hv_HalfHeadWidth * hv_DR);
  130. }
  131. //
  132. //Finally create output XLD contour for each input point pair
  133. for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_Length.TupleLength())) - 1); hv_Index = (int)hv_Index + 1)
  134. {
  135. if ((int)(new HTuple(((hv_Length.TupleSelect(hv_Index))).TupleEqual(-1))) != 0)
  136. {
  137. //Create_ single points for arrows with identical start and end point
  138. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  139. {
  140. ho_TempArrow.Dispose();
  141. HOperatorSet.GenContourPolygonXld(out ho_TempArrow, hv_Row1.TupleSelect(hv_Index),
  142. hv_Column1.TupleSelect(hv_Index));
  143. }
  144. }
  145. else
  146. {
  147. //Create arrow contour
  148. using (HDevDisposeHelper dh = new HDevDisposeHelper())
  149. {
  150. ho_TempArrow.Dispose();
  151. HOperatorSet.GenContourPolygonXld(out ho_TempArrow, ((((((((((hv_Row1.TupleSelect(
  152. hv_Index))).TupleConcat(hv_Row2.TupleSelect(hv_Index)))).TupleConcat(
  153. hv_RowP1.TupleSelect(hv_Index)))).TupleConcat(hv_Row2.TupleSelect(hv_Index)))).TupleConcat(
  154. hv_RowP2.TupleSelect(hv_Index)))).TupleConcat(hv_Row2.TupleSelect(hv_Index)),
  155. ((((((((((hv_Column1.TupleSelect(hv_Index))).TupleConcat(hv_Column2.TupleSelect(
  156. hv_Index)))).TupleConcat(hv_ColP1.TupleSelect(hv_Index)))).TupleConcat(
  157. hv_Column2.TupleSelect(hv_Index)))).TupleConcat(hv_ColP2.TupleSelect(
  158. hv_Index)))).TupleConcat(hv_Column2.TupleSelect(hv_Index)));
  159. }
  160. }
  161. {
  162. HObject ExpTmpOutVar_0;
  163. HOperatorSet.ConcatObj(ho_Arrow, ho_TempArrow, out ExpTmpOutVar_0);
  164. ho_Arrow.Dispose();
  165. ho_Arrow = ExpTmpOutVar_0;
  166. }
  167. }
  168. ho_TempArrow.Dispose();
  169. hv_Length.Dispose();
  170. hv_ZeroLengthIndices.Dispose();
  171. hv_DR.Dispose();
  172. hv_DC.Dispose();
  173. hv_HalfHeadWidth.Dispose();
  174. hv_RowP1.Dispose();
  175. hv_ColP1.Dispose();
  176. hv_RowP2.Dispose();
  177. hv_ColP2.Dispose();
  178. hv_Index.Dispose();
  179. return;
  180. }
  181. }
  182. }