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