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

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

Added original make3d

File size: 8.7 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 map all confidences above a\n");
144  fprintf (stderr, "   cutoff threshold to 1.0 and those below to 0.0.\n");
145  fprintf (stderr, "\n");
146  exit(-1);
147}
148
149
150
151/******************************************************************************
152Read in the PLY file from standard in.
153******************************************************************************/
154
155void
156read_file(FILE *inFile)
157{
158  int i,j,k;
159  PlyFile *ply;
160  int nprops;
161  int num_elems;
162  PlyProperty **plist;
163  char *elem_name;
164  float version;
165
166
167  /*** Read in the original PLY object ***/
168
169
170  ply  = ply_read (inFile, &nelems, &elist);
171  ply_get_info (ply, &version, &file_type);
172
173  for (i = 0; i < nelems; i++) {
174
175    /* get the description of the first element */
176    elem_name = elist[i];
177    plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
178
179    if (equal_strings ("vertex", elem_name)) {
180
181      /* create a vertex list to hold all the vertices */
182      vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems);
183      nverts = num_elems;
184
185      /* set up for getting vertex elements */
186
187      ply_get_property (ply, elem_name, &vert_props[0]);
188      ply_get_property (ply, elem_name, &vert_props[1]);
189      ply_get_property (ply, elem_name, &vert_props[2]);
190      ply_get_property (ply, elem_name, &vert_props[3]);
191      vert_other = ply_get_other_properties (ply, elem_name,
192                     offsetof(Vertex,other_props));
193
194      /* grab all the vertex elements */
195      for (j = 0; j < num_elems; j++) {
196        vlist[j] = (Vertex *) malloc (sizeof (Vertex));
197        ply_get_element (ply, (void *) vlist[j]);
198      }
199    }
200    else if (equal_strings ("face", elem_name)) {
201
202      /* create a list to hold all the face elements */
203      flist = (Face **) malloc (sizeof (Face *) * num_elems);
204      nfaces = num_elems;
205
206      /* set up for getting face elements */
207
208      ply_get_property (ply, elem_name, &face_props[0]);
209      face_other = ply_get_other_properties (ply, elem_name,
210                     offsetof(Face,other_props));
211
212      /* grab all the face elements */
213      for (j = 0; j < num_elems; j++) {
214        flist[j] = (Face *) malloc (sizeof (Face));
215        ply_get_element (ply, (void *) flist[j]);
216        /* DEBUG
217        fprintf(stderr, "face %d: %d verts: %d %d %d\n",
218                j, flist[j]->nverts,
219                flist[j]->verts[0],
220                flist[j]->verts[1],
221                flist[j]->verts[2]);
222        */
223      }
224    }
225    else
226      other_elements = ply_get_other_element (ply, elem_name, num_elems);
227  }
228
229  comments = ply_get_comments (ply, &num_comments);
230  obj_info = ply_get_obj_info (ply, &num_obj_info);
231
232  ply_close (ply);
233}
234
235
236/******************************************************************************
237Write out the PLY file to standard out.
238Ignore all the points (and corresponding faces) that have confidence
239below the specified threshold....
240******************************************************************************/
241
242void
243write_file()
244{
245  int i,j,k;
246  PlyFile *ply;
247  int num_elems;
248  char *elem_name;
249  int vert_count;
250  int face_count;
251
252  /*** Write out the final PLY object ***/
253
254
255  ply = ply_write (stdout, nelems, elist, file_type);
256
257  // count the vertices that have valid confidence
258  vert_count = 0;
259  for (i = 0; i < nverts; i++) {
260    // Set the index to either the index number, or -1...
261     vlist[i]->index = vert_count;
262     vert_count++;
263    if (vlist[i]->confidence > CONF_THRESH) {
264       vlist[i]->confidence = 1.0;
265    } else {
266       vlist[i]->confidence = 0.0;
267    }
268  }
269
270  // count the faces that are still valid
271  face_count = 0;
272  for (i = 0; i < nfaces; i++) {
273    bool valid = (flist[i]->nverts > 0);
274    for (j = 0; j < flist[i]->nverts; j++) {
275      if (vlist[flist[i]->verts[j]]->index == -1) {
276        valid = false;
277        break;
278      }
279    }
280   
281    // If face not valid, set nverts to 0, so it won't
282    // get written out later.
283    if (valid) {
284      face_count++;
285    } else {
286      flist[i]->nverts = 0;
287    }
288  }
289
290  /* describe what properties go into the vertex and face elements */
291
292  ply_element_count (ply, "vertex", vert_count);
293  ply_describe_property (ply, "vertex", &vert_props[0]);
294  ply_describe_property (ply, "vertex", &vert_props[1]);
295  ply_describe_property (ply, "vertex", &vert_props[2]);
296  ply_describe_other_properties (ply, vert_other, offsetof(Vertex,other_props));
297  ply_describe_property (ply, "vertex", &vert_props[3]);
298
299  ply_element_count (ply, "face", face_count);
300  ply_describe_property (ply, "face", &face_props[0]);
301  ply_describe_other_properties (ply, face_other, offsetof(Face,other_props));
302
303  ply_describe_other_elements (ply, other_elements);
304
305  for (i = 0; i < num_comments; i++)
306    ply_put_comment (ply, comments[i]);
307
308  for (i = 0; i < num_obj_info; i++)
309    ply_put_obj_info (ply, obj_info[i]);
310
311  ply_header_complete (ply);
312
313  /* set up and write the vertex elements */
314
315  ply_put_element_setup (ply, "vertex");
316
317  for (i = 0; i < nverts; i++)
318    if (vlist[i]->index > -1)
319      ply_put_element (ply, (void *) vlist[i]);
320
321  /* set up and write the face elements */
322  ply_put_element_setup (ply, "face");
323
324  for (i = 0; i < nfaces; i++) {
325    if (flist[i]->nverts == 0)
326      continue;
327    for (j = 0; j < flist[i]->nverts; j++)
328      flist[i]->verts[j] = (vlist[flist[i]->verts[j]])->index;
329    ply_put_element (ply, (void *) flist[i]);
330  }
331
332  ply_put_other_elements (ply);
333
334  /* close the PLY file */
335  ply_close (ply);
336}
Note: See TracBrowser for help on using the repository browser.