using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OTSExtremum.Business { public class Classify { public enum Type { A = 0, B = 1, C = 2, D = 3, DS = 4 } public static Dictionary> getClassByGroup(string path) { Data.ParticleData particleData = new Data.ParticleData(path); DataTable particles = particleData.GetParticleListAndEm(); List A = new List(); List B = new List(); List C = new List(); List D = new List(); List DS = new List(); DataTable BD = particles.Clone(); foreach (DataRow item in particles.Rows) { if (Convert.ToInt32(item["RectWidth"]) == 0) { continue; } //获取最大长度和最小宽度 double h = Convert.ToDouble(item["DMAX"]); double w = Convert.ToDouble(item["DMIN"]); double dLengthWidthRatio = h / w; if (dLengthWidthRatio < 1) { dLengthWidthRatio = 1 / dLengthWidthRatio; } if (dLengthWidthRatio >= 3)//长宽比大于3的颗粒,根据化学元素不同,分为A类和C类 { //A or C class string element = item["Element"].ToString(); if (element.IndexOf("S-") > -1) { A.Add(Convert.ToDouble(item["PERIMETER"])); } else if (element.IndexOf("O-") > -1) { C.Add(Convert.ToDouble(item["PERIMETER"])); } } else//长宽比小于3的颗粒,有3种情况,一种是串条状的B类颗粒,一种是单独的D类颗粒,如果费雷特直径大于13则为DS类颗粒 { // B, or D or DS // compute Feret's diameter double dFeretDiameter = Convert.ToDouble(item["PERIMETER"]); if (dFeretDiameter >= 13) { // DS DS.Add(Convert.ToDouble(item["PERIMETER"])); } else { //不能确定是B或D,先设为INVALID BD.Rows.Add(item.ItemArray); } } } foreach (DataRow item in BD.Rows) { double xs = 1142 / 1024; int left = Convert.ToInt32(item["RectLeft"]); int top = Convert.ToInt32(item["RectTop"]); int width = Convert.ToInt32(item["RectWidth"]); int height = Convert.ToInt32(item["RectHeight"]); int cenX = left + width / 2; int Bottom = top - height; bool isline = false; for (int i = 0; i < BD.Rows.Count; i++) { int leftB = Convert.ToInt32(item["RectLeft"]); int topB = Convert.ToInt32(item["RectTop"]); int widthB = Convert.ToInt32(item["RectWidth"]); int heightB = Convert.ToInt32(item["RectHeight"]); int cenBX = leftB + widthB / 2; int BottomB = topB - heightB; double dd = 0, ds = 0; ds = Math.Abs(cenX - cenBX); if (ds * xs < 10)//认为两个颗粒在一条竖直线上,但不在一起 { if (Bottom > topB)//current particle is on the above { dd = Bottom - topB; if (dd * xs < 40)//认为这两个颗粒在一个串条上 { isline = true; break; } } else if (BottomB > top) //current particle is on the below { dd = BottomB - top; if (dd * xs < 40) { isline = true; break; } } } } if (isline) { B.Add(Convert.ToDouble(item["PERIMETER"])); } else { D.Add(Convert.ToDouble(item["PERIMETER"])); } } SetZeroBackfill(particles.Rows.Count, ref A); SetZeroBackfill(particles.Rows.Count, ref B); SetZeroBackfill(particles.Rows.Count, ref C); SetZeroBackfill(particles.Rows.Count, ref D); SetZeroBackfill(particles.Rows.Count, ref DS); Dictionary> dic = new Dictionary>(); dic.Add((int)Type.A, A); dic.Add((int)Type.B, B); dic.Add((int)Type.C, C); dic.Add((int)Type.D, D); dic.Add((int)Type.DS, DS); return dic; } public static double[] getClass(string path) { Data.ParticleData particleData = new Data.ParticleData(path); DataTable particles = particleData.GetParticleListAndEm(); double A=0, B=0, C=0, D=0, DS = 0; DataTable BD = particles.Clone(); foreach (DataRow item in particles.Rows) { if (Convert.ToInt32(item["RectWidth"]) == 0) { continue; } //获取最大长度和最小宽度 double h = Convert.ToDouble(item["DMAX"]); double w = Convert.ToDouble(item["DMIN"]); double dLengthWidthRatio = h / w; if (dLengthWidthRatio < 1) { dLengthWidthRatio = 1 / dLengthWidthRatio; } if (dLengthWidthRatio >= 3)//长宽比大于3的颗粒,根据化学元素不同,分为A类和C类 { //A or C class string element = item["Element"].ToString(); if (element.IndexOf("S-") > -1) { if (Convert.ToDouble(item["PERIMETER"]) > A) { A = Convert.ToDouble(item["PERIMETER"]); } } else if (element.IndexOf("O-") > -1) { if (Convert.ToDouble(item["PERIMETER"]) > C) { C = Convert.ToDouble(item["PERIMETER"]); } } } else//长宽比小于3的颗粒,有3种情况,一种是串条状的B类颗粒,一种是单独的D类颗粒,如果费雷特直径大于13则为DS类颗粒 { // B, or D or DS // compute Feret's diameter double dFeretDiameter = Convert.ToDouble(item["PERIMETER"]); if (dFeretDiameter >= 13) { // DS if (Convert.ToDouble(item["PERIMETER"]) > DS) { DS = Convert.ToDouble(item["PERIMETER"]); } } else { //不能确定是B或D,先设为INVALID BD.Rows.Add(item.ItemArray); } } } foreach (DataRow item in BD.Rows) { double xs = 1142 / 1024; int left = Convert.ToInt32(item["RectLeft"]); int top = Convert.ToInt32(item["RectTop"]); int width = Convert.ToInt32(item["RectWidth"]); int height = Convert.ToInt32(item["RectHeight"]); int cenX = left + width / 2; int Bottom = top- height; bool isline = false; for (int i = 0; i < BD.Rows.Count; i++) { int leftB = Convert.ToInt32(item["RectLeft"]); int topB = Convert.ToInt32(item["RectTop"]); int widthB = Convert.ToInt32(item["RectWidth"]); int heightB = Convert.ToInt32(item["RectHeight"]); int cenBX = leftB + widthB / 2; int BottomB = topB - heightB; double dd = 0, ds = 0; ds =Math.Abs(cenX - cenBX); if (ds* xs < 10)//认为两个颗粒在一条竖直线上,但不在一起 { if (Bottom > topB)//current particle is on the above { dd = Bottom - topB; if (dd* xs < 40)//认为这两个颗粒在一个串条上 { isline= true; break; } } else if (BottomB > top) //current particle is on the below { dd = BottomB - top; if (dd* xs < 40) { isline= true; break; } } } } if (isline) { if (Convert.ToDouble(item["PERIMETER"]) > B) { B = Convert.ToDouble(item["PERIMETER"]); } } else { if (Convert.ToDouble(item["PERIMETER"]) > D) { D = Convert.ToDouble(item["PERIMETER"]); } } } return new double[] {A,B,C,D,DS}; } static void SetZeroBackfill(int count,ref List ll) { for (int i = ll.Count; i < count; i++) { ll.Add(0); } ll.Sort((x, y) => -x.CompareTo(y)); } } }