using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Drawing; using OTSModelSharp.ImageProcess; using OTSModelSharp.ServiceInterface; using OTSDataType; using OTSCLRINTERFACE; namespace OTSModelSharp { using COTSFieldDataList = List; public class COTSFieldData { protected NLog.Logger log ; // ID int m_nID; // position (from field center manager) protected System.Drawing.Point m_poiPos ; protected CBSEImgClr m_pBSEImg; protected double m_pixelSize; IImageProcess m_ImagePro; protected List m_listParticles = new List();//hold up all the particles separated from bse image; protected List m_listAnalysisParticles = new List();// according to the size (the diameter) constraint,pick out all the conform particles. protected List m_listXrayParticles = new List();//hold up all the particles that need the xray data. private int height; private int width; public int Height { get => height; set => height = value; } public int Width { get => width; set => width = value; } public System.Drawing.Point PoiPos { get => m_poiPos; set => m_poiPos = value; } public List GetListAnalysisParticles() { return m_listAnalysisParticles; } public List GetListXrayParticles() { return m_listXrayParticles; } public void SetListAnalysisParticles(List value) { m_listAnalysisParticles = value; } // particle list public COTSFieldData(CBSEImgClr a_pBSEImg, double a_dPixelSize) { log = NLog.LogManager.GetCurrentClassLogger(); Init(); m_pBSEImg = a_pBSEImg; m_pixelSize = a_dPixelSize; width = a_pBSEImg.GetWidth(); height = a_pBSEImg.GetHeight(); } public COTSFieldData() { log = NLog.LogManager.GetCurrentClassLogger(); Init(); } // initialization void Init() { // initialization m_nID = -1; PoiPos =new System.Drawing.Point(0, 0); m_listParticles.Clear(); } public void CCOTSFieldData(COTSFieldData a_oSource) { // can't copy itself if (a_oSource == this) { return; } Duplicate(a_oSource); } public COTSFieldData(COTSFieldData a_poSource) { // can't copy itself if (a_poSource == this) { return; } Duplicate(a_poSource); } public CBSEImgClr GetBSEImage() { return m_pBSEImg; } public void SetBSEImage(CBSEImgClr a_pBSEImg) { if (a_pBSEImg == null) { // invalid BSE image. log.Error("SetBSEImage: invalid BSE image."); return; } m_pBSEImg = a_pBSEImg; } public void RemoveBSEImageBG(COTSImageProcParam a_pImageProcessParam) { CImageHandler imghandler = new CImageHandler(); List xrayParts = new List(); imghandler.RemoveBSEImageBG(m_pBSEImg, a_pImageProcessParam, ref xrayParts); foreach (var p in xrayParts) { m_listParticles.Add(p); m_listXrayParticles.Add(p); } return ; } public void GetPartsBySpecialGray(CIntRangeClr range,bool ifXray) { CImageHandler imghandler = new CImageHandler(); List specialParts = new List(); imghandler.GetParticlesBySpecialGray(m_pBSEImg, range, ref specialParts); foreach (var p in specialParts) { m_listParticles.Add(p); if (ifXray) { m_listXrayParticles.Add(p); } } return; } public bool CalParticleImageProp(List particles) { m_ImagePro = new CImageHandler(); foreach (COTSParticleClr part in particles) { m_ImagePro.CalParticleImageProp( part, m_pixelSize); } return true; } public void FilterParticles(COTSImageProcParam a_pImageProcessParam, double a_dPixelSize) { m_listAnalysisParticles = new List(); // get area range CDoubleRange oAreaRange = a_pImageProcessParam.GetIncAreaRange(); double rMin = oAreaRange.GetStart() / 2f; double rMax = oAreaRange.GetEnd() / 2f; double dAreaLow = rMin*rMin * 3.14159; double dAreaHigh = rMax*rMax * 3.14159; // gray level range CIntRange oParticleGrayRange = a_pImageProcessParam.GetParticleGray(); //here,no longer consider the gray range of particles,for we can make it by regulate the background range. //------------------------------------------- //int nGrayLow = oParticleGrayRange.GetStart(); //int nGrayHigh = oParticleGrayRange.GetEnd(); //------------------------------------------- // get particles list // go through each particles int nTagId = 0; int nAnalysisPartId = 0; int tooSmallnum = 0; int overSizenum = 0; log.Info("Total Particles: " + m_listParticles.Count); foreach (COTSParticleClr pParticle in m_listParticles) { // get particle area double dPartArea = pParticle.GetArea(); dPartArea = dPartArea * a_dPixelSize * a_dPixelSize; pParticle.SetArea(dPartArea); // get average gray level int nAveGrayLevel = pParticle.GetAveGray(); // oversize particles if (dPartArea > (double)dAreaHigh) { pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.OVERSIZE); overSizenum += 1; continue; } // too small to measure else if (dPartArea < (double)dAreaLow) { pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.SMALL); tooSmallnum += 1; continue; } // set particle tag id pParticle.SetTagId(nTagId++); // set field id pParticle.SetFieldId(GetId()); // add the particle into the output particles list pParticle.SetType((int)otsdataconst.OTS_PARTCLE_TYPE.NO_ANALYSIS_X_RAY); pParticle.SetTypeName("Not Identified"); pParticle.SetAnalysisId(nAnalysisPartId++); m_listAnalysisParticles.Add(pParticle); } log.Info("TooSmall Particles (<"+ dAreaLow .ToString("f2") + "): " + tooSmallnum); log.Info("OverSize Particles (>"+ dAreaHigh.ToString("f2") + "): " + overSizenum); } public bool CreateXrayList(List a_listParticles) { // go through particles list //a_listPosXRay.Clear(); //int nXrayIndex = 0; foreach (COTSParticleClr pPart in a_listParticles) { pPart.CalXrayPos(); // get xray position System.Drawing.Point poi = (System.Drawing.Point)pPart.GetXRayPos(); // create a x-ray CPosXrayClr pPosXray = new CPosXrayClr(); // set x-ray position pPosXray.SetPosition(poi); // set particle tag id pPosXray.SetPartTagId(pPart.GetTagId()); //pPosXray.SetIndex(nXrayIndex++); pPosXray.SetIndex(pPart.GetAnalysisId()); // set field id pPosXray.SetScanFieldId(pPart.GetFieldId()); // add the x-ray into the list //a_listPosXRay.Add(pPosXray); pPart.SetXray(pPosXray); } return true; } public bool NoParticle() { if (m_listParticles.Count == 0) { return true; } else { return false; } } public bool NoAnalysisParticle() { if (m_listAnalysisParticles.Count == 0) { return true; } else { return false; } } public bool Equals(COTSFieldData a_oSource) // CBSEImg& operator=(const CBSEImg&); // =operator { int nSize = m_listParticles.Count; if (nSize != a_oSource.m_listParticles.Count) { return false; } for (int i = 0; i < nSize; ++i) { if (!m_listParticles[i] .Equals( a_oSource.m_listParticles[i])) { return false; } } return m_nID == a_oSource.m_nID && PoiPos == a_oSource.PoiPos; } // destructor // ID public int GetId() { return m_nID; } public void SetId(int a_nID) { m_nID = a_nID; } // position (from field center manager) public System.Drawing.Point GetPosition() { return PoiPos; } public void SetPosition(System.Drawing.Point a_poiPos) { PoiPos = a_poiPos; } public List GetTopBorderedParticles() { List parts = new List(); foreach (var p in m_listParticles) { var segs = p.GetFeature().GetSegmentsList();//COTSSegment foreach (var seg in segs) { if (seg.GetHeight() == 0) { parts.Add(p); break; } } } return parts; } public List< COTSParticleClr > GetBottomBorderedParticles() { List parts = new List(); foreach (var p in m_listParticles) { var segs = p.GetFeature().GetSegmentsList(); foreach (var seg in segs) { if (seg.GetHeight() == this.Height - 1)//the lowest height is 767(height-1),cause starting from 0. { parts.Add(p); break; } } } return parts; } public List GetLeftBorderedParticles() { List parts = new List(); foreach (var p in m_listParticles) { var segs = p.GetFeature().GetSegmentsList(); foreach (var seg in segs) { if (seg.GetStart() == 0) { parts.Add(p); break; } } } return parts; } public List GetRightBorderedParticles() { List parts = new List(); foreach (var p in m_listParticles) { var segs = p.GetFeature().GetSegmentsList(); foreach (var seg in segs) { if (seg.GetStart() + seg.GetLength() == this.Width) { parts.Add(p); break; } } } return parts; } // is empty public bool IsEmpty() { return m_listParticles.Count == 0; } void Duplicate( COTSFieldData a_oSource) { m_nID = a_oSource.m_nID; PoiPos = a_oSource.PoiPos; //m_strFieldFileFolder = a_oSource.m_strFieldFileFolder; // copy data over foreach(var pParticle in a_oSource.m_listParticles) { m_listParticles.Add(pParticle); } } } }