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