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