1 | /* |
---|
2 | |
---|
3 | Remove properties or elements from a PLY file. |
---|
4 | |
---|
5 | Greg Turk, August 1994 |
---|
6 | |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdlib.h> |
---|
10 | #include <stdio.h> |
---|
11 | #include <math.h> |
---|
12 | #include <malloc.h> |
---|
13 | #include <strings.h> |
---|
14 | #include <ply.h> |
---|
15 | |
---|
16 | #ifdef linux |
---|
17 | #include <string.h> |
---|
18 | #endif |
---|
19 | |
---|
20 | typedef struct OtherStruct { |
---|
21 | void *other_props; |
---|
22 | } OtherStruct; |
---|
23 | |
---|
24 | /*** the PLY object ***/ |
---|
25 | |
---|
26 | PlyOtherElems *other_elements = NULL; |
---|
27 | int nelems; |
---|
28 | char **elist; |
---|
29 | int file_type; |
---|
30 | int num_comments; |
---|
31 | char **comments; |
---|
32 | int num_obj_info; |
---|
33 | char **obj_info; |
---|
34 | PlyOtherProp **other_props; |
---|
35 | OtherStruct ***olist; |
---|
36 | int *elem_counts; |
---|
37 | |
---|
38 | |
---|
39 | /* list of properties to remove */ |
---|
40 | |
---|
41 | #define MAX 50 |
---|
42 | static char *prop_list[MAX]; |
---|
43 | static char *prop_elem_list[MAX]; |
---|
44 | static int prop_count = 0; |
---|
45 | |
---|
46 | /* list of elements to remove */ |
---|
47 | |
---|
48 | static char *elem_list[MAX]; |
---|
49 | static int elem_count = 0; |
---|
50 | |
---|
51 | void usage(char *progname); |
---|
52 | void write_file(); |
---|
53 | void read_file(FILE *); |
---|
54 | void remove_element(char *elem); |
---|
55 | void remove_property(PlyOtherProp *other, int index); |
---|
56 | FILE *parse_command_line(int argc, char *argv[]); |
---|
57 | void remove_stuff(); |
---|
58 | |
---|
59 | |
---|
60 | /****************************************************************************** |
---|
61 | Read in, process, and write out a PLY file. |
---|
62 | ******************************************************************************/ |
---|
63 | |
---|
64 | int |
---|
65 | main(int argc, char *argv[]) |
---|
66 | { |
---|
67 | int i,j; |
---|
68 | FILE *inFile; |
---|
69 | |
---|
70 | inFile = parse_command_line (argc, argv); |
---|
71 | |
---|
72 | read_file(inFile); |
---|
73 | |
---|
74 | remove_stuff(); |
---|
75 | |
---|
76 | write_file(); |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | /****************************************************************************** |
---|
81 | Remove an element from the list of elements in a PLY object. |
---|
82 | ******************************************************************************/ |
---|
83 | |
---|
84 | void |
---|
85 | remove_element(char *elem) |
---|
86 | { |
---|
87 | int i,j; |
---|
88 | |
---|
89 | for (i = 0; i < nelems; i++) |
---|
90 | if (equal_strings (elem, elist[i])) { |
---|
91 | for (j = i; j < nelems+1; j++) { |
---|
92 | elist[j] = elist[j+1]; |
---|
93 | olist[j] = olist[j+1]; |
---|
94 | other_props[j] = other_props[j+1]; |
---|
95 | elem_counts[j] = elem_counts[j+1]; |
---|
96 | } |
---|
97 | nelems--; |
---|
98 | return; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /****************************************************************************** |
---|
104 | Remove a property from an other property description. |
---|
105 | ******************************************************************************/ |
---|
106 | |
---|
107 | void |
---|
108 | remove_property(PlyOtherProp *other, int index) |
---|
109 | { |
---|
110 | int i; |
---|
111 | |
---|
112 | for (i = index; i < other->nprops-1; i++) |
---|
113 | copy_property (other->props[i], other->props[i+1]); |
---|
114 | |
---|
115 | other->nprops--; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | /****************************************************************************** |
---|
120 | Remove references to specified elements and properties. |
---|
121 | ******************************************************************************/ |
---|
122 | |
---|
123 | void |
---|
124 | remove_stuff() |
---|
125 | { |
---|
126 | int i,j,k; |
---|
127 | PlyOtherProp *other; |
---|
128 | int found_element; |
---|
129 | |
---|
130 | /* find and remove each element that is on the list for removal */ |
---|
131 | |
---|
132 | for (j = 0; j < elem_count; j++) { |
---|
133 | |
---|
134 | found_element = 0; |
---|
135 | |
---|
136 | for (i = 0; i < nelems; i++) |
---|
137 | if (equal_strings (elist[i], elem_list[j])) { |
---|
138 | /* if so, remove it */ |
---|
139 | remove_element (elem_list[j]); |
---|
140 | found_element = 1; |
---|
141 | break; |
---|
142 | } |
---|
143 | |
---|
144 | if (!found_element) { |
---|
145 | fprintf (stderr, "Can't find element '%s'\n", elem_list[j]); |
---|
146 | exit (-1); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | /* remove each specified property */ |
---|
151 | |
---|
152 | for (i = 0; i < prop_count; i++) { |
---|
153 | |
---|
154 | found_element = 0; |
---|
155 | |
---|
156 | for (j = 0; j < nelems; j++) { |
---|
157 | |
---|
158 | if (equal_strings (elist[j], prop_elem_list[i])) { |
---|
159 | |
---|
160 | found_element = 1; |
---|
161 | other = other_props[j]; |
---|
162 | |
---|
163 | for (k = 0; k < other->nprops; k++) { |
---|
164 | /* remove property by writing over it */ |
---|
165 | if (equal_strings (prop_list[i], other->props[k]->name)) { |
---|
166 | remove_property (other, k); |
---|
167 | if (other->nprops == 0) |
---|
168 | remove_element (prop_elem_list[i]); |
---|
169 | goto here; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | /* if we get here, we didn't find property */ |
---|
174 | fprintf (stderr, "Can't find property '%s.%s'\n", |
---|
175 | prop_elem_list[i], prop_list[i]); |
---|
176 | exit (-1); |
---|
177 | } |
---|
178 | here: ; /* for jumping out of loop */ |
---|
179 | } |
---|
180 | |
---|
181 | if (!found_element) { |
---|
182 | fprintf (stderr, "Can't find element '%s'\n", prop_elem_list[i]); |
---|
183 | exit (-1); |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | /****************************************************************************** |
---|
190 | Find from command line which properties and which elements are to be |
---|
191 | removed. |
---|
192 | ******************************************************************************/ |
---|
193 | |
---|
194 | FILE * |
---|
195 | parse_command_line(int argc, char *argv[]) |
---|
196 | { |
---|
197 | int i,j; |
---|
198 | char *elem; |
---|
199 | char *prop; |
---|
200 | int got_prop; |
---|
201 | int len; |
---|
202 | FILE *inFile = stdin; |
---|
203 | |
---|
204 | /* Check usage */ |
---|
205 | if (argc < 3) { |
---|
206 | usage(argv[0]); |
---|
207 | } |
---|
208 | |
---|
209 | /* First argument: The input file (or - for stdin) */ |
---|
210 | if (argv[1][0] == '-') { |
---|
211 | if (strlen(argv[1]) == 1) { |
---|
212 | inFile = stdin; |
---|
213 | } else { |
---|
214 | fprintf(stderr, "Error: unhandled argument: %s\n", argv[1]); |
---|
215 | usage(argv[0]); |
---|
216 | exit(-1); |
---|
217 | } |
---|
218 | } else { |
---|
219 | inFile = fopen(argv[1], "r"); |
---|
220 | if (inFile == NULL) { |
---|
221 | fprintf(stderr, "Error: Couldn't open input file %s\n", argv[1]); |
---|
222 | usage(argv[0]); |
---|
223 | exit(-1); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | /* Examine each argument and see if it can be split up into */ |
---|
229 | /* a dot-separated pair of element.property. If not, the */ |
---|
230 | /* whole argument is taken as an element name */ |
---|
231 | |
---|
232 | for (i = 2; i < argc; i++) { |
---|
233 | |
---|
234 | elem = strdup (argv[i]); |
---|
235 | len = strlen (elem); |
---|
236 | got_prop = 0; |
---|
237 | |
---|
238 | /* look for dot */ |
---|
239 | for (j = 0; j < len; j++) |
---|
240 | if (elem[j] == '.') { |
---|
241 | elem[j] = '\0'; |
---|
242 | prop = &(elem[j+1]); |
---|
243 | got_prop = 1; |
---|
244 | break; |
---|
245 | } |
---|
246 | |
---|
247 | if (got_prop) { |
---|
248 | prop_list[prop_count] = prop; |
---|
249 | prop_elem_list[prop_count] = elem; |
---|
250 | prop_count++; |
---|
251 | } |
---|
252 | else { |
---|
253 | elem_list[elem_count] = elem; |
---|
254 | elem_count++; |
---|
255 | } |
---|
256 | } |
---|
257 | return(inFile); |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | /****************************************************************************** |
---|
262 | Read in the PLY file from standard in. |
---|
263 | ******************************************************************************/ |
---|
264 | |
---|
265 | void |
---|
266 | read_file(FILE *inFile) |
---|
267 | { |
---|
268 | int i,j,k; |
---|
269 | PlyFile *ply; |
---|
270 | int nprops; |
---|
271 | int num_elems; |
---|
272 | PlyProperty **plist; |
---|
273 | char *elem_name; |
---|
274 | float version; |
---|
275 | |
---|
276 | ply = ply_read (inFile, &nelems, &elist); |
---|
277 | ply_get_info (ply, &version, &file_type); |
---|
278 | |
---|
279 | other_props = (PlyOtherProp **) malloc (sizeof (PlyOtherProp *) * nelems); |
---|
280 | olist = (OtherStruct ***) malloc (sizeof (OtherStruct **) * nelems); |
---|
281 | elem_counts = (int *) malloc (sizeof (int) * nelems); |
---|
282 | |
---|
283 | for (i = 0; i < nelems; i++) { |
---|
284 | |
---|
285 | /* get the description of the element */ |
---|
286 | elem_name = elist[i]; |
---|
287 | plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); |
---|
288 | elem_counts[i] = num_elems; |
---|
289 | |
---|
290 | /* create a list of structures to hold all the elements */ |
---|
291 | olist[i] = (OtherStruct **) malloc (sizeof (OtherStruct *) * num_elems); |
---|
292 | |
---|
293 | /* set up for getting the elements */ |
---|
294 | |
---|
295 | other_props[i] = ply_get_other_properties (ply, elem_name, |
---|
296 | offsetof(OtherStruct,other_props)); |
---|
297 | |
---|
298 | /* grab all the individual elements */ |
---|
299 | for (j = 0; j < num_elems; j++) { |
---|
300 | olist[i][j] = (OtherStruct *) malloc (sizeof (OtherStruct)); |
---|
301 | ply_get_element (ply, (void *) olist[i][j]); |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | comments = ply_get_comments (ply, &num_comments); |
---|
306 | obj_info = ply_get_obj_info (ply, &num_obj_info); |
---|
307 | |
---|
308 | ply_close (ply); |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | /****************************************************************************** |
---|
313 | Write out the PLY file to standard out. |
---|
314 | ******************************************************************************/ |
---|
315 | |
---|
316 | void |
---|
317 | write_file() |
---|
318 | { |
---|
319 | int i,j,k; |
---|
320 | PlyFile *ply; |
---|
321 | int num_elems; |
---|
322 | char *elem_name; |
---|
323 | |
---|
324 | ply = ply_write (stdout, nelems, elist, file_type); |
---|
325 | |
---|
326 | /* describe what properties go into the vertex and face elements */ |
---|
327 | |
---|
328 | for (i = 0; i < nelems; i++) { |
---|
329 | ply_element_count (ply, elist[i], elem_counts[i]); |
---|
330 | ply_describe_other_properties (ply, other_props[i], |
---|
331 | offsetof(OtherStruct,other_props)); |
---|
332 | } |
---|
333 | |
---|
334 | for (i = 0; i < num_comments; i++) |
---|
335 | ply_put_comment (ply, comments[i]); |
---|
336 | |
---|
337 | for (i = 0; i < num_obj_info; i++) |
---|
338 | ply_put_obj_info (ply, obj_info[i]); |
---|
339 | |
---|
340 | ply_header_complete (ply); |
---|
341 | |
---|
342 | /* set up and write the elements */ |
---|
343 | |
---|
344 | for (i = 0; i < nelems; i++) { |
---|
345 | /* write out elements */ |
---|
346 | ply_put_element_setup (ply, elist[i]); |
---|
347 | for (j = 0; j < elem_counts[i]; j++) |
---|
348 | ply_put_element (ply, (void *) olist[i][j]); |
---|
349 | } |
---|
350 | |
---|
351 | |
---|
352 | /* close the PLY file */ |
---|
353 | ply_close (ply); |
---|
354 | } |
---|
355 | |
---|
356 | /****************************************************************************** |
---|
357 | Print out usage information. |
---|
358 | ******************************************************************************/ |
---|
359 | |
---|
360 | void |
---|
361 | usage(char *progname) |
---|
362 | { |
---|
363 | fprintf (stderr, "usage: %s [in.ply] [element1] ... [elementN] > out.ply\n", |
---|
364 | progname); |
---|
365 | fprintf (stderr, " or: %s - [element1] ... [elementN] < in.ply " |
---|
366 | "> out.ply\n", progname); |
---|
367 | fprintf (stderr, "\n"); |
---|
368 | fprintf (stderr, "This program removes elements from a ply file.\n"); |
---|
369 | fprintf (stderr, "Or you can remove a property from an element.\n"); |
---|
370 | fprintf (stderr, "For example: %s - face vertex.z < x.ply > y.ply\n", |
---|
371 | progname); |
---|
372 | fprintf (stderr, "will remove faces and z components of vertices.\n"); |
---|
373 | fprintf (stderr, "\n"); |
---|
374 | exit(-1); |
---|
375 | } |
---|
376 | |
---|