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