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