Cleanup in spline diffirintiation code - encapsulate max segment length

to resolution calculation functions rather than doing this from callee.

Also switch mask drawing code do the same spline resolution calculation
as used for compositor (which takes frame dimensions into account).
This commit is contained in:
Sergey Sharybin
2012-06-01 12:25:18 +00:00
parent ecac5aef75
commit 1450bc244f
4 changed files with 81 additions and 36 deletions

View File

@@ -57,10 +57,14 @@ void BKE_mask_layer_unique_name(struct Mask *mask, struct MaskLayer *masklay);
/* splines */
struct MaskSpline *BKE_mask_spline_add(struct MaskLayer *masklay);
int BKE_mask_spline_resolution(struct MaskSpline *spline, float max_seg_len);
float (*BKE_mask_spline_differentiate(struct MaskSpline *spline, int *tot_diff_point, int dynamic_res, float max_dseg_len))[2];
float (*BKE_mask_spline_feather_differentiated_points(struct MaskSpline *spline, int *tot_feather_point, int dynamic_res, float max_dseg_len))[2];
float (*BKE_mask_spline_differentiate(struct MaskSpline *spline, int *tot_diff_point))[2];
float (*BKE_mask_spline_feather_differentiated_points(struct MaskSpline *spline, int *tot_feather_point))[2];
float (*BKE_mask_spline_differentiate_with_resolution(struct MaskSpline *spline, int width, int height, int *tot_diff_point))[2];
float (*BKE_mask_spline_feather_differentiated_points_with_resolution(struct MaskSpline *spline,
int width, int height, int *tot_feather_point))[2];
float (*BKE_mask_spline_feather_points(struct MaskSpline *spline, int *tot_feather_point))[2];
/* point */
@@ -68,9 +72,18 @@ int BKE_mask_point_has_handle(struct MaskSplinePoint *point);
void BKE_mask_point_handle(struct MaskSplinePoint *point, float handle[2]);
void BKE_mask_point_set_handle(struct MaskSplinePoint *point, float loc[2], int keep_direction,
float orig_handle[2], float orig_vec[3][3]);
float *BKE_mask_point_segment_diff(struct MaskSpline *spline, struct MaskSplinePoint *point, int *tot_diff_point);
float *BKE_mask_point_segment_feather_diff(struct MaskSpline *spline, struct MaskSplinePoint *point,
int *tot_feather_point);
float *BKE_mask_point_segment_diff_with_resolution(struct MaskSpline *spline, struct MaskSplinePoint *point,
int width, int height, int *tot_diff_point);
float *BKE_mask_point_segment_feather_diff_with_resolution(struct MaskSpline *spline, struct MaskSplinePoint *point,
int width, int height,
int *tot_feather_point);
void BKE_mask_point_segment_co(struct MaskSpline *spline, struct MaskSplinePoint *point, float u, float co[2]);
void BKE_mask_point_normal(struct MaskSpline *spline, struct MaskSplinePoint *point,
float u, float n[2]);

View File

@@ -149,11 +149,18 @@ MaskSpline *BKE_mask_spline_add(MaskLayer *masklay)
return spline;
}
int BKE_mask_spline_resolution(MaskSpline *spline, float max_seg_len)
static int BKE_mask_spline_resolution(MaskSpline *spline, int width, int height)
{
const float max_segment = max_seg_len;
float max_segment = 0.01f;
int i, resol = 1;
if (width != 0 && height != 0) {
if (width >= height)
max_segment = 1.0f / (float) width;
else
max_segment = 1.0f / (float) height;
}
for (i = 0; i < spline->tot_point; i++) {
MaskSplinePoint *point = &spline->points[i];
MaskSplinePoint *next_point;
@@ -186,10 +193,10 @@ int BKE_mask_spline_resolution(MaskSpline *spline, float max_seg_len)
return resol;
}
int BKE_mask_spline_feather_resolution(MaskSpline *spline, float max_seg_len)
static int BKE_mask_spline_feather_resolution(MaskSpline *spline, int width, int height)
{
const float max_segment = 0.005;
int resol = BKE_mask_spline_resolution(spline, max_seg_len);
int resol = BKE_mask_spline_resolution(spline, width, height);
float max_jump = 0.0f;
int i;
@@ -216,18 +223,13 @@ int BKE_mask_spline_feather_resolution(MaskSpline *spline, float max_seg_len)
return resol;
}
float (*BKE_mask_spline_differentiate(MaskSpline *spline, int *tot_diff_point, int dynamic_res, float max_dseg_len))[2]
float (*BKE_mask_spline_differentiate_with_resolution(MaskSpline *spline, int width, int height, int *tot_diff_point))[2]
{
MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
MaskSplinePoint *point, *prev;
float (*diff_points)[2], (*fp)[2];
int a, len, resol;
if (!dynamic_res) {
max_dseg_len = 0.01f;
}
resol = BKE_mask_spline_resolution(spline, max_dseg_len);
int a, len, resol = BKE_mask_spline_resolution(spline, width, height);
if (spline->tot_point <= 1) {
/* nothing to differentiate */
@@ -284,16 +286,18 @@ float (*BKE_mask_spline_differentiate(MaskSpline *spline, int *tot_diff_point, i
return diff_points;
}
float (*BKE_mask_spline_feather_differentiated_points(MaskSpline * spline, int *tot_feather_point, int dynamic_res, float max_dseg_len))[2]
float (*BKE_mask_spline_differentiate(MaskSpline *spline, int *tot_diff_point))[2]
{
return BKE_mask_spline_differentiate_with_resolution(spline, 0, 0, tot_diff_point);
}
float (*BKE_mask_spline_feather_differentiated_points_with_resolution(MaskSpline *spline, int width, int height,
int *tot_feather_point))[2]
{
MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
float (*feather)[2], (*fp)[2];
int i, j, tot, resol;
if (!dynamic_res) {
max_dseg_len = 0.005f;
}
resol = BKE_mask_spline_feather_resolution(spline, max_dseg_len);
int i, j, tot, resol = BKE_mask_spline_feather_resolution(spline, width, height);
tot = resol * spline->tot_point;
feather = fp = MEM_mallocN(tot * sizeof(*feather), "mask spline feather diff points");
@@ -320,6 +324,11 @@ float (*BKE_mask_spline_feather_differentiated_points(MaskSpline * spline, int *
return feather;
}
float (*BKE_mask_spline_feather_differentiated_points(MaskSpline * spline, int *tot_feather_point))[2]
{
return BKE_mask_spline_feather_differentiated_points_with_resolution(spline, 0, 0, tot_feather_point);
}
float (*BKE_mask_spline_feather_points(MaskSpline * spline, int *tot_feather_point))[2]
{
MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
@@ -424,10 +433,12 @@ void BKE_mask_point_set_handle(MaskSplinePoint *point, float loc[2], int keep_di
}
}
float *BKE_mask_point_segment_feather_diff(MaskSpline *spline, MaskSplinePoint *point, int *tot_feather_point)
float *BKE_mask_point_segment_feather_diff_with_resolution(MaskSpline *spline, MaskSplinePoint *point,
int width, int height,
int *tot_feather_point)
{
float *feather, *fp;
int i, resol = BKE_mask_spline_feather_resolution(spline, 0.005f);
int i, resol = BKE_mask_spline_feather_resolution(spline, width, height);
feather = fp = MEM_callocN(2 * resol * sizeof(float), "mask point spline feather diff points");
@@ -448,13 +459,18 @@ float *BKE_mask_point_segment_feather_diff(MaskSpline *spline, MaskSplinePoint *
return feather;
}
float *BKE_mask_point_segment_diff(MaskSpline *spline, MaskSplinePoint *point, int *tot_diff_point)
float *BKE_mask_point_segment_feather_diff(MaskSpline *spline, MaskSplinePoint *point, int *tot_feather_point)
{
return BKE_mask_point_segment_feather_diff_with_resolution(spline, point, 0, 0, tot_feather_point);
}
float *BKE_mask_point_segment_diff_with_resolution(MaskSpline *spline, MaskSplinePoint *point, int width, int height, int *tot_diff_point)
{
MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
BezTriple *bezt, *next;
float *diff_points, *fp;
int j, resol = BKE_mask_spline_resolution(spline, 0.01f);
int j, resol = BKE_mask_spline_resolution(spline, width, height);
bezt = &point->bezt;
@@ -484,6 +500,11 @@ float *BKE_mask_point_segment_diff(MaskSpline *spline, MaskSplinePoint *point, i
return diff_points;
}
float *BKE_mask_point_segment_diff(MaskSpline *spline, MaskSplinePoint *point, int *tot_diff_point)
{
return BKE_mask_point_segment_diff_with_resolution(spline, point, 0, 0, tot_diff_point);
}
void BKE_mask_point_segment_co(MaskSpline *spline, MaskSplinePoint *point, float u, float co[2])
{
MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
@@ -1816,9 +1837,11 @@ void BKE_mask_rasterize(Mask *mask, int width, int height, float *buffer)
float (*diff_feather_points)[2];
int tot_diff_feather_points;
diff_points = BKE_mask_spline_differentiate(spline, &tot_diff_point, 1, max_dseg_len);
diff_points = BKE_mask_spline_differentiate_with_resolution(spline, width, height, &tot_diff_point);
if (tot_diff_point) {
diff_feather_points = BKE_mask_spline_feather_differentiated_points(spline, &tot_diff_feather_points, 1, max_dseg_len);
diff_feather_points =
BKE_mask_spline_feather_differentiated_points_with_resolution(spline, width, height,
&tot_diff_feather_points);
}
/* TODO, make this optional! */

View File

@@ -323,7 +323,8 @@ static void mask_draw_curve_type(MaskSpline *spline, float (*points)[2], int tot
}
static void draw_spline_curve(MaskLayer *masklay, MaskSpline *spline,
const char draw_flag, const char draw_type)
const char draw_flag, const char draw_type,
int width, int height)
{
unsigned char rgb_tmp[4];
@@ -336,7 +337,7 @@ static void draw_spline_curve(MaskLayer *masklay, MaskSpline *spline,
int tot_feather_point;
float (*feather_points)[2];
diff_points = BKE_mask_spline_differentiate(spline, &tot_diff_point, 0, 0.0f);
diff_points = BKE_mask_spline_differentiate_with_resolution(spline, width, height, &tot_diff_point);
if (!diff_points)
return;
@@ -347,7 +348,7 @@ static void draw_spline_curve(MaskLayer *masklay, MaskSpline *spline,
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
feather_points = BKE_mask_spline_feather_differentiated_points(spline, &tot_feather_point, 0, 0.0f);
feather_points = BKE_mask_spline_feather_differentiated_points_with_resolution(spline, width, height, &tot_feather_point);
/* draw feather */
mask_spline_feather_color_get(masklay, spline, is_spline_sel, rgb_tmp);
@@ -371,8 +372,8 @@ static void draw_spline_curve(MaskLayer *masklay, MaskSpline *spline,
(void)draw_type;
}
static void draw_masklays(Mask *mask,
const char draw_flag, const char draw_type)
static void draw_masklays(Mask *mask, const char draw_flag, const char draw_type,
int width, int height)
{
MaskLayer *masklay;
@@ -386,7 +387,7 @@ static void draw_masklays(Mask *mask,
for (spline = masklay->splines.first; spline; spline = spline->next) {
/* draw curve itself first... */
draw_spline_curve(masklay, spline, draw_flag, draw_type);
draw_spline_curve(masklay, spline, draw_flag, draw_type, width, height);
// draw_spline_parents(masklay, spline);
@@ -400,7 +401,7 @@ static void draw_masklays(Mask *mask,
void *back = spline->points_deform;
spline->points_deform = NULL;
draw_spline_curve(masklay, spline, draw_flag, draw_type);
draw_spline_curve(masklay, spline, draw_flag, draw_type, width, height);
// draw_spline_parents(masklay, spline);
draw_spline_points(masklay, spline);
spline->points_deform = back;
@@ -413,9 +414,15 @@ void ED_mask_draw(const bContext *C,
const char draw_flag, const char draw_type)
{
Mask *mask = CTX_data_edit_mask(C);
int width, height;
if (!mask)
return;
draw_masklays(mask, draw_flag, draw_type);
/* TODO: for now, in the future better to make sure all utility functions
* are using const specifier for non-changing pointers
*/
ED_mask_size((bContext *)C, &width, &height);
draw_masklays(mask, draw_flag, draw_type, width, height);
}

View File

@@ -348,14 +348,16 @@ static int find_nearest_diff_point(bContext *C, Mask *mask, const float normal_c
float *diff_points;
int tot_diff_point;
diff_points = BKE_mask_point_segment_diff(spline, cur_point, &tot_diff_point);
diff_points = BKE_mask_point_segment_diff_with_resolution(spline, cur_point, width, height,
&tot_diff_point);
if (diff_points) {
int i, tot_feather_point, tot_point;
float *feather_points = NULL, *points;
if (feather) {
feather_points = BKE_mask_point_segment_feather_diff(spline, cur_point,
feather_points = BKE_mask_point_segment_feather_diff_with_resolution(spline, cur_point,
width, height,
&tot_feather_point);
points = feather_points;