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