svn merge -r39890:39951 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / editors / mesh / loopcut.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joseph Eagar, Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/mesh/loopcut.c
30  *  \ingroup edmesh
31  */
32
33
34 #include <float.h>
35 #define _USE_MATH_DEFINES
36 #include <math.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <stdio.h>
40
41 #include "DNA_ID.h"
42 #include "DNA_object_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_blenlib.h"
52 #include "BLI_dynstr.h" /*for WM_operator_pystring */
53 #include "BLI_editVert.h"
54 #include "BLI_array.h"
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_array_mallocn.h"
65 #include "BKE_tessmesh.h"
66 #include "BKE_depsgraph.h"
67
68 #include "BIF_gl.h"
69 #include "BIF_glutil.h" /* for paint cursor */
70
71 #include "IMB_imbuf_types.h"
72
73 #include "ED_screen.h"
74 #include "ED_space_api.h"
75 #include "ED_view3d.h"
76 #include "ED_mesh.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 tringselOpData {
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
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 == (BMLoop*)l->prev);
166         while (l2->v != lasteed->v1 && l2->v != lasteed->v2) {
167                 l2 = rev ? (BMLoop*)l2->prev : (BMLoop*)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, 0, 0);
208                 eed = BMW_Begin(&walker, startedge);
209                 for (; eed; eed=BMW_Step(&walker)) {
210                         BM_Select(em->bm, eed, 1);
211                 }
212                 BMW_End(&walker);
213
214                 return;
215         }
216
217         BMW_Init(&walker, em->bm, BMW_EDGERING, 0, 0);
218         eed = startedge = BMW_Begin(&walker, startedge);
219         lastv1 = NULL;
220         for (lasteed=NULL; eed; eed=BMW_Step(&walker)) {
221                 if (lasteed) {
222                         if (lastv1) {
223                                 v[1][0] = v[0][0];
224                                 v[1][1] = v[0][1];
225                         } else {
226                                 v[1][0] = lasteed->v1;
227                                 v[1][1] = lasteed->v2;
228                                 lastv1 = lasteed->v1;
229                         }
230
231                         edgering_find_order(em, lasteed, eed, lastv1, v);
232                         lastv1 = v[0][0];
233
234                         for(i=1;i<=previewlines;i++){
235                                 co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
236                                 co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
237                                 co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
238
239                                 co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
240                                 co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
241                                 co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
242                                 
243                                 BLI_array_growone(edges);
244                                 VECCOPY(edges[tot][0], co[0]);
245                                 VECCOPY(edges[tot][1], co[1]);
246                                 tot++;
247                         }
248                 }
249                 lasteed = eed;
250         }
251         
252         if (lasteed != startedge && BM_Edge_Share_Faces(lasteed, startedge)) {
253                 v[1][0] = v[0][0];
254                 v[1][1] = v[0][1];
255
256                 edgering_find_order(em, lasteed, startedge, lastv1, v);
257                 
258                 for(i=1;i<=previewlines;i++){
259                         if (!v[0][0] || !v[0][1] || !v[1][0] || !v[1][1])
260                                 continue;
261                         
262                         co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
263                         co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
264                         co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
265
266                         co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
267                         co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
268                         co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
269                         
270                         BLI_array_growone(edges);
271                         VECCOPY(edges[tot][0], co[0]);
272                         VECCOPY(edges[tot][1], co[1]);
273                         tot++;
274                 }
275         }
276
277         BMW_End(&walker);
278         lcd->edges = edges;
279         lcd->totedge = tot;
280 }
281
282 static void ringsel_find_edge(tringselOpData *lcd, int cuts)
283 {
284         if (lcd->eed) {
285                 edgering_sel(lcd, cuts, 0);
286         } else if(lcd->edges) {
287                 if (lcd->edges)
288                         MEM_freeN(lcd->edges);
289                 lcd->edges = NULL;
290                 lcd->totedge = 0;
291         }
292 }
293
294 static void ringsel_finish(bContext *C, wmOperator *op)
295 {
296         tringselOpData *lcd= op->customdata;
297         int cuts= RNA_int_get(op->ptr, "number_cuts");
298
299         if (lcd->eed) {
300                 BMEditMesh *em = lcd->em;
301
302                 edgering_sel(lcd, cuts, 1);
303                 
304                 if (lcd->do_cut) {
305                         BM_esubdivideflag(lcd->ob, em->bm, BM_SELECT, 0.0f, 
306                                           0.0f, 0, cuts, SUBDIV_SELECT_LOOPCUT, 
307                                           SUBD_PATH, 0, 0, 0);
308                         
309                         /* force edge slide to edge select mode in in face select mode */
310                         if (em->selectmode & SCE_SELECT_FACE) {
311                                 if (em->selectmode == SCE_SELECT_FACE)
312                                         em->selectmode = SCE_SELECT_EDGE;
313                                 else
314                                         em->selectmode &= ~SCE_SELECT_FACE;
315                                 CTX_data_tool_settings(C)->selectmode= em->selectmode;
316                                 EDBM_selectmode_set(em);
317
318                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
319
320                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
321                         }
322
323                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT|ND_DATA, lcd->ob->data);
324                         DAG_id_tag_update(lcd->ob->data, 0);
325                 }
326                 else {
327                         
328                         /* sets as active, useful for other tools */
329                         if(em->selectmode & SCE_SELECT_VERTEX)
330                                 EDBM_selectmode_flush(em);
331                         if(em->selectmode & SCE_SELECT_EDGE)
332                                 EDBM_selectmode_flush(em);
333                         
334                         EDBM_selectmode_flush(lcd->em);
335                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, lcd->ob->data);
336                 }
337         }
338 }
339
340 /* called when modal loop selection is done... */
341 static void ringsel_exit(bContext *UNUSED(C), wmOperator *op)
342 {
343         tringselOpData *lcd= op->customdata;
344         
345         /* deactivate the extra drawing stuff in 3D-View */
346         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
347         
348         if (lcd->edges)
349                 MEM_freeN(lcd->edges);
350
351         ED_region_tag_redraw(lcd->ar);
352
353         /* free the custom data */
354         MEM_freeN(lcd);
355         op->customdata= NULL;
356 }
357
358 /* called when modal loop selection gets set up... */
359 static int ringsel_init (bContext *C, wmOperator *op, int do_cut)
360 {
361         tringselOpData *lcd;
362         
363         /* alloc new customdata */
364         lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
365         
366         /* assign the drawing handle for drawing preview line... */
367         lcd->ar= CTX_wm_region(C);
368         lcd->draw_handle= ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
369         lcd->ob = CTX_data_edit_object(C);
370         lcd->em= ((Mesh *)lcd->ob->data)->edit_btmesh;
371         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
372         lcd->do_cut = do_cut;
373         em_setup_viewcontext(C, &lcd->vc);
374
375         ED_region_tag_redraw(lcd->ar);
376
377         return 1;
378 }
379
380 static int ringcut_cancel (bContext *C, wmOperator *op)
381 {
382         /* this is just a wrapper around exit() */
383         ringsel_exit(C, op);
384         return OPERATOR_CANCELLED;
385 }
386
387 static int ringsel_invoke (bContext *C, wmOperator *op, wmEvent *evt)
388 {
389         tringselOpData *lcd;
390         BMEdge *edge;
391         int dist = 75;
392         
393         view3d_operator_needs_opengl(C);
394
395         if (!ringsel_init(C, op, 0))
396                 return OPERATOR_CANCELLED;
397         
398         lcd = op->customdata;
399         
400         if (lcd->em->selectmode == SCE_SELECT_FACE) {
401                 ringsel_exit(C, op);
402                 WM_operator_name_call(C, "MESH_OT_loop_select", WM_OP_INVOKE_REGION_WIN, NULL);
403                 return OPERATOR_CANCELLED;
404         }
405
406         lcd->vc.mval[0] = evt->mval[0];
407         lcd->vc.mval[1] = evt->mval[1];
408         
409         edge = EDBM_findnearestedge(&lcd->vc, &dist);
410
411         lcd->eed = edge;
412         
413         ringsel_find_edge(lcd, 1);
414         ringsel_finish(C, op);
415         ringsel_exit(C, op);
416
417         return OPERATOR_FINISHED;
418 }
419
420 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
421 {
422         Object *obedit= CTX_data_edit_object(C);
423         tringselOpData *lcd;
424         BMEdge *edge;
425         int dist = 75;
426
427         if(modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
428                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
429         
430         view3d_operator_needs_opengl(C);
431
432         if (!ringsel_init(C, op, 1))
433                 return OPERATOR_CANCELLED;
434         
435         /* add a modal handler for this operator - handles loop selection */
436         WM_event_add_modal_handler(C, op);
437
438         lcd = op->customdata;
439         lcd->vc.mval[0] = evt->mval[0];
440         lcd->vc.mval[1] = evt->mval[1];
441         
442         edge = EDBM_findnearestedge(&lcd->vc, &dist);
443         if (edge != lcd->eed) {
444                 lcd->eed = edge;
445                 ringsel_find_edge(lcd, 1);
446         }
447         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
448         
449         return OPERATOR_RUNNING_MODAL;
450 }
451
452 static int loopcut_modal (bContext *C, wmOperator *op, wmEvent *event)
453 {
454         int cuts= RNA_int_get(op->ptr,"number_cuts");
455         tringselOpData *lcd= op->customdata;
456
457         view3d_operator_needs_opengl(C);
458
459         switch (event->type) {
460                 case RETKEY:
461                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
462                         if (event->val == KM_PRESS) {
463                                 /* finish */
464                                 ED_region_tag_redraw(lcd->ar);
465                                 
466                                 ringsel_finish(C, op);
467                                 ringsel_exit(C, op);
468                                 
469                                 ED_area_headerprint(CTX_wm_area(C), NULL);
470                                 
471                                 return OPERATOR_FINISHED;
472                         }
473                         
474                         ED_region_tag_redraw(lcd->ar);
475                         break;
476                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
477                         ED_region_tag_redraw(lcd->ar);
478                         ringsel_exit(C, op);
479                         ED_area_headerprint(CTX_wm_area(C), NULL);
480
481                         return OPERATOR_FINISHED;
482                 case ESCKEY:
483                         if (event->val == KM_RELEASE) {
484                                 /* cancel */
485                                 ED_region_tag_redraw(lcd->ar);
486                                 ED_area_headerprint(CTX_wm_area(C), NULL);
487                                 
488                                 return ringcut_cancel(C, op);
489                         }
490                         
491                         ED_region_tag_redraw(lcd->ar);
492                         break;
493                 case PAGEUPKEY:
494                 case WHEELUPMOUSE:  /* change number of cuts */
495                         if (event->val == KM_RELEASE)
496                                 break;
497
498                         cuts++;
499                         RNA_int_set(op->ptr,"number_cuts",cuts);
500                         ringsel_find_edge(lcd, cuts);
501                         
502                         ED_region_tag_redraw(lcd->ar);
503                         break;
504                 case PAGEDOWNKEY:
505                 case WHEELDOWNMOUSE:  /* change number of cuts */
506                         if (event->val == KM_RELEASE)
507                                 break;
508
509                         cuts=MAX2(cuts-1,1);
510                         RNA_int_set(op->ptr,"number_cuts",cuts);
511                         ringsel_find_edge(lcd, cuts);
512                         
513                         ED_region_tag_redraw(lcd->ar);
514                         break;
515                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
516                         int dist = 75;
517                         BMEdge *edge;
518
519                         lcd->vc.mval[0] = event->mval[0];
520                         lcd->vc.mval[1] = event->mval[1];
521                         edge = EDBM_findnearestedge(&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         /* keep going until the user confirms */
534         return OPERATOR_RUNNING_MODAL;
535 }
536
537 void MESH_OT_edgering_select (wmOperatorType *ot)
538 {
539         /* description */
540         ot->name= "Edge Ring Select";
541         ot->idname= "MESH_OT_edgering_select";
542         ot->description= "Select an edge ring";
543         
544         /* callbacks */
545         ot->invoke= ringsel_invoke;
546         ot->poll= ED_operator_editmesh_region_view3d; 
547         
548         /* flags */
549         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
550
551         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
552 }
553
554 void MESH_OT_loopcut (wmOperatorType *ot)
555 {
556         /* description */
557         ot->name= "Loop Cut";
558         ot->idname= "MESH_OT_loopcut";
559         ot->description= "Add a new loop between existing loops.";
560         
561         /* callbacks */
562         ot->invoke= ringcut_invoke;
563         ot->modal= loopcut_modal;
564         ot->cancel= ringcut_cancel;
565         ot->poll= ED_operator_editmesh_region_view3d;
566         
567         /* flags */
568         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
569
570         /* properties */
571         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
572 }