-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioply.c
314 lines (269 loc) · 9.22 KB
/
ioply.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "ioply.h"
#include <stdio.h>
#include <string.h>
#include <trico/alloc.h>
#include <rply/rply.h>
static int read_vec3_coord(p_ply_argument argument)
{
float** p_vertices;
ply_get_argument_user_data(argument, (void**)&p_vertices, NULL);
double val = ply_get_argument_value(argument);
*(*p_vertices) = (float)val;
(*p_vertices) += 3;
return 1;
}
static int read_color(p_ply_argument argument)
{
uint8_t** p_color;
ply_get_argument_user_data(argument, (void**)&p_color, NULL);
double val = ply_get_argument_value(argument);
*(*p_color) = (uint8_t)val;
(*p_color) += 4;
return 1;
}
static int read_face(p_ply_argument argument)
{
uint32_t** p_triangle;
ply_get_argument_user_data(argument, (void**)&p_triangle, NULL);
long length, value_index;
ply_get_argument_property(argument, NULL, &length, &value_index);
if (value_index >= 0 && value_index < 3)
{
double val = ply_get_argument_value(argument);
*(*p_triangle) = (uint32_t)val;
++(*p_triangle);
}
return 1;
}
static int read_texcoord(p_ply_argument argument)
{
float** p_uv;
ply_get_argument_user_data(argument, (void**)&p_uv, NULL);
long length, value_index;
ply_get_argument_property(argument, NULL, &length, &value_index);
if (value_index >= 0 && value_index < 6)
{
double val = ply_get_argument_value(argument);
*(*p_uv) = (float)val;
++(*p_uv);
}
if (value_index == (length - 1) && (length != 6)) // this case happens when the ply file has less than 6 texture coordinates for a face
{
for (long j = length; j < 6; ++j) // fill the remaining texture coordinates with 0.f
{
*(*p_uv) = (float)0.f;
++(*p_uv);
}
}
return 1;
}
int trico_read_ply(uint32_t* nr_of_vertices,
float** vertices,
float** vertex_normals,
uint32_t** vertex_colors,
uint32_t* nr_of_triangles,
uint32_t** triangles,
float** texcoords,
const char* filename)
{
*nr_of_vertices = 0;
*vertices = NULL;
*vertex_normals = NULL;
*vertex_colors = NULL;
*nr_of_triangles = 0;
*triangles = NULL;
*texcoords = NULL;
p_ply ply = ply_open(filename, NULL, 0, NULL);
if (!ply)
return 0;
if (!ply_read_header(ply))
return 0;
float* p_vertex_pointer_x = NULL;
float* p_vertex_pointer_y = NULL;
float* p_vertex_pointer_z = NULL;
long nvertices_x = ply_set_read_cb(ply, "vertex", "x", read_vec3_coord, (void*)(&p_vertex_pointer_x), 0);
long nvertices_y = ply_set_read_cb(ply, "vertex", "y", read_vec3_coord, (void*)(&p_vertex_pointer_y), 0);
long nvertices_z = ply_set_read_cb(ply, "vertex", "z", read_vec3_coord, (void*)(&p_vertex_pointer_z), 0);
if ((nvertices_x != nvertices_y) || (nvertices_x != nvertices_z))
{
ply_close(ply);
return 0;
}
*nr_of_vertices = (uint32_t)nvertices_x;
if (nvertices_x > 0)
*vertices = (float*)trico_malloc(*nr_of_vertices * 3 * sizeof(float));
p_vertex_pointer_x = (float*)(*vertices);
p_vertex_pointer_y = p_vertex_pointer_x + 1;
p_vertex_pointer_z = p_vertex_pointer_x + 2;
float* p_normal_pointer_x = NULL;
float* p_normal_pointer_y = NULL;
float* p_normal_pointer_z = NULL;
long nnormals_x = ply_set_read_cb(ply, "vertex", "nx", read_vec3_coord, (void*)(&p_normal_pointer_x), 0);
long nnormals_y = ply_set_read_cb(ply, "vertex", "ny", read_vec3_coord, (void*)(&p_normal_pointer_y), 0);
long nnormals_z = ply_set_read_cb(ply, "vertex", "nz", read_vec3_coord, (void*)(&p_normal_pointer_z), 0);
if ((nnormals_x != nnormals_y) || (nnormals_x != nnormals_z) || (nnormals_x && ((uint32_t)nnormals_x != *nr_of_vertices)))
{
if (*nr_of_vertices)
{
free(*vertices);
*vertices = NULL;
*nr_of_vertices = 0;
}
ply_close(ply);
return 0;
}
if (nnormals_x > 0)
*vertex_normals = (float*)trico_malloc(*nr_of_vertices * 3 * sizeof(float));
p_normal_pointer_x = (float*)(*vertex_normals);
p_normal_pointer_y = p_normal_pointer_x + 1;
p_normal_pointer_z = p_normal_pointer_x + 2;
uint8_t* p_red = NULL;
uint8_t* p_green = NULL;
uint8_t* p_blue = NULL;
uint8_t* p_alpha = NULL;
long nred = ply_set_read_cb(ply, "vertex", "red", read_color, (void*)(&p_red), 0);
long ngreen = ply_set_read_cb(ply, "vertex", "green", read_color, (void*)(&p_green), 0);
long nblue = ply_set_read_cb(ply, "vertex", "blue", read_color, (void*)(&p_blue), 0);
long nalpha = ply_set_read_cb(ply, "vertex", "alpha", read_color, (void*)(&p_alpha), 0);
if (nred == 0)
nred = ply_set_read_cb(ply, "vertex", "r", read_color, (void*)(&p_red), 0);
if (ngreen == 0)
ngreen = ply_set_read_cb(ply, "vertex", "g", read_color, (void*)(&p_green), 0);
if (nblue == 0)
nblue = ply_set_read_cb(ply, "vertex", "b", read_color, (void*)(&p_blue), 0);
if (nalpha == 0)
nalpha = ply_set_read_cb(ply, "vertex", "a", read_color, (void*)(&p_alpha), 0);
if (nred == 0)
nred = ply_set_read_cb(ply, "vertex", "diffuse_red", read_color, (void*)(&p_red), 0);
if (ngreen == 0)
ngreen = ply_set_read_cb(ply, "vertex", "diffuse_green", read_color, (void*)(&p_green), 0);
if (nblue == 0)
nblue = ply_set_read_cb(ply, "vertex", "diffuse_blue", read_color, (void*)(&p_blue), 0);
if (nalpha == 0)
nalpha = ply_set_read_cb(ply, "vertex", "diffuse_alpha", read_color, (void*)(&p_alpha), 0);
if (nred > 0 || ngreen > 0 || nblue > 0 || nalpha > 0)
{
if ((nred && ((uint32_t)nred != *nr_of_vertices)) || (ngreen && ((uint32_t)ngreen != *nr_of_vertices)) || (nblue && ((uint32_t)nblue != *nr_of_vertices)) || (nalpha && ((uint32_t)nalpha != *nr_of_vertices)))
{
if (*nr_of_vertices)
{
free(*vertices);
free(*vertex_normals);
*vertices = NULL;
*vertex_normals = NULL;
*nr_of_vertices = 0;
}
ply_close(ply);
return 0;
}
*vertex_colors = (uint32_t*)trico_malloc(*nr_of_vertices * sizeof(uint32_t));
uint32_t* p_clr = *vertex_colors;
for (uint32_t c = 0; c < *nr_of_vertices; ++c)
*p_clr++ = 0xffffffff;
}
p_red = (uint8_t*)(*vertex_colors);
p_green = p_red + 1;
p_blue = p_red + 2;
p_alpha = p_red + 3;
uint32_t* p_tria_index = NULL;
long ntriangles = ply_set_read_cb(ply, "face", "vertex_indices", read_face, (void*)(&p_tria_index), 0);
if (ntriangles == 0)
ntriangles = ply_set_read_cb(ply, "face", "vertex_index", read_face, (void*)(&p_tria_index), 0);
*nr_of_triangles = (uint32_t)ntriangles;
if (ntriangles > 0)
*triangles = (uint32_t*)trico_malloc(*nr_of_triangles * 3 * sizeof(uint32_t));
p_tria_index = (uint32_t*)(*triangles);
float* p_uv = NULL;
long ntexcoords = ply_set_read_cb(ply, "face", "texcoord", read_texcoord, (void*)(&p_uv), 0);
if (ntexcoords && ((uint32_t)ntexcoords != *nr_of_triangles))
{
if (*nr_of_vertices)
{
free(*vertices);
free(*vertex_normals);
free(*vertex_colors);
*vertices = NULL;
*vertex_normals = NULL;
*vertex_colors = NULL;
*nr_of_vertices = 0;
}
if (*nr_of_triangles)
{
free(*triangles);
*triangles = NULL;
*nr_of_triangles = 0;
}
ply_close(ply);
return 0;
}
if (ntexcoords > 0)
*texcoords = (float*)trico_malloc(*nr_of_triangles * 6 * sizeof(float));
p_uv = (float*)(*texcoords);
if (!ply_read(ply))
return 0;
ply_close(ply);
return 1;
}
int trico_write_ply(const uint32_t nr_of_vertices, const float* vertices, const float* vertex_normals, const uint32_t* vertex_colors, const uint32_t nr_of_triangles, const uint32_t* triangles, const float* texcoords, const char* filename)
{
if (!vertices)
return 0;
if (nr_of_vertices == 0)
return 0;
FILE* fp = fopen(filename, "wb");
if (!fp)
return 0;
fprintf(fp, "ply\n");
int n = 1;
if (*(char *)&n == 1)
fprintf(fp, "format binary_little_endian 1.0\n");
else
fprintf(fp, "format binary_big_endian 1.0\n");
fprintf(fp, "element vertex %d\n", nr_of_vertices);
fprintf(fp, "property float x\n");
fprintf(fp, "property float y\n");
fprintf(fp, "property float z\n");
if (vertex_normals)
{
fprintf(fp, "property float nx\n");
fprintf(fp, "property float ny\n");
fprintf(fp, "property float nz\n");
}
if (vertex_colors)
{
fprintf(fp, "property uchar red\n");
fprintf(fp, "property uchar green\n");
fprintf(fp, "property uchar blue\n");
fprintf(fp, "property uchar alpha\n");
}
if (nr_of_triangles && triangles)
{
fprintf(fp, "element face %d\n", nr_of_triangles);
fprintf(fp, "property list uchar int vertex_indices\n");
if (texcoords)
fprintf(fp, "property list uchar float texcoord\n");
}
fprintf(fp, "end_header\n");
for (uint32_t i = 0; i < nr_of_vertices; ++i)
{
fwrite(vertices + 3 * i, sizeof(float), 3, fp);
if (vertex_normals)
fwrite(vertex_normals + 3 * i, sizeof(float), 3, fp);
if (vertex_colors)
fwrite(vertex_colors + i, sizeof(uint32_t), 1, fp);
}
const unsigned char tria_size = 3;
const unsigned char texcoord_size = 6;
for (uint32_t i = 0; i < nr_of_triangles; ++i)
{
fwrite(&tria_size, 1, 1, fp);
fwrite(triangles + 3 * i, sizeof(uint32_t), 3, fp);
if (texcoords)
{
fwrite(&texcoord_size, 1, 1, fp);
fwrite(texcoords + 6 * i, sizeof(float), 6, fp);
}
}
fclose(fp);
return 1;
}