0
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <string.h>
|
|
4 #include <unistd.h>
|
|
5 #include <libxml/parser.h>
|
|
6 #include "object.h"
|
|
7 #include "sys.h"
|
|
8 #include "xml.h"
|
|
9
|
|
10 #include "error.h"
|
|
11
|
|
12 #define ALIGN_16BYTE 16
|
|
13
|
|
14 extern int decode(char *cont, FILE *outfile);
|
|
15
|
|
16 static inline char *skip_to_number(char *cont)
|
|
17 {
|
|
18 if (cont == NULL) return(NULL);
|
|
19 for (;(*cont < '+' || *cont > '9') && (*cont != '\0');cont++) {}
|
|
20 if (*cont == '\0')
|
|
21 {
|
|
22 fprintf(stderr,"Content data is short\n");
|
|
23 return(NULL);
|
|
24 }
|
|
25 return(cont);
|
|
26 }
|
|
27
|
|
28
|
|
29 static inline char *pickup_exponent(int *exp, char *cont)
|
|
30 {
|
|
31 int n,value=0,shift=10;
|
|
32
|
|
33 if (*cont == ',' || *cont == '\n') return(NULL);
|
|
34
|
|
35 for (;*cont != ',' && *cont != '\n' && *cont != '\t';cont++)
|
|
36 {
|
|
37 if (*cont == '-')
|
|
38 {
|
|
39 shift = 0.1;
|
|
40 }
|
|
41 else if (*cont >= '0' && *cont <= '9')
|
|
42 {
|
|
43 value *= 10;
|
|
44 value += *cont - 48;
|
|
45 }
|
|
46 else if (*cont == '+' || *cont == ' ')
|
|
47 {
|
|
48 // ignore
|
|
49 }
|
|
50 else
|
|
51 {
|
|
52 fprintf(stderr,"Pick up exponent failed : %c(%d)\n",*cont,*cont);
|
|
53 return(NULL);
|
|
54 }
|
|
55 }
|
|
56
|
|
57 for (n=0;n < value;n++) shift *= shift;
|
|
58
|
|
59 if (value == 0)
|
|
60 {
|
|
61 *exp = 1;
|
|
62 }
|
|
63 else
|
|
64 {
|
|
65 *exp = shift;
|
|
66 }
|
|
67 return(cont-1);
|
|
68 }
|
|
69
|
|
70
|
|
71 static inline char *pickup_float(char *cont, float *index)
|
|
72 {
|
|
73 int sign=1,exp=1;
|
|
74 float shift=10,val_dec=0,val_int=0;
|
|
75
|
|
76 cont = skip_to_number(cont);
|
|
77 if (cont == NULL) return(NULL);
|
|
78
|
|
79 for (;*cont != ',' && *cont != '\n' && *cont != '\t';cont++)
|
|
80 {
|
|
81 if (*cont == '-')
|
|
82 {
|
|
83 sign = -1;
|
|
84 }
|
|
85 else if (*cont == '.')
|
|
86 {
|
|
87 shift = 0.1;
|
|
88 }
|
|
89 else if (*cont >= '0' && *cont <= '9')
|
|
90 {
|
|
91 if (shift == 10)
|
|
92 {
|
|
93 val_int *= shift;
|
|
94 val_int += *cont - 48;
|
|
95 }
|
|
96 else
|
|
97 {
|
|
98 val_dec += (*cont - 48) * shift;
|
|
99 shift *= 0.1;
|
|
100 }
|
|
101 }
|
|
102 else if (*cont == 'e' || *cont == 'E')
|
|
103 {
|
|
104 cont = pickup_exponent(&exp,cont+1);
|
|
105 if (cont == NULL) return(NULL);
|
|
106 }
|
|
107 else if (*cont == '+' || *cont == '/' || *cont == ' ')
|
|
108 {
|
|
109 // ignore
|
|
110 }
|
|
111 else
|
|
112 {
|
|
113 fprintf(stderr,"Pick up float failed : %c(%d)\n",*cont,*cont);
|
|
114 return(NULL);
|
|
115 }
|
|
116 }
|
|
117
|
|
118 *index = sign * (val_int + val_dec) * exp;
|
|
119 cont++;
|
|
120 return(cont);
|
|
121 }
|
|
122
|
|
123
|
|
124 static FVECTOR *get_surface_data(SURFACE *surf, xmlNodePtr cur)
|
|
125 {
|
|
126 int n;
|
|
127 char *cont;
|
|
128 float *index;
|
|
129 FVECTOR *data[4];
|
|
130 FILE *outfile = NULL;
|
|
131
|
|
132 /*
|
|
133 data[0]: point position
|
|
134 data[1]: point direction
|
|
135 data[2]: point QT position
|
|
136 data[3]: point color
|
|
137 */
|
|
138
|
|
139 data[0] = (FVECTOR *)malloc(sizeof(FVECTOR)*(surf->size)*4);
|
|
140 data[1] = data[0] + surf->size;
|
|
141 data[2] = data[1] + surf->size;
|
|
142 data[3] = data[2] + surf->size;
|
|
143
|
|
144 for (;cur;cur=cur->next)
|
|
145 {
|
|
146 if (!xmlStrcmp(cur->name,(xmlChar*)"coordinate"))
|
|
147 {
|
|
148 cont = (char *)xmlNodeGetContent(cur);
|
|
149 for (n=0,index=(float*)data[0];n < surf->size;n++,index+=4)
|
|
150 {
|
|
151 cont = pickup_float(cont,index);
|
|
152 cont = pickup_float(cont,index+1);
|
|
153 cont = pickup_float(cont,index+2);
|
|
154 index[3] = 1.0;
|
|
155
|
|
156 if (cont == NULL)
|
|
157 {
|
|
158 fprintf(stderr,"Analyzing surface data failed coordinate\n");
|
|
159 return(NULL);
|
|
160 }
|
|
161 }
|
|
162 }
|
|
163 else if (!xmlStrcmp(cur->name,(xmlChar*)"normal"))
|
|
164 {
|
|
165 cont = (char *)xmlNodeGetContent(cur);
|
|
166 for (n=0,index=(float*)data[1];n < surf->size;n++,index+=4)
|
|
167 {
|
|
168 cont = pickup_float(cont,index);
|
|
169 cont = pickup_float(cont,index+1);
|
|
170 cont = pickup_float(cont,index+2);
|
|
171 index[3] = 1.0;
|
|
172
|
|
173 if (cont == NULL)
|
|
174 {
|
|
175 fprintf(stderr,"Analyzing surface data failed normal\n");
|
|
176 return(NULL);
|
|
177 }
|
|
178 }
|
|
179 }
|
|
180 else if (!xmlStrcmp(cur->name,(xmlChar*)"model"))
|
|
181 {
|
|
182 cont = (char *)xmlNodeGetContent(cur);
|
|
183 //for (n=0,index=(float*)data[2]; n < surf->size;n++,index+=4)
|
|
184 for (n=0,index=(float*)data[2]; n < 1; n++,index+=4)
|
|
185 {
|
|
186 cont = pickup_float(cont,index);
|
|
187 cont = pickup_float(cont,index+1);
|
|
188 cont = pickup_float(cont,index+2);
|
|
189 //cont = pickup_float(cont,index+3);
|
|
190 index[3] = 1.0;
|
|
191
|
|
192 if (cont == NULL)
|
|
193 {
|
|
194 fprintf(stderr,"Analyzing surface data failed model\n");
|
|
195 return(NULL);
|
|
196 }
|
|
197 }
|
|
198 }
|
|
199 else if (!xmlStrcmp(cur->name,(xmlChar*)"texture"))
|
|
200 {
|
|
201 cont = (char *)xmlNodeGetContent(cur);
|
|
202 if (cont == NULL)
|
|
203 {
|
|
204 for (n=0,index=(float*)data[3];n < surf->size;n++,index+=4)
|
|
205 {
|
|
206 index[0] = 0.0;
|
|
207 index[1] = 0.0;
|
|
208 index[2] = 0.0;
|
|
209 index[3] = 0.0;
|
|
210 }
|
|
211 }
|
|
212 else
|
|
213 {
|
|
214 for (n=0,index=(float*)data[3]; n < surf->size; n++,index+=4)
|
|
215 {
|
|
216 cont = pickup_float(cont,index);
|
|
217 cont = pickup_float(cont,index+1);
|
|
218 index[2] = 1.0;
|
|
219 index[3] = 0.0;
|
|
220
|
|
221 if (cont == NULL)
|
|
222 {
|
|
223 fprintf(stderr,"Analyzing surface data failed last\n");
|
|
224 return(NULL);
|
|
225 }
|
|
226 }
|
|
227 }
|
|
228 }
|
|
229 else if(!xmlStrcmp(cur->name,(xmlChar*)"image"))
|
|
230 {
|
|
231 surf->image_name = (char *)xmlGetProp(cur,(xmlChar *)"name");
|
|
232 char *buf = (char*)malloc(strlen(surf->image_name)+10);
|
|
233 buf[0] = 0;
|
|
234 strcat(buf,"/tmp/");
|
|
235 surf->image_name = strcat(buf, surf->image_name);
|
|
236
|
|
237 outfile = fopen(surf->image_name,"wb");
|
|
238 if(NULL == outfile)
|
|
239 {
|
|
240 printf("error open file\n");
|
|
241 }
|
|
242
|
|
243 cont = (char *)xmlNodeGetContent(cur);
|
|
244 decode(cont, outfile);
|
|
245 fclose(outfile);
|
|
246 // unlink(surf->image_name);
|
|
247 }
|
|
248 }
|
|
249 return(data[0]);
|
|
250 }
|
|
251
|
|
252
|
|
253 static SURFACE *create_surface_data(xmlNodePtr cur)
|
|
254 {
|
|
255 char *name;
|
|
256 char *parent_name;
|
|
257 SURFACE *surface;
|
|
258
|
|
259
|
|
260 surface = (SURFACE *)malloc(sizeof(SURFACE));
|
|
261 if (surface == NULL)
|
|
262 {
|
|
263 fprintf(stderr,"malloc failed at create_surface_data");
|
|
264 return(NULL);
|
|
265 }
|
|
266 surface->size = atoi((char *)xmlGetProp(cur,(xmlChar *)"size"));
|
|
267 name = (char *)xmlGetProp(cur,(xmlChar *)"name");
|
|
268 parent_name = (char *)xmlGetProp(cur,(xmlChar *)"parent");
|
|
269 surface->name = name;
|
|
270 surface->parent_name = parent_name;
|
|
271 surface->next = NULL;
|
|
272
|
|
273 surface->data[0] = get_surface_data(surface,cur->children);
|
|
274 surface->data[1] = get_surface_data(surface,cur->children)+surface->size;
|
|
275 surface->data[2] = get_surface_data(surface,cur->children)+surface->size*2;
|
|
276 surface->data[3] = get_surface_data(surface,cur->children)+surface->size*3;
|
|
277
|
|
278
|
|
279 return(surface);
|
|
280 }
|
|
281
|
|
282
|
|
283
|
|
284 static SURFACE *get_node_point(SURFACE *surfaces, char *name)
|
|
285 {
|
|
286 SURFACE *list = surfaces;
|
|
287 SURFACE *node = NULL;
|
|
288 while(list != NULL)
|
|
289 {
|
|
290 if(!xmlStrcmp((xmlChar *)list->name,(xmlChar *)name))
|
|
291 {
|
|
292 node = list;
|
|
293 }
|
|
294 list = list->next;
|
|
295 }
|
|
296 return node;
|
|
297 }
|
|
298
|
|
299
|
|
300 static SURFACE *create_tree(SURFACE *surfaces)
|
|
301 {
|
|
302 SURFACE *linear_list;
|
|
303 SURFACE *list_top = NULL;
|
|
304 SURFACE *parent;
|
|
305
|
|
306 linear_list = surfaces;
|
|
307
|
|
308 while(linear_list != NULL)
|
|
309 {
|
|
310 if(xmlStrcmp((xmlChar *)linear_list->parent_name, (xmlChar *)"NULL"))
|
|
311 {
|
|
312 parent = get_node_point(surfaces,linear_list->parent_name);
|
|
313 if(parent->child == NULL)
|
|
314 {
|
|
315 parent->child = linear_list;
|
|
316 }
|
|
317 else
|
|
318 {
|
|
319 parent->child->brother = linear_list;
|
|
320 }
|
|
321 linear_list->parent = parent;
|
|
322 __debug("name = %s, parent name = %s\n",linear_list->name, linear_list->parent_name);
|
|
323 __debug(" parent = %s\n",linear_list->parent->name);
|
|
324 __debug(" parent->child = %s\n",linear_list->parent->child->name);
|
|
325
|
|
326 linear_list->child = NULL;
|
|
327 linear_list->brother = NULL;
|
|
328 linear_list = linear_list->next;
|
|
329 }
|
|
330 else
|
|
331 {
|
|
332 list_top = linear_list;
|
|
333 list_top->parent = NULL;
|
|
334 list_top->brother = NULL;
|
|
335 list_top->child = NULL;
|
|
336 linear_list = linear_list->next;
|
|
337 //printf("top name = %s\n",list_top->name);
|
|
338 }
|
|
339 }
|
|
340
|
|
341 return list_top;
|
|
342 }
|
|
343
|
|
344
|
|
345 static OBJECT *create_object(xmlDocPtr doc)
|
|
346 {
|
|
347 char *align,*head;
|
|
348 OBJECT *obj;
|
|
349 SURFACE *tmp,**joint;
|
|
350 xmlNodePtr cur;
|
|
351
|
|
352 cur = xmlDocGetRootElement(doc);
|
|
353 if (cur == NULL)
|
|
354 {
|
|
355 fprintf(stderr,"XML file is empty\n");
|
|
356 return(NULL);
|
|
357 }
|
|
358
|
|
359 if (xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D"))
|
|
360 {
|
|
361 fprintf(stderr,"This data format isn't OBJECT-3D\n");
|
|
362 return(NULL);
|
|
363 }
|
|
364
|
|
365 if (malloc_align16(&head,&align,sizeof(OBJECT)) < 0)
|
|
366 {
|
|
367 fprintf(stderr,"malloc failed at create_object : OBJECT\n");
|
|
368 return(NULL);
|
|
369 }
|
|
370
|
|
371 obj = (OBJECT *)align;
|
|
372 obj->free_me = head;
|
|
373 init_object(obj);
|
|
374
|
|
375 obj->surfaces = NULL;
|
|
376 joint = &(obj->surfaces);
|
|
377
|
|
378 for (cur=cur->children; cur; cur=cur->next)
|
|
379 {
|
|
380 if (!xmlStrcmp(cur->name,(xmlChar*)"surface"))
|
|
381 {
|
|
382 tmp = create_surface_data(cur);
|
|
383 if (tmp == NULL)
|
|
384 {
|
|
385 printf("temp is NULL\n");
|
|
386 free_object(obj);
|
|
387 return(NULL);
|
|
388 }
|
|
389 *joint = tmp;
|
|
390 joint = &(tmp->next);
|
|
391 }
|
|
392 }
|
|
393
|
|
394 obj->surfaces = create_tree(obj->surfaces);
|
|
395
|
|
396 return(obj);
|
|
397 }
|
|
398
|
|
399
|
|
400 //static int read_xml_3d_file(const char *file_name)
|
|
401 OBJECT *read_xml_3d_file(const char *file_name)
|
|
402 {
|
|
403 xmlDocPtr doc;
|
|
404 OBJECT *ptr;
|
|
405
|
|
406 doc = xmlParseFile(file_name);
|
|
407 if(doc == NULL)
|
|
408 {
|
|
409 printf("It seems that %s is not a xml data.\n", file_name);
|
|
410 }
|
|
411
|
|
412 ptr = create_object(doc);
|
|
413 if(ptr == NULL)
|
|
414 {
|
|
415 printf("XML create_object failed : %s\n",file_name);
|
|
416 }
|
|
417
|
|
418 xmlFreeDoc(doc);
|
|
419 return(ptr);
|
|
420 }
|