[37] | 1 | /* |
---|
| 2 | |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <stdlib.h> |
---|
| 7 | #include <math.h> |
---|
| 8 | #include <malloc.h> |
---|
| 9 | #include <ply.h> |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | /* user's vertex and face definitions for a polygonal object */ |
---|
| 13 | |
---|
| 14 | typedef struct Vertex { |
---|
| 15 | float x,y,z; |
---|
| 16 | char *other_props; |
---|
| 17 | } Vertex; |
---|
| 18 | |
---|
| 19 | char *elem_names[] = { /* list of the kinds of elements in the user's object */ |
---|
| 20 | "vertex" |
---|
| 21 | }; |
---|
| 22 | |
---|
| 23 | PlyProperty vert_props[] = { /* list of property information for a vertex */ |
---|
| 24 | {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0}, |
---|
| 25 | {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0}, |
---|
| 26 | {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0}, |
---|
| 27 | }; |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | /*** the PLY object ***/ |
---|
| 31 | |
---|
| 32 | int nverts; |
---|
| 33 | Vertex **vlist; |
---|
| 34 | PlyOtherElems *other_elements = NULL; |
---|
| 35 | PlyOtherProp *vert_other; |
---|
| 36 | int nelems; |
---|
| 37 | char **elist; |
---|
| 38 | int num_comments; |
---|
| 39 | char **comments; |
---|
| 40 | int num_obj_info; |
---|
| 41 | char **obj_info; |
---|
| 42 | int file_type; |
---|
| 43 | |
---|
| 44 | static float scale; |
---|
| 45 | |
---|
| 46 | void usage(char *progname); |
---|
| 47 | void write_file(); |
---|
| 48 | void read_file(FILE *inFile); |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | /****************************************************************************** |
---|
| 52 | Transform a PLY file. |
---|
| 53 | ******************************************************************************/ |
---|
| 54 | |
---|
| 55 | int |
---|
| 56 | main(int argc, char *argv[]) |
---|
| 57 | { |
---|
| 58 | int i,j; |
---|
| 59 | char *s; |
---|
| 60 | char *progname; |
---|
| 61 | |
---|
| 62 | char *inName = NULL; |
---|
| 63 | FILE *inFile = NULL; |
---|
| 64 | |
---|
| 65 | progname = argv[0]; |
---|
| 66 | |
---|
| 67 | scale = 1; |
---|
| 68 | |
---|
| 69 | /* Read all the -args */ |
---|
| 70 | while (--argc > 0 && (*++argv)[0]=='-') { |
---|
| 71 | for (s = argv[0]+1; *s; s++) |
---|
| 72 | switch (*s) { |
---|
| 73 | case 's': |
---|
| 74 | scale = atof(*++argv); |
---|
| 75 | argc -= 1; |
---|
| 76 | break; |
---|
| 77 | default: |
---|
| 78 | usage (progname); |
---|
| 79 | exit (-1); |
---|
| 80 | break; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /* Get the input file name */ |
---|
| 85 | if (argc == 0) { |
---|
| 86 | /* No input name -- read from stdin */ |
---|
| 87 | inFile = stdin; |
---|
| 88 | } else if (argc == 1 && argv[0][0] != '-') { |
---|
| 89 | /* Open input file */ |
---|
| 90 | inName = argv[0]; |
---|
| 91 | inFile = fopen(inName, "r"); |
---|
| 92 | if (inFile == NULL) { |
---|
| 93 | fprintf(stderr, "Error, could not open input ply file: %s\n", inName); |
---|
| 94 | usage(progname); |
---|
| 95 | exit(-1); |
---|
| 96 | } |
---|
| 97 | } else { |
---|
| 98 | fprintf(stderr, "Unhandled argument: %s\n", argv[0]); |
---|
| 99 | usage(progname); |
---|
| 100 | exit(-1); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | read_file(inFile); |
---|
| 104 | |
---|
| 105 | write_file(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | /****************************************************************************** |
---|
| 110 | Print out usage information. |
---|
| 111 | ******************************************************************************/ |
---|
| 112 | |
---|
| 113 | void |
---|
| 114 | usage(char *progname) |
---|
| 115 | { |
---|
| 116 | fprintf (stderr, "Usage: %s [flags] [in.ply] > out\n", progname); |
---|
| 117 | fprintf (stderr, " or: %s [flags] < in.ply > out\n", progname); |
---|
| 118 | fprintf (stderr, "Flags:\n"); |
---|
| 119 | fprintf (stderr, " -s scale\n"); |
---|
| 120 | exit(-1); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | /****************************************************************************** |
---|
| 125 | Transform the PLY object. |
---|
| 126 | ******************************************************************************/ |
---|
| 127 | |
---|
| 128 | /****************************************************************************** |
---|
| 129 | Read in the PLY file from standard in. |
---|
| 130 | ******************************************************************************/ |
---|
| 131 | |
---|
| 132 | void |
---|
| 133 | read_file(FILE *inFile) |
---|
| 134 | { |
---|
| 135 | int i,j,k; |
---|
| 136 | PlyFile *ply; |
---|
| 137 | int nprops; |
---|
| 138 | int num_elems; |
---|
| 139 | PlyProperty **plist; |
---|
| 140 | char *elem_name; |
---|
| 141 | float version; |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | /*** Read in the original PLY object ***/ |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | ply = ply_read (inFile, &nelems, &elist); |
---|
| 148 | ply_get_info (ply, &version, &file_type); |
---|
| 149 | |
---|
| 150 | for (i = 0; i < nelems; i++) { |
---|
| 151 | |
---|
| 152 | /* get the description of the first element */ |
---|
| 153 | elem_name = elist[i]; |
---|
| 154 | plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); |
---|
| 155 | |
---|
| 156 | if (equal_strings ("vertex", elem_name)) { |
---|
| 157 | |
---|
| 158 | /* create a vertex list to hold all the vertices */ |
---|
| 159 | vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems); |
---|
| 160 | nverts = num_elems; |
---|
| 161 | |
---|
| 162 | /* set up for getting vertex elements */ |
---|
| 163 | |
---|
| 164 | ply_get_property (ply, elem_name, &vert_props[0]); |
---|
| 165 | ply_get_property (ply, elem_name, &vert_props[1]); |
---|
| 166 | ply_get_property (ply, elem_name, &vert_props[2]); |
---|
| 167 | vert_other = ply_get_other_properties (ply, elem_name, |
---|
| 168 | offsetof(Vertex,other_props)); |
---|
| 169 | |
---|
| 170 | /* grab all the vertex elements */ |
---|
| 171 | for (j = 0; j < num_elems; j++) { |
---|
| 172 | vlist[j] = (Vertex *) malloc (sizeof (Vertex)); |
---|
| 173 | ply_get_element (ply, (void *) vlist[j]); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | comments = ply_get_comments (ply, &num_comments); |
---|
| 179 | obj_info = ply_get_obj_info (ply, &num_obj_info); |
---|
| 180 | |
---|
| 181 | ply_close (ply); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | /****************************************************************************** |
---|
| 186 | Write out the PLY file to standard out. |
---|
| 187 | ******************************************************************************/ |
---|
| 188 | |
---|
| 189 | void |
---|
| 190 | write_file() |
---|
| 191 | { |
---|
| 192 | int i,j,k; |
---|
| 193 | |
---|
| 194 | for (i = 0; i < nverts; i++) |
---|
| 195 | fprintf(stdout, "%d %d %d\n", (int)(scale*vlist[i]->x), |
---|
| 196 | (int)(scale*vlist[i]->y), (int)(scale*vlist[i]->z)); |
---|
| 197 | } |
---|
| 198 | |
---|