COTSFieldData.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Drawing;
  8. using OTSModelSharp.ImageProcess;
  9. using OTSModelSharp.ServiceInterface;
  10. using OTSDataType;
  11. using OTSCLRINTERFACE;
  12. namespace OTSModelSharp
  13. {
  14. using COTSFieldDataList = List<COTSFieldData>;
  15. public class COTSFieldData
  16. {
  17. protected NLog.Logger log ;
  18. // ID
  19. int m_nID;
  20. // position (from field center manager)
  21. protected System.Drawing.Point m_otsPos ;
  22. protected CBSEImgClr m_pBSEImg;
  23. protected double m_pixelSize;
  24. IImageProcess m_ImagePro;
  25. protected List<COTSParticleClr> m_listAllParticles = new List<COTSParticleClr>();//hold up all the particles abstracted from bse image;
  26. protected List<COTSParticleClr> m_listAnalysisParticles = new List<COTSParticleClr>();// according to the size (the diameter) constraint,pick out all the conform particles.
  27. protected List<COTSParticleClr> m_listXrayParticles = new List<COTSParticleClr>();//hold up all the particles that need the xray data.
  28. private int height;
  29. private int width;
  30. public int Height { get => height; set => height = value; }
  31. public int Width { get => width; set => width = value; }
  32. public System.Drawing.Point OTSPos { get => m_otsPos; set => m_otsPos = value; }
  33. public Point SemPos { get => m_semPos; set => m_semPos = value; }
  34. private Point m_semPos;
  35. public List<COTSParticleClr> GetListAnalysisParticles()
  36. {
  37. return m_listAnalysisParticles;
  38. }
  39. public List<COTSParticleClr> GetListXrayParticles()
  40. {
  41. return m_listXrayParticles;
  42. }
  43. public void SetListAnalysisParticles(List<COTSParticleClr> value)
  44. {
  45. m_listAnalysisParticles = value;
  46. }
  47. // particle list
  48. public COTSFieldData(CBSEImgClr a_pBSEImg, double a_dPixelSize)
  49. {
  50. log = NLog.LogManager.GetCurrentClassLogger();
  51. Init();
  52. m_pBSEImg = a_pBSEImg;
  53. m_pixelSize = a_dPixelSize;
  54. width = a_pBSEImg.GetWidth();
  55. height = a_pBSEImg.GetHeight();
  56. }
  57. public COTSFieldData()
  58. {
  59. log = NLog.LogManager.GetCurrentClassLogger();
  60. Init();
  61. }
  62. // initialization
  63. void Init()
  64. {
  65. // initialization
  66. m_nID = -1;
  67. OTSPos =new System.Drawing.Point(0, 0);
  68. m_listAllParticles.Clear();
  69. }
  70. public void CCOTSFieldData(COTSFieldData a_oSource)
  71. {
  72. // can't copy itself
  73. if (a_oSource == this)
  74. {
  75. return;
  76. }
  77. Duplicate(a_oSource);
  78. }
  79. public COTSFieldData(COTSFieldData a_poSource)
  80. {
  81. // can't copy itself
  82. if (a_poSource == this)
  83. {
  84. return;
  85. }
  86. Duplicate(a_poSource);
  87. }
  88. public CBSEImgClr GetBSEImage()
  89. {
  90. return m_pBSEImg;
  91. }
  92. public void SetBSEImage(CBSEImgClr a_pBSEImg)
  93. {
  94. if (a_pBSEImg == null)
  95. {
  96. // invalid BSE image.
  97. log.Error("SetBSEImage: invalid BSE image.");
  98. return;
  99. }
  100. m_pBSEImg = a_pBSEImg;
  101. }
  102. public void RemoveBSEImageBG(COTSImageProcParam a_pImageProcessParam)
  103. {
  104. CImageHandler imghandler = new CImageHandler();
  105. List<COTSParticleClr> xrayParts = new List<COTSParticleClr>();
  106. imghandler.RemoveBSEImageBG(m_pBSEImg, a_pImageProcessParam, ref xrayParts);
  107. foreach (var p in xrayParts)
  108. {
  109. m_listAllParticles.Add(p);
  110. m_listXrayParticles.Add(p);
  111. }
  112. return ;
  113. }
  114. public void GetPartsBySpecialGray(CIntRangeClr range,bool ifXray)
  115. {
  116. CImageHandler imghandler = new CImageHandler();
  117. List<COTSParticleClr> specialParts = new List<COTSParticleClr>();
  118. imghandler.GetParticlesBySpecialGray(m_pBSEImg, range, ref specialParts);
  119. foreach (var p in specialParts)
  120. {
  121. m_listAllParticles.Add(p);
  122. if (ifXray)
  123. {
  124. m_listXrayParticles.Add(p);
  125. }
  126. }
  127. return;
  128. }
  129. public bool CalParticleImageProp(List<COTSParticleClr> particles)
  130. {
  131. m_ImagePro = new CImageHandler();
  132. foreach (COTSParticleClr part in particles)
  133. {
  134. m_ImagePro.CalParticleImageProp( part, m_pixelSize);
  135. }
  136. return true;
  137. }
  138. public void FilterParticles(COTSImageProcParam a_pImageProcessParam, double a_dPixelSize)
  139. {
  140. m_listAnalysisParticles = new List<COTSParticleClr>();
  141. // get area range
  142. CDoubleRange oAreaRange = a_pImageProcessParam.GetIncAreaRange();
  143. double rMin = oAreaRange.GetStart() / 2f;
  144. double rMax = oAreaRange.GetEnd() / 2f;
  145. double dAreaLow = rMin*rMin * 3.14159;
  146. double dAreaHigh = rMax*rMax * 3.14159;
  147. int nTagId = 0;
  148. //int nAnalysisPartId = 0;
  149. int tooSmallnum = 0;
  150. int overSizenum = 0;
  151. log.Info("Total Particles: " + m_listAllParticles.Count);
  152. foreach (COTSParticleClr pParticle in m_listAllParticles)
  153. {
  154. // get particle area
  155. double dPartArea = pParticle.GetArea();
  156. dPartArea = dPartArea * a_dPixelSize * a_dPixelSize;
  157. pParticle.SetArea(dPartArea);
  158. // get average gray level
  159. int nAveGrayLevel = pParticle.GetAveGray();
  160. // oversize particles
  161. if (dPartArea > (double)dAreaHigh)
  162. {
  163. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.OVERSIZE);
  164. overSizenum += 1;
  165. continue;
  166. }
  167. // too small to measure
  168. else if (dPartArea < (double)dAreaLow)
  169. {
  170. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.SMALL);
  171. tooSmallnum += 1;
  172. continue;
  173. }
  174. // set particle tag id
  175. pParticle.SetTagId(nTagId);//give all the conforming particles a unified sequence no.
  176. // set field id
  177. pParticle.SetFieldId(GetId());
  178. // add the particle into the output particles list
  179. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.NO_ANALYSIS_X_RAY);
  180. pParticle.SetTypeName("Not Identified");
  181. pParticle.SetAnalysisId(nTagId); // the same as the tagId no use now.
  182. nTagId++;
  183. m_listAnalysisParticles.Add(pParticle);
  184. }
  185. log.Info("TooSmall Particles (<"+ dAreaLow .ToString("f2") + "): " + tooSmallnum);
  186. log.Info("OverSize Particles (>"+ dAreaHigh.ToString("f2") + "): " + overSizenum);
  187. List<COTSParticleClr> sizeConformingXrayParts = new List<COTSParticleClr>();
  188. foreach (var p in m_listXrayParticles)
  189. {
  190. if (p.GetTagId() > -1)//it has been initialized and it's a valid particle.
  191. {
  192. sizeConformingXrayParts.Add(p);
  193. }
  194. }
  195. m_listXrayParticles = sizeConformingXrayParts;
  196. }
  197. public bool CreateXrayList(List<COTSParticleClr> a_listParticles)
  198. {
  199. // go through particles list
  200. //a_listPosXRay.Clear();
  201. //int nXrayIndex = 0;
  202. foreach (COTSParticleClr pPart in a_listParticles)
  203. {
  204. pPart.CalXrayPos();
  205. // get xray position
  206. System.Drawing.Point poi = (System.Drawing.Point)pPart.GetXRayPos();
  207. // create a x-ray
  208. CPosXrayClr pPosXray = new CPosXrayClr();
  209. // set x-ray position
  210. pPosXray.SetPosition(poi);
  211. // set particle tag id
  212. pPosXray.SetPartTagId(pPart.GetTagId());
  213. //pPosXray.SetIndex(nXrayIndex++);
  214. pPosXray.SetIndex(pPart.GetAnalysisId());
  215. // set field id
  216. pPosXray.SetScanFieldId(pPart.GetFieldId());
  217. pPart.SetXray(pPosXray);
  218. }
  219. return true;
  220. }
  221. public bool NoParticle()
  222. {
  223. if (m_listAllParticles.Count == 0)
  224. {
  225. return true;
  226. }
  227. else
  228. {
  229. return false;
  230. }
  231. }
  232. public bool NoAnalysisParticle()
  233. {
  234. if (m_listAnalysisParticles.Count == 0)
  235. {
  236. return true;
  237. }
  238. else
  239. {
  240. return false;
  241. }
  242. }
  243. public bool Equals(COTSFieldData a_oSource) // CBSEImg& operator=(const CBSEImg&); // =operator
  244. {
  245. int nSize = m_listAllParticles.Count;
  246. if (nSize != a_oSource.m_listAllParticles.Count)
  247. {
  248. return false;
  249. }
  250. for (int i = 0; i < nSize; ++i)
  251. {
  252. if (!m_listAllParticles[i] .Equals( a_oSource.m_listAllParticles[i]))
  253. {
  254. return false;
  255. }
  256. }
  257. return m_nID == a_oSource.m_nID &&
  258. OTSPos == a_oSource.OTSPos;
  259. }
  260. // destructor
  261. // ID
  262. public int GetId()
  263. {
  264. return m_nID;
  265. }
  266. public void SetId(int a_nID)
  267. {
  268. m_nID = a_nID;
  269. }
  270. // position (from field center manager)
  271. public System.Drawing.Point GetOTSPosition()
  272. {
  273. return m_otsPos;
  274. }
  275. public void SetOTSPosition(System.Drawing.Point a_poiPos)
  276. {
  277. m_otsPos = a_poiPos;
  278. }
  279. public List<COTSParticleClr> GetTopBorderedParticles()
  280. {
  281. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  282. foreach (var p in m_listAllParticles)
  283. {
  284. var segs = p.GetFeature().GetSegmentsList();//COTSSegment
  285. foreach (var seg in segs)
  286. {
  287. if (seg.GetHeight() == 0)
  288. {
  289. parts.Add(p);
  290. break;
  291. }
  292. }
  293. }
  294. return parts;
  295. }
  296. public List< COTSParticleClr > GetBottomBorderedParticles()
  297. {
  298. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  299. foreach (var p in m_listAllParticles)
  300. {
  301. var segs = p.GetFeature().GetSegmentsList();
  302. foreach (var seg in segs)
  303. {
  304. if (seg.GetHeight() == this.Height - 1)//the lowest height is 767(height-1),cause starting from 0.
  305. {
  306. parts.Add(p);
  307. break;
  308. }
  309. }
  310. }
  311. return parts;
  312. }
  313. public List<COTSParticleClr> GetLeftBorderedParticles()
  314. {
  315. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  316. foreach (var p in m_listAllParticles)
  317. {
  318. var segs = p.GetFeature().GetSegmentsList();
  319. foreach (var seg in segs)
  320. {
  321. if (seg.GetStart() == 0)
  322. {
  323. parts.Add(p);
  324. break;
  325. }
  326. }
  327. }
  328. return parts;
  329. }
  330. public List <COTSParticleClr> GetRightBorderedParticles()
  331. {
  332. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  333. foreach (var p in m_listAllParticles)
  334. {
  335. var segs = p.GetFeature().GetSegmentsList();
  336. foreach (var seg in segs)
  337. {
  338. if (seg.GetStart() + seg.GetLength() == this.Width)
  339. {
  340. parts.Add(p);
  341. break;
  342. }
  343. }
  344. }
  345. return parts;
  346. }
  347. // is empty
  348. public bool IsEmpty()
  349. {
  350. return m_listAllParticles.Count == 0;
  351. }
  352. void Duplicate( COTSFieldData a_oSource)
  353. {
  354. m_nID = a_oSource.m_nID;
  355. OTSPos = a_oSource.OTSPos;
  356. //m_strFieldFileFolder = a_oSource.m_strFieldFileFolder;
  357. // copy data over
  358. foreach(var pParticle in a_oSource.m_listAllParticles)
  359. {
  360. m_listAllParticles.Add(pParticle);
  361. }
  362. }
  363. }
  364. }