private static bool ParseHeaderElement(PlyTokenStream tokenStream, IList <ElementConfiguration> elementsConfiguration)
        {
            SkipComments(tokenStream);

            if (tokenStream.CurToken == "end_header")
            {
                tokenStream.ReadToken();
                tokenStream.NextLineExpectNoMoreTokens();

                return(false);
            }

            tokenStream.NextTokenExpectValue("element", "'element' keyword was expected.");

            var elementName = tokenStream.ReadTokenExpected();

            if (elementName == null || (elementName != "vertex" && elementName != "face" && elementName != "edge"))
            {
                throw new PlyParsingExcpetion(string.Format("Unsupported element {0}", elementName));
            }

            var count = int.Parse(tokenStream.ReadTokenExpected());

            tokenStream.NextLineExpectNoMoreTokens();

            SkipComments(tokenStream);

            var properties = new List <Tuple <string, string> >();

            while (tokenStream.CurToken == "property")
            {
                tokenStream.NextToken();

                var    type = tokenStream.ReadTokenExpected();
                string fullType;

                if (type == "list")
                {
                    var listIndexType  = tokenStream.ReadTokenExpected();
                    var listValuesType = tokenStream.ReadTokenExpected();

                    fullType = string.Format("{0}:{1}:{2}", type, listIndexType, listValuesType);
                }
                else
                {
                    fullType = type;
                }

                var name = tokenStream.ReadTokenExpected();

                properties.Add(Tuple.Create(fullType, name));

                tokenStream.NextLineExpectNoMoreTokens();
                SkipComments(tokenStream);
            }

            elementsConfiguration.Add(new ElementConfiguration(elementName, properties, count));

            return(true);
        }
 private static void SkipComments(PlyTokenStream tokenStream)
 {
     while (tokenStream.CurToken == "comment")
     {
         tokenStream.NextLine();
     }
 }
        private static IEnumerable <ElementConfiguration> ParseHeader(PlyTokenStream tokenStream)
        {
            tokenStream.NextTokenExpectValue("ply", "'ply' magic string was expected.");
            tokenStream.NextLineExpectNoMoreTokens();

            tokenStream.NextTokenExpectValue("format", "'format' keyword was expected.");
            tokenStream.NextTokenExpectValue("ascii", "Only ascii format is supported.");
            tokenStream.NextTokenExpectValue("1.0", "Only PLY 1.0 is supported");
            tokenStream.NextLineExpectNoMoreTokens();

            var elementsConfiguration = new List <ElementConfiguration>();

            while (ParseHeaderElement(tokenStream, elementsConfiguration))
            {
            }

            return(elementsConfiguration);
        }
        public static void Parse <TVertex, TFace, TEdge>(
            string plyData,
            out IReadOnlyList <TVertex> vertices,
            out IReadOnlyList <TFace> faces,
            out IReadOnlyList <TEdge> edges)

            where TVertex : PlyVertex, new()
            where TFace : PlyFace, new()
            where TEdge : PlyEdge, new()
        {
            var verticesList = new List <TVertex>();
            var facesList    = new List <TFace>();
            var edgesList    = new List <TEdge>();

            var tokenStream = new PlyTokenStream(plyData);

            var elementsConfiguration = ParseHeader(tokenStream);

            foreach (var elementConfiguration in elementsConfiguration)
            {
                var parametersParser = new PlyParametersParser(elementConfiguration.Properties);
                for (int i = 0; i < elementConfiguration.Count; i++)
                {
                    parametersParser.SetParameters(tokenStream.CurLine);

                    var element = CreateElement <TVertex, TFace, TEdge>(
                        verticesList,
                        facesList,
                        edgesList,
                        elementConfiguration.Name);

                    element.Parse(parametersParser);

                    tokenStream.NextLine();
                }
            }

            vertices = verticesList.AsReadOnlyList();
            faces    = facesList.AsReadOnlyList();
            edges    = edgesList.AsReadOnlyList();
        }