public static TurtleMesh ExtractTMesh(Curve c)
        {
            var      m = new TurtleMesh();
            Polyline pl;

            c.TryGetPolyline(out pl);

            TurtleFace f = new TurtleFace(pl.Count - 1);

            for (int j = 0; j < pl.Count - 1; j++)
            {
                var v = pl[j];
                f.Add(m.VertexCount);
                m.AddVertex(new TurtleVertex((float)v.X, (float)v.Y, (float)v.Z));
            }
            m.AddFace(f);
            return(m);
        }
        public static ITurtleMesh Read(TextReader sr)
        {
            TurtleMesh m = new TurtleMesh();

            var ci = System.Globalization.CultureInfo.InvariantCulture;

            int linecount = 0;

            string line;

            while ((line = sr.ReadLine()) != null)
            {
                linecount++;

                if (line.TrimStart(_separators).StartsWith("#", StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                var tokens = line.Split(_separators, StringSplitOptions.RemoveEmptyEntries);

                if (tokens.Length < 2)
                {
                    continue;
                }

                var token0 = tokens[0].ToLowerInvariant();

                if (token0 == "v")
                {
                    if (tokens.Length < 4 || tokens.Length > 5)
                    {
                        throw new ArgumentOutOfRangeException(string.Format("Line {0} contains a vertex with number of coordinates different than 3 or 4.", linecount.ToString(ci)));
                    }

                    float x;
                    if (!float.TryParse(tokens[1], System.Globalization.NumberStyles.Any, ci, out x))
                    {
                        throw new ArgumentException(string.Format("Line {0} contains an invalid X coordinate.", linecount.ToString(ci)));
                    }

                    float y;
                    if (!float.TryParse(tokens[2], System.Globalization.NumberStyles.Any, ci, out y))
                    {
                        throw new ArgumentException(string.Format("Line {0} contains an invalid Y coordinate.", linecount.ToString(ci)));
                    }

                    float z;
                    if (!float.TryParse(tokens[3], System.Globalization.NumberStyles.Any, ci, out z))
                    {
                        throw new ArgumentException(string.Format("Line {0} contains an invalid Z coordinate.", linecount.ToString(ci)));
                    }

                    TurtleVertex v = new TurtleVertex(x, y, z);
                    m.AddVertex(v);
                }
                else if (token0 == "f")
                {
                    if (tokens.Length < 3)
                    {
                        throw new ArgumentOutOfRangeException(string.Format("Line {0} contains a face with less than three indices.", linecount.ToString(ci)));
                    }

                    var f = new TurtleFace();

                    for (int i = 1; i < tokens.Length; i++)
                    {
                        int loc;
                        if (!int.TryParse(tokens[i], System.Globalization.NumberStyles.Integer, ci, out loc))
                        {
                            throw new ArgumentException(string.Format("Line {0} contains an invalid face number in token {1}.", linecount.ToString(ci), (i - 1).ToString(ci)));
                        }

                        f.Add(loc - 1);
                    }

                    m.AddFace(f);
                }
                //else texture, else normal
            }

            return(m);
        }
Ejemplo n.º 3
0
        public static ITurtleMesh Read(TextReader sr)
        {
            TurtleMesh m = new TurtleMesh();

            var ci = System.Globalization.CultureInfo.InvariantCulture;

            int linecount = 0;

            string line;
            while ((line = sr.ReadLine()) != null)
            {
                linecount++;

                if (line.TrimStart(_separators).StartsWith("#", StringComparison.InvariantCultureIgnoreCase)) continue;

                var tokens = line.Split(_separators, StringSplitOptions.RemoveEmptyEntries);

                if (tokens.Length < 2) continue;

                var token0 = tokens[0].ToLowerInvariant();

                if (token0 == "v")
                {
                    if (tokens.Length < 4 || tokens.Length > 5)
                        throw new ArgumentOutOfRangeException(string.Format("Line {0} contains a vertex with number of coordinates different than 3 or 4.", linecount.ToString(ci)));

                    float x;
                    if(!float.TryParse(tokens[1], System.Globalization.NumberStyles.Any, ci, out x))
                        throw new ArgumentException(string.Format("Line {0} contains an invalid X coordinate.", linecount.ToString(ci)));

                    float y;
                    if (!float.TryParse(tokens[2], System.Globalization.NumberStyles.Any, ci, out y))
                        throw new ArgumentException(string.Format("Line {0} contains an invalid Y coordinate.", linecount.ToString(ci)));

                    float z;
                    if (!float.TryParse(tokens[3], System.Globalization.NumberStyles.Any, ci, out z))
                        throw new ArgumentException(string.Format("Line {0} contains an invalid Z coordinate.", linecount.ToString(ci)));

                    TurtleVertex v = new TurtleVertex(x, y, z);
                    m.AddVertex(v);
                }
                else if (token0 == "f")
                {
                    if (tokens.Length < 3)
                        throw new ArgumentOutOfRangeException(string.Format("Line {0} contains a face with less than three indices.", linecount.ToString(ci)));

                    var f = new TurtleFace();

                    for (int i = 1; i < tokens.Length; i++)
                    {
                        int loc;
                        if (!int.TryParse(tokens[i], System.Globalization.NumberStyles.Integer, ci, out loc))
                            throw new ArgumentException(string.Format("Line {0} contains an invalid face number in token {1}.", linecount.ToString(ci), (i - 1).ToString(ci)));

                        f.Add(loc - 1);
                    }

                    m.AddFace(f);
                }
                //else texture, else normal
            }

            return m;
        }