Make bezier calculation more modular
This commit is contained in:
parent
ea8d08204c
commit
62e02582a4
82
src/bezier.c
82
src/bezier.c
@ -30,39 +30,89 @@
|
|||||||
#include <stdlib.h>
|
#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
|
* Calculate a point on the bezier curve according to the
|
||||||
* bezier vertices. If section is set to 0.5 then it will
|
* bezier vertices. If section is set to 0.5 then it will
|
||||||
* return the vector to the point in the middle of the curve.
|
* 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
|
* @param section the section which will be applied to all
|
||||||
* lines between the bezier vertices
|
* 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 *calculate_bezier_point(bez_curv *bez, float section)
|
||||||
{
|
{
|
||||||
vector vec_arr[bez->deg];
|
bez_curv new_bez = { NULL, 0 };
|
||||||
bez_curv new_bez;
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < bez->deg; i++) {
|
free(new_bez.vec);
|
||||||
vector new_vec;
|
|
||||||
|
|
||||||
SUB_VECTORS(&(bez->vec[i + 1]), &(bez->vec[i]), &new_vec);
|
if (!get_reduced_bez_curv(bez, &new_bez, section))
|
||||||
VECTOR_LEN_SCAL_MUL(&new_vec, section, &new_vec);
|
return NULL;
|
||||||
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 (new_bez.deg > 0) {
|
if (new_bez.deg > 0) {
|
||||||
return calculate_bezier_point(&new_bez, section);
|
return calculate_bezier_point(&new_bez, section);
|
||||||
} else {
|
} else {
|
||||||
vector *result_vector = malloc(sizeof(*result_vector));
|
vector *result_vector = malloc(sizeof(*result_vector));
|
||||||
*result_vector = vec_arr[0];
|
*result_vector = new_bez.vec[0];
|
||||||
|
|
||||||
return result_vector;
|
return result_vector;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
vector *calculate_bezier_point(bez_curv *bez, float section);
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user