Ejemplo n.º 1
0
        private void ParserBinary(PlyObjectHeader header, BinaryReader ebr)
        {
            Point3D[]         vertices         = new Point3D[header.Elements["vertex"].Count];;
            PointerToVertex[] pointersToVertex = new PointerToVertex[header.Elements["face"].Count];
            //Initialize bound box calculation
            Point3D pmin, pmax;

            pmin          = pmax = Point3D.Zero;
            this.boundBox = new BoundBox();
            for (int i = 0; i < header.Elements.Count; i++)
            {
                ElementDescription element = header.Elements[i];
                for (int e = 0; e < element.Count; e++)
                {
                    for (int p = 0; p < element.Properties.Count; p++)
                    {
                        PropertyDescription property = element.Properties[p];

                        if (property.IsList)
                        {
                            int listCount = 0;
                            if (property.CountType == PropertyType.uchar || property.CountType == PropertyType.uint8)
                            {
                                listCount = ebr.ReadByte();
                            }
                            else
                            {
                                throw new ArgumentException("The type expected for List Count is 'uchar'. Type in file: '" + property.CountType + "'");
                            }

                            if (property.TypeOfList == PropertyType.@int || property.TypeOfList == PropertyType.int32)
                            {
                                if (element.Name == "face")
                                {
                                    for (int j = 0; j < listCount; j += 3)
                                    {
                                        pointersToVertex[e].Vertex1 = ebr.ReadInt32();
                                        pointersToVertex[e].Vertex2 = ebr.ReadInt32();
                                        pointersToVertex[e].Vertex3 = ebr.ReadInt32();

                                        this.triangles[e] = new Triangle(vertices[pointersToVertex[e].Vertex1],
                                                                         vertices[pointersToVertex[e].Vertex2],
                                                                         vertices[pointersToVertex[e].Vertex3]);

                                        int percent = (int)(((float)e / this.triangles.Length) * 100.0f);
                                        if ((percent % 20) == 0)
                                        {
                                            this.OnElementLoaded(percent, ElementMesh.Triangle);
                                        }
                                    }
                                }
                                else if (element.Name == "vertex")
                                {
                                    #region Ignore other properties for vertex
                                    for (int j = 0; j < listCount; j += 3)
                                    {
                                        ebr.ReadInt32();
                                        ebr.ReadInt32();
                                        ebr.ReadInt32();
                                    }
                                    #endregion
                                }
                            }
                            else
                            {
                                throw new ArgumentException("The type expected for List elements is 'int'. Type in file: '" + property.TypeOfList + "'");
                            }
                        }
                        else
                        {
                            if (element.Name == "face")
                            {
                                #region Ignore other properties for face
                                switch (property.Type)
                                {
                                case PropertyType.@char:
                                case PropertyType.int8:
                                    ebr.ReadSByte();
                                    break;

                                case PropertyType.uchar:
                                case PropertyType.uint8:
                                    ebr.ReadByte();
                                    break;

                                case PropertyType.@short:
                                case PropertyType.int16:
                                    ebr.ReadInt16();
                                    break;

                                case PropertyType.@ushort:
                                case PropertyType.uint16:
                                    ebr.ReadUInt16();
                                    break;

                                case PropertyType.@int:
                                case PropertyType.int32:
                                    ebr.ReadInt32();
                                    break;

                                case PropertyType.@uint:
                                case PropertyType.uint32:
                                    ebr.ReadUInt32();
                                    break;

                                case PropertyType.@float:
                                case PropertyType.float32:
                                    ebr.ReadSingle();
                                    break;

                                case PropertyType.@double:
                                case PropertyType.float64:
                                    ebr.ReadDouble();
                                    break;

                                default:
                                    break;
                                }
                                #endregion
                            }
                            else if (element.Name == "vertex")
                            {
                                switch (property.Type)
                                {
                                case PropertyType.@char:
                                case PropertyType.int8:
                                    ebr.ReadSByte();
                                    break;

                                case PropertyType.uchar:
                                case PropertyType.uint8:
                                    ebr.ReadByte();
                                    break;

                                case PropertyType.@short:
                                case PropertyType.int16:
                                    ebr.ReadInt16();
                                    break;

                                case PropertyType.@ushort:
                                case PropertyType.uint16:
                                    ebr.ReadUInt16();
                                    break;

                                case PropertyType.@int:
                                case PropertyType.int32:
                                    ebr.ReadInt32();
                                    break;

                                case PropertyType.@uint:
                                case PropertyType.uint32:
                                    ebr.ReadUInt32();
                                    break;

                                case PropertyType.@float:
                                case PropertyType.float32:
                                    vertices[e].X = ebr.ReadSingle();
                                    vertices[e].Y = ebr.ReadSingle();
                                    vertices[e].Z = ebr.ReadSingle();
                                    p            += 2;
                                    //Adjusting BoundBox...
                                    this.boundBox.Include(vertices[e]);
                                    //Reporting progress
                                    int percent = (int)(((float)e / vertices.Length) * 100.0f);
                                    if ((percent % 20) == 0)
                                    {
                                        this.OnElementLoaded(percent, ElementMesh.Vertex);
                                    }
                                    break;

                                case PropertyType.@double:
                                case PropertyType.float64:
                                    ebr.ReadDouble();
                                    break;

                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            this.ProcessNormalsPerVertex(pointersToVertex, vertices.Length);
            vertices         = null;
            pointersToVertex = null;
            GC.Collect();
        }