COTSField.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. using OTSCLRINTERFACE;
  2. using OTSMeasureApp._0_OTSModel.OTSDataType;
  3. using OTSModelSharp;
  4. using OTSModelSharp.ServiceCenter;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Xml;
  10. namespace OTSDataType
  11. {
  12. public class COTSField : ISlo
  13. {
  14. public COTSField leftField = null;
  15. public COTSField upField = null;
  16. public COTSField downField = null;
  17. public COTSField rightField = null;
  18. public List<CBSEImgClr> particleImages = new List<CBSEImgClr>();
  19. protected NLog.Logger log;
  20. // ID
  21. int m_nID;
  22. int measureSequence;
  23. private bool m_enable=true;
  24. // position (from field center manager)
  25. protected System.Drawing.PointF m_otsPos;
  26. protected CBSEImgClr m_pBSEImg;
  27. protected double m_pixelSize;
  28. protected CImageHandler m_ImagePro;
  29. protected List<COTSParticleClr> m_listAllParticles = new List<COTSParticleClr>();//hold up all the particles abstracted from bse image;
  30. protected List<COTSParticleClr> m_listAnalysisParticles = new List<COTSParticleClr>();// according to xraylimit constraint,pick out the first big particles.
  31. protected List<COTSParticleClr> m_listXrayParticles = new List<COTSParticleClr>();//hold up all the particles that needing the xray data.
  32. private int imgheight;
  33. private int imgwidth;
  34. private COTSRect m_otsRect;
  35. private COTSSample m_sample;
  36. public COTSField()
  37. {
  38. log = NLog.LogManager.GetCurrentClassLogger();
  39. Init();
  40. }//only using in xmlserialization
  41. public int ImgHeight { get => imgheight; set => imgheight = value; }
  42. public int ImgWidth { get => imgwidth; set => imgwidth = value; }
  43. public COTSSample Sample { get => m_sample; set => m_sample = value; }
  44. public bool Enable { get => m_enable; set => m_enable = value; }
  45. internal COTSRect GetOTSRect()
  46. {
  47. return m_otsRect;
  48. }
  49. internal void SetOTSRect(COTSRect value)
  50. {
  51. m_otsRect = value;
  52. }
  53. public int GetMeasureSequence()
  54. {
  55. return measureSequence;
  56. }
  57. public void SetMeasureSequence(int value)
  58. {
  59. measureSequence = value;
  60. }
  61. public List<string> strKeyName;
  62. public bool GetIsMeasureComplete()
  63. {
  64. return isMeasureComplete;
  65. }
  66. public void SetIsMeasureComplete(bool value)
  67. {
  68. isMeasureComplete = value;
  69. }
  70. private bool isMeasureComplete;
  71. public PointF GetSemPos()
  72. {
  73. return m_semPos;
  74. }
  75. public void SetSemPos(PointF value)
  76. {
  77. m_semPos = value;
  78. }
  79. private PointF m_semPos;
  80. public List<COTSParticleClr> GetAllParticles()
  81. {
  82. return m_listAllParticles;
  83. }
  84. public List<COTSParticleClr> GetListAnalysisParticles()
  85. {
  86. return m_listAnalysisParticles;
  87. }
  88. public List<COTSParticleClr> GetListXrayParticles()
  89. {
  90. m_listXrayParticles.Clear();
  91. foreach (var p in m_listAnalysisParticles)
  92. {
  93. if (p.IsXrayParticle())
  94. {
  95. m_listXrayParticles.Add(p);
  96. }
  97. }
  98. return m_listXrayParticles;
  99. }
  100. public void SetListAnalysisParticles(List<COTSParticleClr> value)
  101. {
  102. m_listAnalysisParticles = value;
  103. }
  104. public COTSField(PointF centerPoint, double a_dPixelSize)
  105. {
  106. log = NLog.LogManager.GetCurrentClassLogger();
  107. Init();
  108. m_otsPos = centerPoint;
  109. m_pixelSize = a_dPixelSize;
  110. }
  111. public double GetPixelSize()
  112. {
  113. return m_pixelSize;
  114. }
  115. public void SetPixelSize(double size)
  116. {
  117. m_pixelSize = size;
  118. }
  119. // initialization
  120. void Init()
  121. {
  122. // initialization
  123. m_nID = -1;
  124. m_otsPos = new System.Drawing.Point(0, 0);
  125. m_listAllParticles.Clear();
  126. m_enable = true;
  127. }
  128. public COTSField(COTSField a_poSource)
  129. {
  130. // can't copy itself
  131. if (a_poSource == this)
  132. {
  133. return;
  134. }
  135. Duplicate(a_poSource);
  136. }
  137. public CBSEImgClr GetBSEImage()
  138. {
  139. return m_pBSEImg;
  140. }
  141. public Bitmap GetAnalysisParticleBlackColoredImage()
  142. {
  143. CImageHandler imghandler = new CImageHandler();
  144. List<COTSParticleClr> Parts = GetAllParticles();
  145. Bitmap img = new Bitmap(this.ImgWidth, this.ImgHeight);
  146. var imgparam = m_sample.GetMsrParams().GetImageProcessParam();
  147. var pixelsize = m_sample.CalculatePixelSize();
  148. imghandler.GetImageWithBlackColoredParts(Parts,imgparam,pixelsize, ref img);
  149. return img;
  150. }
  151. public Bitmap GetAnalysisParticleSTDColoredImage()
  152. {
  153. CImageHandler imghandler = new CImageHandler();
  154. List<COTSParticleClr> Parts = GetListAnalysisParticles();
  155. Bitmap img = new Bitmap(this.ImgWidth, this.ImgHeight);
  156. var imgparam = m_sample.GetMsrParams().GetImageProcessParam();
  157. var pixelsize = m_sample.CalculatePixelSize();
  158. imghandler.GetImageWithSTDColoredParts(Parts, imgparam, pixelsize, ref img);
  159. return img;
  160. }
  161. public void SetBSEImage(CBSEImgClr a_pBSEImg)
  162. {
  163. if (a_pBSEImg == null)
  164. {
  165. // invalid BSE image.
  166. log.Error("SetBSEImage: invalid BSE image.");
  167. return;
  168. }
  169. m_pBSEImg = a_pBSEImg;
  170. imgwidth = a_pBSEImg.GetWidth();
  171. imgheight = a_pBSEImg.GetHeight();
  172. }
  173. public void GetOriginalParticles(CSampleParam pMsrParam, double pixelsize)
  174. {
  175. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  176. // remove BES image background
  177. RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize);
  178. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  179. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  180. {
  181. List<CSpecialGrayRange> ranges = specialPartsparam.GetSpecialGreyRanges();
  182. foreach (var grayRange in ranges)
  183. {
  184. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  185. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  186. GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  187. }
  188. }
  189. log.Info("Find all particle num:" + GetAllParticles().Count);
  190. return;
  191. }
  192. private void RemoveImgBGAndGetParticles(COTSImageProcParam a_pImageProcessParam, double a_pixelSize)
  193. {
  194. if (m_pBSEImg == null)
  195. return;
  196. CImageHandler imghandler = new CImageHandler();
  197. List<COTSParticleClr> allParts = new List<COTSParticleClr>();
  198. imghandler.RemoveBGAndGetParts(this, a_pImageProcessParam, ref allParts);
  199. m_listAllParticles = allParts;
  200. return;
  201. }
  202. class particleCompOnArea : IComparer<COTSParticleClr>
  203. {
  204. public int Compare(COTSParticleClr x, COTSParticleClr y)
  205. {
  206. return y.GetActualArea().CompareTo(x.GetActualArea());//descending sort
  207. }
  208. }
  209. public void FilterParticles(COTSXRayParam pXRayParam)
  210. {
  211. log.Info("filter particle according to xraylimit and outermost border");
  212. m_listAllParticles.Sort(new particleCompOnArea());
  213. var listXray1 = new List<COTSParticleClr>();
  214. if (m_listAllParticles.Count > pXRayParam.GetXrayLimit())
  215. {
  216. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  217. {
  218. var part = m_listAllParticles[i];
  219. int l=0, r=0, t=0, b=0;
  220. part.GetOTSRect(ref l, ref t,ref r, ref b);
  221. COTSRect otsrec = new COTSRect(l, t, r, b);
  222. PointF p1 = otsrec.GetCenterPoint();
  223. if (m_sample.IsThisPointInMeasureArea(new Point((int)p1.X, (int)p1.Y)))
  224. {
  225. listXray1.Add(m_listAllParticles[i]);
  226. }
  227. }
  228. }
  229. else
  230. {
  231. foreach (var p in m_listAllParticles)
  232. {
  233. var part = p;
  234. int l = 0, r = 0, t = 0, b = 0;
  235. part.GetOTSRect(ref l, ref t, ref r, ref b);
  236. COTSRect otsrec = new COTSRect(l, t, r, b);
  237. PointF p1 = otsrec.GetCenterPoint();
  238. if (m_sample.IsThisPointInMeasureArea(new Point((int)p1.X, (int)p1.Y)))
  239. {
  240. listXray1.Add(p);
  241. }
  242. }
  243. }
  244. SetListAnalysisParticles(listXray1);
  245. log.Info("Xray Analysis particles:" + listXray1.Count);
  246. }
  247. public void SelectParticlesAccordingImgProp(COTSImageProcParam a_pImageProcessParam)
  248. {
  249. var selconditiondic = a_pImageProcessParam.GetParticleSelConditionDic();
  250. var listselparts = new List<COTSParticleClr>();
  251. var excludeparts = new List<COTSParticleClr>();
  252. if (selconditiondic.ContainsKey("dmax"))
  253. {
  254. log.Info("Select particles according to dmax");
  255. var rng = selconditiondic["dmax"];
  256. foreach (var p in GetListAnalysisParticles())
  257. {
  258. if (p.GetDMAX() < rng.GetStart() || p.GetDMAX() >= rng.GetEnd())
  259. {
  260. excludeparts.Add(p);
  261. }
  262. else
  263. {
  264. //log.Info("dmax=" + p.GetDMAX().ToString("F2"));
  265. }
  266. }
  267. }
  268. if (selconditiondic.ContainsKey("dmin"))
  269. {
  270. log.Info("Select particles according to dmin");
  271. var rng = selconditiondic["dmin"];
  272. foreach (var p in GetListAnalysisParticles())
  273. {
  274. if (p.GetDMIN() < rng.GetStart() || p.GetDMIN() >= rng.GetEnd())
  275. {
  276. if (!excludeparts.Contains(p))
  277. {
  278. excludeparts.Add(p);
  279. }
  280. else
  281. {
  282. //log.Info("dmin=" + p.GetDMIN().ToString("F2"));
  283. }
  284. }
  285. }
  286. }
  287. if (selconditiondic.ContainsKey("orientation"))
  288. {
  289. log.Info("Select particles according to orientation");
  290. var rng = selconditiondic["orientation"];
  291. foreach (var p in GetListAnalysisParticles())
  292. {
  293. if (p.GetORIENTATION() < rng.GetStart() || p.GetORIENTATION() >= rng.GetEnd())
  294. {
  295. if (!excludeparts.Contains(p))
  296. {
  297. excludeparts.Add(p);
  298. }
  299. else
  300. {
  301. //log.Info("orientation=" + p.GetORIENTATION().ToString("F2"));
  302. }
  303. }
  304. }
  305. }
  306. if (selconditiondic.ContainsKey("aspect"))
  307. {
  308. log.Info("Select particles according to aspect");
  309. var rng = selconditiondic["aspect"];
  310. foreach (var p in GetListAnalysisParticles())
  311. {
  312. double aspect = p.GetDMAX() / p.GetDMIN();
  313. if (aspect < rng.GetStart() || aspect >= rng.GetEnd())
  314. {
  315. if (!excludeparts.Contains(p))
  316. {
  317. excludeparts.Add(p);
  318. }
  319. else
  320. {
  321. //log.Info("aspect=" + aspect.ToString("F2"));
  322. }
  323. }
  324. }
  325. }
  326. if (selconditiondic.ContainsKey("ferret"))
  327. {
  328. log.Info("Select particles according to ferret");
  329. var rng = selconditiondic["ferret"];
  330. foreach (var p in GetListAnalysisParticles())
  331. {
  332. double dferet = p.GetFeretDiameter();
  333. if (dferet < rng.GetStart() || dferet >= rng.GetEnd())
  334. {
  335. if (!excludeparts.Contains(p))
  336. {
  337. excludeparts.Add(p);
  338. }
  339. else
  340. {
  341. //log.Info("ferret=" + dferet.ToString("F2"));
  342. }
  343. }
  344. }
  345. }
  346. foreach (var p in GetListAnalysisParticles())
  347. {
  348. if (!excludeparts.Contains(p))
  349. {
  350. listselparts.Add(p);
  351. }
  352. }
  353. SetListAnalysisParticles(listselparts);
  354. log.Info("Analysis particles:" + listselparts.Count);
  355. }
  356. public void RemoveDuplicateOverlapParticles(int overlap)
  357. {
  358. List<COTSParticleClr> finalparts = new List<COTSParticleClr>();
  359. List<COTSParticleClr> duplicateparts = new List<COTSParticleClr>();
  360. //find left side duplicate particles
  361. var leftparts = this.GetSideParticlesByOverlap(SORTING_DIRECTION.LEFT, overlap);
  362. if (leftField != null && leftField.measureSequence < this.measureSequence)
  363. {
  364. var rightsideparts = leftField.GetSideParticlesByOverlap(SORTING_DIRECTION.RIGHT, overlap);
  365. if (leftField.upField != null && leftField.upField.measureSequence < this.measureSequence)//include the left up corner parts
  366. {
  367. var leftupParts = leftField.upField.GetSideParticlesByOverlap(SORTING_DIRECTION.RIGHT, overlap);
  368. rightsideparts.AddRange(leftupParts);
  369. }
  370. if (leftField.downField != null && leftField.downField.measureSequence < this.measureSequence)//include the left down corner parts
  371. {
  372. var leftdownParts = leftField.downField.GetSideParticlesByOverlap(SORTING_DIRECTION.RIGHT, overlap);
  373. rightsideparts.AddRange(leftdownParts);
  374. }
  375. log.Info("left side particles num:" + leftparts.Count.ToString());
  376. foreach (var p in leftparts)
  377. {
  378. int pleft = 0, pright = 0, ptop = 0, pbottom = 0;
  379. p.GetOTSRect(ref pleft, ref ptop, ref pright, ref pbottom);
  380. COTSRect prec = new COTSRect(pleft, ptop, pright, pbottom);
  381. PointF pcenter = prec.GetCenterPoint();
  382. foreach (var p1 in rightsideparts)
  383. {
  384. int p1left = 0, p1right = 0, p1top = 0, p1bottom = 0;
  385. p1.GetOTSRect(ref p1left, ref p1top, ref p1right, ref p1bottom);
  386. COTSRect p1rec = new COTSRect(p1left, p1top, p1right, p1bottom);
  387. PointF p1Center = p1rec.GetCenterPoint();
  388. if (Math.Abs(pcenter.X - p1Center.X) < 2 * overlap && Math.Abs(pcenter.Y - p1Center.Y) < 2 * overlap)
  389. {
  390. var sim = p.CalculateSimilarity(p1);
  391. if (sim > 0.95)
  392. {
  393. log.Warn("remove left side duplicate particle,similarity:" + sim.ToString("F3"));
  394. log.Warn("P1:" + p.GetImgPortraitString());
  395. log.Warn("P2:" + p1.GetImgPortraitString());
  396. duplicateparts.Add(p);
  397. break;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. //find up side duplicate particles
  404. var upparts = this.GetSideParticlesByOverlap(SORTING_DIRECTION.UP, overlap);
  405. if (upField != null && upField.measureSequence < this.measureSequence)
  406. {
  407. var othersideparts = upField.GetSideParticlesByOverlap(SORTING_DIRECTION.DOWN, overlap);
  408. log.Info("up side particles num:" + upparts.Count.ToString());
  409. foreach (var p in upparts)
  410. {
  411. int pleft = 0, pright = 0, ptop = 0, pbottom = 0;
  412. p.GetOTSRect(ref pleft, ref ptop, ref pright, ref pbottom);
  413. COTSRect prec = new COTSRect(pleft, ptop, pright, pbottom);
  414. PointF pcenter = prec.GetCenterPoint();
  415. foreach (var p1 in othersideparts)
  416. {
  417. int p1left = 0, p1right = 0, p1top = 0, p1bottom = 0;
  418. p1.GetOTSRect(ref p1left, ref p1top, ref p1right, ref p1bottom);
  419. COTSRect p1rec = new COTSRect(p1left, p1top, p1right, p1bottom);
  420. PointF p1Center = p1rec.GetCenterPoint();
  421. if (Math.Abs(pcenter.X - p1Center.X) < 2 * overlap && Math.Abs(pcenter.Y - p1Center.Y) < 2 * overlap)
  422. {
  423. var sim = p.CalculateSimilarity(p1);
  424. if (sim > 0.95)
  425. {
  426. log.Warn("remove upside duplicate particle,similarity:" + sim.ToString("F3"));
  427. log.Warn("P1:" + p.GetImgPortraitString());
  428. log.Warn("P2:" + p1.GetImgPortraitString());
  429. duplicateparts.Add(p);
  430. break;
  431. }
  432. }
  433. }
  434. }
  435. }
  436. //find right side duplicate particles
  437. var rightparts = this.GetSideParticlesByOverlap(SORTING_DIRECTION.RIGHT, overlap);
  438. if (rightField != null && rightField.measureSequence < this.measureSequence)
  439. {
  440. log.Info("right side particles num:" + rightparts.Count.ToString());
  441. var othersideparts = rightField.GetSideParticlesByOverlap(SORTING_DIRECTION.LEFT, overlap);
  442. if (rightField.upField != null && rightField.upField.measureSequence < this.measureSequence)// right up corner parts
  443. {
  444. var rightupParts = rightField.upField.GetSideParticlesByOverlap(SORTING_DIRECTION.LEFT, overlap);
  445. othersideparts.AddRange(rightupParts);
  446. }
  447. if (rightField.downField != null && leftField.downField.measureSequence < this.measureSequence)// rightdown corner parts
  448. {
  449. var rightdownParts = leftField.downField.GetSideParticlesByOverlap(SORTING_DIRECTION.LEFT, overlap);
  450. othersideparts.AddRange(rightdownParts);
  451. }
  452. foreach (var p in rightparts)
  453. {
  454. int pleft = 0, pright = 0, ptop = 0, pbottom = 0;
  455. p.GetOTSRect(ref pleft, ref ptop, ref pright, ref pbottom);
  456. COTSRect prec = new COTSRect(pleft, ptop, pright, pbottom);
  457. PointF pcenter = prec.GetCenterPoint();
  458. foreach (var p1 in othersideparts)
  459. {
  460. int p1left = 0, p1right = 0, p1top = 0, p1bottom = 0;
  461. p1.GetOTSRect(ref p1left, ref p1top, ref p1right, ref p1bottom);
  462. COTSRect p1rec = new COTSRect(p1left, p1top, p1right, p1bottom);
  463. PointF p1Center = p1rec.GetCenterPoint();
  464. if (Math.Abs(pcenter.X - p1Center.X) < 2 * overlap && Math.Abs(pcenter.Y - p1Center.Y) < 2 * overlap)
  465. {
  466. var sim = p.CalculateSimilarity(p1);
  467. if (sim > 0.95)
  468. {
  469. log.Warn("remove right side duplicate particle,similarity:" + sim.ToString("F3"));
  470. log.Warn("P1:" + p.GetImgPortraitString());
  471. log.Warn("P2:" + p1.GetImgPortraitString());
  472. duplicateparts.Add(p);
  473. break;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. //find down side duplicate particles
  480. var downparts = this.GetSideParticlesByOverlap(SORTING_DIRECTION.DOWN, overlap);
  481. if (downField != null && downField.measureSequence < this.measureSequence)
  482. {
  483. log.Info("down side particles num:" + downparts.Count.ToString());
  484. var othersideparts = downField.GetSideParticlesByOverlap(SORTING_DIRECTION.UP, overlap);
  485. foreach (var p in downparts)
  486. {
  487. int pleft = 0, pright = 0, ptop = 0, pbottom = 0;
  488. p.GetOTSRect(ref pleft, ref ptop, ref pright, ref pbottom);
  489. COTSRect prec = new COTSRect(pleft, ptop, pright, pbottom);
  490. PointF pcenter = prec.GetCenterPoint();
  491. foreach (var p1 in othersideparts)
  492. {
  493. int p1left = 0, p1right = 0, p1top = 0, p1bottom = 0;
  494. p1.GetOTSRect(ref p1left, ref p1top, ref p1right, ref p1bottom);
  495. COTSRect p1rec = new COTSRect(p1left, p1top, p1right, p1bottom);
  496. PointF p1Center = p1rec.GetCenterPoint();
  497. if (Math.Abs(pcenter.X - p1Center.X) < 2 * overlap && Math.Abs(pcenter.Y - p1Center.Y) < 2 * overlap)
  498. {
  499. var sim = p.CalculateSimilarity(p1);
  500. if (sim > 0.95)
  501. {
  502. log.Warn("remove down side duplicate particle,similarity:" + sim.ToString("F3"));
  503. log.Warn("P1:" + p.GetImgPortraitString());
  504. log.Warn("P2:" + p1.GetImgPortraitString());
  505. duplicateparts.Add(p);
  506. break;
  507. }
  508. }
  509. }
  510. }
  511. }
  512. foreach (var p in leftparts)
  513. {
  514. if (duplicateparts.Contains(p))
  515. {
  516. continue;
  517. }
  518. if (!finalparts.Contains(p))
  519. {
  520. finalparts.Add(p);
  521. }
  522. }
  523. foreach (var p in upparts)
  524. {
  525. if (duplicateparts.Contains(p))
  526. {
  527. continue;
  528. }
  529. if (!finalparts.Contains(p))
  530. {
  531. finalparts.Add(p);
  532. }
  533. }
  534. foreach (var p in rightparts)
  535. {
  536. if (duplicateparts.Contains(p))
  537. {
  538. continue;
  539. }
  540. if (!finalparts.Contains(p))
  541. {
  542. finalparts.Add(p);
  543. }
  544. }
  545. foreach (var p in downparts)
  546. {
  547. if (duplicateparts.Contains(p))
  548. {
  549. continue;
  550. }
  551. if (!finalparts.Contains(p))
  552. {
  553. finalparts.Add(p);
  554. }
  555. }
  556. foreach (var p in this.GetSideParticlesByOverlap(SORTING_DIRECTION.CENTER, overlap))
  557. {
  558. if (!finalparts.Contains(p))
  559. {
  560. finalparts.Add(p);
  561. }
  562. }
  563. this.SetListAnalysisParticles(finalparts);
  564. log.Info("removing duplicate particles result:" + finalparts.Count);
  565. }
  566. private List<COTSParticleClr> GetSideParticlesByOverlap(SORTING_DIRECTION direction, int overlap)
  567. {
  568. List<COTSParticleClr> sideparts = new List<COTSParticleClr>();
  569. var borderedparts = this.GetBorderedParticles();
  570. if (direction == SORTING_DIRECTION.LEFT)
  571. {
  572. foreach (var p in this.GetListAnalysisParticles())
  573. {
  574. int left = 0, top = 0, right = 0, bottom = 0;
  575. p.GetOTSRect(ref left, ref top, ref right, ref bottom);
  576. if (Math.Abs(right - this.GetOTSRect().GetTopLeft().X) < 2 * overlap)
  577. {
  578. if (!borderedparts.Contains(p) || Math.Abs(left - right) > 2 * overlap)//not on the border or it's a big particle
  579. {
  580. sideparts.Add(p);
  581. }
  582. }
  583. }
  584. }
  585. if (direction == SORTING_DIRECTION.RIGHT)
  586. {
  587. foreach (var p in this.GetListAnalysisParticles())
  588. {
  589. int left = 0, top = 0, right = 0, bottom = 0;
  590. p.GetOTSRect(ref left, ref top, ref right, ref bottom);
  591. if (Math.Abs(this.GetOTSRect().GetBottomRight().X - left) < 2 * overlap)
  592. {
  593. if (!borderedparts.Contains(p) || Math.Abs(left - right) > 2 * overlap)//not on the border or is a big part
  594. {
  595. sideparts.Add(p);
  596. }
  597. }
  598. }
  599. }
  600. if (direction == SORTING_DIRECTION.UP)
  601. {
  602. foreach (var p in this.GetListAnalysisParticles())
  603. {
  604. int left = 0, top = 0, right = 0, bottom = 0;
  605. p.GetOTSRect(ref left, ref top, ref right, ref bottom);
  606. if (Math.Abs(this.GetOTSRect().GetTopLeft().Y - bottom) < 2 * overlap)
  607. {
  608. if (!borderedparts.Contains(p) || Math.Abs(top - bottom) > 2 * overlap)//not on the border
  609. {
  610. sideparts.Add(p);
  611. }
  612. }
  613. }
  614. }
  615. if (direction == SORTING_DIRECTION.DOWN)
  616. {
  617. foreach (var p in this.GetListAnalysisParticles())
  618. {
  619. int left = 0, top = 0, right = 0, bottom = 0;
  620. p.GetOTSRect(ref left, ref top, ref right, ref bottom);
  621. if (Math.Abs(top - this.GetOTSRect().GetBottomRight().Y) < 2 * overlap)
  622. {
  623. if (!borderedparts.Contains(p) || Math.Abs(top - bottom) > 2 * overlap)//not on the border
  624. {
  625. sideparts.Add(p);
  626. }
  627. }
  628. }
  629. }
  630. if (direction == SORTING_DIRECTION.CENTER)
  631. {
  632. foreach (var p in this.GetListAnalysisParticles())
  633. {
  634. int left = 0, top = 0, right = 0, bottom = 0;
  635. p.GetOTSRect(ref left, ref top, ref right, ref bottom);
  636. var fldrec = this.GetOTSRect();
  637. int fldleft = (int)fldrec.GetTopLeft().X;
  638. int fldright = (int)fldrec.GetBottomRight().X;
  639. int fldtop = (int)fldrec.GetTopLeft().Y;
  640. int fldbottom = (int)fldrec.GetBottomRight().Y;
  641. if ((Math.Abs(top - fldtop) > 2 * overlap) && (Math.Abs(fldbottom - bottom) > 2 * overlap) && (Math.Abs(left - fldleft) > 2 * overlap) && (Math.Abs(fldright - right) > 2 * overlap))
  642. {
  643. sideparts.Add(p);
  644. }
  645. }
  646. }
  647. return sideparts;
  648. }
  649. public void GetPartsBySpecialGray(CIntRangeClr grayRange, CDoubleRangeClr diameterRange, double pixelSize, bool ifXray)
  650. {
  651. if (m_pBSEImg == null)
  652. return;
  653. CImageHandler imghandler = new CImageHandler();
  654. List<COTSParticleClr> specialParts = new List<COTSParticleClr>();
  655. imghandler.GetParticlesBySpecialGray(m_pBSEImg, grayRange, diameterRange, pixelSize, ref specialParts);
  656. foreach (var p in specialParts)
  657. {
  658. p.SetIsXrayParticle(ifXray);
  659. m_listAllParticles.Add(p);
  660. }
  661. return;
  662. }
  663. public bool CalParticleImageProp(List<COTSParticleClr> particles)
  664. {
  665. m_ImagePro = new CImageHandler();
  666. foreach (COTSParticleClr part in particles)
  667. {
  668. m_ImagePro.CalParticleImageProp(part, m_pixelSize);
  669. }
  670. return true;
  671. }
  672. public void CalculateParticleAbsolutPos(CSEMStageData pCSEMStageData)
  673. {
  674. foreach (var p in GetListAnalysisParticles())
  675. {
  676. PointF semP = new PointF(); ;
  677. Point semPos = new Point();
  678. pCSEMStageData.ConvertOTSToSEMCoord(GetOTSPosition(), ref semP);
  679. semPos.X = (int)semP.X;
  680. semPos.Y = (int)semP.Y;
  681. p.SetSEMPos(semPos);
  682. }
  683. }
  684. public void InitParticles(COTSImageProcParam a_pImageProcessParam)
  685. {
  686. // get area range
  687. CDoubleRange oAreaRange = a_pImageProcessParam.GetIncAreaRange();
  688. double rMin = oAreaRange.GetStart() / 2.0;
  689. double rMax = oAreaRange.GetEnd() / 2.0;
  690. int nTagId = 0;
  691. foreach (COTSParticleClr pParticle in m_listAnalysisParticles)//m_listAllParticles memorize all the particles .
  692. {
  693. pParticle.SetParticleId(nTagId);//give all the conforming particles a unified sequence no.
  694. pParticle.SetFieldId(GetId());
  695. pParticle.SetType((int)otsdataconst.OTS_PARTICLE_TYPE.NO_ANALYSIS_X_RAY);
  696. pParticle.SetTypeName("Not Identified");
  697. pParticle.SetAnalysisId(nTagId); // the same as the tagId no use now.
  698. nTagId++;
  699. }
  700. log.Info("Total analysis Particles: (>" + rMin.ToString("f2") + "): " + "<" + rMax.ToString("f2") + "): " + m_listAnalysisParticles.Count);
  701. }
  702. public bool CreateXrayList(List<COTSParticleClr> a_listParticles)
  703. {
  704. foreach (COTSParticleClr pPart in a_listParticles)
  705. {
  706. System.Drawing.Point poi = (System.Drawing.Point)pPart.GetXRayPos();
  707. CPosXrayClr pPosXray = new CPosXrayClr();
  708. pPosXray.SetPosition(poi);
  709. pPosXray.SetPartTagId(pPart.GetParticleId());
  710. pPosXray.SetIndex(pPart.GetAnalysisId());
  711. pPosXray.SetScanFieldId(pPart.GetFieldId());
  712. pPart.SetXray(pPosXray);
  713. }
  714. return true;
  715. }
  716. public void ClearAllMeausredData()
  717. {
  718. m_listAllParticles.Clear();
  719. m_listAnalysisParticles.Clear();
  720. m_listXrayParticles.Clear();
  721. m_pBSEImg = null;
  722. }
  723. public bool PositionEquals(COTSField a_oSource)
  724. {
  725. if (a_oSource.m_otsPos == this.m_otsPos)
  726. {
  727. return true;
  728. }
  729. else
  730. {
  731. return false;
  732. }
  733. }
  734. // ID
  735. public int GetId()
  736. {
  737. return m_nID;
  738. }
  739. public void SetId(int a_nID)
  740. {
  741. m_nID = a_nID;
  742. }
  743. // position (from field center manager)
  744. public System.Drawing.PointF GetOTSPosition()
  745. {
  746. return m_otsPos;
  747. }
  748. public void SetOTSPosition(System.Drawing.PointF a_poiPos)
  749. {
  750. m_otsPos = a_poiPos;
  751. }
  752. public List<COTSParticleClr> GetTopBorderedParticles()
  753. {
  754. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  755. foreach (var p in m_listAnalysisParticles)
  756. {
  757. var segs = p.GetFeature().GetSegmentsList();//COTSSegment
  758. foreach (var seg in segs)
  759. {
  760. if (seg.GetHeight() == 0)
  761. {
  762. parts.Add(p);
  763. break;
  764. }
  765. }
  766. }
  767. return parts;
  768. }
  769. public List<COTSParticleClr> GetBottomBorderedParticles()
  770. {
  771. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  772. foreach (var p in m_listAnalysisParticles)
  773. {
  774. var segs = p.GetFeature().GetSegmentsList();
  775. foreach (var seg in segs)
  776. {
  777. if (seg.GetHeight() == this.ImgHeight - 1)//the lowest height is 767(height-1),cause starting from 0.
  778. {
  779. parts.Add(p);
  780. break;
  781. }
  782. }
  783. }
  784. return parts;
  785. }
  786. public List<COTSParticleClr> GetBorderedParticles()
  787. {
  788. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  789. var leftparts = this.GetLeftBorderedParticles();
  790. var rightp = this.GetRightBorderedParticles();
  791. var topp = this.GetTopBorderedParticles();
  792. var bottomp = this.GetBottomBorderedParticles();
  793. foreach (var p in leftparts)
  794. {
  795. if (!parts.Contains(p))
  796. {
  797. parts.Add(p);
  798. }
  799. }
  800. foreach (var p in rightp)//
  801. {
  802. if (!parts.Contains(p))
  803. {
  804. parts.Add(p);
  805. }
  806. }
  807. foreach (var p in topp)
  808. {
  809. if (!parts.Contains(p))//there may be some particles connect to both the left and top.
  810. {
  811. parts.Add(p);
  812. }
  813. }
  814. foreach (var p in bottomp)
  815. {
  816. if (!parts.Contains(p))
  817. {
  818. parts.Add(p);
  819. }
  820. }
  821. return parts;
  822. }
  823. public List<COTSParticleClr> GetLeftBorderedParticles()
  824. {
  825. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  826. foreach (var p in m_listAnalysisParticles)
  827. {
  828. var segs = p.GetFeature().GetSegmentsList();
  829. foreach (var seg in segs)
  830. {
  831. if (seg.GetStart() == 0)
  832. {
  833. parts.Add(p);
  834. break;
  835. }
  836. }
  837. }
  838. return parts;
  839. }
  840. public List<COTSParticleClr> GetRightBorderedParticles()
  841. {
  842. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  843. foreach (var p in m_listAnalysisParticles)
  844. {
  845. var segs = p.GetFeature().GetSegmentsList();
  846. foreach (var seg in segs)
  847. {
  848. if (seg.GetStart() + seg.GetLength() == this.ImgWidth)
  849. {
  850. parts.Add(p);
  851. break;
  852. }
  853. }
  854. }
  855. return parts;
  856. }
  857. // is empty
  858. public bool IsEmpty()
  859. {
  860. return m_listAllParticles.Count == 0;
  861. }
  862. void Duplicate(COTSField a_oSource)
  863. {
  864. m_nID = a_oSource.m_nID;
  865. m_otsPos = a_oSource.m_otsPos;
  866. // copy data over
  867. foreach (var pParticle in a_oSource.m_listAllParticles)
  868. {
  869. m_listAllParticles.Add(pParticle);
  870. }
  871. }
  872. public override void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode)
  873. {
  874. xPoint xPos = new xPoint();
  875. xBool xenable = new xBool();
  876. Slo slo = new Slo();
  877. slo.Register("OTSPosition", xPos);
  878. slo.Register("Enable", xenable);
  879. if (isStoring)
  880. {
  881. xPos.AssignValue(new System.Drawing.Point((int)m_otsPos.X, (int)m_otsPos.Y));
  882. xenable.AssignValue(this.Enable);
  883. slo.Serialize(true, classDoc, rootNode);
  884. }
  885. else
  886. {
  887. slo.Serialize(false, classDoc, rootNode);
  888. m_otsPos = xPos.value();
  889. this.Enable=xenable.value();
  890. }
  891. }
  892. }
  893. }