[Bf-blender-cvs] [c6200a6] gsoc2016-improved_extrusion: Curves: Extend and Trim operator
João Araújo
noreply at git.blender.org
Wed Aug 3 18:28:37 CEST 2016
Commit: c6200a6c6713bf3b384a2a77b64ab96d14f96ecb
Author: João Araújo
Date: Wed Aug 3 17:27:46 2016 +0100
Branches: gsoc2016-improved_extrusion
https://developer.blender.org/rBc6200a6c6713bf3b384a2a77b64ab96d14f96ecb
Curves: Extend and Trim operator
Extend operator: fixed memory related issues
Trim operator: idem, and some bugs
===================================================================
M source/blender/editors/curve/editcurve.c
===================================================================
diff --git a/source/blender/editors/curve/editcurve.c b/source/blender/editors/curve/editcurve.c
index 5a32f91..c84a0cc 100644
--- a/source/blender/editors/curve/editcurve.c
+++ b/source/blender/editors/curve/editcurve.c
@@ -6307,7 +6307,7 @@ void CURVE_OT_match_texture_space(wmOperatorType *ot)
/******************** Extend curve operator ********************/
-static void get_selected_splines(ListBase* spline_list, ListBase *nubase, int *r_number_splines, bool return_cyclic)
+static void get_selected_bezier_splines(ListBase* spline_list, ListBase *nubase, int *r_number_splines, bool return_cyclic)
{
/* receives a list with all Bezier splines in a "curve" object
* returns the first spline of a linked list with all the selected splines,
@@ -6322,6 +6322,9 @@ static void get_selected_splines(ListBase* spline_list, ListBase *nubase, int *r
{
handles = nu->pntsu;
bezt = nu->bezt;
+ if (!nu->bezt) {
+ continue;
+ }
while (handles--)
{ /* cycle through all the handles. see if any is selected */
if (BEZT_ISSEL_ANY(bezt) && (!(nu->flagu & CU_NURB_CYCLIC) || return_cyclic))
@@ -6467,6 +6470,10 @@ static ListBase *interpolate_all_segments(Nurb *nu)
* If the cuve only has two handles, the final result is only on list
* Otherwise, there is an overlap between the last element of a list and
* the first of the next one */
+ if (!nu->bezt) { /* Not a Bezier curve */
+ return NULL;
+ }
+
int i = 0, dims = 3;
float *coord_array;
ListBase *pl = (ListBase *)MEM_callocN(sizeof(ListBase), "interpolate_all_segments1");
@@ -6502,7 +6509,12 @@ static void linear_spline_list(ListBase *nubase, ListBase *spline_list)
for (nu=nubase->first; nu; nu=nu->next) {
link = MEM_callocN(sizeof(LinkData), "linearspllist1");
link->data = interpolate_all_segments(nu);
- BLI_addtail(spline_list, link);
+ if (link->data) {
+ BLI_addtail(spline_list, link);
+ }
+ else {
+ MEM_freeN(link);
+ }
}
}
@@ -6539,15 +6551,13 @@ static void get_intersections(ListBase* il, float *p1, float *p2, ListBase *nuba
MEM_freeN(vi);
}
}
- MEM_freeN(link->data); /* commenting either does not free the memory */
- MEM_freeN(coord_array); /* running both gives an error, but seems to free the memory */
+ MEM_freeN(link->data);
}
BLI_freelistN(spl->data);
BLI_freelistN(points_list);
MEM_freeN(points_list);
}
BLI_freelistN(&spline_list);
- MEM_freeN(&spline_list);
}
static int extend_curve_exec(bContext *C, wmOperator *op)
@@ -6564,16 +6574,7 @@ static int extend_curve_exec(bContext *C, wmOperator *op)
int n_selected_splines = 0, result = 0, a = 0;
float p1[3], p2[3] = {0.0, 0.0, 0.0}, p1_handle[3], bound_box[4], p1_extend[2];
- for (nu = nubase->first; nu; nu = nu->next)
- {
- if (!(nu->type & CU_BEZIER))
- {
- BKE_report(op->reports, RPT_ERROR, "Only Bezier curves can be extended");
- return OPERATOR_CANCELLED;
- }
- }
-
- get_selected_splines(&spline_list, nubase, &n_selected_splines, false);
+ get_selected_bezier_splines(&spline_list, nubase, &n_selected_splines, false);
/* the user can only select one or two splines */
if ((n_selected_splines == 0 || n_selected_splines > 2)) {
@@ -6693,8 +6694,8 @@ static int extend_curve_exec(bContext *C, wmOperator *op)
}
BLI_freelistN(&intersections_list);
if(selected_endpoints) MEM_freeN(selected_endpoints);
- //if(first_selected_endpoints) MEM_freeN(first_selected_endpoints);
- //if(second_selected_endpoints) MEM_freeN(second_selected_endpoints);
+ if(first_selected_endpoints) MEM_freeN(first_selected_endpoints);
+ if(second_selected_endpoints) MEM_freeN(second_selected_endpoints);
if (result != 1)
{
@@ -6798,7 +6799,6 @@ static ListBase *spline_X_shape(Object *obedit, int selected_spline)
intersections = (ListBase *)MEM_callocN(sizeof(ListBase), "splineXshape2");
full_coord_array = (float *)MEM_callocN(3 * ((nu->pntsu - 1 + nu->flagu) * nu->resolu + 1) * sizeof(float), "splineXshape3");
-
/* get the full coord_array for nu */
float *original_first_coord_array;
// original_first_coord_array = (float *)MEM_callocN(3 * (nu->resolu + 1) * sizeof(float), "splineXshape7");
@@ -6900,7 +6900,6 @@ static ListBase *spline_X_shape(Object *obedit, int selected_spline)
BLI_listbase_sort(intersections, XShape_cmp);
MEM_freeN(full_coord_array);
- MEM_freeN(original_first_coord_array);
for (segment = all_segments->first; segment; segment = segment->next)
{
@@ -6919,7 +6918,6 @@ static ListBase *spline_X_shape(Object *obedit, int selected_spline)
MEM_freeN(spline->data);
}
BLI_freelistN(&shape_list);
- MEM_freeN(&shape_list);
return intersections;
}
@@ -7032,25 +7030,27 @@ static void chop(float *x, float *p1, float *p2, float *p3, float *p4, int res,
float *r_s1, float *r_s2)
{
float ratio = ratio_to_segment(x, p1, p2, p3, p4, res);
-
- if (ratio != 0) {
- split_segment(ratio, p1, p2, p3, p4, r_s1, r_s2);
- }
- else
- {
- r_s1 = r_s2 = NULL;
- }
+ split_segment(ratio, p1, p2, p3, p4, r_s1, r_s2);
}
static int trim_curve_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
+ Curve *cu = obedit->data;
ListBase *nubase = object_editcurve_get(obedit);
- ListBase selected_splines = {NULL};
+ ListBase selected_splines = {NULL}, high = {NULL}, low = {NULL};
int n_sel_splines = 0, n_sel_handles = 0;
- Nurb *nu;
+ Nurb *nu, *next, *new_spl = NULL;
+ bool changed = 0;
+
+ get_selected_bezier_splines(&selected_splines, nubase, &n_sel_splines, true);
- get_selected_splines(&selected_splines, nubase, &n_sel_splines, true);
+ nu = selected_splines.first;
+ while (nu) {
+ next = nu->next;
+ BKE_nurb_free(nu);
+ nu = next;
+ }
/* it only makes sense for one spline to be selected */
if (n_sel_splines == 0) {
@@ -7062,7 +7062,7 @@ static int trim_curve_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
- int spline_id = get_selected_spline_id(nubase);
+ int spline_id = cu->actnu;
nu = BLI_findlink(nubase, spline_id);
ListBase *spl_int = spline_X_shape(obedit, spline_id);
@@ -7081,8 +7081,6 @@ static int trim_curve_exec(bContext *C, wmOperator *op)
BKE_report(op->reports, RPT_ERROR, "Something went terribly wrong! Report this bug and assign it to genio84");
return OPERATOR_CANCELLED;
}
- ListBase *low = (ListBase *)MEM_callocN(sizeof(ListBase), "trim_exec1");;
- ListBase *high = (ListBase *)MEM_callocN(sizeof(ListBase), "trim_exec2");;
XShape *xshape;
LinkData *link;
@@ -7091,17 +7089,17 @@ static int trim_curve_exec(bContext *C, wmOperator *op)
if (xshape->order < point_id) {
link = (LinkData *)MEM_callocN(sizeof(LinkData), "trim_exec3");
link->data = xshape;
- BLI_addtail(low, link);
+ BLI_addtail(&low, link);
}
else
{
link = (LinkData *)MEM_callocN(sizeof(LinkData), "trim_exec4");
link->data = xshape;
- BLI_addtail(high, link);
+ BLI_addtail(&high, link);
}
}
- int len_low = BLI_listbase_count(low), len_high = BLI_listbase_count(high);
+ int len_low = BLI_listbase_count(&low), len_high = BLI_listbase_count(&high);
float *s1, *s2, *s3, *s4;
s1 = (float *)MEM_callocN(4 * 3 * sizeof(float), "trim_exec_coord1");
s2 = (float *)MEM_callocN(4 * 3 * sizeof(float), "trim_exec_coord2");
@@ -7115,36 +7113,35 @@ static int trim_curve_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
- int npoints = 0;
+ int npoints = 0, high_first_order = 0, low_last_order = 0;
if (len_low == 0)
{
- MEM_freeN(low);
low = high;
- npoints = ((XShape *)((LinkData *)high->last)->data)->order - ((XShape *)((LinkData *)high->first)->data)->order + 2;
+ npoints = ((XShape *)((LinkData *)high.last)->data)->order - ((XShape *)((LinkData *)high.first)->data)->order + 2;
}
else if (len_high == 0)
{
- MEM_freeN(high);
high = low;
- npoints = ((XShape *)((LinkData *)low->last)->data)->order - ((XShape *)((LinkData *)low->first)->data)->order + 2;
+ npoints = ((XShape *)((LinkData *)low.last)->data)->order - ((XShape *)((LinkData *)low.first)->data)->order + 2;
}
else
{
- npoints = nu->pntsu - ((XShape *)((LinkData *)high->first)->data)->order + ((XShape *)((LinkData *)low->last)->data)->order + 2;
+ npoints = nu->pntsu - ((XShape *)((LinkData *)high.first)->data)->order + ((XShape *)((LinkData *)low.last)->data)->order + 2;
}
- int low_last_order = ((XShape *)((LinkData *)low->last)->data)->order;
- int high_first_order = ((XShape *)((LinkData *)high->first)->data)->order;
+ low_last_order = ((XShape *)((LinkData *)low.last)->data)->order;
+ high_first_order = ((XShape *)((LinkData *)high.first)->data)->order;
- Nurb *new_spl = BKE_nurb_duplicate(nu);
- // new_spl->bezt = (BezTriple *)MEM_callocN(npoints * sizeof(BezTriple), "trimexec5");
+ new_spl = BKE_nurb_duplicate(nu);
+ MEM_freeN(new_spl->bezt);
+ new_spl->bezt = (BezTriple *)MEM_callocN(npoints * sizeof(BezTriple), "trimexec5");
new_spl->pntsu = npoints;
new_spl->flagu = 0;
BezTriple *bezt = new_spl->bezt;
for (int i = 0; i < new_spl->pntsu; i++) {
BezTriple *old_bezt = nu->bezt;
/* get the correct bezier point from the original spline */
- int a = (i + ((XShape *)((LinkData *)high->first)->data)->order ) % ( nu->pntsu );
+ int a = (i + high_first_order ) % ( nu->pntsu );
while ( a-- ) {
old_bezt++;
}
@@ -7154,7 +7151,7 @@ static int trim_curve_exec(bContext *C, wmOperator *op)
bezt++;
}
- chop(((XShape *)((LinkData *)low->last)->data)->intersections,
+ chop(((XShape *)((LinkData *)low.last)->data)->intersections,
(float *)&nu->bezt[low_last_order].vec[1],
(float *)&nu->bezt[low_last_order].vec[2],
(float *)&nu->bezt[(low_last_order + 1) % nu->pntsu].vec[0],
@@ -7166,12 +7163,12 @@ static int trim_curve_exec(bContext *C, wmOperator *op)
new_spl->bezt[new_spl->pntsu - 1].h2 = HD_FREE;
new_spl->bezt[new_spl->pntsu - 2].h1 = HD_FREE;
new_spl->bezt[new_spl->pntsu - 2].h2 = HD_FREE;
- copy_v3_v3(new_spl->bezt[new_spl->pntsu - 1].vec[1], ((XShape *)((LinkData *)low->last)->data)->intersections);
+ copy_v3_v3(new_spl->bezt[new_spl->pntsu - 1].vec[1], ((XShape *)((LinkData *)low.last)->data)->intersections);
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list