Blender V5.3
select_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cfloat>
10#include <cmath>
11#include <cstring>
12
13#include "BLI_kdtree.hh"
14
15#include "BLT_translation.hh"
16
18
19#include "RNA_access.hh"
20
21#include "WM_types.hh"
22
23#include "ED_select_utils.hh"
24
25namespace blender {
26
27int ED_select_op_action(const eSelectOp sel_op, const bool is_select, const bool is_inside)
28{
29 switch (sel_op) {
30 case SEL_OP_ADD:
31 return (!is_select && (is_inside)) ? 1 : -1;
32 case SEL_OP_SUB:
33 return (is_select && is_inside) ? 0 : -1;
34 case SEL_OP_SET:
35 return is_inside ? 1 : 0;
36 case SEL_OP_AND:
37 return (is_select && is_inside) ? -1 : (is_select ? 0 : -1);
38 case SEL_OP_XOR:
39 return (is_select && is_inside) ? 0 : ((!is_select && is_inside) ? 1 : -1);
40 }
41 BLI_assert_msg(0, "invalid sel_op");
42 return -1;
43}
45 const bool is_select,
46 const bool is_inside)
47{
48 switch (sel_op) {
49 case SEL_OP_ADD:
50 return (!is_select && is_inside) ? 1 : -1;
51 case SEL_OP_SUB:
52 return (is_select && is_inside) ? 0 : -1;
53 case SEL_OP_SET:
54 /* Only difference w/ function above. */
55 return is_inside ? 1 : -1;
56 case SEL_OP_AND:
57 return (is_select && is_inside) ? -1 : (is_select ? 0 : -1);
58 case SEL_OP_XOR:
59 return (is_select && is_inside) ? 0 : ((!is_select && is_inside) ? 1 : -1);
60 }
61 BLI_assert_msg(0, "invalid sel_op");
62 return -1;
63}
64
65eSelectOp ED_select_op_modal(const eSelectOp sel_op, const bool is_first)
66{
67 if (sel_op == SEL_OP_SET) {
68 if (is_first == false) {
69 return SEL_OP_ADD;
70 }
71 }
72 return sel_op;
73}
74
75bool ED_select_similar_compare_float(const float delta,
76 const float thresh,
77 const eSimilarCmp compare)
78{
79 BLI_assert(thresh >= 0.0f);
80 switch (compare) {
81 case SIM_CMP_EQ:
82 return (fabsf(delta) <= thresh);
83 case SIM_CMP_GT:
84 return ((delta + thresh) >= 0.0f);
85 case SIM_CMP_LT:
86 return ((delta - thresh) <= 0.0f);
87 default:
89 return false;
90 }
91}
92
94 const float length,
95 const float thresh,
96 const eSimilarCmp compare)
97{
98 BLI_assert(compare == SIM_CMP_EQ || length >= 0.0f); /* See precision note below. */
99
100 /* Length of the edge we want to compare against. */
101 float nearest_edge_length;
102
103 switch (compare) {
104 case SIM_CMP_EQ:
105 /* Compare to the edge closest to the current edge. */
106 nearest_edge_length = length;
107 break;
108 case SIM_CMP_GT:
109 /* Compare against the shortest edge. */
110 /* -FLT_MAX leads to some precision issues and the wrong edge being selected.
111 * For example, in a tree with 1, 2 and 3, which is stored squared as 1, 4, 9,
112 * it returns as the nearest length/node the "4" instead of "1". */
113 nearest_edge_length = -1.0f;
114 break;
115 case SIM_CMP_LT:
116 /* Compare against the longest edge. */
117 nearest_edge_length = FLT_MAX;
118 break;
119 default:
121 return false;
122 }
123
124 KDTreeNearest<float> nearest;
125 if (kdtree_find_nearest<float>(tree, nearest_edge_length, &nearest) != -1) {
126 BLI_assert(compare == SIM_CMP_EQ || nearest.co >= 0.0f); /* See precision note above. */
127 float delta = length - nearest.co;
128 return ED_select_similar_compare_float(delta, thresh, compare);
129 }
130
131 return false;
132}
133
135{
136 const bool extend = RNA_boolean_get(ptr, "extend");
137 const bool deselect = RNA_boolean_get(ptr, "deselect");
138 const bool toggle = RNA_boolean_get(ptr, "toggle");
139
140 if (extend) {
141 return SEL_OP_ADD;
142 }
143 if (deselect) {
144 return SEL_OP_SUB;
145 }
146 if (toggle) {
147 return SEL_OP_XOR;
148 }
149 return SEL_OP_SET;
150}
151
153{
154 SelectPick_Params params = {};
156 params.deselect_all = RNA_boolean_get(ptr, "deselect_all");
157 params.select_passthrough = RNA_boolean_get(ptr, "select_passthrough");
158 return params;
159}
160
161/* -------------------------------------------------------------------- */
164
166{
167 PropertyRNA *prop = RNA_struct_find_property(ptr, "enumerate");
168 const bool enumerate = (prop && RNA_property_boolean_get(ptr, prop));
169
171 switch (params.sel_op) {
172 case SEL_OP_ADD:
173 if (enumerate) {
174 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Select Extend (List)");
175 }
176 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Select Extend");
177 case SEL_OP_SUB:
178 if (enumerate) {
179 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Deselect (List)");
180 }
182 case SEL_OP_XOR:
183 if (enumerate) {
184 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Select Toggle (List)");
185 }
186 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Select Toggle");
187 case SEL_OP_AND:
190 case SEL_OP_SET:
191 break;
192 }
193
194 if (enumerate) {
195 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Select (List)");
196 }
197
199}
200
202{
203 /* Matches options in #WM_operator_properties_select_operation_simple */
204 const eSelectOp sel_op = eSelectOp(RNA_enum_get(ptr, "mode"));
205 switch (sel_op) {
206 case SEL_OP_ADD:
207 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Circle Select (Extend)");
208 case SEL_OP_SUB:
209 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Circle Select (Deselect)");
210 case SEL_OP_XOR:
212 case SEL_OP_AND:
215 case SEL_OP_SET:
216 break;
217 }
218 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Circle Select");
219}
220
222
223} // namespace blender
#define ATTR_FALLTHROUGH
A KD-tree for nearest neighbor search.
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
#define FLT_MAX
float length(VecOp< float, D >) RET
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
std::string ED_select_circle_get_name(wmOperatorType *ot, PointerRNA *ptr)
int RNA_enum_get(PointerRNA *ptr, const char *name)
int ED_select_op_action(eSelectOp sel_op, bool is_select, bool is_inside)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
eSelectOp ED_select_op_from_operator(PointerRNA *ptr) ATTR_WARN_UNUSED_RESULT
eSelectOp ED_select_op_modal(eSelectOp sel_op, bool is_first)
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
bool ED_select_similar_compare_float_tree(const KDTree< float > *tree, float length, float thresh, eSimilarCmp compare)
int ED_select_op_action_deselected(eSelectOp sel_op, bool is_select, bool is_inside)
bool ED_select_similar_compare_float(float delta, float thresh, eSimilarCmp compare)
int kdtree_find_nearest(const KDTree< CoordT > *tree, const CoordT &co, KDTreeNearest< CoordT > *r_nearest)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
SelectPick_Params ED_select_pick_params_from_operator(PointerRNA *ptr) ATTR_NONNULL(1)
std::string ED_select_pick_get_name(wmOperatorType *ot, PointerRNA *ptr)
#define fabsf