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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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.
*
* The Original Code is Copyright (C) 2004 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/mesh/editmesh_path.c
* \ingroup edmesh
*/
#include "DNA_scene_types.h"
#include "DNA_object_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_windowmanager_types.h"
#include "BLI_math.h"
#include "BLI_linklist.h"
#include "BKE_context.h"
#include "BKE_editmesh.h"
#include "BKE_report.h"
#include "ED_mesh.h"
#include "ED_screen.h"
#include "ED_uvedit.h"
#include "ED_view3d.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "WM_types.h"
#include "bmesh.h"
#include "bmesh_tools.h"
#include "mesh_intern.h" /* own include */
struct UserData {
BMesh *bm;
Mesh *me;
Scene *scene;
};
/* -------------------------------------------------------------------- */
/* Vert Path */
/* callbacks */
static bool verttag_filter_cb(BMVert *v, void *UNUSED(user_data_v))
{
return !BM_elem_flag_test(v, BM_ELEM_HIDDEN);
}
static bool verttag_test_cb(BMVert *v, void *UNUSED(user_data_v))
{
return BM_elem_flag_test_bool(v, BM_ELEM_SELECT);
}
static void verttag_set_cb(BMVert *v, bool val, void *user_data_v)
{
struct UserData *user_data = user_data_v;
BM_vert_select_set(user_data->bm, v, val);
}
static bool mouse_mesh_shortest_path_vert(ViewContext *vc)
{
/* unlike edge/face versions, this uses a bmesh operator */
BMEditMesh *em = vc->em;
BMesh *bm = em->bm;
BMVert *v_dst;
float dist = ED_view3d_select_dist_px();
const bool use_length = true;
v_dst = EDBM_vert_find_nearest(vc, &dist, false, false);
if (v_dst) {
struct UserData user_data = {bm, vc->obedit->data, vc->scene};
LinkNode *path = NULL;
BMVert *v_act = BM_mesh_active_vert_get(bm);
if (v_act && (v_act != v_dst)) {
if ((path = BM_mesh_calc_path_vert(bm, v_act, v_dst, use_length,
verttag_filter_cb, &user_data)))
{
BM_select_history_remove(bm, v_act);
}
}
if (path) {
/* toggle the flag */
bool all_set = true;
LinkNode *node;
node = path;
do {
if (!verttag_test_cb((BMVert *)node->link, &user_data)) {
all_set = false;
break;
}
} while ((node = node->next));
node = path;
do {
verttag_set_cb((BMVert *)node->link, !all_set, &user_data);
} while ((node = node->next));
BLI_linklist_free(path, NULL);
}
else {
const bool is_act = !verttag_test_cb(v_dst, &user_data);
verttag_set_cb(v_dst, is_act, &user_data); /* switch the face option */
}
EDBM_selectmode_flush(em);
/* even if this is selected it may not be in the selection list */
if (BM_elem_flag_test(v_dst, BM_ELEM_SELECT) == 0)
BM_select_history_remove(bm, v_dst);
else
BM_select_history_store(bm, v_dst);
EDBM_update_generic(em, false, false);
return true;
}
else {
return false;
}
}
/* -------------------------------------------------------------------- */
/* Edge Path */
/* callbacks */
static bool edgetag_filter_cb(BMEdge *e, void *UNUSED(user_data_v))
{
return !BM_elem_flag_test(e, BM_ELEM_HIDDEN);
}
static bool edgetag_test_cb(BMEdge *e, void *user_data_v)
{
struct UserData *user_data = user_data_v;
Scene *scene = user_data->scene;
BMesh *bm = user_data->bm;
switch (scene->toolsettings->edge_mode) {
case EDGE_MODE_SELECT:
return BM_elem_flag_test(e, BM_ELEM_SELECT) ? true : false;
case EDGE_MODE_TAG_SEAM:
return BM_elem_flag_test(e, BM_ELEM_SEAM) ? true : false;
case EDGE_MODE_TAG_SHARP:
return BM_elem_flag_test(e, BM_ELEM_SMOOTH) ? false : true;
case EDGE_MODE_TAG_CREASE:
return BM_elem_float_data_get(&bm->edata, e, CD_CREASE) ? true : false;
case EDGE_MODE_TAG_BEVEL:
return BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT) ? true : false;
#ifdef WITH_FREESTYLE
case EDGE_MODE_TAG_FREESTYLE:
{
FreestyleEdge *fed = CustomData_bmesh_get(&bm->edata, e->head.data, CD_FREESTYLE_EDGE);
return (!fed) ? false : (fed->flag & FREESTYLE_EDGE_MARK) ? true : false;
}
#endif
}
return 0;
}
static void edgetag_set_cb(BMEdge *e, bool val, void *user_data_v)
{
struct UserData *user_data = user_data_v;
Scene *scene = user_data->scene;
BMesh *bm = user_data->bm;
switch (scene->toolsettings->edge_mode) {
case EDGE_MODE_SELECT:
BM_edge_select_set(bm, e, val);
break;
case EDGE_MODE_TAG_SEAM:
BM_elem_flag_set(e, BM_ELEM_SEAM, val);
break;
case EDGE_MODE_TAG_SHARP:
BM_elem_flag_set(e, BM_ELEM_SMOOTH, !val);
break;
case EDGE_MODE_TAG_CREASE:
BM_elem_float_data_set(&bm->edata, e, CD_CREASE, (val) ? 1.0f : 0.0f);
break;
case EDGE_MODE_TAG_BEVEL:
BM_elem_float_data_set(&bm->edata, e, CD_BWEIGHT, (val) ? 1.0f : 0.0f);
break;
#ifdef WITH_FREESTYLE
case EDGE_MODE_TAG_FREESTYLE:
{
FreestyleEdge *fed;
fed = CustomData_bmesh_get(&bm->edata, e->head.data, CD_FREESTYLE_EDGE);
if (!val)
fed->flag &= ~FREESTYLE_EDGE_MARK;
else
fed->flag |= FREESTYLE_EDGE_MARK;
break;
}
#endif
}
}
static void edgetag_ensure_cd_flag(Scene *scene, Mesh *me)
{
BMesh *bm = me->edit_btmesh->bm;
switch (scene->toolsettings->edge_mode) {
case EDGE_MODE_TAG_CREASE:
BM_mesh_cd_flag_ensure(bm, me, ME_CDFLAG_EDGE_CREASE);
break;
case EDGE_MODE_TAG_BEVEL:
BM_mesh_cd_flag_ensure(bm, me, ME_CDFLAG_EDGE_BWEIGHT);
break;
#ifdef WITH_FREESTYLE
case EDGE_MODE_TAG_FREESTYLE:
if (!CustomData_has_layer(&bm->edata, CD_FREESTYLE_EDGE)) {
BM_data_layer_add(bm, &bm->edata, CD_FREESTYLE_EDGE);
}
break;
#endif
default:
break;
}
}
/* mesh shortest path select, uses prev-selected edge */
/* since you want to create paths with multiple selects, it doesn't have extend option */
static bool mouse_mesh_shortest_path_edge(ViewContext *vc)
{
BMEditMesh *em = vc->em;
BMesh *bm = em->bm;
BMEdge *e_dst;
float dist = ED_view3d_select_dist_px();
const bool use_length = true;
e_dst = EDBM_edge_find_nearest(vc, &dist);
if (e_dst) {
const char edge_mode = vc->scene->toolsettings->edge_mode;
struct UserData user_data = {bm, vc->obedit->data, vc->scene};
LinkNode *path = NULL;
Mesh *me = vc->obedit->data;
BMEdge *e_act = BM_mesh_active_edge_get(bm);
edgetag_ensure_cd_flag(vc->scene, em->ob->data);
if (e_act && (e_act != e_dst)) {
if ((path = BM_mesh_calc_path_edge(bm, e_act, e_dst, use_length,
edgetag_filter_cb, &user_data)))
{
BM_select_history_remove(bm, e_act);
}
}
if (path) {
/* toggle the flag */
bool all_set = true;
LinkNode *node;
node = path;
do {
if (!edgetag_test_cb((BMEdge *)node->link, &user_data)) {
all_set = false;
break;
}
} while ((node = node->next));
node = path;
do {
edgetag_set_cb((BMEdge *)node->link, !all_set, &user_data);
} while ((node = node->next));
BLI_linklist_free(path, NULL);
}
else {
const bool is_act = !edgetag_test_cb(e_dst, &user_data);
edgetag_ensure_cd_flag(vc->scene, vc->obedit->data);
edgetag_set_cb(e_dst, is_act, &user_data); /* switch the edge option */
}
if (edge_mode != EDGE_MODE_SELECT) {
/* simple rules - last edge is _always_ active and selected */
if (e_act)
BM_edge_select_set(bm, e_act, false);
BM_edge_select_set(bm, e_dst, true);
BM_select_history_store(bm, e_dst);
}
EDBM_selectmode_flush(em);
/* even if this is selected it may not be in the selection list */
if (edge_mode == EDGE_MODE_SELECT) {
if (edgetag_test_cb(e_dst, &user_data) == 0)
BM_select_history_remove(bm, e_dst);
else
BM_select_history_store(bm, e_dst);
}
/* force drawmode for mesh */
switch (edge_mode) {
case EDGE_MODE_TAG_SEAM:
me->drawflag |= ME_DRAWSEAMS;
ED_uvedit_live_unwrap(vc->scene, vc->obedit);
break;
case EDGE_MODE_TAG_SHARP:
me->drawflag |= ME_DRAWSHARP;
break;
case EDGE_MODE_TAG_CREASE:
me->drawflag |= ME_DRAWCREASES;
break;
case EDGE_MODE_TAG_BEVEL:
me->drawflag |= ME_DRAWBWEIGHTS;
break;
#ifdef WITH_FREESTYLE
case EDGE_MODE_TAG_FREESTYLE:
me->drawflag |= ME_DRAW_FREESTYLE_EDGE;
break;
#endif
}
EDBM_update_generic(em, false, false);
return true;
}
else {
return false;
}
}
/* -------------------------------------------------------------------- */
/* Face Path */
/* callbacks */
static bool facetag_filter_cb(BMFace *f, void *UNUSED(user_data_v))
{
return !BM_elem_flag_test(f, BM_ELEM_HIDDEN);
}
//static bool facetag_test_cb(Scene *UNUSED(scene), BMesh *UNUSED(bm), BMFace *f)
static bool facetag_test_cb(BMFace *f, void *UNUSED(user_data_v))
{
return BM_elem_flag_test_bool(f, BM_ELEM_SELECT);
}
//static void facetag_set_cb(BMesh *bm, Scene *UNUSED(scene), BMFace *f, const bool val)
static void facetag_set_cb(BMFace *f, bool val, void *user_data_v)
{
struct UserData *user_data = user_data_v;
BM_face_select_set(user_data->bm, f, val);
}
static bool mouse_mesh_shortest_path_face(ViewContext *vc)
{
BMEditMesh *em = vc->em;
BMesh *bm = em->bm;
BMFace *f_dst;
float dist = ED_view3d_select_dist_px();
const bool use_length = true;
f_dst = EDBM_face_find_nearest(vc, &dist);
if (f_dst) {
struct UserData user_data = {bm, vc->obedit->data, vc->scene};
LinkNode *path = NULL;
BMFace *f_act = BM_mesh_active_face_get(bm, false, true);
if (f_act) {
if (f_act != f_dst) {
if ((path = BM_mesh_calc_path_face(bm, f_act, f_dst, use_length,
facetag_filter_cb, &user_data)))
{
BM_select_history_remove(bm, f_act);
}
}
}
if (path) {
/* toggle the flag */
bool all_set = true;
LinkNode *node;
node = path;
do {
if (!facetag_test_cb((BMFace *)node->link, &user_data)) {
all_set = false;
break;
}
} while ((node = node->next));
node = path;
do {
facetag_set_cb((BMFace *)node->link, !all_set, &user_data);
} while ((node = node->next));
BLI_linklist_free(path, NULL);
}
else {
const bool is_act = !facetag_test_cb(f_dst, &user_data);
facetag_set_cb(f_dst, is_act, &user_data); /* switch the face option */
}
EDBM_selectmode_flush(em);
/* even if this is selected it may not be in the selection list */
if (facetag_test_cb(f_dst, &user_data) == 0)
BM_select_history_remove(bm, f_dst);
else
BM_select_history_store(bm, f_dst);
BM_mesh_active_face_set(bm, f_dst);
EDBM_update_generic(em, false, false);
return true;
}
else {
return false;
}
}
/* -------------------------------------------------------------------- */
/* Main Operator for vert/edge/face tag */
static int edbm_shortest_path_pick_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
{
ViewContext vc;
BMEditMesh *em;
BMElem *ele;
em_setup_viewcontext(C, &vc);
copy_v2_v2_int(vc.mval, event->mval);
em = vc.em;
ele = BM_mesh_active_elem_get(em->bm);
if (ele == NULL) {
return OPERATOR_PASS_THROUGH;
}
view3d_operator_needs_opengl(C);
if ((em->selectmode & SCE_SELECT_VERTEX) && (ele->head.htype == BM_VERT)) {
if (mouse_mesh_shortest_path_vert(&vc)) {
return OPERATOR_FINISHED;
}
else {
return OPERATOR_PASS_THROUGH;
}
}
else if ((em->selectmode & SCE_SELECT_EDGE) && (ele->head.htype == BM_EDGE)) {
if (mouse_mesh_shortest_path_edge(&vc)) {
return OPERATOR_FINISHED;
}
else {
return OPERATOR_PASS_THROUGH;
}
}
else if ((em->selectmode & SCE_SELECT_FACE) && (ele->head.htype == BM_FACE)) {
if (mouse_mesh_shortest_path_face(&vc)) {
return OPERATOR_FINISHED;
}
else {
return OPERATOR_PASS_THROUGH;
}
}
return OPERATOR_PASS_THROUGH;
}
void MESH_OT_shortest_path_pick(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Pick Shortest Path";
ot->idname = "MESH_OT_shortest_path_pick";
ot->description = "Select shortest path between two selections";
/* api callbacks */
ot->invoke = edbm_shortest_path_pick_invoke;
ot->poll = ED_operator_editmesh_region_view3d;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
}
/* -------------------------------------------------------------------- */
/* Select path between existing selection */
static bool ele_filter_visible_cb(BMElem *ele, void *UNUSED(user_data))
{
return !BM_elem_flag_test(ele, BM_ELEM_HIDDEN);
}
static int edbm_shortest_path_select_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(ob);
BMesh *bm = em->bm;
BMIter iter;
BMEditSelection *ese_src, *ese_dst;
BMElem *ele_src = NULL, *ele_dst = NULL, *ele;
const bool use_length = RNA_boolean_get(op->ptr, "use_length");
/* first try to find vertices in edit selection */
ese_src = bm->selected.last;
if (ese_src && (ese_dst = ese_src->prev) && (ese_src->htype == ese_dst->htype)) {
ele_src = ese_src->ele;
ele_dst = ese_dst->ele;
}
else {
/* if selection history isn't available, find two selected elements */
ele_src = ele_dst = NULL;
if ((em->selectmode & SCE_SELECT_VERTEX) && (bm->totvertsel >= 2)) {
BM_ITER_MESH (ele, &iter, bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
if (ele_src == NULL) ele_src = ele;
else if (ele_dst == NULL) ele_dst = ele;
else break;
}
}
}
if ((ele_dst == NULL) && (em->selectmode & SCE_SELECT_EDGE) && (bm->totedgesel >= 2)) {
ele_src = NULL;
BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
if (ele_src == NULL) ele_src = ele;
else if (ele_dst == NULL) ele_dst = ele;
else break;
}
}
}
if ((ele_dst == NULL) && (em->selectmode & SCE_SELECT_FACE) && (bm->totfacesel >= 2)) {
ele_src = NULL;
BM_ITER_MESH (ele, &iter, bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
if (ele_src == NULL) ele_src = ele;
else if (ele_dst == NULL) ele_dst = ele;
else break;
}
}
}
}
if (ele_src && ele_dst) {
LinkNode *path = NULL;
switch (ele_src->head.htype) {
case BM_VERT:
path = BM_mesh_calc_path_vert(
bm, (BMVert *)ele_src, (BMVert *)ele_dst, use_length,
NULL, (bool (*)(BMVert *, void *))ele_filter_visible_cb);
break;
case BM_EDGE:
path = BM_mesh_calc_path_edge(
bm, (BMEdge *)ele_src, (BMEdge *)ele_dst, use_length,
NULL, (bool (*)(BMEdge *, void *))ele_filter_visible_cb);
break;
case BM_FACE:
path = BM_mesh_calc_path_face(
bm, (BMFace *)ele_src, (BMFace *)ele_dst, use_length,
NULL, (bool (*)(BMFace *, void *))ele_filter_visible_cb);
break;
}
if (path) {
LinkNode *node = path;
do {
BM_elem_select_set(bm, node->link, true);
} while ((node = node->next));
BLI_linklist_free(path, NULL);
}
else {
BKE_report(op->reports, RPT_WARNING, "Path can't be found");
return OPERATOR_CANCELLED;
}
EDBM_selectmode_flush(em);
EDBM_update_generic(em, false, false);
return OPERATOR_FINISHED;
}
else {
BKE_report(op->reports, RPT_WARNING, "Path selection requires two matching elements to be selected");
return OPERATOR_CANCELLED;
}
}
void MESH_OT_shortest_path_select(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Shortest Path";
ot->idname = "MESH_OT_shortest_path_select";
ot->description = "Selected vertex path between two vertices";
/* api callbacks */
ot->exec = edbm_shortest_path_select_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_boolean(ot->srna, "use_length", true, "Length", "Use length when measuring distance");
}
|