svn merge -r40075:40104 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 ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
388 {
389         Object *obedit= CTX_data_edit_object(C);
390         tringselOpData *lcd;
391         BMEdge *edge;
392         int dist = 75;
393
394         if(modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
395                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
396         
397         view3d_operator_needs_opengl(C);
398
399         if (!ringsel_init(C, op, 1))
400                 return OPERATOR_CANCELLED;
401         
402         /* add a modal handler for this operator - handles loop selection */
403         WM_event_add_modal_handler(C, op);
404
405         lcd = op->customdata;
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         if (edge != lcd->eed) {
411                 lcd->eed = edge;
412                 ringsel_find_edge(lcd, 1);
413         }
414         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
415         
416         return OPERATOR_RUNNING_MODAL;
417 }
418
419 static int loopcut_modal (bContext *C, wmOperator *op, wmEvent *event)
420 {
421         int cuts= RNA_int_get(op->ptr,"number_cuts");
422         tringselOpData *lcd= op->customdata;
423
424         view3d_operator_needs_opengl(C);
425
426         switch (event->type) {
427                 case RETKEY:
428                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
429                         if (event->val == KM_PRESS) {
430                                 /* finish */
431                                 ED_region_tag_redraw(lcd->ar);
432                                 
433                                 ringsel_finish(C, op);
434                                 ringsel_exit(C, op);
435                                 
436                                 ED_area_headerprint(CTX_wm_area(C), NULL);
437                                 
438                                 return OPERATOR_FINISHED;
439                         }
440                         
441                         ED_region_tag_redraw(lcd->ar);
442                         break;
443                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
444                         ED_region_tag_redraw(lcd->ar);
445                         ringsel_exit(C, op);
446                         ED_area_headerprint(CTX_wm_area(C), NULL);
447
448                         return OPERATOR_FINISHED;
449                 case ESCKEY:
450                         if (event->val == KM_RELEASE) {
451                                 /* cancel */
452                                 ED_region_tag_redraw(lcd->ar);
453                                 ED_area_headerprint(CTX_wm_area(C), NULL);
454                                 
455                                 return ringcut_cancel(C, op);
456                         }
457                         
458                         ED_region_tag_redraw(lcd->ar);
459                         break;
460                 case PAGEUPKEY:
461                 case WHEELUPMOUSE:  /* change number of cuts */
462                         if (event->val == KM_RELEASE)
463                                 break;
464
465                         cuts++;
466                         RNA_int_set(op->ptr,"number_cuts",cuts);
467                         ringsel_find_edge(lcd, cuts);
468                         
469                         ED_region_tag_redraw(lcd->ar);
470                         break;
471                 case PAGEDOWNKEY:
472                 case WHEELDOWNMOUSE:  /* change number of cuts */
473                         if (event->val == KM_RELEASE)
474                                 break;
475
476                         cuts=MAX2(cuts-1,1);
477                         RNA_int_set(op->ptr,"number_cuts",cuts);
478                         ringsel_find_edge(lcd, cuts);
479                         
480                         ED_region_tag_redraw(lcd->ar);
481                         break;
482                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
483                         int dist = 75;
484                         BMEdge *edge;
485
486                         lcd->vc.mval[0] = event->mval[0];
487                         lcd->vc.mval[1] = event->mval[1];
488                         edge = EDBM_findnearestedge(&lcd->vc, &dist);
489
490                         if (edge != lcd->eed) {
491                                 lcd->eed = edge;
492                                 ringsel_find_edge(lcd, cuts);
493                         }
494
495                         ED_region_tag_redraw(lcd->ar);
496                         break;
497                 }                       
498         }
499         
500         /* keep going until the user confirms */
501         return OPERATOR_RUNNING_MODAL;
502 }
503
504 void MESH_OT_loopcut (wmOperatorType *ot)
505 {
506         /* description */
507         ot->name= "Loop Cut";
508         ot->idname= "MESH_OT_loopcut";
509         ot->description= "Add a new loop between existing loops.";
510         
511         /* callbacks */
512         ot->invoke= ringcut_invoke;
513         ot->modal= loopcut_modal;
514         ot->cancel= ringcut_cancel;
515         ot->poll= ED_operator_editmesh_region_view3d;
516         
517         /* flags */
518         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
519
520         /* properties */
521         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
522 }