Welcome to mirror list, hosted at ThFree Co, Russian Federation.

spreadsheet_row_filter.cc « space_spreadsheet « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6806e185cfec17b0728433d334aea49eb4c8ecf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <cstring>

#include "BLI_listbase.h"

#include "DNA_screen_types.h"
#include "DNA_space_types.h"

#include "DEG_depsgraph_query.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "RNA_access.h"

#include "spreadsheet_data_source_geometry.hh"
#include "spreadsheet_intern.hh"
#include "spreadsheet_layout.hh"
#include "spreadsheet_row_filter.hh"

namespace blender::ed::spreadsheet {

template<typename T, typename OperationFn>
static void apply_filter_operation(const VArray<T> &data,
                                   OperationFn check_fn,
                                   const IndexMask mask,
                                   Vector<int64_t> &new_indices)
{
  for (const int64_t i : mask) {
    if (check_fn(data[i])) {
      new_indices.append(i);
    }
  }
}

static void apply_row_filter(const SpreadsheetRowFilter &row_filter,
                             const Map<StringRef, const ColumnValues *> &columns,
                             const IndexMask prev_mask,
                             Vector<int64_t> &new_indices)
{
  const ColumnValues &column = *columns.lookup(row_filter.column_name);
  const GVArray &column_data = column.data();
  if (column_data.type().is<float>()) {
    const float value = row_filter.value_float;
    switch (row_filter.operation) {
      case SPREADSHEET_ROW_FILTER_EQUAL: {
        const float threshold = row_filter.threshold;
        apply_filter_operation(
            column_data.typed<float>(),
            [&](const float cell) { return std::abs(cell - value) < threshold; },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_GREATER: {
        apply_filter_operation(
            column_data.typed<float>(),
            [&](const float cell) { return cell > value; },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_LESS: {
        apply_filter_operation(
            column_data.typed<float>(),
            [&](const float cell) { return cell < value; },
            prev_mask,
            new_indices);
        break;
      }
    }
  }
  else if (column_data.type().is<int8_t>()) {
    const int value = row_filter.value_int;
    switch (row_filter.operation) {
      case SPREADSHEET_ROW_FILTER_EQUAL: {
        apply_filter_operation(
            column_data.typed<int8_t>(),
            [&](const int cell) { return cell == value; },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_GREATER: {
        apply_filter_operation(
            column_data.typed<int8_t>(),
            [value](const int cell) { return cell > value; },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_LESS: {
        apply_filter_operation(
            column_data.typed<int8_t>(),
            [&](const int cell) { return cell < value; },
            prev_mask,
            new_indices);
        break;
      }
    }
  }
  else if (column_data.type().is<int>()) {
    const int value = row_filter.value_int;
    switch (row_filter.operation) {
      case SPREADSHEET_ROW_FILTER_EQUAL: {
        apply_filter_operation(
            column_data.typed<int>(),
            [&](const int cell) { return cell == value; },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_GREATER: {
        apply_filter_operation(
            column_data.typed<int>(),
            [value](const int cell) { return cell > value; },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_LESS: {
        apply_filter_operation(
            column_data.typed<int>(),
            [&](const int cell) { return cell < value; },
            prev_mask,
            new_indices);
        break;
      }
    }
  }
  else if (column_data.type().is<float2>()) {
    const float2 value = row_filter.value_float2;
    switch (row_filter.operation) {
      case SPREADSHEET_ROW_FILTER_EQUAL: {
        const float threshold_sq = pow2f(row_filter.threshold);
        apply_filter_operation(
            column_data.typed<float2>(),
            [&](const float2 cell) { return math::distance_squared(cell, value) <= threshold_sq; },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_GREATER: {
        apply_filter_operation(
            column_data.typed<float2>(),
            [&](const float2 cell) { return cell.x > value.x && cell.y > value.y; },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_LESS: {
        apply_filter_operation(
            column_data.typed<float2>(),
            [&](const float2 cell) { return cell.x < value.x && cell.y < value.y; },
            prev_mask,
            new_indices);
        break;
      }
    }
  }
  else if (column_data.type().is<float3>()) {
    const float3 value = row_filter.value_float3;
    switch (row_filter.operation) {
      case SPREADSHEET_ROW_FILTER_EQUAL: {
        const float threshold_sq = pow2f(row_filter.threshold);
        apply_filter_operation(
            column_data.typed<float3>(),
            [&](const float3 cell) { return math::distance_squared(cell, value) <= threshold_sq; },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_GREATER: {
        apply_filter_operation(
            column_data.typed<float3>(),
            [&](const float3 cell) {
              return cell.x > value.x && cell.y > value.y && cell.z > value.z;
            },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_LESS: {
        apply_filter_operation(
            column_data.typed<float3>(),
            [&](const float3 cell) {
              return cell.x < value.x && cell.y < value.y && cell.z < value.z;
            },
            prev_mask,
            new_indices);
        break;
      }
    }
  }
  else if (column_data.type().is<ColorGeometry4f>()) {
    const ColorGeometry4f value = row_filter.value_color;
    switch (row_filter.operation) {
      case SPREADSHEET_ROW_FILTER_EQUAL: {
        const float threshold_sq = pow2f(row_filter.threshold);
        apply_filter_operation(
            column_data.typed<ColorGeometry4f>(),
            [&](const ColorGeometry4f cell) {
              return len_squared_v4v4(cell, value) <= threshold_sq;
            },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_GREATER: {
        apply_filter_operation(
            column_data.typed<ColorGeometry4f>(),
            [&](const ColorGeometry4f cell) {
              return cell.r > value.r && cell.g > value.g && cell.b > value.b && cell.a > value.a;
            },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_LESS: {
        apply_filter_operation(
            column_data.typed<ColorGeometry4f>(),
            [&](const ColorGeometry4f cell) {
              return cell.r < value.r && cell.g < value.g && cell.b < value.b && cell.a < value.a;
            },
            prev_mask,
            new_indices);
        break;
      }
    }
  }
  else if (column_data.type().is<ColorGeometry4b>()) {
    const ColorGeometry4f value = row_filter.value_color;
    switch (row_filter.operation) {
      case SPREADSHEET_ROW_FILTER_EQUAL: {
        const float4 value_floats = {
            (float)value.r, (float)value.g, (float)value.b, (float)value.a};
        const float threshold_sq = pow2f(row_filter.threshold);
        apply_filter_operation(
            column_data.typed<ColorGeometry4b>(),
            [&](const ColorGeometry4b cell_bytes) {
              const ColorGeometry4f cell = cell_bytes.decode();
              const float4 cell_floats = {
                  (float)cell.r, (float)cell.g, (float)cell.b, (float)cell.a};
              return len_squared_v4v4(value_floats, cell_floats) <= threshold_sq;
            },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_GREATER: {
        apply_filter_operation(
            column_data.typed<ColorGeometry4b>(),
            [&](const ColorGeometry4b cell_bytes) {
              const ColorGeometry4f cell = cell_bytes.decode();
              return cell.r > value.r && cell.g > value.g && cell.b > value.b && cell.a > value.a;
            },
            prev_mask,
            new_indices);
        break;
      }
      case SPREADSHEET_ROW_FILTER_LESS: {
        apply_filter_operation(
            column_data.typed<ColorGeometry4b>(),
            [&](const ColorGeometry4b cell_bytes) {
              const ColorGeometry4f cell = cell_bytes.decode();
              return cell.r < value.r && cell.g < value.g && cell.b < value.b && cell.a < value.a;
            },
            prev_mask,
            new_indices);
        break;
      }
    }
  }
  else if (column_data.type().is<InstanceReference>()) {
    const StringRef value = row_filter.value_string;

    apply_filter_operation(
        column_data.typed<InstanceReference>(),
        [&](const InstanceReference cell) {
          switch (cell.type()) {
            case InstanceReference::Type::Object: {
              return value == (reinterpret_cast<ID &>(cell.object()).name + 2);
            }
            case InstanceReference::Type::Collection: {
              return value == (reinterpret_cast<ID &>(cell.collection()).name + 2);
            }
            case InstanceReference::Type::GeometrySet: {
              return false;
            }
            case InstanceReference::Type::None: {
              return false;
            }
          }
          BLI_assert_unreachable();
          return false;
        },
        prev_mask,
        new_indices);
  }
}

static bool use_row_filters(const SpaceSpreadsheet &sspreadsheet)
{
  if (!(sspreadsheet.filter_flag & SPREADSHEET_FILTER_ENABLE)) {
    return false;
  }
  if (BLI_listbase_is_empty(&sspreadsheet.row_filters)) {
    return false;
  }
  return true;
}

static bool use_selection_filter(const SpaceSpreadsheet &sspreadsheet,
                                 const DataSource &data_source)
{
  if (!(sspreadsheet.filter_flag & SPREADSHEET_FILTER_SELECTED_ONLY)) {
    return false;
  }
  if (!data_source.has_selection_filter()) {
    return false;
  }
  return true;
}

IndexMask spreadsheet_filter_rows(const SpaceSpreadsheet &sspreadsheet,
                                  const SpreadsheetLayout &spreadsheet_layout,
                                  const DataSource &data_source,
                                  ResourceScope &scope)
{
  const int tot_rows = data_source.tot_rows();

  const bool use_selection = use_selection_filter(sspreadsheet, data_source);
  const bool use_filters = use_row_filters(sspreadsheet);

  /* Avoid allocating an array if no row filtering is necessary. */
  if (!(use_filters || use_selection)) {
    return IndexMask(tot_rows);
  }

  IndexMask mask(tot_rows);

  Vector<int64_t> mask_indices;
  mask_indices.reserve(tot_rows);

  if (use_selection) {
    const GeometryDataSource *geometry_data_source = dynamic_cast<const GeometryDataSource *>(
        &data_source);
    mask = geometry_data_source->apply_selection_filter(mask_indices);
  }

  if (use_filters) {
    Map<StringRef, const ColumnValues *> columns;
    for (const ColumnLayout &column : spreadsheet_layout.columns) {
      columns.add(column.values->name(), column.values);
    }

    LISTBASE_FOREACH (const SpreadsheetRowFilter *, row_filter, &sspreadsheet.row_filters) {
      if (row_filter->flag & SPREADSHEET_ROW_FILTER_ENABLED) {
        if (!columns.contains(row_filter->column_name)) {
          continue;
        }
        Vector<int64_t> new_indices;
        new_indices.reserve(mask_indices.size());
        apply_row_filter(*row_filter, columns, mask, new_indices);
        std::swap(new_indices, mask_indices);
        mask = IndexMask(mask_indices);
      }
    }
  }

  if (mask_indices.is_empty()) {
    BLI_assert(mask.is_empty() || mask.is_range());
    return mask;
  }

  return IndexMask(scope.add_value(std::move(mask_indices)));
}

SpreadsheetRowFilter *spreadsheet_row_filter_new()
{
  SpreadsheetRowFilter *row_filter = MEM_cnew<SpreadsheetRowFilter>(__func__);
  row_filter->flag = (SPREADSHEET_ROW_FILTER_UI_EXPAND | SPREADSHEET_ROW_FILTER_ENABLED);
  row_filter->operation = SPREADSHEET_ROW_FILTER_LESS;
  row_filter->threshold = 0.01f;
  row_filter->column_name[0] = '\0';

  return row_filter;
}

SpreadsheetRowFilter *spreadsheet_row_filter_copy(const SpreadsheetRowFilter *src_row_filter)
{
  SpreadsheetRowFilter *new_filter = spreadsheet_row_filter_new();

  memcpy(new_filter, src_row_filter, sizeof(SpreadsheetRowFilter));
  new_filter->next = nullptr;
  new_filter->prev = nullptr;

  return new_filter;
}

void spreadsheet_row_filter_free(SpreadsheetRowFilter *row_filter)
{
  MEM_SAFE_FREE(row_filter->value_string);
  MEM_freeN(row_filter);
}

}  // namespace blender::ed::spreadsheet