COTSFieldData.cs 13 KB

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