svn merge ^/trunk/blender -r41932:41939
[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
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
102         int extend;
103         int do_cut;
104 } tringselOpData;
105
106 /* modal loop selection drawing callback */
107 static void ringsel_draw(const bContext *C, ARegion *UNUSED(ar), void *arg)
108 {
109         View3D *v3d = CTX_wm_view3d(C);
110         tringselOpData *lcd = arg;
111         int i;
112         
113         if (lcd->totedge > 0) {
114                 if(v3d && v3d->zbuf)
115                         glDisable(GL_DEPTH_TEST);
116
117                 glPushMatrix();
118                 glMultMatrixf(lcd->ob->obmat);
119
120                 glColor3ub(255, 0, 255);
121                 glBegin(GL_LINES);
122                 for (i=0; i<lcd->totedge; i++) {
123                         glVertex3fv(lcd->edges[i][0]);
124                         glVertex3fv(lcd->edges[i][1]);
125                 }
126                 glEnd();
127
128                 glPopMatrix();
129                 if(v3d && v3d->zbuf)
130                         glEnable(GL_DEPTH_TEST);
131         }
132 }
133
134 /*given two opposite edges in a face, finds the ordering of their vertices so
135   that cut preview lines won't cross each other*/
136 static void edgering_find_order(BMEditMesh *em, BMEdge *lasteed, BMEdge *eed, 
137                                 BMVert *lastv1, BMVert *v[2][2])
138 {
139         BMIter liter;
140         BMLoop *l, *l2;
141         int rev;
142
143         l = eed->l;
144
145         /*find correct order for v[1]*/
146         if (!(BM_Edge_In_Face(l->f, eed) && BM_Edge_In_Face(l->f, lasteed))) {
147                 BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_LOOP, l) {
148                         if (BM_Edge_In_Face(l->f, eed) && BM_Edge_In_Face(l->f, lasteed))
149                                 break;
150                 }
151         }
152         
153         /*this should never happen*/
154         if (!l) {
155                 v[0][0] = eed->v1;
156                 v[0][1] = eed->v2;
157                 v[1][0] = lasteed->v1;
158                 v[1][1] = lasteed->v2;
159                 return;
160         }
161         
162         l2 = BM_OtherFaceLoop(l->e, l->f, eed->v1);
163         rev = (l2 == l->prev);
164         while (l2->v != lasteed->v1 && l2->v != lasteed->v2) {
165                 l2 = rev ? l2->prev : l2->next;
166         }
167
168         if (l2->v == lastv1) {
169                 v[0][0] = eed->v1;
170                 v[0][1] = eed->v2;
171         } else {
172                 v[0][0] = eed->v2;
173                 v[0][1] = eed->v1;
174         }
175 }
176
177 static void edgering_sel(tringselOpData *lcd, int previewlines, int select)
178 {
179         BMEditMesh *em = lcd->em;
180         BMEdge *startedge = lcd->eed;
181         BMEdge *eed, *lasteed;
182         BMVert *v[2][2], *lastv1;
183         BMWalker walker;
184         float (*edges)[2][3] = NULL;
185         BLI_array_declare(edges);
186         float co[2][3];
187         int i, tot=0;
188         
189         memset(v, 0, sizeof(v));
190         
191         if (!startedge)
192                 return;
193
194         if (lcd->edges) {
195                 MEM_freeN(lcd->edges);
196                 lcd->edges = NULL;
197                 lcd->totedge = 0;
198         }
199
200         if (!lcd->extend) {
201                 EDBM_clear_flag_all(lcd->em, BM_SELECT);
202         }
203
204         if (select) {
205                 BMW_Init(&walker, em->bm, BMW_EDGERING, 0, 0);
206                 eed = BMW_Begin(&walker, startedge);
207                 for (; eed; eed=BMW_Step(&walker)) {
208                         BM_Select(em->bm, eed, 1);
209                 }
210                 BMW_End(&walker);
211
212                 return;
213         }
214
215         BMW_Init(&walker, em->bm, BMW_EDGERING, 0, 0);
216         eed = startedge = BMW_Begin(&walker, startedge);
217         lastv1 = NULL;
218         for (lasteed=NULL; eed; eed=BMW_Step(&walker)) {
219                 if (lasteed) {
220                         if (lastv1) {
221                                 v[1][0] = v[0][0];
222                                 v[1][1] = v[0][1];
223                         } else {
224                                 v[1][0] = lasteed->v1;
225                                 v[1][1] = lasteed->v2;
226                                 lastv1 = lasteed->v1;
227                         }
228
229                         edgering_find_order(em, lasteed, eed, lastv1, v);
230                         lastv1 = v[0][0];
231
232                         for(i=1;i<=previewlines;i++){
233                                 co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
234                                 co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
235                                 co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
236
237                                 co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
238                                 co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
239                                 co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
240                                 
241                                 BLI_array_growone(edges);
242                                 VECCOPY(edges[tot][0], co[0]);
243                                 VECCOPY(edges[tot][1], co[1]);
244                                 tot++;
245                         }
246                 }
247                 lasteed = eed;
248         }
249         
250         if (lasteed != startedge && BM_Edge_Share_Faces(lasteed, startedge)) {
251                 v[1][0] = v[0][0];
252                 v[1][1] = v[0][1];
253
254                 edgering_find_order(em, lasteed, startedge, lastv1, v);
255                 
256                 for(i=1;i<=previewlines;i++){
257                         if (!v[0][0] || !v[0][1] || !v[1][0] || !v[1][1])
258                                 continue;
259                         
260                         co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
261                         co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
262                         co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
263
264                         co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
265                         co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
266                         co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
267                         
268                         BLI_array_growone(edges);
269                         VECCOPY(edges[tot][0], co[0]);
270                         VECCOPY(edges[tot][1], co[1]);
271                         tot++;
272                 }
273         }
274
275         BMW_End(&walker);
276         lcd->edges = edges;
277         lcd->totedge = tot;
278 }
279
280 static void ringsel_find_edge(tringselOpData *lcd, int cuts)
281 {
282         if (lcd->eed) {
283                 edgering_sel(lcd, cuts, 0);
284         } else if(lcd->edges) {
285                 if (lcd->edges)
286                         MEM_freeN(lcd->edges);
287                 lcd->edges = NULL;
288                 lcd->totedge = 0;
289         }
290 }
291
292 static void ringsel_finish(bContext *C, wmOperator *op)
293 {
294         tringselOpData *lcd= op->customdata;
295         int cuts= RNA_int_get(op->ptr, "number_cuts");
296
297         if (lcd->eed) {
298                 BMEditMesh *em = lcd->em;
299
300                 edgering_sel(lcd, cuts, 1);
301                 
302                 if (lcd->do_cut) {
303                         BM_esubdivideflag(lcd->ob, em->bm, BM_SELECT, 0.0f, 
304                                           0.0f, 0, cuts, SUBDIV_SELECT_LOOPCUT, 
305                                           SUBD_PATH, 0, 0, 0);
306                         
307                         /* force edge slide to edge select mode in in face select mode */
308                         if (em->selectmode & SCE_SELECT_FACE) {
309                                 if (em->selectmode == SCE_SELECT_FACE)
310                                         em->selectmode = SCE_SELECT_EDGE;
311                                 else
312                                         em->selectmode &= ~SCE_SELECT_FACE;
313                                 CTX_data_tool_settings(C)->selectmode= em->selectmode;
314                                 EDBM_selectmode_set(em);
315
316                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
317
318                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
319                         }
320
321                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT|ND_DATA, lcd->ob->data);
322                         DAG_id_tag_update(lcd->ob->data, 0);
323                 }
324                 else {
325                         
326                         /* sets as active, useful for other tools */
327                         if(em->selectmode & SCE_SELECT_VERTEX)
328                                 EDBM_selectmode_flush(em);
329                         if(em->selectmode & SCE_SELECT_EDGE)
330                                 EDBM_selectmode_flush(em);
331                         
332                         EDBM_selectmode_flush(lcd->em);
333                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, lcd->ob->data);
334                 }
335         }
336 }
337
338 /* called when modal loop selection is done... */
339 static void ringsel_exit(bContext *UNUSED(C), wmOperator *op)
340 {
341         tringselOpData *lcd= op->customdata;
342         
343         /* deactivate the extra drawing stuff in 3D-View */
344         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
345         
346         if (lcd->edges)
347                 MEM_freeN(lcd->edges);
348
349         ED_region_tag_redraw(lcd->ar);
350
351         /* free the custom data */
352         MEM_freeN(lcd);
353         op->customdata= NULL;
354 }
355
356 /* called when modal loop selection gets set up... */
357 static int ringsel_init (bContext *C, wmOperator *op, int do_cut)
358 {
359         tringselOpData *lcd;
360         
361         /* alloc new customdata */
362         lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
363         
364         /* assign the drawing handle for drawing preview line... */
365         lcd->ar= CTX_wm_region(C);
366         lcd->draw_handle= ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
367         lcd->ob = CTX_data_edit_object(C);
368         lcd->em= ((Mesh *)lcd->ob->data)->edit_btmesh;
369         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
370         lcd->do_cut = do_cut;
371         em_setup_viewcontext(C, &lcd->vc);
372
373         ED_region_tag_redraw(lcd->ar);
374
375         return 1;
376 }
377
378 static int ringcut_cancel (bContext *C, wmOperator *op)
379 {
380         /* this is just a wrapper around exit() */
381         ringsel_exit(C, op);
382         return OPERATOR_CANCELLED;
383 }
384
385 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
386 {
387         Object *obedit= CTX_data_edit_object(C);
388         tringselOpData *lcd;
389         BMEdge *edge;
390         int dist = 75;
391
392         if(modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
393                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
394         
395         view3d_operator_needs_opengl(C);
396
397         if (!ringsel_init(C, op, 1))
398                 return OPERATOR_CANCELLED;
399         
400         /* add a modal handler for this operator - handles loop selection */
401         WM_event_add_modal_handler(C, op);
402
403         lcd = op->customdata;
404         lcd->vc.mval[0] = evt->mval[0];
405         lcd->vc.mval[1] = evt->mval[1];
406         
407         edge = EDBM_findnearestedge(&lcd->vc, &dist);
408         if (edge != lcd->eed) {
409                 lcd->eed = edge;
410                 ringsel_find_edge(lcd, 1);
411         }
412         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
413         
414         return OPERATOR_RUNNING_MODAL;
415 }
416
417 static int loopcut_modal (bContext *C, wmOperator *op, wmEvent *event)
418 {
419         int cuts= RNA_int_get(op->ptr,"number_cuts");
420         tringselOpData *lcd= op->customdata;
421
422         view3d_operator_needs_opengl(C);
423
424         switch (event->type) {
425                 case RETKEY:
426                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
427                         if (event->val == KM_PRESS) {
428                                 /* finish */
429                                 ED_region_tag_redraw(lcd->ar);
430                                 
431                                 ringsel_finish(C, op);
432                                 ringsel_exit(C, op);
433                                 
434                                 ED_area_headerprint(CTX_wm_area(C), NULL);
435                                 
436                                 return OPERATOR_FINISHED;
437                         }
438                         
439                         ED_region_tag_redraw(lcd->ar);
440                         break;
441                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
442                         ED_region_tag_redraw(lcd->ar);
443                         ringsel_exit(C, op);
444                         ED_area_headerprint(CTX_wm_area(C), NULL);
445
446                         return OPERATOR_FINISHED;
447                 case ESCKEY:
448                         if (event->val == KM_RELEASE) {
449                                 /* cancel */
450                                 ED_region_tag_redraw(lcd->ar);
451                                 ED_area_headerprint(CTX_wm_area(C), NULL);
452                                 
453                                 return ringcut_cancel(C, op);
454                         }
455                         
456                         ED_region_tag_redraw(lcd->ar);
457                         break;
458                 case PAGEUPKEY:
459                 case WHEELUPMOUSE:  /* change number of cuts */
460                         if (event->val == KM_RELEASE)
461                                 break;
462
463                         cuts++;
464                         RNA_int_set(op->ptr,"number_cuts",cuts);
465                         ringsel_find_edge(lcd, cuts);
466                         
467                         ED_region_tag_redraw(lcd->ar);
468                         break;
469                 case PAGEDOWNKEY:
470                 case WHEELDOWNMOUSE:  /* change number of cuts */
471                         if (event->val == KM_RELEASE)
472                                 break;
473
474                         cuts=MAX2(cuts-1,1);
475                         RNA_int_set(op->ptr,"number_cuts",cuts);
476                         ringsel_find_edge(lcd, cuts);
477                         
478                         ED_region_tag_redraw(lcd->ar);
479                         break;
480                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
481                         int dist = 75;
482                         BMEdge *edge;
483
484                         lcd->vc.mval[0] = event->mval[0];
485                         lcd->vc.mval[1] = event->mval[1];
486                         edge = EDBM_findnearestedge(&lcd->vc, &dist);
487
488                         if (edge != lcd->eed) {
489                                 lcd->eed = edge;
490                                 ringsel_find_edge(lcd, cuts);
491                         }
492
493                         ED_region_tag_redraw(lcd->ar);
494                         break;
495                 }                       
496         }
497         
498         /* keep going until the user confirms */
499         return OPERATOR_RUNNING_MODAL;
500 }
501
502 void MESH_OT_loopcut (wmOperatorType *ot)
503 {
504         /* description */
505         ot->name= "Loop Cut";
506         ot->idname= "MESH_OT_loopcut";
507         ot->description= "Add a new loop between existing loops";
508         
509         /* callbacks */
510         ot->invoke= ringcut_invoke;
511         ot->modal= loopcut_modal;
512         ot->cancel= ringcut_cancel;
513         ot->poll= ED_operator_editmesh_region_view3d;
514         
515         /* flags */
516         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
517
518         /* properties */
519         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
520 }