PLY reading/writing

João Fradinho Oliveira © July, 2005
Department of Computer Science
University College London


The ply format, is a 3D format that was developed by Stanford University. The header of a ply file, consists of some ascii lines
of text, describing properties of the model, and the number of elements in the file. The remainder of the ply is the data itself, which can either be written in ascii, or in binary following the convention of the machine used. (big endian[Macintosh/Powerpc and most unix machines/Sun/Sgi], or little endian [pcs/Intel]). Greg Turk's ply library has been changed to support reading ply files written with different line break conventions (mac \r [1byte], pc \r\n [2bytes], unix \n [1byte]), and reads both binary and ascii files in binary mode.





-------------------- ply.h & plyfile.c ------------------------------

(Greg Turk's library with essentially two functions modified)


* was changed to always open files in binary mode, files written in ascii can also be read with this binary mode.
* allows opening of filenames that are alias files in macintosh, does not affect other OS


* was changed to handle line breaks in UNIX, MACINTOSH, PC, it resets the file pointer
accordingly for the next read.

-------------------- plyextra.cp ------------------------------



int read_PLY_X(char *fname)

--------- sets up temp structures
--------- to call ply_open_for_reading library function (modified to be multi OS capable)
--------- shows how to release temporary list memory allocated by ply lib, whilst
--------- inserting read data in external user data structure

int read_PLY_binary(char *fnx, int verts, int ferts)

--------- alternative ply read function (binary only(not ascii, header is still read fine)), not multiOS, but fast if you already know what you are reading
--------- copes with little endian/big endian, using functions below

int write_PLY_X(char *fname, int PLY_MODE, int OSlinebreaks=0)

--------- writes ply files either in binary or ascii, with the chosen line breaks convention
--------- PLYMODE 1 (ascii) PLYMODE 2 (binary)
--------- OSlinebreaks 0 (mac), OSlinebreaks 1 (unix), OSlinebreaks 2 (pc)

--------- binary reading and swapping for big endian and little endian conversion

unsigned char getbyte(FILE *inf)
unsigned int getuint_NATIVE(FILE *inf)
unsigned int getuint_SWAP(FILE *inf)
int getint_NATIVE(FILE *inf)
int getint_SWAP(FILE *inf)
float getfloat_NATIVE(FILE *inf)
float getfloat_SWAP(FILE *inf)
double getdouble_NATIVE(FILE *inf)
double getdouble_SWAP(FILE *inf)
int putbyte(FILE *outf, unsigned char val)
int putshort_NATIVE(FILE *outf, unsigned short val)
int putuint_NATIVE(FILE *outf, unsigned int val)
int putint_NATIVE(FILE *outf, int val)
int putfloat_NATIVE(FILE *outf, float val)
int putdouble_NATIVE(FILE *outf, double val)

-------------------- ------------------------------

This file, is a zip file with multi OS test files. The zip file has 12 different ply files of the same model (the Stanford bunny reduced to 1443 triangles). The function write_PLY_X in plyextra.cp was used to save 6 versions of the reduced buny on a macintosh, 3 in
binary with macintosh, pc, and unix line breaks, and 3 in ascii with linebreaks in macintosh, pc, and unix.

The 6 models were re-read on the same machine  with read_PLY_X in plyextra.cp, and then read on a PC with the same function,
whilst on a pc, 6 more models were saved like before with the machine's endianess, these 6 models were then transfered back
to a macintosh and read with no problem with read_PLY_X.

[big endian machine]
- ascii -
BAMbunny.ply [Mac]
BAUbunny.ply [Unix]
BAPbunny.ply  [PC]
-binary (although binary, the header is still in ascii!)
BBMbunny.ply [Mac]
BBUbunny.ply [Unix]
BBPbunny.ply  [PC]

[little endian machine]
- ascii -
lambunny.ply [Mac]
laubunny.ply [Unix]
lapbunny.ply  [PC]
-binary (although binary, the header is still in ascii!)
lbmbunny.ply [Mac]
lbubunny.ply [Unix]
lbpbunny.ply  [PC]


get_words allows reading and write_PLY_X writing of  mac/unix/pc OS files, text editors such as BBEdit do not save the linebreaks
properly to target OSs with binary files. (thanks to Liechy Ronald/Metrowerks for the tip)


The Stanford University 3D Scanning Repository (home to the famous Stanford bunny, the 28 million triangle model statue of Lucy, and to everything ply)

The University of North Carolina at Chapel Hill's 13 million power plant ply model, and walkthrough project

Large Geometric Models Archive, Georgia Institute of Technology (home to the dragon, and Happy Buddha Statue and others)


This model has some interesting properties, as numerically it goes towards flat locally.

icosphere.ply  ( 24 Mbytes; triangles: 1310720 vertices: 655362)
subdivision of an icosehedron of 20 triangles, 8 times, based on "OpenGL's red book subdivision recipe", the model was cleaned to have just one connected surface, without cracks or duplicated vertices. Apparently there is a patent on constructing spheres, by normalizing inserted position vectors in subdivided tetrahedra/icosahedrons/etc, I guess it makes this model academic.