soc-2008-mxcurioni: merged changes to revision 23516
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #define _USE_MATH_DEFINES
31 #include <math.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <stdio.h>
35
36 #include "DNA_ID.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_userdef_types.h"
40 #include "DNA_windowmanager_types.h"
41 #include "DNA_object_types.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "PIL_time.h"
46
47 #include "BLI_blenlib.h"
48 #include "BLI_dynstr.h" /*for WM_operator_pystring */
49 #include "BLI_editVert.h"
50
51 #include "BKE_blender.h"
52 #include "BKE_context.h"
53 #include "BKE_scene.h"
54 #include "BKE_utildefines.h"
55 #include "BKE_mesh.h"
56
57 #include "BIF_gl.h"
58 #include "BIF_glutil.h" /* for paint cursor */
59
60 #include "IMB_imbuf_types.h"
61
62 #include "ED_screen.h"
63 #include "ED_util.h"
64 #include "ED_space_api.h"
65 #include "ED_view3d.h"
66 #include "ED_mesh.h"
67
68 #include "RNA_access.h"
69 #include "RNA_define.h"
70
71 #include "UI_interface.h"
72 #include "UI_resources.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "mesh_intern.h"
78
79 /* ringsel operator */
80
81 /* struct for properties used while drawing */
82 typedef struct tringselOpData {
83         ARegion *ar;            /* region that ringsel was activated in */
84         void *draw_handle;      /* for drawing preview loop */
85         
86         float (*edges)[2][3];
87         int totedge;
88
89         ViewContext vc;
90
91         Object *ob;
92         EditMesh *em;
93         EditEdge *eed;
94
95         int extend;
96         int do_cut;
97 } tringselOpData;
98
99 /* modal loop selection drawing callback */
100 static void ringsel_draw(const bContext *C, ARegion *ar, void *arg)
101 {
102         int i;
103         tringselOpData *lcd = arg;
104         
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         glEnable(GL_DEPTH_TEST);
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)
253 {
254         if (lcd->eed)
255                 edgering_sel(lcd, 1, 0);
256 }
257
258 static void ringsel_finish(bContext *C, wmOperator *op)
259 {
260         tringselOpData *lcd= op->customdata;
261
262         if (lcd->eed) {
263                 edgering_sel(lcd, 0, 1);
264                 if (lcd->do_cut) {
265                         EditMesh *em = BKE_mesh_get_editmesh(lcd->ob->data);
266                         esubdivideflag(lcd->ob, em, SELECT, 0.0f, 
267                                        0.0f, 0, 1, SUBDIV_SELECT_LOOPCUT);
268                         
269                         DAG_id_flush_update(lcd->ob->data, OB_RECALC_DATA);
270                 }
271                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, lcd->ob->data);
272         }
273 }
274
275 /* called when modal loop selection is done... */
276 static void ringsel_exit (bContext *C, wmOperator *op)
277 {
278         tringselOpData *lcd= op->customdata;
279
280         /* deactivate the extra drawing stuff in 3D-View */
281         ED_region_draw_cb_exit(lcd->ar->type, lcd->draw_handle);
282         
283         if (lcd->edges)
284                 MEM_freeN(lcd->edges);
285
286         ED_region_tag_redraw(lcd->ar);
287
288         /* free the custom data */
289         MEM_freeN(lcd);
290         op->customdata= NULL;
291 }
292
293 /* called when modal loop selection gets set up... */
294 static int ringsel_init (bContext *C, wmOperator *op, int do_cut)
295 {
296         tringselOpData *lcd;
297         
298         /* alloc new customdata */
299         lcd= op->customdata= MEM_callocN(sizeof(tringselOpData), "ringsel Modal Op Data");
300         
301         /* assign the drawing handle for drawing preview line... */
302         lcd->ar= CTX_wm_region(C);
303         lcd->draw_handle= ED_region_draw_cb_activate(lcd->ar->type, ringsel_draw, lcd, REGION_DRAW_POST);
304         lcd->ob = CTX_data_edit_object(C);
305         lcd->em= BKE_mesh_get_editmesh((Mesh *)lcd->ob->data);
306         lcd->extend = do_cut ? 0 : RNA_boolean_get(op->ptr, "extend");
307         lcd->do_cut = do_cut;
308         em_setup_viewcontext(C, &lcd->vc);
309
310         ED_region_tag_redraw(lcd->ar);
311
312         return 1;
313 }
314
315 static int ringsel_cancel (bContext *C, wmOperator *op)
316 {
317         /* this is just a wrapper around exit() */
318         ringsel_exit(C, op);
319         return OPERATOR_CANCELLED;
320 }
321
322 static int ringsel_invoke (bContext *C, wmOperator *op, wmEvent *evt)
323 {
324         tringselOpData *lcd;
325         EditEdge *edge;
326         int dist = 75;
327
328         view3d_operator_needs_opengl(C);
329
330         if (!ringsel_init(C, op, 0))
331                 return OPERATOR_CANCELLED;
332         
333         /* add a modal handler for this operator - handles loop selection */
334         WM_event_add_modal_handler(C, op);
335
336         lcd = op->customdata;
337         lcd->vc.mval[0] = evt->mval[0];
338         lcd->vc.mval[1] = evt->mval[1];
339         
340         edge = findnearestedge(&lcd->vc, &dist);
341         if (edge != lcd->eed) {
342                 lcd->eed = edge;
343                 ringsel_find_edge(lcd, C, lcd->ar);
344         }
345
346         return OPERATOR_RUNNING_MODAL;
347 }
348
349
350 static int ringcut_invoke (bContext *C, wmOperator *op, wmEvent *evt)
351 {
352         tringselOpData *lcd;
353         EditEdge *edge;
354         int dist = 75;
355
356         view3d_operator_needs_opengl(C);
357
358         if (!ringsel_init(C, op, 1))
359                 return OPERATOR_CANCELLED;
360         
361         /* add a modal handler for this operator - handles loop selection */
362         WM_event_add_modal_handler(C, op);
363
364         lcd = op->customdata;
365         lcd->vc.mval[0] = evt->mval[0];
366         lcd->vc.mval[1] = evt->mval[1];
367         
368         edge = findnearestedge(&lcd->vc, &dist);
369         if (edge != lcd->eed) {
370                 lcd->eed = edge;
371                 ringsel_find_edge(lcd, C, lcd->ar);
372         }
373
374         return OPERATOR_RUNNING_MODAL;
375 }
376
377 static int ringsel_modal (bContext *C, wmOperator *op, wmEvent *event)
378 {
379         tringselOpData *lcd= op->customdata;
380
381         view3d_operator_needs_opengl(C);
382
383         switch (event->type) {
384                 case RIGHTMOUSE:
385                 case LEFTMOUSE: /* confirm */ // XXX hardcoded
386                         if (event->val == KM_RELEASE) {
387                                 /* finish */
388                                 ED_region_tag_redraw(lcd->ar);
389
390                                 ringsel_finish(C, op);
391                                 ringsel_exit(C, op);
392                                 
393                                 return OPERATOR_FINISHED;
394                         }
395
396                         ED_region_tag_redraw(lcd->ar);
397                         break;
398                 case MOUSEMOVE: { /* mouse moved somewhere to select another loop */
399                         int dist = 75;
400                         EditEdge *edge;
401
402                         lcd->vc.mval[0] = event->mval[0];
403                         lcd->vc.mval[1] = event->mval[1];
404                         edge = findnearestedge(&lcd->vc, &dist);
405
406                         if (edge != lcd->eed) {
407                                 lcd->eed = edge;
408                                 ringsel_find_edge(lcd, C, lcd->ar);
409                         }
410
411                         ED_region_tag_redraw(lcd->ar);
412                         break;
413                 }
414         }
415         
416         /* keep going until the user confirms */
417         return OPERATOR_RUNNING_MODAL;
418 }
419
420 void MESH_OT_edgering_select (wmOperatorType *ot)
421 {
422         /* description */
423         ot->name= "Edge Ring Select";
424         ot->idname= "MESH_OT_edgering_select";
425         ot->description= "Select an edge ring";
426         
427         /* callbacks */
428         ot->invoke= ringsel_invoke;
429         ot->modal= ringsel_modal;
430         ot->cancel= ringsel_cancel;
431         ot->poll= ED_operator_editmesh;
432         
433         /* flags */
434         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
435
436         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
437 }
438
439 void MESH_OT_loopcut (wmOperatorType *ot)
440 {
441         /* description */
442         ot->name= "Loop Cut";
443         ot->idname= "MESH_OT_loopcut";
444         ot->description= "Add a new loop between existing loops.";
445         
446         /* callbacks */
447         ot->invoke= ringcut_invoke;
448         ot->modal= ringsel_modal;
449         ot->cancel= ringsel_cancel;
450         ot->poll= ED_operator_editmesh;
451         
452         /* flags */
453         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
454 }