Blender V5.3
separate_geometry.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BKE_curves.hh"
12#include "BKE_customdata.hh"
14#include "BKE_grease_pencil.hh"
15#include "BKE_instances.hh"
16#include "BKE_mesh.hh"
17#include "BKE_pointcloud.hh"
18
20
22
23namespace blender::geometry {
24
25using bke::AttrDomain;
26
28static std::optional<bke::CurvesGeometry> separate_curves_selection(
29 const bke::CurvesGeometry &src_curves,
30 const fn::FieldContext &field_context,
31 const fn::Field<bool> &selection_field,
32 const AttrDomain domain,
33 const bke::AttributeFilter &attribute_filter)
34{
35 const int domain_size = src_curves.attributes().domain_size(domain);
36 fn::FieldEvaluator evaluator{field_context, domain_size};
37 evaluator.set_selection(selection_field);
38 evaluator.evaluate();
39 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
40 if (selection.size() == domain_size) {
41 return std::nullopt;
42 }
43 if (selection.is_empty()) {
44 return bke::CurvesGeometry();
45 }
46
47 if (domain == AttrDomain::Point) {
48 return bke::curves_copy_point_selection(src_curves, selection, attribute_filter);
49 }
50 if (domain == AttrDomain::Curve) {
51 return bke::curves_copy_curve_selection(src_curves, selection, attribute_filter);
52 }
54 return std::nullopt;
55}
56
58static std::optional<PointCloud *> separate_pointcloud_selection(
59 const PointCloud &src_pointcloud,
60 const fn::Field<bool> &selection_field,
61 const bke::AttributeFilter &attribute_filter)
62{
63 const bke::PointCloudFieldContext context{src_pointcloud};
64 fn::FieldEvaluator evaluator{context, src_pointcloud.totpoint};
65 evaluator.set_selection(selection_field);
66 evaluator.evaluate();
67 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
68 if (selection.size() == src_pointcloud.totpoint) {
69 return std::nullopt;
70 }
71 if (selection.is_empty()) {
72 return nullptr;
73 }
74
75 PointCloud *pointcloud = BKE_pointcloud_new_nomain(src_pointcloud.type, selection.size());
76 bke::gather_attributes(src_pointcloud.attributes(),
77 AttrDomain::Point,
78 AttrDomain::Point,
79 attribute_filter,
80 selection,
81 pointcloud->attributes_for_write());
82 return pointcloud;
83}
84
86 const fn::Field<bool> &selection_field,
87 const bke::AttributeFilter &attribute_filter)
88{
89 bke::Instances &instances = *geometry_set.get_instances_for_write();
90 bke::InstancesFieldContext field_context{instances};
91
92 fn::FieldEvaluator evaluator{field_context, instances.instances_num()};
93 evaluator.set_selection(selection_field);
94 evaluator.evaluate();
95 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
96 if (selection.is_empty()) {
97 geometry_set.remove<bke::InstancesComponent>();
98 return;
99 }
100
101 instances.remove(selection, attribute_filter);
102}
103
104static std::optional<Mesh *> separate_mesh_selection(const Mesh &mesh,
105 const fn::Field<bool> &selection_field,
106 const AttrDomain selection_domain,
108 const bke::AttributeFilter &attribute_filter)
109{
110 const bke::AttributeAccessor attributes = mesh.attributes();
111 const bke::MeshFieldContext context(mesh, selection_domain);
112 fn::FieldEvaluator evaluator(context, attributes.domain_size(selection_domain));
113 evaluator.add(selection_field);
114 evaluator.evaluate();
115 const VArray<bool> selection = evaluator.get_evaluated<bool>(0);
116
117 switch (mode) {
119 return mesh_copy_selection(mesh, selection, selection_domain, attribute_filter);
121 return mesh_copy_selection_keep_verts(mesh, selection, selection_domain, attribute_filter);
123 return mesh_copy_selection_keep_edges(mesh, selection, selection_domain, attribute_filter);
124 }
125 return nullptr;
126}
127
128static std::optional<GreasePencil *> separate_grease_pencil_layer_selection(
129 const GreasePencil &src_grease_pencil,
130 const fn::Field<bool> &selection_field,
131 const bke::AttributeFilter &attribute_filter)
132{
133 const bke::AttributeAccessor attributes = src_grease_pencil.attributes();
134 const bke::GeometryFieldContext context(src_grease_pencil);
135
136 fn::FieldEvaluator evaluator(context, attributes.domain_size(AttrDomain::Layer));
137 evaluator.set_selection(selection_field);
138 evaluator.evaluate();
139
140 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
141 if (selection.size() == attributes.domain_size(AttrDomain::Layer)) {
142 return std::nullopt;
143 }
144 if (selection.is_empty()) {
145 return nullptr;
146 }
147
148 const int dst_layers_num = selection.size();
149
150 GreasePencil *dst_grease_pencil = BKE_grease_pencil_new_nomain();
151 BKE_grease_pencil_copy_parameters(src_grease_pencil, *dst_grease_pencil);
152 dst_grease_pencil->add_layers_with_empty_drawings_for_eval(dst_layers_num);
153
154 selection.foreach_index([&](const int src_layer_i, const int dst_layer_i) {
155 const bke::greasepencil::Layer &src_layer = src_grease_pencil.layer(src_layer_i);
156 const bke::greasepencil::Drawing *src_drawing = src_grease_pencil.get_eval_drawing(src_layer);
157
158 bke::greasepencil::Layer &dst_layer = dst_grease_pencil->layer(dst_layer_i);
159 bke::greasepencil::Drawing &dst_drawing = *dst_grease_pencil->get_eval_drawing(dst_layer);
160
161 BKE_grease_pencil_copy_layer_parameters(src_layer, dst_layer);
162 dst_layer.set_name(src_layer.name());
163
164 if (src_drawing) {
165 dst_drawing = *src_drawing;
166 }
167 });
168
169 bke::gather_attributes(src_grease_pencil.attributes(),
170 AttrDomain::Layer,
171 AttrDomain::Layer,
172 attribute_filter,
173 selection,
174 dst_grease_pencil->attributes_for_write());
175
176 return dst_grease_pencil;
177}
178
180 const AttrDomain domain,
182 const fn::Field<bool> &selection,
183 const bke::AttributeFilter &attribute_filter,
184 bool &r_is_error)
185{
186 bool some_valid_domain = false;
187 if (const PointCloud *points = geometry_set.get_pointcloud()) {
188 if (domain == AttrDomain::Point) {
189 std::optional<PointCloud *> dst_points = separate_pointcloud_selection(
190 *points, selection, attribute_filter);
191 if (dst_points) {
192 geometry_set.replace_pointcloud(*dst_points);
193 }
194 some_valid_domain = true;
195 }
196 }
197 if (const Mesh *mesh = geometry_set.get_mesh()) {
198 if (ELEM(domain, AttrDomain::Point, AttrDomain::Edge, AttrDomain::Face)) {
199 std::optional<Mesh *> dst_mesh = separate_mesh_selection(
200 *mesh, selection, domain, mode, attribute_filter);
201 if (dst_mesh) {
202 geometry_set.replace_mesh(*dst_mesh);
203 }
204 some_valid_domain = true;
205 }
206 }
207 if (const Curves *src_curves_id = geometry_set.get_curves()) {
208 if (ELEM(domain, AttrDomain::Point, AttrDomain::Curve)) {
209 const bke::CurvesGeometry &src_curves = src_curves_id->geometry.wrap();
210 const bke::CurvesFieldContext field_context{*src_curves_id, domain};
211 std::optional<bke::CurvesGeometry> dst_curves = separate_curves_selection(
212 src_curves, field_context, selection, domain, attribute_filter);
213 if (dst_curves) {
214 if (dst_curves->is_empty()) {
215 geometry_set.remove<bke::CurveComponent>();
216 }
217 else {
218 Curves *dst_curves_id = bke::curves_new_nomain(*dst_curves);
219 bke::curves_copy_parameters(*src_curves_id, *dst_curves_id);
220 geometry_set.replace_curves(dst_curves_id);
221 }
222 }
223 some_valid_domain = true;
224 }
225 }
226 if (geometry_set.get_grease_pencil()) {
227 using namespace blender::bke::greasepencil;
228 if (domain == AttrDomain::Layer) {
229 const GreasePencil &grease_pencil = *geometry_set.get_grease_pencil();
230 std::optional<GreasePencil *> dst_grease_pencil = separate_grease_pencil_layer_selection(
231 grease_pencil, selection, attribute_filter);
232 if (dst_grease_pencil) {
233 geometry_set.replace_grease_pencil(*dst_grease_pencil);
234 }
235 some_valid_domain = true;
236 }
237 else if (ELEM(domain, AttrDomain::Point, AttrDomain::Curve)) {
239 for (const int layer_index : grease_pencil.layers().index_range()) {
240 Drawing *drawing = grease_pencil.get_eval_drawing(grease_pencil.layer(layer_index));
241 if (drawing == nullptr) {
242 continue;
243 }
244 const bke::CurvesGeometry &src_curves = drawing->strokes();
245 const bke::GreasePencilLayerFieldContext field_context(grease_pencil, domain, layer_index);
246 std::optional<bke::CurvesGeometry> dst_curves = separate_curves_selection(
247 src_curves, field_context, selection, domain, attribute_filter);
248 if (!dst_curves) {
249 continue;
250 }
251 drawing->strokes_for_write() = std::move(*dst_curves);
252 drawing->tag_topology_changed();
253 some_valid_domain = true;
254 }
255 }
256 }
257 if (geometry_set.has_instances()) {
258 if (domain == AttrDomain::Instance) {
259 delete_selected_instances(geometry_set, selection, attribute_filter);
260 some_valid_domain = true;
261 }
262 }
263 r_is_error = !some_valid_domain && geometry_set.has_realized_data();
264}
265
266} // namespace blender::geometry
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
Low-level operations for grease pencil.
General operations for point clouds.
#define BLI_assert_unreachable()
#define ELEM(...)
int domain_size(const AttrDomain domain) const
AttributeAccessor attributes() const
void remove(const IndexMask &mask, const AttributeFilter &attribute_filter)
Definition instances.cc:300
int instances_num() const
Definition instances.cc:408
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
void set_selection(Field< bool > selection)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_selection_as_mask() const
const GVArray & get_evaluated(const int field_index) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
CurvesGeometry curves_copy_curve_selection(const CurvesGeometry &curves, const IndexMask &curves_to_copy, const AttributeFilter &attribute_filter)
CurvesGeometry curves_copy_point_selection(const CurvesGeometry &curves, const IndexMask &points_to_copy, const AttributeFilter &attribute_filter)
void curves_copy_parameters(const Curves &src, Curves &dst)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
Curves * curves_new_nomain(int points_num, int curves_num)
static std::optional< bke::CurvesGeometry > separate_curves_selection(const bke::CurvesGeometry &src_curves, const fn::FieldContext &field_context, const fn::Field< bool > &selection_field, const AttrDomain domain, const bke::AttributeFilter &attribute_filter)
std::optional< Mesh * > mesh_copy_selection_keep_edges(const Mesh &mesh, const VArray< bool > &selection, bke::AttrDomain selection_domain, const bke::AttributeFilter &attribute_filter={})
std::optional< Mesh * > mesh_copy_selection_keep_verts(const Mesh &src_mesh, const VArray< bool > &selection, bke::AttrDomain selection_domain, const bke::AttributeFilter &attribute_filter={})
static std::optional< GreasePencil * > separate_grease_pencil_layer_selection(const GreasePencil &src_grease_pencil, const fn::Field< bool > &selection_field, const bke::AttributeFilter &attribute_filter)
static void delete_selected_instances(bke::GeometrySet &geometry_set, const fn::Field< bool > &selection_field, const bke::AttributeFilter &attribute_filter)
static std::optional< Mesh * > separate_mesh_selection(const Mesh &mesh, const fn::Field< bool > &selection_field, const AttrDomain selection_domain, const GeometryNodeDeleteGeometryMode mode, const bke::AttributeFilter &attribute_filter)
void separate_geometry(bke::GeometrySet &geometry_set, bke::AttrDomain domain, GeometryNodeDeleteGeometryMode mode, const fn::Field< bool > &selection_field, const bke::AttributeFilter &attribute_filter, bool &r_is_error)
std::optional< Mesh * > mesh_copy_selection(const Mesh &src_mesh, const VArray< bool > &selection, bke::AttrDomain selection_domain, const bke::AttributeFilter &attribute_filter={})
static std::optional< PointCloud * > separate_pointcloud_selection(const PointCloud &src_pointcloud, const fn::Field< bool > &selection_field, const bke::AttributeFilter &attribute_filter)
void BKE_grease_pencil_copy_layer_parameters(const bke::greasepencil::Layer &src, bke::greasepencil::Layer &dst)
void BKE_grease_pencil_copy_parameters(const GreasePencil &src, GreasePencil &dst)
GeometryNodeDeleteGeometryMode
@ GEO_NODE_DELETE_GEOMETRY_MODE_ALL
@ GEO_NODE_DELETE_GEOMETRY_MODE_ONLY_FACE
@ GEO_NODE_DELETE_GEOMETRY_MODE_EDGE_FACE
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
GreasePencil * BKE_grease_pencil_new_nomain()
Instances * get_instances_for_write()
const GreasePencil * get_grease_pencil() const
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const Curves * get_curves() const
void replace_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const PointCloud * get_pointcloud() const
const Mesh * get_mesh() const
void remove(const GeometryComponent::Type component_type)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GreasePencil * get_grease_pencil_for_write()
void replace_grease_pencil(GreasePencil *grease_pencil, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)