private void renderToView() { Graphics g = Graphics.FromImage(svgView); Pen pen = new Pen(Color.Black); pen.Color = Color.Red; foreach (Tuple <Int32, Int32> edge in edgeList) { PointFloat point1 = vertexList[edge.Item1]; PointFloat point2 = vertexList[edge.Item2]; g.DrawLine(pen, point1.x, point1.y, point2.x, point2.y); } pen.Color = Color.Blue; foreach (PointFloat point in vertexList) { g.FillEllipse(pen.Brush, point.x - 2, point.y - 2, 4, 4); } }
private void exportToOBJToolStripMenuItem_Click(object sender, EventArgs e) { List <int[]> faceList = new List <int[]>(); List <PointFloat> outputList = new List <PointFloat>(); Dictionary <int, int> vertexCache = new Dictionary <int, int>(); float wallHeight = (float)numericUpDown1.Value * FOOT_TO_METER; float minX = vertexList[0].x; float maxX = vertexList[0].x; float minY = vertexList[0].y; float maxY = vertexList[0].y; foreach (PointFloat point in vertexList) { outputList.Add(point); if (point.x > maxX) { maxX = point.x; } if (point.x < minX) { minX = point.x; } if (point.y > maxY) { maxY = point.y; } if (point.y < minY) { minY = point.y; } } float footPerPixel = (float)numericUpDown2.Value / (maxX - minX); foreach (Tuple <int, int> edge in edgeList) { int[] newFace = new int[4]; PointFloat vertex1 = vertexList[edge.Item1]; PointFloat vertex2 = vertexList[edge.Item2]; if (vertexCache.ContainsKey(edge.Item2)) { newFace[2] = vertexCache[edge.Item2]; } else { outputList.Add(new PointFloat(vertex2.x, vertex2.y, wallHeight)); newFace[2] = outputList.Count - 1; vertexCache.Add(edge.Item2, newFace[2]); } if (vertexCache.ContainsKey(edge.Item1)) { newFace[3] = vertexCache[edge.Item1]; } else { outputList.Add(new PointFloat(vertex1.x, vertex1.y, wallHeight)); newFace[3] = outputList.Count - 1; vertexCache.Add(edge.Item1, newFace[3]); } newFace[0] = edge.Item1 + 1; newFace[1] = edge.Item2 + 1; newFace[2]++; newFace[3]++; faceList.Add(newFace); } String outputString = "o Walls\n"; foreach (PointFloat vertex in outputList) { outputString += "v " + ((vertex.x - minX) * footPerPixel * FOOT_TO_METER) + " " + vertex.z // z is already in meter. + " " + ((vertex.y - minY) * footPerPixel * FOOT_TO_METER) + "\n"; } foreach (int[] face in faceList) { outputString += "f"; foreach (int vertex in face) { outputString += " " + vertex; } outputString += "\n"; } if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { File.WriteAllText(saveFileDialog1.FileName, outputString); } }
private void xmlToVertex() { string token = ""; try { var svgElement = svgXMLDoc.Elements().Where(s => s.Name.LocalName == "svg").FirstOrDefault(); int width = (int)float.Parse(Regex.Replace(svgElement.Attribute("width").Value, "[^0-9\\-\\.]", "")); int height = (int)float.Parse(Regex.Replace(svgElement.Attribute("height").Value, "[^0-9\\-\\.]", "")); svgView = new Bitmap(width, height); pictureBox1.Image = svgView; System.Diagnostics.Debug.WriteLine(width + "x" + height); float translateX = 0, translateY = 0, scaleX = 1, scaleY = 1; var paths = svgElement.Descendants().Where(s => s.Name.LocalName == "path"); vertexList.Clear(); edgeList.Clear(); foreach (var path in paths) { if (path.Parent.Name.LocalName == "g") { System.Diagnostics.Debug.WriteLine(path.Parent.Name); string transform = path.Parent.Attribute("transform").Value; Match translateMatch = Regex.Match(transform, "translate[^\\)]+\\)"); if (translateMatch != null) { string translate = translateMatch.ToString().Substring("translate(".Length); translate = translate.Substring(0, translate.Length - 1); string[] args = translate.Split(",".ToCharArray()); translateX = float.Parse(args[0].Trim()); translateY = float.Parse(args[1].Trim()); } Match scaleMatch = Regex.Match(transform, "scale[^\\)]+\\)"); if (scaleMatch != null) { string scale = scaleMatch.ToString().Substring("scale(".Length); scale = scale.Substring(0, scale.Length - 1); string[] args = scale.Split(",".ToCharArray()); scaleX = float.Parse(args[0].Trim()); scaleY = float.Parse(args[1].Trim()); } } string d = path.Attribute("d").Value.Replace(',', ' ').Replace("\r", "").Replace("\n", "").Replace("\t", " "); d = Regex.Replace(d, "\\s+", " "); Match match; while ((match = Regex.Match(d, "\\d\\-")).Success) { d = d.Substring(0, match.Index + 1) + " " + d.Substring(match.Index + 1); } System.Diagnostics.Debug.WriteLine(d); System.Diagnostics.Debug.WriteLine(translateX + ", " + translateY + " - " + scaleX + ", " + scaleY); float cursorX = 0; float cursorY = 0; token = ""; string previousToken = ""; PointFloat lastStartPoint = new PointFloat(); PointFloat lastEndPoint = new PointFloat(); int loopStart = vertexList.Count; while (d.Length > 0) { //System.Diagnostics.Debug.WriteLine(d); readNextToken(ref d, "\\S"); if (Regex.IsMatch(d.Substring(0, 1), "[^0-9\\-\\.]")) { token = d.Substring(0, 1); d = d.Substring(1); } else { token = previousToken; } switch (token) { case "M": cursorX = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); cursorY = float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY + translateY; vertexList.Add(new PointFloat(cursorX, cursorY)); break; case "m": cursorX += float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); cursorY += float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY; vertexList.Add(new PointFloat(cursorX, cursorY)); break; case "L": case "l": { float x = 0, y = 0; if (token == "L") { x = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); y = float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY + translateY; } else { x = cursorX + float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); y = cursorY + float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY; } cursorX = x; cursorY = y; lineTo(cursorX, cursorY); break; } case "H": case "h": { float x = 0; if (token == "H") { x = float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleX + translateX; } else { x = cursorX + float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleX; } cursorX = x; lineTo(cursorX, cursorY); break; } case "V": case "v": { float y = 0; if (token == "V") { y = float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY + translateY; } else { y = cursorY + float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY; } cursorY = y; lineTo(cursorX, cursorY); break; } case "C": case "c": { float x = 0, y = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0; if (token == "C") { x1 = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); y1 = float.Parse(readNextToken(ref d, "\\s")) * scaleY + translateY; readNextToken(ref d, "[^\\s]"); x2 = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); y2 = float.Parse(readNextToken(ref d, "\\s")) * scaleY + translateY; readNextToken(ref d, "[^\\s]"); x = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); y = float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY + translateY; } else { x1 = cursorX + float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); y1 = cursorY + float.Parse(readNextToken(ref d, "\\s")) * scaleY; readNextToken(ref d, "[^\\s]"); x2 = cursorX + float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); y2 = cursorY + float.Parse(readNextToken(ref d, "\\s")) * scaleY; readNextToken(ref d, "[^\\s]"); x = cursorX + float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); y = cursorY + float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY; } cursorX = x; cursorY = y; curveToCubic(x1, y1, x2, y2, x, y); lastStartPoint.x = x1; lastStartPoint.y = y1; lastEndPoint.x = x2; lastEndPoint.y = y2; break; } case "Q": case "q": { float x = 0, y = 0, x1 = 0, y1 = 0; if (token == "Q") { x1 = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); y1 = float.Parse(readNextToken(ref d, "\\s")) * scaleY + translateY; readNextToken(ref d, "[^\\s]"); x = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); y = float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY + translateY; } else { x1 = cursorX + float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); y1 = cursorY + float.Parse(readNextToken(ref d, "\\s")) * scaleY; readNextToken(ref d, "[^\\s]"); x = cursorX + float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); y = cursorY + float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY; } cursorX = x; cursorY = y; lastStartPoint.x = x1; lastStartPoint.y = y1; curveToQuad(x1, y1, x, y); break; } case "S": case "s": { float x = 0, y = 0, x2 = 0, y2 = 0; if (token == "S") { x2 = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); y2 = float.Parse(readNextToken(ref d, "\\s")) * scaleY + translateY; readNextToken(ref d, "[^\\s]"); x = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); y = float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY + translateY; } else { x2 = cursorX + float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); y2 = cursorY + float.Parse(readNextToken(ref d, "\\s")) * scaleY; readNextToken(ref d, "[^\\s]"); x = cursorX + float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); y = cursorY + float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY; } cursorX = x; cursorY = y; curveToCubic(lastEndPoint.x, lastEndPoint.y, x2, y2, x, y); lastEndPoint.x = x2; lastEndPoint.y = y2; break; } case "T": case "t": { float x = 0, y = 0; if (token == "T") { x = float.Parse(readNextToken(ref d, "\\s")) * scaleX + translateX; readNextToken(ref d, "[^\\s]"); y = float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY + translateY; } else { x = cursorX + float.Parse(readNextToken(ref d, "\\s")) * scaleX; readNextToken(ref d, "[^\\s]"); y = cursorY + float.Parse(readNextToken(ref d, "[^0-9\\-\\.]")) * scaleY; } cursorX = x; cursorY = y; curveToCubic(lastStartPoint.x, lastStartPoint.y, lastEndPoint.x, lastEndPoint.y, x, y); break; } case "z": case "Z": lineTo(vertexList[loopStart].x, vertexList[loopStart].y); loopStart = vertexList.Count; break; default: System.Diagnostics.Debug.WriteLine("doodster"); readNextToken(ref d, "[^a-zA-Z]"); break; } previousToken = token; //System.Diagnostics.Debug.WriteLine(d); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.StackTrace + "\n" + token); } }