More error handling improvements
This commit is contained in:
parent
a1d3c3947e
commit
5b53950def
@ -48,9 +48,13 @@ HE_obj *read_obj_file(char const * const filename)
|
|||||||
char *string = NULL; /* file content */
|
char *string = NULL; /* file content */
|
||||||
HE_obj *obj = NULL;
|
HE_obj *obj = NULL;
|
||||||
|
|
||||||
|
if (!filename || !*filename)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
/* read the whole file into string */
|
/* read the whole file into string */
|
||||||
string = read_file(filename);
|
string = read_file(filename);
|
||||||
if (!filename || !*filename || !string || !*string)
|
|
||||||
|
if (!string)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
obj = parse_obj(string);
|
obj = parse_obj(string);
|
||||||
|
@ -351,7 +351,7 @@ void init(char const * const filename)
|
|||||||
if (!obj)
|
if (!obj)
|
||||||
ABORT("Failed to read object file \"%s\"!", filename);
|
ABORT("Failed to read object file \"%s\"!", filename);
|
||||||
|
|
||||||
normalize_object(obj);
|
NORMALIZE_OBJECT(obj);
|
||||||
|
|
||||||
day = 0;
|
day = 0;
|
||||||
year = 0;
|
year = 0;
|
||||||
|
@ -77,7 +77,7 @@ static bool get_all_emanating_edges(HE_vert const * const vert,
|
|||||||
HE_edge **edge_array;
|
HE_edge **edge_array;
|
||||||
HE_edge **tmp_ptr;
|
HE_edge **tmp_ptr;
|
||||||
|
|
||||||
if (!vert)
|
if (!edge_array_out || !vert || !ec_out)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
edge_array = malloc(sizeof(HE_edge*) * approx_ec);
|
edge_array = malloc(sizeof(HE_edge*) * approx_ec);
|
||||||
@ -254,9 +254,13 @@ float get_normalized_scale_factor(HE_obj const * const obj)
|
|||||||
*
|
*
|
||||||
* @param obj the object we want to scale [mod]
|
* @param obj the object we want to scale [mod]
|
||||||
*/
|
*/
|
||||||
void normalize_object(HE_obj *obj)
|
bool normalize_object(HE_obj *obj)
|
||||||
{
|
{
|
||||||
float scale_factor;
|
float scale_factor;
|
||||||
|
|
||||||
|
if (!obj)
|
||||||
|
return false;
|
||||||
|
|
||||||
scale_factor = get_normalized_scale_factor(obj);
|
scale_factor = get_normalized_scale_factor(obj);
|
||||||
|
|
||||||
for (uint32_t i = 0; i < obj->vc; i++) {
|
for (uint32_t i = 0; i < obj->vc; i++) {
|
||||||
@ -264,6 +268,8 @@ void normalize_object(HE_obj *obj)
|
|||||||
obj->vertices[i].vec->y = obj->vertices[i].vec->y * scale_factor;
|
obj->vertices[i].vec->y = obj->vertices[i].vec->y * scale_factor;
|
||||||
obj->vertices[i].vec->z = obj->vertices[i].vec->z * scale_factor;
|
obj->vertices[i].vec->z = obj->vertices[i].vec->z * scale_factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -335,7 +341,8 @@ HE_obj *parse_obj(char const * const obj_string)
|
|||||||
|
|
||||||
/* exceeds 3 dimensions, malformed vertice */
|
/* exceeds 3 dimensions, malformed vertice */
|
||||||
if (strtok_r(NULL, " ", &str_ptr_space))
|
if (strtok_r(NULL, " ", &str_ptr_space))
|
||||||
return NULL;
|
ABORT("Failure in parse_obj(),\n"
|
||||||
|
"malformed vertice, exceeds 2 dimensions!\n");
|
||||||
|
|
||||||
/* parse faces */
|
/* parse faces */
|
||||||
} else if (!strcmp(str_tmp_ptr, "f")) {
|
} else if (!strcmp(str_tmp_ptr, "f")) {
|
||||||
@ -432,6 +439,9 @@ HE_obj *parse_obj(char const * const obj_string)
|
|||||||
*/
|
*/
|
||||||
void delete_object(HE_obj *obj)
|
void delete_object(HE_obj *obj)
|
||||||
{
|
{
|
||||||
|
if (!obj)
|
||||||
|
return;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < obj->vc; i++)
|
for (uint32_t i = 0; i < obj->vc; i++)
|
||||||
free(obj->vertices[i].vec);
|
free(obj->vertices[i].vec);
|
||||||
free(obj->edges);
|
free(obj->edges);
|
||||||
|
@ -82,6 +82,18 @@
|
|||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fault intolerant macro. Will abort the program if the called
|
||||||
|
* function failed.
|
||||||
|
*/
|
||||||
|
#define NORMALIZE_OBJECT(...) \
|
||||||
|
{ \
|
||||||
|
if (!normalize_object(__VA_ARGS__)) { \
|
||||||
|
fprintf(stderr, "Failure in normalize_object()!\n"); \
|
||||||
|
abort(); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Typedef for the plain faces
|
* Typedef for the plain faces
|
||||||
@ -182,7 +194,7 @@ bool face_normal(HE_edge const * const edge,
|
|||||||
bool vec_normal(HE_vert const * const vert, vector *vec);
|
bool vec_normal(HE_vert const * const vert, vector *vec);
|
||||||
bool find_center(HE_obj const * const obj, vector *vec);
|
bool find_center(HE_obj const * const obj, vector *vec);
|
||||||
float get_normalized_scale_factor(HE_obj const * const obj);
|
float get_normalized_scale_factor(HE_obj const * const obj);
|
||||||
void normalize_object(HE_obj *obj);
|
bool normalize_object(HE_obj *obj);
|
||||||
HE_obj *parse_obj(char const * const filename);
|
HE_obj *parse_obj(char const * const filename);
|
||||||
void delete_object(HE_obj *obj);
|
void delete_object(HE_obj *obj);
|
||||||
|
|
||||||
|
15
src/print.c
15
src/print.c
@ -38,6 +38,9 @@
|
|||||||
*/
|
*/
|
||||||
void print_edges(HE_obj *obj)
|
void print_edges(HE_obj *obj)
|
||||||
{
|
{
|
||||||
|
if (!obj)
|
||||||
|
return;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < obj->ec; i++) {
|
for (uint32_t i = 0; i < obj->ec; i++) {
|
||||||
printf("edge vertices %i:\n", i);
|
printf("edge vertices %i:\n", i);
|
||||||
printf(" x: %f\n", obj->edges[i].vert->vec->x);
|
printf(" x: %f\n", obj->edges[i].vert->vec->x);
|
||||||
@ -54,6 +57,9 @@ void print_edges(HE_obj *obj)
|
|||||||
*/
|
*/
|
||||||
void print_vertices(HE_obj *obj)
|
void print_vertices(HE_obj *obj)
|
||||||
{
|
{
|
||||||
|
if (!obj)
|
||||||
|
return;
|
||||||
|
|
||||||
printf("vertices: %d\n", obj->vc);
|
printf("vertices: %d\n", obj->vc);
|
||||||
for (uint32_t i = 0; i < obj->vc; i++) {
|
for (uint32_t i = 0; i < obj->vc; i++) {
|
||||||
printf("x[%d]: %f\n", i, obj->vertices[i].vec->x);
|
printf("x[%d]: %f\n", i, obj->vertices[i].vec->x);
|
||||||
@ -71,6 +77,9 @@ void print_vertices(HE_obj *obj)
|
|||||||
*/
|
*/
|
||||||
void print_faces(HE_obj *obj)
|
void print_faces(HE_obj *obj)
|
||||||
{
|
{
|
||||||
|
if (!obj)
|
||||||
|
return;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < obj->fc; i++) {
|
for (uint32_t i = 0; i < obj->fc; i++) {
|
||||||
printf("face edge vertice %i:\n", i);
|
printf("face edge vertice %i:\n", i);
|
||||||
printf(" x: %f\n", obj->faces[i].edge->vert->vec->x);
|
printf(" x: %f\n", obj->faces[i].edge->vert->vec->x);
|
||||||
@ -89,6 +98,9 @@ void print_faces(HE_obj *obj)
|
|||||||
*/
|
*/
|
||||||
void print_plain_faces(FACE face, uint32_t fc)
|
void print_plain_faces(FACE face, uint32_t fc)
|
||||||
{
|
{
|
||||||
|
if (!face || !fc)
|
||||||
|
return;
|
||||||
|
|
||||||
printf("plain faces:\n");
|
printf("plain faces:\n");
|
||||||
for (uint32_t i = 0; i < fc - 1; i++) {
|
for (uint32_t i = 0; i < fc - 1; i++) {
|
||||||
uint32_t j = 0;
|
uint32_t j = 0;
|
||||||
@ -109,6 +121,9 @@ void print_plain_faces(FACE face, uint32_t fc)
|
|||||||
*/
|
*/
|
||||||
void print_vector(vector *vec)
|
void print_vector(vector *vec)
|
||||||
{
|
{
|
||||||
|
if (!vec)
|
||||||
|
return;
|
||||||
|
|
||||||
printf("vector:\n"
|
printf("vector:\n"
|
||||||
"x %f\n"
|
"x %f\n"
|
||||||
"y %f\n"
|
"y %f\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user