- use cache_ as a prefix for new modifiers.
[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 #include "ED_numinput.h"
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76
77 #include "UI_interface.h"
78
79 #include "WM_api.h"
80 #include "WM_types.h"
81
82 #include "mesh_intern.h"
83
84 /* ringsel operator */
85
86 /* struct for properties used while drawing */
87 typedef struct tringselOpData {
88         ARegion *ar;            /* region that ringsel was activated in */
89         void *draw_handle;      /* for drawing preview loop */
90         
91         float (*edges)[2][3];
92         int totedge;
93
94         ViewContext vc;
95
96         Object *ob;
97         EditMesh *em;
98         EditEdge *eed;
99         NumInput num;
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         
351         initNumInput(&lcd->num);
352         lcd->num.idx_max = 0;
353         lcd->num.flag |= NUM_NO_NEGATIVE | NUM_NO_FRACTION;
354         
355         em_setup_viewcontext(C, &lcd->vc);
356
357         ED_region_tag_redraw(lcd->ar);
358
359         return 1;
360 }
361
362 static int ringcut_cancel (bContext *UNUSED(C), wmOperator *op)
363 {
364         /* this is just a wrapper around exit() */
365         ringsel_exit(op);
366         return OPERATOR_CANCELLED;
367 }
368
369 static int ringsel_invoke (bContext *C, wmOperator *op, wmEvent *evt)
370 {
371         tringselOpData *lcd;
372         EditEdge *edge;
373         int dist = 75;
374         
375         view3d_operator_needs_opengl(C);
376
377         if (!ringsel_init(C, op, 0))
378                 return OPERATOR_CANCELLED;
379         
380         lcd = op->customdata;
381         
382         if (lcd->em->selectmode == SCE_SELECT_FACE) {
383                 ringsel_exit(op);
384                 WM_operator_name_call(C, "MESH_OT_loop_select", WM_OP_INVOKE_REGION_WIN, NULL);
385                 return OPERATOR_CANCELLED;
386         }
387
388         lcd->vc.mval[0] = evt->mval[0];
389         lcd->vc.mval[1] = evt->mval[1];
390         
391         edge = findnearestedge(&lcd->vc, &dist);
392         if(!edge) {
393                 ringsel_exit(op);
394                 return OPERATOR_CANCELLED;
395         }
396
397         lcd->eed = edge;
398         ringsel_find_edge(lcd, 1);
399
400         ringsel_finish(C, op);
401         ringsel_exit(op);
402
403         return OPERATOR_FINISHED;
404 }
405
406 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
407 {
408         Object *obedit= CTX_data_edit_object(C);
409         tringselOpData *lcd;
410         EditEdge *edge;
411         int dist = 75;
412
413         if(modifiers_isDeformedByLattice(obedit) || modifiers_isDeformedByArmature(obedit))
414                 BKE_report(op->reports, RPT_WARNING, "Loop cut doesn't work well on deformed edit mesh display");
415         
416         view3d_operator_needs_opengl(C);
417
418         if (!ringsel_init(C, op, 1))
419                 return OPERATOR_CANCELLED;
420         
421         /* add a modal handler for this operator - handles loop selection */
422         WM_event_add_modal_handler(C, op);
423
424         lcd = op->customdata;
425         lcd->vc.mval[0] = evt->mval[0];
426         lcd->vc.mval[1] = evt->mval[1];
427         
428         edge = findnearestedge(&lcd->vc, &dist);
429         if (edge != lcd->eed) {
430                 lcd->eed = edge;
431                 ringsel_find_edge(lcd, 1);
432         }
433         ED_area_headerprint(CTX_wm_area(C), "Select a ring to be cut, use mouse-wheel or page-up/down for number of cuts");
434         
435         return OPERATOR_RUNNING_MODAL;
436 }
437
438 static int ringcut_modal (bContext *C, wmOperator *op, wmEvent *event)
439 {
440         int cuts= RNA_int_get(op->ptr,"number_cuts");
441         tringselOpData *lcd= op->customdata;
442
443         view3d_operator_needs_opengl(C);
444
445
446         switch (event->type) {
447                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
448                         if (event->val == KM_PRESS) {
449                                 /* finish */
450                                 ED_region_tag_redraw(lcd->ar);
451                                 
452                                 ringsel_finish(C, op);
453                                 ringsel_exit(op);
454                                 ED_area_headerprint(CTX_wm_area(C), NULL);
455                                 
456                                 return OPERATOR_FINISHED;
457                         }
458                         
459                         ED_region_tag_redraw(lcd->ar);
460                         break;
461                 case RIGHTMOUSE: /* abort */ // XXX hardcoded
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 WHEELUPMOUSE:  /* change number of cuts */
474                 case PADPLUSKEY:
475                 case PAGEUPKEY:
476                         if (event->val == KM_PRESS) {
477                                 cuts++;
478                                 RNA_int_set(op->ptr, "number_cuts",cuts);
479                                 ringsel_find_edge(lcd, cuts);
480                                 
481                                 ED_region_tag_redraw(lcd->ar);
482                         }
483                         break;
484                 case WHEELDOWNMOUSE:  /* change number of cuts */
485                 case PADMINUS:
486                 case PAGEDOWNKEY:
487                         if (event->val == KM_PRESS) {
488                                 cuts=MAX2(cuts-1,1);
489                                 RNA_int_set(op->ptr,"number_cuts",cuts);
490                                 ringsel_find_edge(lcd, cuts);
491                                 
492                                 ED_region_tag_redraw(lcd->ar);
493                         }
494                         break;
495                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
496                         int dist = 75;
497                         EditEdge *edge;
498
499                         lcd->vc.mval[0] = event->mval[0];
500                         lcd->vc.mval[1] = event->mval[1];
501                         edge = findnearestedge(&lcd->vc, &dist);
502
503                         if (edge != lcd->eed) {
504                                 lcd->eed = edge;
505                                 ringsel_find_edge(lcd, cuts);
506                         }
507
508                         ED_region_tag_redraw(lcd->ar);
509                         break;
510                 }                       
511         }
512         
513         /* using the keyboard to input the number of cuts */
514         if (event->val==KM_PRESS) {
515                 float value;
516                 
517                 if (handleNumInput(&lcd->num, event))
518                 {
519                         applyNumInput(&lcd->num, &value);
520                         
521                         cuts= CLAMPIS(value, 1, 32);
522                         
523                         RNA_int_set(op->ptr,"number_cuts",cuts);
524                         ringsel_find_edge(lcd, cuts);
525                         
526                         ED_region_tag_redraw(lcd->ar);
527                 }
528         }
529         
530         /* keep going until the user confirms */
531         return OPERATOR_RUNNING_MODAL;
532 }
533
534 void MESH_OT_edgering_select (wmOperatorType *ot)
535 {
536         /* description */
537         ot->name= "Edge Ring Select";
538         ot->idname= "MESH_OT_edgering_select";
539         ot->description= "Select an edge ring";
540         
541         /* callbacks */
542         ot->invoke= ringsel_invoke;
543         ot->poll= ED_operator_editmesh_region_view3d; 
544         
545         /* flags */
546         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
547
548         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
549 }
550
551 void MESH_OT_loopcut (wmOperatorType *ot)
552 {
553         /* description */
554         ot->name= "Loop Cut";
555         ot->idname= "MESH_OT_loopcut";
556         ot->description= "Add a new loop between existing loops";
557         
558         /* callbacks */
559         ot->invoke= ringcut_invoke;
560         ot->modal= ringcut_modal;
561         ot->cancel= ringcut_cancel;
562         ot->poll= ED_operator_editmesh_region_view3d;
563         
564         /* flags */
565         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
566
567         /* properties */
568         RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
569 }