COTSFieldData.cs 11 KB

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