source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/vrippack-0.31/src/plytools/plyflip.c @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 9.2 KB
Line 
1/*
2
3Flip the order of vertices in the faces and/or flip the vertex normals.
4
5Greg Turk, August 1994
6
7*/
8
9#include <stdlib.h>
10#include <stdio.h>
11#include <math.h>
12#include <malloc.h>
13#include <strings.h>
14#include <ply.h>
15
16/* user's vertex and face definitions for a polygonal object */
17
18typedef struct Vertex {
19  float x,y,z;
20  float nx,ny,nz;
21  void *other_props;       /* other properties */
22} Vertex;
23
24typedef 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
30char *elem_names[] = { /* list of the kinds of elements in the user's object */
31  "vertex", "face"
32};
33
34PlyProperty vert_props[] = { /* list of property information for a vertex */
35  {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0},
36  {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0},
37  {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0},
38  {"nx", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,nx), 0, 0, 0, 0},
39  {"ny", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,ny), 0, 0, 0, 0},
40  {"nz", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,nz), 0, 0, 0, 0},
41};
42
43PlyProperty face_props[] = { /* list of property information for a vertex */
44  {"vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts),
45   1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)},
46};
47
48
49/*** the PLY object ***/
50
51static int nverts,nfaces;
52static Vertex **vlist;
53static Face **flist;
54static PlyOtherElems *other_elements = NULL;
55static PlyOtherProp *vert_other,*face_other;
56static int nelems;
57static char **elist;
58static int num_comments;
59static char **comments;
60static int num_obj_info;
61static char **obj_info;
62static int file_type;
63
64static int flip_order = 1;       /* flip order of vertices around the faces? */
65static int flip_normals = 0;     /* flip vertex normals? */
66static int has_nx,has_ny,has_nz; /* are normals in PLY file? */
67
68void usage(char *progname);
69void write_file();
70void read_file(FILE *inFile);
71void negate_normals();
72void flip_vertex_order();
73
74
75/******************************************************************************
76Main program.
77******************************************************************************/
78
79int
80main(int argc, char *argv[])
81{
82  int i,j;
83  char *s;
84  char *progname;
85  FILE *inFile = stdin;
86
87  progname = argv[0];
88
89  /* Parse -flags */
90  while (--argc > 0 && (*++argv)[0]=='-') {
91    for (s = argv[0]+1; *s; s++)
92      switch (*s) {
93        case 'n':
94          flip_normals = 1;
95          flip_order = 0;
96          break;
97        case 'b':
98          flip_normals = 1;
99          flip_order = 1;
100          break;
101        default:
102          usage (progname);
103          exit (-1);
104          break;
105      }
106  }
107
108
109   /* optional input file (if not, read stdin ) */
110   if (argc > 0 && *argv[0] != '-') {
111       inFile = fopen(argv[0], "r");
112       if (inFile == NULL) {
113           fprintf(stderr, "Error: Couldn't open input file %s\n", argv[0]);
114           usage(progname);
115           exit(-1);
116       }
117       argc --;
118       argv ++;
119   } 
120
121   /* Check no extra args */
122   if (argc > 0) {
123     fprintf(stderr, "Error: Unhandled arg: %s\n", argv[0]);
124     usage(progname);
125     exit(-1);
126   }
127
128  read_file(inFile);
129
130  /* maybe flip the order of the vertices in each face */
131  if (flip_order)
132    flip_vertex_order();
133
134  /* maybe flip the vertex normals */
135  if (flip_normals)
136    negate_normals();
137
138  write_file();
139}
140
141
142/******************************************************************************
143Print out usage information.
144******************************************************************************/
145
146void
147usage(char *progname)
148{
149  fprintf (stderr, "usage: %s [flags] [in.ply] > out.ply\n", progname);
150  fprintf (stderr, "   or: %s [flags] < in.ply > out.ply\n", progname);
151  fprintf (stderr, "       -n (flip normals)\n");
152  fprintf (stderr, "       -b (flip both normals and vertex order in faces)\n");
153}
154
155
156/******************************************************************************
157Reverse the order of the vertices in each face.
158******************************************************************************/
159
160void
161flip_vertex_order()
162{
163  int i,j;
164  int temp;
165  int num;
166  Face *face;
167
168  for (i = 0; i < nfaces; i++) {
169
170    face = flist[i];
171    num = face->nverts;
172
173    /* swap early vertices with later ones */
174    for (j = 0; j < num / 2; j++) {
175      temp = face->verts[j];
176      face->verts[j] = face->verts[num-j-1];
177      face->verts[num-j-1] = temp;
178    }
179  }
180}
181
182
183/******************************************************************************
184Negate the vertex normals.
185******************************************************************************/
186
187void
188negate_normals()
189{
190  int i;
191
192  for (i = 0; i < nverts; i++) {
193    if (has_nx) vlist[i]->nx *= -1;
194    if (has_ny) vlist[i]->ny *= -1;
195    if (has_nz) vlist[i]->nz *= -1;
196  }
197}
198
199
200/******************************************************************************
201Read in the PLY file from standard in.
202******************************************************************************/
203
204void
205read_file(FILE *inFile)
206{
207  int i,j,k;
208  PlyFile *ply;
209  int nprops;
210  int num_elems;
211  PlyProperty **plist;
212  char *elem_name;
213  float version;
214
215
216  /*** Read in the original PLY object ***/
217
218
219  ply  = ply_read (inFile, &nelems, &elist);
220  ply_get_info (ply, &version, &file_type);
221
222  for (i = 0; i < nelems; i++) {
223
224    /* get the description of the first element */
225    elem_name = elist[i];
226    plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
227
228    if (equal_strings ("vertex", elem_name)) {
229
230      /* see if vertex holds any normal information */
231      has_nx = has_ny = has_nz = 0;
232      for (j = 0; j < nprops; j++) {
233        if (equal_strings ("nx", plist[j]->name)) has_nx = 1;
234        if (equal_strings ("ny", plist[j]->name)) has_ny = 1;
235        if (equal_strings ("nz", plist[j]->name)) has_nz = 1;
236      }
237
238      /* create a vertex list to hold all the vertices */
239      vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems);
240      nverts = num_elems;
241
242      /* set up for getting vertex elements */
243
244      ply_get_property (ply, elem_name, &vert_props[0]);
245      ply_get_property (ply, elem_name, &vert_props[1]);
246      ply_get_property (ply, elem_name, &vert_props[2]);
247      if (has_nx) ply_get_property (ply, elem_name, &vert_props[3]);
248      if (has_ny) ply_get_property (ply, elem_name, &vert_props[4]);
249      if (has_nz) ply_get_property (ply, elem_name, &vert_props[5]);
250      vert_other = ply_get_other_properties (ply, elem_name,
251                     offsetof(Vertex,other_props));
252
253      /* grab all the vertex elements */
254      for (j = 0; j < num_elems; j++) {
255        vlist[j] = (Vertex *) malloc (sizeof (Vertex));
256        ply_get_element (ply, (void *) vlist[j]);
257      }
258    }
259    else if (equal_strings ("face", elem_name)) {
260
261      /* create a list to hold all the face elements */
262      flist = (Face **) malloc (sizeof (Face *) * num_elems);
263      nfaces = num_elems;
264
265      /* set up for getting face elements */
266
267      ply_get_property (ply, elem_name, &face_props[0]);
268      face_other = ply_get_other_properties (ply, elem_name,
269                     offsetof(Face,other_props));
270
271      /* grab all the face elements */
272      for (j = 0; j < num_elems; j++) {
273        flist[j] = (Face *) malloc (sizeof (Face));
274        ply_get_element (ply, (void *) flist[j]);
275      }
276    }
277    else
278      other_elements = ply_get_other_element (ply, elem_name, num_elems);
279  }
280
281  comments = ply_get_comments (ply, &num_comments);
282  obj_info = ply_get_obj_info (ply, &num_obj_info);
283
284  ply_close (ply);
285}
286
287
288/******************************************************************************
289Write out the PLY file to standard out.
290******************************************************************************/
291
292void
293write_file()
294{
295  int i,j,k;
296  PlyFile *ply;
297  int num_elems;
298  char *elem_name;
299
300  /*** Write out the final PLY object ***/
301
302
303  ply = ply_write (stdout, nelems, elist, file_type);
304
305
306  /* describe what properties go into the vertex and face elements */
307
308  ply_element_count (ply, "vertex", nverts);
309  ply_describe_property (ply, "vertex", &vert_props[0]);
310  ply_describe_property (ply, "vertex", &vert_props[1]);
311  ply_describe_property (ply, "vertex", &vert_props[2]);
312  if (has_nx) ply_describe_property (ply, "vertex", &vert_props[3]);
313  if (has_ny) ply_describe_property (ply, "vertex", &vert_props[4]);
314  if (has_nz) ply_describe_property (ply, "vertex", &vert_props[5]);
315  ply_describe_other_properties (ply, vert_other, offsetof(Vertex,other_props));
316
317  ply_element_count (ply, "face", nfaces);
318  ply_describe_property (ply, "face", &face_props[0]);
319  ply_describe_other_properties (ply, face_other, offsetof(Face,other_props));
320
321  ply_describe_other_elements (ply, other_elements);
322
323  for (i = 0; i < num_comments; i++)
324    ply_put_comment (ply, comments[i]);
325
326  for (i = 0; i < num_obj_info; i++)
327    ply_put_obj_info (ply, obj_info[i]);
328
329  ply_header_complete (ply);
330
331  /* set up and write the vertex elements */
332  ply_put_element_setup (ply, "vertex");
333  for (i = 0; i < nverts; i++)
334    ply_put_element (ply, (void *) vlist[i]);
335
336  /* set up and write the face elements */
337  ply_put_element_setup (ply, "face");
338  for (i = 0; i < nfaces; i++)
339    ply_put_element (ply, (void *) flist[i]);
340
341  ply_put_other_elements (ply);
342
343  /* close the PLY file */
344  ply_close (ply);
345}
346
Note: See TracBrowser for help on using the repository browser.