Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

plytest.c

Go to the documentation of this file.
00001 /*
00002 
00003 Sample code showing how to read and write PLY polygon files.
00004 
00005 Greg Turk, March 1994
00006 
00007 */
00008 
00009 #include <stdio.h>
00010 #include <math.h>
00011 #include <ply.h>
00012 
00013 /* user's vertex and face definitions for a polygonal object */
00014 
00015 typedef struct Vertex {
00016   float x,y,z;             /* the usual 3-space position of a vertex */
00017 } Vertex;
00018 
00019 typedef struct Face {
00020   unsigned char intensity; /* this user attaches intensity to faces */
00021   unsigned char nverts;    /* number of vertex indices in list */
00022   int *verts;              /* vertex index list */
00023 } Face;
00024 
00025 /* polygon description of an object (a cube) */
00026 
00027 Vertex verts[] = {  /* vertices */
00028   { 0.0, 0.0, 0.0},
00029   { 1.0, 0.0, 0.0},
00030   { 1.0, 1.0, 0.0},
00031   { 0.0, 1.0, 0.0},
00032   { 0.0, 0.0, 1.0},
00033   { 1.0, 0.0, 1.0},
00034   { 1.0, 1.0, 1.0},
00035   { 0.0, 1.0, 1.0},
00036 };
00037 
00038 Face faces[] = {  /* faces */
00039   { '\001', 4, NULL },  /* intensity, vertex list count, vertex list (empty) */
00040   { '\004', 4, NULL },
00041   { '\010', 4, NULL },
00042   { '\020', 4, NULL },
00043   { '\144', 4, NULL },
00044   { '\377', 4, NULL },
00045 };
00046 
00047 /* list of vertices for each face */
00048 /* (notice that indices begin at zero) */
00049 
00050 typedef int Vertex_Indices[4];
00051 Vertex_Indices vert_ptrs[] = {
00052   { 0, 1, 2, 3 },
00053   { 7, 6, 5, 4 },
00054   { 0, 4, 5, 1 },
00055   { 1, 5, 6, 2 },
00056   { 2, 6, 7, 3 },
00057   { 3, 7, 4, 0 },
00058 };
00059 
00060 /* information needed to describe the user's data to the PLY routines */
00061 
00062 char *elem_names[] = { /* list of the kinds of elements in the user's object */
00063   "vertex", "face"
00064 };
00065 
00066 PlyProperty vert_props[] = { /* list of property information for a vertex */
00067   {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0},
00068   {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0},
00069   {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0},
00070 };
00071 
00072 PlyProperty face_props[] = { /* list of property information for a vertex */
00073   {"intensity", PLY_UCHAR, PLY_UCHAR, offsetof(Face,intensity), 0, 0, 0, 0},
00074   {"vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts),
00075    1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)},
00076 };
00077 
00078 
00079 
00080 /******************************************************************************
00081 The main routine just creates and then reads a PLY file.
00082 ******************************************************************************/
00083 
00084 main()
00085 {
00086 
00087 #if 1
00088   /* write a PLY file */
00089   write_test();
00090 #endif
00091 
00092 #if 1
00093   /* read a PLY file */
00094   read_test();
00095 #endif
00096 
00097 }
00098 
00099 
00100 /******************************************************************************
00101 Write out a PLY file.
00102 ******************************************************************************/
00103 
00104 write_test()
00105 {
00106   int i,j;
00107   PlyFile *ply;
00108   int nelems;
00109   char **elist;
00110   int file_type;
00111   float version;
00112   int nverts = sizeof (verts) / sizeof (Vertex);
00113   int nfaces = sizeof (faces) / sizeof (Face);
00114 
00115   /* create the vertex index lists for the faces */
00116   for (i = 0; i < nfaces; i++)
00117     faces[i].verts = vert_ptrs[i];
00118 
00119   /* open either a binary or ascii PLY file for writing */
00120   /* (the file will be called "test.ply" because the routines */
00121   /*  enforce the .ply filename extension) */
00122 
00123 #if 1
00124   ply = ply_open_for_writing("test", 2, elem_names, PLY_ASCII, &version);
00125 #else
00126   ply = ply_open_for_writing("test", 2, elem_names, PLY_BINARY_BE, &version);
00127 #endif
00128 
00129   /* describe what properties go into the vertex and face elements */
00130 
00131   ply_element_count (ply, "vertex", nverts);
00132   ply_describe_property (ply, "vertex", &vert_props[0]);
00133   ply_describe_property (ply, "vertex", &vert_props[1]);
00134   ply_describe_property (ply, "vertex", &vert_props[2]);
00135 
00136   ply_element_count (ply, "face", nfaces);
00137   ply_describe_property (ply, "face", &face_props[0]);
00138   ply_describe_property (ply, "face", &face_props[1]);
00139 
00140   /* write a comment and an object information field */
00141   ply_put_comment (ply, "author: Greg Turk");
00142   ply_put_obj_info (ply, "random information");
00143 
00144   /* we have described exactly what we will put in the file, so */
00145   /* we are now done with the header info */
00146   ply_header_complete (ply);
00147 
00148   /* set up and write the vertex elements */
00149   ply_put_element_setup (ply, "vertex");
00150   for (i = 0; i < nverts; i++)
00151     ply_put_element (ply, (void *) &verts[i]);
00152 
00153   /* set up and write the face elements */
00154   ply_put_element_setup (ply, "face");
00155   for (i = 0; i < nfaces; i++)
00156     ply_put_element (ply, (void *) &faces[i]);
00157 
00158   /* close the PLY file */
00159   ply_close (ply);
00160 }
00161 
00162 
00163 /******************************************************************************
00164 Read in a PLY file.
00165 ******************************************************************************/
00166 
00167 read_test()
00168 {
00169   int i,j,k;
00170   PlyFile *ply;
00171   int nelems;
00172   char **elist;
00173   int file_type;
00174   float version;
00175   int nprops;
00176   int num_elems;
00177   PlyProperty **plist;
00178   Vertex **vlist;
00179   Face **flist;
00180   char *elem_name;
00181   int num_comments;
00182   char **comments;
00183   int num_obj_info;
00184   char **obj_info;
00185 
00186   /* open a PLY file for reading */
00187   ply = ply_open_for_reading("test", &nelems, &elist, &file_type, &version);
00188 
00189   /* print what we found out about the file */
00190   printf ("version %f\n", version);
00191   printf ("type %d\n", file_type);
00192 
00193   /* go through each kind of element that we learned is in the file */
00194   /* and read them */
00195 
00196   for (i = 0; i < nelems; i++) {
00197 
00198     /* get the description of the first element */
00199     elem_name = elist[i];
00200     plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
00201 
00202     /* print the name of the element, for debugging */
00203     printf ("element %s %d\n", elem_name, num_elems);
00204 
00205     /* if we're on vertex elements, read them in */
00206     if (equal_strings ("vertex", elem_name)) {
00207 
00208       /* create a vertex list to hold all the vertices */
00209       vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems);
00210 
00211       /* set up for getting vertex elements */
00212 
00213       ply_get_property (ply, elem_name, &vert_props[0]);
00214       ply_get_property (ply, elem_name, &vert_props[1]);
00215       ply_get_property (ply, elem_name, &vert_props[2]);
00216 
00217       /* grab all the vertex elements */
00218       for (j = 0; j < num_elems; j++) {
00219 
00220         /* grab and element from the file */
00221         vlist[j] = (Vertex *) malloc (sizeof (Vertex));
00222         ply_get_element (ply, (void *) vlist[j]);
00223 
00224         /* print out vertex x,y,z for debugging */
00225         printf ("vertex: %g %g %g\n", vlist[j]->x, vlist[j]->y, vlist[j]->z);
00226       }
00227     }
00228 
00229     /* if we're on face elements, read them in */
00230     if (equal_strings ("face", elem_name)) {
00231 
00232       /* create a list to hold all the face elements */
00233       flist = (Face **) malloc (sizeof (Face *) * num_elems);
00234 
00235       /* set up for getting face elements */
00236 
00237       ply_get_property (ply, elem_name, &face_props[0]);
00238       ply_get_property (ply, elem_name, &face_props[1]);
00239 
00240       /* grab all the face elements */
00241       for (j = 0; j < num_elems; j++) {
00242 
00243         /* grab and element from the file */
00244         flist[j] = (Face *) malloc (sizeof (Face));
00245         ply_get_element (ply, (void *) flist[j]);
00246 
00247         /* print out face info, for debugging */
00248         printf ("face: %d, list = ", flist[j]->intensity);
00249         for (k = 0; k < flist[j]->nverts; k++)
00250           printf ("%d ", flist[j]->verts[k]);
00251         printf ("\n");
00252       }
00253     }
00254     
00255     /* print out the properties we got, for debugging */
00256     for (j = 0; j < nprops; j++)
00257       printf ("property %s\n", plist[j]->name);
00258   }
00259 
00260   /* grab and print out the comments in the file */
00261   comments = ply_get_comments (ply, &num_comments);
00262   for (i = 0; i < num_comments; i++)
00263     printf ("comment = '%s'\n", comments[i]);
00264 
00265   /* grab and print out the object information */
00266   obj_info = ply_get_obj_info (ply, &num_obj_info);
00267   for (i = 0; i < num_obj_info; i++)
00268     printf ("obj_info = '%s'\n", obj_info[i]);
00269 
00270   /* close the PLY file */
00271   ply_close (ply);
00272 }
00273 
 

Powered by Plone

This site conforms to the following standards: