Beispiel #1
0
        static public void ReadPlyFile(string filePath, bool preloadIntoMemory = true)
        {
            {
                byte[]  contents = File.ReadAllBytes(filePath);
                var     ms       = new MemoryStream(contents);
                PlyFile file     = new PlyFile();
                file.ParseHeader(ms);
                Console.WriteLine("[ply_header] Type: " + (file.IsBinaryFile() ? "binary" : "ascii"));
                foreach (var comment in file.GetComments())
                {
                    Console.WriteLine("\t[ply_header] Comment: " + comment);
                }

                foreach (var info in file.GetInfo())
                {
                    Console.WriteLine("\t[ply_header] Info: " + info);
                }

                foreach (var element in file.GetElements())
                {
                    Console.WriteLine("\t[ply_header] element: " + element.Name + "(" + element.Size + ")");
                    foreach (var p in element.Properties)
                    {
                        Console.Write("\t[ply_header] \tproperty: " + p.Name + " (type=" + PlyHelper.PropertyTable[p.PropertyType].Str + ")");
                        if (p.IsList)
                        {
                            Console.Write(" (list_type=" + PlyHelper.PropertyTable[p.ListType].Str + ")");
                        }
                        Console.WriteLine();
                    }
                }

                PlyData vertices = file.RequestPropertiesFromElement("vertex", new List <string> {
                    "x", "y", "z"
                });
                PlyData normals = file.RequestPropertiesFromElement("vertex", new List <string> {
                    "nx", "ny", "nz"
                });
                PlyData faces = file.RequestPropertiesFromElement("face", new List <string> {
                    "vertex_indices"
                });
                file.Read(ms);

                if (vertices != null)
                {
                    Console.WriteLine("\tRead " + vertices.Count + " total vertices");
                }
                if (normals != null)
                {
                    Console.WriteLine("\tRead " + normals.Count + " total vertex normals");
                }
                if (faces != null)
                {
                    Console.WriteLine("\tRead " + faces.Count + " total faces (triangles)");
                }
            }
        }
Beispiel #2
0
        private bool ParsePlyFile(string filepath)
        {
            var contents = File.ReadAllBytes(filepath);
            var sizemb   = contents.Length * 1e-6;

            PlyFile file         = new PlyFile();
            var     ms           = new MemoryStream(contents);
            bool    headerResult = file.ParseHeader(ms);

            // All ply files are required to have a vertex element
            Dictionary <string, PlyData> vertexElement = new Dictionary <string, PlyData>();

            Console.WriteLine("Testing: " + filepath + " - filetype: " + (file.IsBinaryFile() ? "binary" : "ascii"));
            Assert.IsTrue(file.GetElements().Count > 0);

            string likelyFacePropertyName = null;

            // Extract a fat vertex structure (will likely include more than xyz)
            foreach (var e in file.GetElements())
            {
                if (e.Name == "vertex")
                {
                    Assert.IsTrue(e.Properties.Count > 0);
                    foreach (var p in e.Properties)
                    {
                        try
                        {
                            vertexElement.Add(p.Name, file.RequestPropertiesFromElement(e.Name, new List <string> {
                                p.Name
                            }));
                        }
                        catch
                        {
                        }
                    }
                }

                // Heuristic...
                if (e.Name == "face" || e.Name == "tristrips")
                {
                    for (int i = 0; i < 1; ++i)
                    {
                        likelyFacePropertyName = e.Properties[i].Name;
                    }
                }
            }

            PlyData faces, tripstrip;

            if (!string.IsNullOrEmpty(likelyFacePropertyName))
            {
                try
                {
                    faces = file.RequestPropertiesFromElement("face", new List <string> {
                        likelyFacePropertyName
                    }, 0);
                }
                catch { }

                try
                {
                    tripstrip = file.RequestPropertiesFromElement("tristrips", new List <string> {
                        likelyFacePropertyName
                    }, 0);
                }
                catch { }
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();
            file.Read(ms);
            sw.Stop();
            Console.WriteLine("\tparsing " + sizemb + "mb in " + sw.ElapsedMilliseconds + " ms");

            foreach (var p in vertexElement)
            {
                Assert.IsTrue(p.Value.Count > 0);
                foreach (var e in file.GetElements())
                {
                    foreach (var prop in e.Properties)
                    {
                        if (e.Name == "vertex" && prop.Name == p.Key)
                        {
                            Assert.IsTrue(e.Size == p.Value.Count);
                        }
                    }
                }
            }

            sw.Restart();
            TranscodePlyFile(file, filepath);
            sw.Stop();

            Console.WriteLine("\ttranscoded in " + sw.ElapsedMilliseconds + " ms");


            return(headerResult);
        }