COTSFieldData.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 /2;
  239. var yoffSet = height * m_pixelSize/2 ;
  240. if (a_oSource.m_otsPos.X < (m_otsPos.X+xoffSet) && a_oSource.m_otsPos.X>( m_otsPos.X-xoffSet))
  241. {
  242. if (a_oSource.m_otsPos.Y < (m_otsPos.Y + yoffSet) && a_oSource.m_otsPos.Y > (m_otsPos.Y - yoffSet))
  243. {
  244. return true;
  245. }
  246. return false;
  247. }
  248. else
  249. {
  250. return false;
  251. }
  252. //return OTSPos.X == a_oSource.m_otsPos.X && OTSPos.Y == a_oSource.m_otsPos.Y;
  253. }
  254. // ID
  255. public int GetId()
  256. {
  257. return m_nID;
  258. }
  259. public void SetId(int a_nID)
  260. {
  261. m_nID = a_nID;
  262. }
  263. // position (from field center manager)
  264. public System.Drawing.PointF GetOTSPosition()
  265. {
  266. return m_otsPos;
  267. }
  268. public void SetOTSPosition(System.Drawing.PointF a_poiPos)
  269. {
  270. m_otsPos = a_poiPos;
  271. }
  272. public List<COTSParticleClr> GetTopBorderedParticles()
  273. {
  274. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  275. foreach (var p in m_listAllParticles)
  276. {
  277. var segs = p.GetFeature().GetSegmentsList();//COTSSegment
  278. foreach (var seg in segs)
  279. {
  280. if (seg.GetHeight() == 0)
  281. {
  282. parts.Add(p);
  283. break;
  284. }
  285. }
  286. }
  287. return parts;
  288. }
  289. public List< COTSParticleClr > GetBottomBorderedParticles()
  290. {
  291. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  292. foreach (var p in m_listAllParticles)
  293. {
  294. var segs = p.GetFeature().GetSegmentsList();
  295. foreach (var seg in segs)
  296. {
  297. if (seg.GetHeight() == this.Height - 1)//the lowest height is 767(height-1),cause starting from 0.
  298. {
  299. parts.Add(p);
  300. break;
  301. }
  302. }
  303. }
  304. return parts;
  305. }
  306. public List<COTSParticleClr> GetLeftBorderedParticles()
  307. {
  308. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  309. foreach (var p in m_listAllParticles)
  310. {
  311. var segs = p.GetFeature().GetSegmentsList();
  312. foreach (var seg in segs)
  313. {
  314. if (seg.GetStart() == 0)
  315. {
  316. parts.Add(p);
  317. break;
  318. }
  319. }
  320. }
  321. return parts;
  322. }
  323. public List <COTSParticleClr> GetRightBorderedParticles()
  324. {
  325. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  326. foreach (var p in m_listAllParticles)
  327. {
  328. var segs = p.GetFeature().GetSegmentsList();
  329. foreach (var seg in segs)
  330. {
  331. if (seg.GetStart() + seg.GetLength() == this.Width)
  332. {
  333. parts.Add(p);
  334. break;
  335. }
  336. }
  337. }
  338. return parts;
  339. }
  340. // is empty
  341. public bool IsEmpty()
  342. {
  343. return m_listAllParticles.Count == 0;
  344. }
  345. void Duplicate( COTSFieldData a_oSource)
  346. {
  347. m_nID = a_oSource.m_nID;
  348. OTSPos = a_oSource.OTSPos;
  349. // copy data over
  350. foreach(var pParticle in a_oSource.m_listAllParticles)
  351. {
  352. m_listAllParticles.Add(pParticle);
  353. }
  354. }
  355. public override void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode)
  356. {
  357. xPoint xPos = new xPoint();
  358. Slo slo = new Slo();
  359. slo.Register("OTSPosition", xPos);
  360. if (isStoring)
  361. {
  362. xPos.AssignValue(new Point((int)m_otsPos.X,(int)m_otsPos.Y));
  363. slo.Serialize(true, classDoc, rootNode);
  364. }
  365. else
  366. {
  367. slo.Serialize(false, classDoc, rootNode);
  368. m_otsPos = xPos.value();
  369. }
  370. }
  371. }
  372. }