Merging r46469 through r46494 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / mesh / editmesh_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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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/editmesh_loopcut.c
28  *  \ingroup edmesh
29  */
30
31 #include <float.h>
32 #define _USE_MATH_DEFINES
33 #include <math.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <stdio.h>
37
38 #include "DNA_ID.h"
39 #include "DNA_object_types.h"
40 #include "DNA_mesh_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_math.h"
52 #include "BLI_dynstr.h" /*for WM_operator_pystring */
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(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_ELEM (l, &liter, l, BM_LOOPS_OF_LOOP) {
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_face_other_edge_loop(l->f, l->e, 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         }
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_flag_disable_all(lcd->em, BM_ELEM_SELECT);
204         }
205
206         if (select) {
207                 BMW_init(&walker, em->bm, BMW_EDGERING,
208                          BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
209                          BMW_FLAG_TEST_HIDDEN,
210                          BMW_NIL_LAY);
211
212                 eed = BMW_begin(&walker, startedge);
213                 for ( ; eed; eed = BMW_step(&walker)) {
214                         BM_edge_select_set(em->bm, eed, TRUE);
215                 }
216                 BMW_end(&walker);
217
218                 return;
219         }
220
221         BMW_init(&walker, em->bm, BMW_EDGERING,
222                  BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
223                  BMW_FLAG_TEST_HIDDEN,
224                  BMW_NIL_LAY);
225
226         eed = startedge = BMW_begin(&walker, startedge);
227         lastv1 = NULL;
228         for (lasteed = NULL; eed; eed = BMW_step(&walker)) {
229                 if (lasteed) {
230                         if (lastv1) {
231                                 v[1][0] = v[0][0];
232                                 v[1][1] = v[0][1];
233                         }
234                         else {
235                                 v[1][0] = lasteed->v1;
236                                 v[1][1] = lasteed->v2;
237                                 lastv1 = lasteed->v1;
238                         }
239
240                         edgering_find_order(lasteed, eed, lastv1, v);
241                         lastv1 = v[0][0];
242
243                         BLI_array_grow_items(edges, previewlines);
244
245                         for (i = 1; i <= previewlines; i++) {
246                                 co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0]) * (i / ((float)previewlines + 1)) + v[0][0]->co[0];
247                                 co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1]) * (i / ((float)previewlines + 1)) + v[0][0]->co[1];
248                                 co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2]) * (i / ((float)previewlines + 1)) + v[0][0]->co[2];
249
250                                 co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0]) * (i / ((float)previewlines + 1)) + v[1][0]->co[0];
251                                 co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1]) * (i / ((float)previewlines + 1)) + v[1][0]->co[1];
252                                 co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2]) * (i / ((float)previewlines + 1)) + v[1][0]->co[2];
253
254                                 copy_v3_v3(edges[tot][0], co[0]);
255                                 copy_v3_v3(edges[tot][1], co[1]);
256                                 tot++;
257                         }
258                 }
259                 lasteed = eed;
260         }
261         
262         if (lasteed != startedge && BM_edge_share_face_count(lasteed, startedge)) {
263                 v[1][0] = v[0][0];
264                 v[1][1] = v[0][1];
265
266                 edgering_find_order(lasteed, startedge, lastv1, v);
267                 
268                 BLI_array_grow_items(edges, previewlines);
269
270                 for (i = 1; i <= previewlines; i++) {
271                         if (!v[0][0] || !v[0][1] || !v[1][0] || !v[1][1])
272                                 continue;
273                         
274                         co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0]) * (i / ((float)previewlines + 1)) + v[0][0]->co[0];
275                         co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1]) * (i / ((float)previewlines + 1)) + v[0][0]->co[1];
276                         co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2]) * (i / ((float)previewlines + 1)) + v[0][0]->co[2];
277
278                         co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0]) * (i / ((float)previewlines + 1)) + v[1][0]->co[0];
279                         co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1]) * (i / ((float)previewlines + 1)) + v[1][0]->co[1];
280                         co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2]) * (i / ((float)previewlines + 1)) + v[1][0]->co[2];
281                         
282                         copy_v3_v3(edges[tot][0], co[0]);
283                         copy_v3_v3(edges[tot][1], co[1]);
284                         tot++;
285                 }
286         }
287
288         BMW_end(&walker);
289         lcd->edges = edges;
290         lcd->totedge = tot;
291 }
292
293 static void ringsel_find_edge(tringselOpData *lcd, int cuts)
294 {
295         if (lcd->eed) {
296                 edgering_sel(lcd, cuts, 0);
297         }
298         else if (lcd->edges) {
299                 MEM_freeN(lcd->edges);
300                 lcd->edges = NULL;
301                 lcd->totedge = 0;
302         }
303 }
304
305 static void ringsel_finish(bContext *C, wmOperator *op)
306 {
307         tringselOpData *lcd = op->customdata;
308         int cuts = RNA_int_get(op->ptr, "number_cuts");
309
310         if (lcd->eed) {
311                 BMEditMesh *em = lcd->em;
312
313                 edgering_sel(lcd, cuts, 1);
314                 
315                 if (lcd->do_cut) {
316                         BM_mesh_esubdivide(em->bm, BM_ELEM_SELECT,
317                                            0.0f, 0.0f, 0.0f,
318                                            cuts,
319                                            SUBDIV_SELECT_LOOPCUT, SUBD_PATH, 0, FALSE, 0);
320
321                         /* force edge slide to edge select mode in in face select mode */
322                         if (em->selectmode & SCE_SELECT_FACE) {
323                                 if (em->selectmode == SCE_SELECT_FACE)
324                                         em->selectmode = SCE_SELECT_EDGE;
325                                 else
326                                         em->selectmode &= ~SCE_SELECT_FACE;
327                                 CTX_data_tool_settings(C)->selectmode = em->selectmode;
328                                 EDBM_selectmode_set(em);
329
330                                 WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, CTX_data_scene(C));
331                         }
332
333                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT | ND_DATA, lcd->ob->data);
334                         DAG_id_tag_update(lcd->ob->data, 0);
335                 }
336                 else {
337                         
338                         /* sets as active, useful for other tools */
339                         if (em->selectmode & SCE_SELECT_VERTEX)
340                                 BM_select_history_store(em->bm, lcd->eed->v1);  /* low priority TODO, get vertrex close to mouse */
341                         if (em->selectmode & SCE_SELECT_EDGE)
342                                 BM_select_history_store(em->bm, lcd->eed);
343                         
344                         EDBM_selectmode_flush(lcd->em);
345                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, lcd->ob->data);
346                 }
347         }
348 }
349
350 /* called when modal loop selection is done... */
351 static void ringsel_exit(bContext *UNUSED(C), wmOperator *op)
352 {
353         tringselOpData *lcd = op->customdata;
354
355         /* deactivate the extra drawing stuff in 3D-View */
356         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
357         
358         if (lcd->edges)
359                 MEM_freeN(lcd->edges);
360
361         ED_region_tag_redraw(lcd->ar);
362
363         /* free the custom data */
364         MEM_freeN(lcd);
365         op->customdata = NULL;
366 }
367
368 /* called when modal loop selection gets set up... */
369 static int ringsel_init(bContext *C, wmOperator *op, int do_cut)
370 {
371         tringselOpData *lcd;
372         
373         /* alloc new customdata */
374         lcd = op->customdata = MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
375         
376         /* assign the drawing handle for drawing preview line... */
377         lcd->ar = CTX_wm_region(C);
378         lcd->draw_handle = ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
379         lcd->ob = CTX_data_edit_object(C);
380         lcd->em = BMEdit_FromObject(lcd->ob);
381         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
382         lcd->do_cut = do_cut;
383         
384         initNumInput(&lcd->num);
385         lcd->num.idx_max = 0;
386         lcd->num.flag |= NUM_NO_NEGATIVE | NUM_NO_FRACTION;
387         
388         em_setup_viewcontext(C, &lcd->vc);
389
390         ED_region_tag_redraw(lcd->ar);
391
392         return 1;
393 }
394
395 static int ringcut_cancel(bContext *C, wmOperator *op)
396 {
397         /* this is just a wrapper around exit() */
398         ringsel_exit(C, op);
399         return OPERATOR_CANCELLED;
400 }
401
402 static int ringcut_invoke(bContext *C, wmOperator *op, wmEvent *evt)
403 {
404         Object *obedit = CTX_data_edit_object(C);
405         tringselOpData *lcd;
406         BMEdge *edge;
407         int dist = 75;
408
409         if (modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
410                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
411         
412         view3d_operator_needs_opengl(C);
413
414         if (!ringsel_init(C, op, 1))
415                 return OPERATOR_CANCELLED;
416         
417         /* add a modal handler for this operator - handles loop selection */
418         WM_event_add_modal_handler(C, op);
419
420         lcd = op->customdata;
421         lcd->vc.mval[0] = evt->mval[0];
422         lcd->vc.mval[1] = evt->mval[1];
423         
424         edge = EDBM_edge_find_nearest(&lcd->vc, &dist);
425         if (edge != lcd->eed) {
426                 lcd->eed = edge;
427                 ringsel_find_edge(lcd, 1);
428         }
429         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
430         
431         return OPERATOR_RUNNING_MODAL;
432 }
433
434 static int loopcut_modal(bContext *C, wmOperator *op, wmEvent *event)
435 {
436         int cuts = RNA_int_get(op->ptr, "number_cuts");
437         tringselOpData *lcd = op->customdata;
438         int show_cuts = 0;
439
440         view3d_operator_needs_opengl(C);
441
442         switch (event->type) {
443                 case RETKEY:
444                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
445                         if (event->val == KM_PRESS) {
446                                 /* finish */
447                                 ED_region_tag_redraw(lcd->ar);
448                                 
449                                 ringsel_finish(C, op);
450                                 ringsel_exit(C, op);
451                                 
452                                 ED_area_headerprint(CTX_wm_area(C), NULL);
453                                 
454                                 return OPERATOR_FINISHED;
455                         }
456                         
457                         ED_region_tag_redraw(lcd->ar);
458                         break;
459                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
460                         ED_region_tag_redraw(lcd->ar);
461                         ringsel_exit(C, op);
462                         ED_area_headerprint(CTX_wm_area(C), NULL);
463
464                         return OPERATOR_FINISHED;
465                 case ESCKEY:
466                         if (event->val == KM_RELEASE) {
467                                 /* cancel */
468                                 ED_region_tag_redraw(lcd->ar);
469                                 ED_area_headerprint(CTX_wm_area(C), NULL);
470                                 
471                                 return ringcut_cancel(C, op);
472                         }
473                         
474                         ED_region_tag_redraw(lcd->ar);
475                         break;
476                 case PADPLUSKEY:
477                 case PAGEUPKEY:
478                 case WHEELUPMOUSE:  /* change number of cuts */
479                         if (event->val == KM_RELEASE)
480                                 break;
481
482                         cuts++;
483                         RNA_int_set(op->ptr, "number_cuts", cuts);
484                         ringsel_find_edge(lcd, cuts);
485                         show_cuts = TRUE;
486                         
487                         ED_region_tag_redraw(lcd->ar);
488                         break;
489                 case PADMINUS:
490                 case PAGEDOWNKEY:
491                 case WHEELDOWNMOUSE:  /* change number of cuts */
492                         if (event->val == KM_RELEASE)
493                                 break;
494
495                         cuts = MAX2(cuts - 1, 0);
496                         RNA_int_set(op->ptr, "number_cuts", cuts);
497                         ringsel_find_edge(lcd, cuts);
498                         show_cuts = TRUE;
499                         
500                         ED_region_tag_redraw(lcd->ar);
501                         break;
502                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
503                         int dist = 75;
504                         BMEdge *edge;
505
506                         lcd->vc.mval[0] = event->mval[0];
507                         lcd->vc.mval[1] = event->mval[1];
508                         edge = EDBM_edge_find_nearest(&lcd->vc, &dist);
509
510                         if (edge != lcd->eed) {
511                                 lcd->eed = edge;
512                                 ringsel_find_edge(lcd, cuts);
513                         }
514
515                         ED_region_tag_redraw(lcd->ar);
516                         break;
517                 }                       
518         }
519         
520         /* using the keyboard to input the number of cuts */
521         if (event->val == KM_PRESS) {
522                 /* init as zero so backspace clears */
523                 float value = 0.0f;
524                 
525                 if (handleNumInput(&lcd->num, event)) {
526                         applyNumInput(&lcd->num, &value);
527                         
528                         /* allow zero so you can backspace and type in a value
529                          * otherwise 1 as minimum would make more sense */
530                         cuts = CLAMPIS(value, 0, 130);
531                         
532                         RNA_int_set(op->ptr, "number_cuts", cuts);
533                         ringsel_find_edge(lcd, cuts);
534                         show_cuts = TRUE;
535                         
536                         ED_region_tag_redraw(lcd->ar);
537                 }
538         }
539         
540         if (show_cuts) {
541                 char buf[64];
542                 BLI_snprintf(buf, sizeof(buf), "Number of Cuts: %d", cuts);
543                 ED_area_headerprint(CTX_wm_area(C), buf);
544         }
545         
546         /* keep going until the user confirms */
547         return OPERATOR_RUNNING_MODAL;
548 }
549
550 /* for bmesh this tool is in bmesh_select.c */
551 #if 0
552
553 void MESH_OT_edgering_select(wmOperatorType *ot)
554 {
555         /* description */
556         ot->name = "Edge Ring Select";
557         ot->idname = "MESH_OT_edgering_select";
558         ot->description = "Select an edge ring";
559         
560         /* callbacks */
561         ot->invoke = ringsel_invoke;
562         ot->poll = ED_operator_editmesh_region_view3d; 
563         
564         /* flags */
565         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
566
567         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
568 }
569
570 #endif
571
572 void MESH_OT_loopcut(wmOperatorType *ot)
573 {
574         PropertyRNA *prop;
575
576         /* description */
577         ot->name = "Loop Cut";
578         ot->idname = "MESH_OT_loopcut";
579         ot->description = "Add a new loop between existing loops";
580         
581         /* callbacks */
582         ot->invoke = ringcut_invoke;
583         ot->modal = loopcut_modal;
584         ot->cancel = ringcut_cancel;
585         ot->poll = ED_operator_editmesh_region_view3d;
586         
587         /* flags */
588         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING;
589
590         /* properties */
591         prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
592         /* avoid re-using last var because it can cause _very_ high poly meshes and annoy users (or worse crash) */
593         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
594 }