Merge with trunk r41342
[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., 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/loopcut.c
28  *  \ingroup edmesh
29  */
30
31
32 #include <float.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <stdio.h>
36
37 #include "DNA_ID.h"
38 #include "DNA_object_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_userdef_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "MEM_guardedalloc.h"
45
46 #include "PIL_time.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_dynstr.h" /*for WM_operator_pystring */
50 #include "BLI_editVert.h"
51 #include "BLI_math.h"
52 #include "BLI_utildefines.h"
53
54 #include "BKE_blender.h"
55 #include "BKE_context.h"
56 #include "BKE_depsgraph.h"
57 #include "BKE_mesh.h"
58 #include "BKE_modifier.h"
59 #include "BKE_report.h"
60 #include "BKE_scene.h"
61 #include "BKE_array_mallocn.h"
62
63 #include "BIF_gl.h"
64 #include "BIF_glutil.h" /* for paint cursor */
65
66 #include "IMB_imbuf_types.h"
67
68 #include "ED_screen.h"
69 #include "ED_space_api.h"
70 #include "ED_view3d.h"
71 #include "ED_mesh.h"
72
73 #include "RNA_access.h"
74 #include "RNA_define.h"
75
76 #include "UI_interface.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80
81 #include "mesh_intern.h"
82
83 /* ringsel operator */
84
85 /* struct for properties used while drawing */
86 typedef struct tringselOpData {
87         ARegion *ar;            /* region that ringsel was activated in */
88         void *draw_handle;      /* for drawing preview loop */
89         
90         float (*edges)[2][3];
91         int totedge;
92
93         ViewContext vc;
94
95         Object *ob;
96         EditMesh *em;
97         EditEdge *eed;
98
99         int extend;
100         int do_cut;
101 } tringselOpData;
102
103 /* modal loop selection drawing callback */
104 static void ringsel_draw(const bContext *C, ARegion *UNUSED(ar), void *arg)
105 {
106         View3D *v3d = CTX_wm_view3d(C);
107         tringselOpData *lcd = arg;
108         int i;
109         
110         if (lcd->totedge > 0) {
111                 if(v3d && v3d->zbuf)
112                         glDisable(GL_DEPTH_TEST);
113
114                 glPushMatrix();
115                 glMultMatrixf(lcd->ob->obmat);
116
117                 glColor3ub(255, 0, 255);
118                 glBegin(GL_LINES);
119                 for (i=0; i<lcd->totedge; i++) {
120                         glVertex3fv(lcd->edges[i][0]);
121                         glVertex3fv(lcd->edges[i][1]);
122                 }
123                 glEnd();
124
125                 glPopMatrix();
126                 if(v3d && v3d->zbuf)
127                         glEnable(GL_DEPTH_TEST);
128         }
129 }
130
131 static void edgering_sel(tringselOpData *lcd, int previewlines, int select)
132 {
133         EditMesh *em = lcd->em;
134         EditEdge *startedge = lcd->eed;
135         EditEdge *eed;
136         EditFace *efa;
137         EditVert *v[2][2];
138         float (*edges)[2][3] = NULL;
139         V_DYNDECLARE(edges);
140         float co[2][3];
141         int looking=1, i, tot=0;
142         
143         if (!startedge)
144                 return;
145
146         if (lcd->edges) {
147                 MEM_freeN(lcd->edges);
148                 lcd->edges = NULL;
149                 lcd->totedge = 0;
150         }
151
152         if (!lcd->extend) {
153                 EM_clear_flag_all(lcd->em, SELECT);
154         }
155
156         /* in eed->f1 we put the valence (amount of faces in edge) */
157         /* in eed->f2 we put tagged flag as correct loop */
158         /* in efa->f1 we put tagged flag as correct to select */
159
160         for(eed= em->edges.first; eed; eed= eed->next) {
161                 eed->f1= 0;
162                 eed->f2= 0;
163         }
164
165         for(efa= em->faces.first; efa; efa= efa->next) {
166                 efa->f1= 0;
167                 if(efa->h==0) {
168                         efa->e1->f1++;
169                         efa->e2->f1++;
170                         efa->e3->f1++;
171                         if(efa->e4) efa->e4->f1++;
172                 }
173         }
174         
175         // tag startedge OK
176         startedge->f2= 1;
177         
178         while(looking) {
179                 looking= 0;
180                 
181                 for(efa= em->faces.first; efa; efa= efa->next) {
182                         if(efa->e4 && efa->f1==0 && efa->h == 0) {      // not done quad
183                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
184
185                                         // if edge tagged, select opposing edge and mark face ok
186                                         if(efa->e1->f2) {
187                                                 efa->e3->f2= 1;
188                                                 efa->f1= 1;
189                                                 looking= 1;
190                                         }
191                                         else if(efa->e2->f2) {
192                                                 efa->e4->f2= 1;
193                                                 efa->f1= 1;
194                                                 looking= 1;
195                                         }
196                                         if(efa->e3->f2) {
197                                                 efa->e1->f2= 1;
198                                                 efa->f1= 1;
199                                                 looking= 1;
200                                         }
201                                         if(efa->e4->f2) {
202                                                 efa->e2->f2= 1;
203                                                 efa->f1= 1;
204                                                 looking= 1;
205                                         }
206                                 }
207                         }
208                 }
209         }
210         
211         if(previewlines > 0 && !select){
212                         for(efa= em->faces.first; efa; efa= efa->next) {
213                                 if(efa->v4 == NULL) {  continue; }
214                                 if(efa->h == 0){
215                                         if(efa->e1->f2 == 1){
216                                                 if(efa->e1->h == 1 || efa->e3->h == 1 )
217                                                         continue;
218                                                 
219                                                 v[0][0] = efa->v1;
220                                                 v[0][1] = efa->v2;
221                                                 v[1][0] = efa->v4;
222                                                 v[1][1] = efa->v3;
223                                         } else if(efa->e2->f2 == 1){
224                                                 if(efa->e2->h == 1 || efa->e4->h == 1)
225                                                         continue;
226                                                 v[0][0] = efa->v2;
227                                                 v[0][1] = efa->v3;
228                                                 v[1][0] = efa->v1;
229                                                 v[1][1] = efa->v4;                                      
230                                         } else { continue; }
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                                                 V_GROW(edges);
242                                                 VECCOPY(edges[tot][0], co[0]);
243                                                 VECCOPY(edges[tot][1], co[1]);
244                                                 tot++;
245                                         }
246                                 }
247                         }
248         } else {
249                 select = (startedge->f & SELECT) == 0;
250
251                 /* select the edges */
252                 for(eed= em->edges.first; eed; eed= eed->next) {
253                         if(eed->f2) EM_select_edge(eed, select);
254                 }
255         }
256
257         lcd->edges = edges;
258         lcd->totedge = tot;
259 }
260
261 static void ringsel_find_edge(tringselOpData *lcd, int cuts)
262 {
263         if (lcd->eed) {
264                 edgering_sel(lcd, cuts, 0);
265         } else if(lcd->edges) {
266                 MEM_freeN(lcd->edges);
267                 lcd->edges = NULL;
268                 lcd->totedge = 0;
269         }
270 }
271
272 static void ringsel_finish(bContext *C, wmOperator *op)
273 {
274         tringselOpData *lcd= op->customdata;
275         int cuts= (lcd->do_cut)? RNA_int_get(op->ptr,"number_cuts"): 0;
276
277         if (lcd->eed) {
278                 EditMesh *em = BKE_mesh_get_editmesh(lcd->ob->data);
279                 
280                 edgering_sel(lcd, cuts, 1);
281                 
282                 if (lcd->do_cut) {
283
284                         esubdivideflag(lcd->ob, em, SELECT, 0.0f, 0.0f, 0, cuts, 0, SUBDIV_SELECT_LOOPCUT);
285
286                         /* force edge slide to edge select mode in in face select mode */
287                         if (em->selectmode & SCE_SELECT_FACE) {
288                                 if (em->selectmode == SCE_SELECT_FACE)
289                                         em->selectmode = SCE_SELECT_EDGE;
290                                 else
291                                         em->selectmode &= ~SCE_SELECT_FACE;
292                                 CTX_data_tool_settings(C)->selectmode= em->selectmode;
293                                 EM_selectmode_set(em);
294
295                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
296                         }
297                         
298                         DAG_id_tag_update(lcd->ob->data, 0);
299                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, lcd->ob->data);
300                 }
301                 else {
302                         
303                         /* sets as active, useful for other tools */
304                         if(em->selectmode & SCE_SELECT_VERTEX)
305                                 EM_store_selection(em, lcd->eed->v1, EDITVERT);
306                         if(em->selectmode & SCE_SELECT_EDGE)
307                                 EM_store_selection(em, lcd->eed, EDITEDGE);
308                         
309                         EM_selectmode_flush(lcd->em);
310                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, lcd->ob->data);
311                 }
312         }
313 }
314
315 /* called when modal loop selection is done... */
316 static void ringsel_exit(wmOperator *op)
317 {
318         tringselOpData *lcd= op->customdata;
319
320         /* deactivate the extra drawing stuff in 3D-View */
321         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
322         
323         if (lcd->edges)
324                 MEM_freeN(lcd->edges);
325
326         ED_region_tag_redraw(lcd->ar);
327
328         /* free the custom data */
329         MEM_freeN(lcd);
330         op->customdata= NULL;
331 }
332
333 /* called when modal loop selection gets set up... */
334 static int ringsel_init (bContext *C, wmOperator *op, int do_cut)
335 {
336         tringselOpData *lcd;
337         
338         /* alloc new customdata */
339         lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
340         
341         /* assign the drawing handle for drawing preview line... */
342         lcd->ar= CTX_wm_region(C);
343         lcd->draw_handle= ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
344         lcd->ob = CTX_data_edit_object(C);
345         lcd->em= BKE_mesh_get_editmesh((Mesh *)lcd->ob->data);
346         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
347         lcd->do_cut = do_cut;
348         em_setup_viewcontext(C, &lcd->vc);
349
350         ED_region_tag_redraw(lcd->ar);
351
352         return 1;
353 }
354
355 static int ringcut_cancel (bContext *UNUSED(C), wmOperator *op)
356 {
357         /* this is just a wrapper around exit() */
358         ringsel_exit(op);
359         return OPERATOR_CANCELLED;
360 }
361
362 static int ringsel_invoke (bContext *C, wmOperator *op, wmEvent *evt)
363 {
364         tringselOpData *lcd;
365         EditEdge *edge;
366         int dist = 75;
367         
368         view3d_operator_needs_opengl(C);
369
370         if (!ringsel_init(C, op, 0))
371                 return OPERATOR_CANCELLED;
372         
373         lcd = op->customdata;
374         
375         if (lcd->em->selectmode == SCE_SELECT_FACE) {
376                 ringsel_exit(op);
377                 WM_operator_name_call(C, "MESH_OT_loop_select", WM_OP_INVOKE_REGION_WIN, NULL);
378                 return OPERATOR_CANCELLED;
379         }
380
381         lcd->vc.mval[0] = evt->mval[0];
382         lcd->vc.mval[1] = evt->mval[1];
383         
384         edge = findnearestedge(&lcd->vc, &dist);
385         if(!edge) {
386                 ringsel_exit(op);
387                 return OPERATOR_CANCELLED;
388         }
389
390         lcd->eed = edge;
391         ringsel_find_edge(lcd, 1);
392
393         ringsel_finish(C, op);
394         ringsel_exit(op);
395
396         return OPERATOR_FINISHED;
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         EditEdge *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 = 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 ringcut_modal (bContext *C, wmOperator *op, wmEvent *event)
432 {
433         int cuts= RNA_int_get(op->ptr,"number_cuts");
434         tringselOpData *lcd= op->customdata;
435
436         view3d_operator_needs_opengl(C);
437
438
439         switch (event->type) {
440                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
441                         if (event->val == KM_PRESS) {
442                                 /* finish */
443                                 ED_region_tag_redraw(lcd->ar);
444                                 
445                                 ringsel_finish(C, op);
446                                 ringsel_exit(op);
447                                 ED_area_headerprint(CTX_wm_area(C), NULL);
448                                 
449                                 return OPERATOR_FINISHED;
450                         }
451                         
452                         ED_region_tag_redraw(lcd->ar);
453                         break;
454                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
455                 case ESCKEY:
456                         if (event->val == KM_RELEASE) {
457                                 /* cancel */
458                                 ED_region_tag_redraw(lcd->ar);
459                                 ED_area_headerprint(CTX_wm_area(C), NULL);
460                                 
461                                 return ringcut_cancel(C, op);
462                         }
463                         
464                         ED_region_tag_redraw(lcd->ar);
465                         break;
466                 case WHEELUPMOUSE:  /* change number of cuts */
467                 case PAGEUPKEY:
468                         if (event->val == KM_PRESS) {
469                                 cuts++;
470                                 RNA_int_set(op->ptr, "number_cuts",cuts);
471                                 ringsel_find_edge(lcd, cuts);
472                                 
473                                 ED_region_tag_redraw(lcd->ar);
474                         }
475                         break;
476                 case WHEELDOWNMOUSE:  /* change number of cuts */
477                 case PAGEDOWNKEY:
478                         if (event->val == KM_PRESS) {
479                                 cuts=MAX2(cuts-1,1);
480                                 RNA_int_set(op->ptr,"number_cuts",cuts);
481                                 ringsel_find_edge(lcd, cuts);
482                                 
483                                 ED_region_tag_redraw(lcd->ar);
484                         }
485                         break;
486                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
487                         int dist = 75;
488                         EditEdge *edge;
489
490                         lcd->vc.mval[0] = event->mval[0];
491                         lcd->vc.mval[1] = event->mval[1];
492                         edge = findnearestedge(&lcd->vc, &dist);
493
494                         if (edge != lcd->eed) {
495                                 lcd->eed = edge;
496                                 ringsel_find_edge(lcd, cuts);
497                         }
498
499                         ED_region_tag_redraw(lcd->ar);
500                         break;
501                 }                       
502         }
503         
504         /* keep going until the user confirms */
505         return OPERATOR_RUNNING_MODAL;
506 }
507
508 void MESH_OT_edgering_select (wmOperatorType *ot)
509 {
510         /* description */
511         ot->name= "Edge Ring Select";
512         ot->idname= "MESH_OT_edgering_select";
513         ot->description= "Select an edge ring";
514         
515         /* callbacks */
516         ot->invoke= ringsel_invoke;
517         ot->poll= ED_operator_editmesh_region_view3d; 
518         
519         /* flags */
520         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
521
522         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
523 }
524
525 void MESH_OT_loopcut (wmOperatorType *ot)
526 {
527         /* description */
528         ot->name= "Loop Cut";
529         ot->idname= "MESH_OT_loopcut";
530         ot->description= "Add a new loop between existing loops";
531         
532         /* callbacks */
533         ot->invoke= ringcut_invoke;
534         ot->modal= ringcut_modal;
535         ot->cancel= ringcut_cancel;
536         ot->poll= ED_operator_editmesh_region_view3d;
537         
538         /* flags */
539         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
540
541         /* properties */
542         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
543 }