[37] | 1 | /* |
---|
| 2 | |
---|
| 3 | Apply a 3-D transformation to an object from a PLY file. |
---|
| 4 | |
---|
| 5 | Greg Turk, August 1994 |
---|
| 6 | |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include <math.h> |
---|
| 12 | #include <malloc.h> |
---|
| 13 | #include <ply.h> |
---|
| 14 | |
---|
| 15 | #include "Linear.h" |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | /* user's vertex and face definitions for a polygonal object */ |
---|
| 19 | |
---|
| 20 | typedef struct Vertex { |
---|
| 21 | float x,y,z; |
---|
| 22 | void *other_props; /* other properties */ |
---|
| 23 | } Vertex; |
---|
| 24 | |
---|
| 25 | char *elem_names[] = { /* list of the kinds of elements in the user's object */ |
---|
| 26 | "vertex" |
---|
| 27 | }; |
---|
| 28 | |
---|
| 29 | PlyProperty vert_props[] = { /* list of property information for a vertex */ |
---|
| 30 | {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0}, |
---|
| 31 | {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0}, |
---|
| 32 | {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0}, |
---|
| 33 | }; |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | /*** the PLY object ***/ |
---|
| 37 | |
---|
| 38 | int nverts; |
---|
| 39 | Vertex **vlist; |
---|
| 40 | PlyOtherElems *other_elements = NULL; |
---|
| 41 | PlyOtherProp *vert_other; |
---|
| 42 | int nelems; |
---|
| 43 | char **elist; |
---|
| 44 | int num_comments; |
---|
| 45 | char **comments; |
---|
| 46 | int num_obj_info; |
---|
| 47 | char **obj_info; |
---|
| 48 | int file_type; |
---|
| 49 | |
---|
| 50 | static float xtrans = 0; |
---|
| 51 | static float ytrans = 0; |
---|
| 52 | static float ztrans = 0; |
---|
| 53 | |
---|
| 54 | static float xscale = 1; |
---|
| 55 | static float yscale = 1; |
---|
| 56 | static float zscale = 1; |
---|
| 57 | |
---|
| 58 | static float rotx = 0; |
---|
| 59 | static float roty = 0; |
---|
| 60 | static float rotz = 0; |
---|
| 61 | |
---|
| 62 | static Quaternion quat; |
---|
| 63 | |
---|
| 64 | static Matrix4f xfmat; |
---|
| 65 | |
---|
| 66 | void usage(char *progname); |
---|
| 67 | void write_file(); |
---|
| 68 | void read_file(FILE *inFile); |
---|
| 69 | void transform(); |
---|
| 70 | |
---|
| 71 | /****************************************************************************** |
---|
| 72 | Transform a PLY file. |
---|
| 73 | ******************************************************************************/ |
---|
| 74 | |
---|
| 75 | int |
---|
| 76 | main(int argc, char *argv[]) |
---|
| 77 | { |
---|
| 78 | int i,j; |
---|
| 79 | char *s; |
---|
| 80 | char *progname; |
---|
| 81 | char *xfname = NULL; |
---|
| 82 | FILE *inFile = stdin; |
---|
| 83 | |
---|
| 84 | progname = argv[0]; |
---|
| 85 | |
---|
| 86 | quat.q[0] = 0; |
---|
| 87 | quat.q[1] = 0; |
---|
| 88 | quat.q[2] = 0; |
---|
| 89 | quat.q[3] = 1; |
---|
| 90 | |
---|
| 91 | xfmat.makeIdentity(); |
---|
| 92 | |
---|
| 93 | /* Parse -flags */ |
---|
| 94 | while (--argc > 0 && (*++argv)[0]=='-') { |
---|
| 95 | for (s = argv[0]+1; *s; s++) |
---|
| 96 | switch (*s) { |
---|
| 97 | case 's': |
---|
| 98 | if (argc < 4) usage(progname); |
---|
| 99 | xscale = atof (*++argv); |
---|
| 100 | yscale = atof (*++argv); |
---|
| 101 | zscale = atof (*++argv); |
---|
| 102 | argc -= 3; |
---|
| 103 | break; |
---|
| 104 | case 'f': |
---|
| 105 | if (argc < 2) usage(progname); |
---|
| 106 | xfname = (*++argv); |
---|
| 107 | argc-=1; |
---|
| 108 | break; |
---|
| 109 | case 't': |
---|
| 110 | if (argc < 4) usage(progname); |
---|
| 111 | xtrans = atof (*++argv); |
---|
| 112 | ytrans = atof (*++argv); |
---|
| 113 | ztrans = atof (*++argv); |
---|
| 114 | argc -= 3; |
---|
| 115 | break; |
---|
| 116 | case 'r': |
---|
| 117 | if (argc < 4) usage(progname); |
---|
| 118 | rotx = atof (*++argv) * M_PI/180; |
---|
| 119 | roty = atof (*++argv) * M_PI/180; |
---|
| 120 | rotz = atof (*++argv) * M_PI/180; |
---|
| 121 | argc -= 3; |
---|
| 122 | break; |
---|
| 123 | case 'q': |
---|
| 124 | if (argc < 5) usage(progname); |
---|
| 125 | quat.q[0] = atof (*++argv); |
---|
| 126 | quat.q[1] = atof (*++argv); |
---|
| 127 | quat.q[2] = atof (*++argv); |
---|
| 128 | quat.q[3] = atof (*++argv); |
---|
| 129 | argc -= 4; |
---|
| 130 | break; |
---|
| 131 | default: |
---|
| 132 | usage(progname); |
---|
| 133 | exit(-1); |
---|
| 134 | break; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /* optional input file (if not, read stdin ) */ |
---|
| 139 | if (argc > 0 && *argv[0] != '-') { |
---|
| 140 | inFile = fopen(argv[0], "r"); |
---|
| 141 | if (inFile == NULL) { |
---|
| 142 | fprintf(stderr, "Error: Couldn't open input file %s\n", argv[0]); |
---|
| 143 | usage(progname); |
---|
| 144 | exit(-1); |
---|
| 145 | } |
---|
| 146 | argc --; |
---|
| 147 | argv ++; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /* Check no extra args */ |
---|
| 151 | if (argc > 0) { |
---|
| 152 | fprintf(stderr, "Error: Unhandled arg: %s\n", argv[0]); |
---|
| 153 | usage(progname); |
---|
| 154 | exit(-1); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /* Read xf file if given... */ |
---|
| 158 | if (xfname) { |
---|
| 159 | FILE *xf = fopen(xfname, "r"); |
---|
| 160 | if (xf == NULL) { |
---|
| 161 | fprintf(stderr, "Error, couldn't open .xf file %s\n", xfname); |
---|
| 162 | usage(progname); |
---|
| 163 | exit(-1); |
---|
| 164 | } |
---|
| 165 | for (int i=0; i < 4; i++) { |
---|
| 166 | float a,b,c,d; |
---|
| 167 | fscanf(xf, "%f %f %f %f\n", &a, &b, &c, &d); |
---|
| 168 | xfmat.setElem(i,0,a); |
---|
| 169 | xfmat.setElem(i,1,b); |
---|
| 170 | xfmat.setElem(i,2,c); |
---|
| 171 | xfmat.setElem(i,3,d); |
---|
| 172 | } |
---|
| 173 | fclose(xf); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | read_file(inFile); |
---|
| 177 | transform(); |
---|
| 178 | write_file(); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | /****************************************************************************** |
---|
| 183 | Transform the PLY object. |
---|
| 184 | ******************************************************************************/ |
---|
| 185 | |
---|
| 186 | void |
---|
| 187 | transform() |
---|
| 188 | { |
---|
| 189 | int i; |
---|
| 190 | Vertex *vert; |
---|
| 191 | Vec3f vec1, vec2; |
---|
| 192 | Matrix4f mat, qmat; |
---|
| 193 | |
---|
| 194 | quat.toMatrix(qmat); |
---|
| 195 | mat.makeIdentity(); |
---|
| 196 | mat.scale(xscale, yscale, zscale); |
---|
| 197 | mat.rotateX(rotx); |
---|
| 198 | mat.rotateY(roty); |
---|
| 199 | mat.rotateZ(rotz); |
---|
| 200 | mat.multLeft(qmat); |
---|
| 201 | mat.setTranslate(xtrans, ytrans, ztrans); |
---|
| 202 | mat.multLeft(xfmat); |
---|
| 203 | |
---|
| 204 | for (i = 0; i < nverts; i++) { |
---|
| 205 | vert = vlist[i]; |
---|
| 206 | vec1.setValue(vert->x, vert->y, vert->z); |
---|
| 207 | mat.multVec(vec1, vec2); |
---|
| 208 | vert->x = vec2.x; |
---|
| 209 | vert->y = vec2.y; |
---|
| 210 | vert->z = vec2.z; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | /****************************************************************************** |
---|
| 216 | Read in the PLY file from standard in. |
---|
| 217 | ******************************************************************************/ |
---|
| 218 | |
---|
| 219 | void |
---|
| 220 | read_file(FILE *inFile) |
---|
| 221 | { |
---|
| 222 | int i,j,k; |
---|
| 223 | PlyFile *ply; |
---|
| 224 | int nprops; |
---|
| 225 | int num_elems; |
---|
| 226 | char *elem_name; |
---|
| 227 | float version; |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | /*** Read in the original PLY object ***/ |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | ply = ply_read (inFile, &nelems, &elist); |
---|
| 234 | ply_get_info (ply, &version, &file_type); |
---|
| 235 | |
---|
| 236 | for (i = 0; i < nelems; i++) { |
---|
| 237 | |
---|
| 238 | /* get the description of the first element */ |
---|
| 239 | elem_name = elist[i]; |
---|
| 240 | ply_get_element_description (ply, elem_name, &num_elems, &nprops); |
---|
| 241 | |
---|
| 242 | if (equal_strings ("vertex", elem_name)) { |
---|
| 243 | |
---|
| 244 | /* create a vertex list to hold all the vertices */ |
---|
| 245 | vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems); |
---|
| 246 | nverts = num_elems; |
---|
| 247 | |
---|
| 248 | /* set up for getting vertex elements */ |
---|
| 249 | |
---|
| 250 | ply_get_property (ply, elem_name, &vert_props[0]); |
---|
| 251 | ply_get_property (ply, elem_name, &vert_props[1]); |
---|
| 252 | ply_get_property (ply, elem_name, &vert_props[2]); |
---|
| 253 | vert_other = ply_get_other_properties (ply, elem_name, |
---|
| 254 | offsetof(Vertex,other_props)); |
---|
| 255 | |
---|
| 256 | /* grab all the vertex elements */ |
---|
| 257 | for (j = 0; j < num_elems; j++) { |
---|
| 258 | vlist[j] = (Vertex *) malloc (sizeof (Vertex)); |
---|
| 259 | ply_get_element (ply, (void *) vlist[j]); |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | else |
---|
| 263 | other_elements = ply_get_other_element (ply, elem_name, num_elems); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | comments = ply_get_comments (ply, &num_comments); |
---|
| 267 | obj_info = ply_get_obj_info (ply, &num_obj_info); |
---|
| 268 | |
---|
| 269 | ply_close (ply); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | /****************************************************************************** |
---|
| 274 | Write out the PLY file to standard out. |
---|
| 275 | ******************************************************************************/ |
---|
| 276 | |
---|
| 277 | void |
---|
| 278 | write_file() |
---|
| 279 | { |
---|
| 280 | int i,j,k; |
---|
| 281 | PlyFile *ply; |
---|
| 282 | int num_elems; |
---|
| 283 | char *elem_name; |
---|
| 284 | |
---|
| 285 | /*** Write out the transformed PLY object ***/ |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | ply = ply_write (stdout, nelems, elist, file_type); |
---|
| 289 | |
---|
| 290 | |
---|
| 291 | /* describe what properties go into the vertex and face elements */ |
---|
| 292 | |
---|
| 293 | ply_element_count (ply, "vertex", nverts); |
---|
| 294 | ply_describe_property (ply, "vertex", &vert_props[0]); |
---|
| 295 | ply_describe_property (ply, "vertex", &vert_props[1]); |
---|
| 296 | ply_describe_property (ply, "vertex", &vert_props[2]); |
---|
| 297 | ply_describe_other_properties (ply, vert_other, offsetof(Vertex,other_props)); |
---|
| 298 | |
---|
| 299 | ply_describe_other_elements (ply, other_elements); |
---|
| 300 | |
---|
| 301 | for (i = 0; i < num_comments; i++) |
---|
| 302 | ply_put_comment (ply, comments[i]); |
---|
| 303 | |
---|
| 304 | for (i = 0; i < num_obj_info; i++) |
---|
| 305 | ply_put_obj_info (ply, obj_info[i]); |
---|
| 306 | |
---|
| 307 | ply_header_complete (ply); |
---|
| 308 | |
---|
| 309 | /* set up and write the vertex elements */ |
---|
| 310 | ply_put_element_setup (ply, "vertex"); |
---|
| 311 | for (i = 0; i < nverts; i++) |
---|
| 312 | ply_put_element (ply, (void *) vlist[i]); |
---|
| 313 | |
---|
| 314 | ply_put_other_elements (ply); |
---|
| 315 | |
---|
| 316 | ply_close (ply); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | |
---|
| 320 | /****************************************************************************** |
---|
| 321 | Print out usage information. |
---|
| 322 | ******************************************************************************/ |
---|
| 323 | |
---|
| 324 | void |
---|
| 325 | usage(char *progname) |
---|
| 326 | { |
---|
| 327 | fprintf (stderr, "usage: %s [flags] [in.ply] > out.ply\n", progname); |
---|
| 328 | fprintf (stderr, " or: %s [flags] < in.ply > out.ply\n", progname); |
---|
| 329 | fprintf (stderr, " -f m.xf (a transform matrix file)\n"); |
---|
| 330 | fprintf (stderr, " -t xtrans ytrans ztrans\n"); |
---|
| 331 | fprintf (stderr, " -s xscale yscale zscale\n"); |
---|
| 332 | fprintf (stderr, " -r xangle yangle zangle (all in degrees)\n"); |
---|
| 333 | fprintf (stderr, " -q qi qj qk ql\n"); |
---|
| 334 | fprintf (stderr, " (point = m.xf * (ftrans_factor + rotz * roty * rotx * scale_factor * point))\n"); |
---|
| 335 | exit (-1); |
---|
| 336 | } |
---|