Blender V5.3
spreadsheet_row_filter.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
5#include <cstring>
6
7#include "BLI_color.hh"
8#include "BLI_listbase.hh"
9#include "BLI_math_vector.hh"
10
11#include "DNA_space_types.h"
12
13#include "BKE_instances.hh"
14
16#include "spreadsheet_layout.hh"
18
20
21template<typename T, typename OperationFn>
23 OperationFn check_fn,
24 const IndexMask &mask,
25 LinearAllocator<> &memory)
26{
28 mask, memory, [&](const int64_t i) { return check_fn(data[i]); });
29}
30
33 const IndexMask &prev_mask,
34 LinearAllocator<> &memory)
35{
36 const ColumnValues &column = *columns.lookup(row_filter.column_name);
37 const GVArray &column_data = column.data();
38 if (column_data.type().is<float>()) {
39 const float value = row_filter.value_float;
40 switch (row_filter.operation) {
42 const float threshold = row_filter.threshold;
44 column_data.typed<float>(),
45 [&](const float cell) { return std::abs(cell - value) < threshold; },
46 prev_mask,
47 memory);
48 }
51 column_data.typed<float>(),
52 [&](const float cell) { return cell > value; },
53 prev_mask,
54 memory);
55 }
58 column_data.typed<float>(),
59 [&](const float cell) { return cell < value; },
60 prev_mask,
61 memory);
62 }
63 }
64 }
65 else if (column_data.type().is<bool>()) {
66 const bool value = (row_filter.flag & SPREADSHEET_ROW_FILTER_BOOL_VALUE) != 0;
68 column_data.typed<bool>(),
69 [&](const bool cell) { return cell == value; },
70 prev_mask,
71 memory);
72 }
73 else if (column_data.type().is<int8_t>()) {
74 const int value = row_filter.value_int;
75 switch (row_filter.operation) {
78 column_data.typed<int8_t>(),
79 [&](const int cell) { return cell == value; },
80 prev_mask,
81 memory);
82 }
85 column_data.typed<int8_t>(),
86 [value](const int cell) { return cell > value; },
87 prev_mask,
88 memory);
89 }
92 column_data.typed<int8_t>(),
93 [&](const int cell) { return cell < value; },
94 prev_mask,
95 memory);
96 }
97 }
98 }
99 else if (column_data.type().is<int>()) {
100 const int value = row_filter.value_int;
101 switch (row_filter.operation) {
104 column_data.typed<int>(),
105 [&](const int cell) { return cell == value; },
106 prev_mask,
107 memory);
108 }
111 column_data.typed<int>(),
112 [value](const int cell) { return cell > value; },
113 prev_mask,
114 memory);
115 }
118 column_data.typed<int>(),
119 [&](const int cell) { return cell < value; },
120 prev_mask,
121 memory);
122 }
123 }
124 }
125 else if (column_data.type().is<int64_t>()) {
126 const int64_t value = row_filter.value_int;
127 switch (row_filter.operation) {
130 column_data.typed<int64_t>(),
131 [&](const int64_t cell) { return cell == value; },
132 prev_mask,
133 memory);
134 }
137 column_data.typed<int64_t>(),
138 [value](const int64_t cell) { return cell > value; },
139 prev_mask,
140 memory);
141 }
144 column_data.typed<int64_t>(),
145 [&](const int64_t cell) { return cell < value; },
146 prev_mask,
147 memory);
148 }
149 }
150 }
151 else if (column_data.type().is<int2>()) {
152 const int2 value = row_filter.value_int2;
153 switch (row_filter.operation) {
156 column_data.typed<int2>(),
157 [&](const int2 cell) { return cell == value; },
158 prev_mask,
159 memory);
160 }
163 column_data.typed<int2>(),
164 [&](const int2 cell) { return cell.x > value.x && cell.y > value.y; },
165 prev_mask,
166 memory);
167 }
170 column_data.typed<int2>(),
171 [&](const int2 cell) { return cell.x < value.x && cell.y < value.y; },
172 prev_mask,
173 memory);
174 }
175 }
176 }
177 else if (column_data.type().is<int3>()) {
178 const int3 value = row_filter.value_int3;
179 switch (row_filter.operation) {
182 column_data.typed<int3>(),
183 [&](const int3 cell) { return cell == value; },
184 prev_mask,
185 memory);
186 }
189 column_data.typed<int3>(),
190 [&](const int3 cell) {
191 return cell.x > value.x && cell.y > value.y && cell.z > value.z;
192 },
193 prev_mask,
194 memory);
195 }
198 column_data.typed<int3>(),
199 [&](const int3 cell) {
200 return cell.x < value.x && cell.y < value.y && cell.z < value.z;
201 },
202 prev_mask,
203 memory);
204 }
205 }
206 }
207 else if (column_data.type().is<short2>()) {
208 const short2 value = short2(int2(row_filter.value_int2));
209 switch (row_filter.operation) {
212 column_data.typed<short2>(),
213 [&](const short2 cell) { return cell == value; },
214 prev_mask,
215 memory);
216 }
219 column_data.typed<short2>(),
220 [&](const short2 cell) { return cell.x > value.x && cell.y > value.y; },
221 prev_mask,
222 memory);
223 }
226 column_data.typed<short2>(),
227 [&](const short2 cell) { return cell.x < value.x && cell.y < value.y; },
228 prev_mask,
229 memory);
230 }
231 }
232 }
233 else if (column_data.type().is<float2>()) {
234 const float2 value = row_filter.value_float2;
235 switch (row_filter.operation) {
237 const float threshold_sq = pow2f(row_filter.threshold);
239 column_data.typed<float2>(),
240 [&](const float2 cell) { return math::distance_squared(cell, value) <= threshold_sq; },
241 prev_mask,
242 memory);
243 }
246 column_data.typed<float2>(),
247 [&](const float2 cell) { return cell.x > value.x && cell.y > value.y; },
248 prev_mask,
249 memory);
250 }
253 column_data.typed<float2>(),
254 [&](const float2 cell) { return cell.x < value.x && cell.y < value.y; },
255 prev_mask,
256 memory);
257 }
258 }
259 }
260 else if (column_data.type().is<float3>()) {
261 const float3 value = row_filter.value_float3;
262 switch (row_filter.operation) {
264 const float threshold_sq = pow2f(row_filter.threshold);
266 column_data.typed<float3>(),
267 [&](const float3 cell) { return math::distance_squared(cell, value) <= threshold_sq; },
268 prev_mask,
269 memory);
270 }
273 column_data.typed<float3>(),
274 [&](const float3 cell) {
275 return cell.x > value.x && cell.y > value.y && cell.z > value.z;
276 },
277 prev_mask,
278 memory);
279 }
282 column_data.typed<float3>(),
283 [&](const float3 cell) {
284 return cell.x < value.x && cell.y < value.y && cell.z < value.z;
285 },
286 prev_mask,
287 memory);
288 }
289 }
290 }
291 else if (column_data.type().is<float4>()) {
292 const float4 value = row_filter.value_float4;
293 switch (row_filter.operation) {
295 const float threshold_sq = pow2f(row_filter.threshold);
297 column_data.typed<float4>(),
298 [&](const float4 cell) { return math::distance_squared(cell, value) <= threshold_sq; },
299 prev_mask,
300 memory);
301 }
304 column_data.typed<float4>(),
305 [&](const float4 cell) {
306 return cell.x > value.x && cell.y > value.y && cell.z > value.z && cell.w > value.w;
307 },
308 prev_mask,
309 memory);
310 }
313 column_data.typed<float4>(),
314 [&](const float4 cell) {
315 return cell.x < value.x && cell.y < value.y && cell.z < value.z && cell.w < value.w;
316 },
317 prev_mask,
318 memory);
319 }
320 }
321 }
322 else if (column_data.type().is<ColorGeometry4f>()) {
323 const ColorGeometry4f value = row_filter.value_color;
324 switch (row_filter.operation) {
326 const float threshold_sq = pow2f(row_filter.threshold);
328 column_data.typed<ColorGeometry4f>(),
329 [&](const ColorGeometry4f cell) {
330 return math::distance_squared(float4(cell), float4(value)) <= threshold_sq;
331 },
332 prev_mask,
333 memory);
334 }
337 column_data.typed<ColorGeometry4f>(),
338 [&](const ColorGeometry4f cell) {
339 return cell.r > value.r && cell.g > value.g && cell.b > value.b && cell.a > value.a;
340 },
341 prev_mask,
342 memory);
343 }
346 column_data.typed<ColorGeometry4f>(),
347 [&](const ColorGeometry4f cell) {
348 return cell.r < value.r && cell.g < value.g && cell.b < value.b && cell.a < value.a;
349 },
350 prev_mask,
351 memory);
352 }
353 }
354 }
355 else if (column_data.type().is<ColorGeometry4b>()) {
356 const ColorGeometry4f value = row_filter.value_color;
357 switch (row_filter.operation) {
359 const float4 value_floats = {
360 float(value.r), float(value.g), float(value.b), float(value.a)};
361 const float threshold_sq = pow2f(row_filter.threshold);
363 column_data.typed<ColorGeometry4b>(),
364 [&](const ColorGeometry4b cell_bytes) {
365 const ColorGeometry4f cell = color::decode(cell_bytes);
366 const float4 cell_floats = {
367 float(cell.r), float(cell.g), float(cell.b), float(cell.a)};
368 return math::distance_squared(value_floats, cell_floats) <= threshold_sq;
369 },
370 prev_mask,
371 memory);
372 }
375 column_data.typed<ColorGeometry4b>(),
376 [&](const ColorGeometry4b cell_bytes) {
377 const ColorGeometry4f cell = color::decode(cell_bytes);
378 return cell.r > value.r && cell.g > value.g && cell.b > value.b && cell.a > value.a;
379 },
380 prev_mask,
381 memory);
382 }
385 column_data.typed<ColorGeometry4b>(),
386 [&](const ColorGeometry4b cell_bytes) {
387 const ColorGeometry4f cell = color::decode(cell_bytes);
388 return cell.r < value.r && cell.g < value.g && cell.b < value.b && cell.a < value.a;
389 },
390 prev_mask,
391 memory);
392 }
393 }
394 }
395 else if (column_data.type().is<bke::InstanceReference>()) {
396 const StringRef value = row_filter.value_string;
398 column_data.typed<bke::InstanceReference>(),
399 [&](const bke::InstanceReference cell) {
400 switch (cell.type()) {
401 case bke::InstanceReference::Type::Object: {
402 return value == (reinterpret_cast<ID &>(cell.object()).name + 2);
403 }
404 case bke::InstanceReference::Type::Collection: {
405 return value == (reinterpret_cast<ID &>(cell.collection()).name + 2);
406 }
407 case bke::InstanceReference::Type::GeometrySet: {
408 return value == cell.geometry_set().name();
409 }
410 case bke::InstanceReference::Type::None: {
411 return false;
412 }
413 }
415 return false;
416 },
417 prev_mask,
418 memory);
419 }
420 return prev_mask;
421}
422
423static bool use_row_filters(const SpaceSpreadsheet &sspreadsheet)
424{
425 if (!(sspreadsheet.filter_flag & SPREADSHEET_FILTER_ENABLE)) {
426 return false;
427 }
428 if (sspreadsheet.row_filters.is_empty()) {
429 return false;
430 }
431 return true;
432}
433
434static bool use_selection_filter(const SpaceSpreadsheet &sspreadsheet,
435 const DataSource &data_source)
436{
437 if (!(sspreadsheet.filter_flag & SPREADSHEET_FILTER_SELECTED_ONLY)) {
438 return false;
439 }
440 if (!data_source.has_selection_filter()) {
441 return false;
442 }
443 return true;
444}
445
447 const SpreadsheetLayout &spreadsheet_layout,
448 const DataSource &data_source,
449 ResourceScope &scope)
450{
451 const int tot_rows = data_source.tot_rows();
452
453 const bool use_selection = use_selection_filter(sspreadsheet, data_source);
454 const bool use_filters = use_row_filters(sspreadsheet);
455
456 /* Avoid allocating an array if no row filtering is necessary. */
457 if (!(use_filters || use_selection)) {
458 return IndexMask(tot_rows);
459 }
460
461 IndexMask mask(tot_rows);
462
463 if (use_selection) {
464 const GeometryDataSource *geometry_data_source = dynamic_cast<const GeometryDataSource *>(
465 &data_source);
466 mask = geometry_data_source->apply_selection_filter(scope.allocator());
467 }
468
469 if (use_filters) {
471 for (const ColumnLayout &column : spreadsheet_layout.columns) {
472 columns.add(column.values->name(), column.values);
473 }
474
475 for (const SpreadsheetRowFilter &row_filter : sspreadsheet.row_filters) {
476 if (row_filter.flag & SPREADSHEET_ROW_FILTER_ENABLED) {
477 if (!columns.contains(row_filter.column_name)) {
478 continue;
479 }
480 mask = apply_row_filter(row_filter, columns, mask, scope.allocator());
481 }
482 }
483 }
484
485 return mask;
486}
487
489{
490 SpreadsheetRowFilter *row_filter = MEM_new<SpreadsheetRowFilter>(__func__);
493 row_filter->threshold = 0.01f;
494 row_filter->column_name[0] = '\0';
495
496 return row_filter;
497}
498
500{
502
503 memcpy(new_filter, src_row_filter, sizeof(SpreadsheetRowFilter));
504 new_filter->next = nullptr;
505 new_filter->prev = nullptr;
506
507 return new_filter;
508}
509
511{
512 MEM_SAFE_DELETE(row_filter->value_string);
513 MEM_delete(row_filter);
514}
515
516} // namespace blender::ed::spreadsheet
#define BLI_assert_unreachable()
bool is() const
ChannelStorageType r
ChannelStorageType g
ChannelStorageType b
ChannelStorageType a
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool contains(const Key &key) const
Definition BLI_map.hh:353
LinearAllocator & allocator()
IndexMask apply_selection_filter(LinearAllocator<> &memory) const
SpreadsheetRowFilter * spreadsheet_row_filter_new()
IndexMask spreadsheet_filter_rows(const SpaceSpreadsheet &sspreadsheet, const SpreadsheetLayout &spreadsheet_layout, const DataSource &data_source, ResourceScope &scope)
SpreadsheetRowFilter * spreadsheet_row_filter_copy(const SpreadsheetRowFilter *src_row_filter)
static bool use_selection_filter(const SpaceSpreadsheet &sspreadsheet, const DataSource &data_source)
static IndexMask apply_filter_operation(const VArray< T > &data, OperationFn check_fn, const IndexMask &mask, LinearAllocator<> &memory)
void spreadsheet_row_filter_free(SpreadsheetRowFilter *row_filter)
static IndexMask apply_row_filter(const SpreadsheetRowFilter &row_filter, const Map< StringRef, const ColumnValues * > &columns, const IndexMask &prev_mask, LinearAllocator<> &memory)
static bool use_row_filters(const SpaceSpreadsheet &sspreadsheet)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
__int64 int64_t
Definition BKE_text.h:90
@ SPREADSHEET_FILTER_SELECTED_ONLY
@ SPREADSHEET_FILTER_ENABLE
MINLINE float pow2f(float x)
@ SPREADSHEET_ROW_FILTER_BOOL_VALUE
@ SPREADSHEET_ROW_FILTER_UI_EXPAND
@ SPREADSHEET_ROW_FILTER_ENABLED
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
BMesh const char void * data
@ SPREADSHEET_ROW_FILTER_GREATER
@ SPREADSHEET_ROW_FILTER_EQUAL
@ SPREADSHEET_ROW_FILTER_LESS
VecBase< int16_t, 2 > short2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< struct SpreadsheetRowFilter > row_filters
struct SpreadsheetRowFilter * prev
struct SpreadsheetRowFilter * next