COTSFieldData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 rMin = oAreaRange.GetStart() / 2f;
  111. double rMax = oAreaRange.GetEnd() / 2f;
  112. double dAreaLow = rMin*rMin * 3.14159;
  113. double dAreaHigh = rMax*rMax * 3.14159;
  114. // gray level range
  115. CIntRange oParticleGrayRange = a_pImageProcessParam.GetParticleGray();
  116. //here,no longer consider the gray range of particles,for we can make it by regulate the background range.
  117. //-------------------------------------------
  118. //int nGrayLow = oParticleGrayRange.GetStart();
  119. //int nGrayHigh = oParticleGrayRange.GetEnd();
  120. //-------------------------------------------
  121. // get particles list
  122. List<COTSParticleClr> listParticles =m_listParticles;
  123. // go through each particles
  124. int nTagId = 0;
  125. int nAnalysisPartId = 0;
  126. int tooSmallnum = 0;
  127. int overSizenum = 0;
  128. log.Info("Total Particles: " + listParticles.Count);
  129. foreach (COTSParticleClr pParticle in listParticles)
  130. {
  131. // get particle area
  132. double dPartArea = pParticle.GetArea();
  133. dPartArea = dPartArea * a_dPixelSize * a_dPixelSize;
  134. pParticle.SetArea(dPartArea);
  135. // get average gray level
  136. int nAveGrayLevel = pParticle.GetAveGray();
  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. // set particle tag id
  152. pParticle.SetTagId(nTagId++);
  153. // set field id
  154. pParticle.SetFieldId(GetId());
  155. // add the particle into the output particles list
  156. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.NO_ANALYSIS_X_RAY);
  157. pParticle.SetTypeName("Not Identified");
  158. pParticle.SetAnalysisId(nAnalysisPartId++);
  159. m_listAnalysisParticles.Add(pParticle);
  160. }
  161. log.Info("TooSmall Particles (<"+ dAreaLow .ToString("f2") + "): " + tooSmallnum);
  162. log.Info("OverSize Particles (>"+ dAreaHigh.ToString("f2") + "): " + overSizenum);
  163. }
  164. public bool CreateXrayList(List<COTSParticleClr> a_listParticles)
  165. {
  166. // go through particles list
  167. //a_listPosXRay.Clear();
  168. //int nXrayIndex = 0;
  169. foreach (COTSParticleClr pPart in a_listParticles)
  170. {
  171. pPart.CalXrayPos();
  172. // get xray position
  173. System.Drawing.Point poi = (System.Drawing.Point)pPart.GetXRayPos();
  174. // create a x-ray
  175. CPosXrayClr pPosXray = new CPosXrayClr();
  176. // set x-ray position
  177. pPosXray.SetPosition(poi);
  178. // set particle tag id
  179. pPosXray.SetPartTagId(pPart.GetTagId());
  180. //pPosXray.SetIndex(nXrayIndex++);
  181. pPosXray.SetIndex(pPart.GetAnalysisId());
  182. // set field id
  183. pPosXray.SetScanFieldId(pPart.GetFieldId());
  184. // add the x-ray into the list
  185. //a_listPosXRay.Add(pPosXray);
  186. pPart.SetXray(pPosXray);
  187. }
  188. return true;
  189. }
  190. public bool NoParticle()
  191. {
  192. if (m_listParticles.Count == 0)
  193. {
  194. return true;
  195. }
  196. else
  197. {
  198. return false;
  199. }
  200. }
  201. public bool NoAnalysisParticle()
  202. {
  203. if (m_listAnalysisParticles.Count == 0)
  204. {
  205. return true;
  206. }
  207. else
  208. {
  209. return false;
  210. }
  211. }
  212. public bool Equals(COTSFieldData a_oSource) // CBSEImg& operator=(const CBSEImg&); // =operator
  213. {
  214. int nSize = m_listParticles.Count;
  215. if (nSize != a_oSource.m_listParticles.Count)
  216. {
  217. return false;
  218. }
  219. for (int i = 0; i < nSize; ++i)
  220. {
  221. if (!m_listParticles[i] .Equals( a_oSource.m_listParticles[i]))
  222. {
  223. return false;
  224. }
  225. }
  226. return m_nID == a_oSource.m_nID &&
  227. PoiPos == a_oSource.PoiPos;
  228. }
  229. // destructor
  230. // ID
  231. public int GetId()
  232. {
  233. return m_nID;
  234. }
  235. public void SetId(int a_nID)
  236. {
  237. m_nID = a_nID;
  238. }
  239. // position (from field center manager)
  240. public System.Drawing.Point GetPosition()
  241. {
  242. return PoiPos;
  243. }
  244. public void SetPosition(System.Drawing.Point a_poiPos)
  245. {
  246. PoiPos = a_poiPos;
  247. }
  248. public List<COTSParticleClr> GetTopBorderedParticles()
  249. {
  250. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  251. foreach (var p in m_listParticles)
  252. {
  253. var segs = p.GetFeature().GetSegmentsList();//COTSSegment
  254. foreach (var seg in segs)
  255. {
  256. if (seg.GetHeight() == 0)
  257. {
  258. parts.Add(p);
  259. break;
  260. }
  261. }
  262. }
  263. return parts;
  264. }
  265. public List< COTSParticleClr > GetBottomBorderedParticles()
  266. {
  267. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  268. foreach (var p in m_listParticles)
  269. {
  270. var segs = p.GetFeature().GetSegmentsList();
  271. foreach (var seg in segs)
  272. {
  273. if (seg.GetHeight() == this.Height - 1)//the lowest height is 767(height-1),cause starting from 0.
  274. {
  275. parts.Add(p);
  276. break;
  277. }
  278. }
  279. }
  280. return parts;
  281. }
  282. public List<COTSParticleClr> GetLeftBorderedParticles()
  283. {
  284. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  285. foreach (var p in m_listParticles)
  286. {
  287. var segs = p.GetFeature().GetSegmentsList();
  288. foreach (var seg in segs)
  289. {
  290. if (seg.GetStart() == 0)
  291. {
  292. parts.Add(p);
  293. break;
  294. }
  295. }
  296. }
  297. return parts;
  298. }
  299. public List <COTSParticleClr> GetRightBorderedParticles()
  300. {
  301. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  302. foreach (var p in m_listParticles)
  303. {
  304. var segs = p.GetFeature().GetSegmentsList();
  305. foreach (var seg in segs)
  306. {
  307. if (seg.GetStart() + seg.GetLength() == this.Width)
  308. {
  309. parts.Add(p);
  310. break;
  311. }
  312. }
  313. }
  314. return parts;
  315. }
  316. // is empty
  317. public bool IsEmpty()
  318. {
  319. return m_listParticles.Count == 0;
  320. }
  321. void Duplicate( COTSFieldData a_oSource)
  322. {
  323. m_nID = a_oSource.m_nID;
  324. PoiPos = a_oSource.PoiPos;
  325. //m_strFieldFileFolder = a_oSource.m_strFieldFileFolder;
  326. // copy data over
  327. foreach(var pParticle in a_oSource.m_listParticles)
  328. {
  329. m_listParticles.Add(pParticle);
  330. }
  331. }
  332. }
  333. }