COTSFieldData.cs 12 KB

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