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

node_geo_legacy_point_separate.cc « legacy « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9260928b3118a9a91ec10c34120ed9403f4caf56 (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
/*
 * 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_attribute_math.hh"
#include "BKE_mesh.h"
#include "BKE_pointcloud.h"

#include "DNA_mesh_types.h"
#include "DNA_pointcloud_types.h"

#include "node_geometry_util.hh"

namespace blender::nodes {

template<typename T>
static void copy_data_based_on_mask(Span<T> data,
                                    Span<bool> masks,
                                    const bool invert,
                                    MutableSpan<T> out_data)
{
  int offset = 0;
  for (const int i : data.index_range()) {
    if (masks[i] != invert) {
      out_data[offset] = data[i];
      offset++;
    }
  }
}

void copy_point_attributes_based_on_mask(const GeometryComponent &in_component,
                                         GeometryComponent &result_component,
                                         Span<bool> masks,
                                         const bool invert)
{
  for (const AttributeIDRef &attribute_id : in_component.attribute_ids()) {
    ReadAttributeLookup attribute = in_component.attribute_try_get_for_read(attribute_id);
    const CustomDataType data_type = bke::cpp_type_to_custom_data_type(attribute.varray.type());

    /* Only copy point attributes. Theoretically this could interpolate attributes on other
     * domains to the point domain, but that would conflict with attributes that are built-in
     * on other domains, which causes creating the attributes to fail. */
    if (attribute.domain != ATTR_DOMAIN_POINT) {
      continue;
    }

    OutputAttribute result_attribute = result_component.attribute_try_get_for_output_only(
        attribute_id, ATTR_DOMAIN_POINT, data_type);

    attribute_math::convert_to_static_type(data_type, [&](auto dummy) {
      using T = decltype(dummy);
      VArray_Span span{attribute.varray.typed<T>()};
      MutableSpan<T> out_span = result_attribute.as_span<T>();
      copy_data_based_on_mask(span, masks, invert, out_span);
    });

    result_attribute.save();
  }
}

}  // namespace blender::nodes

namespace blender::nodes::node_geo_legacy_point_separate_cc {

static void node_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::Geometry>(N_("Geometry"));
  b.add_input<decl::String>(N_("Mask"));
  b.add_output<decl::Geometry>(N_("Geometry 1"));
  b.add_output<decl::Geometry>(N_("Geometry 2"));
}

static void create_component_points(GeometryComponent &component, const int total)
{
  switch (component.type()) {
    case GEO_COMPONENT_TYPE_MESH:
      static_cast<MeshComponent &>(component).replace(BKE_mesh_new_nomain(total, 0, 0, 0, 0));
      break;
    case GEO_COMPONENT_TYPE_POINT_CLOUD:
      static_cast<PointCloudComponent &>(component).replace(BKE_pointcloud_new_nomain(total));
      break;
    default:
      BLI_assert(false);
      break;
  }
}

static void separate_points_from_component(const GeometryComponent &in_component,
                                           GeometryComponent &out_component,
                                           const StringRef mask_name,
                                           const bool invert)
{
  if (!in_component.attribute_domain_supported(ATTR_DOMAIN_POINT) ||
      in_component.attribute_domain_size(ATTR_DOMAIN_POINT) == 0) {
    return;
  }

  const VArray<bool> mask_attribute = in_component.attribute_get_for_read<bool>(
      mask_name, ATTR_DOMAIN_POINT, false);
  VArray_Span<bool> masks{mask_attribute};

  const int total = masks.count(!invert);
  if (total == 0) {
    return;
  }

  create_component_points(out_component, total);

  copy_point_attributes_based_on_mask(in_component, out_component, masks, invert);
}

static GeometrySet separate_geometry_set(const GeometrySet &set_in,
                                         const StringRef mask_name,
                                         const bool invert)
{
  GeometrySet set_out;
  for (const GeometryComponent *component : set_in.get_components_for_read()) {
    if (component->type() == GEO_COMPONENT_TYPE_CURVE) {
      /* Don't support the curve component for now, even though it has a point domain. */
      continue;
    }
    GeometryComponent &out_component = set_out.get_component_for_write(component->type());
    separate_points_from_component(*component, out_component, mask_name, invert);
  }
  return set_out;
}

static void node_geo_exec(GeoNodeExecParams params)
{
  bool wait_for_inputs = false;
  wait_for_inputs |= params.lazy_require_input("Geometry");
  wait_for_inputs |= params.lazy_require_input("Mask");
  if (wait_for_inputs) {
    return;
  }
  const std::string mask_attribute_name = params.get_input<std::string>("Mask");
  GeometrySet geometry_set = params.get_input<GeometrySet>("Geometry");

  /* TODO: This is not necessary-- the input geometry set can be read only,
   * but it must be rewritten to handle instance groups. */
  geometry_set = geometry::realize_instances_legacy(geometry_set);

  if (params.lazy_output_is_required("Geometry 1")) {
    params.set_output("Geometry 1",
                      separate_geometry_set(geometry_set, mask_attribute_name, true));
  }
  if (params.lazy_output_is_required("Geometry 2")) {
    params.set_output("Geometry 2",
                      separate_geometry_set(geometry_set, mask_attribute_name, false));
  }
}

}  // namespace blender::nodes::node_geo_legacy_point_separate_cc

void register_node_type_geo_point_separate()
{
  namespace file_ns = blender::nodes::node_geo_legacy_point_separate_cc;

  static bNodeType ntype;

  geo_node_type_base(
      &ntype, GEO_NODE_LEGACY_POINT_SEPARATE, "Point Separate", NODE_CLASS_GEOMETRY, 0);
  ntype.declare = file_ns::node_declare;
  ntype.geometry_node_execute = file_ns::node_geo_exec;
  ntype.geometry_node_execute_supports_laziness = true;
  nodeRegisterType(&ntype);
}