svn merge ^/trunk/blender -r42009:42053
[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_blenlib.h"
50 #include "BLI_dynstr.h" /*for WM_operator_pystring */
51 #include "BLI_editVert.h"
52 #include "BLI_array.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_array_mallocn.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, 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         
374         initNumInput(&lcd->num);
375         lcd->num.idx_max = 0;
376         lcd->num.flag |= NUM_NO_NEGATIVE | NUM_NO_FRACTION;
377         
378         em_setup_viewcontext(C, &lcd->vc);
379
380         ED_region_tag_redraw(lcd->ar);
381
382         return 1;
383 }
384
385 static int ringcut_cancel (bContext *C, wmOperator *op)
386 {
387         /* this is just a wrapper around exit() */
388         ringsel_exit(C, op);
389         return OPERATOR_CANCELLED;
390 }
391
392 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
393 {
394         Object *obedit= CTX_data_edit_object(C);
395         tringselOpData *lcd;
396         BMEdge *edge;
397         int dist = 75;
398
399         if(modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
400                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
401         
402         view3d_operator_needs_opengl(C);
403
404         if (!ringsel_init(C, op, 1))
405                 return OPERATOR_CANCELLED;
406         
407         /* add a modal handler for this operator - handles loop selection */
408         WM_event_add_modal_handler(C, op);
409
410         lcd = op->customdata;
411         lcd->vc.mval[0] = evt->mval[0];
412         lcd->vc.mval[1] = evt->mval[1];
413         
414         edge = EDBM_findnearestedge(&lcd->vc, &dist);
415         if (edge != lcd->eed) {
416                 lcd->eed = edge;
417                 ringsel_find_edge(lcd, 1);
418         }
419         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
420         
421         return OPERATOR_RUNNING_MODAL;
422 }
423
424 static int loopcut_modal (bContext *C, wmOperator *op, wmEvent *event)
425 {
426         int cuts= RNA_int_get(op->ptr,"number_cuts");
427         tringselOpData *lcd= op->customdata;
428
429         view3d_operator_needs_opengl(C);
430
431         switch (event->type) {
432                 case RETKEY:
433                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
434                         if (event->val == KM_PRESS) {
435                                 /* finish */
436                                 ED_region_tag_redraw(lcd->ar);
437                                 
438                                 ringsel_finish(C, op);
439                                 ringsel_exit(C, op);
440                                 
441                                 ED_area_headerprint(CTX_wm_area(C), NULL);
442                                 
443                                 return OPERATOR_FINISHED;
444                         }
445                         
446                         ED_region_tag_redraw(lcd->ar);
447                         break;
448                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
449                         ED_region_tag_redraw(lcd->ar);
450                         ringsel_exit(C, op);
451                         ED_area_headerprint(CTX_wm_area(C), NULL);
452
453                         return OPERATOR_FINISHED;
454                 case ESCKEY:
455                         if (event->val == KM_RELEASE) {
456                                 /* cancel */
457                                 ED_region_tag_redraw(lcd->ar);
458                                 ED_area_headerprint(CTX_wm_area(C), NULL);
459                                 
460                                 return ringcut_cancel(C, op);
461                         }
462                         
463                         ED_region_tag_redraw(lcd->ar);
464                         break;
465                 case PADPLUSKEY:
466                 case PAGEUPKEY:
467                 case WHEELUPMOUSE:  /* change number of cuts */
468                         if (event->val == KM_RELEASE)
469                                 break;
470
471                         cuts++;
472                         RNA_int_set(op->ptr,"number_cuts",cuts);
473                         ringsel_find_edge(lcd, cuts);
474                         
475                         ED_region_tag_redraw(lcd->ar);
476                         break;
477                 case PADMINUS:
478                 case PAGEDOWNKEY:
479                 case WHEELDOWNMOUSE:  /* change number of cuts */
480                         if (event->val == KM_RELEASE)
481                                 break;
482
483                         cuts=MAX2(cuts-1,1);
484                         RNA_int_set(op->ptr,"number_cuts",cuts);
485                         ringsel_find_edge(lcd, cuts);
486                         
487                         ED_region_tag_redraw(lcd->ar);
488                         break;
489                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
490                         int dist = 75;
491                         BMEdge *edge;
492
493                         lcd->vc.mval[0] = event->mval[0];
494                         lcd->vc.mval[1] = event->mval[1];
495                         edge = EDBM_findnearestedge(&lcd->vc, &dist);
496
497                         if (edge != lcd->eed) {
498                                 lcd->eed = edge;
499                                 ringsel_find_edge(lcd, cuts);
500                         }
501
502                         ED_region_tag_redraw(lcd->ar);
503                         break;
504                 }                       
505         }
506         
507         /* using the keyboard to input the number of cuts */
508         if (event->val==KM_PRESS) {
509                 float value;
510                 
511                 if (handleNumInput(&lcd->num, event))
512                 {
513                         applyNumInput(&lcd->num, &value);
514                         
515                         cuts= CLAMPIS(value, 1, 32);
516                         
517                         RNA_int_set(op->ptr,"number_cuts",cuts);
518                         ringsel_find_edge(lcd, cuts);
519                         
520                         ED_region_tag_redraw(lcd->ar);
521                 }
522         }
523         
524         /* keep going until the user confirms */
525         return OPERATOR_RUNNING_MODAL;
526 }
527
528 void MESH_OT_loopcut (wmOperatorType *ot)
529 {
530         /* description */
531         ot->name= "Loop Cut";
532         ot->idname= "MESH_OT_loopcut";
533         ot->description= "Add a new loop between existing loops";
534         
535         /* callbacks */
536         ot->invoke= ringcut_invoke;
537         ot->modal= loopcut_modal;
538         ot->cancel= ringcut_cancel;
539         ot->poll= ED_operator_editmesh_region_view3d;
540         
541         /* flags */
542         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
543
544         /* properties */
545         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
546 }