OTSImageDisHelp.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. using OpenCvSharp;
  2. using OTSCLRINTERFACE;
  3. using OTSCommon.Model;
  4. using OTSIncAReportApp.DataOperation.DataAccess;
  5. using OTSIncAReportApp.SysMgrTools;
  6. using OTSIncAReportGraph.Class;
  7. using OTSIncAReportGraph.Controls;
  8. using OTSModelSharp.DTLBase;
  9. using OTSModelSharp.ServiceInterface;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Diagnostics;
  15. using System.Drawing;
  16. using System.Drawing.Drawing2D;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Runtime.Serialization.Formatters.Binary;
  20. using Point = System.Drawing.Point;
  21. namespace OTSIncAReportGraph.OTSIncAReportGraphFuncation
  22. {
  23. public class OTSImageDisHelp
  24. {
  25. #region 枚举定义
  26. /// <summary>
  27. /// 样品台X轴方向
  28. /// </summary>
  29. enum OTS_X_AXIS_DIRECTION
  30. {
  31. LEFT_TOWARD = 0,
  32. RIGHT_TOWARD = 1
  33. }
  34. /// <summary>
  35. /// 样品台Y轴方向
  36. /// </summary>
  37. enum OTS_Y_AXIS_DIRECTION
  38. {
  39. UP_TOWARD = 0,
  40. DOWN_TOWARD = 1
  41. }
  42. #endregion
  43. #region 定义变量
  44. private ResultFile resultFile = null;
  45. public ParticleSegmentation m_ParticleSegmentation;
  46. //记录field列表的原值
  47. public List<DisplayField> m_original_list_dfield = null;
  48. //记录原值,颗粒和线段
  49. public List<BaseObject> m_original_list_baseobject = null;
  50. NLog.Logger log;
  51. //field的数量
  52. public int m_field_count = 0;
  53. //particle的数量
  54. public int m_particle_count = 0;
  55. //加载使用的时间
  56. public string m_time_str = "";
  57. //加载使用时间计算时间段2
  58. public string m_time_str2 = "";
  59. //电镜设置对象
  60. public ServiceInterface.HardwareController m_cfun = null;
  61. //是否已经连接到了电镜
  62. //public bool m_SEMConnectionState = false;
  63. //连接到电镜的ID号
  64. public int m_SEM_ID = 0;
  65. #endregion
  66. #region 构造函数
  67. public OTSImageDisHelp( ResultFile result)
  68. {
  69. m_original_list_dfield = new List<DisplayField>();
  70. m_original_list_baseobject = new List<BaseObject>();
  71. resultFile = result;
  72. m_cfun = ServiceInterface.HardwareController.GetSemController();
  73. log = NLog.LogManager.GetCurrentClassLogger();
  74. }
  75. #endregion
  76. #region 封装自定义方法
  77. public Point GetOTSCoordLeftBottomPoint(List<Point> allFldPos)
  78. {
  79. //the ots coordinate system is always positive on the right and up direction.So the leftbottom point would be the smallest point.
  80. //找出最小的x,y用来做偏移运算
  81. int i_offset_x = 1000000000;
  82. int i_offset_y = 1000000000;
  83. //先取出最小的x,y
  84. for (int i = 0; i < allFldPos.Count; i++)
  85. {
  86. if (i_offset_x > allFldPos[i].X)
  87. {
  88. i_offset_x = allFldPos[i].X;
  89. }
  90. if (i_offset_y > allFldPos[i].Y)
  91. {
  92. i_offset_y = allFldPos[i].Y;
  93. }
  94. }
  95. return new Point(i_offset_x, i_offset_y);
  96. }
  97. public Point ConvertOTSCoordinateToScreenCoord(Point otsleftBottomPoint,double pixelSize, Point currenFldPos,RectangleF wholeImageRec,RectangleF singleImgRec)//
  98. {
  99. var OTSPoint=new Point(currenFldPos.X - otsleftBottomPoint.X, currenFldPos.Y - otsleftBottomPoint.Y);
  100. double screenHeight = wholeImageRec.Height + (0 - (int)(Convert.ToDouble(OTSPoint.Y) / pixelSize));//because the screen coordinate is downward rightward positive,so we need to translate the Y coordinate of the OTS system which is upward rightward positive.
  101. screenHeight = screenHeight - singleImgRec.Height;
  102. var screenPoint = new Point((int)(Convert.ToDouble(OTSPoint.X)/pixelSize), (int)screenHeight);
  103. return screenPoint;
  104. }
  105. public Rectangle ConvertAndGetMaxRect(List<Point> inPoints, int in_width, int in_height)
  106. {
  107. //首先要能确定下来,单个物理坐标的宽和高--------------------------------
  108. int i_wl_width = 0;
  109. int i_wl_height = 0;
  110. RectangleF ls_r = GetPhysicalFieldWidthAndHeight(inPoints,in_width,in_height);
  111. i_wl_width = (int)ls_r.Width;
  112. i_wl_height = (int)ls_r.Height;
  113. //-----------------------------------------------------------------------------
  114. int point_x_min = 10000000;
  115. int point_x_max = -10000000;
  116. int point_y_min = 10000000;
  117. int point_y_max = -10000000;
  118. for (int i = 0; i < inPoints.Count(); i++)
  119. {
  120. Point ls_point = inPoints[i];
  121. //取出正数最大x
  122. if (ls_point.X > point_x_max)
  123. point_x_max = ls_point.X;
  124. if (ls_point.Y > point_y_max)
  125. point_y_max = ls_point.Y;
  126. if (ls_point.X < point_x_min)
  127. point_x_min = ls_point.X;
  128. if (ls_point.Y < point_y_min)
  129. point_y_min = ls_point.Y;
  130. }
  131. //然后分别用最大值+abs(最小值),就是x,和y轴的总长值
  132. point_x_max = point_x_max - point_x_min;
  133. point_y_max = point_y_max - point_y_min;
  134. //该算法有个问题,就是不能直观的得到整个范围的大小,要除以倍数再补1能补充缺少的一个field视域**********
  135. point_x_max = ((point_x_max / i_wl_width) + 1) * i_wl_width;
  136. point_y_max = ((point_y_max / i_wl_height) + 1) * i_wl_height;
  137. //将物理宽高,变换成分辨率宽高
  138. if (i_wl_width != 0) point_x_max = (point_x_max / i_wl_width) * in_width; else point_x_max = 0;
  139. if (i_wl_height != 0) point_y_max = (point_y_max / i_wl_height) * in_height; else point_y_max = 0;
  140. Rectangle ret_rectangle = new Rectangle(0, 0, 0, 0);
  141. //判断一下防止出错,只有在有数据的情况下,进行赋值才行
  142. if (inPoints.Count > 0)
  143. {
  144. ret_rectangle = new Rectangle(0, 0, point_x_max, point_y_max);
  145. }
  146. //这样返回是物理坐标的总大小,应该返回像素坐标大小才对
  147. return ret_rectangle;
  148. }
  149. /// <summary>
  150. /// 计算单个field的物理大小 传入field的list,还有测量结果管理类对象,在无法计算出单field的物理大小的情况下,到这里取再计算得出
  151. /// </summary>
  152. /// <returns></returns>
  153. public RectangleF GetPhysicalFieldWidthAndHeight(List<Point> points,int imageWidth,int imageHeight)
  154. {
  155. int width_max = -10000000;
  156. int height_max = -10000000;
  157. int width_max2 = -10000000;
  158. int height_max2 = -10000000;
  159. //先找出最大的值,
  160. for (int i = 0; i < points.Count(); i++)
  161. {
  162. if (width_max < points[i].X)
  163. width_max = points[i].X;
  164. if (height_max < points[i].Y)
  165. height_max = points[i].Y;
  166. }
  167. //再找出第二大的值
  168. for (int i = 0; i < points.Count(); i++)
  169. {
  170. if (width_max2 < points[i].X && width_max != points[i].X)
  171. width_max2 = points[i].X;
  172. if (height_max2 < points[i].Y && height_max != points[i].Y)
  173. height_max2 = points[i].Y;
  174. }
  175. //需要针对第二大的值,获取时进行判断,感觉这里应该如果并未找到第二大的值的情况下,赋于0值,便于以后进行计算
  176. if (width_max2 == -10000000)
  177. width_max2 = width_max;
  178. if (height_max2 == -10000000)
  179. height_max2 = height_max;
  180. RectangleF ret_rect = new RectangleF(0, 0, width_max - width_max2, height_max - height_max2);
  181. //如果最后计算出的宽高有0则重新到测量数据中获取---------------------------------------
  182. if (ret_rect.Width == 0 || ret_rect.Height == 0)
  183. {
  184. //到参数中去取单个宽
  185. double d_scanFieldSize_width = Convert.ToDouble(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["scanFieldSize"]);
  186. //然后再用单个宽去计算出高是多少
  187. double d_scanFieldSize_height = 0;
  188. if (d_scanFieldSize_width != 0)
  189. d_scanFieldSize_height = (d_scanFieldSize_width / Convert.ToDouble(imageWidth)) * imageHeight;
  190. ret_rect.Width = (int)d_scanFieldSize_width;
  191. ret_rect.Height = (int)d_scanFieldSize_height;
  192. }
  193. ///-----------because all the fields 's height/width=0.75 so here we make an enforce. gsp add at 2019/10/31
  194. ///sometimes the gbfields are not conform to this for the cuting and merging operation.
  195. //if (ret_rect.Height / ret_rect.Width != 0.75f)
  196. //{
  197. // ret_rect = new Rectangle(ret_rect.X, ret_rect.Y, ret_rect.Width, (int)(ret_rect.Width * 0.75f));
  198. //}
  199. return ret_rect;
  200. }
  201. #endregion
  202. #region 电镜操作相关方法
  203. /// <summary>
  204. /// 连接电镜,分布图使用
  205. /// </summary>
  206. public bool ConnectToIpcSvr()
  207. {
  208. //和电镜建立通讯连接
  209. return m_cfun.ConnectToIpcSvr();
  210. }
  211. /// <summary>
  212. /// 移动电镜到指定的X,Y坐标上,R坐标使用原先的值进行移动
  213. /// </summary>
  214. /// <param name="PositionX"></param>
  215. /// <param name="PositionY"></param>
  216. public void MoveSemToPointXY(double in_PositionX, double in_PositionY)
  217. {
  218. log.Trace("Begin MoveSemToPointXY:(" +in_PositionX.ToString()+","+in_PositionY.ToString()+")");
  219. //首先获取电镜当前的位置,并记录原R值
  220. double ls_PositionX = 0;
  221. double ls_PositionY = 0;
  222. double ls_PositionR = 0;
  223. if (m_cfun.ConnectToIpcSvr())
  224. {
  225. m_cfun.GetSemPositionXY(ref ls_PositionX, ref ls_PositionY, ref ls_PositionR);
  226. }
  227. else
  228. {
  229. log.Error("Failed to GetSemPositionXY");
  230. return;
  231. }
  232. if (m_cfun.ConnectToIpcSvr())
  233. {
  234. m_cfun.MoveSEMToPoint(new Point((int)in_PositionX, (int)in_PositionY), ls_PositionR);
  235. }
  236. }
  237. #endregion
  238. #region //--------------------------------------颗粒分布图相关部份---------------------------------------------------------------------
  239. /// <summary>
  240. /// 传入颗粒的tagid和fieldid,来获取该颗粒下对应的xray数据
  241. /// </summary>
  242. /// <param name="in_clr_tagid"></param>
  243. /// <param name="in_clr_fieldid"></param>
  244. /// <param name="Search_xray"></param>
  245. /// <param name="Analysis_xray"></param>
  246. public void GetXrayByParticleTagIDAndFieldID_ForDrawDistrbutionImageAndBSE(int in_clr_tagid, int in_clr_fieldid, out uint[] Search_xray, out uint[] Analysis_xray, out int xray_id, out List<Element> list_celementchemistryclr)
  247. {
  248. Search_xray = new uint[2000];
  249. Analysis_xray = new uint[2000];
  250. xray_id = 0;
  251. list_celementchemistryclr = new List<Element>();
  252. //防止为空校验判断
  253. if (resultFile.List_OTSField == null)
  254. return;
  255. Particle particle = resultFile.List_OTSField.Find(x => x.FieldID == in_clr_fieldid).ParticleList.Find(x => x.ParticleId == in_clr_tagid);
  256. var tmpPart = new ParticleData(resultFile.FilePath).GetParticleXrayDataByFidAndPid(Convert.ToString(particle.FieldId), Convert.ToString(particle.XrayId));
  257. if (tmpPart != null)
  258. {
  259. particle.XRayData = tmpPart.XRayData;
  260. if (particle.XrayId > -1)
  261. {
  262. for (int i = 0; i < 2000; i++)
  263. {
  264. Analysis_xray[i] = BitConverter.ToUInt32(particle.XRayData, i * 4);
  265. }
  266. Search_xray = Analysis_xray;
  267. xray_id = particle.XrayId;
  268. list_celementchemistryclr = particle.ElementList;
  269. }
  270. }
  271. }
  272. public bool DeleteParticlesFromLibrary(int in_clr_tagid, int in_clr_fieldid)
  273. {
  274. bool DoesItExist = false;
  275. for (int i=1;i<201;i++)
  276. {
  277. if (System.IO.Directory.Exists(resultFile.FilePath + "\\FIELD_FILES\\Backups"+i.ToString()))
  278. {
  279. DoesItExist = true;
  280. break;
  281. }
  282. }
  283. if (!DoesItExist)
  284. {
  285. //备份数据库
  286. BackupDatabase();
  287. }
  288. DoesItExist = false;
  289. //防止为空校验判断
  290. if (resultFile.List_OTSField == null)
  291. return false;
  292. Particle particle = resultFile.List_OTSField.Find(x => x.FieldID == in_clr_fieldid).ParticleList.Find(x => x.ParticleId == in_clr_tagid);
  293. if (new ParticleData(resultFile.FilePath).DeleteFromData(Convert.ToString(particle.FieldId), Convert.ToString(particle.XrayId)))
  294. {
  295. return true;
  296. }else
  297. {
  298. return false;
  299. }
  300. }
  301. public bool BackupDatabase()
  302. {
  303. //创建备份数据库文件夹
  304. string sourcePath = resultFile.FilePath + "\\FIELD_FILES\\Inclusion.db";//源文件路径
  305. string sourceName = "Inclusion.db";//源文件名称
  306. string folderPath = resultFile.FilePath + "\\FIELD_FILES\\Backups";//目标文件夹
  307. string Fields = "";
  308. for (int i = 1; i < 201; i++)
  309. {
  310. if (System.IO.Directory.Exists(folderPath + i.ToString()))
  311. {
  312. Fields = folderPath + i.ToString();
  313. break;
  314. }
  315. else
  316. {
  317. System.IO.Directory.CreateDirectory(folderPath + i.ToString());
  318. Fields = folderPath + i.ToString();
  319. break;
  320. }
  321. }
  322. if (Fields == "")
  323. {
  324. return false;
  325. }
  326. //复制数据库文件
  327. string targetPath = Path.Combine(Fields, sourceName);
  328. FileInfo file = new FileInfo(sourcePath);
  329. if (file.Exists)
  330. {
  331. file.CopyTo(targetPath, true);
  332. }
  333. return true;
  334. }
  335. /// <summary>
  336. /// 传入所有的物理field坐标点,和单个物理field的宽高,返回所有field的左上角位置,和整个field组成的rect大小
  337. /// </summary>
  338. /// <param name="in_list_point"></param>
  339. /// <param name="in_width"></param>
  340. /// <param name="in_height"></param>
  341. /// <returns></returns>
  342. public Rectangle GetWlRectTopLeftAndRect(List<Point> in_list_point, int in_width, int in_height)
  343. {
  344. //分别获取整个rect的xy最小值和最大值
  345. int i_rect_x_min = 100000000;
  346. int i_rect_y_min = 100000000;
  347. int i_rect_x_max = -100000000;
  348. int i_rect_y_max = -100000000;
  349. for (int i = 0; i < in_list_point.Count; i++)
  350. {
  351. if (i_rect_x_min > in_list_point[i].X)
  352. i_rect_x_min = in_list_point[i].X;
  353. if (i_rect_y_min > in_list_point[i].Y)
  354. i_rect_y_min = in_list_point[i].Y;
  355. if (i_rect_x_max < in_list_point[i].X)
  356. i_rect_x_max = in_list_point[i].X;
  357. if (i_rect_y_max < in_list_point[i].Y)
  358. i_rect_y_max = in_list_point[i].Y;
  359. }
  360. Rectangle ret_rect = new Rectangle(i_rect_x_min, i_rect_y_min,
  361. i_rect_x_max - i_rect_x_min, i_rect_y_max - i_rect_y_min);
  362. return ret_rect;
  363. }
  364. /// <summary>
  365. /// 根据Field的ID,来获取Field列表中对应FIeld的OTS 坐标
  366. /// </summary>
  367. /// <param name="in_fieldid"></param>
  368. /// <returns></returns>
  369. public Point GetOTSPointByFieldID(List<DisplayField> in_list_dfield, int in_fieldid)
  370. {
  371. Point ret_point = new Point(0, 0);
  372. for (int i = 0; i < in_list_dfield.Count; i++)
  373. {
  374. //这里TagID先代表的是底层返回的ID
  375. if (in_list_dfield[i].FieldID == in_fieldid.ToString())
  376. {
  377. ret_point = new Point(Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.X), Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.Y));
  378. }
  379. }
  380. return ret_point;
  381. }
  382. /// <summary>
  383. /// 将OTS坐标转换为Sem 坐标
  384. /// </summary>
  385. /// <param name="POTSCoord"></param>
  386. /// <returns></returns>
  387. public Point ChangeOTSToSemCoord(Point POTSCoord)
  388. {
  389. //first if m_semstagedata is null to get stage inforation
  390. Convert.ToDouble(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["scanFieldSize"]);
  391. //after obtaining stage info,calc stage point data
  392. Point ret_SEM_point = new Point();
  393. // get center point, um
  394. long xStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["XAxis"])["start"]);
  395. long xEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["XAxis"])["end"]);
  396. long xCenter = (xStart + xEnd) / 2;
  397. long yStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["YAxis"])["start"]);
  398. long yEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["YAxis"])["end"]);
  399. long yCenter = (yStart + yEnd) / 2;
  400. // delte = SEM - OTSa
  401. long deltex = xCenter - 0;
  402. long deltey = yCenter - 0;
  403. int xdir = Convert.ToInt32(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["xAxisDir"]);
  404. int ydir = Convert.ToInt32(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["yAxisDir"]);
  405. if (xdir == (int)OTS_X_AXIS_DIRECTION.LEFT_TOWARD)
  406. {
  407. ret_SEM_point.X = -1 * (POTSCoord.X - Convert.ToInt32(deltex));
  408. }
  409. else if (xdir == (int)OTS_X_AXIS_DIRECTION.RIGHT_TOWARD)
  410. {
  411. ret_SEM_point.X = POTSCoord.X + Convert.ToInt32(deltex);
  412. }
  413. if (ydir == (int)OTS_Y_AXIS_DIRECTION.UP_TOWARD)
  414. {
  415. ret_SEM_point.Y = POTSCoord.Y + Convert.ToInt32(deltey);
  416. }
  417. else if (ydir == (int)OTS_Y_AXIS_DIRECTION.DOWN_TOWARD)
  418. {
  419. ret_SEM_point.Y = -1 * (POTSCoord.Y - Convert.ToInt32(deltey));
  420. }
  421. return ret_SEM_point;
  422. }
  423. #endregion
  424. /// <summary>
  425. /// 判断该点是否在多边形的范围内
  426. /// </summary>
  427. /// <param name="inPoints"></param>
  428. /// <param name="WhetherPoint"></param>
  429. /// <returns></returns>
  430. public bool WhetherInRange(DisplayParticle Part,/*PointF[] inPoints,*/ Point WhetherPoint)
  431. {
  432. var rect = Part.Rect;
  433. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  434. {
  435. var itm = (BaseObject)Part;
  436. itm.GPath = Part.GetRegionFromDSegments();
  437. PointF[] inPoints = itm.GPath.PathPoints;
  438. bool b_inrange = false;
  439. GraphicsPath myGraphicsPath = new GraphicsPath();
  440. Region myRegion = new Region();
  441. myGraphicsPath.Reset();
  442. myGraphicsPath.AddPolygon(inPoints);
  443. myRegion.MakeEmpty();
  444. myRegion.Union(myGraphicsPath);
  445. //返回判断点是否在多边形里
  446. b_inrange = myRegion.IsVisible(WhetherPoint);
  447. return b_inrange;
  448. }
  449. else
  450. {
  451. return false;
  452. }
  453. }
  454. public bool WhetherInRange(RectangleF rec,PointF[] inPoints, Point WhetherPoint)
  455. {
  456. var rect = rec;
  457. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  458. {
  459. //var itm = (BaseObject)Part;
  460. //PointF[] inPoints = itm.GPath.PathPoints;
  461. bool b_inrange = false;
  462. GraphicsPath myGraphicsPath = new GraphicsPath();
  463. Region myRegion = new Region();
  464. myGraphicsPath.Reset();
  465. myGraphicsPath.AddPolygon(inPoints);
  466. myRegion.MakeEmpty();
  467. myRegion.Union(myGraphicsPath);
  468. //返回判断点是否在多边形里
  469. b_inrange = myRegion.IsVisible(WhetherPoint);
  470. return b_inrange;
  471. }
  472. else
  473. {
  474. return false;
  475. }
  476. }
  477. /// <summary>
  478. /// 判断该点是否在多边形的范围内的float版本重载
  479. /// </summary>
  480. /// <param name="inPoints"></param>
  481. /// <param name="WhetherPoint"></param>
  482. /// <returns></returns>
  483. public bool WhetherInRange(DisplayParticle Part,/* PointF[] inPoints, */PointF WhetherPoint)
  484. {
  485. var rect = Part.Rect;
  486. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  487. {
  488. var itm = (BaseObject)Part;
  489. PointF[] inPoints = itm.GPath.PathPoints;
  490. bool b_inrange = false;
  491. GraphicsPath myGraphicsPath = new GraphicsPath();
  492. Region myRegion = new Region();
  493. myGraphicsPath.Reset();
  494. myGraphicsPath.AddPolygon(inPoints);
  495. myRegion.MakeEmpty();
  496. myRegion.Union(myGraphicsPath);
  497. //返回判断点是否在多边形里
  498. b_inrange = myRegion.IsVisible(WhetherPoint);
  499. return b_inrange;
  500. }
  501. else
  502. {
  503. return false;
  504. }
  505. }
  506. #region 颗粒分割功能方法
  507. /// <summary>
  508. /// 获取线段与矩形的两个交点
  509. /// </summary>
  510. /// <param name="line1S"></param>
  511. /// <param name="line1E"></param>
  512. /// <param name="rectangle"></param>
  513. /// <param name="pointList"></param>
  514. /// <returns></returns>
  515. public bool GetInterBetweenLinesAndRect(Point line1S, Point line1E, RectangleF rectangle, ref List<Point> pointList)
  516. {
  517. try
  518. {
  519. PointF pointF = new PointF(-1, -1);
  520. Rect rect = new Rect();
  521. rect.X = m_ParticleSegmentation.ParticleData.RectLeft;
  522. rect.Y = m_ParticleSegmentation.ParticleData.RectTop;
  523. rect.Width = m_ParticleSegmentation.ParticleData.RectWidth;
  524. rect.Height = m_ParticleSegmentation.ParticleData.RectHeight;
  525. float border = 0;
  526. if (GetInterBetweenTwoLines(line1S, line1E, new PointF(rectangle.Left, rectangle.Top), new PointF(rectangle.Right, rectangle.Top), ref pointF))
  527. {
  528. border = (pointF.X - rectangle.Left) / (rectangle.Right - rectangle.Left);
  529. pointList.Add(new Point((int)(rect.X + rect.Width * border), rect.Y));
  530. }
  531. if (GetInterBetweenTwoLines(line1S, line1E,
  532. new PointF(rectangle.Left, rectangle.Bottom), new PointF(rectangle.Right, rectangle.Bottom), ref pointF))
  533. {
  534. border = (pointF.X - rectangle.Left) / (rectangle.Right - rectangle.Left);
  535. pointList.Add(new Point((int)(rect.X + rect.Width * border), rect.Y + rect.Height));
  536. }
  537. if (GetInterBetweenTwoLines(line1S, line1E,
  538. new PointF(rectangle.Left, rectangle.Top), new PointF(rectangle.Left, rectangle.Bottom), ref pointF))
  539. {
  540. border = (pointF.Y - rectangle.Top) / (rectangle.Bottom - rectangle.Top);
  541. pointList.Add(new Point(rect.X, (int)(rect.Y + rect.Height * border)));
  542. }
  543. if (GetInterBetweenTwoLines(line1S, line1E,
  544. new PointF(rectangle.Right, rectangle.Top), new PointF(rectangle.Right, rectangle.Bottom), ref pointF))
  545. {
  546. border = (pointF.Y - rectangle.Top) / (rectangle.Bottom - rectangle.Top);
  547. pointList.Add(new Point(rect.X + rect.Width, (int)(rect.Y + rect.Height * border)));
  548. }
  549. if (pointList.Count == 2)
  550. {
  551. return true;
  552. }
  553. else
  554. {
  555. return false;
  556. }
  557. }
  558. catch (Exception ex)
  559. {
  560. log.Trace("(GetSplitPartFun)" + ex);
  561. return false;
  562. }
  563. }
  564. /// <summary>
  565. /// 获取分离颗粒方法
  566. /// </summary>
  567. /// <param name="startPoint"></param>
  568. /// <param name="endPoint"></param>
  569. /// <returns></returns>
  570. public bool GetSplitPartFun(Point startPoint, Point endPoint)
  571. {
  572. try
  573. {
  574. CImageHandler m_ImagePro = new CImageHandler();
  575. Particle particle1 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
  576. Particle particle2 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
  577. Dictionary<string, object> sampleMembers = ((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["Sample"])["Members"]);
  578. Dictionary<string, object> imageScanParam = (Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)sampleMembers["MsrParams"])["Members"])["ImageScanParam"];
  579. double d_scanFieldSize_width = Convert.ToDouble(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["scanFieldSize"]);
  580. string ImageResolution = imageScanParam["ImageResolution"].ToString();
  581. int width = int.Parse(ImageResolution.Split('_')[1]);
  582. int height = int.Parse(ImageResolution.Split('_')[2]);
  583. double dPixelSize = d_scanFieldSize_width / width;
  584. using (Mat mat = new Mat(height, width, MatType.CV_8UC1, Scalar.Black))//黑色底图
  585. using (Mat labelMat = new Mat())
  586. using (Mat stats = new Mat())
  587. using (Mat centroids = new Mat())
  588. using (Mat matBse = new Mat(resultFile.List_OTSField[particle1.FieldId].FieldImage, ImreadModes.Grayscale))
  589. {
  590. foreach (Segment segment in m_ParticleSegmentation.ParticleData.SegmentList)
  591. {
  592. Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
  593. }
  594. //寻找坐标点
  595. List<Point> points1 = new List<Point>();
  596. List<Point> points2 = new List<Point>();
  597. List<int> aveGray1 = new List<int>();
  598. List<int> aveGray2 = new List<int>();
  599. for (int k = 0; k < mat.Height; k++)
  600. {
  601. for (int j = 0; j < mat.Width; j++)
  602. {
  603. if (mat.Get<int>(k, j) != 0)
  604. {
  605. int side = (startPoint.X - j) * (endPoint.Y - k) - (startPoint.Y - k) * (endPoint.X - j);
  606. if (side >= 0)//区分像素位置
  607. {
  608. points1.Add(new Point(j, k));
  609. aveGray1.Add(matBse.Get<byte>(k, j));
  610. }
  611. else
  612. {
  613. points2.Add(new Point(j, k));
  614. aveGray2.Add(matBse.Get<byte>(k, j));
  615. }
  616. }
  617. }
  618. }
  619. //处理Segment
  620. List<COTSSegmentClr> SegmentClrList1 = new List<COTSSegmentClr>();
  621. List<COTSSegmentClr> SegmentClrList2 = new List<COTSSegmentClr>();
  622. List<Segment> SegmentList1 = new List<Segment>();
  623. List<Segment> SegmentList2 = new List<Segment>();
  624. GetSegment(points1, SegmentClrList1, ref SegmentList1);
  625. GetSegment(points2, SegmentClrList2, ref SegmentList2);
  626. //颗粒一
  627. Cv2.Threshold(mat, mat, 0, 0, ThresholdTypes.Binary);
  628. foreach (Segment segment in SegmentList1)
  629. {
  630. Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
  631. }
  632. Cv2.ConnectedComponentsWithStats(mat, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  633. CopyToPart(particle1, SegmentList1, SegmentClrList1, aveGray1, stats, centroids, dPixelSize);
  634. //颗粒二
  635. Cv2.Threshold(mat, mat, 0, 0, ThresholdTypes.Binary);
  636. foreach (Segment segment in SegmentList2)
  637. {
  638. Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
  639. }
  640. Cv2.ConnectedComponentsWithStats(mat, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  641. CopyToPart(particle2, SegmentList2, SegmentClrList2, aveGray2, stats, centroids, dPixelSize);
  642. if (!SaveToDb(particle1, particle2))
  643. {
  644. log.Trace("(GetSplitPartFun) SaveToDb Faild");
  645. return false;
  646. }
  647. }
  648. return true;
  649. }
  650. catch (Exception ex)
  651. {
  652. log.Trace("(GetSplitPartFun)" + ex);
  653. return false;
  654. }
  655. }
  656. /// <summary>
  657. /// 拷贝颗粒数据
  658. /// </summary>
  659. /// <param name="particle"></param>
  660. /// <param name="segments"></param>
  661. /// <param name="SegmentClrList"></param>
  662. /// <param name="aveGray"></param>
  663. /// <param name="stats"></param>
  664. /// <param name="centroids"></param>
  665. /// <param name="dPixelSize"></param>
  666. private void CopyToPart(Particle particle, List<Segment> segments, List<COTSSegmentClr> SegmentClrList, List<int> aveGray, Mat stats, Mat centroids, double dPixelSize)
  667. {
  668. CImageHandler m_ImagePro = new CImageHandler();
  669. COTSParticleClr part = new COTSParticleClr();
  670. part.SetArea(stats.At<int>(1, 4) * dPixelSize * dPixelSize);
  671. part.SetParticleRect(new Rectangle(stats.At<int>(1, 0), stats.At<int>(1, 1), stats.At<int>(1, 2), stats.At<int>(1, 3)));
  672. part.GetFeature().SetSegmentsList(SegmentClrList, false);
  673. m_ImagePro.CalParticleImageProp(part, dPixelSize);
  674. particle.SegmentList = segments;
  675. particle.SegmentNum = segments.Count;
  676. particle.AveGray = (int)aveGray.Average();
  677. particle.RectLeft = stats.At<int>(1, 0);
  678. particle.RectTop = stats.At<int>(1, 1);
  679. particle.RectWidth = stats.At<int>(1, 2);
  680. particle.RectHeight = stats.At<int>(1, 3);
  681. particle.Area = stats.At<int>(1, 4) * dPixelSize * dPixelSize;
  682. particle.PosX = (int)centroids.At<double>(1, 0);
  683. particle.PosY = (int)centroids.At<double>(1, 1);
  684. particle.DFERET = part.GetFeretDiameter();
  685. particle.DMAX = part.GetDMAX();
  686. particle.DMIN = part.GetDMIN();
  687. particle.DPERP = part.GetDMPERP();
  688. particle.PERIMETER = part.GetDPRIMETER();
  689. particle.ORIENTATION = part.GetORIENTATION();
  690. particle.DINSCR = part.GetDINSCR();
  691. particle.DMEAN = part.GetDMEAN();
  692. particle.DELONG = part.GetDELONG();
  693. }
  694. /// <summary>
  695. /// 保存数据库
  696. /// </summary>
  697. /// <param name="particle1"></param>
  698. /// <param name="particle2"></param>
  699. /// <returns></returns>
  700. public bool SaveToDb(Particle particle1, Particle particle2)
  701. {
  702. //初始化
  703. SQLiteHelper sQLiteHelper = new SQLiteHelper(resultFile.FilePath + "\\FIELD_FILES\\Inclusion.db");
  704. sQLiteHelper.GetDBConnection();
  705. sQLiteHelper.BeginTransaction();
  706. //修改Segment表
  707. if (particle1.SegmentList.Count == 0 || particle2.SegmentList.Count == 0)
  708. {
  709. return false;
  710. }
  711. int SegmentCount = sQLiteHelper.ExecuteNewPartIdForTransaction() + 1;
  712. if (!sQLiteHelper.ExecuteSegmentForTransaction(particle1, particle2, SegmentCount))//修改Segment表
  713. {
  714. return false;
  715. }
  716. if (!sQLiteHelper.ExecuteXrayForTransaction(particle1, particle2, SegmentCount))//修改XRayData和PoxXrayInfo表
  717. {
  718. return false;
  719. }
  720. if (!sQLiteHelper.ExecuteIncAForTransaction(particle1, particle2, SegmentCount))//修改IncAData表
  721. {
  722. return false;
  723. }
  724. sQLiteHelper.CommitTransaction();
  725. return true;
  726. }
  727. /// <summary>
  728. /// 线段与矩形是否相交
  729. /// </summary>
  730. /// <param name="linePointX1"></param>
  731. /// <param name="linePointY1"></param>
  732. /// <param name="linePointX2"></param>
  733. /// <param name="linePointY2"></param>
  734. /// <param name="rectangleLeftTopX"></param>
  735. /// <param name="rectangleLeftTopY"></param>
  736. /// <param name="rectangleRightBottomX"></param>
  737. /// <param name="rectangleRightBottomY"></param>
  738. /// <returns></returns>
  739. public bool isLineIntersectRectangle(float linePointX1,
  740. float linePointY1,
  741. float linePointX2,
  742. float linePointY2,
  743. float rectangleLeftTopX,
  744. float rectangleLeftTopY,
  745. float rectangleRightBottomX,
  746. float rectangleRightBottomY)
  747. {
  748. float lineHeight = linePointY1 - linePointY2;
  749. float lineWidth = linePointX2 - linePointX1; // 计算叉乘
  750. float c = linePointX1 * linePointY2 - linePointX2 * linePointY1;
  751. if ((lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c <= 0)
  752. || (lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c >= 0)
  753. || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c <= 0)
  754. || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c >= 0))
  755. {
  756. if (rectangleLeftTopX > rectangleRightBottomX)
  757. {
  758. float temp = rectangleLeftTopX;
  759. rectangleLeftTopX = rectangleRightBottomX;
  760. rectangleRightBottomX = temp;
  761. }
  762. if (rectangleLeftTopY < rectangleRightBottomY)
  763. {
  764. float temp1 = rectangleLeftTopY;
  765. rectangleLeftTopY = rectangleRightBottomY;
  766. rectangleRightBottomY = temp1;
  767. }
  768. if ((linePointX1 < rectangleLeftTopX && linePointX2 < rectangleLeftTopX)
  769. || (linePointX1 > rectangleRightBottomX && linePointX2 > rectangleRightBottomX)
  770. || (linePointY1 > rectangleLeftTopY && linePointY2 > rectangleLeftTopY)
  771. || (linePointY1 < rectangleRightBottomY && linePointY2 < rectangleRightBottomY))
  772. {
  773. return false;
  774. }
  775. else
  776. {
  777. return true;
  778. }
  779. }
  780. else
  781. {
  782. return false;
  783. }
  784. }
  785. /// <summary>
  786. /// 求一个线段与另一个线段的交点
  787. /// </summary>
  788. /// <param name="line1S"></param>
  789. /// <param name="line1E"></param>
  790. /// <param name="line2S"></param>
  791. /// <param name="line2E"></param>
  792. /// <param name="interPoint"></param>
  793. /// <returns>-1:不存在交点;1:存在一个交点;2:存在无穷多个交点(重合或部分重合)</returns>
  794. public bool GetInterBetweenTwoLines(PointF line1S, PointF line1E,
  795. PointF line2S, PointF line2E, ref PointF interPoint)
  796. {
  797. int status = -1;
  798. // 判断两条直线各自的矩形包围盒的X与Y的值范围
  799. float line1Xmin = line1S.X < line1E.X ? line1S.X : line1E.X,
  800. line1Xmax = line1S.X > line1E.X ? line1S.X : line1E.X,
  801. line1Ymin = line1S.Y < line1E.Y ? line1S.Y : line1E.Y,
  802. line1Ymax = line1S.Y > line1E.Y ? line1S.Y : line1E.Y,
  803. line2Xmin = line2S.X < line2E.X ? line2S.X : line2E.X,
  804. line2Xmax = line2S.X > line2E.X ? line2S.X : line2E.X,
  805. line2Ymin = line2S.Y < line2E.Y ? line2S.Y : line2E.Y,
  806. line2Ymax = line2S.Y > line2E.Y ? line2S.Y : line2E.Y;
  807. if (line1S.X - line1E.X == 0)
  808. {
  809. // 两条线都垂直于X轴
  810. if (line2S.X - line2E.X == 0)
  811. {
  812. if (line1S.X != line2S.X) status = -1;
  813. else if ((line1Ymin > line2Ymax) || (line1Ymax < line2Ymin)) status = -1;
  814. else if (line1Ymin == line2Ymax)
  815. {
  816. interPoint = new PointF(line1S.X, line1Ymin); status = 1;
  817. }
  818. else if (line1Ymax == line2Ymin)
  819. {
  820. interPoint = new PointF(line1S.X, line1Ymax); status = 1;
  821. }
  822. else status = 2;
  823. }
  824. // line1垂直于X轴,line2不垂直于X轴
  825. else
  826. {
  827. float slope = (line2S.Y - line2E.Y) / (line2S.X - line2E.X);
  828. float offset = line2S.Y - slope * line2S.X;
  829. float newX = line1S.X, newY = slope * newX + offset;
  830. if (newX >= line2Xmin && newX <= line2Xmax && newY >= line1Ymin && newY <= line1Ymax
  831. && newY >= line2Ymin && newY <= line2Ymax)
  832. {
  833. interPoint = new PointF(newX, newY); status = 1;
  834. }
  835. }
  836. }
  837. else
  838. {
  839. // line1不垂直于X轴,line2垂直于X轴
  840. if (line2S.X - line2E.X == 0)
  841. {
  842. float slope = (line1S.Y - line1E.Y) / (line1S.X - line1E.X);
  843. float offset = line1S.Y - slope * line1S.X;
  844. float newX = line2S.X, newY = slope * newX + offset;
  845. if (newX >= line1Xmin && newX <= line1Xmax && newY >= line1Ymin && newY <= line1Ymax
  846. && newY >= line2Ymin && newY <= line2Ymax)
  847. {
  848. interPoint = new PointF(newX, newY); status = 1;
  849. }
  850. }
  851. // line1和line2都不垂直于X轴
  852. else
  853. {
  854. float slope1 = (line1S.Y - line1E.Y) / (line1S.X - line1E.X);
  855. float offset1 = line1S.Y - slope1 * line1S.X;
  856. float slope2 = (line2S.Y - line2E.Y) / (line2S.X - line2E.X);
  857. float offset2 = line2S.Y - slope2 * line2S.X;
  858. // 如果两条直线平行
  859. if (slope1 == slope2)
  860. {
  861. if (offset1 != offset2) status = -1;
  862. else if (line1Xmax == line2Xmin)
  863. {
  864. interPoint = new PointF(line1Xmax, line1Xmax * slope1 + offset1); status = 1;
  865. }
  866. else if (line1Xmin == line2Xmax)
  867. {
  868. interPoint = new PointF(line1Xmin, line1Xmin * slope1 + offset1); status = 1;
  869. }
  870. else if (line1Xmax < line2Xmin || line1Xmin > line2Xmax) status = -1;
  871. else status = 2;
  872. }
  873. else
  874. {
  875. float newX = (offset2 - offset1) / (slope1 - slope2), newY = newX * slope1 + offset1;
  876. if (newX >= line1Xmin && newX <= line1Xmax && newX >= line2Xmin && newX <= line2Xmax)
  877. {
  878. interPoint = new PointF(newX, newY); status = 1;
  879. }
  880. }
  881. }
  882. }
  883. if (status == 1)
  884. {
  885. return true;
  886. }
  887. else
  888. {
  889. return false;
  890. }
  891. }
  892. /// <summary>
  893. /// 获取Segment信息
  894. /// </summary>
  895. /// <param name="points"></param>
  896. /// <param name="SegmentClrList"></param>
  897. /// <param name="SegmentList"></param>
  898. public void GetSegment(List<Point> points, List<COTSSegmentClr> SegmentClrList, ref List<Segment> SegmentList)
  899. {
  900. List<int> segmentStart = new List<int>();
  901. List<int> segmentHeight = new List<int>();
  902. List<int> segmentLength = new List<int>();
  903. FindSegment(points, ref segmentStart, ref segmentHeight, ref segmentLength);
  904. for (int i = 0; i < segmentLength.Count; i++)
  905. {
  906. COTSSegmentClr seg = new COTSSegmentClr();
  907. seg.SetStart(segmentStart[i]);
  908. seg.SetHeight(segmentHeight[i]);
  909. seg.SetLength(segmentLength[i]);
  910. SegmentClrList.Add(seg);
  911. Segment segment = new Segment();
  912. segment.Start = segmentStart[i];
  913. segment.Height = segmentHeight[i];
  914. segment.Length = segmentLength[i];
  915. segment.SegmentNum = segmentLength.Count;
  916. segment.SegmentId = i;
  917. segment.FieldId = m_ParticleSegmentation.ParticleData.FieldId;
  918. segment.XRayId = m_ParticleSegmentation.ParticleData.XrayId;
  919. segment.ParticleId = m_ParticleSegmentation.ParticleData.ParticleId;
  920. SegmentList.Add(segment);
  921. }
  922. }
  923. /// <summary>
  924. /// 寻找segment方法
  925. /// </summary>
  926. public void FindSegment(List<Point> point_list_final, ref List<int> segmentStart, ref List<int> segmentHeight, ref List<int> segmentLength)
  927. {
  928. int flag = 0;
  929. point_list_final.Sort(my_sort);//排序
  930. for (int k = 0; k < point_list_final.Count(); k++)
  931. {
  932. if (k == 0 && k != point_list_final.Count() - 1)//第一个颗粒
  933. {
  934. flag++;
  935. segmentStart.Add(point_list_final[k].X);
  936. segmentHeight.Add(point_list_final[k].Y);
  937. }
  938. else if (k == 0 && k == point_list_final.Count() - 1)//只有一个颗粒
  939. {
  940. segmentStart.Add(point_list_final[k].X);
  941. segmentHeight.Add(point_list_final[k].Y);
  942. segmentLength.Add(1);
  943. }
  944. else if (k == point_list_final.Count() - 1)//最后一个颗粒
  945. {
  946. flag++;
  947. segmentLength.Add(flag);
  948. }
  949. else if (point_list_final[k].Y == point_list_final[k - 1].Y && point_list_final[k].X == point_list_final[k - 1].X + 1)//同行连续
  950. {
  951. flag++;
  952. }
  953. else if (point_list_final[k].Y == point_list_final[k - 1].Y && point_list_final[k].X != point_list_final[k - 1].X + 1)//同行隔断
  954. {
  955. segmentLength.Add(flag);
  956. flag = 1;
  957. segmentStart.Add(point_list_final[k].X);
  958. segmentHeight.Add(point_list_final[k].Y);
  959. }
  960. else if (point_list_final[k].Y != point_list_final[k - 1].Y)//不同行
  961. {
  962. segmentLength.Add(flag);
  963. flag = 1;
  964. segmentStart.Add(point_list_final[k].X);
  965. segmentHeight.Add(point_list_final[k].Y);
  966. }
  967. }
  968. }
  969. /// <summary>
  970. /// 排序方法
  971. /// </summary>
  972. /// <param name="p1"></param>
  973. /// <param name="p2"></param>
  974. /// <returns></returns>
  975. private int my_sort(Point p1, Point p2)
  976. {
  977. return p1.Y == p2.Y ? p1.X.CompareTo(p2.X) : p1.Y.CompareTo(p2.Y); //升序排序
  978. }
  979. public object CloneObject(object obj)
  980. {
  981. using (MemoryStream stream = new MemoryStream())
  982. {
  983. BinaryFormatter formatter = new BinaryFormatter();
  984. stream.Position = 0;
  985. formatter.Serialize(stream, obj);
  986. stream.Position = 0;
  987. return formatter.Deserialize(stream);
  988. }
  989. }
  990. #endregion
  991. }
  992. }