COTSFieldData.cs 12 KB

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