summaryrefslogtreecommitdiff
path: root/src/modules/evas/model_loaders
diff options
context:
space:
mode:
authorperepelits.m <perepelits.m@samsung.com>2015-12-10 16:37:30 +0900
committerJean-Philippe Andre <jp.andre@samsung.com>2015-12-10 16:50:06 +0900
commit01a32f64c0ffbe2ce048f8eb016d685913558cf5 (patch)
treef257fdc3489c8e4d2eb9e3fffa4299866d927b5c /src/modules/evas/model_loaders
parent1363cd757a8dcdc9adeaf47f781ad038446b2014 (diff)
Evas: Refactor model's savers and loaders.
Summary: Move common part to a separated document. Make code more readable using smaller functions. (from Task T2713) I did it again because somehow test is passing now. It seems like this test suite is unstable. Please, let me know if there are any errors after running distcheck. Reviewers: cedric, raster, Hermet, stefan_schmidt Subscribers: jpeg, artem.popov Differential Revision: https://phab.enlightenment.org/D3420
Diffstat (limited to 'src/modules/evas/model_loaders')
-rw-r--r--src/modules/evas/model_loaders/eet/evas_model_load_eet.c2
-rw-r--r--src/modules/evas/model_loaders/ply/evas_model_load_ply.c319
2 files changed, 79 insertions, 242 deletions
diff --git a/src/modules/evas/model_loaders/eet/evas_model_load_eet.c b/src/modules/evas/model_loaders/eet/evas_model_load_eet.c
index f5b1b6b..e049e2c 100644
--- a/src/modules/evas/model_loaders/eet/evas_model_load_eet.c
+++ b/src/modules/evas/model_loaders/eet/evas_model_load_eet.c
@@ -127,5 +127,3 @@ evas_model_load_file_eet(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
127 127
128 _evas_canvas3d_eet_file_free(eet_file); 128 _evas_canvas3d_eet_file_free(eet_file);
129} 129}
130
131
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 1beef18..971e951 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
@@ -1,44 +1,4 @@
1#ifdef HAVE_CONFIG_H 1#include "evas_model_load_save_common.h"
2# include "config.h"
3#endif
4
5#include <stdlib.h>
6#include "stdio.h"
7#include "evas_common_private.h"
8#include "evas_private.h"
9#include <Eina.h>
10
11/* set value to position [x][y] to array name which have. */
12#define ARRAY_2D(name, x, y, count_y) (*(name + x * count_y + y))
13
14/* Structures for reading data from file. */
15typedef struct _PLY_Header PLY_Header;
16
17struct _PLY_Header
18{
19 int vertices_count;
20 int triangles_count;
21 Eina_Bool existence_of_geometries;
22 Eina_Bool existence_of_normals;
23 Eina_Bool existence_of_texcoords;
24 Eina_Bool existence_of_colors;
25};
26
27/* create new header */
28static inline PLY_Header
29_new_ply_header()
30{
31 PLY_Header header;
32
33 header.vertices_count = 0;
34 header.triangles_count = 0;
35 header.existence_of_geometries = EINA_FALSE;
36 header.existence_of_normals = EINA_FALSE;
37 header.existence_of_texcoords = EINA_FALSE;
38 header.existence_of_colors = EINA_FALSE;
39
40 return header;
41}
42 2
43static inline char * 3static inline char *
44_to_next_line(char *current) 4_to_next_line(char *current)
@@ -81,8 +41,8 @@ _read_data(float *array, int place, int count, char *current, float divider)
81 return current; 41 return current;
82} 42}
83 43
84static inline PLY_Header 44static inline Eina_Bool
85_read_header(char *map)//Check properties of mesh in .ply file. 45_read_ply_header(char *map, Evas_Model_Load_Save_Header *header)
86{ 46{
87 eina_init(); 47 eina_init();
88 48
@@ -90,30 +50,29 @@ _read_header(char *map)//Check properties of mesh in .ply file.
90 int vertex_lines, triangles = 0, vertices_in_current_face = 0; 50 int vertex_lines, triangles = 0, vertices_in_current_face = 0;
91 char **helping_pointer; 51 char **helping_pointer;
92 char *current; 52 char *current;
93 PLY_Header header;
94 53
95 header = _new_ply_header();
96 helping_pointer = eina_str_split(map, "vertex ", 0); 54 helping_pointer = eina_str_split(map, "vertex ", 0);
97 55
98 if (helping_pointer == NULL) 56 if (helping_pointer == NULL)
99 { 57 {
100 ERR("File have not kayword vertex. It is necessary."); 58 ERR("File have not kayword vertex. It is necessary.");
101 return header; 59 return EINA_FALSE;
102 } 60 }
103 61
104 sscanf(helping_pointer[1], "%d", &header.vertices_count); 62 sscanf(helping_pointer[1], "%d", &header->vertices_count);
63
105 free(helping_pointer); 64 free(helping_pointer);
106 helping_pointer = eina_str_split(map, "end_header\n", 0); 65 helping_pointer = eina_str_split(map, "end_header\n", 0);
107 66
108 if (helping_pointer == NULL) 67 if (helping_pointer == NULL)
109 { 68 {
110 ERR("File have not kayword end_header. It is necessary."); 69 ERR("File have not kayword end_header. It is necessary.");
111 return header; 70 return EINA_FALSE;
112 } 71 }
113 72
114 current = helping_pointer[1]; 73 current = helping_pointer[1];
115 74
116 vertex_lines = header.vertices_count; 75 vertex_lines = header->vertices_count;
117 while (*current != '\0') 76 while (*current != '\0')
118 { 77 {
119 if (vertex_lines == 1) 78 if (vertex_lines == 1)
@@ -139,8 +98,7 @@ _read_header(char *map)//Check properties of mesh in .ply file.
139 current++; 98 current++;
140 } 99 }
141 100
142 header.triangles_count = triangles; 101 header->indices_count = 3 * triangles;
143 free(helping_pointer);
144 102
145 /* analyse flags used when file was saved in blender */ 103 /* analyse flags used when file was saved in blender */
146 helping_pointer = eina_str_split(map, "property float ", 0); 104 helping_pointer = eina_str_split(map, "property float ", 0);
@@ -148,243 +106,124 @@ _read_header(char *map)//Check properties of mesh in .ply file.
148 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'x') && 106 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'x') &&
149 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'y') && 107 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'y') &&
150 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'z')) 108 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'z'))
151 header.existence_of_geometries = EINA_TRUE; 109 header->existence_of_positions = EINA_TRUE;
152 else return header; 110 else return EINA_FALSE;
153 111
154 if ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 'n') && 112 if ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 'n') &&
155 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 'n') && 113 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 'n') &&
156 (helping_pointer[6] != NULL) && (*helping_pointer[6] == 'n')) 114 (helping_pointer[6] != NULL) && (*helping_pointer[6] == 'n'))
157 header.existence_of_normals = EINA_TRUE; 115 header->existence_of_normals = EINA_TRUE;
158 116
159 if ((header.existence_of_normals && 117 if ((header->existence_of_normals &&
160 ((helping_pointer[7] != NULL) && (*helping_pointer[7] == 's') && 118 ((helping_pointer[7] != NULL) && (*helping_pointer[7] == 's') &&
161 (helping_pointer[8] != NULL) && (*helping_pointer[8] == 't'))) || 119 (helping_pointer[8] != NULL) && (*helping_pointer[8] == 't'))) ||
162 (!header.existence_of_normals && 120 (!header->existence_of_normals &&
163 ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 's') && 121 ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 's') &&
164 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 't')))) 122 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 't'))))
165 header.existence_of_texcoords = EINA_TRUE; 123 header->existence_of_tex_coords = EINA_TRUE;
166 124
167 helping_pointer = eina_str_split(map, "property uchar ", 0); 125 helping_pointer = eina_str_split(map, "property uchar ", 0);
168 126
169 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'r') && 127 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'r') &&
170 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'g') && 128 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'g') &&
171 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'b')) 129 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'b'))
172 header.existence_of_colors = EINA_TRUE; 130 header->existence_of_colors = EINA_TRUE;
173
174 free(helping_pointer);
175
176 return header;
177}
178
179void
180evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
181{
182 Evas_Canvas3D_Mesh_Data *pd;
183 int i = 0, j = 0, count_of_triangles_in_line = 0;
184 float *pos, *nor, *tex, *col;
185 int stride_pos, stride_nor, stride_tex, stride_col;
186 char *current, *map;
187 PLY_Header header;
188 float *_vertices_ply = NULL, *_normals_ply = NULL;
189 float *_tex_coords_ply = NULL, *_colors_ply = NULL;
190 char **helping_pointer;
191
192 map = eina_file_map_all(file, EINA_FILE_SEQUENTIAL);
193 131
194 if (map == NULL) 132 if (!header->existence_of_positions)
195 {
196 ERR("Failed to create map from file %s\n", eina_file_filename_get(file));
197 return;
198 }
199
200 header = _read_header(map);
201
202 if (!header.existence_of_geometries)
203 { 133 {
204 ERR("File have not x, y, or z field as the first 3 float fields. They are necessary."); 134 ERR("File have not x, y, or z field as the first 3 float fields. They are necessary.");
205 return; 135 return EINA_FALSE;
206 } 136 }
207 137
208 helping_pointer = eina_str_split(map, "end_header\n", 0); 138 free(helping_pointer);
209 139 return EINA_TRUE;
210 if (helping_pointer == NULL) 140}
211 {
212 ERR("File have not kayword end_header. It is necessary.");
213 return;
214 }
215
216 current = helping_pointer[1];
217
218 _vertices_ply = malloc(header.vertices_count * 3 * sizeof(float));
219 if (header.existence_of_normals)
220 _normals_ply = malloc(header.vertices_count * 3 * sizeof(float));
221 if (header.existence_of_texcoords)
222 _tex_coords_ply = malloc(header.vertices_count * 2 * sizeof(float));
223 if (header.existence_of_colors)
224 _colors_ply = malloc(header.vertices_count * 3 * sizeof(float));
225 unsigned short *_indices = malloc(header.triangles_count * 3 * sizeof(unsigned short));
226
227 if ((header.existence_of_geometries && (_vertices_ply == NULL)) ||
228 (header.existence_of_normals && (_normals_ply == NULL)) ||
229 (header.existence_of_texcoords && (_tex_coords_ply == NULL)) ||
230 (header.existence_of_colors && (_colors_ply == NULL)) ||
231 (_indices == NULL))
232 {
233 ERR("Allocate memory is failed.");
234 free(_vertices_ply);
235 free(_normals_ply);
236 free(_tex_coords_ply);
237 free(_colors_ply);
238 free(_indices);
239 return;
240 }
241 141
142static inline void
143_read_ply_vertex_data(Evas_Model_Load_Save_Header header,
144 char **current,
145 Evas_Model_Load_Save_Data data)
146{
147 int i;
242 for (i = 0; i < header.vertices_count; i++) 148 for (i = 0; i < header.vertices_count; i++)
243 { 149 {
244 current = _read_data(_vertices_ply, i, 3, current, 1.0); 150 *current = _read_data(data.positions, i, 3, *current, 1.0);
245 if (header.existence_of_normals) 151 if (header.existence_of_normals)
246 current = _read_data(_normals_ply, i, 3, current, 1.0); 152 *current = _read_data(data.normals, i, 3, *current, 1.0);
247 if (header.existence_of_texcoords) 153 if (header.existence_of_tex_coords)
248 current = _read_data(_tex_coords_ply, i, 2, current, 1.0); 154 *current = _read_data(data.tex_coords, i, 2, *current, 1.0);
249 if (header.existence_of_colors) 155 if (header.existence_of_colors)
250 current = _read_data(_colors_ply, i, 3, current, 255.0); 156 *current = _read_data(data.colors, i, 3, *current, 255.0);
251 current = _to_begin_of_line(current); 157 *current = _to_begin_of_line(*current);
252 } 158 }
159}
253 160
254 for (i = 0; i < header.triangles_count * 3;) 161static inline void
162_read_ply_indices_data(Evas_Model_Load_Save_Header header,
163 char **current,
164 Evas_Model_Load_Save_Data data)
165{
166 int i, j, count_of_triangles_in_line = 0;
167
168 for (i = 0; i < header.indices_count;)
255 { 169 {
256 sscanf (current,"%d", &count_of_triangles_in_line); 170 sscanf (*current,"%d", &count_of_triangles_in_line);
257 count_of_triangles_in_line -= 2; 171 count_of_triangles_in_line -= 2;
258 current = _to_next_number(current, 1); 172 *current = _to_next_number(*current, 1);
259 173
260 sscanf (current,"%hu", _indices + i); 174 sscanf (*current,"%hu", data.indices + i);
261 175
262 for (j = 0; j < count_of_triangles_in_line; j++) 176 for (j = 0; j < count_of_triangles_in_line; j++)
263 { 177 {
264 if (j > 0) 178 if (j > 0)
265 _indices[i] = _indices[i - 3]; 179 data.indices[i] = data.indices[i - 3];
266 current = _to_next_number(current, 1); 180 *current = _to_next_number(*current, 1);
267 sscanf (current,"%hu %hu", 181 sscanf (*current,"%hu %hu",
268 _indices + i + 1, 182 data.indices + i + 1,
269 _indices + i + 2); 183 data.indices + i + 2);
270 i+=3; 184 i+=3;
271 } 185 }
272 current = _to_next_line(current); 186 *current = _to_next_line(*current);
273 } 187 }
188}
274 189
275 /* prepare of mesh and take pointers to data which must be read */ 190void
276 eo_do(mesh, 191evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
277 evas_canvas3d_mesh_vertex_count_set(header.vertices_count), 192{
278 evas_canvas3d_mesh_vertex_assembly_set(EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLES), 193 char *current = NULL, *map = NULL;
279 evas_canvas3d_mesh_frame_add(0), 194 Evas_Model_Load_Save_Header header;
280 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION, 0, NULL), 195 Evas_Model_Load_Save_Data data;
281 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL, 0, NULL), 196 Evas_Model_Load_Save_Stride stride;
282 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD, 0, NULL),
283 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR, 0, NULL),
284
285 pos = (float *)evas_canvas3d_mesh_frame_vertex_data_map(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION),
286 nor = (float *)evas_canvas3d_mesh_frame_vertex_data_map(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL),
287 tex = (float *)evas_canvas3d_mesh_frame_vertex_data_map(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD),
288 col = (float *)evas_canvas3d_mesh_frame_vertex_data_map(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR),
289
290 stride_pos = evas_canvas3d_mesh_frame_vertex_stride_get(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION),
291 stride_nor = evas_canvas3d_mesh_frame_vertex_stride_get(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL),
292 stride_tex = evas_canvas3d_mesh_frame_vertex_stride_get(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD),
293 stride_col = evas_canvas3d_mesh_frame_vertex_stride_get(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR));
294
295 if (stride_pos == 0) stride_pos = sizeof(float) * 3;
296 if (stride_nor == 0) stride_nor = sizeof(float) * 3;
297 if (stride_tex == 0) stride_tex = sizeof(float) * 2;
298 if (stride_col == 0) stride_col = sizeof(float) * 4;
299
300
301 for (j = 0; j < header.vertices_count; j++)
302 {
303 float *p, *n, *t, *c;
304
305 p = (float *)((char *)pos + stride_pos * j);
306 n = (float *)((char *)nor + stride_nor * j);
307 t = (float *)((char *)tex + stride_tex * j);
308 c = (float *)((char *)col + stride_col * j);
309
310
311 p[0] = ARRAY_2D(_vertices_ply, j, 0, 3);
312 p[1] = ARRAY_2D(_vertices_ply, j, 1, 3);
313 p[2] = ARRAY_2D(_vertices_ply, j, 2, 3);
314
315 if (header.existence_of_normals)
316 {
317 n[0] = ARRAY_2D(_normals_ply, j, 0, 3);
318 n[1] = ARRAY_2D(_normals_ply, j, 1, 3);
319 n[2] = ARRAY_2D(_normals_ply, j, 2, 3);
320 }
321 else
322 {
323 n[0] = 0.0;
324 n[1] = 0.0;
325 n[2] = 0.0;
326 }
327
328 if (header.existence_of_texcoords)
329 {
330 t[0] = ARRAY_2D(_tex_coords_ply, j, 0, 2);
331 t[1] = ARRAY_2D(_tex_coords_ply, j, 1, 2);
332 }
333 else
334 {
335 t[0] = 0.0;
336 t[1] = 0.0;
337 }
338 197
339 if (header.existence_of_colors) 198 map = eina_file_map_all(file, EINA_FILE_SEQUENTIAL);
340 { 199 if (map == NULL)
341 c[0] = ARRAY_2D(_colors_ply, j, 0, 3); 200 {
342 c[1] = ARRAY_2D(_colors_ply, j, 1, 3); 201 ERR("Failed to create map from file %s\n", eina_file_filename_get(file));
343 c[2] = ARRAY_2D(_colors_ply, j, 2, 3); 202 return;
344 c[3] = 1.0;
345 }
346 else
347 {
348 c[0] = 0.0;
349 c[1] = 0.0;
350 c[2] = 0.0;
351 c[3] = 1.0;
352 }
353 } 203 }
354 204
355 eo_do(mesh, 205 header = evas_model_load_save_header_new();
356 evas_canvas3d_mesh_index_data_copy_set(EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_SHORT, 206 if(!_read_ply_header(map, &header)) return;
357 header.triangles_count * 3,
358 _indices));
359 207
360 free(helping_pointer); 208 if (!evas_model_load_allocate_data_due_to_header(header, &data))
361 free(_vertices_ply);
362 if (header.existence_of_normals)
363 free(_normals_ply);
364 if (header.existence_of_texcoords)
365 free(_tex_coords_ply);
366 if (header.existence_of_colors)
367 free(_colors_ply);
368 free(_indices);
369
370 /* Unmap vertex buffer. */
371 eo_do(mesh,
372 evas_canvas3d_mesh_frame_vertex_data_unmap(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION),
373 evas_canvas3d_mesh_frame_vertex_data_unmap(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL),
374 evas_canvas3d_mesh_frame_vertex_data_unmap(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD),
375 evas_canvas3d_mesh_frame_vertex_data_unmap(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR));
376
377 pd = eo_data_scope_get(mesh, EVAS_CANVAS3D_MESH_CLASS);
378
379 if (!evas_canvas3d_mesh_aabb_add_to_frame(pd, 0, stride_pos))
380 { 209 {
381 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0); 210 ERR("Memory allocation is failed.");
211 return;
382 } 212 }
383 213
214 current = eina_str_split(map, "end_header\n", 0)[1];
215 _read_ply_vertex_data(header, &current, data);
216 _read_ply_indices_data(header, &current, data);
217 evas_model_load_vertex_data_to_mesh(mesh, header, data, &stride);
218 evas_model_load_indices_data_to_mesh(mesh, header, data);
219 evas_model_load_vertex_data_unmap(mesh, 0, header);
220 evas_model_load_aabb_add_to_frame(mesh, 0, stride);
221
384 if (map) 222 if (map)
385 { 223 {
386 eina_file_map_free(file, map); 224 eina_file_map_free(file, map);
387 map = NULL; 225 map = NULL;
388 } 226 }
389}
390 227
228 evas_model_load_save_data_free(header, &data);
229}