Animation: Gaussian Smooth operator for Graph Editor #105635
|
@ -330,6 +330,7 @@ class GRAPH_MT_slider(Menu):
|
|||
layout.operator("graph.blend_to_neighbor", text="Blend to Neighbor")
|
||||
layout.operator("graph.blend_to_default", text="Blend to Default Value")
|
||||
layout.operator("graph.ease", text="Ease")
|
||||
layout.operator("graph.gauss_smooth", text="Gauss Smooth")
|
||||
|
||||
|
||||
class GRAPH_MT_view_pie(Menu):
|
||||
|
|
|
@ -392,7 +392,44 @@ void blend_to_default_fcurve(PointerRNA *id_ptr, FCurve *fcu, const float factor
|
|||
move_key(&fcu->bezt[i], key_y_value);
|
||||
}
|
||||
}
|
||||
/* ---------------- */
|
||||
|
||||
void get_1d_gauss_kernel(const int filter_width, const float sigma, double *kernel)
|
||||
|
||||
{
|
||||
dr.sybren marked this conversation as resolved
Sybren A. Stüvel
commented
You could add
to document the assumptions of the code. If You could add
```c
BLI_assert(sigma > 0.0f);
BLI_assert(kernel_size > 0);
```
to document the assumptions of the code. If `sigma == 0` it'll cause some nice division issues.
|
||||
double norm = 1.0 / (M_2_SQRTPI * sigma);
|
||||
Sybren A. Stüvel
commented
I think you can remove I think you can remove `norm` from the calculations completely. Since you normalise the end result anyway, this constant factor doesn't have an impact on the final result.
|
||||
double sig_sq = 2.0 * sigma * sigma;
|
||||
Sybren A. Stüvel
commented
`const`
|
||||
double sum = 0.0;
|
||||
|
||||
for (int i = -filter_width; i <= filter_width; i++) {
|
||||
kernel[i + filter_width] = norm * exp(-i * i / sig_sq);
|
||||
sum += kernel[i + filter_width];
|
||||
}
|
||||
|
||||
/* Normalize kernel values. */
|
||||
for (int i = 0; i < filter_width * 2 + 1; i++) {
|
||||
kernel[i] = kernel[i] / sum;
|
||||
}
|
||||
}
|
||||
|
||||
void smooth_fcurve_segment(FCurve *fcu,
|
||||
FCurveSegment *segment,
|
||||
float *samples,
|
||||
const float factor,
|
||||
const int filter_order,
|
||||
dr.sybren marked this conversation as resolved
Sybren A. Stüvel
commented
```c
r_kernel[i] /= sum;
```
|
||||
double *kernel)
|
||||
{
|
||||
for (int i = segment->start_index; i < segment->start_index + segment->length; i++) {
|
||||
const int sample_index = (int)(fcu->bezt[i].vec[1][0] -
|
||||
fcu->bezt[segment->start_index].vec[1][0]) +
|
||||
filter_order;
|
||||
double filter_result = 0;
|
||||
/* Apply the kernel. */
|
||||
for (int j = -filter_order; j <= filter_order; j++) {
|
||||
filter_result += samples[sample_index + j] * kernel[filter_order + j];
|
||||
}
|
||||
dr.sybren marked this conversation as resolved
Sybren A. Stüvel
commented
`segment->start_index + segment->length` and `fcu->bezt[segment->start_index].vec[1][0]` don't change during the loop, so you can store them in a constant outside of the loop and use that instead.
|
||||
fcu->bezt[i].vec[1][1] = interpf((float)filter_result, samples[sample_index], factor);
|
||||
}
|
||||
}
|
||||
/* ---------------- */
|
||||
|
||||
void ease_fcurve_segment(FCurve *fcu, FCurveSegment *segment, const float factor)
|
||||
Sybren A. Stüvel
commented
This comment should swap with the line above it, as This comment should swap with the line above it, as `double filter_result = samples[sample_index] * kernel[0];` is already part of applying the kernel.
|
||||
|
@ -680,6 +717,16 @@ typedef struct TempFrameValCache {
|
|||
float frame, val;
|
||||
} TempFrameValCache;
|
||||
|
||||
void sample_fcurve_segment(FCurve *fcu,
|
||||
const float start_frame,
|
||||
float *samples,
|
||||
const int sample_count)
|
||||
{
|
||||
for (int i = 0; i < sample_count; i++) {
|
||||
samples[i] = evaluate_fcurve(fcu, start_frame + i);
|
||||
}
|
||||
}
|
||||
|
||||
void sample_fcurve(FCurve *fcu)
|
||||
{
|
||||
BezTriple *bezt, *start = NULL, *end = NULL;
|
||||
|
|
|
@ -424,6 +424,13 @@ void blend_to_neighbor_fcurve_segment(struct FCurve *fcu,
|
|||
struct FCurveSegment *segment,
|
||||
float factor);
|
||||
void breakdown_fcurve_segment(struct FCurve *fcu, struct FCurveSegment *segment, float factor);
|
||||
void get_1d_gauss_kernel(int filter_width, const float sigma, double *kernel);
|
||||
void smooth_fcurve_segment(struct FCurve *fcu,
|
||||
struct FCurveSegment *segment,
|
||||
float *samples,
|
||||
float factor,
|
||||
int filter_order,
|
||||
double *kernel);
|
||||
void ease_fcurve_segment(struct FCurve *fcu, struct FCurveSegment *segment, float factor);
|
||||
bool decimate_fcurve(struct bAnimListElem *ale, float remove_ratio, float error_sq_max);
|
||||
void blend_to_default_fcurve(struct PointerRNA *id_ptr, struct FCurve *fcu, float factor);
|
||||
|
@ -432,6 +439,10 @@ void blend_to_default_fcurve(struct PointerRNA *id_ptr, struct FCurve *fcu, floa
|
|||
*/
|
||||
void smooth_fcurve(struct FCurve *fcu);
|
||||
void sample_fcurve(struct FCurve *fcu);
|
||||
void sample_fcurve_segment(struct FCurve *fcu,
|
||||
float start_frame,
|
||||
float *r_samples,
|
||||
int sample_count);
|
||||
|
||||
/* ----------- */
|
||||
|
||||
|
|
|
@ -116,6 +116,7 @@ void GRAPH_OT_breakdown(struct wmOperatorType *ot);
|
|||
void GRAPH_OT_ease(struct wmOperatorType *ot);
|
||||
void GRAPH_OT_decimate(struct wmOperatorType *ot);
|
||||
void GRAPH_OT_blend_to_default(struct wmOperatorType *ot);
|
||||
void GRAPH_OT_gauss_smooth(struct wmOperatorType *ot);
|
||||
void GRAPH_OT_sample(struct wmOperatorType *ot);
|
||||
void GRAPH_OT_bake(struct wmOperatorType *ot);
|
||||
void GRAPH_OT_unbake(struct wmOperatorType *ot);
|
||||
|
|
|
@ -464,6 +464,7 @@ void graphedit_operatortypes(void)
|
|||
WM_operatortype_append(GRAPH_OT_breakdown);
|
||||
WM_operatortype_append(GRAPH_OT_ease);
|
||||
WM_operatortype_append(GRAPH_OT_blend_to_default);
|
||||
WM_operatortype_append(GRAPH_OT_gauss_smooth);
|
||||
WM_operatortype_append(GRAPH_OT_euler_filter);
|
||||
WM_operatortype_append(GRAPH_OT_delete);
|
||||
WM_operatortype_append(GRAPH_OT_duplicate);
|
||||
|
|
|
@ -1053,3 +1053,109 @@ void GRAPH_OT_ease(wmOperatorType *ot)
|
|||
}
|
||||
|
||||
/** \} */
|
||||
/* -------------------------------------------------------------------- */
|
||||
/** \name Gauss Smooth Operator
|
||||
* \{ */
|
||||
|
||||
static void gauss_smooth_graph_keys(bAnimContext *ac,
|
||||
const float factor,
|
||||
const float sigma,
|
||||
const int filter_width)
|
||||
{
|
||||
ListBase anim_data = {NULL, NULL};
|
||||
ANIM_animdata_filter(ac, &anim_data, OPERATOR_DATA_FILTER, ac->data, ac->datatype);
|
||||
|
||||
bAnimListElem *ale;
|
||||
|
||||
double *kernel = MEM_callocN(sizeof(double) * (filter_width * 2 + 1), "Gauss Kernel");
|
||||
get_1d_gauss_kernel(filter_width, sigma, kernel);
|
||||
|
||||
for (ale = anim_data.first; ale; ale = ale->next) {
|
||||
FCurve *fcu = (FCurve *)ale->key_data;
|
||||
ListBase segments = find_fcurve_segments(fcu);
|
||||
|
||||
Sybren A. Stüvel
commented
Document what this struct contains / what it's for. Document what this struct contains / what it's for.
|
||||
LISTBASE_FOREACH (FCurveSegment *, segment, &segments) {
|
||||
BezTriple left_bezt = fcu->bezt[segment->start_index];
|
||||
BezTriple right_bezt = fcu->bezt[segment->start_index + segment->length - 1];
|
||||
const int sample_count = (int)(right_bezt.vec[1][0] - left_bezt.vec[1][0]) +
|
||||
(filter_width * 2) + 1;
|
||||
float *samples = MEM_callocN(sizeof(float) * sample_count, "Smooth FCurve Op Samples");
|
||||
sample_fcurve_segment(fcu, left_bezt.vec[1][0] - filter_width, samples, sample_count);
|
||||
smooth_fcurve_segment(fcu, segment, samples, factor, filter_width, kernel);
|
||||
MEM_freeN(samples);
|
||||
}
|
||||
|
||||
BLI_freelistN(&segments);
|
||||
ale->update |= ANIM_UPDATE_DEFAULT;
|
||||
}
|
||||
|
||||
MEM_freeN(kernel);
|
||||
ANIM_animdata_update(ac, &anim_data);
|
||||
ANIM_animdata_freelist(&anim_data);
|
||||
}
|
||||
|
||||
static int gauss_exec(bContext *C, wmOperator *op)
|
||||
{
|
||||
bAnimContext ac;
|
||||
|
||||
if (ANIM_animdata_get_context(C, &ac) == 0) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
const float factor = RNA_float_get(op->ptr, "factor");
|
||||
const float sigma = RNA_float_get(op->ptr, "sigma");
|
||||
const int filter_width = RNA_int_get(op->ptr, "filter_width");
|
||||
gauss_smooth_graph_keys(&ac, factor, sigma, filter_width);
|
||||
|
||||
/* Set notifier that keyframes have changed. */
|
||||
WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
|
||||
|
||||
return OPERATOR_FINISHED;
|
||||
}
|
||||
|
||||
void GRAPH_OT_gauss_smooth(wmOperatorType *ot)
|
||||
{
|
||||
/* Identifiers. */
|
||||
ot->name = "Gauss Smooth";
|
||||
ot->idname = "GRAPH_OT_gauss_smooth";
|
||||
ot->description = "Smooth the curve using a Gauss filter";
|
||||
|
||||
/* API callbacks. */
|
||||
/* ot->invoke = fft_invoke; */
|
||||
/* ot->modal = fft_modal; */
|
||||
ot->exec = gauss_exec;
|
||||
ot->poll = graphop_editable_keyframes_poll;
|
||||
|
||||
/* Flags. */
|
||||
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
||||
|
||||
RNA_def_float_factor(ot->srna,
|
||||
"factor",
|
||||
1.0f,
|
||||
0.0f,
|
||||
FLT_MAX,
|
||||
"Factor",
|
||||
"How much to blend to the default value",
|
||||
0.0f,
|
||||
1.0f);
|
||||
|
||||
RNA_def_float(ot->srna,
|
||||
"sigma",
|
||||
2.0f,
|
||||
0.001f,
|
||||
Brecht Van Lommel
commented
Change this to:
It's using the old name now. Copying to a small fixed buffer like that can very easily overflow, but you don't need to copy at all. Change this to:
```
const char *mode_str = TIP_("Gaussian Smooth");
```
It's using the old name now. Copying to a small fixed buffer like that can very easily overflow, but you don't need to copy at all.
|
||||
FLT_MAX,
|
||||
"Sigma",
|
||||
"At which frquency the factor should be applied",
|
||||
0.001f,
|
||||
100.0f);
|
||||
|
||||
RNA_def_int(ot->srna,
|
||||
"filter_width",
|
||||
8,
|
||||
1,
|
||||
64,
|
||||
"Filter Width",
|
||||
"How far to each side the operator will average the key values",
|
||||
1,
|
||||
32);
|
||||
}
|
||||
/** \} */
|
||||
|
|
For non-static functions, I think it might be better to move the
ED_anim_...
prefixes.