Exemple #1
0
        protected override T ReadPropertyInternal <T>(PlyProperty expected)
        {
            var token       = _textReader.ReadToken();
            var parsedValue = PlyTypeConverter.ParseStringToNativeType <T>(token, expected.ValueType);

            return(parsedValue);
        }
Exemple #2
0
        public static UnexpectedDataTypeException GetUnexpectedDataTypeException(PlyProperty expected, Type actual)
        {
            var expectedType     = PlyTypeConverter.ToStringRepresentation(expected.ValueType);
            var exceptionMessage = $@"Unexpected data type. Was expecting {expectedType} 
                for property {expected.Name}, but got {actual.Name} instead.";

            return(new UnexpectedDataTypeException(exceptionMessage, expected));
        }
Exemple #3
0
        public T ReadProperty <T>()
        {
            PlyProperty expected = _iterator.CurrentProperty;

            if (expected is PlyArrayProperty)
            {
                throw ExceptionFactory.GetTryingToReadValueWhenArrayExpectedException(expected);
            }

            _iterator.MoveNext();
            return(ReadPropertyInternal <T>(expected));
        }
Exemple #4
0
        public IEnumerable <T> ReadArray <T>()
        {
            PlyProperty expected = _iterator.CurrentProperty;

            if (!(expected is PlyArrayProperty))
            {
                throw ExceptionFactory.GetTryingToReadArrayWhenValueExpectedException(expected);
            }

            _iterator.MoveNext();
            return(ReadArrayInternal <T>((PlyArrayProperty)expected));
        }
Exemple #5
0
        public void SkipProperty(int count = 1)
        {
            for (var i = 0; i < count; ++i)
            {
                PlyProperty expected = _iterator.CurrentProperty;
                if (expected is PlyArrayProperty)
                {
                    SkipPropertyInternal((PlyArrayProperty)expected);
                }
                else
                {
                    SkipPropertyInternal(expected);
                }

                _iterator.MoveNext();
            }
        }
Exemple #6
0
        private void AddProperty(PlyFile plyFile, string[] tokens)
        {
            PlyProperty property;

            if (tokens[1] == PlyKeywords.List)
            {
                var name    = tokens[4];
                var element = plyFile.Elements.Last();
                property = new PlyProperty(name, element.Properties.Count, TypeMapper.GetPropertyType(tokens[3]), TypeMapper.GetPropertyType(tokens[2]));
                element.Properties.Add(tokens[4], property);
            }
            else
            {
                var element = plyFile.Elements.Last();
                property = new PlyProperty(tokens[2], element.Properties.Count, TypeMapper.GetPropertyType(tokens[1]));
                element.Properties.Add(tokens[2], property);
            }
        }
Exemple #7
0
 protected override void SkipPropertyInternal(PlyProperty expected)
 {
     ReadPropertyInternal <object>(expected);
 }
Exemple #8
0
 public static InvalidOperationException GetCannotDisposeBecauseIterationIsNotDoneException(PlyProperty expected, PlyWriter writer)
 {
     return(new InvalidOperationException($@"Not all defined elements have been written.
         The {writer.GetType().Name} is now in a invalid state. Last property expected was {expected.Name}."));
 }
Exemple #9
0
 protected abstract void SkipPropertyInternal(PlyProperty expected);
Exemple #10
0
        protected override T ReadPropertyInternal <T>(PlyProperty expected)
        {
            var value = ReadBytesFor(expected.ValueType);

            return(PlyTypeConverter.ParseBytesToNative <T>(value, expected.ValueType));
        }
Exemple #11
0
        /// <summary>
        /// Reads and validates the header lines of a ply file.
        /// </summary>
        /// <param name="headerLines">The lines to read.</param>
        /// <returns></returns>
        private PlyHeader ReadHeader(string[] headerLines)
        {
            if (headerLines.Length > 2 && (PlyHeaderItems)Enum.Parse(typeof(PlyHeaderItems), headerLines[0]) == PlyHeaderItems.ply &&
                (PlyHeaderItems)Enum.Parse(typeof(PlyHeaderItems), headerLines[headerLines.Length - 1]) == PlyHeaderItems.end_header)
            {
                var formatSpecLineParts = headerLines[1].Split(' ');
                var formatStr           = formatSpecLineParts[0];
                var formatTypeStr       = formatSpecLineParts[1];
                var fileVersion         = Version.Parse(formatSpecLineParts[2]);

                if ((PlyHeaderItems)Enum.Parse(typeof(PlyHeaderItems), formatStr) == PlyHeaderItems.format &&
                    Enum.TryParse(formatTypeStr, out PlyFormatTypes formatType) && fileVersion <= SUPPORTEDVERSION)
                {
                    var comments = new List <string>();
                    var objInfos = new List <Tuple <string, string> >();
                    var elements = new List <PlyElement>();

                    for (int i = 2; i < headerLines.Length - 1; i++)
                    {
                        var lineParts = headerLines[i].Split(' ');
                        if (Enum.TryParse(lineParts[0], out PlyHeaderItems headerItemType))
                        {
                            switch (headerItemType)
                            {
                            case PlyHeaderItems.element:
                            {
                                if (lineParts.Length == 3)
                                {
                                    var elementName  = lineParts[1];
                                    var elementCount = int.Parse(lineParts[2]);
                                    var element      = new PlyElement(elementName, elementCount, new List <PlyProperty[]> {
                                            new PlyProperty[] { }
                                        });
                                    elements.Add(element);
                                }
                                break;
                            }

                            case PlyHeaderItems.property:
                            {
                                if (lineParts.Length >= 3 && elements.Count > 0)
                                {
                                    if (lineParts[1] != "list" && lineParts.Length == 3)
                                    {
                                        if (Enum.TryParse($"_{lineParts[1]}", out PlyDataTypes propertyType))
                                        {
                                            var propertyName = lineParts[2];

                                            var property = new PlyProperty(propertyName, propertyType, null, false, PlyDataTypes._char, null);

                                            var newPropertyList = new List <PlyProperty>();
                                            for (int j = 0; j < elements.Last().Instances[0].Length; j++)
                                            {
                                                newPropertyList.Add(elements.Last().Instances[0][j]);
                                            }
                                            newPropertyList.Add(property);
                                            elements.Last().Instances[0] = newPropertyList.ToArray();
                                        }
                                        else
                                        {
                                            throw new InvalidDataException($"Invalid data type, {lineParts[1]}.");
                                        }
                                    }
                                    else if (lineParts[1] == "list" && lineParts.Length == 5)
                                    {
                                        //array property
                                        if (Enum.TryParse($"_{lineParts[2]}", out PlyDataTypes propertyType) && Enum.TryParse($"_{lineParts[3]}", out PlyDataTypes listContentType))
                                        {
                                            var propertyName = lineParts[4];

                                            var property = new PlyProperty(propertyName, propertyType, null, true, listContentType, null);

                                            var newPropertyList = new List <PlyProperty>();
                                            for (int j = 0; j < elements.Last().Instances[0].Length; j++)
                                            {
                                                newPropertyList.Add(elements.Last().Instances[0][j]);
                                            }
                                            newPropertyList.Add(property);
                                            elements.Last().Instances[0] = newPropertyList.ToArray();
                                        }
                                        else
                                        {
                                            throw new InvalidDataException($"Invalid data type, {lineParts[1]}.");
                                        }
                                    }
                                    else
                                    {
                                        throw new InvalidDataException("Invalid property definition.");
                                    }
                                }
                                break;
                            }

                            case PlyHeaderItems.obj_info:
                            {
                                if (lineParts.Length == 3)
                                {
                                    objInfos.Add(new Tuple <string, string>(lineParts[1], lineParts[2]));
                                }
                                else
                                {
                                    objInfos.Add(new Tuple <string, string>($"htk_info_{objInfos.Count}", headerLines[i].Substring(lineParts[0].Length + 1)));
                                }
                                break;
                            }

                            case PlyHeaderItems.comment:
                            {
                                comments.Add(headerLines[i].Substring(lineParts[0].Length + 1));
                                break;
                            }

                            default:
                            {
                                throw new InvalidDataException($"Unknown header item, {lineParts[0]}.");
                            }
                            }
                        }
                        else
                        {
                            throw new InvalidDataException($"Unknown header item, {lineParts[0]}.");
                        }
                    }

                    var plyHeader = new PlyHeader(formatType, fileVersion, elements.ToArray(), objInfos.ToArray(), comments.ToArray());
                    return(plyHeader);
                }
                else
                {
                    throw new InvalidDataException("Invalid format specification.");
                }
            }
Exemple #12
0
 protected abstract T ReadPropertyInternal <T>(PlyProperty expected);
Exemple #13
0
 public static UnexpectedDataTypeException GetTryingToReadValueWhenArrayExpectedException(PlyProperty expected)
 {
     return(new UnexpectedDataTypeException("Trying to read a value when array is expected.", expected));
 }
Exemple #14
0
 public static UnexpectedDataTypeException GetTryingToReadArrayWhenValueExpectedException(PlyProperty expected)
 {
     return(new UnexpectedDataTypeException("Trying to read an array when value is expected.", expected));
 }
Exemple #15
0
 protected override void SkipPropertyInternal(PlyProperty expected)
 {
     _textReader.ReadToken();
 }
Exemple #16
0
 public static UnexpectedDataTypeException GetGotValueWhenArrayExpectedException(PlyProperty expected)
 {
     return(new UnexpectedDataTypeException(
                $"Got an value but was expecting an array for property {expected.Name}."));
 }
Exemple #17
0
 public static UnexpectedDataTypeException GetGotArrayWhenValueExpectedException(PlyProperty expected)
 {
     return(new UnexpectedDataTypeException(
                $"Got an array but was expecting single value for property {expected.Name}."));
 }