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
        static void WritePlyExample(string filename)
        {
            Geometry cube = MakeCubeGeometry();

            {
                PlyFile cubeFile = new PlyFile();


                byte[] verticesbytes = new byte[cube.vertices.Count * sizeof(float)];
                System.Buffer.BlockCopy(cube.vertices.ToArray(), 0, verticesbytes, 0, verticesbytes.Length);
                cubeFile.AddPropertiesToElement("vertex", new List <string> {
                    "x", "y", "z"
                },
                                                TinyplyCSharp.Type.FLOAT32, cube.vertices.Count / 3, verticesbytes, TinyplyCSharp.Type.INVALID, 0);

                byte[] normalbytes = new byte[cube.normals.Count * sizeof(float)];
                System.Buffer.BlockCopy(cube.normals.ToArray(), 0, normalbytes, 0, normalbytes.Length);
                cubeFile.AddPropertiesToElement("vertex", new List <string> {
                    "nx", "ny", "nz"
                },
                                                TinyplyCSharp.Type.FLOAT32, cube.normals.Count / 3, normalbytes, TinyplyCSharp.Type.INVALID, 0);

                byte[] trianglebytes = new byte[cube.triangles.Count * sizeof(int)];
                System.Buffer.BlockCopy(cube.triangles.ToArray(), 0, trianglebytes, 0, trianglebytes.Length);
                cubeFile.AddPropertiesToElement("face", new List <string> {
                    "vertex_indices"
                },
                                                TinyplyCSharp.Type.UINT32, cube.triangles.Count / 3, trianglebytes, TinyplyCSharp.Type.UINT8, 3);

                cubeFile.GetComments().Add("generated by tinyply 2.3");

                using (FileStream outStream = File.Open(filename + "-ascii.ply", FileMode.Create))
                {
                    cubeFile.Write(outStream, false);
                }

                using (FileStream outStream = File.Open(filename + "-binary.ply", FileMode.Create))
                {
                    cubeFile.Write(outStream, true);
                }
            }
        }