Blender V5.3
selection.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array_utils.hh"
10#include "BLI_index_mask.hh"
11#include "BLI_lasso_2d.hh"
12#include "BLI_math_vector.hh"
13#include "BLI_rect.hh"
14
15#include "BKE_attribute.hh"
16
17#include "ED_pointcloud.hh"
18#include "ED_select_utils.hh"
19#include "ED_view3d.hh"
20
22
24
26{
27 const VArray<bool> selection = *pointcloud.attributes().lookup<bool>(".selection");
28 return !selection || array_utils::contains(selection, selection.index_range(), true);
29}
30
32 bke::AttrType create_type)
33{
34 const bke::AttrDomain selection_domain = bke::AttrDomain::Point;
35 const StringRef attribute_name = ".selection";
36
37 bke::MutableAttributeAccessor attributes = pointcloud.attributes_for_write();
38 if (attributes.contains(attribute_name)) {
39 return attributes.lookup_for_write_span(attribute_name);
40 }
41 switch (create_type) {
43 attributes.add(
44 attribute_name, selection_domain, bke::AttrType::Bool, bke::AttributeInitValue(true));
45 break;
47 attributes.add(
48 attribute_name, selection_domain, bke::AttrType::Float, bke::AttributeInitValue(1.0f));
49 break;
50 default:
52 }
53 return attributes.lookup_for_write_span(attribute_name);
54}
55
57{
58 if (selection.type().is<bool>()) {
59 index_mask::masked_fill(selection.typed<bool>(), false, mask);
60 }
61 else if (selection.type().is<float>()) {
62 index_mask::masked_fill(selection.typed<float>(), 0.0f, mask);
63 }
64}
65
67{
68 fill_selection_true(selection, IndexMask(selection.size()));
69}
70
72{
73 if (selection.type().is<bool>()) {
74 index_mask::masked_fill(selection.typed<bool>(), true, mask);
75 }
76 else if (selection.type().is<float>()) {
77 index_mask::masked_fill(selection.typed<float>(), 1.0f, mask);
78 }
79}
80
81static void invert_selection(MutableSpan<float> selection, const IndexMask &mask)
82{
83 mask.foreach_index_optimized<int64_t>(
84 [&](const int64_t i) { selection[i] = 1.0f - selection[i]; }, exec_mode::grain_size(4096));
85}
86
87static void invert_selection(GMutableSpan selection, const IndexMask &mask)
88{
89 if (selection.type().is<bool>()) {
90 array_utils::invert_booleans(selection.typed<bool>(), mask);
91 }
92 else if (selection.type().is<float>()) {
93 invert_selection(selection.typed<float>(), mask);
94 }
95}
96
97static void select_all(PointCloud &pointcloud, const IndexMask &mask, int action)
98{
99 if (action == SEL_SELECT) {
100 std::optional<IndexRange> range = mask.to_range();
101 if (range.has_value() && (*range == IndexRange(pointcloud.totpoint))) {
102 bke::MutableAttributeAccessor attributes = pointcloud.attributes_for_write();
103 /* As an optimization, just remove the selection attributes when everything is selected. */
104 attributes.remove(".selection");
105 return;
106 }
107 }
108
111 if (action == SEL_SELECT) {
112 fill_selection_true(selection.span, mask);
113 }
114 else if (action == SEL_DESELECT) {
115 fill_selection_false(selection.span, mask);
116 }
117 else if (action == SEL_INVERT) {
118 invert_selection(selection.span, mask);
119 }
120 selection.finish();
121}
122
124{
125 select_all(pointcloud, IndexRange(pointcloud.totpoint), action);
126}
127
129 const IndexMask &mask,
130 eSelectOp sel_op)
131{
132 bool changed = false;
135 if (sel_op == SEL_OP_SET) {
136 fill_selection_false(selection.span, IndexRange(selection.span.size()));
137 changed = true;
138 }
139 switch (sel_op) {
140 case SEL_OP_ADD:
141 case SEL_OP_SET:
142 fill_selection_true(selection.span, mask);
143 break;
144 case SEL_OP_SUB:
145 fill_selection_false(selection.span, mask);
146 break;
147 case SEL_OP_XOR:
148 invert_selection(selection.span, mask);
149 break;
150 default:
151 break;
152 }
153 changed |= !mask.is_empty();
154 selection.finish();
155 return changed;
156}
157
159 const ARegion &region,
160 const float4x4 &projection,
161 const rcti &rect,
162 const eSelectOp sel_op)
163{
164 const Span<float3> positions = pointcloud.positions();
165
166 IndexMaskMemory memory;
168 positions.index_range(), memory, [&](const int point) {
169 const float2 pos_proj = ED_view3d_project_float_v2_m4(
170 &region, positions[point], projection);
171 return BLI_rcti_isect_pt_v(&rect, int2(pos_proj));
172 });
173
175}
176
178 const ARegion &region,
179 const float4x4 &projection,
180 const Span<int2> lasso_coords,
181 const eSelectOp sel_op)
182{
183 rcti bbox;
184 BLI_lasso_boundbox(&bbox, lasso_coords);
185
186 const Span<float3> positions = pointcloud.positions();
187
188 IndexMaskMemory memory;
190 positions.index_range(), memory, [&](const int point) {
191 const float2 pos_proj = ED_view3d_project_float_v2_m4(
192 &region, positions[point], projection);
193 if (!BLI_rcti_isect_pt_v(&bbox, int2(pos_proj))) {
194 return false;
195 }
197 lasso_coords, int(pos_proj.x), int(pos_proj.y), IS_CLIPPED)) {
198 return false;
199 }
200 return true;
201 });
202
204}
205
207 const ARegion &region,
208 const float4x4 &projection,
209 const int2 coord,
210 const float radius,
211 const eSelectOp sel_op)
212{
213 const float radius_sq = radius * radius;
214
215 const Span<float3> positions = pointcloud.positions();
216
217 IndexMaskMemory memory;
219 positions.index_range(), memory, [&](const int point) {
220 const float2 pos_proj = ED_view3d_project_float_v2_m4(
221 &region, positions[point], projection);
222 return math::distance_squared(pos_proj, float2(coord)) <= radius_sq;
223 });
224
226}
227
229{
230 if (a.distance_sq < b.distance_sq) {
231 return a;
232 }
233 return b;
234}
235
236std::optional<FindClosestData> find_closest_point_to_screen_co(
237 const ARegion &region,
238 const Span<float3> positions,
239 const float4x4 &projection,
240 const IndexMask &points_mask,
241 const float2 mouse_pos,
242 const float radius,
243 const FindClosestData &initial_closest)
244{
245 const float radius_sq = radius * radius;
246 const FindClosestData new_closest_data = threading::parallel_reduce(
247 points_mask.index_range(),
248 1024,
249 initial_closest,
250 [&](const IndexRange range, const FindClosestData &init) {
251 FindClosestData best_match = init;
252 points_mask.slice(range).foreach_index([&](const int point) {
253 const float3 &pos = positions[point];
254 const float2 pos_proj = ED_view3d_project_float_v2_m4(&region, pos, projection);
255
256 const float distance_proj_sq = math::distance_squared(pos_proj, mouse_pos);
257 if (distance_proj_sq > radius_sq || distance_proj_sq > best_match.distance_sq) {
258 return;
259 }
260
261 best_match = {point, distance_proj_sq};
262 });
263 return best_match;
264 },
265 closer_elem);
266
267 if (new_closest_data.distance_sq < initial_closest.distance_sq) {
268 return new_closest_data;
269 }
270
271 return {};
272}
273
275{
276 const VArray selection = *pointcloud.attributes().lookup_or_default<bool>(
277 ".selection", bke::AttrDomain::Point, true);
278 return IndexMask::from_bools(selection, memory);
279}
280
281} // namespace blender::ed::pointcloud
#define IS_CLIPPED
Definition ED_view3d.hh:269
#define BLI_assert_unreachable()
bool is() const
const CPPType & type() const
MutableSpan< T > typed() const
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr IndexRange()=default
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
IndexRange index_range() const
bool contains(StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_for_write_span(StringRef name)
void invert_booleans(MutableSpan< bool > span)
bool contains(const VArray< bool > &varray, const IndexMask &indices_to_check, bool value)
bool select_box(PointCloud &pointcloud, const ARegion &region, const float4x4 &projection, const rcti &rect, const eSelectOp sel_op)
Definition selection.cc:158
bool select_lasso(PointCloud &pointcloud, const ARegion &region, const float4x4 &projection, const Span< int2 > lasso_coords, const eSelectOp sel_op)
Definition selection.cc:177
bool select_circle(PointCloud &pointcloud, const ARegion &region, const float4x4 &projection, const int2 coord, const float radius, const eSelectOp sel_op)
Definition selection.cc:206
void fill_selection_true(GMutableSpan selection)
Definition selection.cc:66
static void invert_selection(MutableSpan< float > selection, const IndexMask &mask)
Definition selection.cc:81
static bool apply_selection_operation(PointCloud &pointcloud, const IndexMask &mask, eSelectOp sel_op)
Definition selection.cc:128
static FindClosestData closer_elem(const FindClosestData &a, const FindClosestData &b)
Definition selection.cc:228
IndexMask retrieve_selected_points(const PointCloud &pointcloud, IndexMaskMemory &memory)
Definition selection.cc:274
bke::GSpanAttributeWriter ensure_selection_attribute(PointCloud &pointcloud, bke::AttrType create_type)
Definition selection.cc:31
void fill_selection_false(GMutableSpan selection, const IndexMask &mask)
Definition selection.cc:56
std::optional< FindClosestData > find_closest_point_to_screen_co(const ARegion &region, const Span< float3 > positions, const float4x4 &projection, const IndexMask &points_mask, const float2 mouse_pos, const float radius, const FindClosestData &initial_closest)
Definition selection.cc:236
bool has_anything_selected(const PointCloud &pointcloud)
Definition selection.cc:25
void select_all(PointCloud &pointcloud, int action)
Definition selection.cc:123
constexpr ParallelGrainSize grain_size(int grain_size)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
__int64 int64_t
Definition BKE_text.h:90
MatBase< float, 4, 4 > float4x4
bool BLI_lasso_is_point_inside(Span< int2 > mcoords, int sx, int sy, int error_value)
Definition lasso_2d.cc:42
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void BLI_lasso_boundbox(rcti *rect, Span< int2 > mcoords)
Definition lasso_2d.cc:21