Merging r39717 through r39983 from trunk into soc-2011-tomato
[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 } tringselOpData;
104
105 /* modal loop selection drawing callback */
106 static void ringsel_draw(const bContext *C, ARegion *UNUSED(ar), void *arg)
107 {
108         View3D *v3d = CTX_wm_view3d(C);
109         tringselOpData *lcd = arg;
110         int i;
111         
112         if (lcd->totedge > 0) {
113                 if(v3d && v3d->zbuf)
114                         glDisable(GL_DEPTH_TEST);
115
116                 glPushMatrix();
117                 glMultMatrixf(lcd->ob->obmat);
118
119                 glColor3ub(255, 0, 255);
120                 glBegin(GL_LINES);
121                 for (i=0; i<lcd->totedge; i++) {
122                         glVertex3fv(lcd->edges[i][0]);
123                         glVertex3fv(lcd->edges[i][1]);
124                 }
125                 glEnd();
126
127                 glPopMatrix();
128                 if(v3d && v3d->zbuf)
129                         glEnable(GL_DEPTH_TEST);
130         }
131 }
132
133 static void edgering_sel(tringselOpData *lcd, int previewlines, int select)
134 {
135         EditMesh *em = lcd->em;
136         EditEdge *startedge = lcd->eed;
137         EditEdge *eed;
138         EditFace *efa;
139         EditVert *v[2][2];
140         float (*edges)[2][3] = NULL;
141         V_DYNDECLARE(edges);
142         float co[2][3];
143         int looking=1, i, tot=0;
144         
145         if (!startedge)
146                 return;
147
148         if (lcd->edges) {
149                 MEM_freeN(lcd->edges);
150                 lcd->edges = NULL;
151                 lcd->totedge = 0;
152         }
153
154         if (!lcd->extend) {
155                 EM_clear_flag_all(lcd->em, SELECT);
156         }
157
158         /* in eed->f1 we put the valence (amount of faces in edge) */
159         /* in eed->f2 we put tagged flag as correct loop */
160         /* in efa->f1 we put tagged flag as correct to select */
161
162         for(eed= em->edges.first; eed; eed= eed->next) {
163                 eed->f1= 0;
164                 eed->f2= 0;
165         }
166
167         for(efa= em->faces.first; efa; efa= efa->next) {
168                 efa->f1= 0;
169                 if(efa->h==0) {
170                         efa->e1->f1++;
171                         efa->e2->f1++;
172                         efa->e3->f1++;
173                         if(efa->e4) efa->e4->f1++;
174                 }
175         }
176         
177         // tag startedge OK
178         startedge->f2= 1;
179         
180         while(looking) {
181                 looking= 0;
182                 
183                 for(efa= em->faces.first; efa; efa= efa->next) {
184                         if(efa->e4 && efa->f1==0 && efa->h == 0) {      // not done quad
185                                 if(efa->e1->f1<=2 && efa->e2->f1<=2 && efa->e3->f1<=2 && efa->e4->f1<=2) { // valence ok
186
187                                         // if edge tagged, select opposing edge and mark face ok
188                                         if(efa->e1->f2) {
189                                                 efa->e3->f2= 1;
190                                                 efa->f1= 1;
191                                                 looking= 1;
192                                         }
193                                         else if(efa->e2->f2) {
194                                                 efa->e4->f2= 1;
195                                                 efa->f1= 1;
196                                                 looking= 1;
197                                         }
198                                         if(efa->e3->f2) {
199                                                 efa->e1->f2= 1;
200                                                 efa->f1= 1;
201                                                 looking= 1;
202                                         }
203                                         if(efa->e4->f2) {
204                                                 efa->e2->f2= 1;
205                                                 efa->f1= 1;
206                                                 looking= 1;
207                                         }
208                                 }
209                         }
210                 }
211         }
212         
213         if(previewlines > 0 && !select){
214                         for(efa= em->faces.first; efa; efa= efa->next) {
215                                 if(efa->v4 == NULL) {  continue; }
216                                 if(efa->h == 0){
217                                         if(efa->e1->f2 == 1){
218                                                 if(efa->e1->h == 1 || efa->e3->h == 1 )
219                                                         continue;
220                                                 
221                                                 v[0][0] = efa->v1;
222                                                 v[0][1] = efa->v2;
223                                                 v[1][0] = efa->v4;
224                                                 v[1][1] = efa->v3;
225                                         } else if(efa->e2->f2 == 1){
226                                                 if(efa->e2->h == 1 || efa->e4->h == 1)
227                                                         continue;
228                                                 v[0][0] = efa->v2;
229                                                 v[0][1] = efa->v3;
230                                                 v[1][0] = efa->v1;
231                                                 v[1][1] = efa->v4;                                      
232                                         } else { continue; }
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                                                 V_GROW(edges);
244                                                 VECCOPY(edges[tot][0], co[0]);
245                                                 VECCOPY(edges[tot][1], co[1]);
246                                                 tot++;
247                                         }
248                                 }
249                         }
250         } else {
251                 select = (startedge->f & SELECT) == 0;
252
253                 /* select the edges */
254                 for(eed= em->edges.first; eed; eed= eed->next) {
255                         if(eed->f2) EM_select_edge(eed, select);
256                 }
257         }
258
259         lcd->edges = edges;
260         lcd->totedge = tot;
261 }
262
263 static void ringsel_find_edge(tringselOpData *lcd, int cuts)
264 {
265         if (lcd->eed) {
266                 edgering_sel(lcd, cuts, 0);
267         } else if(lcd->edges) {
268                 MEM_freeN(lcd->edges);
269                 lcd->edges = NULL;
270                 lcd->totedge = 0;
271         }
272 }
273
274 static void ringsel_finish(bContext *C, wmOperator *op)
275 {
276         tringselOpData *lcd= op->customdata;
277         int cuts= (lcd->do_cut)? RNA_int_get(op->ptr,"number_cuts"): 0;
278
279         if (lcd->eed) {
280                 EditMesh *em = BKE_mesh_get_editmesh(lcd->ob->data);
281                 
282                 edgering_sel(lcd, cuts, 1);
283                 
284                 if (lcd->do_cut) {
285
286                         esubdivideflag(lcd->ob, em, SELECT, 0.0f, 0.0f, 0, cuts, 0, SUBDIV_SELECT_LOOPCUT);
287
288                         /* force edge slide to edge select mode in in face select mode */
289                         if (em->selectmode & SCE_SELECT_FACE) {
290                                 if (em->selectmode == SCE_SELECT_FACE)
291                                         em->selectmode = SCE_SELECT_EDGE;
292                                 else
293                                         em->selectmode &= ~SCE_SELECT_FACE;
294                                 CTX_data_tool_settings(C)->selectmode= em->selectmode;
295                                 EM_selectmode_set(em);
296
297                                 WM_event_add_notifier(C, NC_SCENE|ND_TOOLSETTINGS, CTX_data_scene(C));
298                         }
299                         
300                         DAG_id_tag_update(lcd->ob->data, 0);
301                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, lcd->ob->data);
302                 }
303                 else {
304                         
305                         /* sets as active, useful for other tools */
306                         if(em->selectmode & SCE_SELECT_VERTEX)
307                                 EM_store_selection(em, lcd->eed->v1, EDITVERT);
308                         if(em->selectmode & SCE_SELECT_EDGE)
309                                 EM_store_selection(em, lcd->eed, EDITEDGE);
310                         
311                         EM_selectmode_flush(lcd->em);
312                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, lcd->ob->data);
313                 }
314         }
315 }
316
317 /* called when modal loop selection is done... */
318 static void ringsel_exit(wmOperator *op)
319 {
320         tringselOpData *lcd= op->customdata;
321
322         /* deactivate the extra drawing stuff in 3D-View */
323         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
324         
325         if (lcd->edges)
326                 MEM_freeN(lcd->edges);
327
328         ED_region_tag_redraw(lcd->ar);
329
330         /* free the custom data */
331         MEM_freeN(lcd);
332         op->customdata= NULL;
333 }
334
335 /* called when modal loop selection gets set up... */
336 static int ringsel_init (bContext *C, wmOperator *op, int do_cut)
337 {
338         tringselOpData *lcd;
339         
340         /* alloc new customdata */
341         lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
342         
343         /* assign the drawing handle for drawing preview line... */
344         lcd->ar= CTX_wm_region(C);
345         lcd->draw_handle= ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST_VIEW);
346         lcd->ob = CTX_data_edit_object(C);
347         lcd->em= BKE_mesh_get_editmesh((Mesh *)lcd->ob->data);
348         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
349         lcd->do_cut = do_cut;
350         em_setup_viewcontext(C, &lcd->vc);
351
352         ED_region_tag_redraw(lcd->ar);
353
354         return 1;
355 }
356
357 static int ringcut_cancel (bContext *UNUSED(C), wmOperator *op)
358 {
359         /* this is just a wrapper around exit() */
360         ringsel_exit(op);
361         return OPERATOR_CANCELLED;
362 }
363
364 static int ringsel_invoke (bContext *C, wmOperator *op, wmEvent *evt)
365 {
366         tringselOpData *lcd;
367         EditEdge *edge;
368         int dist = 75;
369         
370         view3d_operator_needs_opengl(C);
371
372         if (!ringsel_init(C, op, 0))
373                 return OPERATOR_CANCELLED;
374         
375         lcd = op->customdata;
376         
377         if (lcd->em->selectmode == SCE_SELECT_FACE) {
378                 ringsel_exit(op);
379                 WM_operator_name_call(C, "MESH_OT_loop_select", WM_OP_INVOKE_REGION_WIN, NULL);
380                 return OPERATOR_CANCELLED;
381         }
382
383         lcd->vc.mval[0] = evt->mval[0];
384         lcd->vc.mval[1] = evt->mval[1];
385         
386         edge = findnearestedge(&lcd->vc, &dist);
387         if(!edge) {
388                 ringsel_exit(op);
389                 return OPERATOR_CANCELLED;
390         }
391
392         lcd->eed = edge;
393         ringsel_find_edge(lcd, 1);
394
395         ringsel_finish(C, op);
396         ringsel_exit(op);
397
398         return OPERATOR_FINISHED;
399 }
400
401 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
402 {
403         Object *obedit= CTX_data_edit_object(C);
404         tringselOpData *lcd;
405         EditEdge *edge;
406         int dist = 75;
407
408         if(modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
409                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
410         
411         view3d_operator_needs_opengl(C);
412
413         if (!ringsel_init(C, op, 1))
414                 return OPERATOR_CANCELLED;
415         
416         /* add a modal handler for this operator - handles loop selection */
417         WM_event_add_modal_handler(C, op);
418
419         lcd = op->customdata;
420         lcd->vc.mval[0] = evt->mval[0];
421         lcd->vc.mval[1] = evt->mval[1];
422         
423         edge = findnearestedge(&lcd->vc, &dist);
424         if (edge != lcd->eed) {
425                 lcd->eed = edge;
426                 ringsel_find_edge(lcd, 1);
427         }
428         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
429         
430         return OPERATOR_RUNNING_MODAL;
431 }
432
433 static int ringcut_modal (bContext *C, wmOperator *op, wmEvent *event)
434 {
435         int cuts= RNA_int_get(op->ptr,"number_cuts");
436         tringselOpData *lcd= op->customdata;
437
438         view3d_operator_needs_opengl(C);
439
440
441         switch (event->type) {
442                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
443                         if (event->val == KM_PRESS) {
444                                 /* finish */
445                                 ED_region_tag_redraw(lcd->ar);
446                                 
447                                 ringsel_finish(C, op);
448                                 ringsel_exit(op);
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                 case ESCKEY:
458                         if (event->val == KM_RELEASE) {
459                                 /* cancel */
460                                 ED_region_tag_redraw(lcd->ar);
461                                 ED_area_headerprint(CTX_wm_area(C), NULL);
462                                 
463                                 return ringcut_cancel(C, op);
464                         }
465                         
466                         ED_region_tag_redraw(lcd->ar);
467                         break;
468                 case WHEELUPMOUSE:  /* change number of cuts */
469                 case PAGEUPKEY:
470                         if (event->val == KM_PRESS) {
471                                 cuts++;
472                                 RNA_int_set(op->ptr, "number_cuts",cuts);
473                                 ringsel_find_edge(lcd, cuts);
474                                 
475                                 ED_region_tag_redraw(lcd->ar);
476                         }
477                         break;
478                 case WHEELDOWNMOUSE:  /* change number of cuts */
479                 case PAGEDOWNKEY:
480                         if (event->val == KM_PRESS) {
481                                 cuts=MAX2(cuts-1,1);
482                                 RNA_int_set(op->ptr,"number_cuts",cuts);
483                                 ringsel_find_edge(lcd, cuts);
484                                 
485                                 ED_region_tag_redraw(lcd->ar);
486                         }
487                         break;
488                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
489                         int dist = 75;
490                         EditEdge *edge;
491
492                         lcd->vc.mval[0] = event->mval[0];
493                         lcd->vc.mval[1] = event->mval[1];
494                         edge = findnearestedge(&lcd->vc, &dist);
495
496                         if (edge != lcd->eed) {
497                                 lcd->eed = edge;
498                                 ringsel_find_edge(lcd, cuts);
499                         }
500
501                         ED_region_tag_redraw(lcd->ar);
502                         break;
503                 }                       
504         }
505         
506         /* keep going until the user confirms */
507         return OPERATOR_RUNNING_MODAL;
508 }
509
510 void MESH_OT_edgering_select (wmOperatorType *ot)
511 {
512         /* description */
513         ot->name= "Edge Ring Select";
514         ot->idname= "MESH_OT_edgering_select";
515         ot->description= "Select an edge ring";
516         
517         /* callbacks */
518         ot->invoke= ringsel_invoke;
519         ot->poll= ED_operator_editmesh_region_view3d; 
520         
521         /* flags */
522         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
523
524         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
525 }
526
527 void MESH_OT_loopcut (wmOperatorType *ot)
528 {
529         /* description */
530         ot->name= "Loop Cut";
531         ot->idname= "MESH_OT_loopcut";
532         ot->description= "Add a new loop between existing loops";
533         
534         /* callbacks */
535         ot->invoke= ringcut_invoke;
536         ot->modal= ringcut_modal;
537         ot->cancel= ringcut_cancel;
538         ot->poll= ED_operator_editmesh_region_view3d;
539         
540         /* flags */
541         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
542
543         /* properties */
544         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
545 }