Make bezier calculation more modular

This commit is contained in:
hasufell 2014-06-01 16:17:51 +02:00
parent ea8d08204c
commit 62e02582a4
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
2 changed files with 67 additions and 16 deletions

View File

@ -30,39 +30,89 @@
#include <stdlib.h>
/*
* static function declarations
*/
static bool get_section_vec(vector *a, vector *b, vector *c, float section);
static bool get_section_vec(vector *a, vector *b, vector *c, float section)
{
vector a_tmp,
b_tmp;
if (!a || !b || !c)
return false;
copy_vector(a, &a_tmp);
copy_vector(b, &b_tmp);
set_null_vector(c);
SUB_VECTORS(&a_tmp, &b_tmp, c);
VECTOR_LEN_SCAL_MUL(c, section, c);
ADD_VECTORS(c, b, c);
return true;
}
/**
* Get the reduced bezier curve which is of one degree less
* and strained between the defined sections of the old curve.
*
* @param bez the bezier curve the calcluate the next from
* @param new_bez the new bezier curve, with a newly
* allocated vector member [out]
* @param section the section which will be applied to all
* lines between the bezier vertices
* @return true/false for success/failure
*/
bool get_reduced_bez_curv(bez_curv *bez, bez_curv *new_bez, float section)
{
vector *vec_arr = malloc(sizeof(*vec_arr) * bez->deg);
for (uint32_t i = 0; i < bez->deg; i++) {
vector new_vec;
get_section_vec(&(bez->vec[i + 1]),
&(bez->vec[i]),
&new_vec,
section);
vec_arr[i] = new_vec;
}
new_bez->vec = vec_arr;
new_bez->deg = bez->deg - 1;
return new_bez;
}
/**
* Calculate a point on the bezier curve according to the
* bezier vertices. If section is set to 0.5 then it will
* return the vector to the point in the middle of the curve.
*
* @param obj the object holding the bezier vertices information
* @param bez the bezier curve the calcluate the point from
* @param section the section which will be applied to all
* lines between the bezier vertices
* @return the vector to the calculated point
* @return the vector to the calculated point, newly allocated
*/
vector *calculate_bezier_point(bez_curv *bez, float section)
{
vector vec_arr[bez->deg];
bez_curv new_bez;
bez_curv new_bez = { NULL, 0 };
for (uint32_t i = 0; i < bez->deg; i++) {
vector new_vec;
free(new_bez.vec);
SUB_VECTORS(&(bez->vec[i + 1]), &(bez->vec[i]), &new_vec);
VECTOR_LEN_SCAL_MUL(&new_vec, section, &new_vec);
ADD_VECTORS(&new_vec, &(bez->vec[i]), &new_vec);
vec_arr[i] = new_vec;
}
new_bez.vec = vec_arr;
new_bez.deg = bez->deg - 1;
if (!get_reduced_bez_curv(bez, &new_bez, section))
return NULL;
if (new_bez.deg > 0) {
return calculate_bezier_point(&new_bez, section);
} else {
vector *result_vector = malloc(sizeof(*result_vector));
*result_vector = vec_arr[0];
*result_vector = new_bez.vec[0];
return result_vector;
}
}

View File

@ -49,6 +49,7 @@ struct bez_curv {
};
bool get_reduced_bez_curv(bez_curv *bez, bez_curv *new_bez, float section);
vector *calculate_bezier_point(bez_curv *bez, float section);