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