svn merge ^/trunk/blender -r43443:43461
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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_dynstr.h" /*for WM_operator_pystring */
52 #include "BLI_editVert.h"
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_OtherFaceLoop(l->e, l->f, 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_clear_flag_all(lcd->em, BM_SELECT);
203         }
204
205         if (select) {
206                 BMW_Init(&walker, em->bm, BMW_EDGERING,  0,0,0,0,  BMW_NIL_LAY);
207                 eed = BMW_Begin(&walker, startedge);
208                 for (; eed; eed=BMW_Step(&walker)) {
209                         BM_Select(em->bm, eed, TRUE);
210                 }
211                 BMW_End(&walker);
212
213                 return;
214         }
215
216         BMW_Init(&walker, em->bm, BMW_EDGERING,  0,0,0,0,  BMW_NIL_LAY);
217         eed = startedge = BMW_Begin(&walker, startedge);
218         lastv1 = NULL;
219         for (lasteed=NULL; eed; eed=BMW_Step(&walker)) {
220                 if (lasteed) {
221                         if (lastv1) {
222                                 v[1][0] = v[0][0];
223                                 v[1][1] = v[0][1];
224                         } else {
225                                 v[1][0] = lasteed->v1;
226                                 v[1][1] = lasteed->v2;
227                                 lastv1 = lasteed->v1;
228                         }
229
230                         edgering_find_order(em, lasteed, eed, lastv1, v);
231                         lastv1 = v[0][0];
232
233                         for(i=1;i<=previewlines;i++){
234                                 co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
235                                 co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
236                                 co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
237
238                                 co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
239                                 co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
240                                 co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
241                                 
242                                 BLI_array_growone(edges);
243                                 VECCOPY(edges[tot][0], co[0]);
244                                 VECCOPY(edges[tot][1], co[1]);
245                                 tot++;
246                         }
247                 }
248                 lasteed = eed;
249         }
250         
251         if (lasteed != startedge && BM_Edge_Share_Faces(lasteed, startedge)) {
252                 v[1][0] = v[0][0];
253                 v[1][1] = v[0][1];
254
255                 edgering_find_order(em, lasteed, startedge, lastv1, v);
256                 
257                 for(i=1;i<=previewlines;i++){
258                         if (!v[0][0] || !v[0][1] || !v[1][0] || !v[1][1])
259                                 continue;
260                         
261                         co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
262                         co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
263                         co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
264
265                         co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
266                         co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
267                         co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
268                         
269                         BLI_array_growone(edges);
270                         copy_v3_v3(edges[tot][0], co[0]);
271                         copy_v3_v3(edges[tot][1], co[1]);
272                         tot++;
273                 }
274         }
275
276         BMW_End(&walker);
277         lcd->edges = edges;
278         lcd->totedge = tot;
279 }
280
281 static void ringsel_find_edge(tringselOpData *lcd, int cuts)
282 {
283         if (lcd->eed) {
284                 edgering_sel(lcd, cuts, 0);
285         } else if(lcd->edges) {
286                 if (lcd->edges)
287                         MEM_freeN(lcd->edges);
288                 lcd->edges = NULL;
289                 lcd->totedge = 0;
290         }
291 }
292
293 static void ringsel_finish(bContext *C, wmOperator *op)
294 {
295         tringselOpData *lcd= op->customdata;
296         int cuts= RNA_int_get(op->ptr, "number_cuts");
297
298         if (lcd->eed) {
299                 BMEditMesh *em = lcd->em;
300
301                 edgering_sel(lcd, cuts, 1);
302                 
303                 if (lcd->do_cut) {
304                         BM_esubdivideflag(lcd->ob, em->bm, BM_SELECT, 0.0f, 
305                                           0.0f, 0, cuts, SUBDIV_SELECT_LOOPCUT, 
306                                           SUBD_PATH, 0, 0, 0);
307                         
308                         /* force edge slide to edge select mode in in face select mode */
309                         if (em->selectmode & SCE_SELECT_FACE) {
310                                 if (em->selectmode == SCE_SELECT_FACE)
311                                         em->selectmode = SCE_SELECT_EDGE;
312                                 else
313                                         em->selectmode &= ~SCE_SELECT_FACE;
314                                 CTX_data_tool_settings(C)->selectmode= em->selectmode;
315                                 EDBM_selectmode_set(em);
316
317                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
318
319                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
320                         }
321
322                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT|ND_DATA, lcd->ob->data);
323                         DAG_id_tag_update(lcd->ob->data, 0);
324                 }
325                 else {
326                         
327                         /* sets as active, useful for other tools */
328                         if(em->selectmode & SCE_SELECT_VERTEX)
329                                 EDBM_selectmode_flush(em);
330                         if(em->selectmode & SCE_SELECT_EDGE)
331                                 EDBM_selectmode_flush(em);
332                         
333                         EDBM_selectmode_flush(lcd->em);
334                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, lcd->ob->data);
335                 }
336         }
337 }
338
339 /* called when modal loop selection is done... */
340 static void ringsel_exit(bContext *UNUSED(C), wmOperator *op)
341 {
342         tringselOpData *lcd= op->customdata;
343         
344         /* deactivate the extra drawing stuff in 3D-View */
345         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
346         
347         if (lcd->edges)
348                 MEM_freeN(lcd->edges);
349
350         ED_region_tag_redraw(lcd->ar);
351
352         /* free the custom data */
353         MEM_freeN(lcd);
354         op->customdata= NULL;
355 }
356
357 /* called when modal loop selection gets set up... */
358 static int ringsel_init (bContext *C, wmOperator *op, int do_cut)
359 {
360         tringselOpData *lcd;
361         
362         /* alloc new customdata */
363         lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
364         
365         /* assign the drawing handle for drawing preview line... */
366         lcd->ar= CTX_wm_region(C);
367         lcd->draw_handle= ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
368         lcd->ob = CTX_data_edit_object(C);
369         lcd->em= ((Mesh *)lcd->ob->data)->edit_btmesh;
370         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
371         lcd->do_cut = do_cut;
372         
373         initNumInput(&lcd->num);
374         lcd->num.idx_max = 0;
375         lcd->num.flag |= NUM_NO_NEGATIVE | NUM_NO_FRACTION;
376         
377         em_setup_viewcontext(C, &lcd->vc);
378
379         ED_region_tag_redraw(lcd->ar);
380
381         return 1;
382 }
383
384 static int ringcut_cancel (bContext *C, wmOperator *op)
385 {
386         /* this is just a wrapper around exit() */
387         ringsel_exit(C, op);
388         return OPERATOR_CANCELLED;
389 }
390
391 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
392 {
393         Object *obedit= CTX_data_edit_object(C);
394         tringselOpData *lcd;
395         BMEdge *edge;
396         int dist = 75;
397
398         if(modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
399                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
400         
401         view3d_operator_needs_opengl(C);
402
403         if (!ringsel_init(C, op, 1))
404                 return OPERATOR_CANCELLED;
405         
406         /* add a modal handler for this operator - handles loop selection */
407         WM_event_add_modal_handler(C, op);
408
409         lcd = op->customdata;
410         lcd->vc.mval[0] = evt->mval[0];
411         lcd->vc.mval[1] = evt->mval[1];
412         
413         edge = EDBM_findnearestedge(&lcd->vc, &dist);
414         if (edge != lcd->eed) {
415                 lcd->eed = edge;
416                 ringsel_find_edge(lcd, 1);
417         }
418         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
419         
420         return OPERATOR_RUNNING_MODAL;
421 }
422
423 static int loopcut_modal (bContext *C, wmOperator *op, wmEvent *event)
424 {
425         int cuts= RNA_int_get(op->ptr,"number_cuts");
426         tringselOpData *lcd= op->customdata;
427
428         view3d_operator_needs_opengl(C);
429
430         switch (event->type) {
431                 case RETKEY:
432                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
433                         if (event->val == KM_PRESS) {
434                                 /* finish */
435                                 ED_region_tag_redraw(lcd->ar);
436                                 
437                                 ringsel_finish(C, op);
438                                 ringsel_exit(C, op);
439                                 
440                                 ED_area_headerprint(CTX_wm_area(C), NULL);
441                                 
442                                 return OPERATOR_FINISHED;
443                         }
444                         
445                         ED_region_tag_redraw(lcd->ar);
446                         break;
447                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
448                         ED_region_tag_redraw(lcd->ar);
449                         ringsel_exit(C, op);
450                         ED_area_headerprint(CTX_wm_area(C), NULL);
451
452                         return OPERATOR_FINISHED;
453                 case ESCKEY:
454                         if (event->val == KM_RELEASE) {
455                                 /* cancel */
456                                 ED_region_tag_redraw(lcd->ar);
457                                 ED_area_headerprint(CTX_wm_area(C), NULL);
458                                 
459                                 return ringcut_cancel(C, op);
460                         }
461                         
462                         ED_region_tag_redraw(lcd->ar);
463                         break;
464                 case PADPLUSKEY:
465                 case PAGEUPKEY:
466                 case WHEELUPMOUSE:  /* change number of cuts */
467                         if (event->val == KM_RELEASE)
468                                 break;
469
470                         cuts++;
471                         RNA_int_set(op->ptr,"number_cuts",cuts);
472                         ringsel_find_edge(lcd, cuts);
473                         
474                         ED_region_tag_redraw(lcd->ar);
475                         break;
476                 case PADMINUS:
477                 case PAGEDOWNKEY:
478                 case WHEELDOWNMOUSE:  /* change number of cuts */
479                         if (event->val == KM_RELEASE)
480                                 break;
481
482                         cuts=MAX2(cuts-1,1);
483                         RNA_int_set(op->ptr,"number_cuts",cuts);
484                         ringsel_find_edge(lcd, cuts);
485                         
486                         ED_region_tag_redraw(lcd->ar);
487                         break;
488                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
489                         int dist = 75;
490                         BMEdge *edge;
491
492                         lcd->vc.mval[0] = event->mval[0];
493                         lcd->vc.mval[1] = event->mval[1];
494                         edge = EDBM_findnearestedge(&lcd->vc, &dist);
495
496                         if (edge != lcd->eed) {
497                                 lcd->eed = edge;
498                                 ringsel_find_edge(lcd, cuts);
499                         }
500
501                         ED_region_tag_redraw(lcd->ar);
502                         break;
503                 }                       
504         }
505         
506         /* using the keyboard to input the number of cuts */
507         if (event->val==KM_PRESS) {
508                 float value;
509                 
510                 if (handleNumInput(&lcd->num, event))
511                 {
512                         applyNumInput(&lcd->num, &value);
513                         
514                         cuts= CLAMPIS(value, 1, 32);
515                         
516                         RNA_int_set(op->ptr,"number_cuts",cuts);
517                         ringsel_find_edge(lcd, cuts);
518                         
519                         ED_region_tag_redraw(lcd->ar);
520                 }
521         }
522         
523         /* keep going until the user confirms */
524         return OPERATOR_RUNNING_MODAL;
525 }
526
527 void MESH_OT_loopcut (wmOperatorType *ot)
528 {
529         /* description */
530         ot->name= "Loop Cut";
531         ot->idname= "MESH_OT_loopcut";
532         ot->description= "Add a new loop between existing loops";
533         
534         /* callbacks */
535         ot->invoke= ringcut_invoke;
536         ot->modal= loopcut_modal;
537         ot->cancel= ringcut_cancel;
538         ot->poll= ED_operator_editmesh_region_view3d;
539         
540         /* flags */
541         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
542
543         /* properties */
544         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
545 }