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