From f2c52aa0e1a5a6905849daa299ab907e60cab983 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 29 Jun 2021 20:13:55 +1000 Subject: Cleanup: use 'const' arguments in transform calculation Use const arguments to simplify further optimizations. Transforming elements shouldn't need to change their containers data-structures. ElementResize for grease pencil stroke thickness was modifying TransInfo.num & TransInfo.values_final. Now copies are operated on to preserve const correctness although it's worth investigating if this can be avoided altogether. --- source/blender/editors/transform/transform.h | 20 +++--- .../editors/transform/transform_constraints.c | 82 +++++++++++++--------- .../editors/transform/transform_constraints.h | 2 +- source/blender/editors/transform/transform_mode.c | 49 +++++++++---- source/blender/editors/transform/transform_mode.h | 22 +++--- source/blender/editors/transform/transform_snap.c | 10 +-- source/blender/editors/transform/transform_snap.h | 4 +- 7 files changed, 114 insertions(+), 75 deletions(-) diff --git a/source/blender/editors/transform/transform.h b/source/blender/editors/transform/transform.h index 2df0d86d02b..4f97c3b6713 100644 --- a/source/blender/editors/transform/transform.h +++ b/source/blender/editors/transform/transform.h @@ -356,22 +356,22 @@ typedef struct TransCon { /** Apply function pointer for linear vectorial transformation * The last three parameters are pointers to the in/out/printable vectors. */ - void (*applyVec)(struct TransInfo *t, - struct TransDataContainer *tc, + void (*applyVec)(const struct TransInfo *t, + const struct TransDataContainer *tc, struct TransData *td, const float in[3], - float out[3]); + float r_out[3]); /** Apply function pointer for size transformation. */ - void (*applySize)(struct TransInfo *t, - struct TransDataContainer *tc, + void (*applySize)(const struct TransInfo *t, + const struct TransDataContainer *tc, struct TransData *td, - float smat[3][3]); + float r_smat[3][3]); /** Apply function pointer for rotation transformation */ - void (*applyRot)(struct TransInfo *t, - struct TransDataContainer *tc, + void (*applyRot)(const struct TransInfo *t, + const struct TransDataContainer *tc, struct TransData *td, - float vec[3], - float *angle); + float r_axis[3], + float *r_angle); } TransCon; typedef struct MouseInput { diff --git a/source/blender/editors/transform/transform_constraints.c b/source/blender/editors/transform/transform_constraints.c index 8c74d5349ba..78fc6575e6e 100644 --- a/source/blender/editors/transform/transform_constraints.c +++ b/source/blender/editors/transform/transform_constraints.c @@ -97,7 +97,7 @@ static void view_vector_calc(const TransInfo *t, const float focus[3], float r_v /* ************************** CONSTRAINTS ************************* */ #define CONSTRAIN_EPSILON 0.0001f -static void constraint_plane_calc(TransInfo *t, float r_plane[4]) +static void constraint_plane_calc(const TransInfo *t, float r_plane[4]) { const float *constraint_vector[2]; int n = 0; @@ -391,8 +391,11 @@ static void planeProjection(const TransInfo *t, const float in[3], float out[3]) * projected along the view vector. * (in perspective mode, the view vector is relative to the position on screen) */ -static void applyAxisConstraintVec( - TransInfo *t, TransDataContainer *UNUSED(tc), TransData *td, const float in[3], float out[3]) +static void applyAxisConstraintVec(const TransInfo *t, + const TransDataContainer *UNUSED(tc), + TransData *td, + const float in[3], + float out[3]) { copy_v3_v3(out, in); if (!td && t->con.mode & CON_APPLY) { @@ -472,8 +475,11 @@ static void applyAxisConstraintVec( * * Further down, that vector is mapped to each data's space. */ -static void applyObjectConstraintVec( - TransInfo *t, TransDataContainer *tc, TransData *td, const float in[3], float out[3]) +static void applyObjectConstraintVec(const TransInfo *t, + const TransDataContainer *tc, + TransData *td, + const float in[3], + float out[3]) { if (!td) { applyAxisConstraintVec(t, tc, td, in, out); @@ -494,36 +500,36 @@ static void applyObjectConstraintVec( /** * Generic callback for constant spatial constraints applied to resize motion. */ -static void applyAxisConstraintSize(TransInfo *t, - TransDataContainer *UNUSED(tc), +static void applyAxisConstraintSize(const TransInfo *t, + const TransDataContainer *UNUSED(tc), TransData *td, - float smat[3][3]) + float r_smat[3][3]) { if (!td && t->con.mode & CON_APPLY) { float tmat[3][3]; if (!(t->con.mode & CON_AXIS0)) { - smat[0][0] = 1.0f; + r_smat[0][0] = 1.0f; } if (!(t->con.mode & CON_AXIS1)) { - smat[1][1] = 1.0f; + r_smat[1][1] = 1.0f; } if (!(t->con.mode & CON_AXIS2)) { - smat[2][2] = 1.0f; + r_smat[2][2] = 1.0f; } - mul_m3_m3m3(tmat, smat, t->spacemtx_inv); - mul_m3_m3m3(smat, t->spacemtx, tmat); + mul_m3_m3m3(tmat, r_smat, t->spacemtx_inv); + mul_m3_m3m3(r_smat, t->spacemtx, tmat); } } /** * Callback for object based spatial constraints applied to resize motion. */ -static void applyObjectConstraintSize(TransInfo *t, - TransDataContainer *tc, +static void applyObjectConstraintSize(const TransInfo *t, + const TransDataContainer *tc, TransData *td, - float smat[3][3]) + float r_smat[3][3]) { if (td && t->con.mode & CON_APPLY) { float tmat[3][3]; @@ -532,26 +538,26 @@ static void applyObjectConstraintSize(TransInfo *t, invert_m3_m3(imat, td->axismtx); if (!(t->con.mode & CON_AXIS0)) { - smat[0][0] = 1.0f; + r_smat[0][0] = 1.0f; } if (!(t->con.mode & CON_AXIS1)) { - smat[1][1] = 1.0f; + r_smat[1][1] = 1.0f; } if (!(t->con.mode & CON_AXIS2)) { - smat[2][2] = 1.0f; + r_smat[2][2] = 1.0f; } - mul_m3_m3m3(tmat, smat, imat); + mul_m3_m3m3(tmat, r_smat, imat); if (t->flag & T_EDIT) { - mul_m3_m3m3(smat, tc->mat3_unit, smat); + mul_m3_m3m3(r_smat, tc->mat3_unit, r_smat); } - mul_m3_m3m3(smat, td->axismtx, tmat); + mul_m3_m3m3(r_smat, td->axismtx, tmat); } } -static void constraints_rotation_impl(TransInfo *t, +static void constraints_rotation_impl(const TransInfo *t, const float axismtx[3][3], - float r_vec[3], + float r_axis[3], float *r_angle) { BLI_assert(t->con.mode & CON_APPLY); @@ -560,15 +566,15 @@ static void constraints_rotation_impl(TransInfo *t, switch (mode) { case CON_AXIS0: case (CON_AXIS1 | CON_AXIS2): - copy_v3_v3(r_vec, axismtx[0]); + copy_v3_v3(r_axis, axismtx[0]); break; case CON_AXIS1: case (CON_AXIS0 | CON_AXIS2): - copy_v3_v3(r_vec, axismtx[1]); + copy_v3_v3(r_axis, axismtx[1]); break; case CON_AXIS2: case (CON_AXIS0 | CON_AXIS1): - copy_v3_v3(r_vec, axismtx[2]); + copy_v3_v3(r_axis, axismtx[2]); break; } /* don't flip axis if asked to or if num input */ @@ -576,7 +582,7 @@ static void constraints_rotation_impl(TransInfo *t, !((mode & CON_NOFLIP) || hasNumInput(&t->num) || (t->flag & T_INPUT_IS_VALUES_FINAL))) { float view_vector[3]; view_vector_calc(t, t->center_global, view_vector); - if (dot_v3v3(r_vec, view_vector) > 0.0f) { + if (dot_v3v3(r_axis, view_vector) > 0.0f) { *r_angle = -(*r_angle); } } @@ -595,11 +601,14 @@ static void constraints_rotation_impl(TransInfo *t, * This insures that the rotation is always logically following the mouse. * (ie: not doing counterclockwise rotations when the mouse moves clockwise). */ -static void applyAxisConstraintRot( - TransInfo *t, TransDataContainer *UNUSED(tc), TransData *td, float vec[3], float *angle) +static void applyAxisConstraintRot(const TransInfo *t, + const TransDataContainer *UNUSED(tc), + TransData *td, + float r_axis[3], + float *r_angle) { if (!td && t->con.mode & CON_APPLY) { - constraints_rotation_impl(t, t->spacemtx, vec, angle); + constraints_rotation_impl(t, t->spacemtx, r_axis, r_angle); } } @@ -616,8 +625,11 @@ static void applyAxisConstraintRot( * This insures that the rotation is always logically following the mouse. * (ie: not doing counterclockwise rotations when the mouse moves clockwise). */ -static void applyObjectConstraintRot( - TransInfo *t, TransDataContainer *tc, TransData *td, float vec[3], float *angle) +static void applyObjectConstraintRot(const TransInfo *t, + const TransDataContainer *tc, + TransData *td, + float r_axis[3], + float *r_angle) { if (t->con.mode & CON_APPLY) { float tmp_axismtx[3][3]; @@ -638,7 +650,7 @@ static void applyObjectConstraintRot( axismtx = td->axismtx; } - constraints_rotation_impl(t, axismtx, vec, angle); + constraints_rotation_impl(t, axismtx, r_axis, r_angle); } } @@ -1164,7 +1176,7 @@ bool isLockConstraint(TransInfo *t) * even if they aren't actually used in the callback function. * (Which could happen for weird constraints not yet designed. Along a path for example.) */ -int getConstraintSpaceDimension(TransInfo *t) +int getConstraintSpaceDimension(const TransInfo *t) { int n = 0; diff --git a/source/blender/editors/transform/transform_constraints.h b/source/blender/editors/transform/transform_constraints.h index ac62c057f9d..cbf6c7bb576 100644 --- a/source/blender/editors/transform/transform_constraints.h +++ b/source/blender/editors/transform/transform_constraints.h @@ -47,4 +47,4 @@ void setNearestAxis(TransInfo *t); int constraintModeToIndex(const TransInfo *t); char constraintModeToChar(const TransInfo *t); bool isLockConstraint(TransInfo *t); -int getConstraintSpaceDimension(TransInfo *t); +int getConstraintSpaceDimension(const TransInfo *t); diff --git a/source/blender/editors/transform/transform_mode.c b/source/blender/editors/transform/transform_mode.c index 6a946994e06..65a673940f8 100644 --- a/source/blender/editors/transform/transform_mode.c +++ b/source/blender/editors/transform/transform_mode.c @@ -68,7 +68,7 @@ int transform_mode_really_used(bContext *C, int mode) return mode; } -bool transdata_check_local_center(TransInfo *t, short around) +bool transdata_check_local_center(const TransInfo *t, short around) { return ((around == V3D_AROUND_LOCAL_ORIGINS) && ((t->options & (CTX_OBJECT | CTX_POSE_BONE)) || @@ -248,7 +248,7 @@ void protectedSizeBits(short protectflag, float size[3]) /** \name Transform Limits * \{ */ -void constraintTransLim(TransInfo *t, TransData *td) +void constraintTransLim(const TransInfo *t, TransData *td) { if (td->con) { const bConstraintTypeInfo *ctiLoc = BKE_constraint_typeinfo_from_type( @@ -359,7 +359,7 @@ static void constraintob_from_transdata(bConstraintOb *cob, TransData *td) } } -static void constraintRotLim(TransInfo *UNUSED(t), TransData *td) +static void constraintRotLim(const TransInfo *UNUSED(t), TransData *td) { if (td->con) { const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_ROTLIMIT); @@ -432,7 +432,7 @@ static void constraintRotLim(TransInfo *UNUSED(t), TransData *td) } } -void constraintSizeLim(TransInfo *t, TransData *td) +void constraintSizeLim(const TransInfo *t, TransData *td) { if (td->con && td->ext) { const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_from_type(CONSTRAINT_TYPE_SIZELIMIT); @@ -557,8 +557,8 @@ void headerRotation(TransInfo *t, char *str, const int str_size, float final) * * Protected axis and other transform settings are taken into account. */ -void ElementRotation_ex(TransInfo *t, - TransDataContainer *tc, +void ElementRotation_ex(const TransInfo *t, + const TransDataContainer *tc, TransData *td, const float mat[3][3], const float *center) @@ -810,8 +810,11 @@ void ElementRotation_ex(TransInfo *t, } } -void ElementRotation( - TransInfo *t, TransDataContainer *tc, TransData *td, float mat[3][3], const short around) +void ElementRotation(const TransInfo *t, + const TransDataContainer *tc, + TransData *td, + const float mat[3][3], + const short around) { const float *center; @@ -921,7 +924,10 @@ static void TransMat3ToSize(const float mat[3][3], const float smat[3][3], float } } -void ElementResize(TransInfo *t, TransDataContainer *tc, TransData *td, float mat[3][3]) +void ElementResize(const TransInfo *t, + const TransDataContainer *tc, + TransData *td, + const float mat[3][3]) { float tmat[3][3], smat[3][3], center[3]; float vec[3]; @@ -1014,17 +1020,31 @@ void ElementResize(TransInfo *t, TransDataContainer *tc, TransData *td, float ma sub_v3_v3(vec, td->center); } - /* grease pencil falloff */ + /* Grease pencil falloff. + * + * FIXME: This is bad on multiple levels! + * + * - #applyNumInput is not intended to be run for every element, + * this writes back into the number input in a way that doesn't make sense to run many times. + * + * - Writing into #TransInfo should be avoided since it means order of operations + * may impact the result and isn't thread-safe. + * + * Operating on copies as a temporary solution. + */ if (t->options & CTX_GPENCIL_STROKES) { bGPDstroke *gps = (bGPDstroke *)td->extra; mul_v3_fl(vec, td->factor * gps->runtime.multi_frame_falloff); - /* scale stroke thickness */ + /* Scale stroke thickness. */ if (td->val) { - transform_snap_increment(t, t->values_final); - applyNumInput(&t->num, t->values_final); + NumInput num_evil = t->num; + float values_final_evil[4]; + copy_v4_v4(values_final_evil, t->values_final); + transform_snap_increment(t, values_final_evil); + applyNumInput(&num_evil, values_final_evil); - float ratio = t->values_final[0]; + float ratio = values_final_evil[0]; *td->val = td->ival * ratio * gps->runtime.multi_frame_falloff; CLAMP_MIN(*td->val, 0.001f); } @@ -1044,6 +1064,7 @@ void ElementResize(TransInfo *t, TransDataContainer *tc, TransData *td, float ma constraintTransLim(t, td); } + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/editors/transform/transform_mode.h b/source/blender/editors/transform/transform_mode.h index a2b95eb3de4..027fb6b6982 100644 --- a/source/blender/editors/transform/transform_mode.h +++ b/source/blender/editors/transform/transform_mode.h @@ -41,22 +41,28 @@ typedef struct TransDataGenericSlideVert { /* transform_mode.c */ int transform_mode_really_used(struct bContext *C, int mode); -bool transdata_check_local_center(TransInfo *t, short around); +bool transdata_check_local_center(const TransInfo *t, short around); bool transform_mode_is_changeable(const int mode); void protectedTransBits(short protectflag, float vec[3]); void protectedSizeBits(short protectflag, float size[3]); -void constraintTransLim(TransInfo *t, TransData *td); -void constraintSizeLim(TransInfo *t, TransData *td); +void constraintTransLim(const TransInfo *t, TransData *td); +void constraintSizeLim(const TransInfo *t, TransData *td); void headerRotation(TransInfo *t, char *str, int str_size, float final); -void ElementRotation_ex(TransInfo *t, - TransDataContainer *tc, +void ElementRotation_ex(const TransInfo *t, + const TransDataContainer *tc, TransData *td, const float mat[3][3], const float *center); -void ElementRotation( - TransInfo *t, TransDataContainer *tc, TransData *td, float mat[3][3], const short around); +void ElementRotation(const TransInfo *t, + const TransDataContainer *tc, + TransData *td, + const float mat[3][3], + const short around); void headerResize(TransInfo *t, const float vec[3], char *str, int str_size); -void ElementResize(TransInfo *t, TransDataContainer *tc, TransData *td, float mat[3][3]); +void ElementResize(const TransInfo *t, + const TransDataContainer *tc, + TransData *td, + const float mat[3][3]); short getAnimEdit_SnapMode(TransInfo *t); void doAnimEdit_SnapFrame( TransInfo *t, TransData *td, TransData2D *td2d, struct AnimData *adt, short autosnap); diff --git a/source/blender/editors/transform/transform_snap.c b/source/blender/editors/transform/transform_snap.c index 4ecb9594325..b6e0f07cc70 100644 --- a/source/blender/editors/transform/transform_snap.c +++ b/source/blender/editors/transform/transform_snap.c @@ -1517,7 +1517,7 @@ bool transform_snap_grid(TransInfo *t, float *val) return true; } -static void snap_increment_apply_ex(TransInfo *UNUSED(t), +static void snap_increment_apply_ex(const TransInfo *UNUSED(t), const int max_index, const float increment_val, const float aspect[3], @@ -1531,8 +1531,8 @@ static void snap_increment_apply_ex(TransInfo *UNUSED(t), } } -static void snap_increment_apply(TransInfo *t, - int max_index, +static void snap_increment_apply(const TransInfo *t, + const int max_index, const float increment_dist, float *r_val) { @@ -1564,7 +1564,7 @@ static void snap_increment_apply(TransInfo *t, snap_increment_apply_ex(t, max_index, increment_dist, asp, r_val, r_val); } -bool transform_snap_increment_ex(TransInfo *t, bool use_local_space, float *r_val) +bool transform_snap_increment_ex(const TransInfo *t, bool use_local_space, float *r_val) { if (!activeSnap(t)) { return false; @@ -1595,7 +1595,7 @@ bool transform_snap_increment_ex(TransInfo *t, bool use_local_space, float *r_va return true; } -bool transform_snap_increment(TransInfo *t, float *r_val) +bool transform_snap_increment(const TransInfo *t, float *r_val) { return transform_snap_increment_ex(t, false, r_val); } diff --git a/source/blender/editors/transform/transform_snap.h b/source/blender/editors/transform/transform_snap.h index 1632b49fbbf..bf14e564380 100644 --- a/source/blender/editors/transform/transform_snap.h +++ b/source/blender/editors/transform/transform_snap.h @@ -54,8 +54,8 @@ void snapFrameTransform(struct TransInfo *t, bool transformModeUseSnap(const TransInfo *t); -bool transform_snap_increment_ex(TransInfo *t, bool use_local_space, float *r_val); -bool transform_snap_increment(TransInfo *t, float *val); +bool transform_snap_increment_ex(const TransInfo *t, bool use_local_space, float *r_val); +bool transform_snap_increment(const TransInfo *t, float *val); bool transform_snap_grid(TransInfo *t, float *val); void snapSequenceBounds(TransInfo *t, const int mval[2]); -- cgit v1.2.3