~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to source/blender/src/editmesh_tools.c

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
2
 * $Id: 
3
3
 *
4
 
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 
4
 * ***** BEGIN GPL LICENSE BLOCK *****
5
5
 *
6
6
 * This program is free software; you can redistribute it and/or
7
7
 * modify it under the terms of the GNU General Public License
8
8
 * as published by the Free Software Foundation; either version 2
9
 
 * of the License, or (at your option) any later version. The Blender
10
 
 * Foundation also sells licenses for use in proprietary software under
11
 
 * the Blender License.  See http://www.blender.org/BL/ for information
12
 
 * about this.
 
9
 * of the License, or (at your option) any later version.
13
10
 *
14
11
 * This program is distributed in the hope that it will be useful,
15
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
22
 *
26
23
 * The Original Code is: all of this file.
27
24
 *
28
 
 * Contributor(s): Johnny Matthews.
 
25
 * Contributor(s): Johnny Matthews, Geoffrey Bantle.
29
26
 *
30
 
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
27
 * ***** END GPL LICENSE BLOCK *****
31
28
 */
32
29
 
33
30
/*
51
48
#include "DNA_mesh_types.h"
52
49
#include "DNA_material_types.h"
53
50
#include "DNA_meshdata_types.h"
 
51
#include "DNA_modifier_types.h"
54
52
#include "DNA_object_types.h"
55
53
#include "DNA_scene_types.h"
56
54
#include "DNA_screen_types.h"
66
64
#include "BLI_heap.h"
67
65
 
68
66
#include "BKE_depsgraph.h"
 
67
#include "BKE_customdata.h"
69
68
#include "BKE_global.h"
70
69
#include "BKE_library.h"
71
70
#include "BKE_mesh.h"
72
71
#include "BKE_object.h"
73
72
#include "BKE_utildefines.h"
 
73
#include "BKE_bmesh.h"
 
74
 
 
75
#ifdef WITH_VERSE
 
76
#include "BKE_verse.h"
 
77
#endif
74
78
 
75
79
#include "BIF_cursors.h"
76
80
#include "BIF_editmesh.h"
77
81
#include "BIF_gl.h"
 
82
#include "BIF_glutil.h"
78
83
#include "BIF_graphics.h"
79
84
#include "BIF_interface.h"
80
85
#include "BIF_mywindow.h"
83
88
#include "BIF_resources.h"
84
89
#include "BIF_toolbox.h"
85
90
#include "BIF_transform.h"
 
91
#include "transform.h"
 
92
 
 
93
#ifdef WITH_VERSE
 
94
#include "BIF_verse.h"
 
95
#endif
86
96
 
87
97
#include "BDR_drawobject.h"
88
98
#include "BDR_editobject.h"
90
100
#include "BSE_view.h"
91
101
#include "BSE_edit.h"
92
102
 
 
103
#include "blendef.h"
 
104
#include "multires.h"
93
105
#include "mydevice.h"
94
 
#include "blendef.h"
95
106
 
96
107
#include "editmesh.h"
97
108
 
101
112
 
102
113
/* local prototypes ---------------*/
103
114
void bevel_menu(void);
104
 
static void free_tagged_edgelist(EditEdge *eed);
105
 
static void free_tagged_facelist(EditFace *efa);
 
115
static void free_tagged_edges_faces(EditEdge *eed, EditFace *efa);
106
116
 
107
117
/********* qsort routines *********/
108
118
 
145
155
        EditFace *efa, *efan, *next;
146
156
        float fac;
147
157
        
 
158
        if(multires_test()) return;
 
159
        
148
160
        efa= em->faces.last;
149
161
        while(efa) {
150
162
                next= efa->prev;
154
166
                                fac= VecLenf(efa->v1->co, efa->v3->co) - VecLenf(efa->v2->co, efa->v4->co);
155
167
                                /* this makes sure exact squares get split different in both cases */
156
168
                                if( (direction==0 && fac<FLT_EPSILON) || (direction && fac>0.0f) ) {
157
 
                                        
158
 
                                        efan= addfacelist(efa->v1, efa->v2, efa->v3, 0, efa, NULL);
159
 
                                        if(efa->f & SELECT) EM_select_face(efan, 1);
160
 
                                        efan= addfacelist(efa->v1, efa->v3, efa->v4, 0, efa, NULL);
161
 
                                        if(efa->f & SELECT) EM_select_face(efan, 1);
162
 
 
163
 
                                        efan->tf.uv[1][0]= efan->tf.uv[2][0];
164
 
                                        efan->tf.uv[1][1]= efan->tf.uv[2][1];
165
 
                                        efan->tf.uv[2][0]= efan->tf.uv[3][0];
166
 
                                        efan->tf.uv[2][1]= efan->tf.uv[3][1];
167
 
                                        
168
 
                                        efan->tf.col[1]= efan->tf.col[2];
169
 
                                        efan->tf.col[2]= efan->tf.col[3];
 
169
                                        efan= EM_face_from_faces(efa, NULL, 0, 1, 2, -1);
 
170
                                        if(efa->f & SELECT) EM_select_face(efan, 1);
 
171
                                        efan= EM_face_from_faces(efa, NULL, 0, 2, 3, -1);
 
172
                                        if(efa->f & SELECT) EM_select_face(efan, 1);
170
173
                                }
171
174
                                else {
172
 
                                        efan= addfacelist(efa->v1, efa->v2, efa->v4, 0, efa, NULL);
173
 
                                        if(efa->f & SELECT) EM_select_face(efan, 1);
174
 
                                        
175
 
                                        efan->tf.uv[2][0]= efan->tf.uv[3][0];
176
 
                                        efan->tf.uv[2][1]= efan->tf.uv[3][1];
177
 
                                        efan->tf.col[2]= efan->tf.col[3];
178
 
                                        
179
 
                                        efan= addfacelist(efa->v2, efa->v3, efa->v4, 0, efa, NULL);
180
 
                                        if(efa->f & SELECT) EM_select_face(efan, 1);
181
 
                                        
182
 
                                        efan->tf.uv[0][0]= efan->tf.uv[1][0];
183
 
                                        efan->tf.uv[0][1]= efan->tf.uv[1][1];
184
 
                                        efan->tf.uv[1][0]= efan->tf.uv[2][0];
185
 
                                        efan->tf.uv[1][1]= efan->tf.uv[2][1];
186
 
                                        efan->tf.uv[2][0]= efan->tf.uv[3][0];
187
 
                                        efan->tf.uv[2][1]= efan->tf.uv[3][1];
188
 
                                        
189
 
                                        efan->tf.col[0]= efan->tf.col[1];
190
 
                                        efan->tf.col[1]= efan->tf.col[2];
191
 
                                        efan->tf.col[2]= efan->tf.col[3];
 
175
                                        efan= EM_face_from_faces(efa, NULL, 0, 1, 3, -1);
 
176
                                        if(efa->f & SELECT) EM_select_face(efan, 1);
 
177
                                        efan= EM_face_from_faces(efa, NULL, 1, 2, 3, -1);
 
178
                                        if(efa->f & SELECT) EM_select_face(efan, 1);
192
179
                                }
193
180
                                
194
181
                                BLI_remlink(&em->faces, efa);
199
186
        }
200
187
        
201
188
        EM_fgon_flags();        // redo flags and indices for fgons
 
189
 
 
190
#ifdef WITH_VERSE
 
191
        if(G.editMesh->vnode)
 
192
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
193
#endif
202
194
        BIF_undo_push("Convert Quads to Triangles");
203
195
        
204
196
}
205
197
 
206
 
 
207
 
int removedoublesflag(short flag, float limit)          /* return amount */
 
198
int removedoublesflag(short flag, short automerge, float limit)         /* return amount */
208
199
{
 
200
        /*
 
201
                flag -          Test with vert->flags
 
202
                automerge -     Alternative operation, merge unselected into selected.
 
203
                                        Used for "Auto Weld" mode. warning.
 
204
                limit -         Quick manhattan distance between verts.
 
205
        */
 
206
        
209
207
        EditMesh *em = G.editMesh;
210
208
        /* all verts with (flag & 'flag') are being evaluated */
211
209
        EditVert *eve, *v1, *nextve;
213
211
        EditFace *efa, *nextvl;
214
212
        xvertsort *sortblock, *sb, *sb1;
215
213
        struct facesort *vlsortblock, *vsb, *vsb1;
216
 
        float dist;
217
 
        int a, b, test, amount, currweight, doubweight, targetweight;
218
 
        MDeformWeight *newdw;
 
214
        int a, b, test, amount;
 
215
        
 
216
        if(multires_test()) return 0;
219
217
 
 
218
        
220
219
        /* flag 128 is cleared, count */
 
220
 
 
221
        /* Normal non weld operation */
221
222
        eve= em->verts.first;
222
223
        amount= 0;
223
224
        while(eve) {
224
225
                eve->f &= ~128;
225
 
                if(eve->h==0 && (eve->f & flag)) amount++;
 
226
                if(eve->h==0 && (automerge || (eve->f & flag))) amount++;
226
227
                eve= eve->next;
227
228
        }
228
229
        if(amount==0) return 0;
231
232
        sb= sortblock= MEM_mallocN(sizeof(xvertsort)*amount,"sortremovedoub");
232
233
        eve= em->verts.first;
233
234
        while(eve) {
234
 
                if(eve->h==0 && (eve->f & flag)) {
 
235
                if(eve->h==0 && (automerge || (eve->f & flag))) {
235
236
                        sb->x= eve->co[0]+eve->co[1]+eve->co[2];
236
237
                        sb->v1= eve;
237
238
                        sb++;
240
241
        }
241
242
        qsort(sortblock, amount, sizeof(xvertsort), vergxco);
242
243
 
 
244
        
243
245
        /* test for doubles */
244
 
        sb= sortblock;
245
 
        for(a=0; a<amount; a++) {
246
 
                eve= sb->v1;
247
 
                if( (eve->f & 128)==0 ) {
248
 
                        sb1= sb+1;
249
 
                        for(b=a+1; b<amount; b++) {
250
 
                                /* first test: simpel dist */
251
 
                                dist= sb1->x - sb->x;
252
 
                                if(dist > limit) break;
253
 
                                
254
 
                                /* second test: is vertex allowed */
255
 
                                v1= sb1->v1;
256
 
                                if( (v1->f & 128)==0 ) {
 
246
        sb= sortblock;  
 
247
        if (automerge) {
 
248
                for(a=0; a<amount; a++, sb++) {
 
249
                        eve= sb->v1;
 
250
                        if( (eve->f & 128)==0 ) {
 
251
                                sb1= sb+1;
 
252
                                for(b=a+1; b<amount && (eve->f & 128)==0; b++, sb1++) {
 
253
                                        if(sb1->x - sb->x > limit) break;
257
254
                                        
258
 
                                        dist= (float)fabs(v1->co[0]-eve->co[0]);
259
 
                                        if(dist<=limit) {
260
 
                                                dist= (float)fabs(v1->co[1]-eve->co[1]);
261
 
                                                if(dist<=limit) {
262
 
                                                        dist= (float)fabs(v1->co[2]-eve->co[2]);
263
 
                                                        if(dist<=limit) {
 
255
                                        /* when automarge, only allow unselected->selected */
 
256
                                        v1= sb1->v1;
 
257
                                        if( (v1->f & 128)==0 ) {
 
258
                                                if ((eve->f & flag)==0 && (v1->f & flag)==1) {
 
259
                                                        if(     (float)fabs(v1->co[0]-eve->co[0])<=limit && 
 
260
                                                                (float)fabs(v1->co[1]-eve->co[1])<=limit &&
 
261
                                                                (float)fabs(v1->co[2]-eve->co[2])<=limit)
 
262
                                                        {       /* unique bit */
 
263
                                                                eve->f|= 128;
 
264
                                                                eve->tmp.v = v1;
 
265
                                                        }
 
266
                                                } else if(      (eve->f & flag)==1 && (v1->f & flag)==0 ) {
 
267
                                                        if(     (float)fabs(v1->co[0]-eve->co[0])<=limit && 
 
268
                                                                (float)fabs(v1->co[1]-eve->co[1])<=limit &&
 
269
                                                                (float)fabs(v1->co[2]-eve->co[2])<=limit)
 
270
                                                        {       /* unique bit */
264
271
                                                                v1->f|= 128;
265
272
                                                                v1->tmp.v = eve;
266
273
                                                        }
267
274
                                                }
268
275
                                        }
269
276
                                }
270
 
                                sb1++;
271
 
                        }
272
 
                }
273
 
                sb++;
 
277
                        }
 
278
                }
 
279
        } else {
 
280
                for(a=0; a<amount; a++, sb++) {
 
281
                        eve= sb->v1;
 
282
                        if( (eve->f & 128)==0 ) {
 
283
                                sb1= sb+1;
 
284
                                for(b=a+1; b<amount; b++, sb1++) {
 
285
                                        /* first test: simpel dist */
 
286
                                        if(sb1->x - sb->x > limit) break;
 
287
                                        v1= sb1->v1;
 
288
                                        
 
289
                                        /* second test: is vertex allowed */
 
290
                                        if( (v1->f & 128)==0 ) {
 
291
                                                if(     (float)fabs(v1->co[0]-eve->co[0])<=limit && 
 
292
                                                        (float)fabs(v1->co[1]-eve->co[1])<=limit &&
 
293
                                                        (float)fabs(v1->co[2]-eve->co[2])<=limit)
 
294
                                                {
 
295
                                                        v1->f|= 128;
 
296
                                                        v1->tmp.v = eve;
 
297
                                                }
 
298
                                        }
 
299
                                }
 
300
                        }
 
301
                }
274
302
        }
275
303
        MEM_freeN(sortblock);
276
 
 
277
 
        
278
 
        for(eve = em->verts.first; eve; eve=eve->next){
279
 
                
280
 
                if(eve->f & flag) {
281
 
                        if(eve->f & 128) {
282
 
                        
283
 
                                v1 = eve->tmp.v;
284
 
                                
285
 
                                                                
286
 
                                if(v1->dw && eve->dw){
287
 
                                        for(doubweight=0; doubweight < eve->totweight; doubweight++){
288
 
                                                targetweight = -1;
289
 
                                                for(currweight = 0; currweight < v1->totweight; currweight++){
290
 
                                                        if(v1->dw[currweight].def_nr == eve->dw[doubweight].def_nr){
291
 
                                                                targetweight = currweight;
292
 
                                                                break;
293
 
                                                        }
294
 
                                                }
295
 
                                                
296
 
                                                if(targetweight != -1){         /*average*/
297
 
                                                        v1->dw[targetweight].weight = (v1->dw[targetweight].weight + eve->dw[doubweight].weight) / 2;
298
 
                                                }
299
 
                                                else{   /*append*/
300
 
                                                        newdw = MEM_callocN(sizeof(MDeformWeight)*(v1->totweight+1), "MDeformWeight Append");
301
 
                                                        memcpy(newdw, v1->dw, sizeof(MDeformWeight)*v1->totweight);
302
 
                                                        MEM_freeN(v1->dw);
303
 
                                                        
304
 
                                                        v1->dw= newdw;
305
 
                                                        v1->dw[v1->totweight].weight = eve->dw[doubweight].weight;
306
 
                                                        v1->dw[v1->totweight].def_nr = eve->dw[doubweight].def_nr;
307
 
                                                        v1->totweight++;
308
 
                                                }       
309
 
                                        }
310
 
                                }
311
 
                                else if(eve->dw){       /*just straight copy vert weights*/
312
 
                                        
313
 
                                        newdw = MEM_mallocN(sizeof(MDeformWeight) * (eve->totweight), "MDeformWeight Copy");
314
 
                                        memcpy(newdw, eve->dw, sizeof(MDeformWeight)*eve->totweight);
315
 
                                        v1->dw= newdw;
316
 
                                        
317
 
                                }
318
 
                        }
319
 
                }
320
 
        }
321
 
 
 
304
        
 
305
        if (!automerge)
 
306
                for(eve = em->verts.first; eve; eve=eve->next)
 
307
                        if((eve->f & flag) && (eve->f & 128))
 
308
                                EM_data_interp_from_verts(eve, eve->tmp.v, eve->tmp.v, 0.5f);
 
309
        
322
310
        /* test edges and insert again */
323
311
        eed= em->edges.first;
324
312
        while(eed) {
336
324
                                if(eed->v1->f & 128) eed->v1 = eed->v1->tmp.v;
337
325
                                if(eed->v2->f & 128) eed->v2 = eed->v2->tmp.v;
338
326
                                e1= addedgelist(eed->v1, eed->v2, eed);
339
 
                                
340
 
                                if(e1) e1->f2= 1;
 
327
 
 
328
                                if(e1) {
 
329
                                        e1->f2= 1;
 
330
                                        if(eed->f & SELECT)
 
331
                                                e1->f |= SELECT;
 
332
                                }
341
333
                                if(e1!=eed) free_editedge(eed);
342
334
                        }
343
335
                }
381
373
                                if(efa->v4) {
382
374
                                        if(test==1 || test==2) {
383
375
                                                efa->v2= efa->v3;
384
 
                                                efa->tf.uv[1][0] = efa->tf.uv[2][0];
385
 
                                                efa->tf.uv[1][1] = efa->tf.uv[2][1];
386
 
                                                efa->tf.col[1] = efa->tf.col[2];
387
 
                                                
388
376
                                                efa->v3= efa->v4;
389
 
                                                efa->tf.uv[2][0] = efa->tf.uv[3][0];
390
 
                                                efa->tf.uv[2][1] = efa->tf.uv[3][1];
391
 
                                                efa->tf.col[2] = efa->tf.col[3];
392
377
                                                efa->v4= 0;
 
378
 
 
379
                                                EM_data_interp_from_faces(efa, NULL, efa, 0, 2, 3, 3);
 
380
 
393
381
                                                test= 0;
394
382
                                        }
395
383
                                        else if(test==8 || test==16) {
498
486
        eve= (struct EditVert *)em->verts.first;
499
487
        while(eve) {
500
488
                nextve= eve->next;
501
 
                if(eve->f & flag) {
 
489
                if(automerge || eve->f & flag) {
502
490
                        if(eve->f & 128) {
503
491
                                a++;
504
492
                                BLI_remlink(&em->verts, eve);
508
496
                eve= nextve;
509
497
        }
510
498
 
 
499
#ifdef WITH_VERSE
 
500
        if((a>0) && (G.editMesh->vnode)) {
 
501
                sync_all_verseverts_with_editverts((VNode*)G.editMesh->vnode);
 
502
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
503
        }
 
504
#endif
 
505
 
511
506
        return a;       /* amount */
512
507
}
513
508
 
527
522
        ListBase tbase;
528
523
        int i, amount = BLI_countlist(&em->verts);
529
524
        
 
525
        if(multires_test()) return;
 
526
        
530
527
        sortblock = MEM_callocN(sizeof(xvertsort)*amount,"xsort");
531
528
        for (i=0,eve=em->verts.first; eve; i++,eve=eve->next)
532
529
                if(eve->f & flag)
548
545
        addlisttolist(&em->verts, &tbase);
549
546
        
550
547
        MEM_freeN(sortblock);
 
548
 
 
549
#ifdef WITH_VERSE
 
550
        if(G.editMesh->vnode)
 
551
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
552
#endif
 
553
 
551
554
        BIF_undo_push("Xsort");
552
555
        
553
556
}
562
565
        ListBase tbase;
563
566
        int amount, a, b;
564
567
        
 
568
        if(multires_test()) return;
 
569
        
565
570
        /* count */
566
571
        eve= em->verts.first;
567
572
        amount= 0;
608
613
        addlisttolist(&em->verts, &tbase);
609
614
        
610
615
        MEM_freeN(sortblock);
 
616
#ifdef WITH_VERSE
 
617
        if(G.editMesh->vnode)
 
618
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
619
#endif
611
620
        BIF_undo_push("Hash");
612
621
 
613
622
}
619
628
        short nr, transmode= 0;
620
629
 
621
630
        TEST_EDITMESH
 
631
        if(multires_test()) return;
622
632
        
623
633
        if(G.scene->selectmode & SCE_SELECT_VERTEX) {
624
634
                if(G.totvertsel==0) nr= 0;
673
683
                /* individual faces? */
674
684
                BIF_TransformSetUndo("Extrude");
675
685
                if(nr==2) {
676
 
                        initTransform(TFM_SHRINKFATTEN, CTX_NO_PET);
 
686
                        initTransform(TFM_SHRINKFATTEN, CTX_NO_PET|CTX_NO_MIRROR);
677
687
                        Transform();
678
688
                }
679
689
                else {
680
 
                        initTransform(TFM_TRANSLATION, CTX_NO_PET);
 
690
                        initTransform(TFM_TRANSLATION, CTX_NO_PET|CTX_NO_MIRROR);
681
691
                        if(transmode=='n') {
682
692
                                Mat4MulVecfl(G.obedit->obmat, nor);
683
693
                                VecSubf(nor, nor, G.obedit->obmat[3]);
684
 
                                BIF_setSingleAxisConstraint(nor, NULL);
 
694
                                BIF_setSingleAxisConstraint(nor, "along normal");
685
695
                        }
686
696
                        Transform();
687
697
                }
693
703
{
694
704
 
695
705
        TEST_EDITMESH
 
706
        if(multires_test()) return;
696
707
 
697
708
        if(okee(" Split ")==0) return;
698
709
 
709
720
        countall();
710
721
        allqueue(REDRAWVIEW3D, 0);
711
722
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
723
 
 
724
#ifdef WITH_VERSE
 
725
        if(G.editMesh->vnode)
 
726
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
727
#endif
 
728
 
712
729
        BIF_undo_push("Split");
713
730
 
714
731
}
719
736
        short a;
720
737
 
721
738
        TEST_EDITMESH
 
739
        if(multires_test()) return;
722
740
        
723
741
        /* dvec */
724
742
        dvec[0]= G.vd->persinv[2][0];
725
743
        dvec[1]= G.vd->persinv[2][1];
726
744
        dvec[2]= G.vd->persinv[2][2];
727
 
        Normalise(dvec);
 
745
        Normalize(dvec);
728
746
        dvec[0]*= offs;
729
747
        dvec[1]*= offs;
730
748
        dvec[2]*= offs;
746
764
        
747
765
        allqueue(REDRAWVIEW3D, 0);
748
766
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
749
 
        
 
767
 
750
768
        BIF_undo_push("Extrude Repeat");
751
769
}
752
770
 
753
 
void spin_mesh(int steps, int degr, float *dvec, int mode)
 
771
void spin_mesh(int steps, float degr, float *dvec, int mode)
754
772
{
755
773
        EditMesh *em = G.editMesh;
756
774
        EditVert *eve,*nextve;
761
779
        short a,ok;
762
780
 
763
781
        TEST_EDITMESH
 
782
        if(multires_test()) return;
764
783
        
765
 
        /* imat and centre and size */
 
784
        /* imat and center and size */
766
785
        Mat3CpyMat4(bmat, G.obedit->obmat);
767
786
        Mat3Inv(imat,bmat);
768
787
 
773
792
        cent[2]-= G.obedit->obmat[3][2];
774
793
        Mat3MulVecfl(imat, cent);
775
794
 
776
 
        phi= (float)(degr*M_PI/360.0);
 
795
        phi= degr*M_PI/360.0;
777
796
        phi/= steps;
778
797
        if(G.scene->toolsettings->editbutflag & B_CLOCKWISE) phi= -phi;
779
798
 
780
799
        if(dvec) {
781
 
                n[0]=n[1]= 0.0;
782
 
                n[2]= 1.0;
 
800
                n[0]= G.vd->viewinv[1][0];
 
801
                n[1]= G.vd->viewinv[1][1];
 
802
                n[2]= G.vd->viewinv[1][2];
783
803
        } else {
784
804
                n[0]= G.vd->viewinv[2][0];
785
805
                n[1]= G.vd->viewinv[2][1];
786
806
                n[2]= G.vd->viewinv[2][2];
787
 
                Normalise(n);
788
807
        }
 
808
        Normalize(n);
789
809
 
790
810
        q[0]= (float)cos(phi);
791
811
        si= (float)sin(phi);
832
852
        countall();
833
853
        allqueue(REDRAWVIEW3D, 0);
834
854
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
855
 
835
856
        
836
857
        if(dvec==NULL) BIF_undo_push("Spin");
837
858
}
844
865
        float dvec[3], nor[3];
845
866
 
846
867
        TEST_EDITMESH
847
 
 
848
 
        /* first condition: we need frontview! */
849
 
        if(G.vd->view!=1) {
850
 
                error("Must be in Front View");
851
 
                return;
852
 
        }
 
868
        if(multires_test()) return;
853
869
        
854
870
        /* clear flags */
855
871
        eve= em->verts.first;
963
979
        char *str="Erase";
964
980
 
965
981
        TEST_EDITMESH
 
982
        if(multires_test()) return;
966
983
        
967
984
        event= pupmenu("Erase %t|Vertices%x10|Edges%x1|Faces%x2|All%x3|Edges & Faces%x4|Only Faces%x5|Edge Loop%x6");
968
985
        if(event<1) return;
974
991
                erase_vertices(&em->verts);
975
992
        } 
976
993
        else if(event==6) {
977
 
                if(!EdgeLoopDelete()) {
978
 
                        BIF_undo();
979
 
                }
 
994
                if(!EdgeLoopDelete())
 
995
                        return;
 
996
 
 
997
                str= "Erase Edge Loop";
980
998
        }
981
999
        else if(event==4) {
982
1000
                str= "Erase Edges & Faces";
1109
1127
        short ok;
1110
1128
 
1111
1129
        if(G.obedit==0 || (G.obedit->type!=OB_MESH)) return;
 
1130
        if(multires_test()) return;
1112
1131
 
1113
1132
        waitcursor(1);
1114
1133
 
1179
1198
        countall();
1180
1199
        allqueue(REDRAWVIEW3D, 0);
1181
1200
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
1201
 
 
1202
#ifdef WITH_VERSE
 
1203
        if(G.editMesh->vnode)
 
1204
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
1205
#endif
 
1206
 
1182
1207
        BIF_undo_push("Fill");
1183
1208
}
1184
 
 
 
1209
/*GB*/
1185
1210
/*-------------------------------------------------------------------------------*/
1186
1211
/*--------------------------- Edge Based Subdivide ------------------------------*/
1187
1212
 
1193
1218
/*used by faceloop cut to select only edges valid for edge slide*/
1194
1219
#define DOUBLEOPFILL 16
1195
1220
 
1196
 
/* Mostly mirrored from editdeform.c, here only used for the function below */
1197
 
/* Only to be used to add new weights in eve, the value of weight has been premultiplied with subdiv factor, so is added only */
1198
 
static void subdiv_add_defweight (EditVert *eve, int defgroup, float weight)
1199
 
{
1200
 
        MDeformWeight *newdw;
1201
 
        int     i;
1202
 
        
1203
 
        if (defgroup<0)
1204
 
                return;
1205
 
        
1206
 
        for (i=0; i<eve->totweight; i++) {
1207
 
                if (eve->dw[i].def_nr == defgroup) {
1208
 
                        eve->dw[i].weight += weight;
1209
 
                        return;
1210
 
                }
1211
 
        }
1212
 
        
1213
 
        newdw = MEM_callocN (sizeof(MDeformWeight)*(eve->totweight+1), "subdiv deformWeight");
1214
 
        if (eve->dw) {
1215
 
                memcpy (newdw, eve->dw, sizeof(MDeformWeight)*eve->totweight);
1216
 
                MEM_freeN (eve->dw);
1217
 
        }
1218
 
        eve->dw= newdw;
1219
 
        
1220
 
        eve->dw[eve->totweight].weight= weight;
1221
 
        eve->dw[eve->totweight].def_nr= defgroup;
1222
 
        
1223
 
        eve->totweight++;
1224
 
        
1225
 
}
1226
 
 
1227
 
 
1228
 
/* the new vertex *eve will get vertex groups as defined in eed, based on fac subdivide */
1229
 
static void subdivide_edge_vgroups(EditEdge *eed, EditVert *eve, float fac)
1230
 
{
1231
 
        EditVert *v1= eed->v1, *v2= eed->v2;
1232
 
        int i;
1233
 
        
1234
 
        /* let's first check of there are groups */
1235
 
        if(v1->totweight==0 && v2->totweight==0)
1236
 
                return;
1237
 
        
1238
 
        /* now add the weights of v1 into the new vertex */
1239
 
        for (i=0; i<v1->totweight; i++) {
1240
 
                subdiv_add_defweight(eve, v1->dw[i].def_nr, v1->dw[i].weight*(1.0f-fac));
1241
 
        }
1242
 
        
1243
 
        /* now add the weights of v2 into the new vertex */
1244
 
        for (i=0; i<v2->totweight; i++) {
1245
 
                subdiv_add_defweight(eve, v2->dw[i].def_nr, v2->dw[i].weight*fac);
1246
 
        }
1247
 
}
1248
 
 
1249
1221
/* calculates offset for co, based on fractal, sphere or smooth settings  */
1250
1222
static void alter_co(float *co, EditEdge *edge, float rad, int beauty, float perc)
1251
1223
{
1256
1228
                float len, fac, nor[3], nor1[3], nor2[3];
1257
1229
                
1258
1230
                VecSubf(nor, edge->v1->co, edge->v2->co);
1259
 
                len= 0.5f*Normalise(nor);
 
1231
                len= 0.5f*Normalize(nor);
1260
1232
        
1261
1233
                VECCOPY(nor1, edge->v1->no);
1262
1234
                VECCOPY(nor2, edge->v2->no);
1285
1257
        }
1286
1258
        else {
1287
1259
                if(rad > 0.0) {   /* subdivide sphere */
1288
 
                        Normalise(co);
 
1260
                        Normalize(co);
1289
1261
                        co[0]*= rad;
1290
1262
                        co[1]*= rad;
1291
1263
                        co[2]*= rad;
1316
1288
        /* offset for smooth or sphere or fractal */
1317
1289
        alter_co(co, edge, rad, beauty, percent);
1318
1290
        
1319
 
        ev = addvertlist(co);
1320
 
        
1321
 
        /* vgroups */
1322
 
        subdivide_edge_vgroups(edge, ev, percent);
 
1291
        /* clip if needed by mirror modifier */
 
1292
        if (edge->v1->f2) {
 
1293
                if ( edge->v1->f2 & edge->v2->f2 & 1) {
 
1294
                        co[0]= 0.0f;
 
1295
                }
 
1296
                if ( edge->v1->f2 & edge->v2->f2 & 2) {
 
1297
                        co[1]= 0.0f;
 
1298
                }
 
1299
                if ( edge->v1->f2 & edge->v2->f2 & 4) {
 
1300
                        co[2]= 0.0f;
 
1301
                }
 
1302
        }
 
1303
        
 
1304
        ev = addvertlist(co, NULL);
 
1305
        
 
1306
        /* vert data (vgroups, ..) */
 
1307
        EM_data_interp_from_verts(edge->v1, edge->v2, ev, percent);
1323
1308
        
1324
1309
        /* normal */
1325
1310
        ev->no[0] = (edge->v2->no[0]-edge->v1->no[0])*percent + edge->v1->no[0];
1326
1311
        ev->no[1] = (edge->v2->no[1]-edge->v1->no[1])*percent + edge->v1->no[1];
1327
1312
        ev->no[2] = (edge->v2->no[2]-edge->v1->no[2])*percent + edge->v1->no[2];
1328
 
        Normalise(ev->no);
 
1313
        Normalize(ev->no);
1329
1314
        
1330
1315
        return ev;
1331
1316
}
1342
1327
        }
1343
1328
}
1344
1329
 
1345
 
static int VecEqual(float *a, float *b)
1346
 
{
1347
 
        if( a[0] == b[0] && 
1348
 
           (a[1] == b[1] && 
1349
 
                a[2] == b[2])) {
1350
 
                return 1;
1351
 
        }
1352
 
        else {
1353
 
                return 0;
1354
 
        }
1355
 
}
1356
 
 
1357
 
static void set_uv_vcol(EditFace *efa, float *co, float *uv, char *col)
1358
 
1359
 
        EditVert *v1,*v2,*v3,*v4;
1360
 
        float xn, yn, zn;
1361
 
        float t00, t01, t10, t11;
1362
 
        float detsh, u, v, l;
1363
 
        int fac;
1364
 
        short i, j;
1365
 
        char *cp0, *cp1, *cp2;
1366
 
        char *hold;
1367
 
        
1368
 
        //First Check for exact match between co and efa verts
1369
 
        if(VecEqual(co,efa->v1->co)) {
1370
 
                uv[0] = efa->tf.uv[0][0];
1371
 
                uv[1] = efa->tf.uv[0][1];
1372
 
                
1373
 
                hold = (char*)&efa->tf.col[0];
1374
 
                col[0]= hold[0];
1375
 
                col[1]= hold[1];
1376
 
                col[2]= hold[2];
1377
 
                col[3]= hold[3];
1378
 
                return;   
1379
 
        } else if(VecEqual(co,efa->v2->co)) {
1380
 
                uv[0] = efa->tf.uv[1][0];
1381
 
                uv[1] = efa->tf.uv[1][1];
1382
 
                
1383
 
                hold = (char*)&efa->tf.col[1];
1384
 
                col[0]= hold[0];
1385
 
                col[1]= hold[1];
1386
 
                col[2]= hold[2];
1387
 
                col[3]= hold[3]; 
1388
 
                return;    
1389
 
        } else if(VecEqual(co,efa->v3->co)) {
1390
 
                uv[0] = efa->tf.uv[2][0];
1391
 
                uv[1] = efa->tf.uv[2][1];
1392
 
                
1393
 
                hold = (char*)&efa->tf.col[2];
1394
 
                col[0]= hold[0];
1395
 
                col[1]= hold[1];
1396
 
                col[2]= hold[2];
1397
 
                col[3]= hold[3];        
1398
 
                return;   
1399
 
        } else if(efa->v4 && VecEqual(co,efa->v4->co)) {
1400
 
                uv[0] = efa->tf.uv[3][0];
1401
 
                uv[1] = efa->tf.uv[3][1];
1402
 
                
1403
 
                hold = (char*)&efa->tf.col[3];
1404
 
                col[0]= hold[0];
1405
 
                col[1]= hold[1];
1406
 
                col[2]= hold[2];
1407
 
                col[3]= hold[3];   
1408
 
                return;  
1409
 
        }       
1410
 
        
1411
 
        /* define best projection of face XY, XZ or YZ */
1412
 
        xn= fabs(efa->n[0]);
1413
 
        yn= fabs(efa->n[1]);
1414
 
        zn= fabs(efa->n[2]);
1415
 
        if(zn>=xn && zn>=yn) {i= 0; j= 1;}
1416
 
        else if(yn>=xn && yn>=zn) {i= 0; j= 2;}
1417
 
        else {i= 1; j= 2;} 
1418
 
        
1419
 
        /* calculate u and v */
1420
 
        v1= efa->v1;
1421
 
        v2= efa->v2;
1422
 
        v3= efa->v3;
1423
 
                
1424
 
        t00= v3->co[i]-v1->co[i]; t01= v3->co[j]-v1->co[j];
1425
 
        t10= v3->co[i]-v2->co[i]; t11= v3->co[j]-v2->co[j];
1426
 
                
1427
 
        detsh= 1.0/(t00*t11-t10*t01);   /* potential danger */
1428
 
        t00*= detsh; t01*=detsh;
1429
 
        t10*=detsh; t11*=detsh;
1430
 
                
1431
 
        u= (co[i]-v3->co[i])*t11-(co[j]-v3->co[j])*t10;
1432
 
        v= (co[j]-v3->co[j])*t00-(co[i]-v3->co[i])*t01; 
1433
 
        
1434
 
        /* btw; u and v range from -1 to 0 */
1435
 
                
1436
 
        /* interpolate */
1437
 
        l= 1.0+u+v;
1438
 
                /* outside triangle? */
1439
 
                /* printf("l: %f u %f v %f\n",l,u,v); */
1440
 
        
1441
 
        if(efa->v4 && (v>0.001f)) {     /* only check for positive v is OK, that's the diagonal */
1442
 
                /* printf("outside\n"); */
1443
 
                /* do it all over, but now with vertex 2 replaced with 4 */
1444
 
                
1445
 
                /* calculate u and v */
1446
 
                v1= efa->v1;
1447
 
                v4= efa->v4;
1448
 
                v3= efa->v3;
1449
 
                                
1450
 
                t00= v3->co[i]-v1->co[i]; t01= v3->co[j]-v1->co[j];
1451
 
                t10= v3->co[i]-v4->co[i]; t11= v3->co[j]-v4->co[j];
1452
 
                                
1453
 
                detsh= 1.0/(t00*t11-t10*t01);   /* potential danger */
1454
 
                t00*= detsh; t01*=detsh;
1455
 
                t10*=detsh; t11*=detsh;
1456
 
                                
1457
 
                u= (co[i]-v3->co[i])*t11-(co[j]-v3->co[j])*t10;
1458
 
                v= (co[j]-v3->co[j])*t00-(co[i]-v3->co[i])*t01; 
1459
 
                
1460
 
                /* btw; u and v range from -1 to 0 */
1461
 
                                
1462
 
                /* interpolate */
1463
 
                l= 1.0+u+v;
1464
 
                uv[0] = (l*efa->tf.uv[2][0] - u*efa->tf.uv[0][0] - v*efa->tf.uv[3][0]);
1465
 
                uv[1] = (l*efa->tf.uv[2][1] - u*efa->tf.uv[0][1] - v*efa->tf.uv[3][1]);
1466
 
 
1467
 
                cp0= (char*)&(efa->tf.col[0]);
1468
 
                cp1= (char*)&(efa->tf.col[3]);
1469
 
                cp2= (char*)&(efa->tf.col[2]);
1470
 
                
1471
 
                for(i=0; i<4; i++) {
1472
 
                        fac= (int)(l*cp2[i] - u*cp0[i] - v*cp1[i]);
1473
 
                        col[i]= CLAMPIS(fac, 0, 255);
1474
 
                }                         
1475
 
        } else {         
1476
 
        //      printf("inside\n");      
1477
 
                //new = l*vertex3_val - u*vertex1_val - v*vertex2_val;
1478
 
                uv[0] = (l*efa->tf.uv[2][0] - u*efa->tf.uv[0][0] - v*efa->tf.uv[1][0]);
1479
 
                uv[1] = (l*efa->tf.uv[2][1] - u*efa->tf.uv[0][1] - v*efa->tf.uv[1][1]);
1480
 
 
1481
 
                cp0= (char*)&(efa->tf.col[0]);
1482
 
                cp1= (char*)&(efa->tf.col[1]);
1483
 
                cp2= (char*)&(efa->tf.col[2]);
1484
 
                
1485
 
                for(i=0; i<4; i++) {
1486
 
                        fac= (int)(l*cp2[i] - u*cp0[i] - v*cp1[i]);
1487
 
                        col[i]= CLAMPIS(fac, 0, 255);
1488
 
                }                                          
1489
 
        }
1490
 
1491
 
 
1492
 
static void facecopy(EditFace *source,EditFace *target)
1493
 
{
1494
 
 
1495
 
        set_uv_vcol(source,target->v1->co,target->tf.uv[0],(char*)&target->tf.col[0]);
1496
 
        set_uv_vcol(source,target->v2->co,target->tf.uv[1],(char*)&target->tf.col[1]);
1497
 
        set_uv_vcol(source,target->v3->co,target->tf.uv[2],(char*)&target->tf.col[2]);
1498
 
        if(target->v4)
1499
 
                set_uv_vcol(source,target->v4->co,target->tf.uv[3],(char*)&target->tf.col[3]);
1500
 
 
1501
 
        target->mat_nr   = source->mat_nr;
1502
 
        target->tf.flag = source->tf.flag&~TF_ACTIVE;
1503
 
        target->tf.transp  = source->tf.transp;
1504
 
        target->tf.mode = source->tf.mode;
1505
 
        target->tf.tile = source->tf.tile;
1506
 
        target->tf.unwrap  = source->tf.unwrap;
1507
 
        target->tf.tpage   = source->tf.tpage;
1508
 
        target->flag       = source->flag;      
 
1330
static void facecopy(EditFace *source, EditFace *target)
 
1331
{
 
1332
        EditMesh *em= G.editMesh;
 
1333
        float *v1 = source->v1->co, *v2 = source->v2->co, *v3 = source->v3->co;
 
1334
        float *v4 = source->v4? source->v4->co: NULL;
 
1335
        float w[4][4];
 
1336
 
 
1337
        CustomData_em_copy_data(&em->fdata, &em->fdata, source->data, &target->data);
 
1338
 
 
1339
        target->mat_nr = source->mat_nr;
 
1340
        target->flag   = source->flag;  
 
1341
        target->h          = source->h;
 
1342
        
 
1343
        InterpWeightsQ3Dfl(v1, v2, v3, v4, target->v1->co, w[0]);
 
1344
        InterpWeightsQ3Dfl(v1, v2, v3, v4, target->v2->co, w[1]);
 
1345
        InterpWeightsQ3Dfl(v1, v2, v3, v4, target->v3->co, w[2]);
 
1346
        if (target->v4)
 
1347
                InterpWeightsQ3Dfl(v1, v2, v3, v4, target->v4->co, w[3]);
 
1348
        
 
1349
        CustomData_em_interp(&em->fdata, &source->data, NULL, (float*)w, 1, target->data);
1509
1350
}
1510
1351
 
1511
1352
static void fill_quad_single(EditFace *efa, struct GHash *gh, int numcuts, int seltype)
1571
1412
        }else{
1572
1413
                hold = addfacelist(verts[(vertsize-1)/2],v[left],v[right],NULL, NULL,NULL);  
1573
1414
                hold->e1->f2 |= EDGEINNER;
1574
 
                hold->e3->f2 |= EDGEINNER;                
 
1415
                hold->e3->f2 |= EDGEINNER;
1575
1416
        }
1576
1417
        facecopy(efa,hold);
1577
1418
 
1927
1768
        inner = MEM_mallocN(sizeof(EditVert*)*numcuts,"New inner verts");
1928
1769
        
1929
1770
        for(i=0;i<numcuts;i++) {
1930
 
                        co[0] = (verts[0][numcuts-i]->co[0] + verts[1][i+1]->co[0] ) / 2 ;
1931
 
                        co[1] = (verts[0][numcuts-i]->co[1] + verts[1][i+1]->co[1] ) / 2 ;
1932
 
                        co[2] = (verts[0][numcuts-i]->co[2] + verts[1][i+1]->co[2] ) / 2 ;
1933
 
                        inner[i] = addvertlist(co);
1934
 
                        inner[i]->f2 |= EDGEINNER;
 
1771
                co[0] = (verts[0][numcuts-i]->co[0] + verts[1][i+1]->co[0] ) / 2 ;
 
1772
                co[1] = (verts[0][numcuts-i]->co[1] + verts[1][i+1]->co[1] ) / 2 ;
 
1773
                co[2] = (verts[0][numcuts-i]->co[2] + verts[1][i+1]->co[2] ) / 2 ;
 
1774
                inner[i] = addvertlist(co, NULL);
 
1775
                inner[i]->f2 |= EDGEINNER;
 
1776
 
 
1777
                EM_data_interp_from_verts(verts[0][numcuts-i], verts[1][i+1], inner[i], 0.5f);
1935
1778
        }
1936
1779
        
1937
1780
        // Add Corner Quad
2039
1882
 
2040
1883
static void fill_quad_triple(EditFace *efa, struct GHash *gh, int numcuts)
2041
1884
{
2042
 
        EditEdge *cedge[3];
 
1885
        EditEdge *cedge[3]={0};
2043
1886
        EditVert *v[4], **verts[3];
2044
1887
        EditFace *hold;
2045
1888
        short start=0, start2=0, start3=0, vertsize, i, repeats;
2414
2257
        MEM_freeN(innerverts);
2415
2258
}
2416
2259
 
 
2260
//Next two fill types are for knife exact only and are provided to allow for knifing through vertices
 
2261
//This means there is no multicut!
 
2262
static void fill_quad_doublevert(EditFace *efa, int v1, int v2){
 
2263
        EditFace *hold;
 
2264
        /*
 
2265
                Depending on which two vertices have been knifed through (v1 and v2), we
 
2266
                triangulate like the patterns below.
 
2267
                                X-------|       |-------X
 
2268
                                | \     |       |     / |
 
2269
                                |   \   |       |   /   |
 
2270
                                |         \     |       | /         |
 
2271
                                --------X       X--------
 
2272
        */
 
2273
        
 
2274
        if(v1 == 1 && v2 == 3){
 
2275
                hold= addfacelist(efa->v1, efa->v2, efa->v3, 0, efa, NULL);
 
2276
                hold->e1->f2 |= EDGENEW;
 
2277
                hold->e2->f2 |= EDGENEW;
 
2278
                hold->e3->f2 |= EDGENEW;
 
2279
                hold->e3->f2 |= EDGEINNER;
 
2280
                facecopy(efa, hold);
 
2281
                
 
2282
                hold= addfacelist(efa->v1, efa->v3, efa->v4, 0, efa, NULL);
 
2283
                hold->e1->f2 |= EDGENEW;
 
2284
                hold->e2->f2 |= EDGENEW;
 
2285
                hold->e3->f2 |= EDGENEW;
 
2286
                hold->e1->f2 |= EDGEINNER;
 
2287
                facecopy(efa, hold);
 
2288
        }
 
2289
        else{
 
2290
                hold= addfacelist(efa->v1, efa->v2, efa->v4, 0, efa, NULL);
 
2291
                hold->e1->f2 |= EDGENEW;
 
2292
                hold->e2->f2 |= EDGENEW;
 
2293
                hold->e3->f2 |= EDGENEW;
 
2294
                hold->e2->f2 |= EDGEINNER;
 
2295
                facecopy(efa, hold);
 
2296
                
 
2297
                hold= addfacelist(efa->v2, efa->v3, efa->v4, 0, efa, NULL);
 
2298
                hold->e1->f2 |= EDGENEW;
 
2299
                hold->e2->f2 |= EDGENEW;
 
2300
                hold->e3->f2 |= EDGENEW;
 
2301
                hold->e3->f2 |= EDGEINNER;
 
2302
                facecopy(efa, hold);
 
2303
        }
 
2304
}
 
2305
 
 
2306
static void fill_quad_singlevert(EditFace *efa, struct GHash *gh)
 
2307
{
 
2308
        EditEdge *cedge=NULL;
 
2309
        EditVert *v[4], **verts;
 
2310
        EditFace *hold;
 
2311
        short start=0, end, left, right, vertsize;   
 
2312
                                                        
 
2313
        v[0] = efa->v1;
 
2314
        v[1] = efa->v2;
 
2315
        v[2] = efa->v3;
 
2316
        v[3] = efa->v4;  
 
2317
 
 
2318
        if(efa->e1->f & SELECT)   { cedge = efa->e1; start = 0;}
 
2319
        else if(efa->e2->f & SELECT) { cedge = efa->e2; start = 1;}        
 
2320
        else if(efa->e3->f & SELECT) { cedge = efa->e3; start = 2;}        
 
2321
        else if(efa->e4->f & SELECT) { cedge = efa->e4; start = 3;}              
 
2322
 
 
2323
        // Point verts to the array of new verts for cedge
 
2324
        verts = BLI_ghash_lookup(gh, cedge);
 
2325
        //This is the index size of the verts array
 
2326
        vertsize = 3;
 
2327
 
 
2328
        // Is the original v1 the same as the first vert on the selected edge?
 
2329
        // if not, the edge is running the opposite direction in this face so flip
 
2330
        // the array to the correct direction
 
2331
 
 
2332
        if(verts[0] != v[start]) {flipvertarray(verts,3);}
 
2333
        end     = (start+1)%4;
 
2334
        left   = (start+2)%4;
 
2335
        right  = (start+3)%4; 
 
2336
 
 
2337
/*
 
2338
        We should have something like this now
 
2339
 
 
2340
                          end            start                           
 
2341
                           2     1     0   
 
2342
                           |-----*-----|
 
2343
                           |               |
 
2344
                           |               |       
 
2345
                           |               |
 
2346
                           -------------           
 
2347
                          left     right
 
2348
 
 
2349
        where start,end,left, right are indexes of EditFace->v1, etc (stored in v)
 
2350
        and 0,1,2 are the indexes of the new verts stored in verts. We fill like
 
2351
        this, depending on whether its vertex 'left' or vertex 'right' thats
 
2352
        been knifed through...
 
2353
                                
 
2354
                                |---*---|       |---*---|
 
2355
                                |  /    |       |    \  |
 
2356
                                | /             |       |         \ |
 
2357
                                |/              |       |          \|
 
2358
                                X--------       --------X
 
2359
*/
 
2360
 
 
2361
        if(v[left]->f1){
 
2362
                //triangle is composed of cutvert, end and left
 
2363
                hold = addfacelist(verts[1],v[end],v[left],NULL, NULL,NULL);
 
2364
                hold->e1->f2 |= EDGENEW;
 
2365
                hold->e2->f2 |= EDGENEW;
 
2366
                hold->e3->f2 |= EDGENEW;
 
2367
                hold->e3->f2 |= EDGEINNER;
 
2368
                facecopy(efa, hold);
 
2369
                
 
2370
                //quad is composed of cutvert, left, right and start
 
2371
                hold = addfacelist(verts[1],v[left],v[right],v[start], NULL, NULL);
 
2372
                hold->e1->f2 |= EDGENEW;
 
2373
                hold->e2->f2 |= EDGENEW;
 
2374
                hold->e3->f2 |= EDGENEW;
 
2375
                hold->e4->f2 |= EDGENEW;
 
2376
                hold->e1->f2 |= EDGEINNER;
 
2377
                facecopy(efa, hold);
 
2378
        }
 
2379
        else if(v[right]->f1){
 
2380
                //triangle is composed of cutvert, right and start
 
2381
                hold = addfacelist(verts[1],v[right],v[start], NULL, NULL, NULL);
 
2382
                hold->e1->f2 |= EDGENEW;
 
2383
                hold->e2->f2 |= EDGENEW;
 
2384
                hold->e3->f2 |= EDGENEW;
 
2385
                hold->e1->f2 |= EDGEINNER;
 
2386
                facecopy(efa, hold);
 
2387
                //quad is composed of cutvert, end, left, right
 
2388
                hold = addfacelist(verts[1],v[end], v[left], v[right], NULL, NULL);
 
2389
                hold->e1->f2 |= EDGENEW;
 
2390
                hold->e2->f2 |= EDGENEW;
 
2391
                hold->e3->f2 |= EDGENEW;
 
2392
                hold->e4->f2 |= EDGENEW;
 
2393
                hold->e4->f2 |= EDGEINNER;
 
2394
                facecopy(efa, hold);
 
2395
        }
 
2396
        
 
2397
}       
 
2398
 
2417
2399
// This function takes an example edge, the current point to create and 
2418
2400
// the total # of points to create, then creates the point and return the
2419
2401
// editvert pointer to it.
2423
2405
        float percent;
2424
2406
         
2425
2407
        if (beauty & (B_PERCENTSUBD) && totpoint == 1)
2426
 
                percent=(float)(edge->f1)/32768.0f;
 
2408
                //percent=(float)(edge->tmp.l)/32768.0f;
 
2409
                percent= edge->tmp.fp;
2427
2410
        else
2428
2411
                percent= (float)curpoint/(float)(totpoint+1);
2429
2412
 
2438
2421
        EditMesh *em = G.editMesh;
2439
2422
        EditFace *ef;
2440
2423
        EditEdge *eed, *cedge, *sort[4];
2441
 
        EditVert **templist;
 
2424
        EditVert *eve, **templist;
2442
2425
        struct GHash *gh;
2443
2426
        float length[4], v1mat[3], v2mat[3], v3mat[3], v4mat[3];
2444
 
        int i, j, edgecount, facetype,hold;
 
2427
        int i, j, edgecount, touchcount, facetype,hold;
 
2428
        ModifierData *md= G.obedit->modifiers.first;
2445
2429
        
 
2430
        if(multires_test()) return;
 
2431
 
2446
2432
        //Set faces f1 to 0 cause we need it later
2447
 
        for(ef=em->faces.first;ef;ef = ef->next) {
2448
 
                ef->f1 = 0;
 
2433
        for(ef=em->faces.first;ef;ef = ef->next) ef->f1 = 0;
 
2434
        for(eve=em->verts.first; eve; eve=eve->next) eve->f1 = eve->f2 = 0;
 
2435
 
 
2436
        for (; md; md=md->next) {
 
2437
                if (md->type==eModifierType_Mirror) {
 
2438
                        MirrorModifierData *mmd = (MirrorModifierData*) md;     
 
2439
                
 
2440
                        if(mmd->flag & MOD_MIR_CLIPPING) {
 
2441
                                for (eve= em->verts.first; eve; eve= eve->next) {
 
2442
                                        eve->f2= 0;
 
2443
                                        switch(mmd->axis){
 
2444
                                                case 0:
 
2445
                                                        if (fabs(eve->co[0]) < mmd->tolerance)
 
2446
                                                                eve->f2 |= 1;
 
2447
                                                        break;
 
2448
                                                case 1:
 
2449
                                                        if (fabs(eve->co[1]) < mmd->tolerance)
 
2450
                                                                eve->f2 |= 2;
 
2451
                                                        break;
 
2452
                                                case 2:
 
2453
                                                        if (fabs(eve->co[2]) < mmd->tolerance)
 
2454
                                                                eve->f2 |= 4;
 
2455
                                                        break;
 
2456
                                        }
 
2457
                                }
 
2458
                        }
 
2459
                }
2449
2460
        }
2450
2461
        
2451
2462
        //Flush vertex flags upward to the edges
2457
2468
                if(eed->f & flag) {
2458
2469
                        eed->f2 |= EDGEOLD;
2459
2470
                }
2460
 
        }   
2461
 
                
2462
 
 
 
2471
        }
 
2472
        
2463
2473
        // We store an array of verts for each edge that is subdivided,
2464
2474
        // we put this array as a value in a ghash which is keyed by the EditEdge*
2465
2475
 
2539
2549
        // If we are knifing, We only need the selected edges that were cut, so deselect if it was not cut
2540
2550
        if(beauty & B_KNIFE) {  
2541
2551
                for(eed= em->edges.first;eed;eed=eed->next) {   
2542
 
                        if( eed->f1 == 0 ) {
2543
 
                                EM_select_edge(eed,0);   
 
2552
                        if( eed->tmp.fp == 0 ) {
 
2553
                                EM_select_edge(eed,0);
2544
2554
                        }
2545
2555
                }
2546
2556
        }  
2583
2593
                }  
2584
2594
                if(facetype == 4) {
2585
2595
                        switch(edgecount) {
2586
 
                                case 0: break;
2587
 
                                case 1: ef->f1 = SELECT;
2588
 
                                        fill_quad_single(ef, gh, numcuts, seltype);
 
2596
                                case 0:
 
2597
                                        if(beauty & B_KNIFE && numcuts == 1){
 
2598
                                                /*Test for when knifing through two opposite verts but no edges*/
 
2599
                                                touchcount = 0;
 
2600
                                                if(ef->v1->f1) touchcount++;
 
2601
                                                if(ef->v2->f1) touchcount++;
 
2602
                                                if(ef->v3->f1) touchcount++;
 
2603
                                                if(ef->v4->f1) touchcount++;
 
2604
                                                if(touchcount == 2){
 
2605
                                                        if(ef->v1->f1 && ef->v3->f1){ 
 
2606
                                                                ef->f1 = SELECT;
 
2607
                                                                fill_quad_doublevert(ef, 1, 3); 
 
2608
                                                        }
 
2609
                                                        else if(ef->v2->f1 && ef->v4->f1){
 
2610
                                                                ef->f1 = SELECT;
 
2611
                                                                fill_quad_doublevert(ef, 2, 4);
 
2612
                                                        }
 
2613
                                                }
 
2614
                                        }
 
2615
                                        break; 
 
2616
                                
 
2617
                                case 1: 
 
2618
                                        if(beauty & B_KNIFE && numcuts == 1){
 
2619
                                                /*Test for when knifing through an edge and one vert*/
 
2620
                                                touchcount = 0;
 
2621
                                                if(ef->v1->f1) touchcount++;
 
2622
                                                if(ef->v2->f1) touchcount++;
 
2623
                                                if(ef->v3->f1) touchcount++;
 
2624
                                                if(ef->v4->f1) touchcount++;
 
2625
                                                
 
2626
                                                if(touchcount == 1){
 
2627
                                                        if( (ef->e1->f & flag && ( !ef->e1->v1->f1 && !ef->e1->v2->f1 )) ||
 
2628
                                                                (ef->e2->f & flag && ( !ef->e2->v1->f1 && !ef->e2->v2->f1 )) ||
 
2629
                                                                (ef->e3->f & flag && ( !ef->e3->v1->f1 && !ef->e3->v2->f1 )) ||
 
2630
                                                                (ef->e4->f & flag && ( !ef->e4->v1->f1 && !ef->e4->v2->f1 )) ){
 
2631
                                                                
 
2632
                                                                ef->f1 = SELECT; 
 
2633
                                                                fill_quad_singlevert(ef, gh);
 
2634
                                                        }
 
2635
                                                        else{
 
2636
                                                                ef->f1 = SELECT;
 
2637
                                                                fill_quad_single(ef, gh, numcuts, seltype);
 
2638
                                                        }
 
2639
                                                }
 
2640
                                                else{ 
 
2641
                                                        ef->f1 = SELECT; 
 
2642
                                                        fill_quad_single(ef, gh, numcuts, seltype);
 
2643
                                                }
 
2644
                                        }
 
2645
                                        else{ 
 
2646
                                                ef->f1 = SELECT;
 
2647
                                                fill_quad_single(ef, gh, numcuts, seltype);
 
2648
                                        }
2589
2649
                                        break;   
2590
2650
                                case 2: ef->f1 = SELECT;
2591
2651
                                        // if there are 2, we check if edge 1 and 3 are either both on or off that way
2625
2685
                }       
2626
2686
        }
2627
2687
        
2628
 
        // Delete Old Faces
2629
 
        free_tagged_facelist(em->faces.first);   
2630
 
        //Delete Old Edges
 
2688
        // Delete Old Edges and Faces
2631
2689
        for(eed = em->edges.first;eed;eed = eed->next) {
2632
2690
                if(BLI_ghash_haskey(gh,eed)) {
2633
2691
                        eed->f1 = SELECT; 
2635
2693
                        eed->f1 = 0;   
2636
2694
                }
2637
2695
        } 
2638
 
        free_tagged_edgelist(em->edges.first); 
 
2696
        free_tagged_edges_faces(em->edges.first, em->faces.first); 
2639
2697
        
2640
2698
        if(seltype == SUBDIV_SELECT_ORIG  && G.qual  != LR_CTRLKEY) {
2641
2699
                for(eed = em->edges.first;eed;eed = eed->next) {
2679
2737
                        }
2680
2738
                }       
2681
2739
        }
 
2740
        
 
2741
        //fix hide flags for edges. First pass, hide edges of hidden faces
 
2742
        for(ef=em->faces.first; ef; ef=ef->next){
 
2743
                if(ef->h){
 
2744
                        ef->e1->h |= 1;
 
2745
                        ef->e2->h |= 1;
 
2746
                        ef->e3->h |= 1;
 
2747
                        if(ef->e4) ef->e4->h |= 1;
 
2748
                }
 
2749
        }
 
2750
        //second pass: unhide edges of visible faces adjacent to hidden faces
 
2751
        for(ef=em->faces.first; ef; ef=ef->next){
 
2752
                if(ef->h == 0){
 
2753
                        ef->e1->h &= ~1;
 
2754
                        ef->e2->h &= ~1;
 
2755
                        ef->e3->h &= ~1;
 
2756
                        if(ef->e4) ef->e4->h &= ~1;
 
2757
                }
 
2758
        }
 
2759
        
2682
2760
        // Free the ghash and call MEM_freeN on all the value entries to return 
2683
2761
        // that memory
2684
2762
        BLI_ghash_free(gh, NULL, (GHashValFreeFP)MEM_freeN);   
2751
2829
 
2752
2830
        while(efa) {
2753
2831
                efa->f1= 0;
2754
 
                if(efa->v4==0) {  /* if triangle */
2755
 
                        if(efa->f & SELECT) {
2756
 
                                
2757
 
                                e1= efa->e1;
2758
 
                                e2= efa->e2;
2759
 
                                e3= efa->e3;
2760
 
                                if(e1->f2<3 && e1->tmp.p) {
2761
 
                                        if(e1->f2<2) {
2762
 
                                                evp= (EVPtr *) e1->tmp.p;
2763
 
                                                evp[(int)e1->f2] = efa;
2764
 
                                        }
2765
 
                                        e1->f2+= 1;
2766
 
                                }
2767
 
                                if(e2->f2<3 && e2->tmp.p) {
2768
 
                                        if(e2->f2<2) {
2769
 
                                                evp= (EVPtr *) e2->tmp.p;
2770
 
                                                evp[(int)e2->f2]= efa;
2771
 
                                        }
2772
 
                                        e2->f2+= 1;
2773
 
                                }
2774
 
                                if(e3->f2<3 && e3->tmp.p) {
2775
 
                                        if(e3->f2<2) {
2776
 
                                                evp= (EVPtr *) e3->tmp.p;
2777
 
                                                evp[(int)e3->f2]= efa;
2778
 
                                        }
2779
 
                                        e3->f2+= 1;
2780
 
                                }
2781
 
                        }
2782
 
                }
 
2832
                if(efa->v4==0 && (efa->f & SELECT)) {  /* if selected triangle */
 
2833
                        e1= efa->e1;
 
2834
                        e2= efa->e2;
 
2835
                        e3= efa->e3;
 
2836
                        if(e1->f2<3 && e1->tmp.p) {
 
2837
                                if(e1->f2<2) {
 
2838
                                        evp= (EVPtr *) e1->tmp.p;
 
2839
                                        evp[(int)e1->f2] = efa;
 
2840
                                }
 
2841
                                e1->f2+= 1;
 
2842
                        }
 
2843
                        if(e2->f2<3 && e2->tmp.p) {
 
2844
                                if(e2->f2<2) {
 
2845
                                        evp= (EVPtr *) e2->tmp.p;
 
2846
                                        evp[(int)e2->f2]= efa;
 
2847
                                }
 
2848
                                e2->f2+= 1;
 
2849
                        }
 
2850
                        if(e3->f2<3 && e3->tmp.p) {
 
2851
                                if(e3->f2<2) {
 
2852
                                        evp= (EVPtr *) e3->tmp.p;
 
2853
                                        evp[(int)e3->f2]= efa;
 
2854
                                }
 
2855
                                e3->f2+= 1;
 
2856
                        }
 
2857
                }
 
2858
                else {
 
2859
                        /* set to 3 to make sure these are not flipped or joined */
 
2860
                        efa->e1->f2= 3;
 
2861
                        efa->e2->f2= 3;
 
2862
                        efa->e3->f2= 3;
 
2863
                        if (efa->e4) efa->e4->f2= 3;
 
2864
                }
 
2865
 
2783
2866
                efa= efa->next;
2784
2867
        }
2785
2868
        return i;
2801
2884
#define VTEST(face, num, other) \
2802
2885
        (face->v##num != other->v1 && face->v##num != other->v2 && face->v##num != other->v3) 
2803
2886
 
2804
 
static void givequadverts(EditFace *efa, EditFace *efa1, EditVert **v1, EditVert **v2, EditVert **v3, EditVert **v4, float **uv, unsigned int *col)
 
2887
static void givequadverts(EditFace *efa, EditFace *efa1, EditVert **v1, EditVert **v2, EditVert **v3, EditVert **v4, int *vindex)
2805
2888
{
2806
2889
        if VTEST(efa, 1, efa1) {
2807
 
        //if(efa->v1!=efa1->v1 && efa->v1!=efa1->v2 && efa->v1!=efa1->v3) {
2808
2890
                *v1= efa->v1;
2809
2891
                *v2= efa->v2;
2810
 
                uv[0] = efa->tf.uv[0];
2811
 
                uv[1] = efa->tf.uv[1];
2812
 
                col[0] = efa->tf.col[0];
2813
 
                col[1] = efa->tf.col[1];
 
2892
                vindex[0]= 0;
 
2893
                vindex[1]= 1;
2814
2894
        }
2815
2895
        else if VTEST(efa, 2, efa1) {
2816
 
        //else if(efa->v2!=efa1->v1 && efa->v2!=efa1->v2 && efa->v2!=efa1->v3) {
2817
2896
                *v1= efa->v2;
2818
2897
                *v2= efa->v3;
2819
 
                uv[0] = efa->tf.uv[1];
2820
 
                uv[1] = efa->tf.uv[2];
2821
 
                col[0] = efa->tf.col[1];
2822
 
                col[1] = efa->tf.col[2];
 
2898
                vindex[0]= 1;
 
2899
                vindex[1]= 2;
2823
2900
        }
2824
2901
        else if VTEST(efa, 3, efa1) {
2825
 
        // else if(efa->v3!=efa1->v1 && efa->v3!=efa1->v2 && efa->v3!=efa1->v3) {
2826
2902
                *v1= efa->v3;
2827
2903
                *v2= efa->v1;
2828
 
                uv[0] = efa->tf.uv[2];
2829
 
                uv[1] = efa->tf.uv[0];
2830
 
                col[0] = efa->tf.col[2];
2831
 
                col[1] = efa->tf.col[0];
 
2904
                vindex[0]= 2;
 
2905
                vindex[1]= 0;
2832
2906
        }
2833
2907
        
2834
2908
        if VTEST(efa1, 1, efa) {
2835
 
        // if(efa1->v1!=efa->v1 && efa1->v1!=efa->v2 && efa1->v1!=efa->v3) {
2836
2909
                *v3= efa1->v1;
2837
 
                uv[2] = efa1->tf.uv[0];
2838
 
                col[2] = efa1->tf.col[0];
2839
 
 
2840
2910
                *v4= efa1->v2;
2841
 
                uv[3] = efa1->tf.uv[1];
2842
 
                col[3] = efa1->tf.col[1];
2843
 
/*
2844
 
if(efa1->v2== *v2) {
2845
 
                        *v4= efa1->v3;
2846
 
                        uv[3] = efa1->tf.uv[2];
2847
 
                } else {
2848
 
                        *v4= efa1->v2;
2849
 
                        uv[3] = efa1->tf.uv[1];
2850
 
                }       
2851
 
                */
 
2911
                vindex[2]= 0;
 
2912
                vindex[3]= 1;
2852
2913
        }
2853
2914
        else if VTEST(efa1, 2, efa) {
2854
 
        // else if(efa1->v2!=efa->v1 && efa1->v2!=efa->v2 && efa1->v2!=efa->v3) {
2855
2915
                *v3= efa1->v2;
2856
 
                uv[2] = efa1->tf.uv[1];
2857
 
                col[2] = efa1->tf.col[1];
2858
 
 
2859
2916
                *v4= efa1->v3;
2860
 
                uv[3] = efa1->tf.uv[2];
2861
 
                col[3] = efa1->tf.col[2];
2862
 
/*
2863
 
if(efa1->v3== *v2) {
2864
 
                        *v4= efa1->v1;
2865
 
                        uv[3] = efa1->tf.uv[0];
2866
 
                } else {        
2867
 
                        *v4= efa1->v3;
2868
 
                        uv[3] = efa1->tf.uv[2];
2869
 
                }       
2870
 
                */
 
2917
                vindex[2]= 1;
 
2918
                vindex[3]= 2;
2871
2919
        }
2872
2920
        else if VTEST(efa1, 3, efa) {
2873
 
        // else if(efa1->v3!=efa->v1 && efa1->v3!=efa->v2 && efa1->v3!=efa->v3) {
2874
2921
                *v3= efa1->v3;
2875
 
                uv[2] = efa1->tf.uv[2];
2876
 
                col[2] = efa1->tf.col[2];
2877
 
 
2878
2922
                *v4= efa1->v1;
2879
 
                uv[3] = efa1->tf.uv[0];
2880
 
                col[3] = efa1->tf.col[0];
2881
 
/*
2882
 
if(efa1->v1== *v2) {
2883
 
                        *v4= efa1->v2;
2884
 
                        uv[3] = efa1->tf.uv[3];
2885
 
                } else {        
2886
 
                        *v4= efa1->v1;
2887
 
                        uv[3] = efa1->tf.uv[0];
2888
 
                }       
2889
 
                */
 
2923
                vindex[2]= 2;
 
2924
                vindex[3]= 0;
2890
2925
        }
2891
 
        else {
 
2926
        else
2892
2927
                *v3= *v4= NULL;
2893
 
                
2894
 
                return;
2895
 
        }
2896
 
        
2897
2928
}
2898
2929
 
2899
2930
/* Helper functions for edge/quad edit features*/
2900
2931
static void untag_edges(EditFace *f)
2901
2932
{
2902
 
        f->e1->f2 = 0;
2903
 
        f->e2->f2 = 0;
2904
 
        if (f->e3) f->e3->f2 = 0;
2905
 
        if (f->e4) f->e4->f2 = 0;
 
2933
        f->e1->f1 = 0;
 
2934
        f->e2->f1 = 0;
 
2935
        f->e3->f1 = 0;
 
2936
        if (f->e4) f->e4->f1 = 0;
2906
2937
}
2907
2938
 
2908
 
/** remove and free list of tagged edges */
2909
 
static void free_tagged_edgelist(EditEdge *eed)
 
2939
/** remove and free list of tagged edges and faces */
 
2940
static void free_tagged_edges_faces(EditEdge *eed, EditFace *efa)
2910
2941
{
 
2942
        EditMesh *em= G.editMesh;
2911
2943
        EditEdge *nexted;
2912
 
 
2913
 
        while(eed) {
2914
 
                nexted= eed->next;
2915
 
                if(eed->f1) {
2916
 
                        remedge(eed);
2917
 
                        free_editedge(eed);
2918
 
                }
2919
 
                eed= nexted;
2920
 
        }       
2921
 
}       
2922
 
/** remove and free list of tagged faces */
2923
 
 
2924
 
static void free_tagged_facelist(EditFace *efa)
2925
 
{       
2926
 
        EditMesh *em = G.editMesh;
2927
2944
        EditFace *nextvl;
2928
2945
 
2929
2946
        while(efa) {
2932
2949
                        BLI_remlink(&em->faces, efa);
2933
2950
                        free_editface(efa);
2934
2951
                }
 
2952
                else
 
2953
                        /* avoid deleting edges that are still in use */
 
2954
                        untag_edges(efa);
2935
2955
                efa= nextvl;
2936
2956
        }
 
2957
 
 
2958
        while(eed) {
 
2959
                nexted= eed->next;
 
2960
                if(eed->f1) {
 
2961
                        remedge(eed);
 
2962
                        free_editedge(eed);
 
2963
                }
 
2964
                eed= nexted;
 
2965
        }       
2937
2966
}       
2938
2967
 
2939
2968
/* note; the EM_selectmode_set() calls here illustrate how badly constructed it all is... from before the
2948
2977
        // void **efaar, **efaa;
2949
2978
        EVPTuple *efaar;
2950
2979
        EVPtr *efaa;
2951
 
        float *uv[4];
2952
 
        unsigned int col[4];
2953
2980
        float len1, len2, len3, len4, len5, len6, opp1, opp2, fac1, fac2;
2954
 
        int totedge, ok, notbeauty=8, onedone;
 
2981
        int totedge, ok, notbeauty=8, onedone, vindex[4];
 
2982
        
 
2983
        if(multires_test()) return;
2955
2984
 
2956
2985
        /* - all selected edges with two faces
2957
2986
                * - find the faces: store them in edges (using datablock)
2965
2994
        totedge = count_selected_edges(em->edges.first);
2966
2995
        if(totedge==0) return;
2967
2996
 
2968
 
        if(okee("Beautify fill")==0) return;
2969
 
        
2970
2997
        /* temp block with face pointers */
2971
2998
        efaar= (EVPTuple *) MEM_callocN(totedge * sizeof(EVPTuple), "beautyfill");
2972
2999
 
2998
3025
                                
2999
3026
                                if(ok) {
3000
3027
                                        /* test convex */
3001
 
                                        givequadverts(efaa[0], efaa[1], &v1, &v2, &v3, &v4, uv, col);
 
3028
                                        givequadverts(efaa[0], efaa[1], &v1, &v2, &v3, &v4, vindex);
3002
3029
                                        if(v1 && v2 && v3 && v4) {
3003
3030
                                                if( convex(v1->co, v2->co, v3->co, v4->co) ) {
3004
3031
 
3051
3078
                                                                        efa= efaa[1];
3052
3079
                                                                        efa->f1= 1;
3053
3080
 
3054
 
                                                                        w= addfacelist(v1, v2, v3, 0, efa, NULL);
3055
 
                                                                        w->f |= SELECT;
3056
 
                                                                        
3057
 
                                                                        UVCOPY(w->tf.uv[0], uv[0]);
3058
 
                                                                        UVCOPY(w->tf.uv[1], uv[1]);
3059
 
                                                                        UVCOPY(w->tf.uv[2], uv[2]);
3060
 
 
3061
 
                                                                        w->tf.col[0] = col[0]; w->tf.col[1] = col[1]; w->tf.col[2] = col[2];
3062
 
                                                                        w= addfacelist(v1, v3, v4, 0, efa, NULL);
3063
 
                                                                        w->f |= SELECT;
3064
 
 
3065
 
                                                                        UVCOPY(w->tf.uv[0], uv[0]);
3066
 
                                                                        UVCOPY(w->tf.uv[1], uv[2]);
3067
 
                                                                        UVCOPY(w->tf.uv[2], uv[3]);
3068
 
 
3069
 
                                                                        w->tf.col[0] = col[0]; w->tf.col[1] = col[2]; w->tf.col[2] = col[3];
 
3081
                                                                        w= EM_face_from_faces(efaa[0], efaa[1],
 
3082
                                                                                vindex[0], vindex[1], 4+vindex[2], -1);
 
3083
                                                                        w->f |= SELECT;
 
3084
 
 
3085
 
 
3086
                                                                        w= EM_face_from_faces(efaa[0], efaa[1],
 
3087
                                                                                vindex[0], 4+vindex[2], 4+vindex[3], -1);
 
3088
                                                                        w->f |= SELECT;
3070
3089
 
3071
3090
                                                                        onedone= 1;
3072
3091
                                                                }
3079
3098
                                                                        efa= efaa[1];
3080
3099
                                                                        efa->f1= 1;
3081
3100
 
3082
 
                                                                        w= addfacelist(v2, v3, v4, 0, efa, NULL);
3083
 
                                                                        w->f |= SELECT;
3084
 
 
3085
 
                                                                        UVCOPY(w->tf.uv[0], uv[1]);
3086
 
                                                                        UVCOPY(w->tf.uv[1], uv[3]);
3087
 
                                                                        UVCOPY(w->tf.uv[2], uv[4]);
3088
 
 
3089
 
                                                                        w= addfacelist(v1, v2, v4, 0, efa, NULL);
3090
 
                                                                        w->f |= SELECT;
3091
 
 
3092
 
                                                                        UVCOPY(w->tf.uv[0], uv[0]);
3093
 
                                                                        UVCOPY(w->tf.uv[1], uv[1]);
3094
 
                                                                        UVCOPY(w->tf.uv[2], uv[3]);
 
3101
 
 
3102
                                                                        w= EM_face_from_faces(efaa[0], efaa[1],
 
3103
                                                                                vindex[1], 4+vindex[2], 4+vindex[3], -1);
 
3104
                                                                        w->f |= SELECT;
 
3105
 
 
3106
 
 
3107
                                                                        w= EM_face_from_faces(efaa[0], efaa[1],
 
3108
                                                                                vindex[0], 4+vindex[1], 4+vindex[3], -1);
 
3109
                                                                        w->f |= SELECT;
3095
3110
 
3096
3111
                                                                        onedone= 1;
3097
3112
                                                                }
3104
3119
                        eed= nexted;
3105
3120
                }
3106
3121
 
3107
 
                free_tagged_edgelist(em->edges.first);
3108
 
                free_tagged_facelist(em->faces.first);
 
3122
                free_tagged_edges_faces(em->edges.first, em->faces.first);
3109
3123
 
3110
3124
                if(onedone==0) break;
3111
3125
                
3118
3132
        
3119
3133
        allqueue(REDRAWVIEW3D, 0);
3120
3134
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
3135
#ifdef WITH_VERSE
 
3136
        if(G.editMesh->vnode)
 
3137
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
3138
#endif
3121
3139
        BIF_undo_push("Beauty Fill");
3122
3140
}
3123
3141
 
3124
3142
 
3125
 
/* ******************** FLIP EDGE ************************************* */
3126
 
 
3127
 
 
3128
 
#define FACE_MARKCLEAR(f) (f->f1 = 1)
3129
 
 
 
3143
/* ******************** BEGIN TRIANGLE TO QUAD ************************************* */
 
3144
static float measure_facepair(EditVert *v1, EditVert *v2, EditVert *v3, EditVert *v4, float limit){
 
3145
        
 
3146
        /*gives a 'weight' to a pair of triangles that join an edge to decide how good a join they would make*/
 
3147
        /*Note: this is more complicated than it needs to be and should be cleaned up...*/
 
3148
        float   measure = 0.0, noA1[3], noA2[3], noB1[3], noB2[3], normalADiff, normalBDiff,
 
3149
                        edgeVec1[3], edgeVec2[3], edgeVec3[3], edgeVec4[3], diff,
 
3150
                        minarea, maxarea, areaA, areaB;
 
3151
        
 
3152
        /*First Test: Normal difference*/
 
3153
        CalcNormFloat(v1->co, v2->co, v3->co, noA1);
 
3154
        CalcNormFloat(v1->co, v3->co, v4->co, noA2);
 
3155
        
 
3156
        if(noA1[0] == noA2[0] && noA1[1] == noA2[1] && noA1[2] == noA2[2]) normalADiff = 0.0;
 
3157
        else normalADiff = VecAngle2(noA1, noA2);
 
3158
                //if(!normalADiff) normalADiff = 179;
 
3159
        CalcNormFloat(v2->co, v3->co, v4->co, noB1);
 
3160
        CalcNormFloat(v4->co, v1->co, v2->co, noB2);
 
3161
        
 
3162
        if(noB1[0] == noB2[0] && noB1[1] == noB2[1] && noB1[2] == noB2[2]) normalBDiff = 0.0;
 
3163
        else normalBDiff = VecAngle2(noB1, noB2);
 
3164
                //if(!normalBDiff) normalBDiff = 179;
 
3165
        
 
3166
        measure += (normalADiff/360) + (normalBDiff/360);
 
3167
        if(measure > limit) return measure;
 
3168
        
 
3169
        /*Second test: Colinearity*/
 
3170
        VecSubf(edgeVec1, v1->co, v2->co);
 
3171
        VecSubf(edgeVec2, v2->co, v3->co);
 
3172
        VecSubf(edgeVec3, v3->co, v4->co);
 
3173
        VecSubf(edgeVec4, v4->co, v1->co);
 
3174
        
 
3175
        diff = 0.0;
 
3176
        
 
3177
        diff = (
 
3178
                fabs(VecAngle2(edgeVec1, edgeVec2) - 90) +
 
3179
                fabs(VecAngle2(edgeVec2, edgeVec3) - 90) + 
 
3180
                fabs(VecAngle2(edgeVec3, edgeVec4) - 90) + 
 
3181
                fabs(VecAngle2(edgeVec4, edgeVec1) - 90)) / 360;
 
3182
        if(!diff) return 0.0;
 
3183
        
 
3184
        measure +=  diff;
 
3185
        if(measure > limit) return measure;
 
3186
 
 
3187
        /*Third test: Concavity*/
 
3188
        areaA = AreaT3Dfl(v1->co, v2->co, v3->co) + AreaT3Dfl(v1->co, v3->co, v4->co);
 
3189
        areaB = AreaT3Dfl(v2->co, v3->co, v4->co) + AreaT3Dfl(v4->co, v1->co, v2->co);
 
3190
        
 
3191
        if(areaA <= areaB) minarea = areaA;
 
3192
        else minarea = areaB;
 
3193
        
 
3194
        if(areaA >= areaB) maxarea = areaA;
 
3195
        else maxarea = areaB;
 
3196
        
 
3197
        if(!maxarea) measure += 1;
 
3198
        else measure += (1 - (minarea / maxarea));
 
3199
 
 
3200
        return measure;
 
3201
}
 
3202
 
 
3203
#define T2QUV_LIMIT 0.005
 
3204
#define T2QCOL_LIMIT 3
 
3205
static int compareFaceAttribs(EditFace *f1, EditFace *f2, EditEdge *eed) 
 
3206
{
 
3207
        /*Test to see if the per-face attributes for the joining edge match within limit*/      
 
3208
        MTFace *tf1, *tf2;
 
3209
        unsigned int *col1, *col2;
 
3210
        short i,attrok=0, flag = G.scene->toolsettings->editbutflag, fe1[2], fe2[2];
 
3211
        
 
3212
        tf1 = CustomData_em_get(&G.editMesh->fdata, f1->data, CD_MTFACE);
 
3213
        tf2 = CustomData_em_get(&G.editMesh->fdata, f2->data, CD_MTFACE);
 
3214
 
 
3215
        col1 = CustomData_em_get(&G.editMesh->fdata, f1->data, CD_MCOL);
 
3216
        col2 = CustomData_em_get(&G.editMesh->fdata, f2->data, CD_MCOL);
 
3217
        
 
3218
        /*store indices for faceedges*/
 
3219
        f1->v1->f1 = 0;
 
3220
        f1->v2->f1 = 1;
 
3221
        f1->v3->f1 = 2;
 
3222
        
 
3223
        fe1[0] = eed->v1->f1;
 
3224
        fe1[1] = eed->v2->f1;
 
3225
        
 
3226
        f2->v1->f1 = 0;
 
3227
        f2->v2->f1 = 1;
 
3228
        f2->v3->f1 = 2;
 
3229
        
 
3230
        fe2[0] = eed->v1->f1;
 
3231
        fe2[1] = eed->v2->f1;
 
3232
        
 
3233
        /*compare faceedges for each face attribute. Additional per face attributes can be added later*/
 
3234
        /*do UVs*/
 
3235
        if(flag & B_JOINTRIA_UV){
 
3236
                
 
3237
                if(tf1 == NULL || tf2 == NULL) attrok |= B_JOINTRIA_UV;
 
3238
                else if(tf1->tpage != tf2->tpage); /*do nothing*/
 
3239
                else{
 
3240
                        for(i = 0; i < 2; i++){
 
3241
                                if(tf1->uv[fe1[i]][0] + T2QUV_LIMIT > tf2->uv[fe2[i]][0] && tf1->uv[fe1[i]][0] - T2QUV_LIMIT < tf2->uv[fe2[i]][0] &&
 
3242
                                        tf1->uv[fe1[i]][1] + T2QUV_LIMIT > tf2->uv[fe2[i]][1] && tf1->uv[fe1[i]][1] - T2QUV_LIMIT < tf2->uv[fe2[i]][1]) attrok |= B_JOINTRIA_UV;
 
3243
                        }
 
3244
                }
 
3245
        }
 
3246
        
 
3247
        /*do VCOLs*/
 
3248
        if(flag & B_JOINTRIA_VCOL){
 
3249
                if(!col1 || !col2) attrok |= B_JOINTRIA_VCOL;
 
3250
                else{
 
3251
                        char *f1vcol, *f2vcol;
 
3252
                        for(i = 0; i < 2; i++){
 
3253
                                f1vcol = (char *)&(col1[fe1[i]]);
 
3254
                                f2vcol = (char *)&(col2[fe2[i]]);
 
3255
                
 
3256
                                /*compare f1vcol with f2vcol*/
 
3257
                                if(     f1vcol[1] + T2QCOL_LIMIT > f2vcol[1] && f1vcol[1] - T2QCOL_LIMIT < f2vcol[1] &&
 
3258
                                        f1vcol[2] + T2QCOL_LIMIT > f2vcol[2] && f1vcol[2] - T2QCOL_LIMIT < f2vcol[2] &&
 
3259
                                        f1vcol[3] + T2QCOL_LIMIT > f2vcol[3] && f1vcol[3] - T2QCOL_LIMIT < f2vcol[3]) attrok |= B_JOINTRIA_VCOL;
 
3260
                        }
 
3261
                }
 
3262
        }
 
3263
        
 
3264
        if( ((attrok & B_JOINTRIA_UV) == (flag & B_JOINTRIA_UV)) && ((attrok & B_JOINTRIA_VCOL) == (flag & B_JOINTRIA_VCOL)) ) return 1;
 
3265
        return 0;
 
3266
}       
 
3267
        
 
3268
static int fplcmp(const void *v1, const void *v2)
 
3269
{
 
3270
        const EditEdge *e1= *((EditEdge**)v1), *e2=*((EditEdge**)v2);
 
3271
        
 
3272
        if( e1->crease > e2->crease) return 1;
 
3273
        else if( e1->crease < e2->crease) return -1;
 
3274
        
 
3275
        return 0;
 
3276
}
 
3277
 
 
3278
/*Bitflags for edges.*/
 
3279
#define T2QDELETE       1
 
3280
#define T2QCOMPLEX      2
 
3281
#define T2QJOIN         4
3130
3282
void join_triangles(void)
3131
3283
{
3132
 
        EditMesh *em = G.editMesh;
3133
 
        EditVert *v1, *v2, *v3, *v4;
3134
 
        EditFace *efa, *w;
3135
 
        EVPTuple *efaar;
3136
 
        EVPtr *efaa;
3137
 
        EditEdge *eed, *nexted;
3138
 
        int totedge, ok;
3139
 
        float *uv[4];
3140
 
        unsigned int col[4];
3141
 
 
3142
 
        EM_selectmode_flush();  // makes sure in selectmode 'face' the edges of selected faces are selected too 
3143
 
        
3144
 
        totedge = count_selected_edges(em->edges.first);
3145
 
        if(totedge==0) return;
3146
 
 
3147
 
        efaar= (EVPTuple *) MEM_callocN(totedge * sizeof(EVPTuple), "jointris");
3148
 
 
 
3284
        EditMesh *em=G.editMesh;
 
3285
        EditVert *v1, *v2, *v3, *v4, *eve;
 
3286
        EditEdge *eed, **edsortblock = NULL, **edb = NULL;
 
3287
        EditFace *efa;
 
3288
        EVPTuple *efaar = NULL;
 
3289
        EVPtr *efaa = NULL;
 
3290
        float *creases = NULL;
 
3291
        float measure; /*Used to set tolerance*/
 
3292
        float limit = G.scene->toolsettings->jointrilimit;
 
3293
        int i, ok, totedge=0, totseledge=0, complexedges, vindex[4];
 
3294
        
 
3295
        /*test for multi-resolution data*/
 
3296
        if(multires_test()) return;
 
3297
 
 
3298
        /*if we take a long time on very dense meshes we want waitcursor to display*/
 
3299
        waitcursor(1);
 
3300
        
 
3301
        totseledge = count_selected_edges(em->edges.first);
 
3302
        if(totseledge==0) return;
 
3303
        
 
3304
        /*abusing crease value to store weights for edge pairs. Nasty*/
 
3305
        for(eed=em->edges.first; eed; eed=eed->next) totedge++;
 
3306
        if(totedge) creases = MEM_callocN(sizeof(float) * totedge, "Join Triangles Crease Array"); 
 
3307
        for(eed=em->edges.first, i = 0; eed; eed=eed->next, i++){
 
3308
                creases[i] = eed->crease; 
 
3309
                eed->crease = 0.0;
 
3310
        }
 
3311
        
 
3312
        /*clear temp flags*/
 
3313
        for(eve=em->verts.first; eve; eve=eve->next) eve->f1 = eve->f2 = 0;
 
3314
        for(eed=em->edges.first; eed; eed=eed->next) eed->f2 = eed->f1 = 0;
 
3315
        for(efa=em->faces.first; efa; efa=efa->next) efa->f1 = efa->tmp.l = 0;
 
3316
 
 
3317
        /*For every selected 2 manifold edge, create pointers to its two faces.*/
 
3318
        efaar= (EVPTuple *) MEM_callocN(totseledge * sizeof(EVPTuple), "Tri2Quad");
3149
3319
        ok = collect_quadedges(efaar, em->edges.first, em->faces.first);
3150
 
        if (G.f & G_DEBUG) {
3151
 
                printf("Edges selected: %d\n", ok);
3152
 
        }       
3153
 
 
3154
 
        eed= em->edges.first;
3155
 
        while(eed) {
3156
 
                nexted= eed->next;
3157
 
                
3158
 
                if(eed->f2==2) {  /* points to 2 faces */
3159
 
                        
3160
 
                        efaa= (EVPtr *) eed->tmp.p;
3161
 
                        
3162
 
                        /* don't do it if flagged */
3163
 
 
3164
 
                        ok= 1;
3165
 
                        efa= efaa[0];
3166
 
                        if(efa->e1->f1 || efa->e2->f1 || efa->e3->f1) ok= 0;
3167
 
                        efa= efaa[1];
3168
 
                        if(efa->e1->f1 || efa->e2->f1 || efa->e3->f1) ok= 0;
3169
 
                        
3170
 
                        if(ok) {
3171
 
                                /* test convex */
3172
 
                                givequadverts(efaa[0], efaa[1], &v1, &v2, &v3, &v4, uv, col);
3173
 
 
3174
 
/*
3175
 
                4-----3         4-----3
3176
 
                |\      |               |        |
3177
 
                | \ 1 |         |        |
3178
 
                |  \  |  ->     |        |      
3179
 
                | 0 \ |         |        | 
3180
 
                |       \|              |        |
3181
 
                1-----2         1-----2
3182
 
*/
3183
 
                                /* make new faces */
3184
 
                                if(v1 && v2 && v3 && v4) {
3185
 
                                        if( convex(v1->co, v2->co, v3->co, v4->co) ) {
3186
 
                                                if(exist_face(v1, v2, v3, v4)==0) {
3187
 
                                                        w = addfacelist(v1, v2, v3, v4, efaa[0], NULL); /* seam edge may get broken */
3188
 
                                                        w->f= efaa[0]->f;       /* copy selection flag */
3189
 
                                                        untag_edges(w);
3190
 
 
3191
 
                                                        UVCOPY(w->tf.uv[0], uv[0]);
3192
 
                                                        UVCOPY(w->tf.uv[1], uv[1]);
3193
 
                                                        UVCOPY(w->tf.uv[2], uv[2]);
3194
 
                                                        UVCOPY(w->tf.uv[3], uv[3]);
3195
 
 
3196
 
                                                        memcpy(w->tf.col, col, sizeof(w->tf.col));
3197
 
                                                }
3198
 
                                                /* tag as to-be-removed */
3199
 
                                                FACE_MARKCLEAR(efaa[0]);
3200
 
                                                FACE_MARKCLEAR(efaa[1]);
3201
 
                                                eed->f1 = 1; 
3202
 
                                        } /* endif test convex */
3203
 
                                }
3204
 
                        }
3205
 
                }
3206
 
                eed= nexted;
3207
 
        }
3208
 
        free_tagged_edgelist(em->edges.first);
3209
 
        free_tagged_facelist(em->faces.first);
3210
 
 
3211
 
        MEM_freeN(efaar);
3212
 
        
 
3320
        complexedges = 0;
 
3321
        
 
3322
        if(ok){
 
3323
                
 
3324
                
 
3325
                /*clear tmp.l flag and store number of faces that are selected and coincident to current face here.*/  
 
3326
                for(eed=em->edges.first; eed; eed=eed->next){
 
3327
                        /* eed->f2 is 2 only if this edge is part of exactly two
 
3328
                           triangles, and both are selected, and it has EVPTuple assigned */
 
3329
                        if(eed->f2 == 2){
 
3330
                                efaa= (EVPtr *) eed->tmp.p;
 
3331
                                efaa[0]->tmp.l++;
 
3332
                                efaa[1]->tmp.l++;
 
3333
                        }
 
3334
                }
 
3335
                
 
3336
                for(eed=em->edges.first; eed; eed=eed->next){
 
3337
                        if(eed->f2 == 2){
 
3338
                                efaa= (EVPtr *) eed->tmp.p;
 
3339
                                v1 = v2 = v3 = v4 = NULL;
 
3340
                                givequadverts(efaa[0], efaa[1], &v1, &v2, &v3, &v4, vindex);
 
3341
                                if(v1 && v2 && v3 && v4){
 
3342
                                        /*test if simple island first. This mimics 2.42 behaviour and the tests are less restrictive.*/
 
3343
                                        if(efaa[0]->tmp.l == 1 && efaa[1]->tmp.l == 1){
 
3344
                                                if( convex(v1->co, v2->co, v3->co, v4->co) ){ 
 
3345
                                                        eed->f1 |= T2QJOIN;
 
3346
                                                        efaa[0]->f1 = 1; //mark for join
 
3347
                                                        efaa[1]->f1 = 1; //mark for join
 
3348
                                                }
 
3349
                                        }
 
3350
                                        else{ 
 
3351
                                                
 
3352
                                                /*      The face pair is part of a 'complex' island, so the rules for dealing with it are more involved.
 
3353
                                                        Depending on what options the user has chosen, this face pair can be 'thrown out' based upon the following criteria:
 
3354
                                                        
 
3355
                                                        1: the two faces do not share the same material
 
3356
                                                        2: the edge joining the two faces is marked as sharp.
 
3357
                                                        3: the two faces UV's do not make a good match
 
3358
                                                        4: the two faces Vertex colors do not make a good match
 
3359
                                                        
 
3360
                                                        If the face pair passes all the applicable tests, it is then given a 'weight' with the measure_facepair() function.
 
3361
                                                        This measures things like concavity, colinearity ect. If this weight is below the threshold set by the user
 
3362
                                                        the edge joining them is marked as being 'complex' and will be compared against other possible pairs which contain one of the
 
3363
                                                        same faces in the current pair later.
 
3364
                                                
 
3365
                                                        This technique is based upon an algorithm that Campbell Barton developed for his Tri2Quad script that was previously part of
 
3366
                                                        the python scripts bundled with Blender releases.
 
3367
                                                */
 
3368
                                                
 
3369
                                                if(G.scene->toolsettings->editbutflag & B_JOINTRIA_SHARP && eed->sharp); /*do nothing*/
 
3370
                                                else if(G.scene->toolsettings->editbutflag & B_JOINTRIA_MAT && efaa[0]->mat_nr != efaa[1]->mat_nr); /*do nothing*/
 
3371
                                                else if(((G.scene->toolsettings->editbutflag & B_JOINTRIA_UV) || (G.scene->toolsettings->editbutflag & B_JOINTRIA_VCOL)) &&
 
3372
                                                                compareFaceAttribs(efaa[0], efaa[1], eed) == 0); /*do nothing*/
 
3373
                                                else{   
 
3374
                                                        measure = measure_facepair(v1, v2, v3, v4, limit);
 
3375
                                                        if(measure < limit){
 
3376
                                                                complexedges++;
 
3377
                                                                eed->f1 |= T2QCOMPLEX;
 
3378
                                                                eed->crease = measure; /*we dont mark edges for join yet*/
 
3379
                                                        }
 
3380
                                                }
 
3381
                                        }
 
3382
                                }
 
3383
                        }
 
3384
                }
 
3385
                
 
3386
                /*Quicksort the complex edges according to their weighting*/
 
3387
                if(complexedges){
 
3388
                        edsortblock = edb = MEM_callocN(sizeof(EditEdge*) * complexedges, "Face Pairs quicksort Array");
 
3389
                        for(eed = em->edges.first; eed; eed=eed->next){
 
3390
                                if(eed->f1 & T2QCOMPLEX){
 
3391
                                        *edb = eed;
 
3392
                                        edb++;
 
3393
                                }
 
3394
                        }
 
3395
                        qsort(edsortblock, complexedges, sizeof(EditEdge*), fplcmp);
 
3396
                        /*now go through and mark the edges who get the highest weighting*/
 
3397
                        for(edb=edsortblock, i=0; i < complexedges; edb++, i++){ 
 
3398
                                efaa = (EVPtr *)((*edb)->tmp.p); /*suspect!*/
 
3399
                                if( !efaa[0]->f1 && !efaa[1]->f1){
 
3400
                                        efaa[0]->f1 = 1; //mark for join
 
3401
                                        efaa[1]->f1 = 1; //mark for join
 
3402
                                        (*edb)->f1 |= T2QJOIN;
 
3403
                                }
 
3404
                        }
 
3405
                }
 
3406
                
 
3407
                /*finally go through all edges marked for join (simple and complex) and create new faces*/ 
 
3408
                for(eed=em->edges.first; eed; eed=eed->next){
 
3409
                        if(eed->f1 & T2QJOIN){
 
3410
                                efaa= (EVPtr *)eed->tmp.p;
 
3411
                                v1 = v2 = v3 = v4 = NULL;
 
3412
                                givequadverts(efaa[0], efaa[1], &v1, &v2, &v3, &v4, vindex);
 
3413
                                if((v1 && v2 && v3 && v4) && (exist_face(v1, v2, v3, v4)==0)){ /*exist_face is very slow! Needs to be adressed.*/
 
3414
                                        /*flag for delete*/
 
3415
                                        eed->f1 |= T2QDELETE;
 
3416
                                        /*create new quad and select*/
 
3417
                                        efa = EM_face_from_faces(efaa[0], efaa[1], vindex[0], vindex[1], 4+vindex[2], 4+vindex[3]);
 
3418
                                        EM_select_face(efa,1);
 
3419
                                }
 
3420
                                else{
 
3421
                                                efaa[0]->f1 = 0;
 
3422
                                                efaa[1]->f1 = 0;
 
3423
                                }
 
3424
                        }
 
3425
                }
 
3426
        }
 
3427
        
 
3428
        /*free data and cleanup*/
 
3429
        if(creases){
 
3430
                for(eed=em->edges.first, i = 0; eed; eed=eed->next, i++) eed->crease = creases[i]; 
 
3431
                MEM_freeN(creases);
 
3432
        }
 
3433
        for(eed=em->edges.first; eed; eed=eed->next){
 
3434
                if(eed->f1 & T2QDELETE) eed->f1 = 1;
 
3435
                else eed->f1 = 0;
 
3436
        }
 
3437
        free_tagged_edges_faces(em->edges.first, em->faces.first);
 
3438
        if(efaar) MEM_freeN(efaar);
 
3439
        if(edsortblock) MEM_freeN(edsortblock);
 
3440
                
 
3441
        EM_selectmode_flush();
 
3442
        countall();
3213
3443
        allqueue(REDRAWVIEW3D, 0);
3214
3444
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
3445
        #ifdef WITH_VERSE
 
3446
        if(G.editMesh->vnode)
 
3447
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
3448
        #endif
 
3449
        waitcursor(0);
3215
3450
        BIF_undo_push("Convert Triangles to Quads");
3216
3451
}
 
3452
/* ******************** END TRIANGLE TO QUAD ************************************* */
 
3453
 
 
3454
#define FACE_MARKCLEAR(f) (f->f1 = 1)
3217
3455
 
3218
3456
/* quick hack, basically a copy of beauty_fill */
3219
3457
void edge_flip(void)
3225
3463
        //void **efaar, **efaa;
3226
3464
        EVPTuple *efaar;
3227
3465
        EVPtr *efaa;
3228
 
 
3229
 
        float *uv[4];
3230
 
        unsigned int col[4];
3231
 
 
3232
 
        int totedge, ok;
 
3466
        int totedge, ok, vindex[4];
3233
3467
        
3234
3468
        /* - all selected edges with two faces
3235
3469
         * - find the faces: store them in edges (using datablock)
3266
3500
                        
3267
3501
                        if(ok) {
3268
3502
                                /* test convex */
3269
 
                                givequadverts(efaa[0], efaa[1], &v1, &v2, &v3, &v4, uv, col);
 
3503
                                givequadverts(efaa[0], efaa[1], &v1, &v2, &v3, &v4, vindex);
3270
3504
 
3271
3505
/*
3272
3506
                4-----3         4-----3
3281
3515
                                if (v1 && v2 && v3) {
3282
3516
                                        if( convex(v1->co, v2->co, v3->co, v4->co) ) {
3283
3517
                                                if(exist_face(v1, v2, v3, v4)==0) {
3284
 
                                                        w = addfacelist(v1, v2, v3, 0, efaa[1], NULL); /* outch this may break seams */ 
3285
 
                                                        EM_select_face(w, 1);
3286
 
                                                        untag_edges(w);
3287
 
 
3288
 
                                                        UVCOPY(w->tf.uv[0], uv[0]);
3289
 
                                                        UVCOPY(w->tf.uv[1], uv[1]);
3290
 
                                                        UVCOPY(w->tf.uv[2], uv[2]);
3291
 
 
3292
 
                                                        w->tf.col[0] = col[0]; w->tf.col[1] = col[1]; w->tf.col[2] = col[2]; 
3293
 
                                                        
3294
 
                                                        w = addfacelist(v1, v3, v4, 0, efaa[1], NULL); /* outch this may break seams */
3295
 
                                                        EM_select_face(w, 1);
3296
 
                                                        untag_edges(w);
3297
 
 
3298
 
                                                        UVCOPY(w->tf.uv[0], uv[0]);
3299
 
                                                        UVCOPY(w->tf.uv[1], uv[2]);
3300
 
                                                        UVCOPY(w->tf.uv[2], uv[3]);
3301
 
 
3302
 
                                                        w->tf.col[0] = col[0]; w->tf.col[1] = col[2]; w->tf.col[2] = col[3]; 
3303
 
                                                        
3304
 
                                                        /* erase old faces and edge */
 
3518
                                                        /* outch this may break seams */ 
 
3519
                                                        w= EM_face_from_faces(efaa[0], efaa[1], vindex[0],
 
3520
                                                                vindex[1], 4+vindex[2], -1);
 
3521
 
 
3522
                                                        EM_select_face(w, 1);
 
3523
 
 
3524
                                                        /* outch this may break seams */
 
3525
                                                        w= EM_face_from_faces(efaa[0], efaa[1], vindex[0],
 
3526
                                                                4+vindex[2], 4+vindex[3], -1);
 
3527
 
 
3528
                                                        EM_select_face(w, 1);
3305
3529
                                                }
3306
3530
                                                /* tag as to-be-removed */
3307
3531
                                                FACE_MARKCLEAR(efaa[1]);
3316
3540
        }
3317
3541
 
3318
3542
        /* clear tagged edges and faces: */
3319
 
        free_tagged_edgelist(em->edges.first);
3320
 
        free_tagged_facelist(em->faces.first);
 
3543
        free_tagged_edges_faces(em->edges.first, em->faces.first);
3321
3544
        
3322
3545
        MEM_freeN(efaar);
3323
3546
        
3324
3547
        allqueue(REDRAWVIEW3D, 0);
3325
3548
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
3549
#ifdef WITH_VERSE
 
3550
        if(G.editMesh->vnode)
 
3551
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
3552
#endif
3326
3553
        BIF_undo_push("Flip Triangle Edges");
3327
3554
        
3328
3555
}
3330
3557
static void edge_rotate(EditEdge *eed,int dir)
3331
3558
{
3332
3559
        EditMesh *em = G.editMesh;
 
3560
        EditVert **verts[2];
3333
3561
        EditFace *face[2], *efa, *newFace[2];
3334
 
        EditVert *faces[2][4],*v1,*v2,*v3,*v4,*vtemp;
3335
 
        EditEdge *srchedge = NULL;
3336
 
        short facecount=0, p1=0,p2=0,p3=0,p4=0,fac1=4,fac2=4,i,j,numhidden;
3337
 
        EditEdge **hiddenedges;
 
3562
        EditEdge **edges[2], **hiddenedges, *srchedge;
 
3563
        int facecount, p1, p2, p3, p4, fac1, fac2, i, j;
 
3564
        int numhidden, numshared, p[2][4];
3338
3565
        
3339
3566
        /* check to make sure that the edge is only part of 2 faces */
 
3567
        facecount = 0;
3340
3568
        for(efa = em->faces.first;efa;efa = efa->next) {
3341
3569
                if((efa->e1 == eed || efa->e2 == eed) || (efa->e3 == eed || efa->e4 == eed)) {
3342
 
                        if(facecount == 2) {
 
3570
                        if(facecount >= 2) {
 
3571
                                /* more than two faces with this edge */
3343
3572
                                return;
3344
3573
                        }
3345
 
                        if(facecount < 2)
 
3574
                        else {
3346
3575
                                face[facecount] = efa;
3347
 
                        facecount++;
 
3576
                                facecount++;
 
3577
                        }
3348
3578
                }
3349
3579
        }
3350
3580
 
3351
 
        if(facecount < 2) {
 
3581
        if(facecount < 2)
3352
3582
                return;
3353
 
        }
3354
3583
 
3355
3584
        /* how many edges does each face have */
3356
 
        if(face[0]->e4 == NULL)
3357
 
                fac1=3;
3358
 
        else
3359
 
                fac1=4;
3360
 
        if(face[1]->e4 == NULL)
3361
 
                fac2=3;
3362
 
        else
3363
 
                fac2=4;
3364
 
        
3365
 
        /*store the face info in a handy array */                       
3366
 
        faces[0][0] =  face[0]->v1;
3367
 
        faces[0][1] =  face[0]->v2;
3368
 
        faces[0][2] =  face[0]->v3;
3369
 
        if(face[0]->e4 != NULL)
3370
 
                faces[0][3] =  face[0]->v4;
3371
 
        else
3372
 
                faces[0][3] = NULL;
3373
 
                        
3374
 
        faces[1][0] =  face[1]->v1;
3375
 
        faces[1][1] =  face[1]->v2;
3376
 
        faces[1][2] =  face[1]->v3;
3377
 
        if(face[1]->e4 != NULL)
3378
 
                faces[1][3] =  face[1]->v4;
3379
 
        else
3380
 
                faces[1][3] = NULL;
3381
 
        
 
3585
        if(face[0]->e4) fac1= 4;
 
3586
        else fac1= 3;
 
3587
 
 
3588
        if(face[1]->e4) fac2= 4;
 
3589
        else fac2= 3;
 
3590
        
 
3591
        /* make a handy array for verts and edges */
 
3592
        verts[0]= &face[0]->v1;
 
3593
        edges[0]= &face[0]->e1;
 
3594
        verts[1]= &face[1]->v1;
 
3595
        edges[1]= &face[1]->e1;
3382
3596
 
3383
3597
        /* we don't want to rotate edges between faces that share more than one edge */
3384
 
        
3385
 
        j=0;
3386
 
        if(face[0]->e1 == face[1]->e1 ||
3387
 
           face[0]->e1 == face[1]->e2 ||
3388
 
           face[0]->e1 == face[1]->e3 ||
3389
 
           ((face[1]->e4) && face[0]->e1 == face[1]->e4) )
3390
 
           j++;
3391
 
           
3392
 
        if(face[0]->e2 == face[1]->e1 ||
3393
 
           face[0]->e2 == face[1]->e2 ||
3394
 
           face[0]->e2 == face[1]->e3 ||
3395
 
           ((face[1]->e4) && face[0]->e2 == face[1]->e4) )
3396
 
           j++;
3397
 
           
3398
 
        if(face[0]->e3 == face[1]->e1 ||
3399
 
           face[0]->e3 == face[1]->e2 ||
3400
 
           face[0]->e3 == face[1]->e3 ||
3401
 
           ((face[1]->e4) && face[0]->e3 == face[1]->e4) )
3402
 
           j++;    
3403
 
 
3404
 
        if(face[0]->e4) {
3405
 
                if(face[0]->e4 == face[1]->e1 ||
3406
 
                   face[0]->e4 == face[1]->e2 ||
3407
 
                   face[0]->e4 == face[1]->e3 ||
3408
 
                   ((face[1]->e4) && face[0]->e4 == face[1]->e4) )
3409
 
                           j++; 
3410
 
         }                 
3411
 
        if(j > 1) {
3412
 
                return;
3413
 
        }
3414
 
        
3415
 
        /* Coplaner Faces Only Please */
3416
 
        if(Inpf(face[0]->n,face[1]->n) <= 0.000001) {   
3417
 
                return;
3418
 
        }
3419
 
        
3420
 
        /*get the edges verts */
3421
 
        v1 = eed->v1;
3422
 
        v2 = eed->v2;
3423
 
        v3 = eed->v1;
3424
 
        v4 = eed->v2;
3425
 
 
3426
 
        /*figure out where the edges verts lie one the 2 faces */
3427
 
        for(i=0;i<4;i++) {
3428
 
                if(v1 == faces[0][i])
3429
 
                        p1 = i;
3430
 
                if(v2 == faces[0][i])
3431
 
                        p2 = i;
3432
 
                if(v1 == faces[1][i])
3433
 
                        p3 = i;
3434
 
                if(v2 == faces[1][i])
3435
 
                        p4 = i;
3436
 
        }
3437
 
        
3438
 
        /*make sure the verts are in the correct order */
3439
 
        if((p1+1)%fac1 == p2) {
3440
 
                vtemp = v2;
3441
 
                v2 = v1;
3442
 
                v1 = vtemp;
3443
 
                
3444
 
                i = p1;
3445
 
                p1 = p2;
3446
 
                p2 = i;
3447
 
        }
3448
 
        if((p3+1)%fac2 == p4) {
3449
 
                vtemp = v4;
3450
 
                v4 = v3;
3451
 
                v3 = vtemp;
3452
 
                
3453
 
                i = p3;
3454
 
                p3 = p4;
3455
 
                p4 = i; 
3456
 
        }       
3457
 
 
3458
 
 
3459
 
        /* Create an Array of the Edges who have h set prior to rotate */
 
3598
        numshared= 0;
 
3599
        for(i=0; i<fac1; i++)
 
3600
                for(j=0; j<fac2; j++)
 
3601
                        if (edges[0][i] == edges[1][j])
 
3602
                                numshared++;
 
3603
 
 
3604
        if(numshared > 1)
 
3605
                return;
 
3606
        
 
3607
        /* coplaner faces only please */
 
3608
        if(Inpf(face[0]->n,face[1]->n) <= 0.000001)
 
3609
                return;
 
3610
        
 
3611
        /* we want to construct an array of vertex indicis in both faces, starting at
 
3612
           the last vertex of the edge being rotated.
 
3613
           - first we find the two vertices that lie on the rotating edge
 
3614
           - then we make sure they are ordered according to the face vertex order
 
3615
           - and then we construct the array */
 
3616
        p1= p2= p3= p4= 0;
 
3617
 
 
3618
        for(i=0; i<4; i++) {
 
3619
                if(eed->v1 == verts[0][i]) p1 = i;
 
3620
                if(eed->v2 == verts[0][i]) p2 = i;
 
3621
                if(eed->v1 == verts[1][i]) p3 = i;
 
3622
                if(eed->v2 == verts[1][i]) p4 = i;
 
3623
        }
 
3624
        
 
3625
        if((p1+1)%fac1 == p2)
 
3626
                SWAP(int, p1, p2);
 
3627
        if((p3+1)%fac2 == p4)
 
3628
                SWAP(int, p3, p4);
 
3629
        
 
3630
        for (i = 0; i < 4; i++) {
 
3631
                p[0][i]= (p1 + i)%fac1;
 
3632
                p[1][i]= (p3 + i)%fac2;
 
3633
        }
 
3634
 
 
3635
        /* create an Array of the Edges who have h set prior to rotate */
3460
3636
        numhidden = 0;
3461
 
        for(srchedge = em->edges.first;srchedge;srchedge = srchedge->next) {
3462
 
                if(srchedge->h && (srchedge->v1->f & SELECT || srchedge->v2->f & SELECT)) {
 
3637
        for(srchedge = em->edges.first;srchedge;srchedge = srchedge->next)
 
3638
                if(srchedge->h && ((srchedge->v1->f & SELECT) || (srchedge->v2->f & SELECT)))
3463
3639
                        numhidden++;
3464
 
                }
3465
 
        }
3466
 
        hiddenedges = MEM_mallocN(sizeof(EditVert*)*numhidden+1,"Hidden Vert Scratch Array for Rotate Edges");
 
3640
 
 
3641
        hiddenedges = MEM_mallocN(sizeof(EditVert*)*numhidden+1, "RotateEdgeHiddenVerts");
3467
3642
        if(!hiddenedges) {
3468
3643
        error("Malloc Was not happy!");
3469
3644
        return;   
3470
3645
    }
 
3646
 
3471
3647
    numhidden = 0;
3472
 
        for(srchedge = em->edges.first;srchedge;srchedge = srchedge->next) {
3473
 
                if(srchedge->h && (srchedge->v1->f & SELECT || srchedge->v2->f & SELECT)) {
3474
 
                        hiddenedges[numhidden] = srchedge;
3475
 
                        numhidden++;
3476
 
                }
3477
 
        }       
3478
 
                                                        
3479
 
        /* create the 2 new faces */                                                            
 
3648
        for(srchedge=em->edges.first; srchedge; srchedge=srchedge->next)
 
3649
                if(srchedge->h && (srchedge->v1->f & SELECT || srchedge->v2->f & SELECT))
 
3650
                        hiddenedges[numhidden++] = srchedge;
 
3651
 
 
3652
        /* create the 2 new faces */
3480
3653
        if(fac1 == 3 && fac2 == 3) {
3481
 
                /*No need of reverse setup*/
3482
 
                newFace[0] = addfacelist(faces[0][(p1+1 )%3],faces[0][(p1+2 )%3],faces[1][(p3+1 )%3],NULL,NULL,NULL);
3483
 
                newFace[1] = addfacelist(faces[1][(p3+1 )%3],faces[1][(p3+2 )%3],faces[0][(p1+1 )%3],NULL,NULL,NULL);
3484
 
        
3485
 
                newFace[0]->tf.col[0] = face[0]->tf.col[(p1+1 )%3];
3486
 
                newFace[0]->tf.col[1] = face[0]->tf.col[(p1+2 )%3];
3487
 
                newFace[0]->tf.col[2] = face[1]->tf.col[(p3+1 )%3];
3488
 
                newFace[1]->tf.col[0] = face[1]->tf.col[(p3+1 )%3];
3489
 
                newFace[1]->tf.col[1] = face[1]->tf.col[(p3+2 )%3];
3490
 
                newFace[1]->tf.col[2] = face[0]->tf.col[(p1+1 )%3];
3491
 
        
3492
 
                UVCOPY(newFace[0]->tf.uv[0],face[0]->tf.uv[(p1+1 )%3]);
3493
 
                UVCOPY(newFace[0]->tf.uv[1],face[0]->tf.uv[(p1+2 )%3]); 
3494
 
                UVCOPY(newFace[0]->tf.uv[2],face[1]->tf.uv[(p3+1 )%3]);
3495
 
                UVCOPY(newFace[1]->tf.uv[0],face[1]->tf.uv[(p3+1 )%3]);
3496
 
                UVCOPY(newFace[1]->tf.uv[1],face[1]->tf.uv[(p3+2 )%3]);
3497
 
                UVCOPY(newFace[1]->tf.uv[2],face[0]->tf.uv[(p1+1 )%3]);
 
3654
                /* no need of reverse setup */
 
3655
 
 
3656
                newFace[0]= EM_face_from_faces(face[0], face[1], p[0][1], p[0][2], 4+p[1][1], -1);
 
3657
                newFace[1]= EM_face_from_faces(face[1], face[0], p[1][1], p[1][2], 4+p[0][1], -1);
3498
3658
        }
3499
3659
        else if(fac1 == 4 && fac2 == 3) {
3500
3660
                if(dir == 1) {
3501
 
                        newFace[0] = addfacelist(faces[0][(p1+1 )%4],faces[0][(p1+2 )%4],faces[0][(p1+3 )%4],faces[1][(p3+1 )%3],NULL,NULL);
3502
 
                        newFace[1] = addfacelist(faces[1][(p3+1 )%3],faces[1][(p3+2 )%3],faces[0][(p1+1 )%4],NULL,NULL,NULL);
3503
 
        
3504
 
                        newFace[0]->tf.col[0] = face[0]->tf.col[(p1+1 )%4];
3505
 
                        newFace[0]->tf.col[1] = face[0]->tf.col[(p1+2 )%4];
3506
 
                        newFace[0]->tf.col[2] = face[0]->tf.col[(p1+3 )%4];
3507
 
                        newFace[0]->tf.col[3] = face[1]->tf.col[(p3+1 )%3];
3508
 
                        newFace[1]->tf.col[0] = face[1]->tf.col[(p3+1 )%3];
3509
 
                        newFace[1]->tf.col[1] = face[1]->tf.col[(p3+2 )%3];
3510
 
                        newFace[1]->tf.col[2] = face[0]->tf.col[(p1+1 )%4];
3511
 
        
3512
 
                        UVCOPY(newFace[0]->tf.uv[0],face[0]->tf.uv[(p1+1 )%4]);
3513
 
                        UVCOPY(newFace[0]->tf.uv[1],face[0]->tf.uv[(p1+2 )%4]); 
3514
 
                        UVCOPY(newFace[0]->tf.uv[2],face[0]->tf.uv[(p1+3 )%4]);         
3515
 
                        UVCOPY(newFace[0]->tf.uv[3],face[1]->tf.uv[(p3+1 )%3]);
3516
 
                        UVCOPY(newFace[1]->tf.uv[0],face[1]->tf.uv[(p3+1 )%3]);
3517
 
                        UVCOPY(newFace[1]->tf.uv[1],face[1]->tf.uv[(p3+2 )%3]);
3518
 
                        UVCOPY(newFace[1]->tf.uv[2],face[0]->tf.uv[(p1+1 )%4]); 
 
3661
                        newFace[0]= EM_face_from_faces(face[0], face[1], p[0][1], p[0][2], p[0][3], 4+p[1][1]);
 
3662
                        newFace[1]= EM_face_from_faces(face[1], face[0], p[1][1], p[1][2], 4+p[0][1], -1);
3519
3663
                } else if (dir == 2) {
3520
 
                        newFace[0] = addfacelist(faces[0][(p1+2 )%4],faces[1][(p3+1)%3],faces[0][(p1)%4],faces[0][(p1+1 )%4],NULL,NULL);
3521
 
                        newFace[1] = addfacelist(faces[0][(p1+2 )%4],faces[1][(p3)%3],faces[1][(p3+1 )%3],NULL,NULL,NULL);
3522
 
 
3523
 
                        newFace[0]->tf.col[0] = face[0]->tf.col[(p1+2)%4];
3524
 
                        newFace[0]->tf.col[1] = face[1]->tf.col[(p3+1)%3];
3525
 
                        newFace[0]->tf.col[2] = face[0]->tf.col[(p1  )%4];
3526
 
                        newFace[0]->tf.col[3] = face[0]->tf.col[(p1+1)%4];
3527
 
                        newFace[1]->tf.col[0] = face[0]->tf.col[(p1+2)%4];
3528
 
                        newFace[1]->tf.col[1] = face[1]->tf.col[(p3  )%3];
3529
 
                        newFace[1]->tf.col[2] = face[1]->tf.col[(p3+1)%3];
3530
 
        
3531
 
                        UVCOPY(newFace[0]->tf.uv[0],face[0]->tf.uv[(p1+2)%4]);
3532
 
                        UVCOPY(newFace[0]->tf.uv[1],face[1]->tf.uv[(p3+1)%3]);  
3533
 
                        UVCOPY(newFace[0]->tf.uv[2],face[0]->tf.uv[(p1  )%4]);          
3534
 
                        UVCOPY(newFace[0]->tf.uv[3],face[0]->tf.uv[(p1+1)%4]);
3535
 
                        UVCOPY(newFace[1]->tf.uv[0],face[0]->tf.uv[(p1+2)%4]);
3536
 
                        UVCOPY(newFace[1]->tf.uv[1],face[1]->tf.uv[(p3  )%3]);
3537
 
                        UVCOPY(newFace[1]->tf.uv[2],face[1]->tf.uv[(p3+1)%3]);  
 
3664
                        newFace[0]= EM_face_from_faces(face[0], face[1], p[0][2], 4+p[1][1], p[0][0], p[0][1]);
 
3665
                        newFace[1]= EM_face_from_faces(face[1], face[0], 4+p[0][2], p[1][0], p[1][1], -1);
3538
3666
                        
3539
 
                        faces[0][(p1+2)%fac1]->f |= SELECT;
3540
 
                        faces[1][(p3+1)%fac2]->f |= SELECT;             
 
3667
                        verts[0][p[0][2]]->f |= SELECT;
 
3668
                        verts[1][p[1][1]]->f |= SELECT;         
3541
3669
                }
3542
3670
        }
3543
 
 
3544
3671
        else if(fac1 == 3 && fac2 == 4) {
3545
3672
                if(dir == 1) {
3546
 
                        newFace[0] = addfacelist(faces[0][(p1+1 )%3],faces[0][(p1+2 )%3],faces[1][(p3+1 )%4],NULL,NULL,NULL);
3547
 
                        newFace[1] = addfacelist(faces[1][(p3+1 )%4],faces[1][(p3+2 )%4],faces[1][(p3+3 )%4],faces[0][(p1+1 )%3],NULL,NULL);
3548
 
        
3549
 
                        newFace[0]->tf.col[0] = face[0]->tf.col[(p1+1 )%3];
3550
 
                        newFace[0]->tf.col[1] = face[0]->tf.col[(p1+2 )%3];
3551
 
                        newFace[0]->tf.col[2] = face[1]->tf.col[(p3+1 )%4];
3552
 
                        newFace[1]->tf.col[0] = face[1]->tf.col[(p3+1 )%4];
3553
 
                        newFace[1]->tf.col[1] = face[1]->tf.col[(p3+2 )%4];
3554
 
                        newFace[1]->tf.col[2] = face[1]->tf.col[(p3+3 )%4];             
3555
 
                        newFace[1]->tf.col[3] = face[0]->tf.col[(p1+1 )%3];
3556
 
        
3557
 
                        UVCOPY(newFace[0]->tf.uv[0],face[0]->tf.uv[(p1+1 )%3]);
3558
 
                        UVCOPY(newFace[0]->tf.uv[1],face[0]->tf.uv[(p1+2 )%3]); 
3559
 
                        UVCOPY(newFace[0]->tf.uv[2],face[1]->tf.uv[(p3+1 )%4]);
3560
 
                        UVCOPY(newFace[1]->tf.uv[0],face[1]->tf.uv[(p3+1 )%4]);
3561
 
                        UVCOPY(newFace[1]->tf.uv[1],face[1]->tf.uv[(p3+2 )%4]);
3562
 
                        UVCOPY(newFace[1]->tf.uv[2],face[1]->tf.uv[(p3+3 )%4]);
3563
 
                        UVCOPY(newFace[1]->tf.uv[3],face[0]->tf.uv[(p1+1 )%3]);
 
3673
                        newFace[0]= EM_face_from_faces(face[0], face[1], p[0][1], p[0][2], 4+p[1][1], -1);
 
3674
                        newFace[1]= EM_face_from_faces(face[1], face[0], p[1][1], p[1][2], p[1][3], 4+p[0][1]);
3564
3675
                } else if (dir == 2) {
3565
 
                        newFace[0] = addfacelist(faces[0][(p1)%3],faces[0][(p1+1 )%3],faces[1][(p3+2 )%4],NULL,NULL,NULL);
3566
 
                        newFace[1] = addfacelist(faces[1][(p3+1 )%4],faces[1][(p3+2 )%4],faces[0][(p1+1 )%3],faces[0][(p1+2 )%3],NULL,NULL);
3567
 
        
3568
 
                        newFace[0]->tf.col[0] = face[0]->tf.col[(p1 )%3];
3569
 
                        newFace[0]->tf.col[1] = face[0]->tf.col[(p1+1 )%3];
3570
 
                        newFace[0]->tf.col[2] = face[1]->tf.col[(p3+2 )%4];
3571
 
                        newFace[1]->tf.col[0] = face[1]->tf.col[(p3+1 )%4];
3572
 
                        newFace[1]->tf.col[1] = face[1]->tf.col[(p3+2 )%4];
3573
 
                        newFace[1]->tf.col[2] = face[0]->tf.col[(p1+1 )%3];             
3574
 
                        newFace[1]->tf.col[3] = face[0]->tf.col[(p1+2 )%3];
3575
 
        
3576
 
                        UVCOPY(newFace[0]->tf.uv[0],face[0]->tf.uv[(p1 )%3]);
3577
 
                        UVCOPY(newFace[0]->tf.uv[1],face[0]->tf.uv[(p1+1 )%3]); 
3578
 
                        UVCOPY(newFace[0]->tf.uv[2],face[1]->tf.uv[(p3+2 )%4]);
3579
 
                        UVCOPY(newFace[1]->tf.uv[0],face[1]->tf.uv[(p3+1 )%4]);
3580
 
                        UVCOPY(newFace[1]->tf.uv[1],face[1]->tf.uv[(p3+2 )%4]);
3581
 
                        UVCOPY(newFace[1]->tf.uv[2],face[0]->tf.uv[(p1+1 )%3]);
3582
 
                        UVCOPY(newFace[1]->tf.uv[3],face[0]->tf.uv[(p1+2 )%3]); 
 
3676
                        newFace[0]= EM_face_from_faces(face[0], face[1], p[0][0], p[0][1], 4+p[1][2], -1);
 
3677
                        newFace[1]= EM_face_from_faces(face[1], face[0], p[1][1], p[1][2], 4+p[0][1], 4+p[0][2]);
3583
3678
                        
3584
 
                        faces[0][(p1+1)%fac1]->f |= SELECT;
3585
 
                        faces[1][(p3+2)%fac2]->f |= SELECT;     
 
3679
                        verts[0][p[0][1]]->f |= SELECT;
 
3680
                        verts[1][p[1][2]]->f |= SELECT; 
3586
3681
                }
3587
3682
        
3588
3683
        }
3589
 
        
3590
3684
        else if(fac1 == 4 && fac2 == 4) {
3591
3685
                if(dir == 1) {
3592
 
                        newFace[0] = addfacelist(faces[0][(p1+1 )%4],faces[0][(p1+2 )%4],faces[0][(p1+3 )%4],faces[1][(p3+1 )%4],NULL,NULL);
3593
 
                        newFace[1] = addfacelist(faces[1][(p3+1 )%4],faces[1][(p3+2 )%4],faces[1][(p3+3 )%4],faces[0][(p1+1 )%4],NULL,NULL);
3594
 
        
3595
 
                        newFace[0]->tf.col[0] = face[0]->tf.col[(p1+1 )%4];
3596
 
                        newFace[0]->tf.col[1] = face[0]->tf.col[(p1+2 )%4];
3597
 
                        newFace[0]->tf.col[2] = face[0]->tf.col[(p1+3 )%4];
3598
 
                        newFace[0]->tf.col[3] = face[1]->tf.col[(p3+1 )%4];
3599
 
                        newFace[1]->tf.col[0] = face[1]->tf.col[(p3+1 )%4];
3600
 
                        newFace[1]->tf.col[1] = face[1]->tf.col[(p3+2 )%4];
3601
 
                        newFace[1]->tf.col[2] = face[1]->tf.col[(p3+3 )%4];             
3602
 
                        newFace[1]->tf.col[3] = face[0]->tf.col[(p1+1 )%4];
3603
 
                                                                        
3604
 
                        UVCOPY(newFace[0]->tf.uv[0],face[0]->tf.uv[(p1+1 )%4]);
3605
 
                        UVCOPY(newFace[0]->tf.uv[1],face[0]->tf.uv[(p1+2 )%4]); 
3606
 
                        UVCOPY(newFace[0]->tf.uv[2],face[0]->tf.uv[(p1+3 )%4]);         
3607
 
                        UVCOPY(newFace[0]->tf.uv[3],face[1]->tf.uv[(p3+1 )%4]);
3608
 
                        UVCOPY(newFace[1]->tf.uv[0],face[1]->tf.uv[(p3+1 )%4]);
3609
 
                        UVCOPY(newFace[1]->tf.uv[1],face[1]->tf.uv[(p3+2 )%4]);
3610
 
                        UVCOPY(newFace[1]->tf.uv[2],face[1]->tf.uv[(p3+3 )%4]);
3611
 
                        UVCOPY(newFace[1]->tf.uv[3],face[0]->tf.uv[(p1+1 )%4]);         
 
3686
                        newFace[0]= EM_face_from_faces(face[0], face[1], p[0][1], p[0][2], p[0][3], 4+p[1][1]);
 
3687
                        newFace[1]= EM_face_from_faces(face[1], face[0], p[1][1], p[1][2], p[1][3], 4+p[0][1]);
3612
3688
                } else if (dir == 2) {
3613
 
                        newFace[0] = addfacelist(faces[0][(p1+2 )%4],faces[0][(p1+3 )%4],faces[1][(p3+1 )%4],faces[1][(p3+2 )%4],NULL,NULL);
3614
 
                        newFace[1] = addfacelist(faces[1][(p3+2 )%4],faces[1][(p3+3 )%4],faces[0][(p1+1 )%4],faces[0][(p1+2 )%4],NULL,NULL);
3615
 
        
3616
 
                        newFace[0]->tf.col[0] = face[0]->tf.col[(p1+2 )%4];
3617
 
                        newFace[0]->tf.col[1] = face[0]->tf.col[(p1+3 )%4];
3618
 
                        newFace[0]->tf.col[2] = face[1]->tf.col[(p3+1 )%4];
3619
 
                        newFace[0]->tf.col[3] = face[1]->tf.col[(p3+2 )%4];
3620
 
                        newFace[1]->tf.col[0] = face[1]->tf.col[(p3+2 )%4];
3621
 
                        newFace[1]->tf.col[1] = face[1]->tf.col[(p3+3 )%4];
3622
 
                        newFace[1]->tf.col[2] = face[0]->tf.col[(p1+1 )%4];             
3623
 
                        newFace[1]->tf.col[3] = face[0]->tf.col[(p1+2 )%4];
3624
 
                                                                        
3625
 
                        UVCOPY(newFace[0]->tf.uv[0],face[0]->tf.uv[(p1+2 )%4]);
3626
 
                        UVCOPY(newFace[0]->tf.uv[1],face[0]->tf.uv[(p1+3 )%4]); 
3627
 
                        UVCOPY(newFace[0]->tf.uv[2],face[1]->tf.uv[(p3+1 )%4]);         
3628
 
                        UVCOPY(newFace[0]->tf.uv[3],face[1]->tf.uv[(p3+2 )%4]);
3629
 
                        UVCOPY(newFace[1]->tf.uv[0],face[1]->tf.uv[(p3+2 )%4]);
3630
 
                        UVCOPY(newFace[1]->tf.uv[1],face[1]->tf.uv[(p3+3 )%4]);
3631
 
                        UVCOPY(newFace[1]->tf.uv[2],face[0]->tf.uv[(p1+1 )%4]);
3632
 
                        UVCOPY(newFace[1]->tf.uv[3],face[0]->tf.uv[(p1+2 )%4]);         
 
3689
                        newFace[0]= EM_face_from_faces(face[0], face[1], p[0][2], p[0][3], 4+p[1][1], 4+p[1][2]);
 
3690
                        newFace[1]= EM_face_from_faces(face[1], face[0], p[1][2], p[1][3], 4+p[0][1], 4+p[0][2]);
3633
3691
                        
3634
 
                        faces[0][(p1+2)%fac1]->f |= SELECT;
3635
 
                        faces[1][(p3+2)%fac2]->f |= SELECT;     
 
3692
                        verts[0][p[0][2]]->f |= SELECT;
 
3693
                        verts[1][p[1][2]]->f |= SELECT; 
3636
3694
                }
3637
 
                
3638
 
 
3639
3695
        }               
3640
 
        else{
3641
 
                /*This should never happen*/
3642
 
                return;
3643
 
        }
 
3696
        else
 
3697
                return; /* This should never happen */
3644
3698
 
3645
 
        if(dir == 1) {
3646
 
                faces[0][(p1+1)%fac1]->f |= SELECT;
3647
 
                faces[1][(p3+1)%fac2]->f |= SELECT;
 
3699
        if(dir == 1 || (fac1 == 3 && fac2 == 3)) {
 
3700
                verts[0][p[0][1]]->f |= SELECT;
 
3701
                verts[1][p[1][1]]->f |= SELECT;
3648
3702
        }
3649
3703
        
3650
 
        /*Copy old edge's flags to new center edge*/
 
3704
        /* copy old edge's flags to new center edge*/
3651
3705
        for(srchedge=em->edges.first;srchedge;srchedge=srchedge->next) {
3652
 
                if(srchedge->v1->f & SELECT &&srchedge->v2->f & SELECT  ) {
 
3706
                if((srchedge->v1->f & SELECT) && (srchedge->v2->f & SELECT)) {
3653
3707
                        srchedge->f = eed->f;
3654
3708
                        srchedge->h = eed->h;
3655
3709
                        srchedge->dir = eed->dir;
3656
3710
                        srchedge->seam = eed->seam;
3657
3711
                        srchedge->crease = eed->crease;
 
3712
                        srchedge->bweight = eed->bweight;
3658
3713
                }
3659
3714
        }
3660
3715
        
3661
 
        
3662
 
        /* copy flags and material */
3663
 
        
3664
 
        newFace[0]->mat_nr       = face[0]->mat_nr;
3665
 
        newFace[0]->tf.flag     = face[0]->tf.flag;
3666
 
        newFace[0]->tf.transp  = face[0]->tf.transp;
3667
 
        newFace[0]->tf.mode     = face[0]->tf.mode;
3668
 
        newFace[0]->tf.tile     = face[0]->tf.tile;
3669
 
        newFace[0]->tf.unwrap  = face[0]->tf.unwrap;
3670
 
        newFace[0]->tf.tpage   = face[0]->tf.tpage;
3671
 
        newFace[0]->flag           = face[0]->flag;
3672
 
 
3673
 
        newFace[1]->mat_nr       = face[1]->mat_nr;
3674
 
        newFace[1]->tf.flag     = face[1]->tf.flag;
3675
 
        newFace[1]->tf.transp  = face[1]->tf.transp;
3676
 
        newFace[1]->tf.mode     = face[1]->tf.mode;
3677
 
        newFace[1]->tf.tile     = face[1]->tf.tile;
3678
 
        newFace[1]->tf.unwrap  = face[1]->tf.unwrap;
3679
 
        newFace[1]->tf.tpage   = face[1]->tf.tpage;
3680
 
        newFace[1]->flag           = face[1]->flag;
3681
 
        
3682
 
        /* Resetting Hidden Flag */
3683
 
        for(numhidden--;numhidden>=0;numhidden--) {
3684
 
                hiddenedges[numhidden]->h = 1;
3685
 
                   
3686
 
        }
 
3716
        /* resetting hidden flag */
 
3717
        for(numhidden--; numhidden>=0; numhidden--)
 
3718
                hiddenedges[numhidden]->h= 1;
3687
3719
        
3688
3720
        /* check for orhphan edges */
3689
 
        for(srchedge=em->edges.first;srchedge;srchedge = srchedge->next) {
3690
 
                srchedge->f1 = -1;   
3691
 
        }
 
3721
        for(srchedge=em->edges.first; srchedge; srchedge=srchedge->next)
 
3722
                srchedge->f1= -1;   
3692
3723
        
3693
 
        /*for(efa = em->faces.first;efa;efa = efa->next) {
3694
 
                if(efa->h == 0) {
3695
 
                        efa->e1->f1 = 1;   
3696
 
                        efa->e2->f1 = 1;   
3697
 
                        efa->e3->f1 = 1;   
3698
 
                        if(efa->e4) {
3699
 
                                efa->e4->f1 = 1;   
3700
 
                        }
3701
 
                }
3702
 
                if(efa->h == 1) {
3703
 
                        if(efa->e1->f1 == -1) {
3704
 
                                efa->e1->f1 = 0; 
3705
 
                        }  
3706
 
                        if(efa->e2->f1 == -1) {
3707
 
                                efa->e2->f1 = 0; 
3708
 
                        } 
3709
 
                                                if(efa->e1->f1 == -1) {
3710
 
                                efa->e1->f1 = 0; 
3711
 
                        }       
3712
 
                        if(efa->e4) {
3713
 
                                efa->e4->f1 = 1;   
3714
 
                        }
3715
 
                }               
3716
 
        }
3717
 
         A Little Cleanup */
 
3724
        /* cleanup */
3718
3725
        MEM_freeN(hiddenedges);
3719
3726
        
3720
3727
        /* get rid of the old edge and faces*/
3724
3731
        free_editface(face[0]); 
3725
3732
        BLI_remlink(&em->faces, face[1]);
3726
3733
        free_editface(face[1]);         
3727
 
        return;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
3728
 
}                                               
 
3734
}
3729
3735
 
3730
3736
/* only accepts 1 selected edge, or 2 selected faces */
3731
3737
void edge_rotate_selected(int dir)
3782
3788
        
3783
3789
        allqueue(REDRAWVIEW3D, 0);
3784
3790
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
3785
 
        
 
3791
 
 
3792
#ifdef WITH_VERSE
 
3793
        if(G.editMesh->vnode)
 
3794
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
3795
#endif
 
3796
 
3786
3797
        BIF_undo_push("Rotate Edge");   
3787
3798
}
3788
3799
 
3796
3807
        VecSubf(c, v3, v2);
3797
3808
 
3798
3809
        Crossf(n_a, a, no);
3799
 
        Normalise(n_a);
 
3810
        Normalize(n_a);
3800
3811
        Crossf(n_c, no, c);
3801
 
        Normalise(n_c);
 
3812
        Normalize(n_c);
3802
3813
 
3803
 
        Normalise(a);
3804
 
        Normalise(c);
 
3814
        Normalize(a);
 
3815
        Normalize(c);
3805
3816
        ac = Inpf(a, c);
3806
3817
 
3807
3818
        if (ac == 1 || ac == -1) {
3811
3822
        ac2 = ac * ac;
3812
3823
        fac = (float)sqrt((ac2 + 2*ac + 1)/(1 - ac2) + 1);
3813
3824
        VecAddf(mid, n_c, n_a);
3814
 
        Normalise(mid);
 
3825
        Normalize(mid);
3815
3826
        VecMulf(mid, d * fac);
3816
3827
        VecAddf(mid, mid, v2);
3817
3828
        VecCopyf(midvec, mid);
3826
3837
        float a[3], b[3], c[3], l_a, l_b, l_c, s_a, s_b, s_c, Pos1[3], Pos2[3], Dir[3];
3827
3838
 
3828
3839
        VecSubf(a, v3, v2);
3829
 
        l_a = Normalise(a);
 
3840
        l_a = Normalize(a);
3830
3841
        VecSubf(b, v4, v3);
3831
 
        Normalise(b);
 
3842
        Normalize(b);
3832
3843
        VecSubf(c, v1, v2);
3833
 
        Normalise(c);
 
3844
        Normalize(c);
3834
3845
 
3835
3846
        s_b = Inpf(a, c);
3836
3847
        s_b = (float)sqrt(1 - (s_b * s_b));
3968
3979
        EditFace *efa;
3969
3980
        float vec[3], no[3], v1[3], v2[3], v3[3], v4[3];
3970
3981
 
3971
 
        /* move edges of all faces with efa->f1 & flag closer towards their centres */
 
3982
        /* move edges of all faces with efa->f1 & flag closer towards their centers */
3972
3983
        efa= em->faces.first;
3973
3984
        while (efa) {
3974
3985
                if (efa->f1 & flag) {
4009
4020
        EditFace *efa;
4010
4021
        float vec[3], no[3], v1[3], v2[3], v3[3], v4[3], fv1[3], fv2[3], fv3[3], fv4[3];
4011
4022
 
4012
 
        /* move edges of all faces with efa->f1 & flag closer towards their centres */
 
4023
        /* move edges of all faces with efa->f1 & flag closer towards their centers */
4013
4024
        efa= em->faces.first;
4014
4025
        while (efa) {
4015
4026
                VECCOPY(v1, efa->v1->co);
4088
4099
        float cent[3], min[3], max[3];
4089
4100
        int a, b, c;
4090
4101
        float limit= 0.001f;
 
4102
        
 
4103
        if(multires_test()) return;
4091
4104
 
4092
4105
        waitcursor(1);
4093
4106
 
4094
 
        removedoublesflag(1, limit);
 
4107
        removedoublesflag(1, 0, limit);
4095
4108
 
4096
4109
        /* tag all original faces */
4097
4110
        efa= em->faces.first;
4120
4133
        while (efa) {
4121
4134
                if (efa->f1 & 1) {
4122
4135
                        efa->f1-= 1;
4123
 
                        v1= addvertlist(efa->v1->co);
 
4136
                        v1= addvertlist(efa->v1->co, efa->v1);
4124
4137
                        v1->f= efa->v1->f & ~128;
4125
4138
                        efa->v1->tmp.v = v1;
4126
 
#ifdef __NLA
4127
 
                        v1->totweight = efa->v1->totweight;
4128
 
                        if (efa->v1->totweight) {
4129
 
                                v1->dw = MEM_mallocN (efa->v1->totweight * sizeof(MDeformWeight), "deformWeight");
4130
 
                                memcpy (v1->dw, efa->v1->dw, efa->v1->totweight * sizeof(MDeformWeight));
4131
 
                        }
4132
 
                        else
4133
 
                                v1->dw=NULL;
4134
 
#endif
4135
 
                        v1= addvertlist(efa->v2->co);
 
4139
 
 
4140
                        v1= addvertlist(efa->v2->co, efa->v2);
4136
4141
                        v1->f= efa->v2->f & ~128;
4137
4142
                        efa->v2->tmp.v = v1;
4138
 
#ifdef __NLA
4139
 
                        v1->totweight = efa->v2->totweight;
4140
 
                        if (efa->v2->totweight) {
4141
 
                                v1->dw = MEM_mallocN (efa->v2->totweight * sizeof(MDeformWeight), "deformWeight");
4142
 
                                memcpy (v1->dw, efa->v2->dw, efa->v2->totweight * sizeof(MDeformWeight));
4143
 
                        }
4144
 
                        else
4145
 
                                v1->dw=NULL;
4146
 
#endif
4147
 
                        v1= addvertlist(efa->v3->co);
 
4143
 
 
4144
                        v1= addvertlist(efa->v3->co, efa->v3);
4148
4145
                        v1->f= efa->v3->f & ~128;
4149
4146
                        efa->v3->tmp.v = v1;
4150
 
#ifdef __NLA
4151
 
                        v1->totweight = efa->v3->totweight;
4152
 
                        if (efa->v3->totweight) {
4153
 
                                v1->dw = MEM_mallocN (efa->v3->totweight * sizeof(MDeformWeight), "deformWeight");
4154
 
                                memcpy (v1->dw, efa->v3->dw, efa->v3->totweight * sizeof(MDeformWeight));
4155
 
                        }
4156
 
                        else
4157
 
                                v1->dw=NULL;
4158
 
#endif
 
4147
 
4159
4148
                        if (efa->v4) {
4160
 
                                v1= addvertlist(efa->v4->co);
 
4149
                                v1= addvertlist(efa->v4->co, efa->v4);
4161
4150
                                v1->f= efa->v4->f & ~128;
4162
4151
                                efa->v4->tmp.v = v1;
4163
 
#ifdef __NLA
4164
 
                                v1->totweight = efa->v4->totweight;
4165
 
                                if (efa->v4->totweight) {
4166
 
                                        v1->dw = MEM_mallocN (efa->v4->totweight * sizeof(MDeformWeight), "deformWeight");
4167
 
                                        memcpy (v1->dw, efa->v4->dw, efa->v4->totweight * sizeof(MDeformWeight));
4168
 
                                }
4169
 
                                else
4170
 
                                        v1->dw=NULL;
4171
 
#endif
4172
4152
                        }
4173
4153
 
4174
4154
                        /* Needs better adaption of creases? */
4416
4396
                                        cent[0]= (min[0]+max[0])/2;
4417
4397
                                        cent[1]= (min[1]+max[1])/2;
4418
4398
                                        cent[2]= (min[2]+max[2])/2;
4419
 
                                        eve2= addvertlist(cent);
 
4399
                                        eve2= addvertlist(cent, NULL);
4420
4400
                                        eve2->f |= 1;
4421
4401
                                        eed= em->edges.first;
4422
4402
                                        while (eed) {
4479
4459
        allqueue(REDRAWVIEW3D, 0);
4480
4460
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
4481
4461
        
4482
 
        removedoublesflag(1, limit);
 
4462
        removedoublesflag(1, 0, limit);
4483
4463
 
4484
4464
        /* flush selected vertices to edges/faces */
4485
4465
        EM_select_flush();
4499
4479
        }
4500
4480
}
4501
4481
 
4502
 
void bevel_menu()
 
4482
void bevel_menu() {
 
4483
        BME_Mesh *bm;
 
4484
        BME_TransData_Head *td;
 
4485
        TransInfo *t;
 
4486
        int options, res, gbm_free = 0;
 
4487
 
 
4488
        t = BIF_GetTransInfo();
 
4489
        if (!G.editBMesh) {
 
4490
                G.editBMesh = MEM_callocN(sizeof(*(G.editBMesh)),"bevel_menu() G.editBMesh");
 
4491
                gbm_free = 1;
 
4492
        }
 
4493
 
 
4494
        G.editBMesh->options = BME_BEVEL_RUNNING | BME_BEVEL_SELECT;
 
4495
        G.editBMesh->res = 1;
 
4496
 
 
4497
        while(G.editBMesh->options & BME_BEVEL_RUNNING) {
 
4498
                options = G.editBMesh->options;
 
4499
                res = G.editBMesh->res;
 
4500
                bm = BME_make_mesh();
 
4501
                bm = BME_editmesh_to_bmesh(G.editMesh, bm);
 
4502
                BIF_undo_push("Pre-Bevel");
 
4503
                free_editMesh(G.editMesh);
 
4504
                BME_bevel(bm,0.1f,res,options,0,0,&td);
 
4505
                BME_bmesh_to_editmesh(bm, td);
 
4506
                EM_selectmode_flush();
 
4507
                G.editBMesh->bm = bm;
 
4508
                G.editBMesh->td = td;
 
4509
                initTransform(TFM_BEVEL,CTX_BMESH);
 
4510
                Transform();
 
4511
                BME_free_transdata(td);
 
4512
                BME_free_mesh(bm);
 
4513
                if (t->state != TRANS_CONFIRM) {
 
4514
                        BIF_undo();
 
4515
                }
 
4516
                if (options == G.editBMesh->options) {
 
4517
                        G.editBMesh->options &= ~BME_BEVEL_RUNNING;
 
4518
                }
 
4519
        }
 
4520
 
 
4521
        if (gbm_free) {
 
4522
                MEM_freeN(G.editBMesh);
 
4523
                G.editBMesh = NULL;
 
4524
        }
 
4525
}
 
4526
 
 
4527
 
 
4528
void bevel_menu_old()
4503
4529
{
4504
4530
        char Finished = 0, Canceled = 0, str[100], Recalc = 0;
4505
4531
        short mval[2], oval[2], curval[2], event = 0, recurs = 1, nr;
4506
 
        float vec[3], d, drawd=0.0, centre[3], fac = 1;
 
4532
        float vec[3], d, drawd=0.0, center[3], fac = 1;
4507
4533
 
4508
4534
        getmouseco_areawin(mval);
4509
4535
        oval[0] = mval[0]; oval[1] = mval[1];
4512
4538
        // while still drawing in the first iteration (and without using another variable)
4513
4539
        curval[0] = mval[0] + 1; curval[1] = mval[1] + 1;
4514
4540
 
4515
 
        window_to_3d(centre, mval[0], mval[1]);
 
4541
        // Init grabz for window to vec conversions
 
4542
        initgrabz(-G.vd->ofs[0], -G.vd->ofs[1], -G.vd->ofs[2]);
 
4543
        window_to_3d(center, mval[0], mval[1]);
4516
4544
 
4517
4545
        if(button(&recurs, 1, 4, "Recursion:")==0) return;
4518
4546
 
4532
4560
                        Recalc = 0;
4533
4561
                        curval[0] = mval[0];
4534
4562
                        curval[1] = mval[1];
4535
 
 
 
4563
                        
4536
4564
                        window_to_3d(vec, mval[0]-oval[0], mval[1]-oval[1]);
4537
 
                        d = Normalise(vec) / 10;
 
4565
                        d = Normalize(vec) / 10;
4538
4566
 
4539
4567
 
4540
4568
                        drawd = d * fac;
4583
4611
                        setlinestyle(0);
4584
4612
                        
4585
4613
                        persp(PERSP_VIEW);
4586
 
                        glFlush(); // flush display for frontbuffer
 
4614
                        bglFlush(); // flush display for frontbuffer
4587
4615
                        glDrawBuffer(GL_BACK);
4588
4616
                }
4589
4617
                while(qtest()) {
4590
 
                        unsigned short val=0;                   
 
4618
                        short val=0;                    
4591
4619
                        event= extern_qread(&val);      // extern_qread stores important events for the mainloop to handle 
4592
4620
 
4593
4621
                        /* val==0 on key-release event */
4629
4657
        if(!EdgeSlide(1, 1)) {
4630
4658
                return 0;
4631
4659
        }
4632
 
        select_more();
4633
 
        removedoublesflag(1,0.001);
 
4660
        EM_select_more();
 
4661
        removedoublesflag(1,0, 0.001);
4634
4662
        EM_select_flush();
4635
4663
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
4636
4664
        return 1;
4895
4923
        nearest = NULL;
4896
4924
        vertdist = -1;  
4897
4925
        while(look) {   
 
4926
                tempsv  = BLI_ghash_lookup(vertgh,(EditVert*)look->link);
 
4927
                
 
4928
                if(!tempsv->up || !tempsv->down) {
 
4929
                        error("Missing rails");
 
4930
                        BLI_ghash_free(vertgh, NULL, (GHashValFreeFP)MEM_freeN);
 
4931
                        BLI_linklist_free(vertlist,NULL); 
 
4932
                        BLI_linklist_free(edgelist,NULL); 
 
4933
                        return 0;
 
4934
                }
 
4935
 
 
4936
                if(G.f & G_DRAW_EDGELEN) {
 
4937
                        if(!(tempsv->up->f & SELECT)) {
 
4938
                                tempsv->up->f |= SELECT;
 
4939
                                tempsv->up->f2 |= 16;
 
4940
                        } else {
 
4941
                                tempsv->up->f2 |= ~16;
 
4942
                        }
 
4943
                        if(!(tempsv->down->f & SELECT)) {
 
4944
                                tempsv->down->f |= SELECT;
 
4945
                                tempsv->down->f2 |= 16;
 
4946
                        } else {
 
4947
                                tempsv->down->f2 |= ~16;
 
4948
                        }
 
4949
                }
 
4950
 
4898
4951
                if(look->next != NULL) {
4899
4952
                        SlideVert *sv;
4900
4953
 
4901
 
                        tempsv  = BLI_ghash_lookup(vertgh,(EditVert*)look->link);
4902
 
                        sv              = BLI_ghash_lookup(vertgh,(EditVert*)look->next->link);
4903
 
                        
4904
 
                        if(!tempsv->up || !tempsv->down) {
4905
 
                                error("Missing rails");
4906
 
                                BLI_ghash_free(vertgh, NULL, (GHashValFreeFP)MEM_freeN);
4907
 
                                BLI_linklist_free(vertlist,NULL); 
4908
 
                                BLI_linklist_free(edgelist,NULL); 
4909
 
                                return 0;
4910
 
                        }
4911
 
 
4912
 
                        if(G.f & G_DRAW_EDGELEN) {
4913
 
                                if(!(tempsv->up->f & SELECT)) {
4914
 
                                        tempsv->up->f |= SELECT;
4915
 
                                        tempsv->up->f2 |= 16;
4916
 
                                } else {
4917
 
                                        tempsv->up->f2 |= ~16;
4918
 
                                }
4919
 
                                if(!(tempsv->down->f & SELECT)) {
4920
 
                                        tempsv->down->f |= SELECT;
4921
 
                                        tempsv->down->f2 |= 16;
4922
 
                                } else {
4923
 
                                        tempsv->down->f2 |= ~16;
4924
 
                                }
4925
 
                        }
 
4954
                        sv = BLI_ghash_lookup(vertgh,(EditVert*)look->next->link);
4926
4955
 
4927
4956
                        if(sv) {
4928
4957
                                float tempdist, co[2];
5101
5130
                }
5102
5131
                if(!immediate) {
5103
5132
                        while(qtest()) {
5104
 
                                unsigned short val=0;                   
 
5133
                                short val=0;                    
5105
5134
                                event= extern_qread(&val);      // extern_qread stores important events for the mainloop to handle 
5106
5135
                                        
5107
5136
                                /* val==0 on key-release event */
5178
5207
        }
5179
5208
        
5180
5209
        force_draw(0);
 
5210
        
 
5211
        if(!immediate)
 
5212
                EM_automerge(0);
5181
5213
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
5182
5214
        scrarea_queue_winredraw(curarea);                
5183
5215
        
5185
5217
        BLI_ghash_free(vertgh, NULL, (GHashValFreeFP)MEM_freeN);
5186
5218
        BLI_linklist_free(vertlist,NULL); 
5187
5219
        BLI_linklist_free(edgelist,NULL); 
5188
 
        
 
5220
 
5189
5221
        if(cancel == 1) {
5190
5222
                return -1;
5191
5223
        }
 
5224
        else {
 
5225
#ifdef WITH_VERSE
 
5226
        if(G.editMesh->vnode) {
 
5227
                sync_all_verseverts_with_editverts((VNode*)G.editMesh->vnode);
 
5228
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
5229
        }
 
5230
#endif
 
5231
        }
5192
5232
        return 1;
5193
5233
}
5194
5234
 
5195
5235
/* -------------------- More tools ------------------ */
5196
5236
 
 
5237
void mesh_set_face_flags(short mode)
 
5238
{
 
5239
        EditMesh *em = G.editMesh;
 
5240
        EditFace *efa;
 
5241
        MTFace *tface;
 
5242
        short m_tex=0, m_tiles=0, m_shared=0, m_light=0, m_invis=0, m_collision=0, m_twoside=0, m_obcolor=0; 
 
5243
        short flag = 0, change = 0;
 
5244
        
 
5245
        if (!EM_texFaceCheck()) {
 
5246
                error("not a mesh with uv/image layers");
 
5247
                return;
 
5248
        }
 
5249
        
 
5250
        add_numbut(0, TOG|SHO, "Texture", 0, 0, &m_tex, NULL);
 
5251
        add_numbut(1, TOG|SHO, "Tiles", 0, 0, &m_tiles, NULL);
 
5252
        add_numbut(2, TOG|SHO, "Shared", 0, 0, &m_shared, NULL);
 
5253
        add_numbut(3, TOG|SHO, "Light", 0, 0, &m_light, NULL);
 
5254
        add_numbut(4, TOG|SHO, "Invisible", 0, 0, &m_invis, NULL);
 
5255
        add_numbut(5, TOG|SHO, "Collision", 0, 0, &m_collision, NULL);
 
5256
        add_numbut(6, TOG|SHO, "Twoside", 0, 0, &m_twoside, NULL);
 
5257
        add_numbut(7, TOG|SHO, "ObColor", 0, 0, &m_obcolor, NULL);
 
5258
        
 
5259
        if (!do_clever_numbuts((mode ? "Set Flags" : "Clear Flags"), 8, REDRAW))
 
5260
                return;
 
5261
        
 
5262
        if (m_tex)                      flag |= TF_TEX;
 
5263
        if (m_tiles)            flag |= TF_TILES;
 
5264
        if (m_shared)           flag |= TF_SHAREDCOL;
 
5265
        if (m_light)            flag |= TF_LIGHT;
 
5266
        if (m_invis)            flag |= TF_INVISIBLE;
 
5267
        if (m_collision)        flag |= TF_DYNAMIC;
 
5268
        if (m_twoside)          flag |= TF_TWOSIDE;
 
5269
        if (m_obcolor)          flag |= TF_OBCOL;
 
5270
        
 
5271
        efa= em->faces.first;
 
5272
        while(efa) {
 
5273
                if(efa->f & SELECT) {
 
5274
                        tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
 
5275
                        if (mode)       tface->mode |= flag;
 
5276
                        else            tface->mode &= ~flag;
 
5277
                }
 
5278
                efa= efa->next;
 
5279
        }
 
5280
        
 
5281
        if (change) {
 
5282
                BIF_undo_push((mode ? "Set Flags" : "Clear Flags"));
 
5283
                
 
5284
                allqueue(REDRAWIMAGE, 0);
 
5285
                allqueue(REDRAWVIEW3D, 0);
 
5286
        }
 
5287
}
 
5288
 
5197
5289
void mesh_set_smooth_faces(short event)
5198
5290
{
5199
5291
        EditMesh *em = G.editMesh;
5261
5353
        EditEdge *eed, *seed= NULL;
5262
5354
        EditFace *efa, *sefa= NULL;
5263
5355
        float projectMat[4][4], viewMat[4][4], vec[3], dist, mindist;
5264
 
        short doit= 1, mval[2];
5265
 
 
 
5356
        short doit= 1, mval[2],propmode,prop;
 
5357
        
 
5358
        propmode = G.scene->prop_mode;
 
5359
        G.scene->prop_mode = 0;
 
5360
        prop = G.scene->proportional;
 
5361
        G.scene->proportional = 0;
 
5362
        
5266
5363
        /* select flush... vertices are important */
5267
5364
        EM_selectmode_set();
5268
5365
        
5307
5404
        for(eve = em->verts.last; eve; eve= eve->prev) {
5308
5405
                eve->tmp.v = NULL;
5309
5406
                if(eve->f & SELECT) {
5310
 
                        eve->tmp.v = addvertlist(eve->co);
 
5407
                        eve->tmp.v = addvertlist(eve->co, eve);
5311
5408
                        eve->f &= ~SELECT;
5312
5409
                        eve->tmp.v->f |= SELECT;
5313
5410
                }
5446
5543
        }
5447
5544
        
5448
5545
        countall();     // apparently always needed when adding stuff, derived mesh
5449
 
        
 
5546
 
 
5547
#ifdef WITH_VERSE
 
5548
        if(G.editMesh->vnode) {
 
5549
                sync_all_verseverts_with_editverts((VNode*)G.editMesh->vnode);
 
5550
                sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
 
5551
        }
 
5552
#endif
 
5553
 
5450
5554
        BIF_TransformSetUndo("Rip");
5451
5555
        initTransform(TFM_TRANSLATION, 0);
5452
5556
        Transform();
 
5557
        
 
5558
        G.scene->prop_mode = propmode;
 
5559
        G.scene->proportional = prop;
5453
5560
}
5454
5561
 
5455
5562
void shape_propagate(){
5551
5658
                }
5552
5659
 
5553
5660
                while(qtest()) {
5554
 
                        unsigned short val=0;                   
 
5661
                        short val=0;                    
5555
5662
                        event= extern_qread(&val);      
5556
5663
                        if(val){
5557
5664
                                if(ELEM3(event, PADENTER, LEFTMOUSE, RETKEY)){
5617
5724
                        }
5618
5725
                        a++;
5619
5726
                }
5620
 
                nr = pupmenu(menu);
 
5727
                nr = pupmenu_col(menu, 20);
5621
5728
                MEM_freeN(menu);                
5622
5729
        } else {
5623
5730
                error("Object Has No Blendshapes");     
5649
5756
/* Collection Routines|Currently used by the improved merge code*/
5650
5757
/* buildEdge_collection() creates a list of lists*/
5651
5758
/* these lists are filled with edges that are topologically connected.*/
 
5759
/* This whole tool needs to be redone, its rather poorly implemented...*/
5652
5760
 
5653
5761
typedef struct Collection{
5654
5762
        struct Collection *next, *prev;
5785
5893
        wUV *curwvert, *newwvert;
5786
5894
        wUVNode *newnode;
5787
5895
        int found;
 
5896
        MTFace *tf = CustomData_em_get(&G.editMesh->fdata, efa->data, CD_MTFACE);
5788
5897
        
5789
5898
        found = 0;
5790
5899
        
5791
5900
        for(curwvert=uvverts->first; curwvert; curwvert=curwvert->next){
5792
 
                if(curwvert->eve == eve && curwvert->u == efa->tf.uv[tfindex][0] && curwvert->v == efa->tf.uv[tfindex][1]){
 
5901
                if(curwvert->eve == eve && curwvert->u == tf->uv[tfindex][0] && curwvert->v == tf->uv[tfindex][1]){
5793
5902
                        newnode = MEM_callocN(sizeof(wUVNode), "Welded UV Vert Node");
5794
 
                        newnode->u = &(efa->tf.uv[tfindex][0]);
5795
 
                        newnode->v = &(efa->tf.uv[tfindex][1]);
 
5903
                        newnode->u = &(tf->uv[tfindex][0]);
 
5904
                        newnode->v = &(tf->uv[tfindex][1]);
5796
5905
                        BLI_addtail(&(curwvert->nodes), newnode);
5797
5906
                        found = 1;
5798
5907
                        break;
5801
5910
        
5802
5911
        if(!found){
5803
5912
                newnode = MEM_callocN(sizeof(wUVNode), "Welded UV Vert Node");
5804
 
                newnode->u = &(efa->tf.uv[tfindex][0]);
5805
 
                newnode->v = &(efa->tf.uv[tfindex][1]);
 
5913
                newnode->u = &(tf->uv[tfindex][0]);
 
5914
                newnode->v = &(tf->uv[tfindex][1]);
5806
5915
                
5807
5916
                newwvert = MEM_callocN(sizeof(wUV), "Welded UV Vert");
5808
5917
                newwvert->u = *(newnode->u);
5830
5939
{
5831
5940
        wUVEdge *curwedge, *newwedge;
5832
5941
        int v1tfindex, v2tfindex, found;
 
5942
        MTFace *tf = CustomData_em_get(&G.editMesh->fdata, efa->data, CD_MTFACE);
5833
5943
        
5834
5944
        found = 0;
5835
5945
        
5836
5946
        if(eed->v1 == efa->v1) v1tfindex = 0;
5837
5947
        else if(eed->v1 == efa->v2) v1tfindex = 1;
5838
5948
        else if(eed->v1 == efa->v3) v1tfindex = 2;
5839
 
        else if(eed->v1 == efa->v4) v1tfindex = 3;
 
5949
        else /* if(eed->v1 == efa->v4) */ v1tfindex = 3;
5840
5950
                        
5841
5951
        if(eed->v2 == efa->v1) v2tfindex = 0;
5842
5952
        else if(eed->v2 == efa->v2) v2tfindex = 1;
5843
5953
        else if(eed->v2 == efa->v3) v2tfindex = 2;
5844
 
        else if(eed->v2 == efa->v4) v2tfindex = 3;
 
5954
        else /* if(eed->v2 == efa->v4) */ v2tfindex = 3;
5845
5955
 
5846
5956
        for(curwedge=uvedges->first; curwedge; curwedge=curwedge->next){
5847
 
                        if(curwedge->eed == eed && curwedge->v1uv[0] == efa->tf.uv[v1tfindex][0] && curwedge->v1uv[1] == efa->tf.uv[v1tfindex][1] && curwedge->v2uv[0] == efa->tf.uv[v2tfindex][0] && curwedge->v2uv[1] == efa->tf.uv[v2tfindex][1]){
 
5957
                        if(curwedge->eed == eed && curwedge->v1uv[0] == tf->uv[v1tfindex][0] && curwedge->v1uv[1] == tf->uv[v1tfindex][1] && curwedge->v2uv[0] == tf->uv[v2tfindex][0] && curwedge->v2uv[1] == tf->uv[v2tfindex][1]){
5848
5958
                                found = 1;
5849
5959
                                break; //do nothing, we don't need another welded uv edge
5850
5960
                        }
5852
5962
        
5853
5963
        if(!found){
5854
5964
                newwedge = MEM_callocN(sizeof(wUVEdge), "Welded UV Edge");
5855
 
                newwedge->v1uv[0] = efa->tf.uv[v1tfindex][0];
5856
 
                newwedge->v1uv[1] = efa->tf.uv[v1tfindex][1];
5857
 
                newwedge->v2uv[0] = efa->tf.uv[v2tfindex][0];
5858
 
                newwedge->v2uv[1] = efa->tf.uv[v2tfindex][1];
 
5965
                newwedge->v1uv[0] = tf->uv[v1tfindex][0];
 
5966
                newwedge->v1uv[1] = tf->uv[v1tfindex][1];
 
5967
                newwedge->v2uv[0] = tf->uv[v2tfindex][0];
 
5968
                newwedge->v2uv[1] = tf->uv[v2tfindex][1];
5859
5969
                newwedge->eed = eed;
5860
5970
                
5861
5971
                BLI_addtail(uvedges, newwedge);
5864
5974
 
5865
5975
static void build_weldedUVEdges(ListBase *uvedges, ListBase *uvverts)
5866
5976
{
5867
 
        
5868
5977
        wUV *curwvert;
5869
5978
        wUVEdge *curwedge;
5870
5979
        EditFace *efa;
5908
6017
        wUVNode *curwnode;
5909
6018
        wUVEdgeCollect *collectedwuve, *newcollectedwuve;
5910
6019
        Collection *wuvecollection, *newcollection;
5911
 
        int curtag, balanced, collectionfound, vcount;
 
6020
        int curtag, balanced, collectionfound= 0, vcount;
5912
6021
        float avg[2];
 
6022
 
 
6023
        if (!EM_texFaceCheck())
 
6024
                return;
5913
6025
        
5914
6026
        uvverts.first = uvverts.last = uvedges.first = uvedges.last = allcollections.first = allcollections.last = NULL;
5915
6027
        
6003
6115
 
6004
6116
/*End UV Edge collapse code*/
6005
6117
 
6006
 
static void collapseuvs(void)
 
6118
static void collapseuvs(EditVert *mergevert)
6007
6119
{
6008
6120
        EditFace *efa;
 
6121
        MTFace *tf;
6009
6122
        int uvcount;
6010
6123
        float uvav[2];
 
6124
 
 
6125
        if (!EM_texFaceCheck())
 
6126
                return;
6011
6127
        
6012
6128
        uvcount = 0;
6013
6129
        uvav[0] = 0;
6014
6130
        uvav[1] = 0;
6015
6131
        
6016
6132
        for(efa = G.editMesh->faces.first; efa; efa=efa->next){
6017
 
                if(efa->v1->f1){
6018
 
                        uvav[0] += efa->tf.uv[0][0];
6019
 
                        uvav[1] += efa->tf.uv[0][1];
6020
 
                        uvcount += 1;
6021
 
                }
6022
 
                if(efa->v2->f1){
6023
 
                        uvav[0] += efa->tf.uv[1][0];            
6024
 
                        uvav[1] += efa->tf.uv[1][1];
6025
 
                        uvcount += 1;
6026
 
                }
6027
 
                if(efa->v3->f1){
6028
 
                        uvav[0] += efa->tf.uv[2][0];
6029
 
                        uvav[1] += efa->tf.uv[2][1];
6030
 
                        uvcount += 1;
6031
 
                }
6032
 
                if(efa->v4){
6033
 
                        if(efa->v4->f1){
6034
 
                                uvav[0] += efa->tf.uv[3][0];
6035
 
                                uvav[1] += efa->tf.uv[3][1];
6036
 
                                uvcount += 1;
6037
 
                        }
 
6133
                tf = CustomData_em_get(&G.editMesh->fdata, efa->data, CD_MTFACE);
 
6134
 
 
6135
                if(efa->v1->f1 && ELEM(mergevert, NULL, efa->v1)) {
 
6136
                        uvav[0] += tf->uv[0][0];
 
6137
                        uvav[1] += tf->uv[0][1];
 
6138
                        uvcount += 1;
 
6139
                }
 
6140
                if(efa->v2->f1 && ELEM(mergevert, NULL, efa->v2)){
 
6141
                        uvav[0] += tf->uv[1][0];                
 
6142
                        uvav[1] += tf->uv[1][1];
 
6143
                        uvcount += 1;
 
6144
                }
 
6145
                if(efa->v3->f1 && ELEM(mergevert, NULL, efa->v3)){
 
6146
                        uvav[0] += tf->uv[2][0];
 
6147
                        uvav[1] += tf->uv[2][1];
 
6148
                        uvcount += 1;
 
6149
                }
 
6150
                if(efa->v4 && efa->v4->f1 && ELEM(mergevert, NULL, efa->v4)){
 
6151
                        uvav[0] += tf->uv[3][0];
 
6152
                        uvav[1] += tf->uv[3][1];
 
6153
                        uvcount += 1;
6038
6154
                }
6039
6155
        }
6040
6156
        
6041
 
        
6042
 
        
6043
 
        if(uvav[0] && uvav[1]){
 
6157
        if(uvcount > 0) {
6044
6158
                uvav[0] /= uvcount; 
6045
6159
                uvav[1] /= uvcount;
6046
6160
        
6047
6161
                for(efa = G.editMesh->faces.first; efa; efa=efa->next){
 
6162
                        tf = CustomData_em_get(&G.editMesh->fdata, efa->data, CD_MTFACE);
 
6163
 
6048
6164
                        if(efa->v1->f1){
6049
 
                                efa->tf.uv[0][0] = uvav[0];
6050
 
                                efa->tf.uv[0][1] = uvav[1];
 
6165
                                tf->uv[0][0] = uvav[0];
 
6166
                                tf->uv[0][1] = uvav[1];
6051
6167
                        }
6052
6168
                        if(efa->v2->f1){
6053
 
                                efa->tf.uv[1][0] = uvav[0];             
6054
 
                                efa->tf.uv[1][1] = uvav[1];
 
6169
                                tf->uv[1][0] = uvav[0];         
 
6170
                                tf->uv[1][1] = uvav[1];
6055
6171
                        }
6056
6172
                        if(efa->v3->f1){
6057
 
                                efa->tf.uv[2][0] = uvav[0];
6058
 
                                efa->tf.uv[2][1] = uvav[1];
 
6173
                                tf->uv[2][0] = uvav[0];
 
6174
                                tf->uv[2][1] = uvav[1];
6059
6175
                        }
6060
 
                        if(efa->v4){
6061
 
                                if(efa->v4->f1){
6062
 
                                        efa->tf.uv[3][0] = uvav[0];
6063
 
                                        efa->tf.uv[3][1] = uvav[1];
6064
 
                                }
 
6176
                        if(efa->v4 && efa->v4->f1){
 
6177
                                tf->uv[3][0] = uvav[0];
 
6178
                                tf->uv[3][1] = uvav[1];
6065
6179
                        }
6066
6180
                }
6067
6181
        }
6076
6190
        CollectedEdge *curredge;
6077
6191
        Collection *edgecollection;
6078
6192
        
6079
 
        
6080
6193
        int totedges, groupcount, mergecount,vcount;
6081
6194
        float avgcount[3];
6082
6195
        
6083
 
        
6084
6196
        allcollections.first = 0;
6085
6197
        allcollections.last = 0;
6086
6198
        
6087
6199
        mergecount = 0;
6088
6200
        
 
6201
        if(multires_test()) return 0;
 
6202
        
6089
6203
        build_edgecollection(&allcollections);
6090
6204
        groupcount = BLI_countlist(&allcollections);
6091
6205
        
6116
6230
                        VECCOPY(((EditEdge*)curredge->eed)->v2->co,avgcount);
6117
6231
                }
6118
6232
                
6119
 
                /*uv collapse*/
6120
 
                for(eve=G.editMesh->verts.first; eve; eve=eve->next) eve->f1 = 0;
6121
 
                for(eed=G.editMesh->edges.first; eed; eed=eed->next) eed->f1 = 0;
6122
 
                for(curredge = edgecollection->collectionbase.first; curredge; curredge = curredge->next){
6123
 
                        curredge->eed->v1->f1 = 1;
6124
 
                        curredge->eed->v2->f1 = 1;
6125
 
                        curredge->eed->f1 = 1;
 
6233
                if (EM_texFaceCheck()) {
 
6234
                        /*uv collapse*/
 
6235
                        for(eve=G.editMesh->verts.first; eve; eve=eve->next) eve->f1 = 0;
 
6236
                        for(eed=G.editMesh->edges.first; eed; eed=eed->next) eed->f1 = 0;
 
6237
                        for(curredge = edgecollection->collectionbase.first; curredge; curredge = curredge->next){
 
6238
                                curredge->eed->v1->f1 = 1;
 
6239
                                curredge->eed->v2->f1 = 1;
 
6240
                                curredge->eed->f1 = 1;
 
6241
                        }
 
6242
                        collapse_edgeuvs();
6126
6243
                }
6127
 
                collapse_edgeuvs();
6128
6244
                
6129
6245
        }
6130
6246
        freecollections(&allcollections);
6131
 
        removedoublesflag(1, MERGELIMIT);
 
6247
        removedoublesflag(1, 0, MERGELIMIT);
6132
6248
        /*get rid of this!*/
6133
6249
        countall();
6134
6250
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
6135
6251
        allqueue(REDRAWVIEW3D, 0);
 
6252
        if (EM_texFaceCheck())
 
6253
                allqueue(REDRAWIMAGE, 0);
6136
6254
        return mergecount;
6137
6255
}
6138
6256
 
6140
6258
{
6141
6259
        EditVert *eve,*mergevert;
6142
6260
        EditSelection *ese;
 
6261
        
 
6262
        if(multires_test()) return 0;
 
6263
        
6143
6264
        /* do sanity check in mergemenu in edit.c ?*/
6144
6265
        if(first == 0){ 
6145
6266
                ese = G.editMesh->selected.last;
6157
6278
                }
6158
6279
        }
6159
6280
        
6160
 
        if(uvmerge){
 
6281
        if(uvmerge && CustomData_has_layer(&G.editMesh->fdata, CD_MTFACE)){
6161
6282
                
6162
6283
                for(eve=G.editMesh->verts.first; eve; eve=eve->next) eve->f1 = 0;
6163
6284
                for(eve=G.editMesh->verts.first; eve; eve=eve->next){
6164
6285
                        if(eve->f&SELECT) eve->f1 = 1;
6165
6286
                }
6166
 
                collapseuvs();
 
6287
                collapseuvs(mergevert);
6167
6288
        }
6168
6289
        
6169
6290
        countall();
6170
 
        return removedoublesflag(1,MERGELIMIT);
 
6291
        return removedoublesflag(1, 0, MERGELIMIT);
6171
6292
}
6172
6293
 
6173
 
 
6174
6294
int merge_target(int target, int uvmerge)
6175
6295
{
6176
6296
        EditVert *eve;
6177
6297
        
 
6298
        if(multires_test()) return 0;
 
6299
        
6178
6300
        if(target) snap_sel_to_curs();
6179
6301
        else snap_to_center();
6180
6302
        
6181
 
        if(uvmerge){
 
6303
        if(uvmerge && CustomData_has_layer(&G.editMesh->fdata, CD_MTFACE)){
6182
6304
                for(eve=G.editMesh->verts.first; eve; eve=eve->next) eve->f1 = 0;
6183
6305
                for(eve=G.editMesh->verts.first; eve; eve=eve->next){
6184
6306
                                if(eve->f&SELECT) eve->f1 = 1;
6185
6307
                }
6186
 
                collapseuvs();
 
6308
                collapseuvs(NULL);
6187
6309
        }
6188
6310
        
6189
6311
        countall();
6190
6312
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
6191
6313
        allqueue(REDRAWVIEW3D, 0);
6192
 
        return removedoublesflag(1,MERGELIMIT);
 
6314
        return removedoublesflag(1, 0, MERGELIMIT);
6193
6315
        
6194
6316
}
6195
6317
#undef MERGELIMIT
6316
6438
                        
6317
6439
                        pnindex = ((PathNode*)s->tmp.p)->u;
6318
6440
                        cost[pnindex] = 0;
6319
 
                        BLI_heap_insert(heap,  0.0f, (void*)pnindex);
 
6441
                        BLI_heap_insert(heap,  0.0f, SET_INT_IN_POINTER(pnindex));
6320
6442
                                                
6321
6443
                        while( !BLI_heap_empty(heap) ){
6322
6444
                                
6323
 
                                pnindex = (int)BLI_heap_popmin(heap);
 
6445
                                pnindex = GET_INT_FROM_POINTER(BLI_heap_popmin(heap));
6324
6446
                                currpn = &(Q[pnindex]);
6325
6447
                                
6326
6448
                                if(currpn == (PathNode*)t->tmp.p) /*target has been reached....*/
6332
6454
                                                        cost[currpe->v] = cost[currpn->u] + currpe->w;
6333
6455
                                                        previous[currpe->v] = currpn->u;
6334
6456
                                                        Q[currpe->v].visited = 1;
6335
 
                                                        BLI_heap_insert(heap, cost[currpe->v], (void*)currpe->v);
 
6457
                                                        BLI_heap_insert(heap, cost[currpe->v], SET_INT_IN_POINTER(currpe->v));
6336
6458
                                                }
6337
6459
                                        }
6338
6460
                                }
6357
6479
                        countall();
6358
6480
                        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
6359
6481
                        allqueue(REDRAWVIEW3D, 0);
 
6482
                        if (EM_texFaceCheck())
 
6483
                                allqueue(REDRAWIMAGE, 0);
6360
6484
                }
6361
6485
        }
6362
6486
        else{
6394
6518
                countall();
6395
6519
                DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
6396
6520
                allqueue(REDRAWVIEW3D, 0);
 
6521
                if (EM_texFaceCheck())
 
6522
                        allqueue(REDRAWIMAGE, 0);
6397
6523
                BIF_undo_push("Face Region to Edge Loop");
6398
6524
 
6399
6525
        }
6552
6678
        freecollections(&allcollections);
6553
6679
        DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
6554
6680
        allqueue(REDRAWVIEW3D, 0);
 
6681
        if (EM_texFaceCheck())
 
6682
                allqueue(REDRAWIMAGE, 0);
6555
6683
        BIF_undo_push("Edge Loop to Face Region");
6556
6684
}
 
6685
 
 
6686
 
 
6687
/* texface and vertex color editmode tools for the face menu */
 
6688
 
 
6689
void mesh_rotate_uvs(void)
 
6690
{
 
6691
        EditMesh *em = G.editMesh;
 
6692
        EditFace *efa;
 
6693
        short change = 0, ccw;
 
6694
        MTFace *tf;
 
6695
        float u1, v1;
 
6696
        
 
6697
        if (!EM_texFaceCheck()) {
 
6698
                error("mesh has no uv/image layers");
 
6699
                return;
 
6700
        }
 
6701
        
 
6702
        ccw = (G.qual == LR_SHIFTKEY);
 
6703
        
 
6704
        for(efa=em->faces.first; efa; efa=efa->next) {
 
6705
                if (efa->f & SELECT) {
 
6706
                        tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
 
6707
                        u1= tf->uv[0][0];
 
6708
                        v1= tf->uv[0][1];
 
6709
                        
 
6710
                        if (ccw) {
 
6711
                                if(efa->v4) {
 
6712
                                        tf->uv[0][0]= tf->uv[3][0];
 
6713
                                        tf->uv[0][1]= tf->uv[3][1];
 
6714
                                        
 
6715
                                        tf->uv[3][0]= tf->uv[2][0];
 
6716
                                        tf->uv[3][1]= tf->uv[2][1];
 
6717
                                } else {
 
6718
                                        tf->uv[0][0]= tf->uv[2][0];
 
6719
                                        tf->uv[0][1]= tf->uv[2][1];
 
6720
                                }
 
6721
                                
 
6722
                                tf->uv[2][0]= tf->uv[1][0];
 
6723
                                tf->uv[2][1]= tf->uv[1][1];
 
6724
                                
 
6725
                                tf->uv[1][0]= u1;
 
6726
                                tf->uv[1][1]= v1;
 
6727
                        } else {        
 
6728
                                tf->uv[0][0]= tf->uv[1][0];
 
6729
                                tf->uv[0][1]= tf->uv[1][1];
 
6730
        
 
6731
                                tf->uv[1][0]= tf->uv[2][0];
 
6732
                                tf->uv[1][1]= tf->uv[2][1];
 
6733
                                
 
6734
                                if(efa->v4) {
 
6735
                                        tf->uv[2][0]= tf->uv[3][0];
 
6736
                                        tf->uv[2][1]= tf->uv[3][1];
 
6737
                                
 
6738
                                        tf->uv[3][0]= u1;
 
6739
                                        tf->uv[3][1]= v1;
 
6740
                                }
 
6741
                                else {
 
6742
                                        tf->uv[2][0]= u1;
 
6743
                                        tf->uv[2][1]= v1;
 
6744
                                }
 
6745
                        }
 
6746
                        change = 1;
 
6747
                }
 
6748
        }
 
6749
        
 
6750
        if (change) {
 
6751
                DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
6752
                allqueue(REDRAWVIEW3D, 0);
 
6753
                BIF_undo_push("Rotate UV face");
 
6754
        }
 
6755
}
 
6756
 
 
6757
void mesh_mirror_uvs(void)
 
6758
{
 
6759
        EditMesh *em = G.editMesh;
 
6760
        EditFace *efa;
 
6761
        short change = 0, altaxis;
 
6762
        MTFace *tf;
 
6763
        float u1, v1;
 
6764
        
 
6765
        if (!EM_texFaceCheck()) {
 
6766
                error("mesh has no uv/image layers");
 
6767
                return;
 
6768
        }
 
6769
        
 
6770
        altaxis = (G.qual == LR_SHIFTKEY);
 
6771
        
 
6772
        for(efa=em->faces.first; efa; efa=efa->next) {
 
6773
                if (efa->f & SELECT) {
 
6774
                        tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
 
6775
                        if (altaxis) {
 
6776
                                u1= tf->uv[1][0];
 
6777
                                v1= tf->uv[1][1];
 
6778
                                if(efa->v4) {
 
6779
                                
 
6780
                                        tf->uv[1][0]= tf->uv[2][0];
 
6781
                                        tf->uv[1][1]= tf->uv[2][1];
 
6782
                        
 
6783
                                        tf->uv[2][0]= u1;
 
6784
                                        tf->uv[2][1]= v1;
 
6785
 
 
6786
                                        u1= tf->uv[3][0];
 
6787
                                        v1= tf->uv[3][1];
 
6788
 
 
6789
                                        tf->uv[3][0]= tf->uv[0][0];
 
6790
                                        tf->uv[3][1]= tf->uv[0][1];
 
6791
                        
 
6792
                                        tf->uv[0][0]= u1;
 
6793
                                        tf->uv[0][1]= v1;
 
6794
                                }
 
6795
                                else {
 
6796
                                        tf->uv[1][0]= tf->uv[2][0];
 
6797
                                        tf->uv[1][1]= tf->uv[2][1];
 
6798
                                        tf->uv[2][0]= u1;
 
6799
                                        tf->uv[2][1]= v1;
 
6800
                                }
 
6801
                                
 
6802
                        } else {
 
6803
                                u1= tf->uv[0][0];
 
6804
                                v1= tf->uv[0][1];
 
6805
                                if(efa->v4) {
 
6806
                                
 
6807
                                        tf->uv[0][0]= tf->uv[1][0];
 
6808
                                        tf->uv[0][1]= tf->uv[1][1];
 
6809
                        
 
6810
                                        tf->uv[1][0]= u1;
 
6811
                                        tf->uv[1][1]= v1;
 
6812
 
 
6813
                                        u1= tf->uv[3][0];
 
6814
                                        v1= tf->uv[3][1];
 
6815
 
 
6816
                                        tf->uv[3][0]= tf->uv[2][0];
 
6817
                                        tf->uv[3][1]= tf->uv[2][1];
 
6818
                        
 
6819
                                        tf->uv[2][0]= u1;
 
6820
                                        tf->uv[2][1]= v1;
 
6821
                                }
 
6822
                                else {
 
6823
                                        tf->uv[0][0]= tf->uv[1][0];
 
6824
                                        tf->uv[0][1]= tf->uv[1][1];
 
6825
                                        tf->uv[1][0]= u1;
 
6826
                                        tf->uv[1][1]= v1;
 
6827
                                }
 
6828
                        }
 
6829
                        change = 1;
 
6830
                }
 
6831
        }
 
6832
        
 
6833
        if (change) {
 
6834
                DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
6835
                allqueue(REDRAWVIEW3D, 0);
 
6836
                BIF_undo_push("Mirror UV face");
 
6837
        }
 
6838
}
 
6839
 
 
6840
void mesh_rotate_colors(void)
 
6841
{
 
6842
        EditMesh *em = G.editMesh;
 
6843
        EditFace *efa;
 
6844
        short change = 0, ccw;
 
6845
        MCol tmpcol, *mcol;
 
6846
        if (!EM_vertColorCheck()) {
 
6847
                error("mesh has no color layers");
 
6848
                return;
 
6849
        }
 
6850
        
 
6851
        ccw = (G.qual == LR_SHIFTKEY);
 
6852
        
 
6853
        for(efa=em->faces.first; efa; efa=efa->next) {
 
6854
                if (efa->f & SELECT) {
 
6855
                        mcol = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
 
6856
                        tmpcol= mcol[0];
 
6857
                        
 
6858
                        if (ccw) {
 
6859
                                if(efa->v4) {
 
6860
                                        mcol[0]= mcol[3];
 
6861
                                        mcol[3]= mcol[2];
 
6862
                                } else {
 
6863
                                        mcol[0]= mcol[2];
 
6864
                                }
 
6865
                                mcol[2]= mcol[1];
 
6866
                                mcol[1]= tmpcol;
 
6867
                        } else {
 
6868
                                mcol[0]= mcol[1];
 
6869
                                mcol[1]= mcol[2];
 
6870
        
 
6871
                                if(efa->v4) {
 
6872
                                        mcol[2]= mcol[3];
 
6873
                                        mcol[3]= tmpcol;
 
6874
                                }
 
6875
                                else
 
6876
                                        mcol[2]= tmpcol;
 
6877
                        }
 
6878
                        change = 1;
 
6879
                }
 
6880
        }
 
6881
        
 
6882
        if (change) {
 
6883
                DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
6884
                allqueue(REDRAWVIEW3D, 0);
 
6885
                BIF_undo_push("Rotate Color face");
 
6886
        }       
 
6887
}
 
6888
 
 
6889
void mesh_mirror_colors(void)
 
6890
{
 
6891
        EditMesh *em = G.editMesh;
 
6892
        EditFace *efa;
 
6893
        short change = 0, altaxis;
 
6894
        MCol tmpcol, *mcol;
 
6895
        if (!EM_vertColorCheck()) {
 
6896
                error("mesh has no color layers");
 
6897
                return;
 
6898
        }
 
6899
        
 
6900
        altaxis = (G.qual == LR_SHIFTKEY);
 
6901
        
 
6902
        for(efa=em->faces.first; efa; efa=efa->next) {
 
6903
                if (efa->f & SELECT) {
 
6904
                        mcol = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
 
6905
                        if (altaxis) {
 
6906
                                tmpcol= mcol[1];
 
6907
                                mcol[1]= mcol[2];
 
6908
                                mcol[2]= tmpcol;
 
6909
                                
 
6910
                                if(efa->v4) {
 
6911
                                        tmpcol= mcol[0];
 
6912
                                        mcol[0]= mcol[3];
 
6913
                                        mcol[3]= tmpcol;
 
6914
                                }
 
6915
                        } else {
 
6916
                                tmpcol= mcol[0];
 
6917
                                mcol[0]= mcol[1];
 
6918
                                mcol[1]= tmpcol;
 
6919
                                
 
6920
                                if(efa->v4) {
 
6921
                                        tmpcol= mcol[2];
 
6922
                                        mcol[2]= mcol[3];
 
6923
                                        mcol[3]= tmpcol;
 
6924
                                }
 
6925
                        }
 
6926
                        change = 1;
 
6927
                }
 
6928
        }
 
6929
        
 
6930
        if (change) {
 
6931
                DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 
6932
                allqueue(REDRAWVIEW3D, 0);
 
6933
                BIF_undo_push("Mirror Color face");
 
6934
        }
 
6935
}