COTSFieldData.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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,double a_pixelSize)
  103. {
  104. CImageHandler imghandler = new CImageHandler();
  105. List<COTSParticleClr> xrayParts = new List<COTSParticleClr>();
  106. imghandler.RemoveBSEImageBG(m_pBSEImg, a_pImageProcessParam,a_pixelSize, 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 grayRange, CIntRangeClr diameterRange,double pixelSize, bool ifXray)
  115. {
  116. CImageHandler imghandler = new CImageHandler();
  117. List<COTSParticleClr> specialParts = new List<COTSParticleClr>();
  118. imghandler.GetParticlesBySpecialGray(m_pBSEImg, grayRange,diameterRange,pixelSize, 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 InitParticles(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() / 2.0;
  144. double rMax = oAreaRange.GetEnd() / 2.0;
  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. double dPartArea = pParticle.GetArea();
  155. dPartArea = dPartArea * a_dPixelSize * a_dPixelSize;
  156. pParticle.SetArea(dPartArea);
  157. int nAveGrayLevel = pParticle.GetAveGray();
  158. if (dPartArea > (double)dAreaHigh)
  159. {
  160. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.OVERSIZE);
  161. overSizenum += 1;
  162. continue;
  163. }
  164. else if (dPartArea < (double)dAreaLow)
  165. {
  166. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.SMALL);
  167. tooSmallnum += 1;
  168. continue;
  169. }
  170. pParticle.SetTagId(nTagId);//give all the conforming particles a unified sequence no.
  171. pParticle.SetFieldId(GetId());
  172. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.NO_ANALYSIS_X_RAY);
  173. pParticle.SetTypeName("Not Identified");
  174. pParticle.SetAnalysisId(nTagId); // the same as the tagId no use now.
  175. nTagId++;
  176. m_listAnalysisParticles.Add(pParticle);
  177. }
  178. log.Info("Normal Particles (>"+ dAreaLow .ToString("f2") + "): "+ "<" + dAreaHigh.ToString("f2") + "): " + m_listAnalysisParticles.Count);
  179. List<COTSParticleClr> sizeConformingXrayParts = new List<COTSParticleClr>();
  180. foreach (var p in m_listXrayParticles)
  181. {
  182. if (p.GetTagId() > -1)//it has been initialized and it's a valid particle.
  183. {
  184. sizeConformingXrayParts.Add(p);
  185. }
  186. }
  187. m_listXrayParticles = sizeConformingXrayParts;
  188. }
  189. public bool CreateXrayList(List<COTSParticleClr> a_listParticles)
  190. {
  191. // go through particles list
  192. //a_listPosXRay.Clear();
  193. //int nXrayIndex = 0;
  194. foreach (COTSParticleClr pPart in a_listParticles)
  195. {
  196. pPart.CalXrayPos();
  197. // get xray position
  198. System.Drawing.Point poi = (System.Drawing.Point)pPart.GetXRayPos();
  199. // create a x-ray
  200. CPosXrayClr pPosXray = new CPosXrayClr();
  201. // set x-ray position
  202. pPosXray.SetPosition(poi);
  203. // set particle tag id
  204. pPosXray.SetPartTagId(pPart.GetTagId());
  205. //pPosXray.SetIndex(nXrayIndex++);
  206. pPosXray.SetIndex(pPart.GetAnalysisId());
  207. // set field id
  208. pPosXray.SetScanFieldId(pPart.GetFieldId());
  209. pPart.SetXray(pPosXray);
  210. }
  211. return true;
  212. }
  213. public bool NoParticle()
  214. {
  215. if (m_listAllParticles.Count == 0)
  216. {
  217. return true;
  218. }
  219. else
  220. {
  221. return false;
  222. }
  223. }
  224. public bool NoAnalysisParticle()
  225. {
  226. if (m_listAnalysisParticles.Count == 0)
  227. {
  228. return true;
  229. }
  230. else
  231. {
  232. return false;
  233. }
  234. }
  235. public bool Equals(COTSFieldData a_oSource) // CBSEImg& operator=(const CBSEImg&); // =operator
  236. {
  237. int nSize = m_listAllParticles.Count;
  238. if (nSize != a_oSource.m_listAllParticles.Count)
  239. {
  240. return false;
  241. }
  242. for (int i = 0; i < nSize; ++i)
  243. {
  244. if (!m_listAllParticles[i] .Equals( a_oSource.m_listAllParticles[i]))
  245. {
  246. return false;
  247. }
  248. }
  249. return m_nID == a_oSource.m_nID &&
  250. OTSPos == a_oSource.OTSPos;
  251. }
  252. // destructor
  253. // ID
  254. public int GetId()
  255. {
  256. return m_nID;
  257. }
  258. public void SetId(int a_nID)
  259. {
  260. m_nID = a_nID;
  261. }
  262. // position (from field center manager)
  263. public System.Drawing.Point GetOTSPosition()
  264. {
  265. return m_otsPos;
  266. }
  267. public void SetOTSPosition(System.Drawing.Point a_poiPos)
  268. {
  269. m_otsPos = a_poiPos;
  270. }
  271. public List<COTSParticleClr> GetTopBorderedParticles()
  272. {
  273. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  274. foreach (var p in m_listAllParticles)
  275. {
  276. var segs = p.GetFeature().GetSegmentsList();//COTSSegment
  277. foreach (var seg in segs)
  278. {
  279. if (seg.GetHeight() == 0)
  280. {
  281. parts.Add(p);
  282. break;
  283. }
  284. }
  285. }
  286. return parts;
  287. }
  288. public List< COTSParticleClr > GetBottomBorderedParticles()
  289. {
  290. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  291. foreach (var p in m_listAllParticles)
  292. {
  293. var segs = p.GetFeature().GetSegmentsList();
  294. foreach (var seg in segs)
  295. {
  296. if (seg.GetHeight() == this.Height - 1)//the lowest height is 767(height-1),cause starting from 0.
  297. {
  298. parts.Add(p);
  299. break;
  300. }
  301. }
  302. }
  303. return parts;
  304. }
  305. public List<COTSParticleClr> GetLeftBorderedParticles()
  306. {
  307. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  308. foreach (var p in m_listAllParticles)
  309. {
  310. var segs = p.GetFeature().GetSegmentsList();
  311. foreach (var seg in segs)
  312. {
  313. if (seg.GetStart() == 0)
  314. {
  315. parts.Add(p);
  316. break;
  317. }
  318. }
  319. }
  320. return parts;
  321. }
  322. public List <COTSParticleClr> GetRightBorderedParticles()
  323. {
  324. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  325. foreach (var p in m_listAllParticles)
  326. {
  327. var segs = p.GetFeature().GetSegmentsList();
  328. foreach (var seg in segs)
  329. {
  330. if (seg.GetStart() + seg.GetLength() == this.Width)
  331. {
  332. parts.Add(p);
  333. break;
  334. }
  335. }
  336. }
  337. return parts;
  338. }
  339. // is empty
  340. public bool IsEmpty()
  341. {
  342. return m_listAllParticles.Count == 0;
  343. }
  344. void Duplicate( COTSFieldData a_oSource)
  345. {
  346. m_nID = a_oSource.m_nID;
  347. OTSPos = a_oSource.OTSPos;
  348. //m_strFieldFileFolder = a_oSource.m_strFieldFileFolder;
  349. // copy data over
  350. foreach(var pParticle in a_oSource.m_listAllParticles)
  351. {
  352. m_listAllParticles.Add(pParticle);
  353. }
  354. }
  355. }
  356. }