00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdio.h>
00010 #include <math.h>
00011 #include <ply.h>
00012
00013
00014
00015 typedef struct Vertex {
00016 float x,y,z;
00017 } Vertex;
00018
00019 typedef struct Face {
00020 unsigned char intensity;
00021 unsigned char nverts;
00022 int *verts;
00023 } Face;
00024
00025
00026
00027 Vertex verts[] = {
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[] = {
00039 { '\001', 4, NULL },
00040 { '\004', 4, NULL },
00041 { '\010', 4, NULL },
00042 { '\020', 4, NULL },
00043 { '\144', 4, NULL },
00044 { '\377', 4, NULL },
00045 };
00046
00047
00048
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
00061
00062 char *elem_names[] = {
00063 "vertex", "face"
00064 };
00065
00066 PlyProperty vert_props[] = {
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[] = {
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
00082
00083
00084 main()
00085 {
00086
00087 #if 1
00088
00089 write_test();
00090 #endif
00091
00092 #if 1
00093
00094 read_test();
00095 #endif
00096
00097 }
00098
00099
00100
00101
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
00116 for (i = 0; i < nfaces; i++)
00117 faces[i].verts = vert_ptrs[i];
00118
00119
00120
00121
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
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
00141 ply_put_comment (ply, "author: Greg Turk");
00142 ply_put_obj_info (ply, "random information");
00143
00144
00145
00146 ply_header_complete (ply);
00147
00148
00149 ply_put_element_setup (ply, "vertex");
00150 for (i = 0; i < nverts; i++)
00151 ply_put_element (ply, (void *) &verts[i]);
00152
00153
00154 ply_put_element_setup (ply, "face");
00155 for (i = 0; i < nfaces; i++)
00156 ply_put_element (ply, (void *) &faces[i]);
00157
00158
00159 ply_close (ply);
00160 }
00161
00162
00163
00164
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
00187 ply = ply_open_for_reading("test", &nelems, &elist, &file_type, &version);
00188
00189
00190 printf ("version %f\n", version);
00191 printf ("type %d\n", file_type);
00192
00193
00194
00195
00196 for (i = 0; i < nelems; i++) {
00197
00198
00199 elem_name = elist[i];
00200 plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
00201
00202
00203 printf ("element %s %d\n", elem_name, num_elems);
00204
00205
00206 if (equal_strings ("vertex", elem_name)) {
00207
00208
00209 vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems);
00210
00211
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
00218 for (j = 0; j < num_elems; j++) {
00219
00220
00221 vlist[j] = (Vertex *) malloc (sizeof (Vertex));
00222 ply_get_element (ply, (void *) vlist[j]);
00223
00224
00225 printf ("vertex: %g %g %g\n", vlist[j]->x, vlist[j]->y, vlist[j]->z);
00226 }
00227 }
00228
00229
00230 if (equal_strings ("face", elem_name)) {
00231
00232
00233 flist = (Face **) malloc (sizeof (Face *) * num_elems);
00234
00235
00236
00237 ply_get_property (ply, elem_name, &face_props[0]);
00238 ply_get_property (ply, elem_name, &face_props[1]);
00239
00240
00241 for (j = 0; j < num_elems; j++) {
00242
00243
00244 flist[j] = (Face *) malloc (sizeof (Face));
00245 ply_get_element (ply, (void *) flist[j]);
00246
00247
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
00256 for (j = 0; j < nprops; j++)
00257 printf ("property %s\n", plist[j]->name);
00258 }
00259
00260
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
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
00271 ply_close (ply);
00272 }
00273