COTSFieldData.cs 13 KB

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