Convert between ASCII and Binary PLY File Formats

Files in the PLY file format come in two variants: ASCII and binary. Some programs have problems dealing with a binary PLY file and can only deal with a ASCII PLY file. There are many programs available to convert between ASCII and binary PLY files. I had some problems with the converted files when I used programs like ply2ascii to do this conversion.

Instead, I discovered that MeshLab is not only a cool mesh viewer program, but can also convert between many mesh formats. It supports conversion between PLY too:

  1. Open the original (say binary) PLY file by using File β†’ Import Mesh.
  2. Export this file to the (say ASCII) PLY file format by using File β†’ Export Mesh As. Choose PLY file format in the Save dialog.
  3. MeshLab shows a dialog specific to PLY. Here depending on which (ASCII or binary) PLY format you want, choose the Binary encoding radio button. Your PLY file is converted! πŸ™‚

Tried with: MeshLab 1.3.0

Output 3D Mesh to PLY File

The PLY file format is one of the simplest ways to read and write a 3D mesh. There are a few libraries which can be used to read or write PLY files from your code.

However, if you are dealing with a simple triangulated 3D mesh, there is no need to use a library. Writing such a mesh out to a PLY file from your code is very simple. Here is sample code in C++:

PLY File Format

The Polygon File Format (PLY) is one of the simplest formats to read or write a 3D mesh. It has both ASCII and binary formats, but we look at only the human-readable ASCII format here.

Consider a tetrahedron composed of these 4 points:

1.2 2.9 15.7
132.4 239.4 139.9
152.9 62.9 20.4
81.9 217.7 65.9

The simplest PLY file representing this tetrahedron would be:

format ascii 1.0
element vertex 4
property float x
property float y
property float z
element face 4
property list uchar int vertex_index
1.2 2.9 15.7
132.4 239.4 139.9
152.9 62.9 20.4
81.9 217.7 65.9
3 0 1 2
3 0 1 3
3 0 2 3
3 1 2 3

It is easy to deduce the file format from the above representation:

  • The element vertex VERTEXNUM is used to indicate the number of points.
  • The number type used for the point coordinates is indicated using the property float x lines.
  • The element face FACENUM is used to indicate the number of polygon faces in the mesh.
  • The number type used for the number of points in each face and the vertex indices of those points is indicated using property list uchar int vertex_index. This means that the number of points in each face is of type uchar and the vertex indices themselves are of type int.
  • After the header, are a list of points, much like in the ASC file format.
  • After the points, are a list of the polygon faces. A line like 3 0 1 2 means that this face has 3 points (hence a triangle) and is composed of the vertices 0, 1 and 2. Note that PLY deals only with polygons (like triangles) and cannot deal with polyhedrons (like tetrahedrons).

PLY files can be opened and rendered in applications like MeshLab. PLY files can have many other features, please refer to the Wikipedia page for those details.