/// <summary>
        /// Removes up to a number of newlines (and whitespace between them) that are found after the specified
        /// <paramref name="tagInstance"/>.
        /// </summary>
        /// <remarks>
        /// If <paramref name="numberOfNewlines"/> is higher than the actual number of newlines after the
        /// <paramref name="tagInstance"/>, only the actual number of newlines will be removed.
        /// </remarks>
        /// <param name="tagInstance">The tag instance to remove whitespace from after</param>
        /// <param name="numberOfNewlines">The number of newlines to remove</param>
        public void RemoveWhitespaceAfterTag(ITagInstance tagInstance, int numberOfNewlines)
        {
            if (numberOfNewlines <= 0)
            {
                throw new ArgumentOutOfRangeException("numberOfNewlines", "numberOfNewlines must be greater than 0.");
            }

            if (tagInstance.CharRange.StartAt >= _RenderString.Length)
            {
                return;
            }

            int   startAt = tagInstance.CharRange.StartAt + tagInstance.CharRange.Length;
            Match match   = _AfterTagWhitespaceRegex.Match(GetRenderString(), startAt);

            if (match.Success == false)
            {
                return;
            }

            CaptureCollection captures = match.Groups["nl"].Captures;

            if (captures.Count <= numberOfNewlines)
            {
                RenderNonTagRange(match.Index, match.Length, String.Empty, false);
            }
            else
            {
                int length = captures.Cast <Capture>().Take(numberOfNewlines).Sum(c => c.Length);
                RenderNonTagRange(startAt, length, String.Empty, false);
            }
        }
 /// <summary>
 /// This should be instantiated only in the Siderite.StreamRegex namespace
 /// </summary>
 /// <param name="captures">Collection of regex captures</param>
 /// <param name="streamPosition"></param>
 internal StreamCaptureCollection(CaptureCollection captures, int streamPosition)
 {
     _list = new List <StreamCapture>(captures
                                      .Cast <Capture>()
                                      .Select(capture => new StreamCapture(capture, streamPosition)));
 }
예제 #3
0
        public static void Load(string filepath, TriangleMesh triMesh, float scale)
        {
            List <Vector3> vertices = new List <Vector3>();

            string[] strings = System.IO.File.ReadAllLines(filepath);

            // Lines starting with v are a vertex:
            // "v 10.2426 4.5e-013 -31.7638"
            Regex vertexRegex = new Regex(@"^v\s+(?<x>\S+)\s+(?<y>\S+)\s+(?<z>\S+)", RegexOptions.IgnoreCase);

            // Lines starting with f are a face.  The indices are <vertex>/<texture>/<normal>, where texture and normal are optional.
            // "f 1/1/1 2/2/1 3/3/1 4/4/1 5/5/1"
            Regex faceRegex = new Regex(@"^f(?<face_data>\s+(?<vertex>\d+)/?(?<texture_coordinate>\d+)?/?(?<vertex_normal>\d+)?)+", RegexOptions.IgnoreCase);

            foreach (string s in strings)
            {
                if (vertexRegex.IsMatch(s))
                {
                    Match m = vertexRegex.Match(s);
                    float x = float.Parse(m.Groups["x"].Value);
                    float y = float.Parse(m.Groups["y"].Value);
                    float z = float.Parse(m.Groups["z"].Value);
                    // Rotate 90 degrees about the X axis - for some reason .obj files saved from sketchup have this issue...
                    Vector3 v = new Vector3(x, -z, y);
                    vertices.Add(v * scale);
                }
                else if (faceRegex.IsMatch(s))
                {
                    Match m = faceRegex.Match(s);

                    //Console.WriteLine(m.Groups["face_data"].Captures.Count);
                    //Console.WriteLine(m.Groups["vertex"].Captures.Count);
                    //Console.WriteLine(m.Groups["texture_coordinate"].Captures.Count);
                    //Console.WriteLine(m.Groups["vertex_normal"].Captures.Count);

                    //Face face = new Face();
                    Polygon polygon = new Polygon();

                    CaptureCollection vert_captures     = m.Groups["vertex"].Captures;
                    CaptureCollection texcoord_captures = m.Groups["texture_coordinate"].Captures;
                    CaptureCollection norm_captures     = m.Groups["vertex_normal"].Captures;

                    var vertexIndices = vert_captures.Cast <Capture>().Select(capture => int.Parse(capture.Value) - 1);

                    foreach (var vertexIndex in vertexIndices)
                    {
                        if (vertexIndex < 0 || vertexIndex > vertices.Count)
                        {
                            Console.WriteLine("Bad vertex index {0}, only {1} vertices loaded", vertexIndex, vertices.Count);
                        }
                        else
                        {
                            polygon.Add(vertices[vertexIndex]);
                        }
                    }
                    //for (int i = 0; i < vert_captures.Count; i++)
                    //{
                    //    int vert_index = int.Parse(vert_captures[i].Value) - 1;
                    //
                    //}
                    if (texcoord_captures.Count == vert_captures.Count)
                    {
                        // TODO: Add texture coordinates to the face
                    }
                    if (norm_captures.Count == vert_captures.Count)
                    {
                        // TODO: Add vertex normals to the face
                    }

                    if (polygon.Vertices.Count() < 3)
                    {
                        Console.WriteLine("Bad face defined, less than 3 vertices");
                    }
                    else
                    {
                        foreach (var triangle in polygon.ToTriangles())
                        {
                            triMesh.AddTriangle(triangle);
                        }
                    }
                }
            }
        }