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

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

Added original make3d

File size: 4.6 KB
Line 
1/*
2
3*/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
8#include <malloc.h>
9#include <ply.h>
10
11
12/* user's vertex and face definitions for a polygonal object */
13
14typedef struct Vertex {
15  float x,y,z;
16   char *other_props;
17} Vertex;
18
19char *elem_names[] = { /* list of the kinds of elements in the user's object */
20  "vertex"
21};
22
23PlyProperty vert_props[] = { /* list of property information for a vertex */
24  {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0},
25  {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0},
26  {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0},
27};
28
29
30/*** the PLY object ***/
31
32int nverts;
33Vertex **vlist;
34PlyOtherElems *other_elements = NULL;
35PlyOtherProp *vert_other;
36int nelems;
37char **elist;
38int num_comments;
39char **comments;
40int num_obj_info;
41char **obj_info;
42int file_type;
43
44static float scale;
45
46void usage(char *progname);
47void write_file();
48void read_file(FILE *inFile);
49
50
51/******************************************************************************
52Transform a PLY file.
53******************************************************************************/
54
55int
56main(int argc, char *argv[])
57{
58  int i,j;
59  char *s;
60  char *progname;
61
62  char *inName = NULL;
63  FILE *inFile = NULL;
64
65  progname = argv[0];
66
67  scale = 1;
68
69  /* Read all the -args */
70  while (--argc > 0 && (*++argv)[0]=='-') {
71    for (s = argv[0]+1; *s; s++)
72      switch (*s) {
73        case 's':
74          scale = atof(*++argv);
75          argc -= 1;
76          break;
77        default:
78          usage (progname);
79          exit (-1);
80          break;
81      }
82  }
83
84  /* Get the input file name */
85  if (argc == 0) {
86    /* No input name -- read from stdin */
87    inFile = stdin;
88  } else if (argc == 1 && argv[0][0] != '-') {
89    /* Open input file */
90    inName = argv[0];
91    inFile = fopen(inName, "r");
92    if (inFile == NULL) {
93      fprintf(stderr, "Error, could not open input ply file: %s\n", inName);
94      usage(progname);
95      exit(-1);
96    }
97  } else {
98    fprintf(stderr, "Unhandled argument: %s\n", argv[0]);
99    usage(progname);
100    exit(-1);
101  }
102
103  read_file(inFile);
104
105  write_file();
106}
107
108
109/******************************************************************************
110Print out usage information.
111******************************************************************************/
112
113void
114usage(char *progname)
115{
116  fprintf (stderr, "Usage: %s [flags] [in.ply] > out\n", progname);
117  fprintf (stderr, "   or: %s [flags] < in.ply > out\n", progname);
118  fprintf (stderr, "Flags:\n");
119  fprintf (stderr, "       -s scale\n");
120  exit(-1);
121}
122
123
124/******************************************************************************
125Transform the PLY object.
126******************************************************************************/
127
128/******************************************************************************
129Read in the PLY file from standard in.
130******************************************************************************/
131
132void
133read_file(FILE *inFile)
134{
135  int i,j,k;
136  PlyFile *ply;
137  int nprops;
138  int num_elems;
139  PlyProperty **plist;
140  char *elem_name;
141  float version;
142
143
144  /*** Read in the original PLY object ***/
145
146
147  ply  = ply_read (inFile, &nelems, &elist);
148  ply_get_info (ply, &version, &file_type);
149
150  for (i = 0; i < nelems; i++) {
151
152    /* get the description of the first element */
153    elem_name = elist[i];
154    plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
155
156    if (equal_strings ("vertex", elem_name)) {
157
158      /* create a vertex list to hold all the vertices */
159      vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems);
160      nverts = num_elems;
161
162      /* set up for getting vertex elements */
163
164      ply_get_property (ply, elem_name, &vert_props[0]);
165      ply_get_property (ply, elem_name, &vert_props[1]);
166      ply_get_property (ply, elem_name, &vert_props[2]);
167      vert_other = ply_get_other_properties (ply, elem_name,
168                     offsetof(Vertex,other_props));
169
170      /* grab all the vertex elements */
171      for (j = 0; j < num_elems; j++) {
172        vlist[j] = (Vertex *) malloc (sizeof (Vertex));
173        ply_get_element (ply, (void *) vlist[j]);
174      }
175    }
176  }
177
178  comments = ply_get_comments (ply, &num_comments);
179  obj_info = ply_get_obj_info (ply, &num_obj_info);
180
181  ply_close (ply);
182}
183
184
185/******************************************************************************
186Write out the PLY file to standard out.
187******************************************************************************/
188
189void
190write_file()
191{
192  int i,j,k;
193
194  for (i = 0; i < nverts; i++)
195     fprintf(stdout, "%d %d %d\n", (int)(scale*vlist[i]->x), 
196             (int)(scale*vlist[i]->y), (int)(scale*vlist[i]->z));
197}
198
Note: See TracBrowser for help on using the repository browser.