Cleanup: Type safety and asserts around ED_select_similar_compare

This commit is contained in:
Chris Blackbourn 2022-06-21 16:36:25 +12:00
parent d7fbc5708a
commit 4144a85bda
2 changed files with 14 additions and 8 deletions

View File

@ -40,11 +40,11 @@ typedef enum {
} eSelectOp;
/* Select Similar */
enum {
typedef enum {
SIM_CMP_EQ = 0,
SIM_CMP_GT,
SIM_CMP_LT,
};
} eSimilarCmp;
#define SEL_OP_USE_OUTSIDE(sel_op) (ELEM(sel_op, SEL_OP_AND))
#define SEL_OP_USE_PRE_DESELECT(sel_op) (ELEM(sel_op, SEL_OP_SET))
@ -63,11 +63,11 @@ int ED_select_op_action(eSelectOp sel_op, bool is_select, bool is_inside);
*/
int ED_select_op_action_deselected(eSelectOp sel_op, bool is_select, bool is_inside);
int ED_select_similar_compare_float(float delta, float thresh, int compare);
bool ED_select_similar_compare_float(float delta, float thresh, eSimilarCmp compare);
bool ED_select_similar_compare_float_tree(const struct KDTree_1d *tree,
float length,
float thresh,
int compare);
eSimilarCmp compare);
/**
* Utility to use for selection operations that run multiple times (circle select).

View File

@ -66,15 +66,18 @@ eSelectOp ED_select_op_modal(const eSelectOp sel_op, const bool is_first)
return sel_op;
}
int ED_select_similar_compare_float(const float delta, const float thresh, const int compare)
bool ED_select_similar_compare_float(const float delta,
const float thresh,
const eSimilarCmp compare)
{
BLI_assert(thresh >= 0.0f);
switch (compare) {
case SIM_CMP_EQ:
return (fabsf(delta) <= thresh);
case SIM_CMP_GT:
return ((delta + thresh) >= 0.0);
return ((delta + thresh) >= 0.0f);
case SIM_CMP_LT:
return ((delta - thresh) <= 0.0);
return ((delta - thresh) <= 0.0f);
default:
BLI_assert_unreachable();
return 0;
@ -84,8 +87,10 @@ int ED_select_similar_compare_float(const float delta, const float thresh, const
bool ED_select_similar_compare_float_tree(const KDTree_1d *tree,
const float length,
const float thresh,
const int compare)
const eSimilarCmp compare)
{
BLI_assert(compare == SIM_CMP_EQ || length >= 0.0f); /* See precision note below. */
/* Length of the edge we want to compare against. */
float nearest_edge_length;
@ -112,6 +117,7 @@ bool ED_select_similar_compare_float_tree(const KDTree_1d *tree,
KDTreeNearest_1d nearest;
if (BLI_kdtree_1d_find_nearest(tree, &nearest_edge_length, &nearest) != -1) {
BLI_assert(compare == SIM_CMP_EQ || nearest.co[0] >= 0.0f); /* See precision note above. */
float delta = length - nearest.co[0];
return ED_select_similar_compare_float(delta, thresh, compare);
}