diff options
author | Campbell Barton <ideasman42@gmail.com> | 2017-10-30 14:36:51 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2017-10-30 14:36:51 +0300 |
commit | 80465ba35a163c9832a4666fac74465561b7c6c5 (patch) | |
tree | 59c1aac8ca95dbdab195971ec808f703fc4a5249 /source/blender/editors/curve/editcurve.c | |
parent | e8daf2e3ea17c2e9569e6fc9b49879c74d9a8c22 (diff) |
Curve Decimate: new tool to simplify bezier curves
Access from the curve clean-up menu
Diffstat (limited to 'source/blender/editors/curve/editcurve.c')
-rw-r--r-- | source/blender/editors/curve/editcurve.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/source/blender/editors/curve/editcurve.c b/source/blender/editors/curve/editcurve.c index d14e7eccdf8..ee83f212463 100644 --- a/source/blender/editors/curve/editcurve.c +++ b/source/blender/editors/curve/editcurve.c @@ -5905,6 +5905,86 @@ void CURVE_OT_dissolve_verts(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } +static bool nurb_bezt_flag_any(const Nurb *nu, const char flag_test) +{ + BezTriple *bezt = nu->bezt; + int i; + + for (i = nu->pntsu, bezt = nu->bezt; i--; bezt++) { + if (bezt->f2 & flag_test) { + return true; + } + } + + return false; +} + +static int curve_decimate_exec(bContext *C, wmOperator *op) +{ + Object *obedit = CTX_data_edit_object(C); + Curve *cu = (Curve *)obedit->data; + bool all_supported = true; + bool changed = false; + + { + const float error_sq_max = FLT_MAX; + float ratio = RNA_float_get(op->ptr, "ratio"); + + ListBase *editnurb = object_editcurve_get(obedit); + Nurb *nu; + + for (nu = editnurb->first; nu; nu = nu->next) { + if (nu->type == CU_BEZIER) { + if ((nu->pntsu > 2) && nurb_bezt_flag_any(nu, SELECT)) { + const int error_target_len = max_ii(2, nu->pntsu * ratio); + if (error_target_len != nu->pntsu) { + BKE_curve_decimate_nurb(nu, cu->resolu, error_sq_max, error_target_len); + changed = true; + } + } + } + else { + all_supported = false; + } + } + } + + if (all_supported == false) { + BKE_report(op->reports, RPT_WARNING, "Only bezier curves are supported"); + } + + if (changed) { + cu->actnu = cu->actvert = CU_ACT_NONE; + if (ED_curve_updateAnimPaths(obedit->data)) { + WM_event_add_notifier(C, NC_OBJECT | ND_KEYS, obedit); + } + + WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data); + DAG_id_tag_update(obedit->data, 0); + } + + return OPERATOR_FINISHED; +} + +void CURVE_OT_decimate(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Decimate Curve"; + ot->description = "Simplify selected curves"; + ot->idname = "CURVE_OT_decimate"; + + /* api callbacks */ + ot->exec = curve_decimate_exec; + ot->poll = ED_operator_editcurve; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + RNA_def_float_factor(ot->srna, "ratio", 1.0f, 0.0f, 1.0f, "Ratio", "", 0.0f, 1.0f); +} + + /********************** shade smooth/flat operator *********************/ static int shade_smooth_exec(bContext *C, wmOperator *op) |