Beispiel #1
0
        public Mesh CreateMeshFromBinary(MemoryStream stream)
        {
            var br = new BinaryReader(stream);

            // seeks past the header
            br.BaseStream.Seek(80, SeekOrigin.Begin);

            var numberOfFacets = br.ReadInt32();

            var mesh = new Mesh {Facets = new Collection<Facet>()};

            for (long i = 0; i < numberOfFacets; i++)
            {
                mesh.Facets.Add(new Facet
                                    {
                                        Normal =
                                            new Point {X = br.ReadSingle(), Y = br.ReadSingle(), Z = br.ReadSingle()},
                                        Vertices = new Collection<Point>
                                                       {
                                                           new Point
                                                               {
                                                                   X = br.ReadSingle(),
                                                                   Y = br.ReadSingle(),
                                                                   Z = br.ReadSingle()
                                                               },
                                                           new Point
                                                               {
                                                                   X = br.ReadSingle(),
                                                                   Y = br.ReadSingle(),
                                                                   Z = br.ReadSingle()
                                                               },
                                                           new Point
                                                               {
                                                                   X = br.ReadSingle(),
                                                                   Y = br.ReadSingle(),
                                                                   Z = br.ReadSingle()
                                                               }
                                                       }
                                    });

                // unused attribute byte count
                br.ReadInt16();
            }

            return mesh;
        }
Beispiel #2
0
        public Mesh CreateMeshFromAscii(string data)
        {
            var lines = data.ToUpper().Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            var mesh = new Mesh { Facets = new Collection<Facet>() };

            Facet facet = null;
            foreach (var line in lines)
            {
                var tokens = line.Split();

                if (tokens[0] == ("FACET"))
                {
                    facet = new Facet
                                {
                                    Normal = new Point
                                                 {
                                                     X = float.Parse(tokens[2]),
                                                     Y = float.Parse(tokens[3]),
                                                     Z = float.Parse(tokens[4])
                                                 }
                                };
                }

                if(tokens[0] == "OUTER" && tokens[1] == "LOOP")
                {
                    if (facet != null)
                    {
                        facet.Vertices = new Collection<Point>();
                    }
                    else
                    {
                        throw new ApplicationException();
                    }
                }

                if (tokens[0] == "VERTEX")
                {
                    if (facet != null && facet.Vertices != null)
                    {
                        facet.Vertices.Add(new Point
                                               {
                                                   X = float.Parse(tokens[1]),
                                                   Y = float.Parse(tokens[2]),
                                                   Z = float.Parse(tokens[3])
                                               });
                    }
                    else
                    {
                        throw new ApplicationException();
                    }
                }

                if (tokens[0] == "ENDFACET")
                {
                    mesh.Facets.Add(facet);
                }
            }

            return mesh;
        }