Restructure find_center()
* return bool for success/failure * use an out-vector to store the result * fix tests
This commit is contained in:
parent
965600ea01
commit
29b8b410d6
@ -96,9 +96,12 @@ static void draw_obj(uint32_t const myxrot,
|
||||
static uint32_t xrot = 0,
|
||||
yrot = 0,
|
||||
zrot = 0;
|
||||
HE_vert *center_vert = find_center(obj);
|
||||
vector center_vert;
|
||||
float scalefactor = get_normalized_scale_factor(obj) * VISIBILITY_FACTOR;
|
||||
|
||||
if (!find_center(obj, ¢er_vert))
|
||||
return; /* TODO: better error handling */
|
||||
|
||||
xrot += myxrot;
|
||||
yrot += myyrot;
|
||||
zrot += myzrot;
|
||||
@ -116,9 +119,9 @@ static void draw_obj(uint32_t const myxrot,
|
||||
glTranslatef(0.0f, 0.0f, SYSTEM_POS_Z_BACK);
|
||||
|
||||
/* pull into middle of universe */
|
||||
glTranslatef(-center_vert->x,
|
||||
-center_vert->y,
|
||||
-center_vert->z + SYSTEM_POS_Z);
|
||||
glTranslatef(-center_vert.x,
|
||||
-center_vert.y,
|
||||
-center_vert.z + SYSTEM_POS_Z);
|
||||
|
||||
glBegin(GL_POLYGON);
|
||||
glColor3f(0.0f, 1.0f, 0.0f);
|
||||
@ -126,8 +129,6 @@ static void draw_obj(uint32_t const myxrot,
|
||||
glEnd();
|
||||
|
||||
glPopMatrix();
|
||||
|
||||
free(center_vert);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -27,7 +27,9 @@
|
||||
#include "err.h"
|
||||
#include "filereader.h"
|
||||
#include "half_edge.h"
|
||||
#include "vector.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -50,21 +52,19 @@ HE_edge **get_all_emanating_edges(HE_vert *vertice)
|
||||
* in a HE_vert struct.
|
||||
*
|
||||
* @param obj the object we want to find the center of
|
||||
* @return newly allocated HE_vert with empty edge member
|
||||
* and coordinates which represent the middle of the object
|
||||
* @param vec the vector to store the result in [out]
|
||||
* @return true/false for success/failure
|
||||
*/
|
||||
HE_vert *find_center(HE_obj const * const obj)
|
||||
bool find_center(HE_obj const * const obj, vector *vec)
|
||||
{
|
||||
float x = 0,
|
||||
y = 0,
|
||||
z = 0;
|
||||
uint32_t i;
|
||||
HE_vert *newvert;
|
||||
|
||||
if (!obj)
|
||||
return NULL;
|
||||
if (!obj || !vec)
|
||||
return false;
|
||||
|
||||
newvert = malloc(sizeof(HE_vert));
|
||||
|
||||
for (i = 0; i < obj->vc; i++) {
|
||||
x += obj->vertices[i].x;
|
||||
@ -72,12 +72,11 @@ HE_vert *find_center(HE_obj const * const obj)
|
||||
z += obj->vertices[i].z;
|
||||
}
|
||||
|
||||
newvert->x = x / i;
|
||||
newvert->y = y / i;
|
||||
newvert->z = z / i;
|
||||
newvert->edge = NULL;
|
||||
vec->x = x / i;
|
||||
vec->y = y / i;
|
||||
vec->z = z / i;
|
||||
|
||||
return newvert;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -28,6 +28,9 @@
|
||||
#define _DROW_ENGINE_HE_OPERATIONS_H
|
||||
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
@ -124,7 +127,7 @@ struct HE_obj {
|
||||
|
||||
|
||||
HE_edge **get_all_emanating_edges(HE_vert *vertice);
|
||||
HE_vert *find_center(HE_obj const * const obj);
|
||||
bool find_center(HE_obj const * const obj, vector *vec);
|
||||
float get_normalized_scale_factor(HE_obj const * const obj);
|
||||
HE_obj *parse_obj(char const * const filename);
|
||||
|
||||
|
@ -106,6 +106,8 @@ int main(void)
|
||||
test_find_center1)) ||
|
||||
(NULL == CU_add_test(pSuite, "test2 finding center ob obj",
|
||||
test_find_center2)) ||
|
||||
(NULL == CU_add_test(pSuite, "test3 finding center ob obj",
|
||||
test_find_center3)) ||
|
||||
(NULL == CU_add_test(pSuite, "test1 getting normalized scale factor",
|
||||
test_get_normalized_scale_factor1)) ||
|
||||
(NULL == CU_add_test(pSuite, "test2 getting normalized scale factor",
|
||||
|
@ -45,6 +45,7 @@ void test_parse_obj4(void);
|
||||
|
||||
void test_find_center1(void);
|
||||
void test_find_center2(void);
|
||||
void test_find_center3(void);
|
||||
|
||||
void test_get_normalized_scale_factor1(void);
|
||||
void test_get_normalized_scale_factor2(void);
|
||||
|
@ -457,13 +457,15 @@ void test_find_center1(void)
|
||||
"f 7 1 3 5\n";
|
||||
|
||||
HE_obj *obj = parse_obj(string);
|
||||
HE_vert *newvert = find_center(obj);
|
||||
vector newvec;
|
||||
bool retval = find_center(obj, &newvec);
|
||||
|
||||
CU_ASSERT_PTR_NOT_NULL(obj);
|
||||
CU_ASSERT_EQUAL(retval, true);
|
||||
|
||||
CU_ASSERT_EQUAL(newvert->x, 10.0);
|
||||
CU_ASSERT_EQUAL(newvert->y, 10.5);
|
||||
CU_ASSERT_EQUAL(newvert->z, 10.0);
|
||||
CU_ASSERT_EQUAL(newvec.x, 10.0);
|
||||
CU_ASSERT_EQUAL(newvec.y, 10.5);
|
||||
CU_ASSERT_EQUAL(newvec.z, 10.0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -471,9 +473,37 @@ void test_find_center1(void)
|
||||
*/
|
||||
void test_find_center2(void)
|
||||
{
|
||||
HE_vert *newvert = find_center(NULL);
|
||||
vector newvec;
|
||||
bool retval = find_center(NULL, &newvec);
|
||||
|
||||
CU_ASSERT_PTR_NULL(newvert);
|
||||
CU_ASSERT_EQUAL(retval, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test error handling by passing a NULL pointer.
|
||||
*/
|
||||
void test_find_center3(void)
|
||||
{
|
||||
char const * const string = ""
|
||||
"v 9.0 10.0 11.0\n"
|
||||
"v 11.0 10.0 11.0\n"
|
||||
"v 9.0 11.0 11.0\n"
|
||||
"v 11.0 11.0 11.0\n"
|
||||
"v 9.0 11.0 9.0\n"
|
||||
"v 11.0 11.0 9.0\n"
|
||||
"v 9.0 10.0 9.0\n"
|
||||
"v 11.0 10.0 9.0\n"
|
||||
"f 1 2 4 3\n"
|
||||
"f 3 4 6 5\n"
|
||||
"f 5 6 8 7\n"
|
||||
"f 7 8 2 1\n"
|
||||
"f 2 8 6 4\n"
|
||||
"f 7 1 3 5\n";
|
||||
|
||||
HE_obj *obj = parse_obj(string);
|
||||
bool retval = find_center(obj, NULL);
|
||||
|
||||
CU_ASSERT_EQUAL(retval, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -499,7 +529,6 @@ void test_get_normalized_scale_factor1(void)
|
||||
"f 7 1 3 5\n";
|
||||
|
||||
HE_obj *obj = parse_obj(string);
|
||||
HE_vert *newvert = find_center(obj);
|
||||
float factor = get_normalized_scale_factor(obj);
|
||||
|
||||
CU_ASSERT_PTR_NOT_NULL(obj);
|
||||
|
Loading…
Reference in New Issue
Block a user