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

spreadsheet_data_source_geometry.cc « space_spreadsheet « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d36d4b1b7f8136bbb20e04265ea006bddba85c5 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "BKE_context.h"
#include "BKE_editmesh.h"
#include "BKE_lib_id.h"
#include "BKE_mesh.h"
#include "BKE_mesh_wrapper.h"
#include "BKE_modifier.h"

#include "DNA_ID.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_space_types.h"
#include "DNA_userdef_types.h"

#include "DEG_depsgraph_query.h"

#include "ED_spreadsheet.h"

#include "bmesh.h"

#include "spreadsheet_data_source_geometry.hh"
#include "spreadsheet_intern.hh"

namespace blender::ed::spreadsheet {

void GeometryDataSource::foreach_default_column_ids(
    FunctionRef<void(const SpreadsheetColumnID &)> fn) const
{
  component_->attribute_foreach([&](StringRefNull name, const AttributeMetaData &meta_data) {
    if (meta_data.domain != domain_) {
      return true;
    }
    SpreadsheetColumnID column_id;
    column_id.name = (char *)name.c_str();
    fn(column_id);
    return true;
  });
}

std::unique_ptr<ColumnValues> GeometryDataSource::get_column_values(
    const SpreadsheetColumnID &column_id) const
{
  std::lock_guard lock{mutex_};

  bke::ReadAttributeLookup attribute = component_->attribute_try_get_for_read(column_id.name);
  if (!attribute) {
    return {};
  }
  const fn::GVArray *varray = scope_.add(std::move(attribute.varray), __func__);
  if (attribute.domain != domain_) {
    return {};
  }
  int domain_size = varray->size();
  const CustomDataType type = bke::cpp_type_to_custom_data_type(varray->type());
  switch (type) {
    case CD_PROP_FLOAT:
      return column_values_from_function(SPREADSHEET_VALUE_TYPE_FLOAT,
                                         column_id.name,
                                         domain_size,
                                         [varray](int index, CellValue &r_cell_value) {
                                           float value;
                                           varray->get(index, &value);
                                           r_cell_value.value_float = value;
                                         });
    case CD_PROP_INT32:
      return column_values_from_function(SPREADSHEET_VALUE_TYPE_INT32,
                                         column_id.name,
                                         domain_size,
                                         [varray](int index, CellValue &r_cell_value) {
                                           int value;
                                           varray->get(index, &value);
                                           r_cell_value.value_int = value;
                                         });
    case CD_PROP_BOOL:
      return column_values_from_function(SPREADSHEET_VALUE_TYPE_BOOL,
                                         column_id.name,
                                         domain_size,
                                         [varray](int index, CellValue &r_cell_value) {
                                           bool value;
                                           varray->get(index, &value);
                                           r_cell_value.value_bool = value;
                                         });
    case CD_PROP_FLOAT2: {
      return column_values_from_function(
          SPREADSHEET_VALUE_TYPE_FLOAT2,
          column_id.name,
          domain_size,
          [varray](int index, CellValue &r_cell_value) {
            float2 value;
            varray->get(index, &value);
            r_cell_value.value_float2 = value;
          },
          default_float2_column_width);
    }
    case CD_PROP_FLOAT3: {
      return column_values_from_function(
          SPREADSHEET_VALUE_TYPE_FLOAT3,
          column_id.name,
          domain_size,
          [varray](int index, CellValue &r_cell_value) {
            float3 value;
            varray->get(index, &value);
            r_cell_value.value_float3 = value;
          },
          default_float3_column_width);
    }
    case CD_PROP_COLOR: {
      return column_values_from_function(
          SPREADSHEET_VALUE_TYPE_COLOR,
          column_id.name,
          domain_size,
          [varray](int index, CellValue &r_cell_value) {
            ColorGeometry4f value;
            varray->get(index, &value);
            r_cell_value.value_color = value;
          },
          default_color_column_width);
    }
    default:
      break;
  }
  return {};
}

int GeometryDataSource::tot_rows() const
{
  return component_->attribute_domain_size(domain_);
}

using IsVertexSelectedFn = FunctionRef<bool(int vertex_index)>;

static void get_selected_vertex_indices(const Mesh &mesh,
                                        const IsVertexSelectedFn is_vertex_selected_fn,
                                        MutableSpan<bool> selection)
{
  for (const int i : IndexRange(mesh.totvert)) {
    if (!selection[i]) {
      continue;
    }
    if (!is_vertex_selected_fn(i)) {
      selection[i] = false;
    }
  }
}

static void get_selected_corner_indices(const Mesh &mesh,
                                        const IsVertexSelectedFn is_vertex_selected_fn,
                                        MutableSpan<bool> selection)
{
  for (const int i : IndexRange(mesh.totloop)) {
    const MLoop &loop = mesh.mloop[i];
    if (!selection[i]) {
      continue;
    }
    if (!is_vertex_selected_fn(loop.v)) {
      selection[i] = false;
    }
  }
}

static void get_selected_face_indices(const Mesh &mesh,
                                      const IsVertexSelectedFn is_vertex_selected_fn,
                                      MutableSpan<bool> selection)
{
  for (const int poly_index : IndexRange(mesh.totpoly)) {
    if (!selection[poly_index]) {
      continue;
    }
    const MPoly &poly = mesh.mpoly[poly_index];
    for (const int loop_index : IndexRange(poly.loopstart, poly.totloop)) {
      const MLoop &loop = mesh.mloop[loop_index];
      if (!is_vertex_selected_fn(loop.v)) {
        selection[poly_index] = false;
        break;
      }
    }
  }
}

static void get_selected_edge_indices(const Mesh &mesh,
                                      const IsVertexSelectedFn is_vertex_selected_fn,
                                      MutableSpan<bool> selection)
{
  for (const int i : IndexRange(mesh.totedge)) {
    if (!selection[i]) {
      continue;
    }
    const MEdge &edge = mesh.medge[i];
    if (!is_vertex_selected_fn(edge.v1) || !is_vertex_selected_fn(edge.v2)) {
      selection[i] = false;
    }
  }
}

static void get_selected_indices_on_domain(const Mesh &mesh,
                                           const AttributeDomain domain,
                                           const IsVertexSelectedFn is_vertex_selected_fn,
                                           MutableSpan<bool> selection)
{
  switch (domain) {
    case ATTR_DOMAIN_POINT:
      return get_selected_vertex_indices(mesh, is_vertex_selected_fn, selection);
    case ATTR_DOMAIN_FACE:
      return get_selected_face_indices(mesh, is_vertex_selected_fn, selection);
    case ATTR_DOMAIN_CORNER:
      return get_selected_corner_indices(mesh, is_vertex_selected_fn, selection);
    case ATTR_DOMAIN_EDGE:
      return get_selected_edge_indices(mesh, is_vertex_selected_fn, selection);
    default:
      return;
  }
}

bool GeometryDataSource::has_selection_filter() const
{
  Object *object_orig = DEG_get_original_object(object_eval_);
  if (object_orig->type == OB_MESH) {
    if (object_orig->mode == OB_MODE_EDIT) {
      return true;
    }
  }
  return false;
}

void GeometryDataSource::apply_selection_filter(MutableSpan<bool> rows_included) const
{
  std::lock_guard lock{mutex_};

  BLI_assert(object_eval_->mode == OB_MODE_EDIT);
  BLI_assert(component_->type() == GEO_COMPONENT_TYPE_MESH);
  Object *object_orig = DEG_get_original_object(object_eval_);
  const MeshComponent *mesh_component = static_cast<const MeshComponent *>(component_);
  const Mesh *mesh_eval = mesh_component->get_for_read();
  Mesh *mesh_orig = (Mesh *)object_orig->data;
  BMesh *bm = mesh_orig->edit_mesh->bm;
  BM_mesh_elem_table_ensure(bm, BM_VERT);

  int *orig_indices = (int *)CustomData_get_layer(&mesh_eval->vdata, CD_ORIGINDEX);
  if (orig_indices != nullptr) {
    /* Use CD_ORIGINDEX layer if it exists. */
    auto is_vertex_selected = [&](int vertex_index) -> bool {
      const int i_orig = orig_indices[vertex_index];
      if (i_orig < 0) {
        return false;
      }
      if (i_orig >= bm->totvert) {
        return false;
      }
      BMVert *vert = bm->vtable[i_orig];
      return BM_elem_flag_test(vert, BM_ELEM_SELECT);
    };
    get_selected_indices_on_domain(*mesh_eval, domain_, is_vertex_selected, rows_included);
  }
  else if (mesh_eval->totvert == bm->totvert) {
    /* Use a simple heuristic to match original vertices to evaluated ones. */
    auto is_vertex_selected = [&](int vertex_index) -> bool {
      BMVert *vert = bm->vtable[vertex_index];
      return BM_elem_flag_test(vert, BM_ELEM_SELECT);
    };
    get_selected_indices_on_domain(*mesh_eval, domain_, is_vertex_selected, rows_included);
  }
}

bool InstancesDataSource::has_selection_filter() const
{
  return false;
}

void InstancesDataSource::foreach_default_column_ids(
    FunctionRef<void(const SpreadsheetColumnID &)> fn) const
{
  if (component_->instances_amount() == 0) {
    return;
  }

  SpreadsheetColumnID column_id;
  column_id.name = (char *)"Name";
  fn(column_id);
  for (const char *name : {"Position", "Rotation", "Scale", "ID"}) {
    column_id.name = (char *)name;
    fn(column_id);
  }
}

std::unique_ptr<ColumnValues> InstancesDataSource::get_column_values(
    const SpreadsheetColumnID &column_id) const
{
  if (component_->instances_amount() == 0) {
    return {};
  }

  const int size = this->tot_rows();
  if (STREQ(column_id.name, "Name")) {
    Span<int> reference_handles = component_->instance_reference_handles();
    Span<InstanceReference> references = component_->references();
    std::unique_ptr<ColumnValues> values = column_values_from_function(
        SPREADSHEET_VALUE_TYPE_INSTANCES,
        "Name",
        size,
        [reference_handles, references](int index, CellValue &r_cell_value) {
          const InstanceReference &reference = references[reference_handles[index]];
          switch (reference.type()) {
            case InstanceReference::Type::Object: {
              Object &object = reference.object();
              r_cell_value.value_object = ObjectCellValue{&object};
              break;
            }
            case InstanceReference::Type::Collection: {
              Collection &collection = reference.collection();
              r_cell_value.value_collection = CollectionCellValue{&collection};
              break;
            }
            case InstanceReference::Type::None: {
              break;
            }
          }
        });
    values->default_width = 8.0f;
    return values;
  }
  Span<float4x4> transforms = component_->instance_transforms();
  if (STREQ(column_id.name, "Position")) {
    return column_values_from_function(
        SPREADSHEET_VALUE_TYPE_FLOAT3,
        column_id.name,
        size,
        [transforms](int index, CellValue &r_cell_value) {
          r_cell_value.value_float3 = transforms[index].translation();
        },
        default_float3_column_width);
  }
  if (STREQ(column_id.name, "Rotation")) {
    return column_values_from_function(
        SPREADSHEET_VALUE_TYPE_FLOAT3,
        column_id.name,
        size,
        [transforms](int index, CellValue &r_cell_value) {
          r_cell_value.value_float3 = transforms[index].to_euler();
        },
        default_float3_column_width);
  }
  if (STREQ(column_id.name, "Scale")) {
    return column_values_from_function(
        SPREADSHEET_VALUE_TYPE_FLOAT3,
        column_id.name,
        size,
        [transforms](int index, CellValue &r_cell_value) {
          r_cell_value.value_float3 = transforms[index].scale();
        },
        default_float3_column_width);
  }
  Span<int> ids = component_->instance_ids();
  if (STREQ(column_id.name, "ID")) {
    /* Make the column a bit wider by default, since the IDs tend to be large numbers. */
    return column_values_from_function(
        SPREADSHEET_VALUE_TYPE_INT32,
        column_id.name,
        size,
        [ids](int index, CellValue &r_cell_value) { r_cell_value.value_int = ids[index]; },
        5.5f);
  }
  return {};
}

int InstancesDataSource::tot_rows() const
{
  return component_->instances_amount();
}

static GeometrySet get_display_geometry_set(SpaceSpreadsheet *sspreadsheet,
                                            Object *object_eval,
                                            const GeometryComponentType used_component_type)
{
  GeometrySet geometry_set;
  if (sspreadsheet->object_eval_state == SPREADSHEET_OBJECT_EVAL_STATE_ORIGINAL) {
    Object *object_orig = DEG_get_original_object(object_eval);
    if (object_orig->type == OB_MESH) {
      MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
      if (object_orig->mode == OB_MODE_EDIT) {
        Mesh *mesh = (Mesh *)object_orig->data;
        BMEditMesh *em = mesh->edit_mesh;
        if (em != nullptr) {
          Mesh *new_mesh = (Mesh *)BKE_id_new_nomain(ID_ME, nullptr);
          /* This is a potentially heavy operation to do on every redraw. The best solution here is
           * to display the data directly from the bmesh without a conversion, which can be
           * implemented a bit later. */
          BM_mesh_bm_to_me_for_eval(em->bm, new_mesh, nullptr);
          mesh_component.replace(new_mesh, GeometryOwnershipType::Owned);
        }
      }
      else {
        Mesh *mesh = (Mesh *)object_orig->data;
        mesh_component.replace(mesh, GeometryOwnershipType::ReadOnly);
      }
      mesh_component.copy_vertex_group_names_from_object(*object_orig);
    }
    else if (object_orig->type == OB_POINTCLOUD) {
      PointCloud *pointcloud = (PointCloud *)object_orig->data;
      PointCloudComponent &pointcloud_component =
          geometry_set.get_component_for_write<PointCloudComponent>();
      pointcloud_component.replace(pointcloud, GeometryOwnershipType::ReadOnly);
    }
  }
  else if (sspreadsheet->object_eval_state == SPREADSHEET_OBJECT_EVAL_STATE_EVALUATED) {
    if (used_component_type == GEO_COMPONENT_TYPE_MESH && object_eval->mode == OB_MODE_EDIT) {
      Mesh *mesh = BKE_modifier_get_evaluated_mesh_from_evaluated_object(object_eval, false);
      if (mesh == nullptr) {
        return geometry_set;
      }
      BKE_mesh_wrapper_ensure_mdata(mesh);
      MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
      mesh_component.replace(mesh, GeometryOwnershipType::ReadOnly);
      mesh_component.copy_vertex_group_names_from_object(*object_eval);
    }
    else {
      if (BLI_listbase_count(&sspreadsheet->context_path) == 1) {
        /* Use final evaluated object. */
        if (object_eval->runtime.geometry_set_eval != nullptr) {
          geometry_set = *object_eval->runtime.geometry_set_eval;
        }
      }
      else {
        if (object_eval->runtime.geometry_set_previews != nullptr) {
          GHash *ghash = (GHash *)object_eval->runtime.geometry_set_previews;
          const uint64_t key = ED_spreadsheet_context_path_hash(sspreadsheet);
          GeometrySet *geometry_set_preview = (GeometrySet *)BLI_ghash_lookup_default(
              ghash, POINTER_FROM_UINT(key), nullptr);
          if (geometry_set_preview != nullptr) {
            geometry_set = *geometry_set_preview;
          }
        }
      }
    }
  }
  return geometry_set;
}

static GeometryComponentType get_display_component_type(const bContext *C, Object *object_eval)
{
  SpaceSpreadsheet *sspreadsheet = CTX_wm_space_spreadsheet(C);
  if (sspreadsheet->object_eval_state != SPREADSHEET_OBJECT_EVAL_STATE_ORIGINAL) {
    return (GeometryComponentType)sspreadsheet->geometry_component_type;
  }
  if (object_eval->type == OB_POINTCLOUD) {
    return GEO_COMPONENT_TYPE_POINT_CLOUD;
  }
  return GEO_COMPONENT_TYPE_MESH;
}

std::unique_ptr<DataSource> data_source_from_geometry(const bContext *C, Object *object_eval)
{
  SpaceSpreadsheet *sspreadsheet = CTX_wm_space_spreadsheet(C);
  const AttributeDomain domain = (AttributeDomain)sspreadsheet->attribute_domain;
  const GeometryComponentType component_type = get_display_component_type(C, object_eval);
  GeometrySet geometry_set = get_display_geometry_set(sspreadsheet, object_eval, component_type);

  if (!geometry_set.has(component_type)) {
    return {};
  }

  if (component_type == GEO_COMPONENT_TYPE_INSTANCES) {
    return std::make_unique<InstancesDataSource>(geometry_set);
  }
  return std::make_unique<GeometryDataSource>(object_eval, geometry_set, component_type, domain);
}

}  // namespace blender::ed::spreadsheet