static XmlElement createInflextionNode(XmlDocument xml, Inflextion inf) { XmlElement newNode = xml.CreateElement("Inflextion"); XmlElement idNode = xml.CreateElement("id"); idNode.InnerText = inf.id.ToString(); newNode.AppendChild(idNode); XmlElement nameNode = xml.CreateElement("name"); nameNode.InnerText = inf.name; newNode.AppendChild(nameNode); XmlElement inflextionTypeNode = xml.CreateElement("inflextionType"); inflextionTypeNode.InnerText = inf.inflextionType; newNode.AppendChild(inflextionTypeNode); XmlElement xNode = xml.CreateElement("x"); xNode.InnerText = inf.x.ToString(); newNode.AppendChild(xNode); XmlElement yNode = xml.CreateElement("y"); yNode.InnerText = inf.y.ToString(); newNode.AppendChild(yNode); return(newNode); }
static void drawInflextions(Dictionary <int, List <Inflextion> > sparsedPreplots, string outpath) { List <Point> allPoints = new List <Point>(); foreach (int preplotId in sparsedPreplots.Keys) { foreach (Inflextion inf in sparsedPreplots[preplotId]) { allPoints.Add(new Point(inf.x, inf.y)); } } Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics g = Graphics.FromImage(bmp); g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bmp.Width, bmp.Height)); double minx = 999999999; double maxx = -999999999; double miny = 999999999; double maxy = -999999999; foreach (Point pt in allPoints) { if (pt.x < minx) { minx = pt.x; } if (pt.y < miny) { miny = pt.y; } if (pt.x > maxx) { maxx = pt.x; } if (pt.y > maxy) { maxy = pt.y; } } double scalex = bmp.Width / Math.Abs(maxx - minx); double scaley = -bmp.Height / Math.Abs(maxy - miny); System.Drawing.PointF[] thePoints = new System.Drawing.PointF[allPoints.Count]; foreach (int preplotId in sparsedPreplots.Keys) { for (int i = 0; i < sparsedPreplots[preplotId].Count - 1; i++) { float x1, y1, x2, y2; { Inflextion inf = sparsedPreplots[preplotId][i]; float x = (float)((inf.x - minx) * scalex); float y = (float)((inf.y - miny) * scaley + bmp.Height); x1 = x; y1 = y; } { Inflextion inf = sparsedPreplots[preplotId][i + 1]; float x = (float)((inf.x - minx) * scalex); float y = (float)((inf.y - miny) * scaley + bmp.Height); x2 = x; y2 = y; } g.DrawLine(new Pen(new SolidBrush(Color.Black)), x1, y1, x2, y2); } } foreach (Point pt in allPoints) { Point p = pt; g.FillRectangle(new SolidBrush(Color.Green), new Rectangle((int)((p.x - minx) * scalex), (int)((p.y - miny) * scaley + bmp.Height), 2, 2)); } bmp.Save(outpath); }
static void Main(string[] args) { PreplotSet preplotSet = new PreplotSet(); //初始化一个xml实例 XmlDocument xml = new XmlDocument(); xml.Load("..\\..\\test_data\\CSSE4D_streamer_simple.klmd"); XmlNodeList preplotsNodeList = xml.SelectNodes("/KLMarineData/KLMDPreplotSet/KLMDPreplot"); foreach (XmlNode preplotNode in preplotsNodeList) { Preplot preplot = new Preplot(); foreach (XmlNode node in preplotNode.ChildNodes) { XmlElement n = (XmlElement)node; if (n.Name == "id") { preplot.id = int.Parse(n.InnerText); } } XmlNodeList inflextionNodeList = preplotNode.SelectNodes("Inflextions/Inflextion"); foreach (XmlNode node in inflextionNodeList) { Inflextion inflextion = new Inflextion(); foreach (XmlNode attrNode in node.ChildNodes) { XmlElement n = (XmlElement)attrNode; if (n.Name == "id") { inflextion.id = int.Parse(n.InnerText); } else if (n.Name == "name") { inflextion.name = n.InnerText; } else if (n.Name == "inflextionType") { inflextion.inflextionType = n.InnerText; } else if (n.Name == "x") { inflextion.x = double.Parse(n.InnerText); } else if (n.Name == "y") { inflextion.y = double.Parse(n.InnerText); } else if (n.Name == "oX") { inflextion.x = double.Parse(n.InnerText); } else if (n.Name == "oY") { inflextion.y = double.Parse(n.InnerText); } } preplot.inflextions.Add(inflextion); } preplotSet.preplots.Add(preplot); } // 抽稀 double DEGREE = 10.0; Dictionary <int, List <Inflextion> > sparsedPreplots = new Dictionary <int, List <Inflextion> >(); foreach (Preplot preplot in preplotSet.preplots) { // 添加第一个点,第一个点必须在集合中 List <Inflextion> sparsedInfList = new List <Inflextion>(); sparsedInfList.Add(preplot.inflextions[0]); // 遍历后续点进行抽稀 for (int i = 1; i < preplot.inflextions.Count - 1; i++) { Inflextion inflextion1 = sparsedInfList[sparsedInfList.Count - 1]; // 最新抽稀的点 Inflextion inflextion2 = preplot.inflextions[i]; Inflextion inflextion3 = preplot.inflextions[i + 1]; Vector v1 = new Vector(inflextion1.x, inflextion1.y, inflextion2.x, inflextion2.y); Vector v2 = new Vector(inflextion2.x, inflextion2.y, inflextion3.x, inflextion3.y); if (Vector.IsSameStraight(v1, v2, DEGREE) == false) { sparsedInfList.Add(inflextion2); } } // 添加最后一个点 sparsedInfList.Add(preplot.inflextions[preplot.inflextions.Count - 1]); sparsedPreplots.Add(preplot.id, sparsedInfList); } // 剪裁xml foreach (XmlNode preplotNode in preplotsNodeList) { string preplotId = "-999"; foreach (XmlNode node in preplotNode.ChildNodes) { XmlElement n = (XmlElement)node; if (n.Name == "id") { preplotId = n.InnerText; } } XmlNodeList inflextionNodeList = preplotNode.SelectNodes("Inflextions/Inflextion"); XmlElement newNode = xml.CreateElement("Inflextions"); foreach (XmlNode node in inflextionNodeList) { string infId = "-99"; foreach (XmlNode attrNode in node.ChildNodes) { XmlElement n = (XmlElement)attrNode; if (n.Name == "id") { infId = n.InnerText; } } // 在集合中则保留,否则删除 Inflextion inf = getInflextionFromSparsed(preplotId, infId, sparsedPreplots); if (inf != null) { inf.inflextionType = "0"; newNode.AppendChild(createInflextionNode(xml, inf)); } } preplotNode.RemoveChild(preplotNode.SelectSingleNode("Inflextions")); preplotNode.AppendChild(newNode); } xml.Save("..\\..\\test_data\\CSSE4D_streamer_sparsed_DEGREE" + DEGREE + ".klmd"); // 画图 List <Point> points = new List <Point>(); foreach (int preplotId in sparsedPreplots.Keys) { foreach (Inflextion inf in sparsedPreplots[preplotId]) { points.Add(new Point(inf.x, inf.y)); } } drawInflextions(points, "..\\..\\test_data\\out-points-sparsed.bmp"); drawInflextions(sparsedPreplots, "..\\..\\test_data\\out-lines-sparsed.bmp"); Console.WriteLine("Dilute " + preplotSet.preplots.Count + " preplots"); }