Blender V4.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
9#include <cfloat>
10#include <cmath>
11#include <cstring>
12
13#include "BLI_kdtree.h"
14#include "BLI_utildefines.h"
15
16#include "BLT_translation.hh"
17
19
20#include "RNA_access.hh"
21
22#include "WM_types.hh"
23
24#include "ED_select_utils.hh"
25
26int ED_select_op_action(const eSelectOp sel_op, const bool is_select, const bool is_inside)
27{
28 switch (sel_op) {
29 case SEL_OP_ADD:
30 return (!is_select && (is_inside)) ? 1 : -1;
31 case SEL_OP_SUB:
32 return (is_select && is_inside) ? 0 : -1;
33 case SEL_OP_SET:
34 return is_inside ? 1 : 0;
35 case SEL_OP_AND:
36 return (is_select && is_inside) ? -1 : (is_select ? 0 : -1);
37 case SEL_OP_XOR:
38 return (is_select && is_inside) ? 0 : ((!is_select && is_inside) ? 1 : -1);
39 }
40 BLI_assert_msg(0, "invalid sel_op");
41 return -1;
42}
44 const bool is_select,
45 const bool is_inside)
46{
47 switch (sel_op) {
48 case SEL_OP_ADD:
49 return (!is_select && is_inside) ? 1 : -1;
50 case SEL_OP_SUB:
51 return (is_select && is_inside) ? 0 : -1;
52 case SEL_OP_SET:
53 /* Only difference w/ function above. */
54 return is_inside ? 1 : -1;
55 case SEL_OP_AND:
56 return (is_select && is_inside) ? -1 : (is_select ? 0 : -1);
57 case SEL_OP_XOR:
58 return (is_select && is_inside) ? 0 : ((!is_select && is_inside) ? 1 : -1);
59 }
60 BLI_assert_msg(0, "invalid sel_op");
61 return -1;
62}
63
64eSelectOp ED_select_op_modal(const eSelectOp sel_op, const bool is_first)
65{
66 if (sel_op == SEL_OP_SET) {
67 if (is_first == false) {
68 return SEL_OP_ADD;
69 }
70 }
71 return sel_op;
72}
73
74bool ED_select_similar_compare_float(const float delta,
75 const float thresh,
76 const eSimilarCmp compare)
77{
78 BLI_assert(thresh >= 0.0f);
79 switch (compare) {
80 case SIM_CMP_EQ:
81 return (fabsf(delta) <= thresh);
82 case SIM_CMP_GT:
83 return ((delta + thresh) >= 0.0f);
84 case SIM_CMP_LT:
85 return ((delta - thresh) <= 0.0f);
86 default:
88 return false;
89 }
90}
91
93 const float length,
94 const float thresh,
95 const eSimilarCmp compare)
96{
97 BLI_assert(compare == SIM_CMP_EQ || length >= 0.0f); /* See precision note below. */
98
99 /* Length of the edge we want to compare against. */
100 float nearest_edge_length;
101
102 switch (compare) {
103 case SIM_CMP_EQ:
104 /* Compare to the edge closest to the current edge. */
105 nearest_edge_length = length;
106 break;
107 case SIM_CMP_GT:
108 /* Compare against the shortest edge. */
109 /* -FLT_MAX leads to some precision issues and the wrong edge being selected.
110 * For example, in a tree with 1, 2 and 3, which is stored squared as 1, 4, 9,
111 * it returns as the nearest length/node the "4" instead of "1". */
112 nearest_edge_length = -1.0f;
113 break;
114 case SIM_CMP_LT:
115 /* Compare against the longest edge. */
116 nearest_edge_length = FLT_MAX;
117 break;
118 default:
120 return false;
121 }
122
123 KDTreeNearest_1d nearest;
124 if (BLI_kdtree_1d_find_nearest(tree, &nearest_edge_length, &nearest) != -1) {
125 BLI_assert(compare == SIM_CMP_EQ || nearest.co[0] >= 0.0f); /* See precision note above. */
126 float delta = length - nearest.co[0];
127 return ED_select_similar_compare_float(delta, thresh, compare);
128 }
129
130 return false;
131}
132
134{
135 const bool extend = RNA_boolean_get(ptr, "extend");
136 const bool deselect = RNA_boolean_get(ptr, "deselect");
137 const bool toggle = RNA_boolean_get(ptr, "toggle");
138
139 if (extend) {
140 return SEL_OP_ADD;
141 }
142 if (deselect) {
143 return SEL_OP_SUB;
144 }
145 if (toggle) {
146 return SEL_OP_XOR;
147 }
148 return SEL_OP_SET;
149}
150
152{
153 memset(params, 0x0, sizeof(*params));
155 params->deselect_all = RNA_boolean_get(ptr, "deselect_all");
156 params->select_passthrough = RNA_boolean_get(ptr, "select_passthrough");
157}
158
159/* -------------------------------------------------------------------- */
164{
167 switch (params.sel_op) {
168 case SEL_OP_ADD:
169 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Select (Extend)");
170 case SEL_OP_SUB:
171 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Select (Deselect)");
172 case SEL_OP_XOR:
173 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Select (Toggle)");
174 case SEL_OP_AND:
177 case SEL_OP_SET:
178 break;
179 }
181}
182
184{
185 /* Matches options in #WM_operator_properties_select_operation_simple */
186 const eSelectOp sel_op = eSelectOp(RNA_enum_get(ptr, "mode"));
187 switch (sel_op) {
188 case SEL_OP_ADD:
189 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Circle Select (Extend)");
190 case SEL_OP_SUB:
191 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Circle Select (Deselect)");
192 case SEL_OP_XOR:
194 case SEL_OP_AND:
197 case SEL_OP_SET:
198 break;
199 }
200 return CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Circle Select");
201}
202
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
#define ATTR_FALLTHROUGH
A KD-tree for nearest neighbor search.
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
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
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
#define fabsf(x)
KDTree_3d * tree
static bool is_inside(int x, int y, int cols, int rows)
Definition filesel.cc:768
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
int RNA_enum_get(PointerRNA *ptr, const char *name)
bool ED_select_similar_compare_float(const float delta, const float thresh, const eSimilarCmp compare)
bool ED_select_similar_compare_float_tree(const KDTree_1d *tree, const float length, const float thresh, const eSimilarCmp compare)
std::string ED_select_pick_get_name(wmOperatorType *, PointerRNA *ptr)
int ED_select_op_action(const eSelectOp sel_op, const bool is_select, const bool is_inside)
eSelectOp ED_select_op_modal(const eSelectOp sel_op, const bool is_first)
void ED_select_pick_params_from_operator(PointerRNA *ptr, SelectPick_Params *params)
eSelectOp ED_select_op_from_operator(PointerRNA *ptr)
int ED_select_op_action_deselected(const eSelectOp sel_op, const bool is_select, const bool is_inside)
std::string ED_select_circle_get_name(wmOperatorType *, PointerRNA *ptr)
#define FLT_MAX
Definition stdcycles.h:14
PointerRNA * ptr
Definition wm_files.cc:4126