COTSFieldData.cs 13 KB

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