Blender  V3.3
select_utils.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <float.h>
8 
9 #include "BLI_kdtree.h"
10 #include "BLI_math.h"
11 #include "BLI_utildefines.h"
12 
14 
15 #include "RNA_access.h"
16 
17 #include "WM_types.h"
18 
19 #include "ED_select_utils.h"
20 
21 int ED_select_op_action(const eSelectOp sel_op, const bool is_select, const bool is_inside)
22 {
23  switch (sel_op) {
24  case SEL_OP_ADD:
25  return (!is_select && (is_inside)) ? 1 : -1;
26  case SEL_OP_SUB:
27  return (is_select && is_inside) ? 0 : -1;
28  case SEL_OP_SET:
29  return is_inside ? 1 : 0;
30  case SEL_OP_AND:
31  return (is_select && is_inside) ? -1 : (is_select ? 0 : -1);
32  case SEL_OP_XOR:
33  return (is_select && is_inside) ? 0 : ((!is_select && is_inside) ? 1 : -1);
34  }
35  BLI_assert_msg(0, "invalid sel_op");
36  return -1;
37 }
39  const bool is_select,
40  const bool is_inside)
41 {
42  switch (sel_op) {
43  case SEL_OP_ADD:
44  return (!is_select && is_inside) ? 1 : -1;
45  case SEL_OP_SUB:
46  return (is_select && is_inside) ? 0 : -1;
47  case SEL_OP_SET:
48  /* Only difference w/ function above. */
49  return is_inside ? 1 : -1;
50  case SEL_OP_AND:
51  return (is_select && is_inside) ? -1 : (is_select ? 0 : -1);
52  case SEL_OP_XOR:
53  return (is_select && is_inside) ? 0 : ((!is_select && is_inside) ? 1 : -1);
54  }
55  BLI_assert_msg(0, "invalid sel_op");
56  return -1;
57 }
58 
59 eSelectOp ED_select_op_modal(const eSelectOp sel_op, const bool is_first)
60 {
61  if (sel_op == SEL_OP_SET) {
62  if (is_first == false) {
63  return SEL_OP_ADD;
64  }
65  }
66  return sel_op;
67 }
68 
69 bool ED_select_similar_compare_float(const float delta,
70  const float thresh,
71  const eSimilarCmp compare)
72 {
73  BLI_assert(thresh >= 0.0f);
74  switch (compare) {
75  case SIM_CMP_EQ:
76  return (fabsf(delta) <= thresh);
77  case SIM_CMP_GT:
78  return ((delta + thresh) >= 0.0f);
79  case SIM_CMP_LT:
80  return ((delta - thresh) <= 0.0f);
81  default:
83  return 0;
84  }
85 }
86 
88  const float length,
89  const float thresh,
90  const eSimilarCmp compare)
91 {
92  BLI_assert(compare == SIM_CMP_EQ || length >= 0.0f); /* See precision note below. */
93 
94  /* Length of the edge we want to compare against. */
95  float nearest_edge_length;
96 
97  switch (compare) {
98  case SIM_CMP_EQ:
99  /* Compare to the edge closest to the current edge. */
100  nearest_edge_length = length;
101  break;
102  case SIM_CMP_GT:
103  /* Compare against the shortest edge. */
104  /* -FLT_MAX leads to some precision issues and the wrong edge being selected.
105  * For example, in a tree with 1, 2 and 3, which is stored squared as 1, 4, 9,
106  * it returns as the nearest length/node the "4" instead of "1". */
107  nearest_edge_length = -1.0f;
108  break;
109  case SIM_CMP_LT:
110  /* Compare against the longest edge. */
111  nearest_edge_length = FLT_MAX;
112  break;
113  default:
115  return false;
116  }
117 
118  KDTreeNearest_1d nearest;
119  if (BLI_kdtree_1d_find_nearest(tree, &nearest_edge_length, &nearest) != -1) {
120  BLI_assert(compare == SIM_CMP_EQ || nearest.co[0] >= 0.0f); /* See precision note above. */
121  float delta = length - nearest.co[0];
122  return ED_select_similar_compare_float(delta, thresh, compare);
123  }
124 
125  return false;
126 }
127 
129 {
130  const bool extend = RNA_boolean_get(ptr, "extend");
131  const bool deselect = RNA_boolean_get(ptr, "deselect");
132  const bool toggle = RNA_boolean_get(ptr, "toggle");
133 
134  if (extend) {
135  return SEL_OP_ADD;
136  }
137  if (deselect) {
138  return SEL_OP_SUB;
139  }
140  if (toggle) {
141  return SEL_OP_XOR;
142  }
143  return SEL_OP_SET;
144 }
145 
147 {
148  memset(params, 0x0, sizeof(*params));
150  params->deselect_all = RNA_boolean_get(ptr, "deselect_all");
151  params->select_passthrough = RNA_boolean_get(ptr, "select_passthrough");
152 }
153 
154 /* -------------------------------------------------------------------- */
159 {
160  struct SelectPick_Params params = {0};
162  switch (params.sel_op) {
163  case SEL_OP_ADD:
164  return "Select (Extend)";
165  case SEL_OP_SUB:
166  return "Select (Deselect)";
167  case SEL_OP_XOR:
168  return "Select (Toggle)";
169  case SEL_OP_AND:
172  case SEL_OP_SET:
173  break;
174  }
175  return "Select";
176 }
177 
179 {
180  /* Matches options in #WM_operator_properties_select_operation_simple */
181  const eSelectOp sel_op = RNA_enum_get(ptr, "mode");
182  switch (sel_op) {
183  case SEL_OP_ADD:
184  return "Circle Select (Extend)";
185  case SEL_OP_SUB:
186  return "Circle Select (Deselect)";
187  case SEL_OP_XOR:
189  case SEL_OP_AND:
192  case SEL_OP_SET:
193  break;
194  }
195  return "Circle Select";
196 }
197 
#define BLI_assert_unreachable()
Definition: BLI_assert.h:93
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
#define ATTR_FALLTHROUGH
A KD-tree for nearest neighbor search.
#define UNUSED(x)
eSimilarCmp
@ SIM_CMP_LT
@ SIM_CMP_GT
@ SIM_CMP_EQ
eSelectOp
@ SEL_OP_ADD
@ SEL_OP_SUB
@ SEL_OP_SET
@ SEL_OP_AND
@ SEL_OP_XOR
void * tree
static bool is_inside(int x, int y, int cols, int rows)
Definition: filesel.c:706
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define fabsf(x)
Definition: metal/compat.h:219
T length(const vec_base< T, Size > &a)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4863
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
const char * ED_select_pick_get_name(wmOperatorType *UNUSED(ot), PointerRNA *ptr)
Definition: select_utils.c:158
bool ED_select_similar_compare_float(const float delta, const float thresh, const eSimilarCmp compare)
Definition: select_utils.c:69
bool ED_select_similar_compare_float_tree(const KDTree_1d *tree, const float length, const float thresh, const eSimilarCmp compare)
Definition: select_utils.c:87
const char * ED_select_circle_get_name(wmOperatorType *UNUSED(ot), PointerRNA *ptr)
Definition: select_utils.c:178
void ED_select_pick_params_from_operator(PointerRNA *ptr, struct SelectPick_Params *params)
Definition: select_utils.c:146
int ED_select_op_action(const eSelectOp sel_op, const bool is_select, const bool is_inside)
Definition: select_utils.c:21
eSelectOp ED_select_op_modal(const eSelectOp sel_op, const bool is_first)
Definition: select_utils.c:59
eSelectOp ED_select_op_from_operator(PointerRNA *ptr)
Definition: select_utils.c:128
int ED_select_op_action_deselected(const eSelectOp sel_op, const bool is_select, const bool is_inside)
Definition: select_utils.c:38
PointerRNA * ptr
Definition: wm_files.c:3480
wmOperatorType * ot
Definition: wm_files.c:3479