COTSFieldData.cs 13 KB

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