1 #ifndef CISPLYSERIALIZE_H_ 2 #define CISPLYSERIALIZE_H_ 41 void free_ply(
ply_t *p) {
44 for (i = 0; i < p->
nvert; i++) {
54 for (i = 0; i < p->
nvert; i++) {
63 if (p->
faces != NULL) {
64 for (i = 0; i < p->
nface; i++) {
65 if (p->
faces[i] != NULL) {
88 int alloc_ply(
ply_t *p,
int nvert,
int nface,
int do_color) {
94 float **new_vert = (
float**)malloc(p->
nvert*
sizeof(
float*));
95 if (new_vert == NULL) {
96 cislog_error(
"alloc_ply: Failed to allocate vertices.");
101 for (i = 0; i < p->
nvert; i++) {
102 float *ivert = (
float*)malloc(3*
sizeof(
float));
104 cislog_error(
"alloc_ply: Failed to allocate vertex %d.", i);
110 cislog_debug(
"alloc_ply: Allocated %d vertices.", nvert);
113 int **new_vert = (
int**)malloc(p->
nvert*
sizeof(
int*));
114 if (new_vert == NULL) {
115 cislog_error(
"alloc_ply: Failed to allocate vertex_colors.");
120 for (i = 0; i < p->
nvert; i++) {
121 int *ivert = (
int*)malloc(3*
sizeof(
int));
123 cislog_error(
"alloc_ply: Failed to allocate vertex color %d.", i);
129 cislog_debug(
"alloc_ply: Allocated %d vertex colors.", nvert);
132 int **new_face = (
int**)malloc(p->
nface*
sizeof(
int*));
133 if (new_face == NULL) {
134 cislog_error(
"alloc_ply: Failed to allocate faces.");
139 for (i = 0; i < p->
nface; i++) {
148 cislog_debug(
"alloc_ply: Allocated %d faces.", nface);
150 int *new_nvert = (
int*)malloc(p->
nface*
sizeof(
int));
151 if (new_nvert == NULL) {
152 cislog_error(
"alloc_ply: Failed to allocate nvert_in_face.");
157 for (i = 0; i < p->
nface; i++) {
160 cislog_debug(
"alloc_ply: Allocate for %d vertices and %d faces.",
176 int serialize_ply(
const seri_t s,
char *buf,
const size_t buf_size,
177 int *args_used, va_list ap) {
185 char header_format[500] =
"ply\n" 187 "comment author cis_auto\n" 188 "comment File generated by cis_interface\n" 189 "element vertex %d\n" 192 "property float z\n";
194 char header_format_colors[100] =
"property uchar diffuse_red\n" 195 "property uchar diffuse_green\n" 196 "property uchar diffuse_blue\n";
197 strcat(header_format, header_format_colors);
199 char header_format2[100] =
"element face %d\n" 200 "property list uchar int vertex_indices\n" 202 strcat(header_format, header_format2);
203 ilen = snprintf(buf, buf_size, header_format, p.
nvert, p.
nface);
205 cislog_error(
"serialize_ply: Error formatting header.");
207 }
else if (ilen >= buf_size) {
208 cislog_error(
"serialize_ply: Buffer (size = %d) is not large " 209 "enough to contain the header (size = %d).", buf_size, ilen);
210 return msg_len + ilen;
212 msg_len = msg_len + ilen;
215 for (i = 0; i < p.
nvert; i++) {
217 ilen = snprintf(buf + msg_len, buf_size - msg_len,
"%f %f %f %d %d %d\n",
221 ilen = snprintf(buf + msg_len, buf_size - msg_len,
"%f %f %f\n",
225 cislog_error(
"serialize_ply: Error formatting vertex %d.", i);
227 }
else if (ilen >= (buf_size - msg_len)) {
228 cislog_error(
"serialize_ply: Buffer (size = %d) is not large " 229 "enough to contain vertex %d (size = %d).",
230 buf_size, i, ilen + msg_len);
231 return msg_len + ilen;
233 msg_len = msg_len + ilen;
236 for (i = 0; i < p.
nface; i++) {
237 ilen = snprintf(buf + msg_len, buf_size - msg_len,
"%d", p.
nvert_in_face[i]);
239 cislog_error(
"serialize_ply: Error formatting number of verts for face %d.", i);
241 }
else if (ilen > (buf_size - msg_len)) {
242 cislog_error(
"serialize_ply: Buffer (size = %d) is not large " 243 "enough to contain number of verts for face %d (size = %d).",
244 buf_size, i, ilen + msg_len);
245 return msg_len + ilen;
247 msg_len = msg_len + ilen;
249 ilen = snprintf(buf + msg_len, buf_size - msg_len,
" %d", p.
faces[i][j]);
251 cislog_error(
"serialize_ply: Error formatting element %d of face %d.", j, i);
253 }
else if (ilen > (buf_size - msg_len)) {
254 cislog_error(
"serialize_ply: Buffer (size = %d) is not large " 255 "enough to contain element %d of face %d (size = %d).",
256 buf_size, j, i, ilen + msg_len);
257 return msg_len + ilen;
259 msg_len = msg_len + ilen;
261 ilen = snprintf(buf + msg_len, buf_size - msg_len,
"\n");
263 cislog_error(
"serialize_ply: Error formatting newline for face %d.", i);
265 }
else if (ilen > (buf_size - msg_len)) {
266 cislog_error(
"serialize_ply: Buffer (size = %d) is not large " 267 "enough to contain newline for face %d (size = %d).",
268 buf_size, i, ilen + msg_len);
269 return msg_len + ilen;
271 msg_len = msg_len + ilen;
286 int deserialize_ply(
const seri_t s,
const char *buf,
const size_t buf_siz,
293 size_t *sind_body = NULL;
294 size_t *eind_body = NULL;
297 size_t begin_body = 0;
300 int nvert = 0, nface = 0;
309 n_sub_matches = find_matches(
"element vertex ([[:digit:]]+)\n", buf, &sind, &eind);
310 if (n_sub_matches < 2) {
311 cislog_error(
"deserialize_ply: Could not locate number of vertices in ply header.");
314 value_size = eind[1] - sind[1];
315 memcpy(value, buf + sind[1], value_size);
316 value[value_size] =
'\0';
321 n_sub_matches = find_matches(
"element face ([[:digit:]]+)\n", buf, &sind, &eind);
322 if (n_sub_matches < 2) {
323 cislog_error(
"deserialize_ply: Could not locate number of faces in ply header.");
326 value_size = eind[1] - sind[1];
327 memcpy(value, buf + sind[1], value_size);
328 value[value_size] =
'\0';
333 n_sub_matches = find_matches(
"green", buf, &sind, &eind);
334 if (n_sub_matches != 0) {
340 n_sub_matches = find_matches(
"end_header\n", buf, &sind, &eind);
341 if (n_sub_matches < 1) {
342 cislog_error(
"deserialize_ply: Could not locate end of header.");
345 begin_body = eind[0];
350 int nlines_expected = nvert + nface;
352 sind_body = (
size_t*)realloc(sind_body, (nlines_expected+1)*
sizeof(size_t));
353 eind_body = (
size_t*)realloc(eind_body, (nlines_expected+1)*
sizeof(size_t));
354 size_t cur_pos = begin_body;
356 n_sub_matches = find_matches(
"([^\n]*)\n", buf + cur_pos, &sind, &eind);
357 if (n_sub_matches < 2) {
360 if (nlines > nlines_expected) {
366 sind_body[nlines] = cur_pos + sind[1];
367 eind_body[nlines] = cur_pos + eind[1];
369 cur_pos = cur_pos + eind[0];
371 if ((nvert + nface) > nlines) {
372 cislog_error(
"deserialize_ply: Not enough lines (%d) for %d vertices " 374 nlines, nvert, nface);
380 int ret = alloc_ply(p, nvert, nface, do_colors);
382 cislog_error(
"deserialize_ply: Error allocating ply structure.");
392 char vert_re[80] =
"([^ ]+) ([^ ]+) ([^ ]+)";
394 for (i = 0; i < p->
nvert; i++) {
396 line_size = eind_body[line_no] - sind_body[line_no];
397 memcpy(iline, buf + sind_body[line_no], line_size);
398 iline[line_size] =
'\0';
399 n_sub_matches = find_matches(vert_re, iline, &sind, &eind);
400 if (n_sub_matches != nexpected + 1) {
401 cislog_error(
"deserialize_ply: Vertex should contain %d entries. " 402 "%d were found", nexpected, n_sub_matches - 1);
406 for (j = 0; j < 3; j++) {
407 p->
vertices[i][j] = atof(iline + sind[j + 1]);
410 for (j = 0; j < 3; j++) {
421 for (i = 0; i < p->
nface; i++) {
422 line_no = i + p->
nvert;
423 line_size = eind_body[line_no] - sind_body[line_no];
424 memcpy(iline, buf + sind_body[line_no], line_size);
425 iline[line_size] =
'\0';
426 nexpected = atoi(iline);
428 char face_re[80] =
"([^ ]+)";
429 for (j = 0; j < nexpected; j++) {
430 strcat(face_re,
" ([^ ]+)");
432 n_sub_matches = find_matches(face_re, iline, &sind, &eind);
433 if (n_sub_matches < (nexpected + 2)) {
434 cislog_error(
"deserialize_ply: Face should contain %d entries. " 435 "%d were found.", nexpected, n_sub_matches - 2);
439 int *iface = (
int*)realloc(p->
faces[i], nexpected*
sizeof(
int));
441 cislog_error(
"deserialize_ply: Could not allocate face %d.", i);
446 for (j = 0; j < nexpected; j++) {
447 p->
faces[i][j] = atoi(iline + sind[j + 2]);
454 if (sind != NULL) free(sind);
455 if (eind != NULL) free(eind);
456 if (sind_body != NULL) free(sind_body);
457 if (eind_body != NULL) free(eind_body);
int ** faces
Indices of the vertices composing each face.
Definition: PlySerialize.h:15
Serializer structure.
Definition: SerializeBase.h:19
int nface
Number faces.
Definition: PlySerialize.h:13
int ** vertex_colors
RGB colors of each vertex.
Definition: PlySerialize.h:16
int * nvert_in_face
Number of vertices in each face.
Definition: PlySerialize.h:17
Ply structure.
Definition: PlySerialize.h:11
int nvert
Number of vertices.
Definition: PlySerialize.h:12
float ** vertices
X, Y, Z positions of vertices.
Definition: PlySerialize.h:14