262f81abacb22833b7fae5ccb733ec1924c3c4a1
[blender.git] / source / blender / editors / mesh / editmesh_loopcut.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Joseph Eagar, Joshua Leung
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/mesh/editmesh_loopcut.c
28  *  \ingroup edmesh
29  */
30
31 #include <float.h>
32 #ifdef _MSC_VER
33 #  define _USE_MATH_DEFINES
34 #endif
35 #include <math.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <stdio.h>
39
40 #include "DNA_ID.h"
41 #include "DNA_object_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_userdef_types.h"
46
47 #include "MEM_guardedalloc.h"
48
49 #include "PIL_time.h"
50
51 #include "BLI_array.h"
52 #include "BLI_blenlib.h"
53 #include "BLI_math.h"
54 #include "BLI_dynstr.h" /*for WM_operator_pystring */
55 #include "BLI_utildefines.h"
56
57 #include "BKE_blender.h"
58 #include "BKE_context.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_mesh.h"
61 #include "BKE_modifier.h"
62 #include "BKE_report.h"
63 #include "BKE_scene.h"
64 #include "BKE_tessmesh.h"
65 #include "BKE_depsgraph.h"
66
67 #include "BIF_gl.h"
68 #include "BIF_glutil.h" /* for paint cursor */
69
70 #include "IMB_imbuf_types.h"
71
72 #include "ED_screen.h"
73 #include "ED_space_api.h"
74 #include "ED_view3d.h"
75 #include "ED_mesh.h"
76 #include "ED_numinput.h"
77
78 #include "RNA_access.h"
79 #include "RNA_define.h"
80
81 #include "UI_interface.h"
82
83 #include "WM_api.h"
84 #include "WM_types.h"
85
86 #include "mesh_intern.h"
87
88 /* ringsel operator */
89
90 /* struct for properties used while drawing */
91 typedef struct RingSelOpData {
92         ARegion *ar;        /* region that ringsel was activated in */
93         void *draw_handle;  /* for drawing preview loop */
94         
95         float (*edges)[2][3];
96         int totedge;
97
98         ViewContext vc;
99
100         Object *ob;
101         BMEditMesh *em;
102         BMEdge *eed;
103         NumInput num;
104
105         int extend;
106         int do_cut;
107 } RingSelOpData;
108
109 /* modal loop selection drawing callback */
110 static void ringsel_draw(const bContext *C, ARegion *UNUSED(ar), void *arg)
111 {
112         View3D *v3d = CTX_wm_view3d(C);
113         RingSelOpData *lcd = arg;
114         int i;
115         
116         if (lcd->totedge > 0) {
117                 if (v3d && v3d->zbuf)
118                         glDisable(GL_DEPTH_TEST);
119
120                 glPushMatrix();
121                 glMultMatrixf(lcd->ob->obmat);
122
123                 glColor3ub(255, 0, 255);
124                 glBegin(GL_LINES);
125                 for (i = 0; i < lcd->totedge; i++) {
126                         glVertex3fv(lcd->edges[i][0]);
127                         glVertex3fv(lcd->edges[i][1]);
128                 }
129                 glEnd();
130
131                 glPopMatrix();
132                 if (v3d && v3d->zbuf)
133                         glEnable(GL_DEPTH_TEST);
134         }
135 }
136
137 /* given two opposite edges in a face, finds the ordering of their vertices so
138  * that cut preview lines won't cross each other */
139 static void edgering_find_order(BMEdge *lasteed, BMEdge *eed,
140                                 BMVert *lastv1, BMVert *v[2][2])
141 {
142         BMIter liter;
143         BMLoop *l, *l2;
144         int rev;
145
146         l = eed->l;
147
148         /* find correct order for v[1] */
149         if (!(BM_edge_in_face(l->f, eed) && BM_edge_in_face(l->f, lasteed))) {
150                 BM_ITER_ELEM (l, &liter, l, BM_LOOPS_OF_LOOP) {
151                         if (BM_edge_in_face(l->f, eed) && BM_edge_in_face(l->f, lasteed))
152                                 break;
153                 }
154         }
155         
156         /* this should never happen */
157         if (!l) {
158                 v[0][0] = eed->v1;
159                 v[0][1] = eed->v2;
160                 v[1][0] = lasteed->v1;
161                 v[1][1] = lasteed->v2;
162                 return;
163         }
164         
165         l2 = BM_face_other_edge_loop(l->f, l->e, eed->v1);
166         rev = (l2 == l->prev);
167         while (l2->v != lasteed->v1 && l2->v != lasteed->v2) {
168                 l2 = rev ? l2->prev : l2->next;
169         }
170
171         if (l2->v == lastv1) {
172                 v[0][0] = eed->v1;
173                 v[0][1] = eed->v2;
174         }
175         else {
176                 v[0][0] = eed->v2;
177                 v[0][1] = eed->v1;
178         }
179 }
180
181 static void edgering_sel(RingSelOpData *lcd, int previewlines, int select)
182 {
183         BMEditMesh *em = lcd->em;
184         BMEdge *startedge = lcd->eed;
185         BMEdge *eed, *lasteed;
186         BMVert *v[2][2], *lastv1;
187         BMWalker walker;
188         float (*edges)[2][3] = NULL;
189         BLI_array_declare(edges);
190         float co[2][3];
191         int i, tot = 0;
192         
193         memset(v, 0, sizeof(v));
194         
195         if (!startedge)
196                 return;
197
198         if (lcd->edges) {
199                 MEM_freeN(lcd->edges);
200                 lcd->edges = NULL;
201                 lcd->totedge = 0;
202         }
203
204         if (!lcd->extend) {
205                 EDBM_flag_disable_all(lcd->em, BM_ELEM_SELECT);
206         }
207
208         if (select) {
209                 BMW_init(&walker, em->bm, BMW_EDGERING,
210                          BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
211                          BMW_FLAG_TEST_HIDDEN,
212                          BMW_NIL_LAY);
213
214                 eed = BMW_begin(&walker, startedge);
215                 for ( ; eed; eed = BMW_step(&walker)) {
216                         BM_edge_select_set(em->bm, eed, TRUE);
217                 }
218                 BMW_end(&walker);
219
220                 return;
221         }
222
223         BMW_init(&walker, em->bm, BMW_EDGERING,
224                  BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
225                  BMW_FLAG_TEST_HIDDEN,
226                  BMW_NIL_LAY);
227
228         eed = startedge = BMW_begin(&walker, startedge);
229         lastv1 = NULL;
230         for (lasteed = NULL; eed; eed = BMW_step(&walker)) {
231                 if (lasteed) {
232                         if (lastv1) {
233                                 v[1][0] = v[0][0];
234                                 v[1][1] = v[0][1];
235                         }
236                         else {
237                                 v[1][0] = lasteed->v1;
238                                 v[1][1] = lasteed->v2;
239                                 lastv1 = lasteed->v1;
240                         }
241
242                         edgering_find_order(lasteed, eed, lastv1, v);
243                         lastv1 = v[0][0];
244
245                         BLI_array_grow_items(edges, previewlines);
246
247                         for (i = 1; i <= previewlines; i++) {
248                                 co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0]) * (i / ((float)previewlines + 1)) + v[0][0]->co[0];
249                                 co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1]) * (i / ((float)previewlines + 1)) + v[0][0]->co[1];
250                                 co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2]) * (i / ((float)previewlines + 1)) + v[0][0]->co[2];
251
252                                 co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0]) * (i / ((float)previewlines + 1)) + v[1][0]->co[0];
253                                 co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1]) * (i / ((float)previewlines + 1)) + v[1][0]->co[1];
254                                 co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2]) * (i / ((float)previewlines + 1)) + v[1][0]->co[2];
255
256                                 copy_v3_v3(edges[tot][0], co[0]);
257                                 copy_v3_v3(edges[tot][1], co[1]);
258                                 tot++;
259                         }
260                 }
261                 lasteed = eed;
262         }
263         
264         if (lasteed != startedge && BM_edge_share_face_count(lasteed, startedge)) {
265                 v[1][0] = v[0][0];
266                 v[1][1] = v[0][1];
267
268                 edgering_find_order(lasteed, startedge, lastv1, v);
269                 
270                 BLI_array_grow_items(edges, previewlines);
271
272                 for (i = 1; i <= previewlines; i++) {
273                         if (!v[0][0] || !v[0][1] || !v[1][0] || !v[1][1])
274                                 continue;
275                         
276                         co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0]) * (i / ((float)previewlines + 1)) + v[0][0]->co[0];
277                         co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1]) * (i / ((float)previewlines + 1)) + v[0][0]->co[1];
278                         co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2]) * (i / ((float)previewlines + 1)) + v[0][0]->co[2];
279
280                         co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0]) * (i / ((float)previewlines + 1)) + v[1][0]->co[0];
281                         co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1]) * (i / ((float)previewlines + 1)) + v[1][0]->co[1];
282                         co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2]) * (i / ((float)previewlines + 1)) + v[1][0]->co[2];
283                         
284                         copy_v3_v3(edges[tot][0], co[0]);
285                         copy_v3_v3(edges[tot][1], co[1]);
286                         tot++;
287                 }
288         }
289
290         BMW_end(&walker);
291         lcd->edges = edges;
292         lcd->totedge = tot;
293 }
294
295 static void ringsel_find_edge(RingSelOpData *lcd, int cuts)
296 {
297         if (lcd->eed) {
298                 edgering_sel(lcd, cuts, 0);
299         }
300         else if (lcd->edges) {
301                 MEM_freeN(lcd->edges);
302                 lcd->edges = NULL;
303                 lcd->totedge = 0;
304         }
305 }
306
307 static void ringsel_finish(bContext *C, wmOperator *op)
308 {
309         RingSelOpData *lcd = op->customdata;
310         int cuts = RNA_int_get(op->ptr, "number_cuts");
311
312         if (lcd->eed) {
313                 BMEditMesh *em = lcd->em;
314
315                 edgering_sel(lcd, cuts, 1);
316                 
317                 if (lcd->do_cut) {
318                         /* Enable gridfill, so that intersecting loopcut works as one would expect.
319                          * Note though that it will break edgeslide in this specific case.
320                          * See [#31939]. */
321                         BM_mesh_esubdivide(em->bm, BM_ELEM_SELECT,
322                                            0.0f, 0.0f, 0.0f,
323                                            cuts,
324                                            SUBDIV_SELECT_LOOPCUT, SUBD_PATH, 0, TRUE, 0);
325
326                         /* force edge slide to edge select mode in in face select mode */
327                         if (em->selectmode & SCE_SELECT_FACE) {
328                                 if (em->selectmode == SCE_SELECT_FACE)
329                                         em->selectmode = SCE_SELECT_EDGE;
330                                 else
331                                         em->selectmode &= ~SCE_SELECT_FACE;
332                                 CTX_data_tool_settings(C)->selectmode = em->selectmode;
333                                 EDBM_selectmode_set(em);
334
335                                 WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, CTX_data_scene(C));
336                         }
337                         else
338                                 EDBM_selectmode_flush(lcd->em);
339
340                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT | ND_DATA, lcd->ob->data);
341                         DAG_id_tag_update(lcd->ob->data, 0);
342                 }
343                 else {
344                         /* XXX Is this piece of code ever used now? Simple loop select is now
345                          *     in editmesh_select.c (around line 1000)... */
346                         /* sets as active, useful for other tools */
347                         if (em->selectmode & SCE_SELECT_VERTEX)
348                                 BM_select_history_store(em->bm, lcd->eed->v1);  /* low priority TODO, get vertrex close to mouse */
349                         if (em->selectmode & SCE_SELECT_EDGE)
350                                 BM_select_history_store(em->bm, lcd->eed);
351                         
352                         EDBM_selectmode_flush(lcd->em);
353                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, lcd->ob->data);
354                 }
355         }
356 }
357
358 /* called when modal loop selection is done... */
359 static void ringsel_exit(bContext *UNUSED(C), wmOperator *op)
360 {
361         RingSelOpData *lcd = op->customdata;
362
363         /* deactivate the extra drawing stuff in 3D-View */
364         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
365         
366         if (lcd->edges)
367                 MEM_freeN(lcd->edges);
368
369         ED_region_tag_redraw(lcd->ar);
370
371         /* free the custom data */
372         MEM_freeN(lcd);
373         op->customdata = NULL;
374 }
375
376
377 /* called when modal loop selection gets set up... */
378 static int ringsel_init(bContext *C, wmOperator *op, int do_cut)
379 {
380         RingSelOpData *lcd;
381
382         /* alloc new customdata */
383         lcd = op->customdata = MEM_callocN(sizeof(RingSelOpData), "ringsel Modal Op Data");
384         
385         /* assign the drawing handle for drawing preview line... */
386         lcd->ar = CTX_wm_region(C);
387         lcd->draw_handle = ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
388         lcd->ob = CTX_data_edit_object(C);
389         lcd->em = BMEdit_FromObject(lcd->ob);
390         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
391         lcd->do_cut = do_cut;
392         
393         initNumInput(&lcd->num);
394         lcd->num.idx_max = 0;
395         lcd->num.flag |= NUM_NO_NEGATIVE | NUM_NO_FRACTION;
396
397         /* XXX, temp, workaround for [# ] */
398         EDBM_mesh_ensure_valid_dm_hack(CTX_data_scene(C), lcd->em);
399
400         em_setup_viewcontext(C, &lcd->vc);
401
402         ED_region_tag_redraw(lcd->ar);
403
404         return 1;
405 }
406
407 static int ringcut_cancel(bContext *C, wmOperator *op)
408 {
409         /* this is just a wrapper around exit() */
410         ringsel_exit(C, op);
411         return OPERATOR_CANCELLED;
412 }
413
414 static int ringcut_invoke(bContext *C, wmOperator *op, wmEvent *evt)
415 {
416         Object *obedit = CTX_data_edit_object(C);
417         RingSelOpData *lcd;
418         BMEdge *edge;
419         float dist = 75.0f;
420
421         if (modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
422                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
423         
424         view3d_operator_needs_opengl(C);
425
426         if (!ringsel_init(C, op, 1))
427                 return OPERATOR_CANCELLED;
428         
429         /* add a modal handler for this operator - handles loop selection */
430         WM_event_add_modal_handler(C, op);
431
432         lcd = op->customdata;
433         lcd->vc.mval[0] = evt->mval[0];
434         lcd->vc.mval[1] = evt->mval[1];
435         
436         edge = EDBM_edge_find_nearest(&lcd->vc, &dist);
437         if (edge != lcd->eed) {
438                 lcd->eed = edge;
439                 ringsel_find_edge(lcd, 1);
440         }
441         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
442         
443         return OPERATOR_RUNNING_MODAL;
444 }
445
446 static int loopcut_modal(bContext *C, wmOperator *op, wmEvent *event)
447 {
448         int cuts = RNA_int_get(op->ptr, "number_cuts");
449         RingSelOpData *lcd = op->customdata;
450         int show_cuts = 0;
451
452         view3d_operator_needs_opengl(C);
453
454         switch (event->type) {
455                 case RETKEY:
456                 case PADENTER:
457                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
458                         if (event->val == KM_PRESS) {
459                                 /* finish */
460                                 ED_region_tag_redraw(lcd->ar);
461                                 
462                                 ringsel_finish(C, op);
463                                 ringsel_exit(C, op);
464                                 
465                                 ED_area_headerprint(CTX_wm_area(C), NULL);
466                                 
467                                 return OPERATOR_FINISHED;
468                         }
469                         
470                         ED_region_tag_redraw(lcd->ar);
471                         break;
472                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
473                         ED_region_tag_redraw(lcd->ar);
474                         ringsel_exit(C, op);
475                         ED_area_headerprint(CTX_wm_area(C), NULL);
476
477                         return OPERATOR_FINISHED;
478                 case ESCKEY:
479                         if (event->val == KM_RELEASE) {
480                                 /* cancel */
481                                 ED_region_tag_redraw(lcd->ar);
482                                 ED_area_headerprint(CTX_wm_area(C), NULL);
483                                 
484                                 return ringcut_cancel(C, op);
485                         }
486                         
487                         ED_region_tag_redraw(lcd->ar);
488                         break;
489                 case PADPLUSKEY:
490                 case PAGEUPKEY:
491                 case WHEELUPMOUSE:  /* change number of cuts */
492                         if (event->val == KM_RELEASE)
493                                 break;
494
495                         cuts++;
496                         RNA_int_set(op->ptr, "number_cuts", cuts);
497                         ringsel_find_edge(lcd, cuts);
498                         show_cuts = TRUE;
499                         
500                         ED_region_tag_redraw(lcd->ar);
501                         break;
502                 case PADMINUS:
503                 case PAGEDOWNKEY:
504                 case WHEELDOWNMOUSE:  /* change number of cuts */
505                         if (event->val == KM_RELEASE)
506                                 break;
507
508                         cuts = max_ii(cuts - 1, 0);
509                         RNA_int_set(op->ptr, "number_cuts", cuts);
510                         ringsel_find_edge(lcd, cuts);
511                         show_cuts = TRUE;
512                         
513                         ED_region_tag_redraw(lcd->ar);
514                         break;
515                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
516                         float dist = 75.0f;
517                         BMEdge *edge;
518
519                         lcd->vc.mval[0] = event->mval[0];
520                         lcd->vc.mval[1] = event->mval[1];
521                         edge = EDBM_edge_find_nearest(&lcd->vc, &dist);
522
523                         if (edge != lcd->eed) {
524                                 lcd->eed = edge;
525                                 ringsel_find_edge(lcd, cuts);
526                         }
527
528                         ED_region_tag_redraw(lcd->ar);
529                         break;
530                 }
531         }
532         
533         /* using the keyboard to input the number of cuts */
534         if (event->val == KM_PRESS) {
535                 /* init as zero so backspace clears */
536                 float value = 0.0f;
537                 
538                 if (handleNumInput(&lcd->num, event)) {
539                         applyNumInput(&lcd->num, &value);
540                         
541                         /* allow zero so you can backspace and type in a value
542                          * otherwise 1 as minimum would make more sense */
543                         cuts = CLAMPIS(value, 0, 130);
544                         
545                         RNA_int_set(op->ptr, "number_cuts", cuts);
546                         ringsel_find_edge(lcd, cuts);
547                         show_cuts = TRUE;
548                         
549                         ED_region_tag_redraw(lcd->ar);
550                 }
551         }
552         
553         if (show_cuts) {
554                 char buf[64];
555                 BLI_snprintf(buf, sizeof(buf), "Number of Cuts: %d", cuts);
556                 ED_area_headerprint(CTX_wm_area(C), buf);
557         }
558         
559         /* keep going until the user confirms */
560         return OPERATOR_RUNNING_MODAL;
561 }
562
563 /* for bmesh this tool is in bmesh_select.c */
564 #if 0
565
566 void MESH_OT_edgering_select(wmOperatorType *ot)
567 {
568         /* description */
569         ot->name = "Edge Ring Select";
570         ot->idname = "MESH_OT_edgering_select";
571         ot->description = "Select an edge ring";
572         
573         /* callbacks */
574         ot->invoke = ringsel_invoke;
575         ot->poll = ED_operator_editmesh_region_view3d; 
576         
577         /* flags */
578         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
579
580         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
581 }
582
583 #endif
584
585 void MESH_OT_loopcut(wmOperatorType *ot)
586 {
587         PropertyRNA *prop;
588
589         /* description */
590         ot->name = "Loop Cut";
591         ot->idname = "MESH_OT_loopcut";
592         ot->description = "Add a new loop between existing loops";
593         
594         /* callbacks */
595         ot->invoke = ringcut_invoke;
596         ot->modal = loopcut_modal;
597         ot->cancel = ringcut_cancel;
598         ot->poll = ED_operator_editmesh_region_view3d;
599         
600         /* flags */
601         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING;
602
603         /* properties */
604         prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
605         /* avoid re-using last var because it can cause _very_ high poly meshes and annoy users (or worse crash) */
606         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
607 }