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

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

Added original make3d

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