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 (>"+ dAreaLow .ToString("f2") + "): "+ "<" + dAreaHigh.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. // get xray position
  178. System.Drawing.Point poi = (System.Drawing.Point)pPart.GetXRayPos();
  179. // create a x-ray
  180. CPosXrayClr pPosXray = new CPosXrayClr();
  181. // set x-ray position
  182. pPosXray.SetPosition(poi);
  183. // set particle tag id
  184. pPosXray.SetPartTagId(pPart.GetTagId());
  185. //pPosXray.SetIndex(nXrayIndex++);
  186. pPosXray.SetIndex(pPart.GetAnalysisId());
  187. // set field id
  188. pPosXray.SetScanFieldId(pPart.GetFieldId());
  189. pPart.SetXray(pPosXray);
  190. }
  191. return true;
  192. }
  193. public bool NoParticle()
  194. {
  195. if (m_listAllParticles.Count == 0)
  196. {
  197. return true;
  198. }
  199. else
  200. {
  201. return false;
  202. }
  203. }
  204. public bool NoAnalysisParticle()
  205. {
  206. if (m_listAnalysisParticles.Count == 0)
  207. {
  208. return true;
  209. }
  210. else
  211. {
  212. return false;
  213. }
  214. }
  215. public bool Equals(COTSFieldData a_oSource) // CBSEImg& operator=(const CBSEImg&); // =operator
  216. {
  217. int nSize = m_listAllParticles.Count;
  218. if (nSize != a_oSource.m_listAllParticles.Count)
  219. {
  220. return false;
  221. }
  222. for (int i = 0; i < nSize; ++i)
  223. {
  224. if (!m_listAllParticles[i] .Equals( a_oSource.m_listAllParticles[i]))
  225. {
  226. return false;
  227. }
  228. }
  229. return m_nID == a_oSource.m_nID &&
  230. OTSPos == a_oSource.OTSPos;
  231. }
  232. // destructor
  233. // ID
  234. public int GetId()
  235. {
  236. return m_nID;
  237. }
  238. public void SetId(int a_nID)
  239. {
  240. m_nID = a_nID;
  241. }
  242. // position (from field center manager)
  243. public System.Drawing.Point GetOTSPosition()
  244. {
  245. return m_otsPos;
  246. }
  247. public void SetOTSPosition(System.Drawing.Point a_poiPos)
  248. {
  249. m_otsPos = a_poiPos;
  250. }
  251. public List<COTSParticleClr> GetTopBorderedParticles()
  252. {
  253. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  254. foreach (var p in m_listAllParticles)
  255. {
  256. var segs = p.GetFeature().GetSegmentsList();//COTSSegment
  257. foreach (var seg in segs)
  258. {
  259. if (seg.GetHeight() == 0)
  260. {
  261. parts.Add(p);
  262. break;
  263. }
  264. }
  265. }
  266. return parts;
  267. }
  268. public List< COTSParticleClr > GetBottomBorderedParticles()
  269. {
  270. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  271. foreach (var p in m_listAllParticles)
  272. {
  273. var segs = p.GetFeature().GetSegmentsList();
  274. foreach (var seg in segs)
  275. {
  276. if (seg.GetHeight() == this.Height - 1)//the lowest height is 767(height-1),cause starting from 0.
  277. {
  278. parts.Add(p);
  279. break;
  280. }
  281. }
  282. }
  283. return parts;
  284. }
  285. public List<COTSParticleClr> GetLeftBorderedParticles()
  286. {
  287. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  288. foreach (var p in m_listAllParticles)
  289. {
  290. var segs = p.GetFeature().GetSegmentsList();
  291. foreach (var seg in segs)
  292. {
  293. if (seg.GetStart() == 0)
  294. {
  295. parts.Add(p);
  296. break;
  297. }
  298. }
  299. }
  300. return parts;
  301. }
  302. public List <COTSParticleClr> GetRightBorderedParticles()
  303. {
  304. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  305. foreach (var p in m_listAllParticles)
  306. {
  307. var segs = p.GetFeature().GetSegmentsList();
  308. foreach (var seg in segs)
  309. {
  310. if (seg.GetStart() + seg.GetLength() == this.Width)
  311. {
  312. parts.Add(p);
  313. break;
  314. }
  315. }
  316. }
  317. return parts;
  318. }
  319. // is empty
  320. public bool IsEmpty()
  321. {
  322. return m_listAllParticles.Count == 0;
  323. }
  324. void Duplicate( COTSFieldData a_oSource)
  325. {
  326. m_nID = a_oSource.m_nID;
  327. OTSPos = a_oSource.OTSPos;
  328. //m_strFieldFileFolder = a_oSource.m_strFieldFileFolder;
  329. // copy data over
  330. foreach(var pParticle in a_oSource.m_listAllParticles)
  331. {
  332. m_listAllParticles.Add(pParticle);
  333. }
  334. }
  335. }
  336. }