COTSFieldData.cs 13 KB

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