COTSFieldData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 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. // oversize particles
  134. if (dPartArea > (double)dAreaHigh)
  135. {
  136. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.OVERSIZE);
  137. overSizenum += 1;
  138. continue;
  139. }
  140. // too small to measure
  141. else if (dPartArea < (double)dAreaLow)
  142. {
  143. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.SMALL);
  144. tooSmallnum += 1;
  145. continue;
  146. }
  147. // set particle tag id
  148. pParticle.SetTagId(nTagId++);
  149. // set field id
  150. pParticle.SetFieldId(GetId());
  151. // add the particle into the output particles list
  152. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.NO_ANALYSIS_X_RAY);
  153. pParticle.SetTypeName("Not Identified");
  154. pParticle.SetAnalysisId(nAnalysisPartId++);
  155. m_listAnalysisParticles.Add(pParticle);
  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. }
  225. // destructor
  226. // ID
  227. public int GetId()
  228. {
  229. return m_nID;
  230. }
  231. public void SetId(int a_nID)
  232. {
  233. m_nID = a_nID;
  234. }
  235. // position (from field center manager)
  236. public System.Drawing.Point GetPosition()
  237. {
  238. return PoiPos;
  239. }
  240. public void SetPosition(System.Drawing.Point a_poiPos)
  241. {
  242. PoiPos = a_poiPos;
  243. }
  244. public List<COTSParticleClr> GetTopBorderedParticles()
  245. {
  246. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  247. foreach (var p in m_listParticles)
  248. {
  249. var segs = p.GetFeature().GetSegmentsList();//COTSSegment
  250. foreach (var seg in segs)
  251. {
  252. if (seg.GetHeight() == 0)
  253. {
  254. parts.Add(p);
  255. break;
  256. }
  257. }
  258. }
  259. return parts;
  260. }
  261. public List< COTSParticleClr > GetBottomBorderedParticles()
  262. {
  263. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  264. foreach (var p in m_listParticles)
  265. {
  266. var segs = p.GetFeature().GetSegmentsList();
  267. foreach (var seg in segs)
  268. {
  269. if (seg.GetHeight() == this.Height - 1)//the lowest height is 767(height-1),cause starting from 0.
  270. {
  271. parts.Add(p);
  272. break;
  273. }
  274. }
  275. }
  276. return parts;
  277. }
  278. public List<COTSParticleClr> GetLeftBorderedParticles()
  279. {
  280. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  281. foreach (var p in m_listParticles)
  282. {
  283. var segs = p.GetFeature().GetSegmentsList();
  284. foreach (var seg in segs)
  285. {
  286. if (seg.GetStart() == 0)
  287. {
  288. parts.Add(p);
  289. break;
  290. }
  291. }
  292. }
  293. return parts;
  294. }
  295. public List <COTSParticleClr> GetRightBorderedParticles()
  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.GetStart() + seg.GetLength() == this.Width)
  304. {
  305. parts.Add(p);
  306. break;
  307. }
  308. }
  309. }
  310. return parts;
  311. }
  312. // is empty
  313. public bool IsEmpty()
  314. {
  315. return m_listParticles.Count == 0;
  316. }
  317. void Duplicate( COTSFieldData a_oSource)
  318. {
  319. m_nID = a_oSource.m_nID;
  320. PoiPos = a_oSource.PoiPos;
  321. //m_strFieldFileFolder = a_oSource.m_strFieldFileFolder;
  322. // copy data over
  323. foreach(var pParticle in a_oSource.m_listParticles)
  324. {
  325. m_listParticles.Add(pParticle);
  326. }
  327. }
  328. }
  329. }