[37] | 1 | /* |
---|
| 2 | |
---|
| 3 | Convert a shared-vertex PLY object into one where each face has its |
---|
| 4 | own vertices (un-shared vertex representation). |
---|
| 5 | |
---|
| 6 | Greg Turk, August 1994 |
---|
| 7 | |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include <stdio.h> |
---|
| 12 | #include <math.h> |
---|
| 13 | #include <malloc.h> |
---|
| 14 | #include <strings.h> |
---|
| 15 | #include <ply.h> |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | /* user's vertex and face definitions for a polygonal object */ |
---|
| 19 | |
---|
| 20 | typedef struct Vertex { |
---|
| 21 | void *other_props; /* other properties */ |
---|
| 22 | } Vertex; |
---|
| 23 | |
---|
| 24 | typedef struct Face { |
---|
| 25 | unsigned char nverts; /* number of vertex indices in list */ |
---|
| 26 | int *verts; /* vertex index list */ |
---|
| 27 | void *other_props; /* other properties */ |
---|
| 28 | } Face; |
---|
| 29 | |
---|
| 30 | char *elem_names[] = { /* list of the kinds of elements in the user's object */ |
---|
| 31 | "vertex", "face" |
---|
| 32 | }; |
---|
| 33 | |
---|
| 34 | PlyProperty face_props[] = { /* list of property information for a vertex */ |
---|
| 35 | {"vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts), |
---|
| 36 | 1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)}, |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | /*** the PLY object ***/ |
---|
| 41 | |
---|
| 42 | static int nverts,nfaces; |
---|
| 43 | static Vertex **vlist; |
---|
| 44 | static Face **flist; |
---|
| 45 | static PlyOtherElems *other_elements = NULL; |
---|
| 46 | static PlyOtherProp *vert_other,*face_other; |
---|
| 47 | static int nelems; |
---|
| 48 | static char **elist; |
---|
| 49 | static int num_comments; |
---|
| 50 | static char **comments; |
---|
| 51 | static int num_obj_info; |
---|
| 52 | static char **obj_info; |
---|
| 53 | static int file_type; |
---|
| 54 | |
---|
| 55 | void usage(char *progname); |
---|
| 56 | void write_file(); |
---|
| 57 | void read_file(FILE *inFile); |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /****************************************************************************** |
---|
| 61 | Turn a shared-vertex PLY object into an un-shared one. |
---|
| 62 | ******************************************************************************/ |
---|
| 63 | |
---|
| 64 | int |
---|
| 65 | main(int argc, char *argv[]) |
---|
| 66 | { |
---|
| 67 | int i,j; |
---|
| 68 | char *s; |
---|
| 69 | char *progname; |
---|
| 70 | FILE *inFile = stdin; |
---|
| 71 | |
---|
| 72 | progname = argv[0]; |
---|
| 73 | |
---|
| 74 | /* Parse -flags */ |
---|
| 75 | while (--argc > 0 && (*++argv)[0]=='-') { |
---|
| 76 | for (s = argv[0]+1; *s; s++) |
---|
| 77 | switch (*s) { |
---|
| 78 | default: |
---|
| 79 | usage (progname); |
---|
| 80 | exit (-1); |
---|
| 81 | break; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /* optional input file (if not, read stdin ) */ |
---|
| 86 | if (argc > 0 && *argv[0] != '-') { |
---|
| 87 | inFile = fopen(argv[0], "r"); |
---|
| 88 | if (inFile == NULL) { |
---|
| 89 | fprintf(stderr, "Error: Couldn't open input file %s\n", argv[0]); |
---|
| 90 | usage(progname); |
---|
| 91 | exit(-1); |
---|
| 92 | } |
---|
| 93 | argc --; |
---|
| 94 | argv ++; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /* Check no extra args */ |
---|
| 98 | if (argc > 0) { |
---|
| 99 | fprintf(stderr, "Error: Unhandled arg: %s\n", argv[0]); |
---|
| 100 | usage(progname); |
---|
| 101 | exit(-1); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | read_file(inFile); |
---|
| 105 | |
---|
| 106 | write_file(); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | /****************************************************************************** |
---|
| 111 | Print out usage information. |
---|
| 112 | ******************************************************************************/ |
---|
| 113 | |
---|
| 114 | void |
---|
| 115 | usage(char *progname) |
---|
| 116 | { |
---|
| 117 | fprintf (stderr, "usage: %s [in.ply] > out.ply\n", progname); |
---|
| 118 | fprintf (stderr, " or: %s < in.ply > out.ply\n", progname); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | /****************************************************************************** |
---|
| 123 | Read in the PLY file from standard in. |
---|
| 124 | ******************************************************************************/ |
---|
| 125 | |
---|
| 126 | void |
---|
| 127 | read_file(FILE *inFile) |
---|
| 128 | { |
---|
| 129 | int i,j,k; |
---|
| 130 | PlyFile *ply; |
---|
| 131 | int nprops; |
---|
| 132 | int num_elems; |
---|
| 133 | PlyProperty **plist; |
---|
| 134 | char *elem_name; |
---|
| 135 | float version; |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | /*** Read in the original PLY object ***/ |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | ply = ply_read (inFile, &nelems, &elist); |
---|
| 142 | ply_get_info (ply, &version, &file_type); |
---|
| 143 | |
---|
| 144 | for (i = 0; i < nelems; i++) { |
---|
| 145 | |
---|
| 146 | /* get the description of the first element */ |
---|
| 147 | elem_name = elist[i]; |
---|
| 148 | plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); |
---|
| 149 | |
---|
| 150 | if (equal_strings ("vertex", elem_name)) { |
---|
| 151 | |
---|
| 152 | /* create a vertex list to hold all the vertices */ |
---|
| 153 | vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems); |
---|
| 154 | nverts = num_elems; |
---|
| 155 | |
---|
| 156 | /* set up for getting vertex elements */ |
---|
| 157 | |
---|
| 158 | vert_other = ply_get_other_properties (ply, elem_name, |
---|
| 159 | offsetof(Vertex,other_props)); |
---|
| 160 | |
---|
| 161 | /* grab all the vertex elements */ |
---|
| 162 | for (j = 0; j < num_elems; j++) { |
---|
| 163 | vlist[j] = (Vertex *) malloc (sizeof (Vertex)); |
---|
| 164 | ply_get_element (ply, (void *) vlist[j]); |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | else if (equal_strings ("face", elem_name)) { |
---|
| 168 | |
---|
| 169 | /* create a list to hold all the face elements */ |
---|
| 170 | flist = (Face **) malloc (sizeof (Face *) * num_elems); |
---|
| 171 | nfaces = num_elems; |
---|
| 172 | |
---|
| 173 | /* set up for getting face elements */ |
---|
| 174 | |
---|
| 175 | ply_get_property (ply, elem_name, &face_props[0]); |
---|
| 176 | face_other = ply_get_other_properties (ply, elem_name, |
---|
| 177 | offsetof(Face,other_props)); |
---|
| 178 | |
---|
| 179 | /* grab all the face elements */ |
---|
| 180 | for (j = 0; j < num_elems; j++) { |
---|
| 181 | flist[j] = (Face *) malloc (sizeof (Face)); |
---|
| 182 | ply_get_element (ply, (void *) flist[j]); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | else |
---|
| 186 | other_elements = ply_get_other_element (ply, elem_name, num_elems); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | comments = ply_get_comments (ply, &num_comments); |
---|
| 190 | obj_info = ply_get_obj_info (ply, &num_obj_info); |
---|
| 191 | |
---|
| 192 | ply_close (ply); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | /****************************************************************************** |
---|
| 197 | Write out the PLY file to standard out. |
---|
| 198 | ******************************************************************************/ |
---|
| 199 | |
---|
| 200 | void |
---|
| 201 | write_file() |
---|
| 202 | { |
---|
| 203 | int i,j,k; |
---|
| 204 | PlyFile *ply; |
---|
| 205 | int num_elems; |
---|
| 206 | char *elem_name; |
---|
| 207 | int vert_count; |
---|
| 208 | int index; |
---|
| 209 | Face *face; |
---|
| 210 | |
---|
| 211 | /*** Write out the final PLY object ***/ |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | ply = ply_write (stdout, nelems, elist, file_type); |
---|
| 215 | |
---|
| 216 | /* count the un-shared vertices */ |
---|
| 217 | |
---|
| 218 | vert_count = 0; |
---|
| 219 | for (i = 0; i < nfaces; i++) |
---|
| 220 | vert_count += flist[i]->nverts; |
---|
| 221 | |
---|
| 222 | /* describe what properties go into the vertex and face elements */ |
---|
| 223 | |
---|
| 224 | ply_element_count (ply, "vertex", vert_count); |
---|
| 225 | ply_describe_other_properties (ply, vert_other, offsetof(Vertex,other_props)); |
---|
| 226 | |
---|
| 227 | ply_element_count (ply, "face", nfaces); |
---|
| 228 | ply_describe_property (ply, "face", &face_props[0]); |
---|
| 229 | ply_describe_other_properties (ply, face_other, offsetof(Face,other_props)); |
---|
| 230 | |
---|
| 231 | ply_describe_other_elements (ply, other_elements); |
---|
| 232 | |
---|
| 233 | for (i = 0; i < num_comments; i++) |
---|
| 234 | ply_put_comment (ply, comments[i]); |
---|
| 235 | |
---|
| 236 | for (i = 0; i < num_obj_info; i++) |
---|
| 237 | ply_put_obj_info (ply, obj_info[i]); |
---|
| 238 | |
---|
| 239 | ply_header_complete (ply); |
---|
| 240 | |
---|
| 241 | /* set up and write the vertex elements */ |
---|
| 242 | |
---|
| 243 | ply_put_element_setup (ply, "vertex"); |
---|
| 244 | |
---|
| 245 | for (i = 0; i < nfaces; i++) { |
---|
| 246 | face = flist[i]; |
---|
| 247 | for (j = 0; j < face->nverts; j++) |
---|
| 248 | ply_put_element (ply, (void *) vlist[face->verts[j]]); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /* set up and write the face elements */ |
---|
| 252 | |
---|
| 253 | ply_put_element_setup (ply, "face"); |
---|
| 254 | |
---|
| 255 | index = 0; |
---|
| 256 | for (i = 0; i < nfaces; i++) { |
---|
| 257 | face = flist[i]; |
---|
| 258 | for (j = 0; j < face->nverts; j++) |
---|
| 259 | face->verts[j] = index++; |
---|
| 260 | ply_put_element (ply, (void *) face); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | ply_put_other_elements (ply); |
---|
| 264 | |
---|
| 265 | /* close the PLY file */ |
---|
| 266 | ply_close (ply); |
---|
| 267 | } |
---|
| 268 | |
---|