Transform: Grid snap target refactor

The code takes many turns to get a suitable "target" for the snap to grid.

Perhaps there were other reasons awaited for `transformCenter_from_type`
and `TransCenterData center_cache[5]`.

But since nothing is defined, it is better to simplify the code.

No user functional changes
This commit is contained in:
2021-02-05 11:01:30 -03:00
parent bc56c12770
commit be60b3b239
4 changed files with 66 additions and 89 deletions

View File

@@ -166,8 +166,10 @@ typedef enum {
typedef enum {
SNAP_FORCED = 1 << 0,
TARGET_INIT = 1 << 1,
POINT_INIT = 1 << 2,
MULTI_POINTS = 1 << 3,
/* Special flag for snap to grid. */
TARGET_GRID_INIT = 1 << 2,
POINT_INIT = 1 << 3,
MULTI_POINTS = 1 << 4,
} eTSnap;
/** #TransCon.mode, #TransInfo.con.mode */
@@ -310,6 +312,7 @@ typedef struct TransSnap {
float snapPoint[3];
/** to this point (in global-space). */
float snapTarget[3];
float snapTargetGrid[3];
float snapNormal[3];
char snapNodeBorder;
ListBase points;
@@ -539,9 +542,6 @@ typedef struct TransInfo {
float center_global[3];
/** center in screen coordinates. */
float center2d[2];
/* Lazy initialize center data for when we need other center values.
* V3D_AROUND_ACTIVE + 1 (static assert checks this) */
TransCenterData center_cache[5];
/** maximum index on the input vector. */
short idx_max;
/** Snapping Gears. */
@@ -764,7 +764,6 @@ void recalcData(TransInfo *t);
void calculateCenter2D(TransInfo *t);
void calculateCenterLocal(TransInfo *t, const float center_global[3]);
const TransCenterData *transformCenter_from_type(TransInfo *t, int around);
void calculateCenter(TransInfo *t);
/* API functions for getting center points */

View File

@@ -993,10 +993,6 @@ void createTransData(bContext *C, TransInfo *t)
else {
convert_type = TC_CURSOR_VIEW3D;
}
/* Since we're transforming the cursor, initialize this value before it's modified.
* Needed for #snap_grid_apply to access the cursor location. */
transformCenter_from_type(t, V3D_AROUND_CURSOR);
}
else if (!(t->options & CTX_PAINT_CURVE) && (t->spacetype == SPACE_VIEW3D) && ob &&
(ob->mode == OB_MODE_SCULPT) && ob->sculpt) {

View File

@@ -1184,13 +1184,6 @@ void calculateCenter(TransInfo *t)
}
calculateCenterLocal(t, t->center_global);
/* avoid calculating again */
{
TransCenterData *cd = &t->center_cache[t->around];
copy_v3_v3(cd->global, t->center_global);
cd->is_set = true;
}
calculateCenter2D(t);
/* For panning from the camera-view. */
@@ -1237,23 +1230,6 @@ void calculateCenter(TransInfo *t)
}
}
BLI_STATIC_ASSERT(ARRAY_SIZE(((TransInfo *)NULL)->center_cache) == (V3D_AROUND_ACTIVE + 1),
"test size");
/**
* Lazy initialize transform center data, when we need to access center values from other types.
*/
const TransCenterData *transformCenter_from_type(TransInfo *t, int around)
{
BLI_assert(around <= V3D_AROUND_ACTIVE);
TransCenterData *cd = &t->center_cache[around];
if (cd->is_set == false) {
calculateCenter_FromAround(t, around, cd->global);
cd->is_set = true;
}
return cd;
}
void calculatePropRatio(TransInfo *t)
{
int i;

View File

@@ -926,6 +926,64 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
/** \name Target
* \{ */
static void snap_target_median_impl(TransInfo *t, float r_median[3])
{
int i_accum = 0;
zero_v3(r_median);
FOREACH_TRANS_DATA_CONTAINER (t, tc) {
TransData *td = tc->data;
int i;
float v[3];
zero_v3(v);
for (i = 0; i < tc->data_len && td->flag & TD_SELECTED; i++, td++) {
add_v3_v3(v, td->center);
}
if (i == 0) {
/* Is this possible? */
continue;
}
mul_v3_fl(v, 1.0 / i);
if (tc->use_local_mat) {
mul_m4_v3(tc->mat, v);
}
add_v3_v3(r_median, v);
i_accum++;
}
mul_v3_fl(r_median, 1.0 / i_accum);
// TargetSnapOffset(t, NULL);
}
static void snap_target_grid_ensure(TransInfo *t)
{
/* Only need to calculate once. */
if ((t->tsnap.status & TARGET_GRID_INIT) == 0) {
if (t->data_type == TC_CURSOR_VIEW3D) {
/* Use a fallback when transforming the cursor.
* In this case the center is _not_ derived from the cursor which is being transformed. */
copy_v3_v3(t->tsnap.snapTargetGrid, TRANS_DATA_CONTAINER_FIRST_SINGLE(t)->data->iloc);
}
else if (t->around == V3D_AROUND_CURSOR) {
/* Use a fallback for cursor selection,
* this isn't useful as a global center for absolute grid snapping
* since its not based on the position of the selection. */
snap_target_median_impl(t, t->tsnap.snapTargetGrid);
}
else {
copy_v3_v3(t->tsnap.snapTargetGrid, t->center_global);
}
t->tsnap.status |= TARGET_GRID_INIT;
}
}
static void TargetSnapOffset(TransInfo *t, TransData *td)
{
if (t->spacetype == SPACE_NODE && td != NULL) {
@@ -997,41 +1055,7 @@ static void TargetSnapMedian(TransInfo *t)
{
/* Only need to calculate once. */
if ((t->tsnap.status & TARGET_INIT) == 0) {
int i_accum = 0;
t->tsnap.snapTarget[0] = 0;
t->tsnap.snapTarget[1] = 0;
t->tsnap.snapTarget[2] = 0;
FOREACH_TRANS_DATA_CONTAINER (t, tc) {
TransData *td = tc->data;
int i;
float v[3];
zero_v3(v);
for (i = 0; i < tc->data_len && td->flag & TD_SELECTED; i++, td++) {
add_v3_v3(v, td->center);
}
if (i == 0) {
/* Is this possible? */
continue;
}
mul_v3_fl(v, 1.0 / i);
if (tc->use_local_mat) {
mul_m4_v3(tc->mat, v);
}
add_v3_v3(t->tsnap.snapTarget, v);
i_accum++;
}
mul_v3_fl(t->tsnap.snapTarget, 1.0 / i_accum);
TargetSnapOffset(t, NULL);
snap_target_median_impl(t, t->tsnap.snapTarget);
t->tsnap.status |= TARGET_INIT;
}
}
@@ -1431,28 +1455,10 @@ static void snap_grid_apply(
TransInfo *t, const int max_index, const float grid_dist, const float loc[3], float r_out[3])
{
BLI_assert(max_index <= 2);
const float *center_global = t->center_global;
snap_target_grid_ensure(t);
const float *center_global = t->tsnap.snapTargetGrid;
const float *asp = t->aspect;
if (t->options & CTX_CURSOR) {
/* Note that we must already have called #transformCenter_from_type, otherwise
* we would be lazy-initializing data which is being transformed,
* causing the transformed cursor location to be used instead of it's initial location. */
BLI_assert(t->center_cache[V3D_AROUND_CURSOR].is_set);
/* Use a fallback when transforming the cursor.
* In this case the center is _not_ derived from the cursor which is being transformed. */
const TransCenterData *cd = transformCenter_from_type(t, V3D_AROUND_CURSOR);
center_global = cd->global;
}
else if (t->around == V3D_AROUND_CURSOR) {
/* Use a fallback for cursor selection,
* this isn't useful as a global center for absolute grid snapping
* since its not based on the position of the selection. */
const TransCenterData *cd = transformCenter_from_type(t, V3D_AROUND_CENTER_MEDIAN);
center_global = cd->global;
}
float in[3];
if (t->con.mode & CON_APPLY) {
BLI_assert(t->tsnap.snapElem == 0);