OTSImageDisHelp.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 void ConnectToSEM()
  207. {
  208. log.Trace("(Connection_ForDrawDistrbutionImageAndBSE)" + "Connect to SEM");
  209. if (!m_SEMConnectionState)
  210. {
  211. //和电镜建立通讯连接
  212. m_SEMConnectionState = m_cfun.Connect();
  213. log.Trace("(Connection_ForDrawDistrbutionImageAndBSE)" + "Connect to SEM" + ":--" + m_SEMConnectionState + "---");
  214. }
  215. else
  216. {
  217. log.Trace("(Connection_ForDrawDistrbutionImageAndBSE)" + ":allready connected, state:" + m_SEMConnectionState);
  218. //断开电镜连接
  219. }
  220. }
  221. public void DisConnectSEM()
  222. {
  223. m_SEMConnectionState = false;
  224. m_cfun.DisConnect();
  225. }
  226. /// <summary>
  227. /// 移动电镜到指定的X,Y坐标上,R坐标使用原先的值进行移动
  228. /// </summary>
  229. /// <param name="PositionX"></param>
  230. /// <param name="PositionY"></param>
  231. public void MoveSemToPointXY(double in_PositionX, double in_PositionY)
  232. {
  233. log.Trace("Begin MoveSemToPointXY:(" +in_PositionX.ToString()+","+in_PositionY.ToString()+")");
  234. //首先获取电镜当前的位置,并记录原R值
  235. double ls_PositionX = 0;
  236. double ls_PositionY = 0;
  237. double ls_PositionR = 0;
  238. if (m_SEMConnectionState)
  239. {
  240. m_cfun.GetSemPositionXY(ref ls_PositionX, ref ls_PositionY, ref ls_PositionR);
  241. }
  242. else
  243. {
  244. log.Error("Failed to GetSemPositionXY");
  245. return;
  246. }
  247. if (m_SEMConnectionState)
  248. {
  249. m_cfun.MoveSEMToPoint(new Point((int)in_PositionX, (int)in_PositionY), ls_PositionR);
  250. }
  251. }
  252. #endregion
  253. #region //--------------------------------------颗粒分布图相关部份---------------------------------------------------------------------
  254. /// <summary>
  255. /// 传入颗粒的tagid和fieldid,来获取该颗粒下对应的xray数据
  256. /// </summary>
  257. /// <param name="in_clr_tagid"></param>
  258. /// <param name="in_clr_fieldid"></param>
  259. /// <param name="Search_xray"></param>
  260. /// <param name="Analysis_xray"></param>
  261. 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)
  262. {
  263. Search_xray = new uint[2000];
  264. Analysis_xray = new uint[2000];
  265. xray_id = 0;
  266. list_celementchemistryclr = new List<Element>();
  267. //防止为空校验判断
  268. if (resultFile.List_OTSField == null)
  269. return;
  270. Particle particle = resultFile.List_OTSField.Find(x => x.FieldID == in_clr_fieldid).ParticleList.Find(x => x.ParticleId == in_clr_tagid);
  271. var tmpPart = new ParticleData(resultFile.FilePath).GetParticleXrayDataByFidAndPid(Convert.ToString(particle.FieldId), Convert.ToString(particle.XrayId));
  272. if (tmpPart != null)
  273. {
  274. particle.XRayData = tmpPart.XRayData;
  275. if (particle.XrayId > -1)
  276. {
  277. for (int i = 0; i < 2000; i++)
  278. {
  279. Analysis_xray[i] = BitConverter.ToUInt32(particle.XRayData, i * 4);
  280. }
  281. Search_xray = Analysis_xray;
  282. xray_id = particle.XrayId;
  283. list_celementchemistryclr = particle.ElementList;
  284. }
  285. }
  286. }
  287. public bool DeleteParticlesFromLibrary(int in_clr_tagid, int in_clr_fieldid)
  288. {
  289. bool DoesItExist = false;
  290. for (int i=1;i<201;i++)
  291. {
  292. if (System.IO.Directory.Exists(resultFile.FilePath + "\\FIELD_FILES\\Backups"+i.ToString()))
  293. {
  294. DoesItExist = true;
  295. break;
  296. }
  297. }
  298. if (!DoesItExist)
  299. {
  300. //备份数据库
  301. BackupDatabase();
  302. }
  303. DoesItExist = false;
  304. //防止为空校验判断
  305. if (resultFile.List_OTSField == null)
  306. return false;
  307. Particle particle = resultFile.List_OTSField.Find(x => x.FieldID == in_clr_fieldid).ParticleList.Find(x => x.ParticleId == in_clr_tagid);
  308. if (new ParticleData(resultFile.FilePath).DeleteFromData(Convert.ToString(particle.FieldId), Convert.ToString(particle.XrayId)))
  309. {
  310. return true;
  311. }else
  312. {
  313. return false;
  314. }
  315. }
  316. public bool BackupDatabase()
  317. {
  318. //创建备份数据库文件夹
  319. string sourcePath = resultFile.FilePath + "\\FIELD_FILES\\Inclusion.db";//源文件路径
  320. string sourceName = "Inclusion.db";//源文件名称
  321. string folderPath = resultFile.FilePath + "\\FIELD_FILES\\Backups";//目标文件夹
  322. string Fields = "";
  323. for (int i = 1; i < 201; i++)
  324. {
  325. if (System.IO.Directory.Exists(folderPath + i.ToString()))
  326. {
  327. Fields = folderPath + i.ToString();
  328. break;
  329. }
  330. else
  331. {
  332. System.IO.Directory.CreateDirectory(folderPath + i.ToString());
  333. Fields = folderPath + i.ToString();
  334. break;
  335. }
  336. }
  337. if (Fields == "")
  338. {
  339. return false;
  340. }
  341. //复制数据库文件
  342. string targetPath = Path.Combine(Fields, sourceName);
  343. FileInfo file = new FileInfo(sourcePath);
  344. if (file.Exists)
  345. {
  346. file.CopyTo(targetPath, true);
  347. }
  348. return true;
  349. }
  350. /// <summary>
  351. /// 传入所有的物理field坐标点,和单个物理field的宽高,返回所有field的左上角位置,和整个field组成的rect大小
  352. /// </summary>
  353. /// <param name="in_list_point"></param>
  354. /// <param name="in_width"></param>
  355. /// <param name="in_height"></param>
  356. /// <returns></returns>
  357. public Rectangle GetWlRectTopLeftAndRect(List<Point> in_list_point, int in_width, int in_height)
  358. {
  359. //分别获取整个rect的xy最小值和最大值
  360. int i_rect_x_min = 100000000;
  361. int i_rect_y_min = 100000000;
  362. int i_rect_x_max = -100000000;
  363. int i_rect_y_max = -100000000;
  364. for (int i = 0; i < in_list_point.Count; i++)
  365. {
  366. if (i_rect_x_min > in_list_point[i].X)
  367. i_rect_x_min = in_list_point[i].X;
  368. if (i_rect_y_min > in_list_point[i].Y)
  369. i_rect_y_min = in_list_point[i].Y;
  370. if (i_rect_x_max < in_list_point[i].X)
  371. i_rect_x_max = in_list_point[i].X;
  372. if (i_rect_y_max < in_list_point[i].Y)
  373. i_rect_y_max = in_list_point[i].Y;
  374. }
  375. Rectangle ret_rect = new Rectangle(i_rect_x_min, i_rect_y_min,
  376. i_rect_x_max - i_rect_x_min, i_rect_y_max - i_rect_y_min);
  377. return ret_rect;
  378. }
  379. /// <summary>
  380. /// 根据Field的ID,来获取Field列表中对应FIeld的OTS 坐标
  381. /// </summary>
  382. /// <param name="in_fieldid"></param>
  383. /// <returns></returns>
  384. public Point GetOTSPointByFieldID(List<DisplayField> in_list_dfield, int in_fieldid)
  385. {
  386. Point ret_point = new Point(0, 0);
  387. for (int i = 0; i < in_list_dfield.Count; i++)
  388. {
  389. //这里TagID先代表的是底层返回的ID
  390. if (in_list_dfield[i].FieldID == in_fieldid.ToString())
  391. {
  392. ret_point = new Point(Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.X), Convert.ToInt32(in_list_dfield[i].OTSCoordinatePos.Y));
  393. }
  394. }
  395. return ret_point;
  396. }
  397. /// <summary>
  398. /// 将OTS坐标转换为Sem 坐标
  399. /// </summary>
  400. /// <param name="POTSCoord"></param>
  401. /// <returns></returns>
  402. public Point ChangeOTSToSemCoord(Point POTSCoord)
  403. {
  404. //first if m_semstagedata is null to get stage inforation
  405. Convert.ToDouble(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["scanFieldSize"]);
  406. //after obtaining stage info,calc stage point data
  407. Point ret_SEM_point = new Point();
  408. // get center point, um
  409. long xStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["XAxis"])["start"]);
  410. long xEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["XAxis"])["end"]);
  411. long xCenter = (xStart + xEnd) / 2;
  412. long yStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["YAxis"])["start"]);
  413. long yEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["Members"])["YAxis"])["end"]);
  414. long yCenter = (yStart + yEnd) / 2;
  415. // delte = SEM - OTSa
  416. long deltex = xCenter - 0;
  417. long deltey = yCenter - 0;
  418. int xdir = Convert.ToInt32(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["xAxisDir"]);
  419. int ydir = Convert.ToInt32(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["yAxisDir"]);
  420. if (xdir == (int)OTS_X_AXIS_DIRECTION.LEFT_TOWARD)
  421. {
  422. ret_SEM_point.X = -1 * (POTSCoord.X - Convert.ToInt32(deltex));
  423. }
  424. else if (xdir == (int)OTS_X_AXIS_DIRECTION.RIGHT_TOWARD)
  425. {
  426. ret_SEM_point.X = POTSCoord.X + Convert.ToInt32(deltex);
  427. }
  428. if (ydir == (int)OTS_Y_AXIS_DIRECTION.UP_TOWARD)
  429. {
  430. ret_SEM_point.Y = POTSCoord.Y + Convert.ToInt32(deltey);
  431. }
  432. else if (ydir == (int)OTS_Y_AXIS_DIRECTION.DOWN_TOWARD)
  433. {
  434. ret_SEM_point.Y = -1 * (POTSCoord.Y - Convert.ToInt32(deltey));
  435. }
  436. return ret_SEM_point;
  437. }
  438. #endregion
  439. /// <summary>
  440. /// 判断该点是否在多边形的范围内
  441. /// </summary>
  442. /// <param name="inPoints"></param>
  443. /// <param name="WhetherPoint"></param>
  444. /// <returns></returns>
  445. public bool WhetherInRange(DisplayParticle Part,/*PointF[] inPoints,*/ Point WhetherPoint)
  446. {
  447. var rect = Part.Rect;
  448. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  449. {
  450. var itm = (BaseObject)Part;
  451. itm.GPath = Part.GetRegionFromDSegments();
  452. PointF[] inPoints = itm.GPath.PathPoints;
  453. bool b_inrange = false;
  454. GraphicsPath myGraphicsPath = new GraphicsPath();
  455. Region myRegion = new Region();
  456. myGraphicsPath.Reset();
  457. myGraphicsPath.AddPolygon(inPoints);
  458. myRegion.MakeEmpty();
  459. myRegion.Union(myGraphicsPath);
  460. //返回判断点是否在多边形里
  461. b_inrange = myRegion.IsVisible(WhetherPoint);
  462. return b_inrange;
  463. }
  464. else
  465. {
  466. return false;
  467. }
  468. }
  469. public bool WhetherInRange(RectangleF rec,PointF[] inPoints, Point WhetherPoint)
  470. {
  471. var rect = rec;
  472. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  473. {
  474. //var itm = (BaseObject)Part;
  475. //PointF[] inPoints = itm.GPath.PathPoints;
  476. bool b_inrange = false;
  477. GraphicsPath myGraphicsPath = new GraphicsPath();
  478. Region myRegion = new Region();
  479. myGraphicsPath.Reset();
  480. myGraphicsPath.AddPolygon(inPoints);
  481. myRegion.MakeEmpty();
  482. myRegion.Union(myGraphicsPath);
  483. //返回判断点是否在多边形里
  484. b_inrange = myRegion.IsVisible(WhetherPoint);
  485. return b_inrange;
  486. }
  487. else
  488. {
  489. return false;
  490. }
  491. }
  492. /// <summary>
  493. /// 判断该点是否在多边形的范围内的float版本重载
  494. /// </summary>
  495. /// <param name="inPoints"></param>
  496. /// <param name="WhetherPoint"></param>
  497. /// <returns></returns>
  498. public bool WhetherInRange(DisplayParticle Part,/* PointF[] inPoints, */PointF WhetherPoint)
  499. {
  500. var rect = Part.Rect;
  501. if ((rect.Left < WhetherPoint.X && WhetherPoint.X < rect.Right) && (rect.Top < WhetherPoint.Y && WhetherPoint.Y < rect.Bottom))
  502. {
  503. var itm = (BaseObject)Part;
  504. PointF[] inPoints = itm.GPath.PathPoints;
  505. bool b_inrange = false;
  506. GraphicsPath myGraphicsPath = new GraphicsPath();
  507. Region myRegion = new Region();
  508. myGraphicsPath.Reset();
  509. myGraphicsPath.AddPolygon(inPoints);
  510. myRegion.MakeEmpty();
  511. myRegion.Union(myGraphicsPath);
  512. //返回判断点是否在多边形里
  513. b_inrange = myRegion.IsVisible(WhetherPoint);
  514. return b_inrange;
  515. }
  516. else
  517. {
  518. return false;
  519. }
  520. }
  521. #region 颗粒分割功能方法
  522. /// <summary>
  523. /// 获取线段与矩形的两个交点
  524. /// </summary>
  525. /// <param name="line1S"></param>
  526. /// <param name="line1E"></param>
  527. /// <param name="rectangle"></param>
  528. /// <param name="pointList"></param>
  529. /// <returns></returns>
  530. public bool GetInterBetweenLinesAndRect(Point line1S, Point line1E, RectangleF rectangle, ref List<Point> pointList)
  531. {
  532. try
  533. {
  534. PointF pointF = new PointF(-1, -1);
  535. Rect rect = new Rect();
  536. rect.X = m_ParticleSegmentation.ParticleData.RectLeft;
  537. rect.Y = m_ParticleSegmentation.ParticleData.RectTop;
  538. rect.Width = m_ParticleSegmentation.ParticleData.RectWidth;
  539. rect.Height = m_ParticleSegmentation.ParticleData.RectHeight;
  540. float border = 0;
  541. if (GetInterBetweenTwoLines(line1S, line1E, new PointF(rectangle.Left, rectangle.Top), new PointF(rectangle.Right, rectangle.Top), ref pointF))
  542. {
  543. border = (pointF.X - rectangle.Left) / (rectangle.Right - rectangle.Left);
  544. pointList.Add(new Point((int)(rect.X + rect.Width * border), rect.Y));
  545. }
  546. if (GetInterBetweenTwoLines(line1S, line1E,
  547. new PointF(rectangle.Left, rectangle.Bottom), new PointF(rectangle.Right, rectangle.Bottom), ref pointF))
  548. {
  549. border = (pointF.X - rectangle.Left) / (rectangle.Right - rectangle.Left);
  550. pointList.Add(new Point((int)(rect.X + rect.Width * border), rect.Y + rect.Height));
  551. }
  552. if (GetInterBetweenTwoLines(line1S, line1E,
  553. new PointF(rectangle.Left, rectangle.Top), new PointF(rectangle.Left, rectangle.Bottom), ref pointF))
  554. {
  555. border = (pointF.Y - rectangle.Top) / (rectangle.Bottom - rectangle.Top);
  556. pointList.Add(new Point(rect.X, (int)(rect.Y + rect.Height * border)));
  557. }
  558. if (GetInterBetweenTwoLines(line1S, line1E,
  559. new PointF(rectangle.Right, rectangle.Top), new PointF(rectangle.Right, rectangle.Bottom), ref pointF))
  560. {
  561. border = (pointF.Y - rectangle.Top) / (rectangle.Bottom - rectangle.Top);
  562. pointList.Add(new Point(rect.X + rect.Width, (int)(rect.Y + rect.Height * border)));
  563. }
  564. if (pointList.Count == 2)
  565. {
  566. return true;
  567. }
  568. else
  569. {
  570. return false;
  571. }
  572. }
  573. catch (Exception ex)
  574. {
  575. log.Trace("(GetSplitPartFun)" + ex);
  576. return false;
  577. }
  578. }
  579. /// <summary>
  580. /// 获取分离颗粒方法
  581. /// </summary>
  582. /// <param name="startPoint"></param>
  583. /// <param name="endPoint"></param>
  584. /// <returns></returns>
  585. public bool GetSplitPartFun(Point startPoint, Point endPoint)
  586. {
  587. try
  588. {
  589. CImageHandler m_ImagePro = new CImageHandler();
  590. Particle particle1 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
  591. Particle particle2 = (Particle)CloneObject(m_ParticleSegmentation.ParticleData);
  592. Dictionary<string, object> sampleMembers = ((Dictionary<string, object>)((Dictionary<string, object>)resultFile.ResultInfo["Sample"])["Members"]);
  593. Dictionary<string, object> imageScanParam = (Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)sampleMembers["MsrParams"])["Members"])["ImageScanParam"];
  594. double d_scanFieldSize_width = Convert.ToDouble(((Dictionary<string, object>)resultFile.ResultInfo["SEMStageData"])["scanFieldSize"]);
  595. string ImageResolution = imageScanParam["ImageResolution"].ToString();
  596. int width = int.Parse(ImageResolution.Split('_')[1]);
  597. int height = int.Parse(ImageResolution.Split('_')[2]);
  598. double dPixelSize = d_scanFieldSize_width / width;
  599. using (Mat mat = new Mat(height, width, MatType.CV_8UC1, Scalar.Black))//黑色底图
  600. using (Mat labelMat = new Mat())
  601. using (Mat stats = new Mat())
  602. using (Mat centroids = new Mat())
  603. using (Mat matBse = new Mat(resultFile.List_OTSField[particle1.FieldId].FieldImage, ImreadModes.Grayscale))
  604. {
  605. foreach (Segment segment in m_ParticleSegmentation.ParticleData.SegmentList)
  606. {
  607. Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
  608. }
  609. //寻找坐标点
  610. List<Point> points1 = new List<Point>();
  611. List<Point> points2 = new List<Point>();
  612. List<int> aveGray1 = new List<int>();
  613. List<int> aveGray2 = new List<int>();
  614. for (int k = 0; k < mat.Height; k++)
  615. {
  616. for (int j = 0; j < mat.Width; j++)
  617. {
  618. if (mat.Get<int>(k, j) != 0)
  619. {
  620. int side = (startPoint.X - j) * (endPoint.Y - k) - (startPoint.Y - k) * (endPoint.X - j);
  621. if (side >= 0)//区分像素位置
  622. {
  623. points1.Add(new Point(j, k));
  624. aveGray1.Add(matBse.Get<byte>(k, j));
  625. }
  626. else
  627. {
  628. points2.Add(new Point(j, k));
  629. aveGray2.Add(matBse.Get<byte>(k, j));
  630. }
  631. }
  632. }
  633. }
  634. //处理Segment
  635. List<COTSSegmentClr> SegmentClrList1 = new List<COTSSegmentClr>();
  636. List<COTSSegmentClr> SegmentClrList2 = new List<COTSSegmentClr>();
  637. List<Segment> SegmentList1 = new List<Segment>();
  638. List<Segment> SegmentList2 = new List<Segment>();
  639. GetSegment(points1, SegmentClrList1, ref SegmentList1);
  640. GetSegment(points2, SegmentClrList2, ref SegmentList2);
  641. //颗粒一
  642. Cv2.Threshold(mat, mat, 0, 0, ThresholdTypes.Binary);
  643. foreach (Segment segment in SegmentList1)
  644. {
  645. Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
  646. }
  647. Cv2.ConnectedComponentsWithStats(mat, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  648. CopyToPart(particle1, SegmentList1, SegmentClrList1, aveGray1, stats, centroids, dPixelSize);
  649. //颗粒二
  650. Cv2.Threshold(mat, mat, 0, 0, ThresholdTypes.Binary);
  651. foreach (Segment segment in SegmentList2)
  652. {
  653. Cv2.Line(mat, new OpenCvSharp.Point(segment.Start, segment.Height), new OpenCvSharp.Point(segment.Start + segment.Length, segment.Height), Scalar.White, 1, LineTypes.AntiAlias);
  654. }
  655. Cv2.ConnectedComponentsWithStats(mat, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  656. CopyToPart(particle2, SegmentList2, SegmentClrList2, aveGray2, stats, centroids, dPixelSize);
  657. if (!SaveToDb(particle1, particle2))
  658. {
  659. log.Trace("(GetSplitPartFun) SaveToDb Faild");
  660. return false;
  661. }
  662. }
  663. return true;
  664. }
  665. catch (Exception ex)
  666. {
  667. log.Trace("(GetSplitPartFun)" + ex);
  668. return false;
  669. }
  670. }
  671. /// <summary>
  672. /// 拷贝颗粒数据
  673. /// </summary>
  674. /// <param name="particle"></param>
  675. /// <param name="segments"></param>
  676. /// <param name="SegmentClrList"></param>
  677. /// <param name="aveGray"></param>
  678. /// <param name="stats"></param>
  679. /// <param name="centroids"></param>
  680. /// <param name="dPixelSize"></param>
  681. private void CopyToPart(Particle particle, List<Segment> segments, List<COTSSegmentClr> SegmentClrList, List<int> aveGray, Mat stats, Mat centroids, double dPixelSize)
  682. {
  683. CImageHandler m_ImagePro = new CImageHandler();
  684. COTSParticleClr part = new COTSParticleClr();
  685. part.SetArea(stats.At<int>(1, 4) * dPixelSize * dPixelSize);
  686. part.SetParticleRect(new Rectangle(stats.At<int>(1, 0), stats.At<int>(1, 1), stats.At<int>(1, 2), stats.At<int>(1, 3)));
  687. part.GetFeature().SetSegmentsList(SegmentClrList, false);
  688. m_ImagePro.CalParticleImageProp(part, dPixelSize);
  689. particle.SegmentList = segments;
  690. particle.SegmentNum = segments.Count;
  691. particle.AveGray = (int)aveGray.Average();
  692. particle.RectLeft = stats.At<int>(1, 0);
  693. particle.RectTop = stats.At<int>(1, 1);
  694. particle.RectWidth = stats.At<int>(1, 2);
  695. particle.RectHeight = stats.At<int>(1, 3);
  696. particle.Area = stats.At<int>(1, 4) * dPixelSize * dPixelSize;
  697. particle.PosX = (int)centroids.At<double>(1, 0);
  698. particle.PosY = (int)centroids.At<double>(1, 1);
  699. particle.DFERET = part.GetFeretDiameter();
  700. particle.DMAX = part.GetDMAX();
  701. particle.DMIN = part.GetDMIN();
  702. particle.DPERP = part.GetDMPERP();
  703. particle.PERIMETER = part.GetDPRIMETER();
  704. particle.ORIENTATION = part.GetORIENTATION();
  705. particle.DINSCR = part.GetDINSCR();
  706. particle.DMEAN = part.GetDMEAN();
  707. particle.DELONG = part.GetDELONG();
  708. }
  709. /// <summary>
  710. /// 保存数据库
  711. /// </summary>
  712. /// <param name="particle1"></param>
  713. /// <param name="particle2"></param>
  714. /// <returns></returns>
  715. public bool SaveToDb(Particle particle1, Particle particle2)
  716. {
  717. //初始化
  718. SQLiteHelper sQLiteHelper = new SQLiteHelper(resultFile.FilePath + "\\FIELD_FILES\\Inclusion.db");
  719. sQLiteHelper.GetDBConnection();
  720. sQLiteHelper.BeginTransaction();
  721. //修改Segment表
  722. if (particle1.SegmentList.Count == 0 || particle2.SegmentList.Count == 0)
  723. {
  724. return false;
  725. }
  726. int SegmentCount = sQLiteHelper.ExecuteNewPartIdForTransaction() + 1;
  727. if (!sQLiteHelper.ExecuteSegmentForTransaction(particle1, particle2, SegmentCount))//修改Segment表
  728. {
  729. return false;
  730. }
  731. if (!sQLiteHelper.ExecuteXrayForTransaction(particle1, particle2, SegmentCount))//修改XRayData和PoxXrayInfo表
  732. {
  733. return false;
  734. }
  735. if (!sQLiteHelper.ExecuteIncAForTransaction(particle1, particle2, SegmentCount))//修改IncAData表
  736. {
  737. return false;
  738. }
  739. sQLiteHelper.CommitTransaction();
  740. return true;
  741. }
  742. /// <summary>
  743. /// 线段与矩形是否相交
  744. /// </summary>
  745. /// <param name="linePointX1"></param>
  746. /// <param name="linePointY1"></param>
  747. /// <param name="linePointX2"></param>
  748. /// <param name="linePointY2"></param>
  749. /// <param name="rectangleLeftTopX"></param>
  750. /// <param name="rectangleLeftTopY"></param>
  751. /// <param name="rectangleRightBottomX"></param>
  752. /// <param name="rectangleRightBottomY"></param>
  753. /// <returns></returns>
  754. public bool isLineIntersectRectangle(float linePointX1,
  755. float linePointY1,
  756. float linePointX2,
  757. float linePointY2,
  758. float rectangleLeftTopX,
  759. float rectangleLeftTopY,
  760. float rectangleRightBottomX,
  761. float rectangleRightBottomY)
  762. {
  763. float lineHeight = linePointY1 - linePointY2;
  764. float lineWidth = linePointX2 - linePointX1; // 计算叉乘
  765. float c = linePointX1 * linePointY2 - linePointX2 * linePointY1;
  766. if ((lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c <= 0)
  767. || (lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c >= 0)
  768. || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c <= 0)
  769. || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c >= 0))
  770. {
  771. if (rectangleLeftTopX > rectangleRightBottomX)
  772. {
  773. float temp = rectangleLeftTopX;
  774. rectangleLeftTopX = rectangleRightBottomX;
  775. rectangleRightBottomX = temp;
  776. }
  777. if (rectangleLeftTopY < rectangleRightBottomY)
  778. {
  779. float temp1 = rectangleLeftTopY;
  780. rectangleLeftTopY = rectangleRightBottomY;
  781. rectangleRightBottomY = temp1;
  782. }
  783. if ((linePointX1 < rectangleLeftTopX && linePointX2 < rectangleLeftTopX)
  784. || (linePointX1 > rectangleRightBottomX && linePointX2 > rectangleRightBottomX)
  785. || (linePointY1 > rectangleLeftTopY && linePointY2 > rectangleLeftTopY)
  786. || (linePointY1 < rectangleRightBottomY && linePointY2 < rectangleRightBottomY))
  787. {
  788. return false;
  789. }
  790. else
  791. {
  792. return true;
  793. }
  794. }
  795. else
  796. {
  797. return false;
  798. }
  799. }
  800. /// <summary>
  801. /// 求一个线段与另一个线段的交点
  802. /// </summary>
  803. /// <param name="line1S"></param>
  804. /// <param name="line1E"></param>
  805. /// <param name="line2S"></param>
  806. /// <param name="line2E"></param>
  807. /// <param name="interPoint"></param>
  808. /// <returns>-1:不存在交点;1:存在一个交点;2:存在无穷多个交点(重合或部分重合)</returns>
  809. public bool GetInterBetweenTwoLines(PointF line1S, PointF line1E,
  810. PointF line2S, PointF line2E, ref PointF interPoint)
  811. {
  812. int status = -1;
  813. // 判断两条直线各自的矩形包围盒的X与Y的值范围
  814. float line1Xmin = line1S.X < line1E.X ? line1S.X : line1E.X,
  815. line1Xmax = line1S.X > line1E.X ? line1S.X : line1E.X,
  816. line1Ymin = line1S.Y < line1E.Y ? line1S.Y : line1E.Y,
  817. line1Ymax = line1S.Y > line1E.Y ? line1S.Y : line1E.Y,
  818. line2Xmin = line2S.X < line2E.X ? line2S.X : line2E.X,
  819. line2Xmax = line2S.X > line2E.X ? line2S.X : line2E.X,
  820. line2Ymin = line2S.Y < line2E.Y ? line2S.Y : line2E.Y,
  821. line2Ymax = line2S.Y > line2E.Y ? line2S.Y : line2E.Y;
  822. if (line1S.X - line1E.X == 0)
  823. {
  824. // 两条线都垂直于X轴
  825. if (line2S.X - line2E.X == 0)
  826. {
  827. if (line1S.X != line2S.X) status = -1;
  828. else if ((line1Ymin > line2Ymax) || (line1Ymax < line2Ymin)) status = -1;
  829. else if (line1Ymin == line2Ymax)
  830. {
  831. interPoint = new PointF(line1S.X, line1Ymin); status = 1;
  832. }
  833. else if (line1Ymax == line2Ymin)
  834. {
  835. interPoint = new PointF(line1S.X, line1Ymax); status = 1;
  836. }
  837. else status = 2;
  838. }
  839. // line1垂直于X轴,line2不垂直于X轴
  840. else
  841. {
  842. float slope = (line2S.Y - line2E.Y) / (line2S.X - line2E.X);
  843. float offset = line2S.Y - slope * line2S.X;
  844. float newX = line1S.X, newY = slope * newX + offset;
  845. if (newX >= line2Xmin && newX <= line2Xmax && newY >= line1Ymin && newY <= line1Ymax
  846. && newY >= line2Ymin && newY <= line2Ymax)
  847. {
  848. interPoint = new PointF(newX, newY); status = 1;
  849. }
  850. }
  851. }
  852. else
  853. {
  854. // line1不垂直于X轴,line2垂直于X轴
  855. if (line2S.X - line2E.X == 0)
  856. {
  857. float slope = (line1S.Y - line1E.Y) / (line1S.X - line1E.X);
  858. float offset = line1S.Y - slope * line1S.X;
  859. float newX = line2S.X, newY = slope * newX + offset;
  860. if (newX >= line1Xmin && newX <= line1Xmax && newY >= line1Ymin && newY <= line1Ymax
  861. && newY >= line2Ymin && newY <= line2Ymax)
  862. {
  863. interPoint = new PointF(newX, newY); status = 1;
  864. }
  865. }
  866. // line1和line2都不垂直于X轴
  867. else
  868. {
  869. float slope1 = (line1S.Y - line1E.Y) / (line1S.X - line1E.X);
  870. float offset1 = line1S.Y - slope1 * line1S.X;
  871. float slope2 = (line2S.Y - line2E.Y) / (line2S.X - line2E.X);
  872. float offset2 = line2S.Y - slope2 * line2S.X;
  873. // 如果两条直线平行
  874. if (slope1 == slope2)
  875. {
  876. if (offset1 != offset2) status = -1;
  877. else if (line1Xmax == line2Xmin)
  878. {
  879. interPoint = new PointF(line1Xmax, line1Xmax * slope1 + offset1); status = 1;
  880. }
  881. else if (line1Xmin == line2Xmax)
  882. {
  883. interPoint = new PointF(line1Xmin, line1Xmin * slope1 + offset1); status = 1;
  884. }
  885. else if (line1Xmax < line2Xmin || line1Xmin > line2Xmax) status = -1;
  886. else status = 2;
  887. }
  888. else
  889. {
  890. float newX = (offset2 - offset1) / (slope1 - slope2), newY = newX * slope1 + offset1;
  891. if (newX >= line1Xmin && newX <= line1Xmax && newX >= line2Xmin && newX <= line2Xmax)
  892. {
  893. interPoint = new PointF(newX, newY); status = 1;
  894. }
  895. }
  896. }
  897. }
  898. if (status == 1)
  899. {
  900. return true;
  901. }
  902. else
  903. {
  904. return false;
  905. }
  906. }
  907. /// <summary>
  908. /// 获取Segment信息
  909. /// </summary>
  910. /// <param name="points"></param>
  911. /// <param name="SegmentClrList"></param>
  912. /// <param name="SegmentList"></param>
  913. public void GetSegment(List<Point> points, List<COTSSegmentClr> SegmentClrList, ref List<Segment> SegmentList)
  914. {
  915. List<int> segmentStart = new List<int>();
  916. List<int> segmentHeight = new List<int>();
  917. List<int> segmentLength = new List<int>();
  918. FindSegment(points, ref segmentStart, ref segmentHeight, ref segmentLength);
  919. for (int i = 0; i < segmentLength.Count; i++)
  920. {
  921. COTSSegmentClr seg = new COTSSegmentClr();
  922. seg.SetStart(segmentStart[i]);
  923. seg.SetHeight(segmentHeight[i]);
  924. seg.SetLength(segmentLength[i]);
  925. SegmentClrList.Add(seg);
  926. Segment segment = new Segment();
  927. segment.Start = segmentStart[i];
  928. segment.Height = segmentHeight[i];
  929. segment.Length = segmentLength[i];
  930. segment.SegmentNum = segmentLength.Count;
  931. segment.SegmentId = i;
  932. segment.FieldId = m_ParticleSegmentation.ParticleData.FieldId;
  933. segment.XRayId = m_ParticleSegmentation.ParticleData.XrayId;
  934. segment.ParticleId = m_ParticleSegmentation.ParticleData.ParticleId;
  935. SegmentList.Add(segment);
  936. }
  937. }
  938. /// <summary>
  939. /// 寻找segment方法
  940. /// </summary>
  941. public void FindSegment(List<Point> point_list_final, ref List<int> segmentStart, ref List<int> segmentHeight, ref List<int> segmentLength)
  942. {
  943. int flag = 0;
  944. point_list_final.Sort(my_sort);//排序
  945. for (int k = 0; k < point_list_final.Count(); k++)
  946. {
  947. if (k == 0 && k != point_list_final.Count() - 1)//第一个颗粒
  948. {
  949. flag++;
  950. segmentStart.Add(point_list_final[k].X);
  951. segmentHeight.Add(point_list_final[k].Y);
  952. }
  953. else if (k == 0 && k == point_list_final.Count() - 1)//只有一个颗粒
  954. {
  955. segmentStart.Add(point_list_final[k].X);
  956. segmentHeight.Add(point_list_final[k].Y);
  957. segmentLength.Add(1);
  958. }
  959. else if (k == point_list_final.Count() - 1)//最后一个颗粒
  960. {
  961. flag++;
  962. segmentLength.Add(flag);
  963. }
  964. 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)//同行连续
  965. {
  966. flag++;
  967. }
  968. 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)//同行隔断
  969. {
  970. segmentLength.Add(flag);
  971. flag = 1;
  972. segmentStart.Add(point_list_final[k].X);
  973. segmentHeight.Add(point_list_final[k].Y);
  974. }
  975. else if (point_list_final[k].Y != point_list_final[k - 1].Y)//不同行
  976. {
  977. segmentLength.Add(flag);
  978. flag = 1;
  979. segmentStart.Add(point_list_final[k].X);
  980. segmentHeight.Add(point_list_final[k].Y);
  981. }
  982. }
  983. }
  984. /// <summary>
  985. /// 排序方法
  986. /// </summary>
  987. /// <param name="p1"></param>
  988. /// <param name="p2"></param>
  989. /// <returns></returns>
  990. private int my_sort(Point p1, Point p2)
  991. {
  992. return p1.Y == p2.Y ? p1.X.CompareTo(p2.X) : p1.Y.CompareTo(p2.Y); //升序排序
  993. }
  994. public object CloneObject(object obj)
  995. {
  996. using (MemoryStream stream = new MemoryStream())
  997. {
  998. BinaryFormatter formatter = new BinaryFormatter();
  999. stream.Position = 0;
  1000. formatter.Serialize(stream, obj);
  1001. stream.Position = 0;
  1002. return formatter.Deserialize(stream);
  1003. }
  1004. }
  1005. #endregion
  1006. }
  1007. }