1 | /* |
---|
2 | |
---|
3 | Skeleton of program that manipulates a PLY object. (Was plyxform.) |
---|
4 | |
---|
5 | Greg Turk, August 1994 |
---|
6 | |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | #include <math.h> |
---|
11 | #include <string.h> |
---|
12 | #include <ply.h> |
---|
13 | #include <limits.h> |
---|
14 | #include <stdlib.h> |
---|
15 | |
---|
16 | void write_file(char *filename); |
---|
17 | void read_file(char *filename); |
---|
18 | void usage(char *progname); |
---|
19 | |
---|
20 | // Must have at least one element to describe or this blows up!!!! |
---|
21 | // ply bug!!! |
---|
22 | |
---|
23 | /* user's vertex definitions for a polygonal object */ |
---|
24 | |
---|
25 | typedef struct Vertex { |
---|
26 | float x,y,z; |
---|
27 | void *other_props; /* other properties */ |
---|
28 | } Vertex; |
---|
29 | |
---|
30 | char *elem_names[] = { /* list of the kinds of elements in the user's object */ |
---|
31 | "vertex" |
---|
32 | }; |
---|
33 | |
---|
34 | PlyProperty 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 | }; |
---|
39 | |
---|
40 | |
---|
41 | /*** the PLY object ***/ |
---|
42 | |
---|
43 | static int nverts; |
---|
44 | static Vertex **vlist; |
---|
45 | static PlyOtherElems *other_elements = NULL; |
---|
46 | static PlyOtherProp *vert_other; |
---|
47 | static int nelems; |
---|
48 | static char **elist; |
---|
49 | static int num_comments; |
---|
50 | static char **comments; |
---|
51 | static int num_obj_info; |
---|
52 | static char **obj_info; |
---|
53 | static int file_type; |
---|
54 | static char **new_info; |
---|
55 | static char num_new_info; |
---|
56 | |
---|
57 | |
---|
58 | /****************************************************************************** |
---|
59 | Main program. |
---|
60 | ******************************************************************************/ |
---|
61 | |
---|
62 | main(int argc, char *argv[]) |
---|
63 | { |
---|
64 | int i; |
---|
65 | char *progname; |
---|
66 | char *filename; |
---|
67 | |
---|
68 | progname = argv[0]; |
---|
69 | |
---|
70 | if (argc < 3) { |
---|
71 | usage(progname); |
---|
72 | exit(1); |
---|
73 | } |
---|
74 | |
---|
75 | /* Check arg1 is filename, not flag */ |
---|
76 | if (argv[1][0] == '-') { |
---|
77 | if (strlen(argv[1]) == 1) { |
---|
78 | /* Ok. The "-" arg means use stdin/stdout */ |
---|
79 | } else { |
---|
80 | fprintf(stderr, "Error: Unhandled arg: %s\n", argv[1]); |
---|
81 | usage(progname); |
---|
82 | exit(-1); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | filename = argv[1]; |
---|
87 | |
---|
88 | num_new_info = 0; |
---|
89 | new_info = (char **)malloc((argc - 2)*sizeof(char *)); |
---|
90 | for (i = 2; i < argc; i++) { |
---|
91 | new_info[num_new_info] = (char *)malloc(PATH_MAX*sizeof(char)); |
---|
92 | new_info[num_new_info][0] = 0; |
---|
93 | strcat(new_info[num_new_info], argv[i]); |
---|
94 | num_new_info++; |
---|
95 | } |
---|
96 | |
---|
97 | read_file(filename); |
---|
98 | write_file(filename); |
---|
99 | |
---|
100 | return 0; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /****************************************************************************** |
---|
105 | Print out usage information. |
---|
106 | ******************************************************************************/ |
---|
107 | void |
---|
108 | usage(char *progname) |
---|
109 | { |
---|
110 | fprintf (stderr, "Usage: %s file.ply 'info_1' ... 'info_n'\n", progname); |
---|
111 | fprintf (stderr, " or: %s - 'info_1' ... 'info_n' < in.ply > out.ply\n", |
---|
112 | progname); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | /****************************************************************************** |
---|
117 | Read in the PLY file from standard in. |
---|
118 | ******************************************************************************/ |
---|
119 | void |
---|
120 | read_file(char *filename) |
---|
121 | { |
---|
122 | int i; |
---|
123 | PlyFile *ply; |
---|
124 | int nprops; |
---|
125 | int num_elems; |
---|
126 | char *elem_name; |
---|
127 | float version; |
---|
128 | |
---|
129 | |
---|
130 | /*** Read in the original PLY object ***/ |
---|
131 | if (!strcmp(filename, "-")) { |
---|
132 | ply = ply_read (stdin, &nelems, &elist); |
---|
133 | file_type = ply->file_type; |
---|
134 | } else { |
---|
135 | ply = |
---|
136 | ply_open_for_reading(filename, &nelems, &elist, &file_type, &version); |
---|
137 | } |
---|
138 | |
---|
139 | if (ply == NULL) { |
---|
140 | fprintf(stderr, "Could not open file %s for reading.\n", filename); |
---|
141 | exit(1); |
---|
142 | } |
---|
143 | |
---|
144 | for (i = 0; i < nelems; i++) { |
---|
145 | |
---|
146 | /* get the description of the first element */ |
---|
147 | elem_name = elist[i]; |
---|
148 | ply_get_element_description (ply, elem_name, &num_elems, &nprops); |
---|
149 | |
---|
150 | if (equal_strings ("vertex", elem_name)) { |
---|
151 | |
---|
152 | /* create a vertex list to hold all the vertices */ |
---|
153 | vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems); |
---|
154 | nverts = num_elems; |
---|
155 | |
---|
156 | /* set up for getting vertex elements */ |
---|
157 | |
---|
158 | ply_get_property (ply, elem_name, &vert_props[0]); |
---|
159 | ply_get_property (ply, elem_name, &vert_props[1]); |
---|
160 | ply_get_property (ply, elem_name, &vert_props[2]); |
---|
161 | vert_other = ply_get_other_properties (ply, elem_name, |
---|
162 | offsetof(Vertex,other_props)); |
---|
163 | |
---|
164 | /* grab all the vertex elements */ |
---|
165 | for (int j = 0; j < num_elems; j++) { |
---|
166 | vlist[j] = (Vertex *) malloc (sizeof (Vertex)); |
---|
167 | ply_get_element (ply, (void *) vlist[j]); |
---|
168 | } |
---|
169 | } |
---|
170 | else |
---|
171 | other_elements = ply_get_other_element (ply, elem_name, num_elems); |
---|
172 | } |
---|
173 | |
---|
174 | comments = ply_get_comments (ply, &num_comments); |
---|
175 | obj_info = ply_get_obj_info (ply, &num_obj_info); |
---|
176 | |
---|
177 | ply_close (ply); |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | /****************************************************************************** |
---|
182 | Write out the PLY file to standard out. |
---|
183 | ******************************************************************************/ |
---|
184 | void |
---|
185 | write_file(char *filename) |
---|
186 | { |
---|
187 | int i; |
---|
188 | PlyFile *ply; |
---|
189 | float version; |
---|
190 | |
---|
191 | /*** Write out the final PLY object ***/ |
---|
192 | |
---|
193 | if (!strcmp(filename, "-")) { |
---|
194 | ply = ply_write(stdout, nelems, elist, file_type); |
---|
195 | } else { |
---|
196 | ply = ply_open_for_writing(filename, nelems, elist, file_type, &version); |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | if (ply == NULL) { |
---|
201 | printf("Could not open file %s for writing.\n", filename); |
---|
202 | exit(1); |
---|
203 | } |
---|
204 | |
---|
205 | /* describe what properties go into the vertex elements */ |
---|
206 | |
---|
207 | ply_element_count (ply, "vertex", nverts); |
---|
208 | ply_describe_property (ply, "vertex", &vert_props[0]); |
---|
209 | ply_describe_property (ply, "vertex", &vert_props[1]); |
---|
210 | ply_describe_property (ply, "vertex", &vert_props[2]); |
---|
211 | ply_describe_other_properties (ply, vert_other, offsetof(Vertex,other_props)); |
---|
212 | |
---|
213 | ply_describe_other_elements (ply, other_elements); |
---|
214 | |
---|
215 | for (i = 0; i < num_comments; i++) |
---|
216 | ply_put_comment (ply, comments[i]); |
---|
217 | |
---|
218 | for (i = 0; i < num_obj_info; i++) |
---|
219 | ply_put_obj_info (ply, obj_info[i]); |
---|
220 | |
---|
221 | for (i = 0; i < num_new_info; i++) |
---|
222 | ply_put_obj_info (ply, new_info[i]); |
---|
223 | |
---|
224 | ply_header_complete (ply); |
---|
225 | |
---|
226 | /* set up and write the vertex elements */ |
---|
227 | ply_put_element_setup (ply, "vertex"); |
---|
228 | for (i = 0; i < nverts; i++) |
---|
229 | ply_put_element (ply, (void *) vlist[i]); |
---|
230 | |
---|
231 | ply_put_other_elements (ply); |
---|
232 | |
---|
233 | /* close the PLY file */ |
---|
234 | ply_close (ply); |
---|
235 | } |
---|
236 | |
---|