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

node_geo_flip_faces.cc « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 95a0013a9e156b823d239e7baff2016de537e322 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "BKE_mesh.h"
#include "BKE_mesh_runtime.h"

#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"

#include "BKE_attribute_math.hh"

#include "node_geometry_util.hh"

namespace blender::nodes::node_geo_flip_faces_cc {

static void node_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::Geometry>(N_("Mesh")).supported_type(GEO_COMPONENT_TYPE_MESH);
  b.add_input<decl::Bool>(N_("Selection")).default_value(true).hide_value().supports_field();
  b.add_output<decl::Geometry>(N_("Mesh"));
}

static void mesh_flip_faces(Mesh &mesh, const Field<bool> &selection_field)
{
  if (mesh.totpoly == 0) {
    return;
  }
  bke::MeshFieldContext field_context{mesh, ATTR_DOMAIN_FACE};
  fn::FieldEvaluator evaluator{field_context, mesh.totpoly};
  evaluator.add(selection_field);
  evaluator.evaluate();
  const IndexMask selection = evaluator.get_evaluated_as_mask(0);

  const Span<MPoly> polys = mesh.polys();
  MutableSpan<MLoop> loops = mesh.loops_for_write();

  for (const int i : selection.index_range()) {
    const MPoly &poly = polys[selection[i]];
    int start = poly.loopstart;
    for (const int j : IndexRange(poly.totloop / 2)) {
      const int index1 = start + j + 1;
      const int index2 = start + poly.totloop - j - 1;
      std::swap(loops[index1].v, loops[index2].v);
      std::swap(loops[index1 - 1].e, loops[index2].e);
    }
  }

  MutableAttributeAccessor attributes = mesh.attributes_for_write();
  attributes.for_all(
      [&](const bke::AttributeIDRef &attribute_id, const AttributeMetaData &meta_data) {
        if (meta_data.data_type == CD_PROP_STRING) {
          return true;
        }
        if (meta_data.domain == ATTR_DOMAIN_CORNER) {
          GSpanAttributeWriter attribute = attributes.lookup_or_add_for_write_span(
              attribute_id, ATTR_DOMAIN_CORNER, meta_data.data_type);
          attribute_math::convert_to_static_type(meta_data.data_type, [&](auto dummy) {
            using T = decltype(dummy);
            MutableSpan<T> dst_span = attribute.span.typed<T>();
            for (const int j : selection.index_range()) {
              const MPoly &poly = polys[selection[j]];
              dst_span.slice(poly.loopstart + 1, poly.totloop - 1).reverse();
            }
          });
          attribute.finish();
        }
        return true;
      });
}

static void node_geo_exec(GeoNodeExecParams params)
{
  GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");

  const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");

  geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
    if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
      mesh_flip_faces(*mesh, selection_field);
    }
  });

  params.set_output("Mesh", std::move(geometry_set));
}

}  // namespace blender::nodes::node_geo_flip_faces_cc

void register_node_type_geo_flip_faces()
{
  namespace file_ns = blender::nodes::node_geo_flip_faces_cc;

  static bNodeType ntype;

  geo_node_type_base(&ntype, GEO_NODE_FLIP_FACES, "Flip Faces", NODE_CLASS_GEOMETRY);
  ntype.geometry_node_execute = file_ns::node_geo_exec;
  ntype.declare = file_ns::node_declare;
  nodeRegisterType(&ntype);
}