GP: replace custom API w/ BKE_deform API

This commit is contained in:
Campbell Barton
2018-08-27 14:20:40 +10:00
parent 21589dcbe0
commit 21c75bc7c5
7 changed files with 35 additions and 131 deletions

View File

@@ -152,10 +152,7 @@ void BKE_gpencil_centroid_3D(struct bGPdata *gpd, float r_centroid[3]);
/* vertex groups */
void BKE_gpencil_dvert_ensure(struct bGPDstroke *gps);
float BKE_gpencil_vgroup_use_index(struct MDeformVert *dvert, int index);
void BKE_gpencil_vgroup_remove(struct Object *ob, struct bDeformGroup *defgroup);
struct MDeformWeight *BKE_gpencil_vgroup_add_point_weight(struct MDeformVert *dvert, int index, float weight);
bool BKE_gpencil_vgroup_remove_point_weight(struct MDeformVert *dvert, int index);
void BKE_gpencil_stroke_weights_duplicate(struct bGPDstroke *gps_src, struct bGPDstroke *gps_dst);
/* GPencil geometry evaluation */

View File

@@ -57,6 +57,7 @@
#include "BKE_context.h"
#include "BKE_action.h"
#include "BKE_animsys.h"
#include "BKE_deform.h"
#include "BKE_global.h"
#include "BKE_gpencil.h"
#include "BKE_colortools.h"
@@ -545,21 +546,7 @@ void BKE_gpencil_stroke_weights_duplicate(bGPDstroke *gps_src, bGPDstroke *gps_d
}
BLI_assert(gps_src->totpoints == gps_dst->totpoints);
if ((gps_src->dvert == NULL) || (gps_dst->dvert == NULL)) {
return;
}
for (int i = 0; i < gps_src->totpoints; i++) {
MDeformVert *dvert_src = &gps_src->dvert[i];
MDeformVert *dvert_dst = &gps_dst->dvert[i];
if (dvert_src->totweight > 0) {
dvert_dst->dw = MEM_dupallocN(dvert_src->dw);
}
else {
dvert_dst->dw = NULL;
}
}
BKE_defvert_array_copy(gps_dst->dvert, gps_src->dvert, gps_src->totpoints);
}
/* make a copy of a given gpencil stroke */
@@ -1227,7 +1214,6 @@ void BKE_gpencil_vgroup_remove(Object *ob, bDeformGroup *defgroup)
{
bGPdata *gpd = ob->data;
MDeformVert *dvert = NULL;
MDeformWeight *gpw = NULL;
const int def_nr = BLI_findindex(&ob->defbase, defgroup);
/* Remove points data */
@@ -1237,15 +1223,9 @@ void BKE_gpencil_vgroup_remove(Object *ob, bDeformGroup *defgroup)
for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
for (int i = 0; i < gps->totpoints; i++) {
dvert = &gps->dvert[i];
for (int i2 = 0; i2 < dvert->totweight; i2++) {
gpw = &dvert->dw[i2];
if (gpw->def_nr == def_nr) {
BKE_gpencil_vgroup_remove_point_weight(dvert, def_nr);
}
/* if index is greater, must be moved one back */
if (gpw->def_nr > def_nr) {
gpw->def_nr--;
}
MDeformWeight *dw = defvert_find_index(dvert, def_nr);
if (dw != NULL) {
defvert_remove_group(dvert, dw);
}
}
}
@@ -1264,84 +1244,6 @@ void BKE_gpencil_dvert_ensure(bGPDstroke *gps)
gps->dvert = MEM_callocN(sizeof(MDeformVert) * gps->totpoints, "gp_stroke_weights");
}
}
/* add a new weight */
MDeformWeight *BKE_gpencil_vgroup_add_point_weight(MDeformVert *dvert, int index, float weight)
{
MDeformWeight *new_gpw = NULL;
MDeformWeight *tmp_gpw;
/* need to verify if was used before to update */
for (int i = 0; i < dvert->totweight; i++) {
tmp_gpw = &dvert->dw[i];
if (tmp_gpw->def_nr == index) {
tmp_gpw->weight = weight;
return tmp_gpw;
}
}
dvert->totweight++;
if (dvert->totweight == 1) {
dvert->dw = MEM_callocN(sizeof(MDeformWeight), "gp_weight");
}
else {
dvert->dw = MEM_reallocN(dvert->dw, sizeof(MDeformWeight) * dvert->totweight);
}
new_gpw = &dvert->dw[dvert->totweight - 1];
new_gpw->def_nr = index;
new_gpw->weight = weight;
return new_gpw;
}
/* return the weight if use index or -1*/
float BKE_gpencil_vgroup_use_index(MDeformVert *dvert, int index)
{
MDeformWeight *gpw;
for (int i = 0; i < dvert->totweight; i++) {
gpw = &dvert->dw[i];
if (gpw->def_nr == index) {
return gpw->weight;
}
}
return -1.0f;
}
/* add a new weight */
bool BKE_gpencil_vgroup_remove_point_weight(MDeformVert *dvert, int index)
{
int e = 0;
if (BKE_gpencil_vgroup_use_index(dvert, index) < 0.0f) {
return false;
}
/* if the array get empty, exit */
if (dvert->totweight == 1) {
dvert->totweight = 0;
MEM_SAFE_FREE(dvert->dw);
return true;
}
/* realloc weights */
MDeformWeight *tmp = MEM_dupallocN(dvert->dw);
MEM_SAFE_FREE(dvert->dw);
dvert->dw = MEM_callocN(sizeof(MDeformWeight) * dvert->totweight - 1, "gp_weights");
for (int x = 0; x < dvert->totweight; x++) {
MDeformWeight *gpw = &tmp[e];
MDeformWeight *final_gpw = &dvert->dw[e];
if (gpw->def_nr != index) {
final_gpw->def_nr = gpw->def_nr;
final_gpw->weight = gpw->weight;
e++;
}
}
MEM_SAFE_FREE(tmp);
dvert->totweight--;
return true;
}
/* ************************************************** */

View File

@@ -35,8 +35,9 @@
#include "DNA_screen_types.h"
#include "DNA_view3d_types.h"
#include "BKE_gpencil.h"
#include "BKE_action.h"
#include "BKE_deform.h"
#include "BKE_gpencil.h"
#include "DRW_render.h"
@@ -502,8 +503,7 @@ GPUBatch *DRW_gpencil_get_edit_geom(bGPDstroke *gps, float alpha, short dflag)
for (int i = 0; i < gps->totpoints; i++, pt++) {
/* weight paint */
if (is_weight_paint) {
float weight = gps->dvert!= NULL ? BKE_gpencil_vgroup_use_index(dvert, vgindex) : 0;
CLAMP(weight, 0.0f, 1.0f);
float weight = gps->dvert ? defvert_find_weight(dvert, vgindex) : 0.0f;
float hue = 2.0f * (1.0f - weight) / 3.0f;
hsv_to_rgb(hue, 1.0f, 1.0f, &selectColor[0], &selectColor[1], &selectColor[2]);
selectColor[3] = 1.0f;
@@ -581,8 +581,7 @@ GPUBatch *DRW_gpencil_get_edlin_geom(bGPDstroke *gps, float alpha, short UNUSED(
for (int i = 0; i < gps->totpoints; i++, pt++) {
/* weight paint */
if (is_weight_paint) {
float weight = gps->dvert != NULL ? BKE_gpencil_vgroup_use_index(dvert, vgindex) : 0;
CLAMP(weight, 0.0f, 1.0f);
float weight = gps->dvert ? defvert_find_weight(dvert, vgindex) : 0.0f;
float hue = 2.0f * (1.0f - weight) / 3.0f;
hsv_to_rgb(hue, 1.0f, 1.0f, &selectColor[0], &selectColor[1], &selectColor[2]);
selectColor[3] = 1.0f;

View File

@@ -57,6 +57,7 @@
#include "DNA_object_types.h"
#include "BKE_context.h"
#include "BKE_deform.h"
#include "BKE_gpencil.h"
#include "BKE_library.h"
#include "BKE_report.h"
@@ -897,14 +898,8 @@ static bool gp_brush_weight_apply(
}
}
/* get current weight */
float curweight = 0.0f;
for (int i = 0; i < dvert->totweight; i++) {
MDeformWeight *gpw = &dvert->dw[i];
if (gpw->def_nr == gso->vrgroup) {
curweight = gpw->weight;
break;
}
}
MDeformWeight *dw = defvert_verify_index(dvert, gso->vrgroup);
float curweight = dw ? dw->weight : 0.0f;
if (gp_brush_invert_check(gso)) {
/* reduce weight */
@@ -916,7 +911,9 @@ static bool gp_brush_weight_apply(
}
CLAMP(curweight, 0.0f, 1.0f);
BKE_gpencil_vgroup_add_point_weight(dvert, gso->vrgroup, curweight);
if (dw) {
dw->weight = curweight;
}
/* weight should stay within [0.0, 1.0] */
if (pt->pressure < 0.0f)

View File

@@ -1764,10 +1764,8 @@ static int gpencil_vertex_group_smooth_exec(bContext *C, wmOperator *op)
ptc = &gps->points[i];
}
float wa = BKE_gpencil_vgroup_use_index(dverta, def_nr);
float wb = BKE_gpencil_vgroup_use_index(dvertb, def_nr);
CLAMP_MIN(wa, 0.0f);
CLAMP_MIN(wb, 0.0f);
float wa = defvert_find_weight(dverta, def_nr);
float wb = defvert_find_weight(dvertb, def_nr);
/* the optimal value is the corresponding to the interpolation of the weight
* at the distance of point b
@@ -1775,8 +1773,10 @@ static int gpencil_vertex_group_smooth_exec(bContext *C, wmOperator *op)
const float opfac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x);
const float optimal = interpf(wa, wb, opfac);
/* Based on influence factor, blend between original and optimal */
wb = interpf(wb, optimal, fac);
BKE_gpencil_vgroup_add_point_weight(dvertb, def_nr, wb);
MDeformWeight *dw = defvert_verify_index(dvertb, def_nr);
if (dw) {
dw->weight = interpf(wb, optimal, fac);
}
}
}
}

View File

@@ -50,6 +50,7 @@
#include "DNA_view3d_types.h"
#include "BKE_action.h"
#include "BKE_deform.h"
#include "BKE_main.h"
#include "BKE_brush.h"
#include "BKE_context.h"
@@ -1225,7 +1226,10 @@ void ED_gpencil_vgroup_assign(bContext *C, Object *ob, float weight)
bGPDspoint *pt = &gps->points[i];
MDeformVert *dvert = &gps->dvert[i];
if (pt->flag & GP_SPOINT_SELECT) {
BKE_gpencil_vgroup_add_point_weight(dvert, def_nr, weight);
MDeformWeight *dw = defvert_verify_index(dvert, def_nr);
if (dw) {
dw->weight = weight;
}
}
}
}
@@ -1250,7 +1254,10 @@ void ED_gpencil_vgroup_remove(bContext *C, Object *ob)
MDeformVert *dvert = &gps->dvert[i];
if ((pt->flag & GP_SPOINT_SELECT) && (dvert->totweight > 0)) {
BKE_gpencil_vgroup_remove_point_weight(dvert, def_nr);
MDeformWeight *dw = defvert_find_index(dvert, def_nr);
if (dw != NULL) {
defvert_remove_group(dvert, dw);
}
}
}
}
@@ -1273,7 +1280,7 @@ void ED_gpencil_vgroup_select(bContext *C, Object *ob)
}
MDeformVert *dvert = &gps->dvert[i];
if (BKE_gpencil_vgroup_use_index(dvert, def_nr) > -1.0f) {
if (defvert_find_index(dvert, def_nr) != NULL) {
pt->flag |= GP_SPOINT_SELECT;
gps->flag |= GP_STROKE_SELECT;
}
@@ -1298,7 +1305,7 @@ void ED_gpencil_vgroup_deselect(bContext *C, Object *ob)
}
MDeformVert *dvert = &gps->dvert[i];
if (BKE_gpencil_vgroup_use_index(dvert, def_nr) > -1.0f) {
if (defvert_find_index(dvert, def_nr) != NULL) {
pt->flag &= ~GP_SPOINT_SELECT;
gps->flag |= GP_STROKE_SELECT;
}

View File

@@ -45,6 +45,7 @@
#include "DNA_gpencil_types.h"
#include "DNA_gpencil_modifier_types.h"
#include "BKE_deform.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_object.h"
@@ -126,7 +127,8 @@ float get_modifier_point_weight(MDeformVert *dvert, int inverse, int vindex)
float weight = 1.0f;
if (vindex >= 0) {
weight = BKE_gpencil_vgroup_use_index(dvert, vindex);
MDeformWeight *dw = defvert_find_index(dvert, vindex);
weight = dw ? dw->weight : -1.0f;
if ((weight >= 0.0f) && (inverse == 1)) {
return -1.0f;
}