COTSFieldData.cs 12 KB

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