=trunk=
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #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 #include "DNA_windowmanager_types.h"
45
46 #include "MEM_guardedalloc.h"
47
48 #include "PIL_time.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_dynstr.h" /*for WM_operator_pystring */
52 #include "BLI_editVert.h"
53 #include "BLI_math.h"
54 #include "BLI_utildefines.h"
55
56 #include "BKE_blender.h"
57 #include "BKE_context.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_mesh.h"
60 #include "BKE_modifier.h"
61 #include "BKE_report.h"
62 #include "BKE_scene.h"
63 #include "BKE_array_mallocn.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
75 #include "RNA_access.h"
76 #include "RNA_define.h"
77
78 #include "UI_interface.h"
79
80 #include "WM_api.h"
81 #include "WM_types.h"
82
83 #include "mesh_intern.h"
84
85 /* ringsel operator */
86
87 /* struct for properties used while drawing */
88 typedef struct tringselOpData {
89         ARegion *ar;            /* region that ringsel was activated in */
90         void *draw_handle;      /* for drawing preview loop */
91         
92         float (*edges)[2][3];
93         int totedge;
94
95         ViewContext vc;
96
97         Object *ob;
98         EditMesh *em;
99         EditEdge *eed;
100
101         int extend;
102         int do_cut;
103         wmTimer *timer;
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 static void edgering_sel(tringselOpData *lcd, int previewlines, int select)
135 {
136         EditMesh *em = lcd->em;
137         EditEdge *startedge = lcd->eed;
138         EditEdge *eed;
139         EditFace *efa;
140         EditVert *v[2][2];
141         float (*edges)[2][3] = NULL;
142         V_DYNDECLARE(edges);
143         float co[2][3];
144         int looking=1, i, tot=0;
145         
146         if (!startedge)
147                 return;
148
149         if (lcd->edges) {
150                 MEM_freeN(lcd->edges);
151                 lcd->edges = NULL;
152                 lcd->totedge = 0;
153         }
154
155         if (!lcd->extend) {
156                 EM_clear_flag_all(lcd->em, SELECT);
157         }
158
159         /* in eed->f1 we put the valence (amount of faces in edge) */
160         /* in eed->f2 we put tagged flag as correct loop */
161         /* in efa->f1 we put tagged flag as correct to select */
162
163         for(eed= em->edges.first; eed; eed= eed->next) {
164                 eed->f1= 0;
165                 eed->f2= 0;
166         }
167
168         for(efa= em->faces.first; efa; efa= efa->next) {
169                 efa->f1= 0;
170                 if(efa->h==0) {
171                         efa->e1->f1++;
172                         efa->e2->f1++;
173                         efa->e3->f1++;
174                         if(efa->e4) efa->e4->f1++;
175                 }
176         }
177         
178         // tag startedge OK
179         startedge->f2= 1;
180         
181         while(looking) {
182                 looking= 0;
183                 
184                 for(efa= em->faces.first; efa; efa= efa->next) {
185                         if(efa->e4 && efa->f1==0 && efa->h == 0) {      // not done quad
186                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
187
188                                         // if edge tagged, select opposing edge and mark face ok
189                                         if(efa->e1->f2) {
190                                                 efa->e3->f2= 1;
191                                                 efa->f1= 1;
192                                                 looking= 1;
193                                         }
194                                         else if(efa->e2->f2) {
195                                                 efa->e4->f2= 1;
196                                                 efa->f1= 1;
197                                                 looking= 1;
198                                         }
199                                         if(efa->e3->f2) {
200                                                 efa->e1->f2= 1;
201                                                 efa->f1= 1;
202                                                 looking= 1;
203                                         }
204                                         if(efa->e4->f2) {
205                                                 efa->e2->f2= 1;
206                                                 efa->f1= 1;
207                                                 looking= 1;
208                                         }
209                                 }
210                         }
211                 }
212         }
213         
214         if(previewlines > 0 && !select){
215                         for(efa= em->faces.first; efa; efa= efa->next) {
216                                 if(efa->v4 == NULL) {  continue; }
217                                 if(efa->h == 0){
218                                         if(efa->e1->f2 == 1){
219                                                 if(efa->e1->h == 1 || efa->e3->h == 1 )
220                                                         continue;
221                                                 
222                                                 v[0][0] = efa->v1;
223                                                 v[0][1] = efa->v2;
224                                                 v[1][0] = efa->v4;
225                                                 v[1][1] = efa->v3;
226                                         } else if(efa->e2->f2 == 1){
227                                                 if(efa->e2->h == 1 || efa->e4->h == 1)
228                                                         continue;
229                                                 v[0][0] = efa->v2;
230                                                 v[0][1] = efa->v3;
231                                                 v[1][0] = efa->v1;
232                                                 v[1][1] = efa->v4;                                      
233                                         } else { continue; }
234                                                                                   
235                                         for(i=1;i<=previewlines;i++){
236                                                 co[0][0] = (v[0][1]->co[0] - v[0][0]->co[0])*(i/((float)previewlines+1))+v[0][0]->co[0];
237                                                 co[0][1] = (v[0][1]->co[1] - v[0][0]->co[1])*(i/((float)previewlines+1))+v[0][0]->co[1];
238                                                 co[0][2] = (v[0][1]->co[2] - v[0][0]->co[2])*(i/((float)previewlines+1))+v[0][0]->co[2];
239
240                                                 co[1][0] = (v[1][1]->co[0] - v[1][0]->co[0])*(i/((float)previewlines+1))+v[1][0]->co[0];
241                                                 co[1][1] = (v[1][1]->co[1] - v[1][0]->co[1])*(i/((float)previewlines+1))+v[1][0]->co[1];
242                                                 co[1][2] = (v[1][1]->co[2] - v[1][0]->co[2])*(i/((float)previewlines+1))+v[1][0]->co[2];                                        
243                                                 
244                                                 V_GROW(edges);
245                                                 VECCOPY(edges[tot][0], co[0]);
246                                                 VECCOPY(edges[tot][1], co[1]);
247                                                 tot++;
248                                         }
249                                 }
250                         }
251         } else {
252                 select = (startedge->f & SELECT) == 0;
253
254                 /* select the edges */
255                 for(eed= em->edges.first; eed; eed= eed->next) {
256                         if(eed->f2) EM_select_edge(eed, select);
257                 }
258         }
259
260         lcd->edges = edges;
261         lcd->totedge = tot;
262 }
263
264 static void ringsel_find_edge(tringselOpData *lcd, int cuts)
265 {
266         if (lcd->eed) {
267                 edgering_sel(lcd, cuts, 0);
268         } else if(lcd->edges) {
269                 MEM_freeN(lcd->edges);
270                 lcd->edges = NULL;
271                 lcd->totedge = 0;
272         }
273 }
274
275 static void ringsel_finish(bContext *C, wmOperator *op)
276 {
277         tringselOpData *lcd= op->customdata;
278         int cuts= (lcd->do_cut)? RNA_int_get(op->ptr,"number_cuts"): 0;
279
280         if (lcd->eed) {
281                 EditMesh *em = BKE_mesh_get_editmesh(lcd->ob->data);
282                 
283                 edgering_sel(lcd, cuts, 1);
284                 
285                 if (lcd->do_cut) {
286
287                         esubdivideflag(lcd->ob, em, SELECT, 0.0f, 0.0f, 0, cuts, 0, SUBDIV_SELECT_LOOPCUT);
288
289                         /* force edge slide to edge select mode in in face select mode */
290                         if (em->selectmode & SCE_SELECT_FACE) {
291                                 if (em->selectmode == SCE_SELECT_FACE)
292                                         em->selectmode = SCE_SELECT_EDGE;
293                                 else
294                                         em->selectmode &= ~SCE_SELECT_FACE;
295                                 CTX_data_tool_settings(C)->selectmode= em->selectmode;
296                                 EM_selectmode_set(em);
297
298                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
299                         }
300                         
301                         DAG_id_tag_update(lcd->ob->data, 0);
302                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, lcd->ob->data);
303                 }
304                 else {
305                         
306                         /* sets as active, useful for other tools */
307                         if(em->selectmode & SCE_SELECT_VERTEX)
308                                 EM_store_selection(em, lcd->eed->v1, EDITVERT);
309                         if(em->selectmode & SCE_SELECT_EDGE)
310                                 EM_store_selection(em, lcd->eed, EDITEDGE);
311                         
312                         EM_selectmode_flush(lcd->em);
313                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, lcd->ob->data);
314                 }
315         }
316 }
317
318 /* called when modal loop selection is done... */
319 static void ringsel_exit(bContext *C, wmOperator *op)
320 {
321         tringselOpData *lcd= op->customdata;
322
323         if (lcd->timer)
324                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), lcd->timer);
325
326         /* deactivate the extra drawing stuff in 3D-View */
327         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
328         
329         if (lcd->edges)
330                 MEM_freeN(lcd->edges);
331
332         ED_region_tag_redraw(lcd->ar);
333
334         /* free the custom data */
335         MEM_freeN(lcd);
336         op->customdata= NULL;
337 }
338
339 /* called when modal loop selection gets set up... */
340 static int ringsel_init (bContext *C, wmOperator *op, int do_cut)
341 {
342         tringselOpData *lcd;
343         
344         /* alloc new customdata */
345         lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
346         
347         /* assign the drawing handle for drawing preview line... */
348         lcd->ar= CTX_wm_region(C);
349         lcd->draw_handle= ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
350         lcd->ob = CTX_data_edit_object(C);
351         lcd->em= BKE_mesh_get_editmesh((Mesh *)lcd->ob->data);
352         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
353         lcd->do_cut = do_cut;
354         em_setup_viewcontext(C, &lcd->vc);
355
356         ED_region_tag_redraw(lcd->ar);
357
358         return 1;
359 }
360
361 static int ringcut_cancel (bContext *C, wmOperator *op)
362 {
363         /* this is just a wrapper around exit() */
364         ringsel_exit(C, op);
365         return OPERATOR_CANCELLED;
366 }
367
368 static int ringsel_invoke (bContext *C, wmOperator *op, wmEvent *evt)
369 {
370         tringselOpData *lcd;
371         EditEdge *edge;
372         int dist = 75;
373         
374         view3d_operator_needs_opengl(C);
375
376         if (!ringsel_init(C, op, 0))
377                 return OPERATOR_CANCELLED;
378         
379         lcd = op->customdata;
380         
381         if (lcd->em->selectmode == SCE_SELECT_FACE) {
382                 ringsel_exit(C, op);
383                 WM_operator_name_call(C, "MESH_OT_loop_select", WM_OP_INVOKE_REGION_WIN, NULL);
384                 return OPERATOR_CANCELLED;
385         }
386
387         lcd->vc.mval[0] = evt->mval[0];
388         lcd->vc.mval[1] = evt->mval[1];
389         
390         edge = findnearestedge(&lcd->vc, &dist);
391         if(!edge) {
392                 ringsel_exit(C, op);
393                 return OPERATOR_CANCELLED;
394         }
395
396         lcd->eed = edge;
397         ringsel_find_edge(lcd, 1);
398
399         ringsel_finish(C, op);
400         ringsel_exit(C, op);
401
402         return OPERATOR_FINISHED;
403 }
404
405 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
406 {
407         Object *obedit= CTX_data_edit_object(C);
408         tringselOpData *lcd;
409         EditEdge *edge;
410         int dist = 75;
411         
412         /*if we're in the cut-n-slide macro, set release_confirm based on user pref*/
413         if (op->opm)
414                 RNA_boolean_set(op->next->ptr, "release_confirm", U.loopcut_finish_on_release);
415         
416         if(modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
417                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
418         
419         view3d_operator_needs_opengl(C);
420
421         if (!ringsel_init(C, op, 1))
422                 return OPERATOR_CANCELLED;
423         
424         /* add a modal handler for this operator - handles loop selection */
425         WM_event_add_modal_handler(C, op);
426
427         lcd = op->customdata;
428         lcd->vc.mval[0] = evt->mval[0];
429         lcd->vc.mval[1] = evt->mval[1];
430         
431         edge = findnearestedge(&lcd->vc, &dist);
432         if (edge != lcd->eed) {
433                 lcd->eed = edge;
434                 ringsel_find_edge(lcd, 1);
435         }
436         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
437         
438         return OPERATOR_RUNNING_MODAL;
439 }
440
441 static int ringcut_modal (bContext *C, wmOperator *op, wmEvent *event)
442 {
443         int cuts= RNA_int_get(op->ptr,"number_cuts");
444         tringselOpData *lcd= op->customdata;
445
446         view3d_operator_needs_opengl(C);
447
448         switch (event->type) {
449                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
450                         if (event->val == KM_RELEASE) {
451                                 /* finish */
452                                 ED_region_tag_redraw(lcd->ar);
453                                 
454                                 ringsel_finish(C, op);
455                                 ringsel_exit(C, op);
456                                 ED_area_headerprint(CTX_wm_area(C), NULL);
457                                 
458                                 return OPERATOR_FINISHED|OPERATOR_ABORT_MACRO;
459                         }else {
460                                 lcd->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER2, 0.12);
461                         }
462                         
463                         ED_region_tag_redraw(lcd->ar);
464                         break;
465                         
466                 case TIMER2: 
467                         /* finish */
468                         ED_region_tag_redraw(lcd->ar);
469                         
470                         ringsel_finish(C, op);
471                         ringsel_exit(C, op);
472                         
473                         ED_area_headerprint(CTX_wm_area(C), NULL);
474
475                         return OPERATOR_FINISHED;
476                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
477                 case ESCKEY:
478                         if (event->val == KM_RELEASE) {
479                                 /* cancel */
480                                 ED_region_tag_redraw(lcd->ar);
481                                 ED_area_headerprint(CTX_wm_area(C), NULL);
482                                 
483                                 return ringcut_cancel(C, op);
484                         }
485                         
486                         ED_region_tag_redraw(lcd->ar);
487                         break;
488                 case WHEELUPMOUSE:  /* change number of cuts */
489                 case PAGEUPKEY:
490                         if (event->val == KM_PRESS) {
491                                 cuts++;
492                                 RNA_int_set(op->ptr, "number_cuts",cuts);
493                                 ringsel_find_edge(lcd, cuts);
494                                 
495                                 ED_region_tag_redraw(lcd->ar);
496                         }
497                         break;
498                 case WHEELDOWNMOUSE:  /* change number of cuts */
499                 case PAGEDOWNKEY:
500                         if (event->val == KM_PRESS) {
501                                 cuts=MAX2(cuts-1,1);
502                                 RNA_int_set(op->ptr,"number_cuts",cuts);
503                                 ringsel_find_edge(lcd, cuts);
504                                 
505                                 ED_region_tag_redraw(lcd->ar);
506                         }
507                         break;
508                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
509                         int dist = 75;
510                         EditEdge *edge;
511
512                         lcd->vc.mval[0] = event->mval[0];
513                         lcd->vc.mval[1] = event->mval[1];
514                         edge = findnearestedge(&lcd->vc, &dist);
515
516                         if (edge != lcd->eed) {
517                                 lcd->eed = edge;
518                                 ringsel_find_edge(lcd, cuts);
519                         }
520
521                         ED_region_tag_redraw(lcd->ar);
522                         break;
523                 }                       
524         }
525         
526         /* keep going until the user confirms */
527         return OPERATOR_RUNNING_MODAL;
528 }
529
530 void MESH_OT_edgering_select (wmOperatorType *ot)
531 {
532         /* description */
533         ot->name= "Edge Ring Select";
534         ot->idname= "MESH_OT_edgering_select";
535         ot->description= "Select an edge ring";
536         
537         /* callbacks */
538         ot->invoke= ringsel_invoke;
539         ot->poll= ED_operator_editmesh_region_view3d; 
540         
541         /* flags */
542         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
543
544         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
545 }
546
547 void MESH_OT_loopcut (wmOperatorType *ot)
548 {
549         /* description */
550         ot->name= "Loop Cut";
551         ot->idname= "MESH_OT_loopcut";
552         ot->description= "Add a new loop between existing loops";
553         
554         /* callbacks */
555         ot->invoke= ringcut_invoke;
556         ot->modal= ringcut_modal;
557         ot->cancel= ringcut_cancel;
558         ot->poll= ED_operator_editmesh_region_view3d;
559         
560         /* flags */
561         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
562
563         /* properties */
564         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
565 }