summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBogdan Devichev <b.devichev@samsung.com>2014-12-03 12:24:03 +0200
committerBogdan Devichev <b.devichev@samsung.com>2014-12-05 11:11:25 +0200
commit952027af93918b6e0bbb23796b7abb49556de12a (patch)
tree31367c1f34306b1bfed0f9cb459a7c98ede79abe
parent8b1d380ba5629545d6561a4d69af41bb52fb36c6 (diff)
Model_save_load. Step 6. Ply_loader and obj_loader work with Eina_File. (need fix of tests).
-rw-r--r--src/modules/evas/model_loaders/obj/evas_model_load_obj.c101
-rw-r--r--src/modules/evas/model_loaders/ply/evas_model_load_ply.c98
2 files changed, 136 insertions, 63 deletions
diff --git a/src/modules/evas/model_loaders/obj/evas_model_load_obj.c b/src/modules/evas/model_loaders/obj/evas_model_load_obj.c
index 36977464ba..8f2b78a80f 100644
--- a/src/modules/evas/model_loaders/obj/evas_model_load_obj.c
+++ b/src/modules/evas/model_loaders/obj/evas_model_load_obj.c
@@ -37,29 +37,59 @@ struct _OBJ_Counts
37 Eina_Bool existence_of_tex_point; 37 Eina_Bool existence_of_tex_point;
38}; 38};
39 39
40static inline char* 40typedef struct _OBJ_Loader
41_file_to_buf(const char *file, long *length)//prepare text file for reading
42{ 41{
43 FILE *file_for_print; 42 Eina_File *file;
44 char *buf; 43 char *map;
45 int unused __attribute__((unused));//this variable fixes warning "ignoring return value of fread" 44 int length;
46 45} OBJ_Loader;
47 *length = 0; 46
48 file_for_print = fopen(file, "rb"); 47static inline void
49 if (!file_for_print) return NULL; 48_obj_loader_fini(OBJ_Loader *loader)
50 fseek(file_for_print, 0, SEEK_END);//set file_for_print to the end of file 49{
51 *length = ftell(file_for_print);//set current position of file_for_print 50 if (loader->map)
52 if (*length < 0) 51 {
52 eina_file_map_free(loader->file, loader->map);
53 loader->map = NULL;
54 }
55
56 if (loader->file)
57 {
58 eina_file_close(loader->file);
59 loader->file = NULL;
60 }
61}
62
63static inline Eina_Bool
64_obj_loader_init(OBJ_Loader *loader, const char *file)
65{
66 memset(loader, 0x00, sizeof(OBJ_Loader));
67
68 /* Open given file. */
69 loader->file = eina_file_open(file, 0);
70
71 if (loader->file == NULL)
53 { 72 {
54 fclose(file_for_print); 73 ERR("Failed to open file %s\n", file);
55 return NULL; 74 goto error;
56 } 75 }
57 buf = malloc(*length + 1); 76
58 fseek(file_for_print, 0, SEEK_SET);//set file_for_print to the begining of file 77 /* Map the file. */
59 unused = fread(buf, *length, 1, file_for_print); 78 loader->map = eina_file_map_all(loader->file, EINA_FILE_SEQUENTIAL);
60 fclose(file_for_print); 79
61 buf[*length] = '\0'; 80 if (loader->map == NULL)
62 return buf; 81 {
82 ERR("Failed to create map from file %s\n", file);
83 goto error;
84 }
85
86 loader->length = eina_file_size_get(loader->file);
87
88 return EINA_TRUE;
89
90error:
91 _obj_loader_fini(loader);
92 return EINA_FALSE;
63} 93}
64 94
65/* create new counter */ 95/* create new counter */
@@ -107,11 +137,11 @@ _analyze_face_line(char * face_analyzer,
107} 137}
108 138
109static inline OBJ_Counts 139static inline OBJ_Counts
110_count_elements(char *start, long length)//count elements of mesh in .obj 140_count_elements(OBJ_Loader loader)//count elements of mesh in .obj
111{ 141{
112 OBJ_Counts counts = _new_count_elements(); 142 OBJ_Counts counts = _new_count_elements();
113 143
114 char * current = start; 144 char * current = loader.map;
115 int polygon_checker = -2;//polygons with n vertices can be represented as n-2 triangles 145 int polygon_checker = -2;//polygons with n vertices can be represented as n-2 triangles
116 Eina_Bool will_check_next_char = EINA_FALSE; 146 Eina_Bool will_check_next_char = EINA_FALSE;
117 Eina_Bool first_char_is_v = EINA_FALSE; 147 Eina_Bool first_char_is_v = EINA_FALSE;
@@ -120,7 +150,7 @@ _count_elements(char *start, long length)//count elements of mesh in .obj
120 150
121 long i = 0; 151 long i = 0;
122 /* count elements of mesh in .obj */ 152 /* count elements of mesh in .obj */
123 for (; length > i; i++) 153 for (; loader.length > i; i++)
124 { 154 {
125 if (will_check_next_char) 155 if (will_check_next_char)
126 { 156 {
@@ -204,9 +234,9 @@ _count_elements(char *start, long length)//count elements of mesh in .obj
204void 234void
205evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file) 235evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
206{ 236{
207 long length, i; 237 long i;
208 char * start = _file_to_buf(file, &length); 238 OBJ_Counts counts;//count elements of mesh in .obj
209 OBJ_Counts counts = _count_elements(start, length);//count elements of mesh in .obj 239 OBJ_Loader loader;
210 Eina_Bool will_check_next_char = EINA_FALSE; 240 Eina_Bool will_check_next_char = EINA_FALSE;
211 Eina_Bool first_char_is_v = EINA_FALSE; 241 Eina_Bool first_char_is_v = EINA_FALSE;
212 Eina_Bool first_char_is_f = EINA_FALSE; 242 Eina_Bool first_char_is_f = EINA_FALSE;
@@ -215,17 +245,26 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
215 int j, k; 245 int j, k;
216 char * current; 246 char * current;
217 247
248 /* Initialize PLY loader */
249 if (!_obj_loader_init(&loader, file))
250 {
251 ERR("Failed to initialize PLY loader.");
252 return;
253 }
254
255 counts = _count_elements(loader);
256
218 float *_vertices_obj = malloc(counts._vertex_counter * 3 * sizeof(float)); 257 float *_vertices_obj = malloc(counts._vertex_counter * 3 * sizeof(float));
219 float *_normales_obj = malloc(counts._normal_counter * 3 * sizeof(float)); 258 float *_normales_obj = malloc(counts._normal_counter * 3 * sizeof(float));
220 float *_tex_coords_obj = malloc(counts._texture_point_counter * 3 * sizeof(float)); 259 float *_tex_coords_obj = malloc(counts._texture_point_counter * 3 * sizeof(float));
221 /* triangle has 3 points, every point has 3(vertix, texture and normal) coord */ 260 /* triangle has 3 points, every point has 3(vertix, texture and normal) coord */
222 int *_triangles = malloc(counts._triangles_counter * 9 * sizeof(int)); 261 int *_triangles = malloc(counts._triangles_counter * 9 * sizeof(int));
223 262
224 if ((start == NULL) || (_vertices_obj == NULL) || 263 if ((loader.map == NULL) || (_vertices_obj == NULL) ||
225 (_normales_obj == NULL) || (_tex_coords_obj == NULL) || (_triangles == NULL)) 264 (_normales_obj == NULL) || (_tex_coords_obj == NULL) || (_triangles == NULL))
226 { 265 {
227 ERR("Allocate memory is failed."); 266 ERR("Allocate memory is failed.");
228 free(start); 267 _obj_loader_fini(&loader);
229 free(_vertices_obj); 268 free(_vertices_obj);
230 free(_normales_obj); 269 free(_normales_obj);
231 free(_tex_coords_obj); 270 free(_tex_coords_obj);
@@ -233,11 +272,11 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
233 return; 272 return;
234 } 273 }
235 274
236 current = start; 275 current = loader.map;
237 i = 0; 276 i = 0;
238 277
239 /* put data to arrays */ 278 /* put data to arrays */
240 for (; length > i; i++) 279 for (; loader.length > i; i++)
241 { 280 {
242 if (will_check_next_char) 281 if (will_check_next_char)
243 { 282 {
@@ -337,7 +376,6 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
337 } 376 }
338 current++; 377 current++;
339 } 378 }
340 free(start);
341 379
342 /* prepare of mesh and take pointers to data which must be read */ 380 /* prepare of mesh and take pointers to data which must be read */
343 eo_do(mesh, 381 eo_do(mesh,
@@ -398,4 +436,5 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
398 { 436 {
399 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0); 437 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0);
400 } 438 }
439 _obj_loader_fini(&loader);
401} 440}
diff --git a/src/modules/evas/model_loaders/ply/evas_model_load_ply.c b/src/modules/evas/model_loaders/ply/evas_model_load_ply.c
index a294f6eb0a..45e28a0de3 100644
--- a/src/modules/evas/model_loaders/ply/evas_model_load_ply.c
+++ b/src/modules/evas/model_loaders/ply/evas_model_load_ply.c
@@ -24,24 +24,56 @@ struct _PLY_Header
24 Eina_Bool existence_of_colors; 24 Eina_Bool existence_of_colors;
25}; 25};
26 26
27static inline char* 27typedef struct _PLY_Loader
28_file_to_buf(const char *file, long *length)//prepare text file for reading
29{ 28{
30 FILE *file_for_print; 29 Eina_File *file;
31 char *buf; 30 char *map;
32 int unused __attribute__((unused));//this variable fixes warning "ignoring return value of fread" 31} PLY_Loader;
33 32
34 *length = 0; 33static inline void
35 file_for_print = fopen(file, "rb"); 34_ply_loader_fini(PLY_Loader *loader)
36 if (!file_for_print) return NULL; 35{
37 fseek(file_for_print, 0, SEEK_END);//set file_for_print to the end of file 36 if (loader->map)
38 *length = ftell(file_for_print);//set current position of file_for_print 37 {
39 buf = malloc(*length + 1); 38 eina_file_map_free(loader->file, loader->map);
40 fseek(file_for_print, 0, SEEK_SET);//set file_for_print to the begining of file 39 loader->map = NULL;
41 unused = fread(buf, *length, 1, file_for_print); 40 }
42 fclose(file_for_print); 41
43 buf[*length] = '\0'; 42 if (loader->file)
44 return buf; 43 {
44 eina_file_close(loader->file);
45 loader->file = NULL;
46 }
47}
48
49static inline Eina_Bool
50_ply_loader_init(PLY_Loader *loader, const char *file)
51{
52 memset(loader, 0x00, sizeof(PLY_Loader));
53
54 /* Open given file. */
55 loader->file = eina_file_open(file, 0);
56
57 if (loader->file == NULL)
58 {
59 ERR("Failed to open file %s\n", file);
60 goto error;
61 }
62
63 /* Map the file. */
64 loader->map = eina_file_map_all(loader->file, EINA_FILE_SEQUENTIAL);
65
66 if (loader->map == NULL)
67 {
68 ERR("Failed to create map from file %s\n", file);
69 goto error;
70 }
71
72 return EINA_TRUE;
73
74error:
75 _ply_loader_fini(loader);
76 return EINA_FALSE;
45} 77}
46 78
47/* create new header */ 79/* create new header */
@@ -102,7 +134,7 @@ _read_data(float *array, int place, int count, char *current, float divider)
102} 134}
103 135
104static inline PLY_Header 136static inline PLY_Header
105_read_header(char *start)//Check properties of mesh in .ply file. 137_read_header(char *map)//Check properties of mesh in .ply file.
106{ 138{
107 eina_init(); 139 eina_init();
108 140
@@ -113,7 +145,7 @@ _read_header(char *start)//Check properties of mesh in .ply file.
113 PLY_Header header; 145 PLY_Header header;
114 146
115 header = _new_ply_header(); 147 header = _new_ply_header();
116 helping_pointer = eina_str_split(start, "vertex ", 0); 148 helping_pointer = eina_str_split(map, "vertex ", 0);
117 149
118 if (helping_pointer == NULL) 150 if (helping_pointer == NULL)
119 { 151 {
@@ -124,7 +156,7 @@ _read_header(char *start)//Check properties of mesh in .ply file.
124 sscanf(helping_pointer[1], "%d", &header.vertices_count); 156 sscanf(helping_pointer[1], "%d", &header.vertices_count);
125 157
126 free(helping_pointer); 158 free(helping_pointer);
127 helping_pointer = eina_str_split(start, "end_header\n", 0); 159 helping_pointer = eina_str_split(map, "end_header\n", 0);
128 160
129 if (helping_pointer == NULL) 161 if (helping_pointer == NULL)
130 { 162 {
@@ -164,7 +196,7 @@ _read_header(char *start)//Check properties of mesh in .ply file.
164 free(helping_pointer); 196 free(helping_pointer);
165 197
166 /* analyse flags used when file was saved in blender */ 198 /* analyse flags used when file was saved in blender */
167 helping_pointer = eina_str_split(start, "property float ", 0); 199 helping_pointer = eina_str_split(map, "property float ", 0);
168 200
169 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'x') && 201 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'x') &&
170 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'y') && 202 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'y') &&
@@ -185,7 +217,7 @@ _read_header(char *start)//Check properties of mesh in .ply file.
185 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 't')))) 217 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 't'))))
186 header.existence_of_texcoords = EINA_TRUE; 218 header.existence_of_texcoords = EINA_TRUE;
187 219
188 helping_pointer = eina_str_split(start, "property uchar ", 0); 220 helping_pointer = eina_str_split(map, "property uchar ", 0);
189 221
190 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'r') && 222 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'r') &&
191 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'g') && 223 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'g') &&
@@ -200,25 +232,25 @@ _read_header(char *start)//Check properties of mesh in .ply file.
200void 232void
201evas_model_load_file_ply(Evas_3D_Mesh *mesh, const char *file) 233evas_model_load_file_ply(Evas_3D_Mesh *mesh, const char *file)
202{ 234{
203 long length;
204 Evas_3D_Mesh_Data *pd; 235 Evas_3D_Mesh_Data *pd;
205 int i = 0, j = 0, k = 0, count_of_triangles_in_line = 0; 236 int i = 0, j = 0, k = 0, count_of_triangles_in_line = 0;
206 float *pos, *nor, *tex, *col; 237 float *pos, *nor, *tex, *col;
207 int stride_pos, stride_nor, stride_tex, stride_col; 238 int stride_pos, stride_nor, stride_tex, stride_col;
208 char *start, *current; 239 char *current;
209 PLY_Header header; 240 PLY_Header header;
210 float *_vertices_ply = NULL, *_normals_ply = NULL, *_tex_coords_ply = NULL, *_colors_ply = NULL; 241 float *_vertices_ply = NULL, *_normals_ply = NULL;
242 float *_tex_coords_ply = NULL, *_colors_ply = NULL;
211 char **helping_pointer; 243 char **helping_pointer;
244 PLY_Loader loader;
212 245
213 start = _file_to_buf(file, &length); 246 /* Initialize PLY loader */
214 247 if (!_ply_loader_init(&loader, file))
215 if (start == NULL)
216 { 248 {
217 ERR("Buffer is empty after preparation file for reading."); 249 ERR("Failed to initialize PLY loader.");
218 return; 250 return;
219 } 251 }
220 252
221 header = _read_header(start); 253 header = _read_header(loader.map);
222 254
223 if (!header.existence_of_geometries) 255 if (!header.existence_of_geometries)
224 { 256 {
@@ -226,7 +258,7 @@ evas_model_load_file_ply(Evas_3D_Mesh *mesh, const char *file)
226 return; 258 return;
227 } 259 }
228 260
229 helping_pointer = eina_str_split(start, "end_header\n", 0); 261 helping_pointer = eina_str_split(loader.map, "end_header\n", 0);
230 262
231 if (helping_pointer == NULL) 263 if (helping_pointer == NULL)
232 { 264 {
@@ -252,7 +284,7 @@ evas_model_load_file_ply(Evas_3D_Mesh *mesh, const char *file)
252 (_triangles == NULL)) 284 (_triangles == NULL))
253 { 285 {
254 ERR("Allocate memory is failed."); 286 ERR("Allocate memory is failed.");
255 free(start); 287 _ply_loader_fini(&loader);
256 free(_vertices_ply); 288 free(_vertices_ply);
257 free(_normals_ply); 289 free(_normals_ply);
258 free(_tex_coords_ply); 290 free(_tex_coords_ply);
@@ -398,4 +430,6 @@ evas_model_load_file_ply(Evas_3D_Mesh *mesh, const char *file)
398 { 430 {
399 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0); 431 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0);
400 } 432 }
433
434 _ply_loader_fini(&loader);
401} 435}