|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | + |
| 5 | +namespace RadiantMapToWavefrontObj |
| 6 | +{ |
| 7 | + public class Patch |
| 8 | + { |
| 9 | + public Vertex[][] Grid { get; private set; } |
| 10 | + |
| 11 | + private int _x; |
| 12 | + private int _y; |
| 13 | + |
| 14 | + public Vertex[] this[int index] |
| 15 | + { |
| 16 | + get => Grid[index]; |
| 17 | + set => Grid[index] = value; |
| 18 | + } |
| 19 | + |
| 20 | + // Constructor for a patch. |
| 21 | + public Patch(int width, int height) |
| 22 | + { |
| 23 | + Grid = new Vertex[width][]; |
| 24 | + |
| 25 | + for (int i = 0; i < Grid.Length; ++i) |
| 26 | + { |
| 27 | + Grid[i] = new Vertex[height]; |
| 28 | + for (int j = 0; j < Grid[i].Length; ++j) |
| 29 | + Grid[i][j] = null; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Collect all vertices and return them. |
| 34 | + public Vertex[] GetVertices() |
| 35 | + { |
| 36 | + List<Vertex> vertices = new List<Vertex>(); |
| 37 | + |
| 38 | + foreach (Vertex[] row in Grid) |
| 39 | + { |
| 40 | + foreach (Vertex vert in row) |
| 41 | + vertices.Add(vert); |
| 42 | + } |
| 43 | + |
| 44 | + return vertices.ToArray(); |
| 45 | + } |
| 46 | + |
| 47 | + // Adds a vertex to the grid in the next available slot. |
| 48 | + public void Add(Vertex vertex) |
| 49 | + { |
| 50 | + Grid[_y][_x] = vertex; |
| 51 | + |
| 52 | + if (_x < Grid[0].Length-1) |
| 53 | + _x++; |
| 54 | + else |
| 55 | + { |
| 56 | + _x = 0; |
| 57 | + if (_y < Grid.Length - 1) |
| 58 | + _y++; |
| 59 | + else |
| 60 | + _y = 0; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Creates a radiant patch from a piece of code. |
| 65 | + public static Patch CreateFromCode(string[] code) |
| 66 | + { |
| 67 | + string sizePattern = @"(\s+)?\(\s?(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)(\s(\d+)\s(\d+))?\s?\)"; //width,height [2,3] |
| 68 | + Regex sizeRegex = new Regex(sizePattern, RegexOptions.IgnoreCase); |
| 69 | + |
| 70 | + Patch patch = null; |
| 71 | + |
| 72 | + int line = 0; |
| 73 | + |
| 74 | + while (line < code.Length) |
| 75 | + { |
| 76 | + Match m = sizeRegex.Match(code[line]); |
| 77 | + ++line; |
| 78 | + if (m.Success) |
| 79 | + { |
| 80 | + patch = new Patch(int.Parse(m.Groups[2].ToString()), int.Parse(m.Groups[3].ToString())); |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + string num = @"-?\d+(\.\d+)?"; |
| 86 | + string vertex = @"(\(\s?(" + num + @")\s(" + num + @")\s(" + num + @")\s(" //x,y,z [2,4,6] |
| 87 | + + num + @")\s(" + num + @")\s(" //patchDef2 |
| 88 | + + "(" + num + @")\s(" + num + @")\s(" + num + @")\s(" + num + @")\s(" + num + @"))?\s?\))"; //patchDef3 |
| 89 | + |
| 90 | + string pattern = vertex; |
| 91 | + |
| 92 | + Regex regex = new Regex(pattern, RegexOptions.IgnoreCase); |
| 93 | + |
| 94 | + while (line < code.Length) |
| 95 | + { |
| 96 | + MatchCollection m = regex.Matches(code[line]); |
| 97 | + ++line; |
| 98 | + if (m.Count > 0) |
| 99 | + { |
| 100 | + for (int i = 0; i < m.Count; ++i) |
| 101 | + { |
| 102 | + Vertex v = new Vertex(-Double.Parse(m[i].Groups[2].ToString(), System.Globalization.CultureInfo.InvariantCulture), |
| 103 | + -Double.Parse(m[i].Groups[4].ToString(), System.Globalization.CultureInfo.InvariantCulture), |
| 104 | + -Double.Parse(m[i].Groups[6].ToString(), System.Globalization.CultureInfo.InvariantCulture)); |
| 105 | + |
| 106 | + if (patch != null) |
| 107 | + patch.Add(v); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return patch; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments