Blender V5.3
transform_snap_object_editmesh.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_index_mask.hh"
10
11#include "BKE_attribute.hh"
12#include "BKE_bvhutils.hh"
13#include "BKE_editmesh.hh"
14#include "BKE_global.hh"
15#include "BKE_lib_id.hh"
16#include "BKE_mesh.hh"
17#include "BKE_object.hh"
18#include "BKE_object_types.hh"
19
20#include "GEO_mesh_selection.hh"
21
23
25
27
28namespace blender::ed::transform {
29
30/* -------------------------------------------------------------------- */
33
34static const Mesh *get_mesh_ref(const Object *ob_eval)
35{
36 if (const Mesh *me = BKE_object_get_editmesh_eval_final(ob_eval)) {
37 return me;
38 }
39
40 if (const Mesh *me = BKE_object_get_editmesh_eval_cage(ob_eval)) {
41 return me;
42 }
43
44 return id_cast<const Mesh *>(ob_eval->data);
45}
46
53
54static void calc_target_elems(const Mesh &mesh,
55 const SnapEditMeshTarget target,
56 SnapTargetElems &r_elems)
57{
58 const bke::AttributeAccessor attributes = mesh.attributes();
59 const Span<int2> edges = mesh.edges();
60 const OffsetIndices faces = mesh.faces();
61 IndexMaskMemory &memory = r_elems.memory;
62
64 *attributes.lookup_or_default(".hide_vert", bke::AttrDomain::Point, false), memory);
66 *attributes.lookup_or_default(".hide_edge", bke::AttrDomain::Edge, false), memory);
68 *attributes.lookup_or_default(".hide_poly", bke::AttrDomain::Face, false), memory);
69
71 const VArray<bool> select_vert = *attributes.lookup_or_default(
72 ".select_vert", bke::AttrDomain::Point, false);
73
74 target_verts = IndexMask::from_bools_inverse(target_verts, select_vert, memory);
75 target_edges = IndexMask::from_bools_inverse(
76 target_edges,
77 *attributes.lookup_or_default(".select_edge", bke::AttrDomain::Edge, false),
78 memory);
79 target_faces = IndexMask::from_bools_inverse(
80 target_faces,
81 *attributes.lookup_or_default(".select_poly", bke::AttrDomain::Face, false),
82 memory);
83
84 /* Also remove edges and faces that use a selected vertex. */
85 target_edges = IndexMask::from_predicate(target_edges, memory, [&](const int edge) {
86 return !select_vert[edges[edge][0]] && !select_vert[edges[edge][1]];
87 });
88 const Span<int> corner_verts = mesh.corner_verts();
89 target_faces = IndexMask::from_predicate(target_faces, memory, [&](const int face) {
90 return std::ranges::none_of(corner_verts.slice(faces[face]),
91 [&](const int vert) { return select_vert[vert]; });
92 });
93 }
94
95 r_elems.faces = target_faces;
96
97 /* Vertices and edges are only snapped to on their own when nothing of a higher dimension that
98 * is snapped to covers them already. */
100 target_verts,
101 geometry::vert_selection_from_edge(edges, target_edges, mesh.verts_num, memory),
102 memory);
104 target_edges,
106 faces, target_faces, mesh.corner_edges(), mesh.edges_num, memory),
107 memory);
108}
109
115 const Mesh *mesh_ = nullptr;
117
118 std::optional<SnapTargetElems> elems_;
119 std::optional<bke::BVHTreeFromMesh> corner_tris_;
120 std::optional<bke::BVHTreeFromMesh> loose_edges_;
121 std::optional<bke::BVHTreeFromMesh> loose_verts_;
122
123 public:
125 void set_source(const Mesh &mesh, const SnapEditMeshTarget target)
126 {
127 if (mesh_ == &mesh && target_ == target) {
128 return;
129 }
130 this->clear();
131 mesh_ = &mesh;
132 target_ = target;
133 }
134
139 void clear()
140 {
141 mesh_ = nullptr;
142 elems_.reset();
143 corner_tris_.reset();
144 loose_edges_.reset();
145 loose_verts_.reset();
146 }
147
149 {
150 if (!corner_tris_) {
151 corner_tris_ = bke::bvhtree_from_mesh_corner_tris_ex(mesh_->vert_positions(),
152 mesh_->faces(),
153 mesh_->corner_verts(),
154 mesh_->corner_tris(),
155 this->selection().faces);
156 }
157 return *corner_tris_;
158 }
159
161 {
162 if (!loose_edges_) {
163 loose_edges_ = bke::bvhtree_from_mesh_edges_ex(
164 mesh_->vert_positions(), mesh_->edges(), this->selection().loose_edges);
165 }
166 return *loose_edges_;
167 }
168
170 {
171 if (!loose_verts_) {
172 loose_verts_ = bke::bvhtree_from_mesh_verts_ex(mesh_->vert_positions(),
173 this->selection().loose_verts);
174 }
175 return *loose_verts_;
176 }
177
178 private:
179 const SnapTargetElems &selection()
180 {
181 if (!elems_) {
182 calc_target_elems(*mesh_, target_, elems_.emplace());
183 }
184 return *elems_;
185 }
186};
187
207 /* Mesh created from the edited mesh. */
209
210 /* Trees of the elements of #mesh that can be snapped to. */
212
213 /* Reference to pointers that change when the mesh is changed. It is used to detect updates. */
217
219 {
220 if (mesh != this->mesh_ref || mesh->runtime != this->runtime_ref ||
221 mesh->runtime->edit_data.get() != this->edit_data_ref)
222 {
223 return true;
224 }
225
226 return false;
227 }
228
229 void clear()
230 {
231 this->trees.clear();
232 if (this->mesh) {
233 BKE_id_free(nullptr, this->mesh);
234 this->mesh = nullptr;
235 }
236 }
237
239 {
240 this->clear();
241 }
242
243 MEM_CXX_CLASS_ALLOC_FUNCS("SnapCache_EditMesh")
244};
245
246static Mesh *create_mesh(const Object *ob_eval)
247{
249 Object *ob_orig = const_cast<Object *>(DEG_get_original(ob_eval));
251 /* The hide and selection status is all that's needed to find the elements that can be snapped
252 * to, see #SnapTargetTreesEditMesh. */
254 return mesh;
255}
256
258 const Object *ob_eval,
259 bool create)
260{
261 BLI_assert((ob_eval->mode & OB_MODE_EDIT) || sctx->runtime.params.ignore_editmode_filtering);
262 SnapCache_EditMesh *em_cache = nullptr;
263
264 bool init = false;
265 const Mesh *mesh_ref = (G.moving) ? /* WORKAROUND:
266 * Avoid updating while transforming. Do not check if the
267 * reference mesh has been updated. */
268 nullptr :
269 get_mesh_ref(ob_eval);
270
271 if (std::unique_ptr<SnapObjectContext::SnapCache> *em_cache_p = sctx->editmesh_caches.lookup_ptr(
272 ob_eval->runtime->data_orig))
273 {
274 em_cache = static_cast<SnapCache_EditMesh *>(em_cache_p->get());
275
276 /* Check if the geometry has changed. */
277 if (mesh_ref && em_cache->has_mesh_updated(mesh_ref)) {
278 em_cache->clear();
279 init = true;
280 }
281 }
282 else if (create) {
283 std::unique_ptr<SnapCache_EditMesh> em_cache_ptr = std::make_unique<SnapCache_EditMesh>();
284 em_cache = em_cache_ptr.get();
285 sctx->editmesh_caches.add_new(ob_eval->runtime->data_orig, std::move(em_cache_ptr));
286 init = true;
287 }
288
289 if (init) {
290 em_cache->mesh = create_mesh(ob_eval);
291 if (mesh_ref) {
292 em_cache->mesh_ref = mesh_ref;
293 em_cache->runtime_ref = mesh_ref->runtime;
294 em_cache->edit_data_ref = mesh_ref->runtime->edit_data.get();
295 }
296 }
297
298 if (em_cache && em_cache->mesh) {
299 em_cache->trees.set_source(*em_cache->mesh, sctx->editmesh_target);
300 }
301
302 return em_cache;
303}
304
306
307/* -------------------------------------------------------------------- */
310
312{
313 eSnapMode snap_mode_supported = SCE_SNAP_TO_NONE;
314 if (bm->totface) {
315 snap_mode_supported |= SCE_SNAP_TO_FACE | SCE_SNAP_TO_FACE_MIDPOINT |
317 }
318 else if (bm->totedge) {
319 snap_mode_supported |= SNAP_TO_EDGE_ELEMENTS | SCE_SNAP_TO_POINT;
320 }
321 else if (bm->totvert) {
322 snap_mode_supported |= SCE_SNAP_TO_POINT;
323 }
324 return snap_mode_supported;
325}
326
328 const Object *ob_eval,
329 eSnapMode snap_to_flag)
330{
331 /* See code-comment on #SnapCache_EditMesh for why this is needed. */
332 if (!sctx->runtime.params.ignore_editmode_filtering) {
333 if ((ob_eval->mode & OB_MODE_EDIT) == 0) {
334 return nullptr;
335 }
336 }
337
338 Object *ob_orig = const_cast<Object *>(DEG_get_original(ob_eval));
340 if (bm == nullptr) {
341 return nullptr;
342 }
343
344 SnapCache_EditMesh *em_cache = snap_object_data_editmesh_get(sctx, ob_eval, false);
345 if (em_cache != nullptr) {
346 return em_cache;
347 }
348
349 eSnapMode snap_mode_used = snap_to_flag & editmesh_snap_mode_supported(bm);
350 if (snap_mode_used == SCE_SNAP_TO_NONE) {
351 return nullptr;
352 }
353
354 return snap_object_data_editmesh_get(sctx, ob_eval, true);
355}
356
358
360 const Object *ob_eval,
361 const ID * /*id*/,
362 const float4x4 &obmat,
363 eSnapMode snap_to_flag,
364 bool /*use_hide*/)
365{
366 SnapCache_EditMesh *em_cache = editmesh_snapdata_init(sctx, ob_eval, snap_to_flag);
367 if (em_cache && em_cache->mesh) {
368 return snap_object_mesh(
369 sctx, ob_eval, &em_cache->mesh->id, obmat, snap_to_flag, em_cache->trees, true);
370 }
371 return SCE_SNAP_TO_NONE;
372}
373
374} // namespace blender::ed::transform
General operations, lookup, etc. for blender objects.
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
static IndexMask from_difference(const IndexMask &mask_a, const IndexMask &mask_b, LinearAllocator<> &memory)
static IndexMask from_bools_inverse(Span< bool > bools, LinearAllocator<> &memory)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
void set_source(const Mesh &mesh, const SnapEditMeshTarget target)
BVHTreeFromMesh bvhtree_from_mesh_corner_tris_ex(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int3 > corner_tris, const IndexMask &faces_mask, bool map_global_indices=true)
Definition bvhutils.cc:562
BVHTreeFromMesh bvhtree_from_mesh_edges_ex(Span< float3 > vert_positions, Span< int2 > edges, const IndexMask &edges_mask)
Definition bvhutils.cc:477
BVHTreeFromMesh bvhtree_from_mesh_verts_ex(Span< float3 > vert_positions, const IndexMask &verts_mask)
Definition bvhutils.cc:452
static const Mesh * get_mesh_ref(const Object *ob_eval)
static SnapCache_EditMesh * snap_object_data_editmesh_get(SnapObjectContext *sctx, const Object *ob_eval, bool create)
static Mesh * create_mesh(const Object *ob_eval)
eSnapMode snap_object_editmesh(SnapObjectContext *sctx, const Object *ob_eval, const ID *id, const float4x4 &obmat, eSnapMode snap_to_flag, bool use_hide)
eSnapMode snap_object_mesh(SnapObjectContext *sctx, const Object *ob_eval, const ID *id, const float4x4 &obmat, eSnapMode snap_to_flag, bool skip_hidden)
static SnapCache_EditMesh * editmesh_snapdata_init(SnapObjectContext *sctx, const Object *ob_eval, eSnapMode snap_to_flag)
static void calc_target_elems(const Mesh &mesh, const SnapEditMeshTarget target, SnapTargetElems &r_elems)
static eSnapMode editmesh_snap_mode_supported(BMesh *bm)
IndexMask edge_selection_from_face(OffsetIndices< int > faces, const IndexMask &face_mask, Span< int > corner_edges, int edges_num, IndexMaskMemory &memory)
IndexMask vert_selection_from_edge(Span< int2 > edges, const IndexMask &edge_mask, int verts_num, IndexMaskMemory &memory)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const Mesh * BKE_object_get_editmesh_eval_cage(const Object *object)
Definition object.cc:4492
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void BM_mesh_bm_to_me_only_select_and_hide(BMesh &bm, Mesh &mesh)
const Mesh * BKE_object_get_editmesh_eval_final(const Object *object)
Definition object.cc:4477
MatBase< float, 4, 4 > float4x4
Global G
Definition blender.cc:57
@ SCE_SNAP_TO_FACE_MIDPOINT
@ SCE_SNAP_INDIVIDUAL_NEAREST
void BKE_id_free(Main *bmain, void *idv)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
T * DEG_get_original(T *id)
bke::MeshRuntime * runtime
bke::ObjectRuntime * runtime
std::unique_ptr< EditMeshData > edit_data
struct blender::ed::transform::SnapObjectContext::@247331373256345130115246240121215056001150353127 runtime
Map< const ID *, std::unique_ptr< SnapCache > > editmesh_caches
#define SNAP_TO_EDGE_ELEMENTS