Blender V5.3
ed_transverts.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_mesh_types.h"
10#include "MEM_guardedalloc.h"
11
12#include "DNA_armature_types.h"
13#include "DNA_curve_types.h"
14#include "DNA_curves_types.h"
15#include "DNA_lattice_types.h"
16#include "DNA_meta_types.h"
17#include "DNA_object_types.h"
19#include "DNA_scene_types.h"
20
21#include "BLI_listbase.hh"
22#include "BLI_utildefines.hh"
23
24#include "BKE_armature.hh"
25#include "BKE_context.hh"
26#include "BKE_curve.hh"
27#include "BKE_editmesh.hh"
28#include "BKE_lattice.hh"
29#include "BKE_mesh_iterators.hh"
30#include "BKE_mesh_types.hh"
31#include "BKE_object.hh"
32
33#include "DEG_depsgraph.hh"
35
36#include "ED_armature.hh"
37#include "ED_curves.hh"
38#include "ED_pointcloud.hh"
39
40#include "ANIM_armature.hh"
41
42#include "ED_transverts.hh" /* own include */
43
44namespace blender {
45
47{
48 /* NOTE: copied from `editobject.c`, now uses (almost) proper depsgraph. */
49
50 const int mode = tvs->mode;
52
54
55 if (obedit->type == OB_MESH) {
58 }
59 else if (ELEM(obedit->type, OB_CURVES_LEGACY, OB_SURF)) {
60 Curve *cu = id_cast<Curve *>(obedit->data);
61 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
62 Nurb *nu = nurbs->first();
63
64 /* #ED_transverts_create_from_obedit fills a single contiguous array spanning all nurbs,
65 * so advance over every nurb's verts instead of resetting per nurb. */
66 TransVert *tv = tvs->transverts;
67
68 while (nu) {
69 /* keep handles' vectors unchanged */
70 if (nu->bezt && (mode & TM_SKIP_HANDLES)) {
71 int a = nu->pntsu;
72 BezTriple *bezt = nu->bezt;
73
74 while (a--) {
75 if (bezt->hide == 0) {
76 bool skip_handle = false;
77 if (bezt->f2 & SELECT) {
78 skip_handle = (mode & TM_SKIP_HANDLES) != 0;
79 }
80
81 if ((bezt->f1 & SELECT) && !skip_handle) {
82 BLI_assert(tv->loc == bezt->vec[0]);
83 tv++;
84 }
85
86 if (bezt->f2 & SELECT) {
87 float v[3];
88
89 if (((bezt->f1 & SELECT) && !skip_handle) == 0) {
90 sub_v3_v3v3(v, tv->loc, tv->oldloc);
91 add_v3_v3(bezt->vec[0], v);
92 }
93
94 if (((bezt->f3 & SELECT) && !skip_handle) == 0) {
95 sub_v3_v3v3(v, tv->loc, tv->oldloc);
96 add_v3_v3(bezt->vec[2], v);
97 }
98
99 BLI_assert(tv->loc == bezt->vec[1]);
100 tv++;
101 }
102
103 if ((bezt->f3 & SELECT) && !skip_handle) {
104 BLI_assert(tv->loc == bezt->vec[2]);
105 tv++;
106 }
107 }
108
109 bezt++;
110 }
111 }
112 else if (nu->bp && (mode & TM_SKIP_HANDLES)) {
113 /* No handles to keep unchanged, but the `tv` array must be kept in sync. */
114 int a = nu->pntsu * nu->pntsv;
115 BPoint *bp = nu->bp;
116 while (a--) {
117 if ((bp->hide == 0) && (bp->f1 & SELECT)) {
118 tv++;
119 }
120 bp++;
121 }
122 }
123
124 if (CU_IS_2D(cu)) {
126 }
127 BKE_nurb_handles_test(nu, NURB_HANDLE_TEST_EACH, false); /* test for bezier too */
128 nu = nu->next;
129 }
130 }
131 else if (obedit->type == OB_ARMATURE) {
132 bArmature *arm = id_cast<bArmature *>(obedit->data);
133 TransVert *tv = tvs->transverts;
134 int a = 0;
135
136 /* Ensure all bone tails are correctly adjusted */
137 for (EditBone &ebo : *arm->edbo) {
138 if (!animrig::bone_is_visible(arm, &ebo)) {
139 continue;
140 }
141 /* adjust tip if both ends selected */
142 if ((ebo.flag & BONE_ROOTSEL) && (ebo.flag & BONE_TIPSEL)) {
143 if (tv) {
144 float diffvec[3];
145
146 sub_v3_v3v3(diffvec, tv->loc, tv->oldloc);
147 add_v3_v3(ebo.tail, diffvec);
148
149 a++;
150 if (a < tvs->transverts_tot) {
151 tv++;
152 }
153 }
154 }
155 }
156
157 /* Ensure all bones are correctly adjusted */
158 for (EditBone &ebo : *arm->edbo) {
159 if ((ebo.flag & BONE_CONNECTED) && ebo.parent) {
160 /* If this bone has a parent tip that has been moved */
161 if (animrig::bone_is_visible(arm, ebo.parent) && (ebo.parent->flag & BONE_TIPSEL)) {
162 copy_v3_v3(ebo.head, ebo.parent->tail);
163 }
164 /* If this bone has a parent tip that has NOT been moved */
165 else {
166 copy_v3_v3(ebo.parent->tail, ebo.head);
167 }
168 }
169 }
170 if (arm->flag & ARM_MIRROR_EDIT) {
172 }
173 }
174 else if (obedit->type == OB_LATTICE) {
175 Lattice *lt = id_cast<Lattice *>(obedit->data);
176
177 if (lt->editlatt->latt->flag & LT_OUTSIDE) {
179 }
180 }
181 else if (obedit->type == OB_CURVES) {
182 Curves *curves_id = id_cast<Curves *>(obedit->data);
184 curves_id->geometry.wrap(), tvs, (mode & TM_SKIP_HANDLES) != 0);
185 }
186 else if (obedit->type == OB_POINTCLOUD) {
187 PointCloud *pointcloud = id_cast<PointCloud *>(obedit->data);
188 pointcloud->tag_positions_changed();
189 }
190}
191
192static void set_mapped_co(void *vuserdata, int index, const float co[3], const float /*no*/[3])
193{
194 void **userdata = static_cast<void **>(vuserdata);
195 BMesh *bm = static_cast<BMesh *>(userdata[0]);
196 TransVert *tv = static_cast<TransVert *>(userdata[1]);
198
199 if (BM_elem_index_get(eve) != TM_INDEX_SKIP) {
200 tv = &tv[BM_elem_index_get(eve)];
201
202 /* Be clever, get the closest vertex to the original,
203 * behaves most logically when the mirror modifier is used for eg #33051. */
204 if ((tv->flag & TX_VERT_USE_MAPLOC) == 0) {
205 /* first time */
206 copy_v3_v3(tv->maploc, co);
208 }
209 else {
210 /* find best location to use */
211 if (len_squared_v3v3(eve->co, co) < len_squared_v3v3(eve->co, tv->maploc)) {
212 copy_v3_v3(tv->maploc, co);
213 }
214 }
215 }
216}
217
219{
220 return ELEM(obedit->type,
223 OB_MESH,
224 OB_SURF,
226 OB_MBALL,
227 OB_CURVES,
229}
230
231void ED_transverts_create_from_obedit(TransVertStore *tvs, const Object *obedit, const int mode)
232{
234
235 Nurb *nu;
236 BezTriple *bezt;
237 BPoint *bp;
238 TransVert *tv = nullptr;
239 MetaElem *ml;
240 BMVert *eve;
241 int a;
242
243 tvs->transverts_tot = 0;
244
245 if (obedit->type == OB_MESH) {
246 /* The evaluated mesh may not have an edit-mesh, use the original. */
247 const Object *object_orig = DEG_get_original(obedit);
248 Mesh &mesh = *id_cast<Mesh *>(object_orig->data);
249 BMEditMesh *em = mesh.runtime->edit_mesh.get();
250 BMesh *bm = const_cast<BMesh *>(BKE_editmesh_bmesh_get(&mesh));
251 BMIter iter;
252 void *userdata[2] = {bm, nullptr};
253 // int proptrans = 0; /*UNUSED*/
254
255 /* abuses vertex index all over, set, just set dirty here,
256 * perhaps this could use its own array instead? - campbell */
257
258 /* transform now requires awareness for select mode, so we tag the f1 flags in verts */
259 tvs->transverts_tot = 0;
260 if (em->selectmode & SCE_SELECT_VERTEX) {
261 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
263 BM_elem_index_set(eve, TM_INDEX_ON); /* set_dirty! */
264 tvs->transverts_tot++;
265 }
266 else {
267 BM_elem_index_set(eve, TM_INDEX_OFF); /* set_dirty! */
268 }
269 }
270 }
271 else if (em->selectmode & SCE_SELECT_EDGE) {
272 BMEdge *eed;
273
274 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
275 BM_elem_index_set(eve, TM_INDEX_OFF); /* set_dirty! */
276 }
277
278 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
280 BM_elem_index_set(eed->v1, TM_INDEX_ON); /* set_dirty! */
281 BM_elem_index_set(eed->v2, TM_INDEX_ON); /* set_dirty! */
282 }
283 }
284
285 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
286 if (BM_elem_index_get(eve) == TM_INDEX_ON) {
287 tvs->transverts_tot++;
288 }
289 }
290 }
291 else {
292 BMFace *efa;
293
294 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
295 BM_elem_index_set(eve, TM_INDEX_OFF); /* set_dirty! */
296 }
297
298 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
300 BMIter liter;
301 BMLoop *l;
302
303 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
304 BM_elem_index_set(l->v, TM_INDEX_ON); /* set_dirty! */
305 }
306 }
307 }
308
309 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
310 if (BM_elem_index_get(eve) == TM_INDEX_ON) {
311 tvs->transverts_tot++;
312 }
313 }
314 }
315 /* for any of the 3 loops above which all dirty the indices */
317
318 /* and now make transverts */
319 if (tvs->transverts_tot) {
321
322 a = 0;
323 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
324 if (BM_elem_index_get(eve)) {
325 BM_elem_index_set(eve, a); /* set_dirty! */
326 copy_v3_v3(tv->oldloc, eve->co);
327 tv->loc = eve->co;
328 tv->flag = (BM_elem_index_get(eve) == TM_INDEX_ON) ? SELECT : 0;
329
330 if (mode & TM_CALC_NORMALS) {
332 copy_v3_v3(tv->normal, eve->no);
333 }
334
335 tv++;
336 a++;
337 }
338 else {
339 BM_elem_index_set(eve, TM_INDEX_SKIP); /* set_dirty! */
340 }
341 }
342 /* set dirty already, above */
343
344 userdata[1] = tvs->transverts;
345 }
346
347 if (mode & TM_CALC_MAPLOC) {
348 const Mesh *editmesh_eval_cage = BKE_object_get_editmesh_eval_cage(obedit);
349 if (tvs->transverts && editmesh_eval_cage) {
352 editmesh_eval_cage, set_mapped_co, userdata, MESH_FOREACH_NOP);
353 }
354 }
355 }
356 else if (obedit->type == OB_ARMATURE) {
357 bArmature *arm = id_cast<bArmature *>(obedit->data);
358 int totmalloc = arm->edbo->count();
359
360 totmalloc *= 2; /* probably overkill but bones can have 2 trans verts each */
361
362 tv = tvs->transverts = MEM_new_array_zeroed<TransVert>(totmalloc, __func__);
363
364 for (EditBone &ebo : *arm->edbo) {
365 if (animrig::bone_is_visible(arm, &ebo)) {
366 const bool tipsel = (ebo.flag & BONE_TIPSEL) != 0;
367 const bool rootsel = (ebo.flag & BONE_ROOTSEL) != 0;
368 const bool rootok = !(
369 ebo.parent && (ebo.flag & BONE_CONNECTED) &&
370 (animrig::bone_is_visible(arm, ebo.parent) && (ebo.parent->flag & BONE_TIPSEL)));
371
372 if ((tipsel && rootsel) || (rootsel)) {
373 /* Don't add the tip (unless mode & TM_ALL_JOINTS, for getting all joints),
374 * otherwise we get zero-length bones as tips will snap to the same
375 * location as heads.
376 */
377 if (rootok) {
378 copy_v3_v3(tv->oldloc, ebo.head);
379 tv->loc = ebo.head;
380 tv->flag = SELECT;
381 tv++;
382 tvs->transverts_tot++;
383 }
384
385 if ((mode & TM_ALL_JOINTS) && (tipsel)) {
386 copy_v3_v3(tv->oldloc, ebo.tail);
387 tv->loc = ebo.tail;
388 tv->flag = SELECT;
389 tv++;
390 tvs->transverts_tot++;
391 }
392 }
393 else if (tipsel) {
394 copy_v3_v3(tv->oldloc, ebo.tail);
395 tv->loc = ebo.tail;
396 tv->flag = SELECT;
397 tv++;
398 tvs->transverts_tot++;
399 }
400 }
401 }
402 }
403 else if (ELEM(obedit->type, OB_CURVES_LEGACY, OB_SURF)) {
404 Curve *cu = id_cast<Curve *>(obedit->data);
405 int totmalloc = 0;
406 ListBaseT<Nurb> *nurbs = BKE_curve_editNurbs_get(cu);
407
408 for (Nurb &nu : *nurbs) {
409 if (nu.type == CU_BEZIER) {
410 totmalloc += 3 * nu.pntsu;
411 }
412 else {
413 totmalloc += nu.pntsu * nu.pntsv;
414 }
415 }
416 tv = tvs->transverts = MEM_new_array_zeroed<TransVert>(totmalloc, __func__);
417
418 nu = nurbs->first();
419 while (nu) {
420 if (nu->type == CU_BEZIER) {
421 a = nu->pntsu;
422 bezt = nu->bezt;
423 while (a--) {
424 if (bezt->hide == 0) {
425 bool skip_handle = false;
426 if (bezt->f2 & SELECT) {
427 skip_handle = (mode & TM_SKIP_HANDLES) != 0;
428 }
429
430 if ((bezt->f1 & SELECT) && !skip_handle) {
431 copy_v3_v3(tv->oldloc, bezt->vec[0]);
432 tv->loc = bezt->vec[0];
433 tv->flag = bezt->f1 & SELECT;
434
435 if (mode & TM_CALC_NORMALS) {
437 BKE_nurb_bezt_calc_plane(nu, bezt, tv->normal);
438 }
439
440 tv++;
441 tvs->transverts_tot++;
442 }
443 if (bezt->f2 & SELECT) {
444 copy_v3_v3(tv->oldloc, bezt->vec[1]);
445 tv->loc = bezt->vec[1];
446 tv->flag = bezt->f2 & SELECT;
447
448 if (mode & TM_CALC_NORMALS) {
450 BKE_nurb_bezt_calc_plane(nu, bezt, tv->normal);
451 }
452
453 tv++;
454 tvs->transverts_tot++;
455 }
456 if ((bezt->f3 & SELECT) && !skip_handle) {
457 copy_v3_v3(tv->oldloc, bezt->vec[2]);
458 tv->loc = bezt->vec[2];
459 tv->flag = bezt->f3 & SELECT;
460
461 if (mode & TM_CALC_NORMALS) {
463 BKE_nurb_bezt_calc_plane(nu, bezt, tv->normal);
464 }
465
466 tv++;
467 tvs->transverts_tot++;
468 }
469 }
470 bezt++;
471 }
472 }
473 else {
474 a = nu->pntsu * nu->pntsv;
475 bp = nu->bp;
476 while (a--) {
477 if (bp->hide == 0) {
478 if (bp->f1 & SELECT) {
479 copy_v3_v3(tv->oldloc, bp->vec);
480 tv->loc = bp->vec;
481 tv->flag = bp->f1 & SELECT;
482 tv++;
483 tvs->transverts_tot++;
484 }
485 }
486 bp++;
487 }
488 }
489 nu = nu->next;
490 }
491 }
492 else if (obedit->type == OB_MBALL) {
493 MetaBall *mb = id_cast<MetaBall *>(obedit->data);
494 int totmalloc = mb->editelems->count();
495
496 tv = tvs->transverts = MEM_new_array_zeroed<TransVert>(totmalloc, __func__);
497
498 ml = mb->editelems->first();
499 while (ml) {
500 if (ml->flag & SELECT) {
501 tv->loc = &ml->x;
502 copy_v3_v3(tv->oldloc, tv->loc);
503 tv->flag = SELECT;
504 tv++;
505 tvs->transverts_tot++;
506 }
507 ml = ml->next;
508 }
509 }
510 else if (obedit->type == OB_LATTICE) {
511 const Object *object_orig = DEG_get_original(obedit);
512 Lattice *lt = id_cast<Lattice *>(object_orig->data);
513
514 bp = lt->editlatt->latt->def;
515
516 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
517
518 tv = tvs->transverts = MEM_new_array_zeroed<TransVert>(a, __func__);
519
520 while (a--) {
521 if (bp->f1 & SELECT) {
522 if (bp->hide == 0) {
523 copy_v3_v3(tv->oldloc, bp->vec);
524 tv->loc = bp->vec;
525 tv->flag = bp->f1 & SELECT;
526 tv++;
527 tvs->transverts_tot++;
528 }
529 }
530 bp++;
531 }
532 }
533 else if (obedit->type == OB_CURVES) {
534 Curves *curves_id = id_cast<Curves *>(obedit->data);
536 curves_id->geometry.wrap(), tvs, ((mode & TM_SKIP_HANDLES) != 0));
537 }
538 else if (obedit->type == OB_POINTCLOUD) {
539 PointCloud *pointcloud = id_cast<PointCloud *>(obedit->data);
540
541 IndexMaskMemory memory;
542 const IndexMask selection = ed::pointcloud::retrieve_selected_points(*pointcloud, memory);
543 MutableSpan<float3> positions = pointcloud->positions_for_write();
544
545 tvs->transverts = MEM_new_array_zeroed<TransVert>(selection.size(), __func__);
546 tvs->transverts_tot = selection.size();
547
548 selection.foreach_index(
549 [&](const int64_t i, const int64_t pos) {
550 TransVert &tv = tvs->transverts[pos];
551 tv.loc = positions[i];
552 tv.flag = SELECT;
553 copy_v3_v3(tv.oldloc, tv.loc);
554 },
556 }
557
558 if (!tvs->transverts_tot && tvs->transverts) {
559 /* Prevent memory leak. happens for curves/lattices due to
560 * difficult condition of adding points to trans data. */
561 MEM_delete(tvs->transverts);
562 tvs->transverts = nullptr;
563 }
564
565 tvs->mode = mode;
566}
567
569{
570 MEM_SAFE_DELETE(tvs->transverts);
571 tvs->transverts_tot = 0;
572}
573
575{
576 Object *obedit = CTX_data_edit_object(C);
577 if (obedit) {
578 if (ED_transverts_check_obedit(obedit)) {
579 return true;
580 }
581 }
582 return false;
583}
584
585} // namespace blender
Functions to deal with Armatures.
#define CU_IS_2D(cu)
Definition BKE_curve.hh:93
#define SELECT
Definition BKE_mask.hh:19
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ELEM(...)
#define BM_elem_index_get(ele)
#define BM_elem_index_set(ele, index)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
bool bone_is_visible(const bArmature *armature, const Bone *bone)
void transverts_from_curves_positions_create(bke::CurvesGeometry &curves, TransVertStore *tvs, const bool skip_handles)
void transverts_update_curves(bke::CurvesGeometry &curves, const TransVertStore *tvs, const bool skip_handles)
IndexMask retrieve_selected_points(const PointCloud &pointcloud, IndexMaskMemory &memory)
Definition selection.cc:274
constexpr ParallelGrainSize grain_size(int grain_size)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
const Mesh * BKE_object_get_editmesh_eval_cage(const Object *object)
Definition object.cc:4492
__int64 int64_t
Definition BKE_text.h:90
void ED_transverts_update_obedit(TransVertStore *tvs, Object *obedit)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
MINLINE void copy_v3_v3(float r[3], const float a[3])
ATTR_WARN_UNUSED_RESULT const BMLoop * l
@ TX_VERT_USE_MAPLOC
@ TX_VERT_USE_NORMAL
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
void ED_armature_edit_transform_mirror_update(Object *obedit)
bool ED_transverts_poll(bContext *C)
ATTR_WARN_UNUSED_RESULT const BMVert * v
@ NURB_HANDLE_TEST_EACH
Definition BKE_curve.hh:65
void BKE_nurb_project_2d(Nurb *nu)
Definition curve.cc:707
void DEG_id_tag_update(ID *id, unsigned int flags)
void ED_transverts_create_from_obedit(TransVertStore *tvs, const Object *obedit, int mode)
void outside_lattice(Lattice *lt)
Definition lattice.cc:415
Object * CTX_data_edit_object(const bContext *C)
static void set_mapped_co(void *vuserdata, int index, const float co[3], const float[3])
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
T * DEG_get_original(T *id)
void BKE_nurb_handles_test(Nurb *nu, eNurbHandleTest_Mode handle_mode, bool use_around_local)
Definition curve.cc:4119
void ED_transverts_free(TransVertStore *tvs)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void BKE_mesh_foreach_mapped_vert(const Mesh *mesh, void(*func)(void *user_data, int index, const float co[3], const float no[3]), void *user_data, MeshForeachFlag flag)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
ListBaseT< Nurb > * BKE_curve_editNurbs_get(Curve *cu)
Definition curve.cc:441
bool DEG_is_evaluated(const T *id)
void BKE_nurb_bezt_calc_plane(Nurb *nu, BezTriple *bezt, float r_plane[3])
Definition curve.cc:1047
bool ED_transverts_check_obedit(const Object *obedit)
void BM_mesh_normals_update(BMesh *bm)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
eBezTriple_Flag f2
eBezTriple_Flag f3
eBezTriple_Flag f1
struct Lattice * latt
struct EditLatt * editlatt
struct BPoint * def
ListBaseT< MetaElem > * editelems
struct MetaElem * next
eMetaElem_Flag flag
BezTriple * bezt
struct Nurb * next
ListBaseT< EditBone > * edbo