COTSFieldData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_otsPos ;
  22. protected CBSEImgClr m_pBSEImg;
  23. protected double m_pixelSize;
  24. IImageProcess m_ImagePro;
  25. protected List<COTSParticleClr> m_listAllParticles = new List<COTSParticleClr>();//hold up all the particles abstracted from bse image;
  26. protected List<COTSParticleClr> m_listAnalysisParticles = new List<COTSParticleClr>();// according to the size (the diameter) constraint,pick out all the conform particles.
  27. protected List<COTSParticleClr> m_listXrayParticles = new List<COTSParticleClr>();//hold up all the particles that need the xray data.
  28. private int height;
  29. private int width;
  30. public int Height { get => height; set => height = value; }
  31. public int Width { get => width; set => width = value; }
  32. public System.Drawing.Point OTSPos { get => m_otsPos; set => m_otsPos = value; }
  33. public Point SemPos { get => m_semPos; set => m_semPos = value; }
  34. private Point m_semPos;
  35. public List<COTSParticleClr> GetListAnalysisParticles()
  36. {
  37. return m_listAnalysisParticles;
  38. }
  39. public List<COTSParticleClr> GetListXrayParticles()
  40. {
  41. return m_listXrayParticles;
  42. }
  43. public void SetListAnalysisParticles(List<COTSParticleClr> value)
  44. {
  45. m_listAnalysisParticles = value;
  46. }
  47. public COTSFieldData(CBSEImgClr a_pBSEImg, double a_dPixelSize)
  48. {
  49. log = NLog.LogManager.GetCurrentClassLogger();
  50. Init();
  51. m_pBSEImg = a_pBSEImg;
  52. m_pixelSize = a_dPixelSize;
  53. width = a_pBSEImg.GetWidth();
  54. height = a_pBSEImg.GetHeight();
  55. }
  56. public COTSFieldData()
  57. {
  58. log = NLog.LogManager.GetCurrentClassLogger();
  59. Init();
  60. }
  61. // initialization
  62. void Init()
  63. {
  64. // initialization
  65. m_nID = -1;
  66. OTSPos =new System.Drawing.Point(0, 0);
  67. m_listAllParticles.Clear();
  68. }
  69. public void CCOTSFieldData(COTSFieldData a_oSource)
  70. {
  71. // can't copy itself
  72. if (a_oSource == this)
  73. {
  74. return;
  75. }
  76. Duplicate(a_oSource);
  77. }
  78. public COTSFieldData(COTSFieldData a_poSource)
  79. {
  80. // can't copy itself
  81. if (a_poSource == this)
  82. {
  83. return;
  84. }
  85. Duplicate(a_poSource);
  86. }
  87. public CBSEImgClr GetBSEImage()
  88. {
  89. return m_pBSEImg;
  90. }
  91. public void SetBSEImage(CBSEImgClr a_pBSEImg)
  92. {
  93. if (a_pBSEImg == null)
  94. {
  95. // invalid BSE image.
  96. log.Error("SetBSEImage: invalid BSE image.");
  97. return;
  98. }
  99. m_pBSEImg = a_pBSEImg;
  100. }
  101. public void RemoveImgBGAndGetParticles(COTSImageProcParam a_pImageProcessParam,double a_pixelSize)
  102. {
  103. CImageHandler imghandler = new CImageHandler();
  104. List<COTSParticleClr> xrayParts = new List<COTSParticleClr>();
  105. imghandler.RemoveBGAndGetParts(m_pBSEImg, a_pImageProcessParam,a_pixelSize, ref xrayParts);
  106. foreach (var p in xrayParts)
  107. {
  108. m_listAllParticles.Add(p);
  109. m_listXrayParticles.Add(p);
  110. }
  111. return ;
  112. }
  113. public void GetPartsBySpecialGray(CIntRangeClr grayRange, CIntRangeClr diameterRange,double pixelSize, bool ifXray)
  114. {
  115. CImageHandler imghandler = new CImageHandler();
  116. List<COTSParticleClr> specialParts = new List<COTSParticleClr>();
  117. imghandler.GetParticlesBySpecialGray(m_pBSEImg, grayRange,diameterRange,pixelSize, ref specialParts);
  118. foreach (var p in specialParts)
  119. {
  120. m_listAllParticles.Add(p);
  121. if (ifXray)
  122. {
  123. m_listXrayParticles.Add(p);
  124. }
  125. }
  126. return;
  127. }
  128. public bool CalParticleImageProp(List<COTSParticleClr> particles)
  129. {
  130. m_ImagePro = new CImageHandler();
  131. foreach (COTSParticleClr part in particles)
  132. {
  133. m_ImagePro.CalParticleImageProp( part, m_pixelSize);
  134. }
  135. return true;
  136. }
  137. public void InitParticles(COTSImageProcParam a_pImageProcessParam,double a_dPixelSize)
  138. {
  139. m_listAnalysisParticles = new List<COTSParticleClr>();
  140. // get area range
  141. CDoubleRange oAreaRange = a_pImageProcessParam.GetIncAreaRange();
  142. double rMin = oAreaRange.GetStart() / 2.0;
  143. double rMax = oAreaRange.GetEnd() / 2.0;
  144. double dAreaLow = rMin * rMin * 3.14159;
  145. double dAreaHigh = rMax * rMax * 3.14159;
  146. int nTagId = 0;
  147. //int nAnalysisPartId = 0;
  148. int tooSmallnum = 0;
  149. int overSizenum = 0;
  150. log.Info("Total Particles: " + m_listAllParticles.Count);
  151. foreach (COTSParticleClr pParticle in m_listAllParticles)//m_listAllParticles memorize all the particles .
  152. {
  153. pParticle.SetTagId(nTagId);//give all the conforming particles a unified sequence no.
  154. pParticle.SetFieldId(GetId());
  155. pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.NO_ANALYSIS_X_RAY);
  156. pParticle.SetTypeName("Not Identified");
  157. pParticle.SetAnalysisId(nTagId); // the same as the tagId no use now.
  158. nTagId++;
  159. m_listAnalysisParticles.Add(pParticle);
  160. }
  161. log.Info("Normal Particles (>"+ oAreaRange.GetStart().ToString("f2") + "): "+ "<" + oAreaRange.GetEnd().ToString("f2") + "): " + m_listAnalysisParticles.Count);
  162. List<COTSParticleClr> xrayParts = new List<COTSParticleClr>();
  163. foreach (var p in m_listXrayParticles)//m_listXrayParticles memorize all the particles which needs xray analysis.
  164. {
  165. if (p.GetTagId() > -1)//it has been initialized and it's a valid particle.
  166. {
  167. xrayParts.Add(p);
  168. }
  169. }
  170. m_listXrayParticles = xrayParts;
  171. }
  172. public bool CreateXrayList(List<COTSParticleClr> a_listParticles)
  173. {
  174. ;
  175. foreach (COTSParticleClr pPart in a_listParticles)
  176. {
  177. System.Drawing.Point poi = (System.Drawing.Point)pPart.GetXRayPos();
  178. CPosXrayClr pPosXray = new CPosXrayClr();
  179. pPosXray.SetPosition(poi);
  180. pPosXray.SetPartTagId(pPart.GetTagId());
  181. pPosXray.SetIndex(pPart.GetAnalysisId());
  182. pPosXray.SetScanFieldId(pPart.GetFieldId());
  183. pPart.SetXray(pPosXray);
  184. }
  185. return true;
  186. }
  187. public bool NoParticle()
  188. {
  189. if (m_listAllParticles.Count == 0)
  190. {
  191. return true;
  192. }
  193. else
  194. {
  195. return false;
  196. }
  197. }
  198. public bool NoAnalysisParticle()
  199. {
  200. if (m_listAnalysisParticles.Count == 0)
  201. {
  202. return true;
  203. }
  204. else
  205. {
  206. return false;
  207. }
  208. }
  209. public bool Equals(COTSFieldData a_oSource) // CBSEImg& operator=(const CBSEImg&); // =operator
  210. {
  211. int nSize = m_listAllParticles.Count;
  212. if (nSize != a_oSource.m_listAllParticles.Count)
  213. {
  214. return false;
  215. }
  216. for (int i = 0; i < nSize; ++i)
  217. {
  218. if (!m_listAllParticles[i] .Equals( a_oSource.m_listAllParticles[i]))
  219. {
  220. return false;
  221. }
  222. }
  223. return m_nID == a_oSource.m_nID &&
  224. OTSPos == a_oSource.OTSPos;
  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 GetOTSPosition()
  238. {
  239. return m_otsPos;
  240. }
  241. public void SetOTSPosition(System.Drawing.Point a_poiPos)
  242. {
  243. m_otsPos = a_poiPos;
  244. }
  245. public List<COTSParticleClr> GetTopBorderedParticles()
  246. {
  247. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  248. foreach (var p in m_listAllParticles)
  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_listAllParticles)
  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_listAllParticles)
  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_listAllParticles)
  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_listAllParticles.Count == 0;
  317. }
  318. void Duplicate( COTSFieldData a_oSource)
  319. {
  320. m_nID = a_oSource.m_nID;
  321. OTSPos = a_oSource.OTSPos;
  322. //m_strFieldFileFolder = a_oSource.m_strFieldFileFolder;
  323. // copy data over
  324. foreach(var pParticle in a_oSource.m_listAllParticles)
  325. {
  326. m_listAllParticles.Add(pParticle);
  327. }
  328. }
  329. }
  330. }