summaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authorBogdan Devichev <b.devichev@samsung.com>2014-12-03 12:24:03 +0200
committerCedric BAIL <cedric@osg.samsung.com>2014-12-23 21:19:20 +0100
commite024f861e1aa3ffd74ea6a79ce480242930471dd (patch)
tree81234f5585ce2956e540e932f9507afbaadd46bc /src/modules
parent4acbae69472662feef7a88aedd19ce6ad4d0ebe1 (diff)
evas: ply_loader and obj_loader work with Eina_File.
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/evas/model_loaders/obj/evas_model_load_obj.c99
-rw-r--r--src/modules/evas/model_loaders/ply/evas_model_load_ply.c98
2 files changed, 134 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..359d29904e 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
@@ -7,6 +7,7 @@
7#include "evas_common_private.h" 7#include "evas_common_private.h"
8#include "evas_private.h" 8#include "evas_private.h"
9 9
10//TODO Increase of stability (reading .obj file saved with any flags).
10/* set value to position [x][y] to array name which have. */ 11/* set value to position [x][y] to array name which have. */
11#define ARRAY_2D(name, x, y, count_y) (*(name + x * count_y + y)) 12#define ARRAY_2D(name, x, y, count_y) (*(name + x * count_y + y))
12 13
@@ -37,29 +38,56 @@ struct _OBJ_Counts
37 Eina_Bool existence_of_tex_point; 38 Eina_Bool existence_of_tex_point;
38}; 39};
39 40
40static inline char* 41typedef struct _OBJ_Loader
41_file_to_buf(const char *file, long *length)//prepare text file for reading
42{ 42{
43 FILE *file_for_print; 43 Eina_File *file;
44 char *buf; 44 char *map;
45 int unused __attribute__((unused));//this variable fixes warning "ignoring return value of fread" 45} OBJ_Loader;
46 46
47 *length = 0; 47static inline void
48 file_for_print = fopen(file, "rb"); 48_obj_loader_fini(OBJ_Loader *loader)
49 if (!file_for_print) return NULL; 49{
50 fseek(file_for_print, 0, SEEK_END);//set file_for_print to the end of file 50 if (loader->map)
51 *length = ftell(file_for_print);//set current position of file_for_print 51 {
52 if (*length < 0) 52 eina_file_map_free(loader->file, loader->map);
53 loader->map = NULL;
54 }
55
56 if (loader->file)
53 { 57 {
54 fclose(file_for_print); 58 eina_file_close(loader->file);
55 return NULL; 59 loader->file = NULL;
56 } 60 }
57 buf = malloc(*length + 1); 61}
58 fseek(file_for_print, 0, SEEK_SET);//set file_for_print to the begining of file 62
59 unused = fread(buf, *length, 1, file_for_print); 63static inline Eina_Bool
60 fclose(file_for_print); 64_obj_loader_init(OBJ_Loader *loader, const char *file)
61 buf[*length] = '\0'; 65{
62 return buf; 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)
72 {
73 ERR("Failed to open file %s\n", file);
74 goto error;
75 }
76
77 /* Map the file. */
78 loader->map = eina_file_map_all(loader->file, EINA_FILE_SEQUENTIAL);
79
80 if (loader->map == NULL)
81 {
82 ERR("Failed to create map from file %s\n", file);
83 goto error;
84 }
85
86 return EINA_TRUE;
87
88error:
89 _obj_loader_fini(loader);
90 return EINA_FALSE;
63} 91}
64 92
65/* create new counter */ 93/* create new counter */
@@ -107,11 +135,11 @@ _analyze_face_line(char * face_analyzer,
107} 135}
108 136
109static inline OBJ_Counts 137static inline OBJ_Counts
110_count_elements(char *start, long length)//count elements of mesh in .obj 138_count_elements(OBJ_Loader loader)//count elements of mesh in .obj
111{ 139{
112 OBJ_Counts counts = _new_count_elements(); 140 OBJ_Counts counts = _new_count_elements();
113 141
114 char * current = start; 142 char * current = loader.map;
115 int polygon_checker = -2;//polygons with n vertices can be represented as n-2 triangles 143 int polygon_checker = -2;//polygons with n vertices can be represented as n-2 triangles
116 Eina_Bool will_check_next_char = EINA_FALSE; 144 Eina_Bool will_check_next_char = EINA_FALSE;
117 Eina_Bool first_char_is_v = EINA_FALSE; 145 Eina_Bool first_char_is_v = EINA_FALSE;
@@ -120,7 +148,7 @@ _count_elements(char *start, long length)//count elements of mesh in .obj
120 148
121 long i = 0; 149 long i = 0;
122 /* count elements of mesh in .obj */ 150 /* count elements of mesh in .obj */
123 for (; length > i; i++) 151 for (; *current != '\00'; i++)
124 { 152 {
125 if (will_check_next_char) 153 if (will_check_next_char)
126 { 154 {
@@ -204,9 +232,9 @@ _count_elements(char *start, long length)//count elements of mesh in .obj
204void 232void
205evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file) 233evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
206{ 234{
207 long length, i; 235 long i;
208 char * start = _file_to_buf(file, &length); 236 OBJ_Counts counts;//count elements of mesh in .obj
209 OBJ_Counts counts = _count_elements(start, length);//count elements of mesh in .obj 237 OBJ_Loader loader;
210 Eina_Bool will_check_next_char = EINA_FALSE; 238 Eina_Bool will_check_next_char = EINA_FALSE;
211 Eina_Bool first_char_is_v = EINA_FALSE; 239 Eina_Bool first_char_is_v = EINA_FALSE;
212 Eina_Bool first_char_is_f = EINA_FALSE; 240 Eina_Bool first_char_is_f = EINA_FALSE;
@@ -215,17 +243,26 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
215 int j, k; 243 int j, k;
216 char * current; 244 char * current;
217 245
246 /* Initialize PLY loader */
247 if (!_obj_loader_init(&loader, file))
248 {
249 ERR("Failed to initialize PLY loader.");
250 return;
251 }
252
253 counts = _count_elements(loader);
254
218 float *_vertices_obj = malloc(counts._vertex_counter * 3 * sizeof(float)); 255 float *_vertices_obj = malloc(counts._vertex_counter * 3 * sizeof(float));
219 float *_normales_obj = malloc(counts._normal_counter * 3 * sizeof(float)); 256 float *_normales_obj = malloc(counts._normal_counter * 3 * sizeof(float));
220 float *_tex_coords_obj = malloc(counts._texture_point_counter * 3 * sizeof(float)); 257 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 */ 258 /* triangle has 3 points, every point has 3(vertix, texture and normal) coord */
222 int *_triangles = malloc(counts._triangles_counter * 9 * sizeof(int)); 259 int *_triangles = malloc(counts._triangles_counter * 9 * sizeof(int));
223 260
224 if ((start == NULL) || (_vertices_obj == NULL) || 261 if ((loader.map == NULL) || (_vertices_obj == NULL) ||
225 (_normales_obj == NULL) || (_tex_coords_obj == NULL) || (_triangles == NULL)) 262 (_normales_obj == NULL) || (_tex_coords_obj == NULL) || (_triangles == NULL))
226 { 263 {
227 ERR("Allocate memory is failed."); 264 ERR("Allocate memory is failed.");
228 free(start); 265 _obj_loader_fini(&loader);
229 free(_vertices_obj); 266 free(_vertices_obj);
230 free(_normales_obj); 267 free(_normales_obj);
231 free(_tex_coords_obj); 268 free(_tex_coords_obj);
@@ -233,11 +270,11 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
233 return; 270 return;
234 } 271 }
235 272
236 current = start; 273 current = loader.map;
237 i = 0; 274 i = 0;
238 275
239 /* put data to arrays */ 276 /* put data to arrays */
240 for (; length > i; i++) 277 for (; *current != '\00'; i++)
241 { 278 {
242 if (will_check_next_char) 279 if (will_check_next_char)
243 { 280 {
@@ -337,7 +374,6 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
337 } 374 }
338 current++; 375 current++;
339 } 376 }
340 free(start);
341 377
342 /* prepare of mesh and take pointers to data which must be read */ 378 /* prepare of mesh and take pointers to data which must be read */
343 eo_do(mesh, 379 eo_do(mesh,
@@ -398,4 +434,5 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
398 { 434 {
399 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0); 435 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0);
400 } 436 }
437 _obj_loader_fini(&loader);
401} 438}
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}