From 81e8aa0552b102acb4fdc68889957c5dce1254f7 Mon Sep 17 00:00:00 2001 From: hasufell Date: Sat, 31 May 2014 17:32:40 +0200 Subject: [PATCH] Allow to read bezier curves --- src/half_edge.c | 4 ++++ src/half_edge.h | 35 ++++++++++++++++++++++++++- src/half_edge_AS.c | 59 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/src/half_edge.c b/src/half_edge.c index 2fbd0d0..79534d9 100644 --- a/src/half_edge.c +++ b/src/half_edge.c @@ -290,7 +290,11 @@ void delete_object(HE_obj *obj) free(obj->vertices[i].vec); free(obj->vertices[i].col); } + for (uint32_t i = 0; i < obj->bzc; i++) + free(obj->bez_curves[i].vec); + free(obj->edges); free(obj->vertices); free(obj->faces); + free(obj->bez_curves); } diff --git a/src/half_edge.h b/src/half_edge.h index 228141d..e7aac38 100644 --- a/src/half_edge.h +++ b/src/half_edge.h @@ -90,18 +90,25 @@ typedef struct obj_items obj_items; * as they are in the .obj file, */ typedef double** VERTICES; -typedef struct FACES FACES; /** * 2d array which can hold the vertices texture * coordinates as they are in the .obj file, */ typedef double** V_TEXTURES; +/** + * 2d array which holds the array or vertice references + * that describe a bezier curve. + */ +typedef int** BEZIER_CURV; + +typedef struct FACES FACES; typedef struct HE_edge HE_edge; typedef struct HE_vert HE_vert; typedef struct HE_vert_acc HE_vert_acc; typedef struct HE_face HE_face; typedef struct HE_obj HE_obj; typedef struct color color; +typedef struct bez_curv bez_curv; /** @@ -123,6 +130,10 @@ struct obj_items { * Raw texture coordinates array. */ V_TEXTURES vt; + /** + * Bezier curve + */ + BEZIER_CURV bez; }; /** @@ -253,6 +264,10 @@ struct HE_obj { * Array of faces. */ HE_face *faces; + /** + * Array of bezier curves. + */ + bez_curv *bez_curves; /** * Count of edges. */ @@ -261,6 +276,10 @@ struct HE_obj { * Count of vertices. */ uint32_t vc; + /** + * Count of bez curves. + */ + uint32_t bzc; /** * Count of faces. */ @@ -284,6 +303,20 @@ struct color { double blue; }; +/** + * Bezier Curve. + */ +struct bez_curv { + /** + * Array of vectors. + */ + vector *vec; + /** + * Degree of the curve. + */ + uint32_t deg; +}; + bool face_normal(HE_edge const * const edge, vector *vec); diff --git a/src/half_edge_AS.c b/src/half_edge_AS.c index 964fb66..64ca5bf 100644 --- a/src/half_edge_AS.c +++ b/src/half_edge_AS.c @@ -49,7 +49,8 @@ static void delete_accel_struct(HE_obj *he_obj); static void delete_raw_object(obj_items *raw_obj, uint32_t fc, uint32_t vc, - uint32_t vt); + uint32_t vt, + uint32_t bzc); /** @@ -83,12 +84,13 @@ static bool assemble_obj_arrays(char const * const obj_string, *str_tmp_ptr = NULL; /* these will be assigned later to the out structs */ - uint32_t vc = 0, fc = 0, ec = 0, vtc = 0; + uint32_t vc = 0, fc = 0, ec = 0, vtc = 0, bzc = 0; VERTICES obj_v = NULL; FACES *obj_f = malloc(sizeof(*obj_f)); uint32_t **obj_f_v = NULL; /* tmp v member of obj_f */ uint32_t **obj_f_vt = NULL; /* tmp vt member of obj_f */ V_TEXTURES obj_vt = NULL; + BEZIER_CURV bez = NULL; if (!obj_string || !raw_obj) return false; @@ -190,6 +192,26 @@ static bool assemble_obj_arrays(char const * const obj_string, } fc++; obj_f_v[fc] = NULL; /* trailing NULL pointer */ + + /* + * Bezier Curve + */ + } else if (!strcmp(str_tmp_ptr, "curv")) { + char *myint = NULL; + uint8_t i = 0; + + REALLOC(bez, sizeof(*bez) * (bzc + 2)); + bez[bzc] = NULL; + while ((myint = strtok_r(NULL, " ", &str_ptr_space))) { + i++; + + REALLOC(bez[bzc], + sizeof(**bez) * (i + 1)); + bez[bzc][i - 1] = atoi(myint); + bez[bzc][i] = 0; + } + bzc++; + bez[bzc] = NULL; /* trailing NULL pointer */ } str_tmp_ptr = strtok_r(NULL, "\n", &str_ptr_newline); @@ -205,6 +227,7 @@ static bool assemble_obj_arrays(char const * const obj_string, obj_f->vt = obj_f_vt; raw_obj->f = obj_f; raw_obj->vt = obj_vt; + raw_obj->bez = bez; /* cleanup */ free(string); @@ -228,12 +251,14 @@ static bool assemble_obj_arrays(char const * const obj_string, static void assemble_HE_stage1(obj_items const * const raw_obj, HE_obj *he_obj) { - uint32_t vc = 0; + uint32_t vc = 0, + bzc = 0; uint8_t const xpos = 0; uint8_t const ypos = 1; uint8_t const zpos = 2; int8_t default_color = -1; HE_vert *vertices = he_obj->vertices; + bez_curv *bez_curves = NULL; while (raw_obj->v[vc]) { vector *tmp_vec; @@ -264,6 +289,25 @@ static void assemble_HE_stage1(obj_items const * const raw_obj, vc++; } + while (raw_obj->bez[bzc]) { + uint32_t i = 0; + vector *bez_vec = NULL; + + REALLOC(bez_curves, sizeof(*bez_curves) * (bzc + 2)); + + while (raw_obj->bez[bzc][i]) { + REALLOC(bez_vec, sizeof(vector) * (i + 1)); + bez_vec[i] = *(vertices[raw_obj->bez[bzc][i] - 1].vec); + i++; + } + + bez_curves[bzc].vec = bez_vec; + bez_curves[bzc].deg = i - 1; /* i is length */ + bzc++; + } + + he_obj->bez_curves = bez_curves; + he_obj->bzc = bzc; he_obj->vertices = vertices; } @@ -476,7 +520,8 @@ HE_obj *parse_obj(char const * const obj_string) assemble_HE_stage3(he_obj); /* cleanup */ - delete_raw_object(&raw_obj, he_obj->fc, he_obj->vc, he_obj->vtc); + delete_raw_object(&raw_obj, he_obj->fc, + he_obj->vc, he_obj->vtc, he_obj->bzc); delete_accel_struct(he_obj); free(string); @@ -503,11 +548,14 @@ static void delete_accel_struct(HE_obj *he_obj) static void delete_raw_object(obj_items *raw_obj, uint32_t fc, uint32_t vc, - uint32_t vtc) + uint32_t vtc, + uint32_t bzc) { if (!raw_obj) return; + for (uint32_t i = 0; i < bzc; i++) + free(raw_obj->bez[i]); for (uint32_t i = 0; i < vtc; i++) free(raw_obj->vt[i]); for (uint32_t i = 0; i < vc; i++) @@ -516,6 +564,7 @@ static void delete_raw_object(obj_items *raw_obj, free(raw_obj->f->v[i]); free(raw_obj->f->vt[i]); } + free(raw_obj->bez); free(raw_obj->f->v); free(raw_obj->f->vt); free(raw_obj->v);