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

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

Added original make3d

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