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