~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/mesh.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
 
47
47
#include "BLI_utildefines.h"
48
48
#include "BLI_blenlib.h"
49
 
#include "BLI_bpath.h"
50
49
#include "BLI_math.h"
51
50
#include "BLI_edgehash.h"
52
51
#include "BLI_scanfill.h"
 
52
#include "BLI_array.h"
53
53
 
54
54
#include "BKE_animsys.h"
55
55
#include "BKE_main.h"
70
70
#include "BKE_tessmesh.h"
71
71
#include "BLI_edgehash.h"
72
72
 
73
 
#include "BLI_blenlib.h"
74
 
#include "BLI_math.h"
75
 
#include "BLI_array.h"
76
 
#include "BLI_edgehash.h"
77
 
 
78
73
#include "bmesh.h"
79
74
 
80
75
enum {
88
83
        MESHCMP_POLYMISMATCH,
89
84
        MESHCMP_EDGEUNKNOWN,
90
85
        MESHCMP_VERTCOMISMATCH,
91
 
        MESHCMP_CDLAYERS_MISMATCH,
 
86
        MESHCMP_CDLAYERS_MISMATCH
92
87
};
93
88
 
94
89
static const char *cmpcode_to_str(int code)
97
92
                case MESHCMP_DVERT_WEIGHTMISMATCH:
98
93
                        return "Vertex Weight Mismatch";
99
94
                case MESHCMP_DVERT_GROUPMISMATCH:
100
 
                                        return "Vertex Group Mismatch";
 
95
                        return "Vertex Group Mismatch";
101
96
                case MESHCMP_DVERT_TOTGROUPMISMATCH:
102
 
                                        return "Vertex Doesn't Belong To Same Number Of Groups";
 
97
                        return "Vertex Doesn't Belong To Same Number Of Groups";
103
98
                case MESHCMP_LOOPCOLMISMATCH:
104
 
                                        return "Vertex Color Mismatch";
 
99
                        return "Vertex Color Mismatch";
105
100
                case MESHCMP_LOOPUVMISMATCH:
106
 
                                        return "UV Mismatch";
 
101
                        return "UV Mismatch";
107
102
                case MESHCMP_LOOPMISMATCH:
108
 
                                        return "Loop Mismatch";
 
103
                        return "Loop Mismatch";
109
104
                case MESHCMP_POLYVERTMISMATCH:
110
 
                                        return "Loop Vert Mismatch In Poly Test";
 
105
                        return "Loop Vert Mismatch In Poly Test";
111
106
                case MESHCMP_POLYMISMATCH:
112
 
                                        return "Loop Vert Mismatch";
 
107
                        return "Loop Vert Mismatch";
113
108
                case MESHCMP_EDGEUNKNOWN:
114
 
                                        return "Edge Mismatch";
 
109
                        return "Edge Mismatch";
115
110
                case MESHCMP_VERTCOMISMATCH:
116
 
                                        return "Vertex Coordinate Mismatch";
 
111
                        return "Vertex Coordinate Mismatch";
117
112
                case MESHCMP_CDLAYERS_MISMATCH:
118
 
                                        return "CustomData Layer Count Mismatch";
 
113
                        return "CustomData Layer Count Mismatch";
119
114
                default:
120
 
                                return "Mesh Comparison Code Unknown";
121
 
                }
 
115
                        return "Mesh Comparison Code Unknown";
 
116
        }
122
117
}
123
118
 
124
119
/* thresh is threshold for comparing vertices, uvs, vertex colors,
126
121
static int customdata_compare(CustomData *c1, CustomData *c2, Mesh *m1, Mesh *m2, float thresh)
127
122
{
128
123
        CustomDataLayer *l1, *l2;
129
 
        int i, i1=0, i2=0, tot, j;
 
124
        int i, i1 = 0, i2 = 0, tot, j;
130
125
        
131
 
        for (i=0; i<c1->totlayer; i++) {
132
 
                if (ELEM7(c1->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
133
 
                                  CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))                
 
126
        for (i = 0; i < c1->totlayer; i++) {
 
127
                if (ELEM7(c1->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY,
 
128
                          CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
 
129
                {
134
130
                        i1++;
 
131
                }
135
132
        }
136
 
        
137
 
        for (i=0; i<c2->totlayer; i++) {
138
 
                if (ELEM7(c2->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
139
 
                                  CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))                
 
133
 
 
134
        for (i = 0; i < c2->totlayer; i++) {
 
135
                if (ELEM7(c2->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY,
 
136
                          CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
 
137
                {
140
138
                        i2++;
 
139
                }
141
140
        }
142
 
        
 
141
 
143
142
        if (i1 != i2)
144
143
                return MESHCMP_CDLAYERS_MISMATCH;
145
144
        
146
145
        l1 = c1->layers; l2 = c2->layers;
147
146
        tot = i1;
148
147
        i1 = 0; i2 = 0; 
149
 
        for (i=0; i < tot; i++) {
 
148
        for (i = 0; i < tot; i++) {
150
149
                while (i1 < c1->totlayer && !ELEM7(l1->type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
151
 
                                  CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
 
150
                                                   CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
 
151
                {
152
152
                        i1++, l1++;
 
153
                }
153
154
 
154
 
                while (i2 < c2->totlayer && !ELEM7(l2->type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
155
 
                                  CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
 
155
                while (i2 < c2->totlayer && !ELEM7(l2->type, CD_MVERT, CD_MEDGE, CD_MPOLY,
 
156
                                                   CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
 
157
                {
156
158
                        i2++, l2++;
 
159
                }
157
160
                
158
161
                if (l1->type == CD_MVERT) {
159
162
                        MVert *v1 = l1->data;
160
163
                        MVert *v2 = l2->data;
161
164
                        int vtot = m1->totvert;
162
165
                        
163
 
                        for (j=0; j<vtot; j++, v1++, v2++) {
 
166
                        for (j = 0; j < vtot; j++, v1++, v2++) {
164
167
                                if (len_v3v3(v1->co, v2->co) > thresh)
165
168
                                        return MESHCMP_VERTCOMISMATCH;
166
 
                                /*I don't care about normals, let's just do coodinates*/
 
169
                                /* I don't care about normals, let's just do coodinates */
167
170
                        }
168
171
                }
169
172
                
174
177
                        EdgeHash *eh = BLI_edgehash_new();
175
178
                        int etot = m1->totedge;
176
179
                
177
 
                        for (j=0; j<etot; j++, e1++) {
 
180
                        for (j = 0; j < etot; j++, e1++) {
178
181
                                BLI_edgehash_insert(eh, e1->v1, e1->v2, e1);
179
182
                        }
180
183
                        
181
 
                        for (j=0; j<etot; j++, e2++) {
 
184
                        for (j = 0; j < etot; j++, e2++) {
182
185
                                if (!BLI_edgehash_lookup(eh, e2->v1, e2->v2))
183
186
                                        return MESHCMP_EDGEUNKNOWN;
184
187
                        }
190
193
                        MPoly *p2 = l2->data;
191
194
                        int ptot = m1->totpoly;
192
195
                
193
 
                        for (j=0; j<ptot; j++, p1++, p2++) {
 
196
                        for (j = 0; j < ptot; j++, p1++, p2++) {
194
197
                                MLoop *lp1, *lp2;
195
198
                                int k;
196
199
                                
200
203
                                lp1 = m1->mloop + p1->loopstart;
201
204
                                lp2 = m2->mloop + p2->loopstart;
202
205
                                
203
 
                                for (k=0; k<p1->totloop; k++, lp1++, lp2++) {
 
206
                                for (k = 0; k < p1->totloop; k++, lp1++, lp2++) {
204
207
                                        if (lp1->v != lp2->v)
205
208
                                                return MESHCMP_POLYVERTMISMATCH;
206
209
                                }
211
214
                        MLoop *lp2 = l2->data;
212
215
                        int ltot = m1->totloop;
213
216
                
214
 
                        for (j=0; j<ltot; j++, lp1++, lp2++) {
 
217
                        for (j = 0; j < ltot; j++, lp1++, lp2++) {
215
218
                                if (lp1->v != lp2->v)
216
219
                                        return MESHCMP_LOOPMISMATCH;
217
220
                        }
221
224
                        MLoopUV *lp2 = l2->data;
222
225
                        int ltot = m1->totloop;
223
226
                
224
 
                        for (j=0; j<ltot; j++, lp1++, lp2++) {
 
227
                        for (j = 0; j < ltot; j++, lp1++, lp2++) {
225
228
                                if (len_v2v2(lp1->uv, lp2->uv) > thresh)
226
229
                                        return MESHCMP_LOOPUVMISMATCH;
227
230
                        }
232
235
                        MLoopCol *lp2 = l2->data;
233
236
                        int ltot = m1->totloop;
234
237
                
235
 
                        for (j=0; j<ltot; j++, lp1++, lp2++) {
 
238
                        for (j = 0; j < ltot; j++, lp1++, lp2++) {
236
239
                                if (ABS(lp1->r - lp2->r) > thresh || 
237
240
                                    ABS(lp1->g - lp2->g) > thresh || 
238
241
                                    ABS(lp1->b - lp2->b) > thresh || 
248
251
                        MDeformVert *dv2 = l2->data;
249
252
                        int dvtot = m1->totvert;
250
253
                
251
 
                        for (j=0; j<dvtot; j++, dv1++, dv2++) {
 
254
                        for (j = 0; j < dvtot; j++, dv1++, dv2++) {
252
255
                                int k;
253
 
                                MDeformWeight *dw1 = dv1->dw, *dw2=dv2->dw;
 
256
                                MDeformWeight *dw1 = dv1->dw, *dw2 = dv2->dw;
254
257
                                
255
258
                                if (dv1->totweight != dv2->totweight)
256
259
                                        return MESHCMP_DVERT_TOTGROUPMISMATCH;
257
260
                                
258
 
                                for (k=0; k<dv1->totweight; k++, dw1++, dw2++) {
 
261
                                for (k = 0; k < dv1->totweight; k++, dw1++, dw2++) {
259
262
                                        if (dw1->def_nr != dw2->def_nr)
260
263
                                                return MESHCMP_DVERT_GROUPMISMATCH;
261
264
                                        if (ABS(dw1->weight - dw2->weight) > thresh)
269
272
}
270
273
 
271
274
/*used for testing.  returns an error string the two meshes don't match*/
272
 
const char *mesh_cmp(Mesh *me1, Mesh *me2, float thresh)
 
275
const char *BKE_mesh_cmp(Mesh *me1, Mesh *me2, float thresh)
273
276
{
274
277
        int c;
275
278
        
285
288
        if (me1->totpoly != me2->totpoly)
286
289
                return "Number of faces don't match";
287
290
                                
288
 
        if (me1->totloop !=me2->totloop)
 
291
        if (me1->totloop != me2->totloop)
289
292
                return "Number of loops don't match";
290
293
        
291
294
        if ((c = customdata_compare(&me1->vdata, &me2->vdata, me1, me2, thresh)))
319
322
                const int totcol_tessface = CustomData_number_of_layers(&me->fdata, CD_MCOL);
320
323
 
321
324
                if (tottex_tessface != tottex_original ||
322
 
                    totcol_tessface != totcol_original )
 
325
                    totcol_tessface != totcol_original)
323
326
                {
324
327
                        BKE_mesh_tessface_clear(me);
325
328
 
332
335
                                 * and check if there was any data to begin with, for now just print the warning with
333
336
                                 * some info to help troubleshoot whats going on - campbell */
334
337
                                printf("%s: warning! Tessellation uvs or vcol data got out of sync, "
335
 
                                           "had to reset!\n    CD_MTFACE: %d != CD_MTEXPOLY: %d || CD_MCOL: %d != CD_MLOOPCOL: %d\n",
336
 
                                           __func__, tottex_tessface, tottex_original, totcol_tessface, totcol_original);
 
338
                                       "had to reset!\n    CD_MTFACE: %d != CD_MTEXPOLY: %d || CD_MCOL: %d != CD_MLOOPCOL: %d\n",
 
339
                                       __func__, tottex_tessface, tottex_original, totcol_tessface, totcol_original);
337
340
                        }
338
341
                }
339
342
        }
363
366
 
364
367
        me->mvert = CustomData_get_layer(&me->vdata, CD_MVERT);
365
368
        me->dvert = CustomData_get_layer(&me->vdata, CD_MDEFORMVERT);
366
 
        me->msticky = CustomData_get_layer(&me->vdata, CD_MSTICKY);
367
369
 
368
370
        me->medge = CustomData_get_layer(&me->edata, CD_MEDGE);
369
371
 
384
386
 * we need a more generic method, like the expand() functions in
385
387
 * readfile.c */
386
388
 
387
 
void unlink_mesh(Mesh *me)
 
389
void BKE_mesh_unlink(Mesh *me)
388
390
{
389
391
        int a;
390
392
        
391
 
        if (me==NULL) return;
 
393
        if (me == NULL) return;
392
394
        
393
 
        for (a=0; a<me->totcol; a++) {
 
395
        if (me->mat)
 
396
        for (a = 0; a < me->totcol; a++) {
394
397
                if (me->mat[a]) me->mat[a]->id.us--;
395
 
                me->mat[a]= NULL;
 
398
                me->mat[a] = NULL;
396
399
        }
397
400
 
398
401
        if (me->key) {
399
402
                me->key->id.us--;
400
403
        }
401
 
        me->key= NULL;
 
404
        me->key = NULL;
402
405
        
403
 
        if (me->texcomesh) me->texcomesh= NULL;
 
406
        if (me->texcomesh) me->texcomesh = NULL;
404
407
}
405
408
 
406
409
/* do not free mesh itself */
407
 
void free_mesh(Mesh *me, int unlink)
 
410
void BKE_mesh_free(Mesh *me, int unlink)
408
411
{
409
412
        if (unlink)
410
 
                unlink_mesh(me);
 
413
                BKE_mesh_unlink(me);
411
414
 
412
415
        CustomData_free(&me->vdata, me->totvert);
413
416
        CustomData_free(&me->edata, me->totedge);
417
420
 
418
421
        if (me->adt) {
419
422
                BKE_free_animdata(&me->id);
420
 
                me->adt= NULL;
 
423
                me->adt = NULL;
421
424
        }
422
425
        
423
426
        if (me->mat) MEM_freeN(me->mat);
427
430
        if (me->edit_btmesh) MEM_freeN(me->edit_btmesh);
428
431
}
429
432
 
430
 
void copy_dverts(MDeformVert *dst, MDeformVert *src, int copycount)
431
 
{
432
 
        /* Assumes dst is already set up */
433
 
        int i;
434
 
 
435
 
        if (!src || !dst)
436
 
                return;
437
 
 
438
 
        memcpy (dst, src, copycount * sizeof(MDeformVert));
439
 
        
440
 
        for (i=0; i<copycount; i++) {
441
 
                if (src[i].dw) {
442
 
                        dst[i].dw = MEM_callocN (sizeof(MDeformWeight)*src[i].totweight, "copy_deformWeight");
443
 
                        memcpy (dst[i].dw, src[i].dw, sizeof (MDeformWeight)*src[i].totweight);
444
 
                }
445
 
        }
446
 
 
447
 
}
448
 
 
449
 
void free_dverts(MDeformVert *dvert, int totvert)
450
 
{
451
 
        /* Instead of freeing the verts directly,
452
 
         * call this function to delete any special
453
 
         * vert data */
454
 
        int     i;
455
 
 
456
 
        if (!dvert)
457
 
                return;
458
 
 
459
 
        /* Free any special data from the verts */
460
 
        for (i=0; i<totvert; i++) {
461
 
                if (dvert[i].dw) MEM_freeN (dvert[i].dw);
462
 
        }
463
 
        MEM_freeN (dvert);
464
 
}
465
 
 
466
433
static void mesh_tessface_clear_intern(Mesh *mesh, int free_customdata)
467
434
{
468
 
        if (free_customdata)
 
435
        if (free_customdata) {
469
436
                CustomData_free(&mesh->fdata, mesh->totface);
 
437
        }
 
438
        else {
 
439
                CustomData_reset(&mesh->fdata);
 
440
        }
470
441
 
471
442
        mesh->mface = NULL;
472
443
        mesh->mtface = NULL;
473
444
        mesh->mcol = NULL;
474
445
        mesh->totface = 0;
475
 
 
476
 
        memset(&mesh->fdata, 0, sizeof(mesh->fdata));
477
446
}
478
447
 
479
 
Mesh *add_mesh(const char *name)
 
448
Mesh *BKE_mesh_add(Main *bmain, const char *name)
480
449
{
481
450
        Mesh *me;
482
451
        
483
 
        me= alloc_libblock(&G.main->mesh, ID_ME, name);
484
 
        
485
 
        me->size[0]= me->size[1]= me->size[2]= 1.0;
486
 
        me->smoothresh= 30;
487
 
        me->texflag= ME_AUTOSPACE;
488
 
        me->flag= ME_TWOSIDED;
489
 
        me->bb= unit_boundbox();
490
 
        me->drawflag= ME_DRAWEDGES|ME_DRAWFACES|ME_DRAWCREASES;
491
 
        
 
452
        me = BKE_libblock_alloc(&bmain->mesh, ID_ME, name);
 
453
        
 
454
        me->size[0] = me->size[1] = me->size[2] = 1.0;
 
455
        me->smoothresh = 30;
 
456
        me->texflag = ME_AUTOSPACE;
 
457
        me->flag = ME_TWOSIDED;
 
458
        me->drawflag = ME_DRAWEDGES | ME_DRAWFACES | ME_DRAWCREASES;
 
459
 
 
460
        CustomData_reset(&me->vdata);
 
461
        CustomData_reset(&me->edata);
 
462
        CustomData_reset(&me->fdata);
 
463
        CustomData_reset(&me->pdata);
 
464
        CustomData_reset(&me->ldata);
 
465
 
492
466
        return me;
493
467
}
494
468
 
495
 
Mesh *copy_mesh(Mesh *me)
 
469
Mesh *BKE_mesh_copy_ex(Main *bmain, Mesh *me)
496
470
{
497
471
        Mesh *men;
498
472
        MTFace *tface;
500
474
        int a, i;
501
475
        const int do_tessface = ((me->totface != 0) && (me->totpoly == 0)); /* only do tessface if we have no polys */
502
476
        
503
 
        men= copy_libblock(&me->id);
 
477
        men = BKE_libblock_copy_ex(bmain, &me->id);
504
478
        
505
 
        men->mat= MEM_dupallocN(me->mat);
506
 
        for (a=0; a<men->totcol; a++) {
 
479
        men->mat = MEM_dupallocN(me->mat);
 
480
        for (a = 0; a < men->totcol; a++) {
507
481
                id_us_plus((ID *)men->mat[a]);
508
482
        }
509
483
        id_us_plus((ID *)men->texcomesh);
522
496
        mesh_update_customdata_pointers(men, do_tessface);
523
497
 
524
498
        /* ensure indirect linked data becomes lib-extern */
525
 
        for (i=0; i<me->fdata.totlayer; i++) {
 
499
        for (i = 0; i < me->fdata.totlayer; i++) {
526
500
                if (me->fdata.layers[i].type == CD_MTFACE) {
527
 
                        tface= (MTFace*)me->fdata.layers[i].data;
 
501
                        tface = (MTFace *)me->fdata.layers[i].data;
528
502
 
529
 
                        for (a=0; a<me->totface; a++, tface++)
 
503
                        for (a = 0; a < me->totface; a++, tface++)
530
504
                                if (tface->tpage)
531
 
                                        id_lib_extern((ID*)tface->tpage);
 
505
                                        id_lib_extern((ID *)tface->tpage);
532
506
                }
533
507
        }
534
508
        
535
 
        for (i=0; i<me->pdata.totlayer; i++) {
 
509
        for (i = 0; i < me->pdata.totlayer; i++) {
536
510
                if (me->pdata.layers[i].type == CD_MTEXPOLY) {
537
 
                        txface= (MTexPoly*)me->pdata.layers[i].data;
 
511
                        txface = (MTexPoly *)me->pdata.layers[i].data;
538
512
 
539
 
                        for (a=0; a<me->totpoly; a++, txface++)
 
513
                        for (a = 0; a < me->totpoly; a++, txface++)
540
514
                                if (txface->tpage)
541
 
                                        id_lib_extern((ID*)txface->tpage);
 
515
                                        id_lib_extern((ID *)txface->tpage);
542
516
                }
543
517
        }
544
518
 
545
 
        men->mselect= NULL;
546
 
        men->edit_btmesh= NULL;
 
519
        men->mselect = NULL;
 
520
        men->edit_btmesh = NULL;
547
521
 
548
 
        men->bb= MEM_dupallocN(men->bb);
 
522
        men->bb = MEM_dupallocN(men->bb);
549
523
        
550
 
        men->key= copy_key(me->key);
551
 
        if (men->key) men->key->from= (ID *)men;
 
524
        men->key = BKE_key_copy(me->key);
 
525
        if (men->key) men->key->from = (ID *)men;
552
526
 
553
527
        return men;
554
528
}
555
529
 
 
530
Mesh *BKE_mesh_copy(Mesh *me)
 
531
{
 
532
        return BKE_mesh_copy_ex(G.main, me);
 
533
}
 
534
 
556
535
BMesh *BKE_mesh_to_bmesh(Mesh *me, Object *ob)
557
536
{
558
537
        BMesh *bm;
559
538
 
560
539
        bm = BM_mesh_create(&bm_mesh_allocsize_default);
561
540
 
562
 
        BM_mesh_bm_from_me(bm, me, TRUE, ob->shapenr);
 
541
        BM_mesh_bm_from_me(bm, me, true, ob->shapenr);
563
542
 
564
543
        return bm;
565
544
}
571
550
        if (me->mtface || me->mtpoly) {
572
551
                int a, i;
573
552
 
574
 
                for (i=0; i<me->pdata.totlayer; i++) {
 
553
                for (i = 0; i < me->pdata.totlayer; i++) {
575
554
                        if (me->pdata.layers[i].type == CD_MTEXPOLY) {
576
 
                                MTexPoly *txface= (MTexPoly*)me->fdata.layers[i].data;
 
555
                                MTexPoly *txface = (MTexPoly *)me->pdata.layers[i].data;
577
556
 
578
 
                                for (a=0; a<me->totpoly; a++, txface++) {
 
557
                                for (a = 0; a < me->totpoly; a++, txface++) {
579
558
                                        /* special case: ima always local immediately */
580
559
                                        if (txface->tpage) {
581
560
                                                id_lib_extern((ID *)txface->tpage);
584
563
                        }
585
564
                }
586
565
 
587
 
                for (i=0; i<me->fdata.totlayer; i++) {
 
566
                for (i = 0; i < me->fdata.totlayer; i++) {
588
567
                        if (me->fdata.layers[i].type == CD_MTFACE) {
589
 
                                MTFace *tface= (MTFace*)me->fdata.layers[i].data;
 
568
                                MTFace *tface = (MTFace *)me->fdata.layers[i].data;
590
569
 
591
 
                                for (a=0; a<me->totface; a++, tface++) {
 
570
                                for (a = 0; a < me->totface; a++, tface++) {
592
571
                                        /* special case: ima always local immediately */
593
572
                                        if (tface->tpage) {
594
573
                                                id_lib_extern((ID *)tface->tpage);
603
582
        }
604
583
}
605
584
 
606
 
void make_local_mesh(Mesh *me)
 
585
void BKE_mesh_make_local(Mesh *me)
607
586
{
608
 
        Main *bmain= G.main;
 
587
        Main *bmain = G.main;
609
588
        Object *ob;
610
 
        int is_local= FALSE, is_lib= FALSE;
 
589
        int is_local = FALSE, is_lib = FALSE;
611
590
 
612
591
        /* - only lib users: do nothing
613
592
         * - only local users: set flag
614
593
         * - mixed: make copy
615
594
         */
616
595
 
617
 
        if (me->id.lib==NULL) return;
618
 
        if (me->id.us==1) {
 
596
        if (me->id.lib == NULL) return;
 
597
        if (me->id.us == 1) {
619
598
                id_clear_lib_data(bmain, &me->id);
620
599
                expand_local_mesh(me);
621
600
                return;
622
601
        }
623
602
 
624
 
        for (ob= bmain->object.first; ob && ELEM(0, is_lib, is_local); ob= ob->id.next) {
 
603
        for (ob = bmain->object.first; ob && ELEM(0, is_lib, is_local); ob = ob->id.next) {
625
604
                if (me == ob->data) {
626
 
                        if (ob->id.lib) is_lib= TRUE;
627
 
                        else is_local= TRUE;
 
605
                        if (ob->id.lib) is_lib = TRUE;
 
606
                        else is_local = TRUE;
628
607
                }
629
608
        }
630
609
 
633
612
                expand_local_mesh(me);
634
613
        }
635
614
        else if (is_local && is_lib) {
636
 
                Mesh *me_new= copy_mesh(me);
637
 
                me_new->id.us= 0;
 
615
                Mesh *me_new = BKE_mesh_copy(me);
 
616
                me_new->id.us = 0;
638
617
 
639
618
 
640
619
                /* Remap paths of new ID using old library as base. */
641
620
                BKE_id_lib_local_paths(bmain, me->id.lib, &me_new->id);
642
621
 
643
 
                for (ob= bmain->object.first; ob; ob= ob->id.next) {
 
622
                for (ob = bmain->object.first; ob; ob = ob->id.next) {
644
623
                        if (me == ob->data) {
645
 
                                if (ob->id.lib==NULL) {
 
624
                                if (ob->id.lib == NULL) {
646
625
                                        set_mesh(ob, me_new);
647
626
                                }
648
627
                        }
650
629
        }
651
630
}
652
631
 
653
 
void boundbox_mesh(Mesh *me, float *loc, float *size)
 
632
void BKE_mesh_boundbox_calc(Mesh *me, float r_loc[3], float r_size[3])
654
633
{
655
634
        BoundBox *bb;
656
635
        float min[3], max[3];
657
636
        float mloc[3], msize[3];
658
637
        
659
 
        if (me->bb==NULL) me->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
660
 
        bb= me->bb;
 
638
        if (me->bb == NULL) me->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
 
639
        bb = me->bb;
661
640
 
662
 
        if (!loc) loc= mloc;
663
 
        if (!size) size= msize;
 
641
        if (!r_loc) r_loc = mloc;
 
642
        if (!r_size) r_size = msize;
664
643
        
665
644
        INIT_MINMAX(min, max);
666
 
        if (!minmax_mesh(me, min, max)) {
 
645
        if (!BKE_mesh_minmax(me, min, max)) {
667
646
                min[0] = min[1] = min[2] = -1.0f;
668
647
                max[0] = max[1] = max[2] = 1.0f;
669
648
        }
670
649
 
671
 
        mid_v3_v3v3(loc, min, max);
 
650
        mid_v3_v3v3(r_loc, min, max);
672
651
                
673
 
        size[0]= (max[0]-min[0])/2.0f;
674
 
        size[1]= (max[1]-min[1])/2.0f;
675
 
        size[2]= (max[2]-min[2])/2.0f;
 
652
        r_size[0] = (max[0] - min[0]) / 2.0f;
 
653
        r_size[1] = (max[1] - min[1]) / 2.0f;
 
654
        r_size[2] = (max[2] - min[2]) / 2.0f;
676
655
        
677
 
        boundbox_set_from_min_max(bb, min, max);
 
656
        BKE_boundbox_init_from_minmax(bb, min, max);
678
657
}
679
658
 
680
 
void tex_space_mesh(Mesh *me)
 
659
void BKE_mesh_texspace_calc(Mesh *me)
681
660
{
682
661
        float loc[3], size[3];
683
662
        int a;
684
663
 
685
 
        boundbox_mesh(me, loc, size);
 
664
        BKE_mesh_boundbox_calc(me, loc, size);
686
665
 
687
666
        if (me->texflag & ME_AUTOSPACE) {
688
 
                for (a=0; a<3; a++) {
689
 
                        if (size[a]==0.0f) size[a]= 1.0f;
690
 
                        else if (size[a]>0.0f && size[a]<0.00001f) size[a]= 0.00001f;
691
 
                        else if (size[a]<0.0f && size[a]> -0.00001f) size[a]= -0.00001f;
 
667
                for (a = 0; a < 3; a++) {
 
668
                        if (size[a] == 0.0f) size[a] = 1.0f;
 
669
                        else if (size[a] > 0.0f && size[a] < 0.00001f) size[a] = 0.00001f;
 
670
                        else if (size[a] < 0.0f && size[a] > -0.00001f) size[a] = -0.00001f;
692
671
                }
693
672
 
694
673
                copy_v3_v3(me->loc, loc);
697
676
        }
698
677
}
699
678
 
700
 
BoundBox *mesh_get_bb(Object *ob)
 
679
BoundBox *BKE_mesh_boundbox_get(Object *ob)
701
680
{
702
 
        Mesh *me= ob->data;
 
681
        Mesh *me = ob->data;
703
682
 
704
683
        if (ob->bb)
705
684
                return ob->bb;
706
685
 
707
686
        if (!me->bb)
708
 
                tex_space_mesh(me);
 
687
                BKE_mesh_texspace_calc(me);
709
688
 
710
689
        return me->bb;
711
690
}
712
691
 
713
 
void mesh_get_texspace(Mesh *me, float r_loc[3], float r_rot[3], float r_size[3])
 
692
void BKE_mesh_texspace_get(Mesh *me, float r_loc[3], float r_rot[3], float r_size[3])
714
693
{
715
694
        if (!me->bb) {
716
 
                tex_space_mesh(me);
 
695
                BKE_mesh_texspace_calc(me);
717
696
        }
718
697
 
719
 
        if (r_loc)  copy_v3_v3(r_loc,  me->loc);
720
 
        if (r_rot)  copy_v3_v3(r_rot,  me->rot);
 
698
        if (r_loc) copy_v3_v3(r_loc,  me->loc);
 
699
        if (r_rot) copy_v3_v3(r_rot,  me->rot);
721
700
        if (r_size) copy_v3_v3(r_size, me->size);
722
701
}
723
702
 
724
 
float *get_mesh_orco_verts(Object *ob)
 
703
float (*BKE_mesh_orco_verts_get(Object *ob))[3]
725
704
{
726
705
        Mesh *me = ob->data;
727
706
        MVert *mvert = NULL;
728
 
        Mesh *tme = me->texcomesh?me->texcomesh:me;
 
707
        Mesh *tme = me->texcomesh ? me->texcomesh : me;
729
708
        int a, totvert;
730
709
        float (*vcos)[3] = NULL;
731
710
 
732
711
        /* Get appropriate vertex coordinates */
733
 
        vcos = MEM_callocN(sizeof(*vcos)*me->totvert, "orco mesh");
 
712
        vcos = MEM_callocN(sizeof(*vcos) * me->totvert, "orco mesh");
734
713
        mvert = tme->mvert;
735
 
        totvert = MIN2(tme->totvert, me->totvert);
 
714
        totvert = min_ii(tme->totvert, me->totvert);
736
715
 
737
 
        for (a=0; a<totvert; a++, mvert++) {
 
716
        for (a = 0; a < totvert; a++, mvert++) {
738
717
                copy_v3_v3(vcos[a], mvert->co);
739
718
        }
740
719
 
741
 
        return (float*)vcos;
 
720
        return vcos;
742
721
}
743
722
 
744
 
void transform_mesh_orco_verts(Mesh *me, float (*orco)[3], int totvert, int invert)
 
723
void BKE_mesh_orco_verts_transform(Mesh *me, float (*orco)[3], int totvert, int invert)
745
724
{
746
725
        float loc[3], size[3];
747
726
        int a;
748
727
 
749
 
        mesh_get_texspace(me->texcomesh?me->texcomesh:me, loc, NULL, size);
 
728
        BKE_mesh_texspace_get(me->texcomesh ? me->texcomesh : me, loc, NULL, size);
750
729
 
751
730
        if (invert) {
752
 
                for (a=0; a<totvert; a++) {
 
731
                for (a = 0; a < totvert; a++) {
753
732
                        float *co = orco[a];
754
733
                        madd_v3_v3v3v3(co, loc, co, size);
755
734
                }
756
735
        }
757
736
        else {
758
 
                for (a=0; a<totvert; a++) {
 
737
                for (a = 0; a < totvert; a++) {
759
738
                        float *co = orco[a];
760
 
                        co[0] = (co[0]-loc[0])/size[0];
761
 
                        co[1] = (co[1]-loc[1])/size[1];
762
 
                        co[2] = (co[2]-loc[2])/size[2];
 
739
                        co[0] = (co[0] - loc[0]) / size[0];
 
740
                        co[1] = (co[1] - loc[1]) / size[1];
 
741
                        co[2] = (co[2] - loc[2]) / size[2];
763
742
                }
764
743
        }
765
744
}
769
748
int test_index_face(MFace *mface, CustomData *fdata, int mfindex, int nr)
770
749
{
771
750
        /* first test if the face is legal */
772
 
        if ((mface->v3 || nr==4) && mface->v3==mface->v4) {
773
 
                mface->v4= 0;
774
 
                nr--;
775
 
        }
776
 
        if ((mface->v2 || mface->v4) && mface->v2==mface->v3) {
777
 
                mface->v3= mface->v4;
778
 
                mface->v4= 0;
779
 
                nr--;
780
 
        }
781
 
        if (mface->v1==mface->v2) {
782
 
                mface->v2= mface->v3;
783
 
                mface->v3= mface->v4;
784
 
                mface->v4= 0;
 
751
        if ((mface->v3 || nr == 4) && mface->v3 == mface->v4) {
 
752
                mface->v4 = 0;
 
753
                nr--;
 
754
        }
 
755
        if ((mface->v2 || mface->v4) && mface->v2 == mface->v3) {
 
756
                mface->v3 = mface->v4;
 
757
                mface->v4 = 0;
 
758
                nr--;
 
759
        }
 
760
        if (mface->v1 == mface->v2) {
 
761
                mface->v2 = mface->v3;
 
762
                mface->v3 = mface->v4;
 
763
                mface->v4 = 0;
785
764
                nr--;
786
765
        }
787
766
 
788
 
        /* check corrupt cases, bowtie geometry, cant handle these because edge data wont exist so just return 0 */
789
 
        if (nr==3) {
 
767
        /* check corrupt cases, bow-tie geometry, cant handle these because edge data wont exist so just return 0 */
 
768
        if (nr == 3) {
790
769
                if (
791
 
                /* real edges */
792
 
                        mface->v1==mface->v2 ||
793
 
                        mface->v2==mface->v3 ||
794
 
                        mface->v3==mface->v1
795
 
                ) {
 
770
                    /* real edges */
 
771
                    mface->v1 == mface->v2 ||
 
772
                    mface->v2 == mface->v3 ||
 
773
                    mface->v3 == mface->v1)
 
774
                {
796
775
                        return 0;
797
776
                }
798
777
        }
799
 
        else if (nr==4) {
 
778
        else if (nr == 4) {
800
779
                if (
801
 
                /* real edges */
802
 
                        mface->v1==mface->v2 ||
803
 
                        mface->v2==mface->v3 ||
804
 
                        mface->v3==mface->v4 ||
805
 
                        mface->v4==mface->v1 ||
806
 
                /* across the face */
807
 
                        mface->v1==mface->v3 ||
808
 
                        mface->v2==mface->v4
809
 
                ) {
 
780
                    /* real edges */
 
781
                    mface->v1 == mface->v2 ||
 
782
                    mface->v2 == mface->v3 ||
 
783
                    mface->v3 == mface->v4 ||
 
784
                    mface->v4 == mface->v1 ||
 
785
                    /* across the face */
 
786
                    mface->v1 == mface->v3 ||
 
787
                    mface->v2 == mface->v4)
 
788
                {
810
789
                        return 0;
811
790
                }
812
791
        }
813
792
 
814
793
        /* prevent a zero at wrong index location */
815
 
        if (nr==3) {
816
 
                if (mface->v3==0) {
 
794
        if (nr == 3) {
 
795
                if (mface->v3 == 0) {
817
796
                        static int corner_indices[4] = {1, 2, 0, 3};
818
797
 
819
798
                        SWAP(unsigned int, mface->v1, mface->v2);
823
802
                                CustomData_swap(fdata, mfindex, corner_indices);
824
803
                }
825
804
        }
826
 
        else if (nr==4) {
827
 
                if (mface->v3==0 || mface->v4==0) {
 
805
        else if (nr == 4) {
 
806
                if (mface->v3 == 0 || mface->v4 == 0) {
828
807
                        static int corner_indices[4] = {2, 3, 0, 1};
829
808
 
830
809
                        SWAP(unsigned int, mface->v1, mface->v3);
838
817
        return nr;
839
818
}
840
819
 
841
 
Mesh *get_mesh(Object *ob)
 
820
Mesh *BKE_mesh_from_object(Object *ob)
842
821
{
843
822
        
844
 
        if (ob==NULL) return NULL;
845
 
        if (ob->type==OB_MESH) return ob->data;
 
823
        if (ob == NULL) return NULL;
 
824
        if (ob->type == OB_MESH) return ob->data;
846
825
        else return NULL;
847
826
}
848
827
 
849
828
void set_mesh(Object *ob, Mesh *me)
850
829
{
851
 
        Mesh *old=NULL;
 
830
        Mesh *old = NULL;
852
831
 
853
832
        multires_force_update(ob);
854
833
        
855
 
        if (ob==NULL) return;
 
834
        if (ob == NULL) return;
856
835
        
857
 
        if (ob->type==OB_MESH) {
858
 
                old= ob->data;
 
836
        if (ob->type == OB_MESH) {
 
837
                old = ob->data;
859
838
                if (old)
860
839
                        old->id.us--;
861
 
                ob->data= me;
 
840
                ob->data = me;
862
841
                id_us_plus((ID *)me);
863
842
        }
864
843
        
879
858
                        unsigned int v1, unsigned int v2,
880
859
                        short is_loose, short is_draw)
881
860
{
882
 
        if (v1<v2) {
883
 
                ed->v1= v1; ed->v2= v2;
 
861
        if (v1 < v2) {
 
862
                ed->v1 = v1; ed->v2 = v2;
884
863
        }
885
864
        else {
886
 
                ed->v1= v2; ed->v2= v1;
 
865
                ed->v1 = v2; ed->v2 = v1;
887
866
        }
888
 
        ed->is_loose= is_loose;
889
 
        ed->is_draw= is_draw;
 
867
        ed->is_loose = is_loose;
 
868
        ed->is_draw = is_draw;
890
869
}
891
870
 
892
871
static int vergedgesort(const void *v1, const void *v2)
893
872
{
894
 
        const struct edgesort *x1=v1, *x2=v2;
 
873
        const struct edgesort *x1 = v1, *x2 = v2;
895
874
 
896
 
        if ( x1->v1 > x2->v1) return 1;
897
 
        else if ( x1->v1 < x2->v1) return -1;
898
 
        else if ( x1->v2 > x2->v2) return 1;
899
 
        else if ( x1->v2 < x2->v2) return -1;
 
875
        if (x1->v1 > x2->v1) return 1;
 
876
        else if (x1->v1 < x2->v1) return -1;
 
877
        else if (x1->v2 > x2->v2) return 1;
 
878
        else if (x1->v2 < x2->v2) return -1;
900
879
        
901
880
        return 0;
902
881
}
904
883
 
905
884
/* Create edges based on known verts and faces */
906
885
static void make_edges_mdata(MVert *UNUSED(allvert), MFace *allface, MLoop *allloop,
907
 
        MPoly *allpoly, int UNUSED(totvert), int totface, int UNUSED(totloop), int totpoly,
908
 
        int old, MEdge **alledge, int *_totedge)
 
886
                             MPoly *allpoly, int UNUSED(totvert), int totface, int UNUSED(totloop), int totpoly,
 
887
                             int old, MEdge **alledge, int *_totedge)
909
888
{
910
889
        MPoly *mpoly;
911
 
        MLoop *mloop;
912
890
        MFace *mface;
913
891
        MEdge *medge;
914
892
        EdgeHash *hash = BLI_edgehash_new();
915
893
        struct edgesort *edsort, *ed;
916
 
        int a, b, totedge=0, final=0;
 
894
        int a, totedge = 0, final = 0;
917
895
 
918
896
        /* we put all edges in array, sort them, and detect doubles that way */
919
897
 
920
 
        for (a= totface, mface= allface; a>0; a--, mface++) {
921
 
                if (mface->v4) totedge+=4;
922
 
                else if (mface->v3) totedge+=3;
923
 
                else totedge+=1;
 
898
        for (a = totface, mface = allface; a > 0; a--, mface++) {
 
899
                if (mface->v4) totedge += 4;
 
900
                else if (mface->v3) totedge += 3;
 
901
                else totedge += 1;
924
902
        }
925
903
 
926
 
        if (totedge==0) {
 
904
        if (totedge == 0) {
927
905
                /* flag that mesh has edges */
928
 
                (*alledge)= MEM_callocN(0, "make mesh edges");
 
906
                (*alledge) = MEM_callocN(0, "make mesh edges");
929
907
                (*_totedge) = 0;
930
908
                return;
931
909
        }
932
910
 
933
 
        ed= edsort= MEM_mallocN(totedge*sizeof(struct edgesort), "edgesort");
 
911
        ed = edsort = MEM_mallocN(totedge * sizeof(struct edgesort), "edgesort");
934
912
 
935
 
        for (a= totface, mface= allface; a>0; a--, mface++) {
 
913
        for (a = totface, mface = allface; a > 0; a--, mface++) {
936
914
                to_edgesort(ed++, mface->v1, mface->v2, !mface->v3, mface->edcode & ME_V1V2);
937
915
                if (mface->v4) {
938
916
                        to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3);
948
926
        qsort(edsort, totedge, sizeof(struct edgesort), vergedgesort);
949
927
 
950
928
        /* count final amount */
951
 
        for (a=totedge, ed=edsort; a>1; a--, ed++) {
 
929
        for (a = totedge, ed = edsort; a > 1; a--, ed++) {
952
930
                /* edge is unique when it differs from next edge, or is last */
953
 
                if (ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) final++;
 
931
                if (ed->v1 != (ed + 1)->v1 || ed->v2 != (ed + 1)->v2) final++;
954
932
        }
955
933
        final++;
956
934
 
957
 
        (*alledge)= medge= MEM_callocN(sizeof (MEdge) * final, "make_edges mdge");
958
 
        (*_totedge)= final;
 
935
        (*alledge) = medge = MEM_callocN(sizeof(MEdge) * final, "BKE_mesh_make_edges mdge");
 
936
        (*_totedge) = final;
959
937
 
960
 
        for (a=totedge, ed=edsort; a>1; a--, ed++) {
 
938
        for (a = totedge, ed = edsort; a > 1; a--, ed++) {
961
939
                /* edge is unique when it differs from next edge, or is last */
962
 
                if (ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) {
963
 
                        medge->v1= ed->v1;
964
 
                        medge->v2= ed->v2;
965
 
                        if (old==0 || ed->is_draw) medge->flag= ME_EDGEDRAW|ME_EDGERENDER;
966
 
                        if (ed->is_loose) medge->flag|= ME_LOOSEEDGE;
 
940
                if (ed->v1 != (ed + 1)->v1 || ed->v2 != (ed + 1)->v2) {
 
941
                        medge->v1 = ed->v1;
 
942
                        medge->v2 = ed->v2;
 
943
                        if (old == 0 || ed->is_draw) medge->flag = ME_EDGEDRAW | ME_EDGERENDER;
 
944
                        if (ed->is_loose) medge->flag |= ME_LOOSEEDGE;
967
945
 
968
946
                        /* order is swapped so extruding this edge as a surface wont flip face normals
969
947
                         * with cyclic curves */
970
 
                        if (ed->v1+1 != ed->v2) {
 
948
                        if (ed->v1 + 1 != ed->v2) {
971
949
                                SWAP(unsigned int, medge->v1, medge->v2);
972
950
                        }
973
951
                        medge++;
974
952
                }
975
953
                else {
976
954
                        /* equal edge, we merge the drawflag */
977
 
                        (ed+1)->is_draw |= ed->is_draw;
 
955
                        (ed + 1)->is_draw |= ed->is_draw;
978
956
                }
979
957
        }
980
958
        /* last edge */
981
 
        medge->v1= ed->v1;
982
 
        medge->v2= ed->v2;
983
 
        medge->flag= ME_EDGEDRAW;
984
 
        if (ed->is_loose) medge->flag|= ME_LOOSEEDGE;
 
959
        medge->v1 = ed->v1;
 
960
        medge->v2 = ed->v2;
 
961
        medge->flag = ME_EDGEDRAW;
 
962
        if (ed->is_loose) medge->flag |= ME_LOOSEEDGE;
985
963
        medge->flag |= ME_EDGERENDER;
986
964
 
987
965
        MEM_freeN(edsort);
988
966
        
989
 
        /*set edge members of mloops*/
990
 
        medge= *alledge;
991
 
        for (a=0; a<*_totedge; a++, medge++) {
 
967
        /* set edge members of mloops */
 
968
        medge = *alledge;
 
969
        for (a = 0; a < *_totedge; a++, medge++) {
992
970
                BLI_edgehash_insert(hash, medge->v1, medge->v2, SET_INT_IN_POINTER(a));
993
971
        }
994
972
        
995
973
        mpoly = allpoly;
996
 
        for (a=0; a<totpoly; a++, mpoly++) {
997
 
                mloop = allloop + mpoly->loopstart;
998
 
                for (b=0; b<mpoly->totloop; b++) {
999
 
                        int v1, v2;
1000
 
                        
1001
 
                        v1 = mloop[b].v;
1002
 
                        v2 = ME_POLY_LOOP_NEXT(mloop, mpoly, b)->v;
1003
 
                        mloop[b].e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(hash, v1, v2));
 
974
        for (a = 0; a < totpoly; a++, mpoly++) {
 
975
                MLoop *ml, *ml_next;
 
976
                int i = mpoly->totloop;
 
977
 
 
978
                ml_next = allloop + mpoly->loopstart;  /* first loop */
 
979
                ml = &ml_next[i - 1];                  /* last loop */
 
980
 
 
981
                while (i-- != 0) {
 
982
                        ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(hash, ml->v, ml_next->v));
 
983
                        ml = ml_next;
 
984
                        ml_next++;
1004
985
                }
1005
986
        }
1006
987
        
1007
988
        BLI_edgehash_free(hash, NULL);
1008
989
}
1009
990
 
1010
 
void make_edges(Mesh *me, int old)
 
991
void BKE_mesh_make_edges(Mesh *me, int old)
1011
992
{
1012
993
        MEdge *medge;
1013
 
        int totedge=0;
 
994
        int totedge = 0;
1014
995
 
1015
996
        make_edges_mdata(me->mvert, me->mface, me->mloop, me->mpoly, me->totvert, me->totface, me->totloop, me->totpoly, old, &medge, &totedge);
1016
 
        if (totedge==0) {
 
997
        if (totedge == 0) {
1017
998
                /* flag that mesh has edges */
1018
999
                me->medge = medge;
1019
1000
                me->totedge = 0;
1020
1001
                return;
1021
1002
        }
1022
1003
 
1023
 
        medge= CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, totedge);
1024
 
        me->medge= medge;
1025
 
        me->totedge= totedge;
 
1004
        medge = CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, totedge);
 
1005
        me->medge = medge;
 
1006
        me->totedge = totedge;
1026
1007
 
1027
 
        mesh_strip_loose_faces(me);
 
1008
        BKE_mesh_strip_loose_faces(me);
1028
1009
}
1029
1010
 
1030
1011
/* We need to keep this for edge creation (for now?), and some old readfile code... */
1031
 
void mesh_strip_loose_faces(Mesh *me)
 
1012
void BKE_mesh_strip_loose_faces(Mesh *me)
1032
1013
{
1033
1014
        MFace *f;
1034
1015
        int a, b;
1052
1033
/* Note: It won't try to guess which loops of an invalid poly to remove!
1053
1034
 *       this is the work of the caller, to mark those loops...
1054
1035
 *       See e.g. BKE_mesh_validate_arrays(). */
1055
 
void mesh_strip_loose_polysloops(Mesh *me)
 
1036
void BKE_mesh_strip_loose_polysloops(Mesh *me)
1056
1037
{
1057
1038
        MPoly *p;
1058
1039
        MLoop *l;
1123
1104
        MEM_freeN(new_idx);
1124
1105
}
1125
1106
 
1126
 
void mesh_strip_loose_edges(Mesh *me)
 
1107
void BKE_mesh_strip_loose_edges(Mesh *me)
1127
1108
{
1128
1109
        MEdge *e;
1129
1110
        MLoop *l;
1158
1139
        MEM_freeN(new_idx);
1159
1140
}
1160
1141
 
1161
 
void mball_to_mesh(ListBase *lb, Mesh *me)
 
1142
void BKE_mesh_from_metaball(ListBase *lb, Mesh *me)
1162
1143
{
1163
1144
        DispList *dl;
1164
1145
        MVert *mvert;
1167
1148
        float *nors, *verts;
1168
1149
        int a, *index;
1169
1150
        
1170
 
        dl= lb->first;
1171
 
        if (dl==NULL) return;
1172
 
 
1173
 
        if (dl->type==DL_INDEX4) {
1174
 
                mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, dl->nr);
1175
 
                allloop= mloop= CustomData_add_layer(&me->ldata, CD_MLOOP, CD_CALLOC, NULL, dl->parts * 4);
1176
 
                mpoly= CustomData_add_layer(&me->pdata, CD_MPOLY, CD_CALLOC, NULL, dl->parts);
1177
 
                me->mvert= mvert;
1178
 
                me->mloop= mloop;
1179
 
                me->mpoly= mpoly;
1180
 
                me->totvert= dl->nr;
1181
 
                me->totpoly= dl->parts;
1182
 
 
1183
 
                a= dl->nr;
1184
 
                nors= dl->nors;
1185
 
                verts= dl->verts;
 
1151
        dl = lb->first;
 
1152
        if (dl == NULL) return;
 
1153
 
 
1154
        if (dl->type == DL_INDEX4) {
 
1155
                mvert = CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, dl->nr);
 
1156
                allloop = mloop = CustomData_add_layer(&me->ldata, CD_MLOOP, CD_CALLOC, NULL, dl->parts * 4);
 
1157
                mpoly = CustomData_add_layer(&me->pdata, CD_MPOLY, CD_CALLOC, NULL, dl->parts);
 
1158
                me->mvert = mvert;
 
1159
                me->mloop = mloop;
 
1160
                me->mpoly = mpoly;
 
1161
                me->totvert = dl->nr;
 
1162
                me->totpoly = dl->parts;
 
1163
 
 
1164
                a = dl->nr;
 
1165
                nors = dl->nors;
 
1166
                verts = dl->verts;
1186
1167
                while (a--) {
1187
1168
                        copy_v3_v3(mvert->co, verts);
1188
1169
                        normal_float_to_short_v3(mvert->no, nors);
1189
1170
                        mvert++;
1190
 
                        nors+= 3;
1191
 
                        verts+= 3;
 
1171
                        nors += 3;
 
1172
                        verts += 3;
1192
1173
                }
1193
1174
                
1194
 
                a= dl->parts;
1195
 
                index= dl->index;
 
1175
                a = dl->parts;
 
1176
                index = dl->index;
1196
1177
                while (a--) {
1197
 
                        int count= index[2] != index[3] ? 4 : 3;
 
1178
                        int count = index[2] != index[3] ? 4 : 3;
1198
1179
 
1199
 
                        mloop[0].v= index[0];
1200
 
                        mloop[1].v= index[1];
1201
 
                        mloop[2].v= index[2];
 
1180
                        mloop[0].v = index[0];
 
1181
                        mloop[1].v = index[1];
 
1182
                        mloop[2].v = index[2];
1202
1183
                        if (count == 4)
1203
 
                                mloop[3].v= index[3];
 
1184
                                mloop[3].v = index[3];
1204
1185
 
1205
 
                        mpoly->totloop= count;
1206
 
                        mpoly->loopstart= (int)(mloop - allloop);
1207
 
                        mpoly->flag= ME_SMOOTH;
 
1186
                        mpoly->totloop = count;
 
1187
                        mpoly->loopstart = (int)(mloop - allloop);
 
1188
                        mpoly->flag = ME_SMOOTH;
1208
1189
 
1209
1190
 
1210
1191
                        mpoly++;
1211
 
                        mloop+= count;
1212
 
                        me->totloop+= count;
1213
 
                        index+= 4;
 
1192
                        mloop += count;
 
1193
                        me->totloop += count;
 
1194
                        index += 4;
1214
1195
                }
1215
1196
 
1216
1197
                mesh_update_customdata_pointers(me, TRUE);
1217
1198
 
1218
 
                mesh_calc_normals(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL);
 
1199
                BKE_mesh_calc_normals(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL);
1219
1200
 
1220
1201
                BKE_mesh_calc_edges(me, TRUE);
1221
1202
        }
1223
1204
 
1224
1205
/* Initialize mverts, medges and, faces for converting nurbs to mesh and derived mesh */
1225
1206
/* return non-zero on error */
1226
 
int nurbs_to_mdata(Object *ob, MVert **allvert, int *totvert,
1227
 
        MEdge **alledge, int *totedge, MLoop **allloop, MPoly **allpoly,
1228
 
        int *totloop, int *totpoly)
 
1207
int BKE_mesh_nurbs_to_mdata(Object *ob, MVert **allvert, int *totvert,
 
1208
                            MEdge **alledge, int *totedge, MLoop **allloop, MPoly **allpoly,
 
1209
                            int *totloop, int *totpoly)
1229
1210
{
1230
 
        return nurbs_to_mdata_customdb(ob, &ob->disp,
1231
 
                                       allvert, totvert,
1232
 
                                       alledge, totedge,
1233
 
                                       allloop, allpoly,
1234
 
                                       totloop, totpoly);
 
1211
        return BKE_mesh_nurbs_displist_to_mdata(ob, &ob->disp,
 
1212
                                                allvert, totvert,
 
1213
                                                alledge, totedge,
 
1214
                                                allloop, allpoly, NULL,
 
1215
                                                totloop, totpoly);
1235
1216
}
1236
1217
 
1237
1218
/* BMESH: this doesn't calculate all edges from polygons,
1238
1219
 * only free standing edges are calculated */
1239
1220
 
1240
1221
/* Initialize mverts, medges and, faces for converting nurbs to mesh and derived mesh */
1241
 
/* use specified dispbase  */
1242
 
int nurbs_to_mdata_customdb(Object *ob, ListBase *dispbase,
1243
 
                            MVert **allvert, int *_totvert,
1244
 
                            MEdge **alledge, int *_totedge,
1245
 
                            MLoop **allloop, MPoly **allpoly,
1246
 
                            int *_totloop, int *_totpoly)
 
1222
/* use specified dispbase */
 
1223
int BKE_mesh_nurbs_displist_to_mdata(Object *ob, ListBase *dispbase,
 
1224
                                     MVert **allvert, int *_totvert,
 
1225
                                     MEdge **alledge, int *_totedge,
 
1226
                                     MLoop **allloop, MPoly **allpoly,
 
1227
                                     MLoopUV **alluv,
 
1228
                                     int *_totloop, int *_totpoly)
1247
1229
{
1248
1230
        DispList *dl;
1249
1231
        Curve *cu;
1250
1232
        MVert *mvert;
1251
1233
        MPoly *mpoly;
1252
1234
        MLoop *mloop;
 
1235
        MLoopUV *mloopuv = NULL;
1253
1236
        MEdge *medge;
1254
1237
        float *data;
1255
 
        int a, b, ofs, vertcount, startvert, totvert=0, totedge=0, totloop=0, totvlak=0;
 
1238
        int a, b, ofs, vertcount, startvert, totvert = 0, totedge = 0, totloop = 0, totvlak = 0;
1256
1239
        int p1, p2, p3, p4, *index;
1257
 
        int conv_polys= 0;
1258
 
 
1259
 
        cu= ob->data;
1260
 
 
1261
 
        conv_polys|= cu->flag & CU_3D;          /* 2d polys are filled with DL_INDEX3 displists */
1262
 
        conv_polys|= ob->type == OB_SURF;       /* surf polys are never filled */
 
1240
        int conv_polys = 0;
 
1241
 
 
1242
        cu = ob->data;
 
1243
 
 
1244
        conv_polys |= cu->flag & CU_3D;      /* 2d polys are filled with DL_INDEX3 displists */
 
1245
        conv_polys |= ob->type == OB_SURF;   /* surf polys are never filled */
1263
1246
 
1264
1247
        /* count */
1265
 
        dl= dispbase->first;
 
1248
        dl = dispbase->first;
1266
1249
        while (dl) {
1267
 
                if (dl->type==DL_SEGM) {
1268
 
                        totvert+= dl->parts*dl->nr;
1269
 
                        totedge+= dl->parts*(dl->nr-1);
 
1250
                if (dl->type == DL_SEGM) {
 
1251
                        totvert += dl->parts * dl->nr;
 
1252
                        totedge += dl->parts * (dl->nr - 1);
1270
1253
                }
1271
 
                else if (dl->type==DL_POLY) {
 
1254
                else if (dl->type == DL_POLY) {
1272
1255
                        if (conv_polys) {
1273
 
                                totvert+= dl->parts*dl->nr;
1274
 
                                totedge+= dl->parts*dl->nr;
 
1256
                                totvert += dl->parts * dl->nr;
 
1257
                                totedge += dl->parts * dl->nr;
1275
1258
                        }
1276
1259
                }
1277
 
                else if (dl->type==DL_SURF) {
 
1260
                else if (dl->type == DL_SURF) {
1278
1261
                        int tot;
1279
 
                        totvert+= dl->parts*dl->nr;
1280
 
                        tot = (dl->parts-1+((dl->flag & DL_CYCL_V)==2))*(dl->nr-1+(dl->flag & DL_CYCL_U));
 
1262
                        totvert += dl->parts * dl->nr;
 
1263
                        tot = (dl->parts - 1 + ((dl->flag & DL_CYCL_V) == 2)) * (dl->nr - 1 + (dl->flag & DL_CYCL_U));
1281
1264
                        totvlak += tot;
1282
1265
                        totloop += tot * 4;
1283
1266
                }
1284
 
                else if (dl->type==DL_INDEX3) {
 
1267
                else if (dl->type == DL_INDEX3) {
1285
1268
                        int tot;
1286
 
                        totvert+= dl->nr;
 
1269
                        totvert += dl->nr;
1287
1270
                        tot = dl->parts;
1288
 
                        totvlak+= tot;
 
1271
                        totvlak += tot;
1289
1272
                        totloop += tot * 3;
1290
1273
                }
1291
 
                dl= dl->next;
 
1274
                dl = dl->next;
1292
1275
        }
1293
1276
 
1294
 
        if (totvert==0) {
 
1277
        if (totvert == 0) {
1295
1278
                /* error("can't convert"); */
1296
1279
                /* Make Sure you check ob->data is a curve */
1297
1280
                return -1;
1301
1284
        *alledge = medge = MEM_callocN(sizeof(MEdge) * totedge, "nurbs_init medge");
1302
1285
        *allloop = mloop = MEM_callocN(sizeof(MLoop) * totvlak * 4, "nurbs_init mloop"); // totloop
1303
1286
        *allpoly = mpoly = MEM_callocN(sizeof(MPoly) * totvlak, "nurbs_init mloop");
 
1287
 
 
1288
        if (alluv)
 
1289
                *alluv = mloopuv = MEM_callocN(sizeof(MLoopUV) * totvlak * 4, "nurbs_init mloopuv");
1304
1290
        
1305
1291
        /* verts and faces */
1306
 
        vertcount= 0;
 
1292
        vertcount = 0;
1307
1293
 
1308
 
        dl= dispbase->first;
 
1294
        dl = dispbase->first;
1309
1295
        while (dl) {
1310
 
                int smooth= dl->rt & CU_SMOOTH ? 1 : 0;
 
1296
                int smooth = dl->rt & CU_SMOOTH ? 1 : 0;
1311
1297
 
1312
 
                if (dl->type==DL_SEGM) {
1313
 
                        startvert= vertcount;
1314
 
                        a= dl->parts*dl->nr;
1315
 
                        data= dl->verts;
 
1298
                if (dl->type == DL_SEGM) {
 
1299
                        startvert = vertcount;
 
1300
                        a = dl->parts * dl->nr;
 
1301
                        data = dl->verts;
1316
1302
                        while (a--) {
1317
1303
                                copy_v3_v3(mvert->co, data);
1318
 
                                data+=3;
 
1304
                                data += 3;
1319
1305
                                vertcount++;
1320
1306
                                mvert++;
1321
1307
                        }
1322
1308
 
1323
 
                        for (a=0; a<dl->parts; a++) {
1324
 
                                ofs= a*dl->nr;
1325
 
                                for (b=1; b<dl->nr; b++) {
1326
 
                                        medge->v1= startvert+ofs+b-1;
1327
 
                                        medge->v2= startvert+ofs+b;
 
1309
                        for (a = 0; a < dl->parts; a++) {
 
1310
                                ofs = a * dl->nr;
 
1311
                                for (b = 1; b < dl->nr; b++) {
 
1312
                                        medge->v1 = startvert + ofs + b - 1;
 
1313
                                        medge->v2 = startvert + ofs + b;
1328
1314
                                        medge->flag = ME_LOOSEEDGE | ME_EDGERENDER | ME_EDGEDRAW;
1329
1315
 
1330
1316
                                        medge++;
1332
1318
                        }
1333
1319
 
1334
1320
                }
1335
 
                else if (dl->type==DL_POLY) {
 
1321
                else if (dl->type == DL_POLY) {
1336
1322
                        if (conv_polys) {
1337
 
                                startvert= vertcount;
1338
 
                                a= dl->parts*dl->nr;
1339
 
                                data= dl->verts;
 
1323
                                startvert = vertcount;
 
1324
                                a = dl->parts * dl->nr;
 
1325
                                data = dl->verts;
1340
1326
                                while (a--) {
1341
1327
                                        copy_v3_v3(mvert->co, data);
1342
 
                                        data+=3;
 
1328
                                        data += 3;
1343
1329
                                        vertcount++;
1344
1330
                                        mvert++;
1345
1331
                                }
1346
1332
 
1347
 
                                for (a=0; a<dl->parts; a++) {
1348
 
                                        ofs= a*dl->nr;
1349
 
                                        for (b=0; b<dl->nr; b++) {
1350
 
                                                medge->v1= startvert+ofs+b;
1351
 
                                                if (b==dl->nr-1) medge->v2= startvert+ofs;
1352
 
                                                else medge->v2= startvert+ofs+b+1;
 
1333
                                for (a = 0; a < dl->parts; a++) {
 
1334
                                        ofs = a * dl->nr;
 
1335
                                        for (b = 0; b < dl->nr; b++) {
 
1336
                                                medge->v1 = startvert + ofs + b;
 
1337
                                                if (b == dl->nr - 1) medge->v2 = startvert + ofs;
 
1338
                                                else medge->v2 = startvert + ofs + b + 1;
1353
1339
                                                medge->flag = ME_LOOSEEDGE | ME_EDGERENDER | ME_EDGEDRAW;
1354
1340
                                                medge++;
1355
1341
                                        }
1356
1342
                                }
1357
1343
                        }
1358
1344
                }
1359
 
                else if (dl->type==DL_INDEX3) {
1360
 
                        startvert= vertcount;
1361
 
                        a= dl->nr;
1362
 
                        data= dl->verts;
 
1345
                else if (dl->type == DL_INDEX3) {
 
1346
                        startvert = vertcount;
 
1347
                        a = dl->nr;
 
1348
                        data = dl->verts;
1363
1349
                        while (a--) {
1364
1350
                                copy_v3_v3(mvert->co, data);
1365
 
                                data+=3;
 
1351
                                data += 3;
1366
1352
                                vertcount++;
1367
1353
                                mvert++;
1368
1354
                        }
1369
1355
 
1370
 
                        a= dl->parts;
1371
 
                        index= dl->index;
 
1356
                        a = dl->parts;
 
1357
                        index = dl->index;
1372
1358
                        while (a--) {
1373
 
                                mloop[0].v = startvert+index[0];
1374
 
                                mloop[1].v = startvert+index[2];
1375
 
                                mloop[2].v = startvert+index[1];
 
1359
                                mloop[0].v = startvert + index[0];
 
1360
                                mloop[1].v = startvert + index[2];
 
1361
                                mloop[2].v = startvert + index[1];
1376
1362
                                mpoly->loopstart = (int)(mloop - (*allloop));
1377
1363
                                mpoly->totloop = 3;
1378
1364
                                mpoly->mat_nr = dl->col;
1379
1365
 
 
1366
                                if (mloopuv) {
 
1367
                                        int i;
 
1368
 
 
1369
                                        for (i = 0; i < 3; i++, mloopuv++) {
 
1370
                                                mloopuv->uv[0] = (mloop[i].v - startvert) / (float)(dl->nr - 1);
 
1371
                                                mloopuv->uv[1] = 0.0f;
 
1372
                                        }
 
1373
                                }
 
1374
 
1380
1375
                                if (smooth) mpoly->flag |= ME_SMOOTH;
1381
1376
                                mpoly++;
1382
 
                                mloop+= 3;
1383
 
                                index+= 3;
 
1377
                                mloop += 3;
 
1378
                                index += 3;
1384
1379
                        }
1385
 
 
1386
 
 
1387
1380
                }
1388
 
                else if (dl->type==DL_SURF) {
1389
 
                        startvert= vertcount;
1390
 
                        a= dl->parts*dl->nr;
1391
 
                        data= dl->verts;
 
1381
                else if (dl->type == DL_SURF) {
 
1382
                        startvert = vertcount;
 
1383
                        a = dl->parts * dl->nr;
 
1384
                        data = dl->verts;
1392
1385
                        while (a--) {
1393
1386
                                copy_v3_v3(mvert->co, data);
1394
 
                                data+=3;
 
1387
                                data += 3;
1395
1388
                                vertcount++;
1396
1389
                                mvert++;
1397
1390
                        }
1398
1391
 
1399
 
                        for (a=0; a<dl->parts; a++) {
1400
 
 
1401
 
                                if ( (dl->flag & DL_CYCL_V)==0 && a==dl->parts-1) break;
1402
 
 
1403
 
                                if (dl->flag & DL_CYCL_U) {                     /* p2 -> p1 -> */
1404
 
                                        p1= startvert+ dl->nr*a;        /* p4 -> p3 -> */
1405
 
                                        p2= p1+ dl->nr-1;               /* -----> next row */
1406
 
                                        p3= p1+ dl->nr;
1407
 
                                        p4= p2+ dl->nr;
1408
 
                                        b= 0;
 
1392
                        for (a = 0; a < dl->parts; a++) {
 
1393
 
 
1394
                                if ( (dl->flag & DL_CYCL_V) == 0 && a == dl->parts - 1) break;
 
1395
 
 
1396
                                if (dl->flag & DL_CYCL_U) {         /* p2 -> p1 -> */
 
1397
                                        p1 = startvert + dl->nr * a;    /* p4 -> p3 -> */
 
1398
                                        p2 = p1 + dl->nr - 1;       /* -----> next row */
 
1399
                                        p3 = p1 + dl->nr;
 
1400
                                        p4 = p2 + dl->nr;
 
1401
                                        b = 0;
1409
1402
                                }
1410
1403
                                else {
1411
 
                                        p2= startvert+ dl->nr*a;
1412
 
                                        p1= p2+1;
1413
 
                                        p4= p2+ dl->nr;
1414
 
                                        p3= p1+ dl->nr;
1415
 
                                        b= 1;
 
1404
                                        p2 = startvert + dl->nr * a;
 
1405
                                        p1 = p2 + 1;
 
1406
                                        p4 = p2 + dl->nr;
 
1407
                                        p3 = p1 + dl->nr;
 
1408
                                        b = 1;
1416
1409
                                }
1417
 
                                if ( (dl->flag & DL_CYCL_V) && a==dl->parts-1) {
1418
 
                                        p3-= dl->parts*dl->nr;
1419
 
                                        p4-= dl->parts*dl->nr;
 
1410
                                if ( (dl->flag & DL_CYCL_V) && a == dl->parts - 1) {
 
1411
                                        p3 -= dl->parts * dl->nr;
 
1412
                                        p4 -= dl->parts * dl->nr;
1420
1413
                                }
1421
1414
 
1422
 
                                for (; b<dl->nr; b++) {
1423
 
                                        mloop[0].v= p1;
1424
 
                                        mloop[1].v= p3;
1425
 
                                        mloop[2].v= p4;
1426
 
                                        mloop[3].v= p2;
 
1415
                                for (; b < dl->nr; b++) {
 
1416
                                        mloop[0].v = p1;
 
1417
                                        mloop[1].v = p3;
 
1418
                                        mloop[2].v = p4;
 
1419
                                        mloop[3].v = p2;
1427
1420
                                        mpoly->loopstart = (int)(mloop - (*allloop));
1428
1421
                                        mpoly->totloop = 4;
1429
1422
                                        mpoly->mat_nr = dl->col;
1430
1423
 
 
1424
                                        if (mloopuv) {
 
1425
                                                int orco_sizeu = dl->nr - 1;
 
1426
                                                int orco_sizev = dl->parts - 1;
 
1427
                                                int i;
 
1428
 
 
1429
                                                /* exception as handled in convertblender.c too */
 
1430
                                                if (dl->flag & DL_CYCL_U) {
 
1431
                                                        orco_sizeu++;
 
1432
                                                        if (dl->flag & DL_CYCL_V)
 
1433
                                                                orco_sizev++;
 
1434
                                                }
 
1435
 
 
1436
                                                for (i = 0; i < 4; i++, mloopuv++) {
 
1437
                                                        /* find uv based on vertex index into grid array */
 
1438
                                                        int v = mloop[i].v - startvert;
 
1439
 
 
1440
                                                        mloopuv->uv[0] = (v / dl->nr) / (float)orco_sizev;
 
1441
                                                        mloopuv->uv[1] = (v % dl->nr) / (float)orco_sizeu;
 
1442
 
 
1443
                                                        /* cyclic correction */
 
1444
                                                        if ((i == 0 || i == 1) && mloopuv->uv[1] == 0.0f)
 
1445
                                                                mloopuv->uv[1] = 1.0f;
 
1446
                                                }
 
1447
                                        }
 
1448
 
1431
1449
                                        if (smooth) mpoly->flag |= ME_SMOOTH;
1432
1450
                                        mpoly++;
1433
 
                                        mloop+= 4;
 
1451
                                        mloop += 4;
1434
1452
 
1435
 
                                        p4= p3;
 
1453
                                        p4 = p3;
1436
1454
                                        p3++;
1437
 
                                        p2= p1;
 
1455
                                        p2 = p1;
1438
1456
                                        p1++;
1439
1457
                                }
1440
1458
                        }
1441
 
 
1442
1459
                }
1443
1460
 
1444
 
                dl= dl->next;
 
1461
                dl = dl->next;
1445
1462
        }
1446
1463
        
1447
 
        *_totpoly= totvlak;
1448
 
        *_totloop= totloop;
1449
 
        *_totedge= totedge;
1450
 
        *_totvert= totvert;
 
1464
        *_totpoly = totvlak;
 
1465
        *_totloop = totloop;
 
1466
        *_totedge = totedge;
 
1467
        *_totvert = totvert;
1451
1468
 
1452
1469
        /* not uded for bmesh */
1453
1470
#if 0
1458
1475
        return 0;
1459
1476
}
1460
1477
 
 
1478
 
1461
1479
/* this may fail replacing ob->data, be sure to check ob->type */
1462
 
void nurbs_to_mesh(Object *ob)
 
1480
void BKE_mesh_from_nurbs_displist(Object *ob, ListBase *dispbase, int use_orco_uv)
1463
1481
{
1464
 
        Main *bmain= G.main;
 
1482
        Main *bmain = G.main;
1465
1483
        Object *ob1;
1466
 
        DerivedMesh *dm= ob->derivedFinal;
 
1484
        DerivedMesh *dm = ob->derivedFinal;
1467
1485
        Mesh *me;
1468
1486
        Curve *cu;
1469
 
        MVert *allvert= NULL;
1470
 
        MEdge *alledge= NULL;
 
1487
        MVert *allvert = NULL;
 
1488
        MEdge *alledge = NULL;
1471
1489
        MLoop *allloop = NULL;
 
1490
        MLoopUV *alluv = NULL;
1472
1491
        MPoly *allpoly = NULL;
1473
1492
        int totvert, totedge, totloop, totpoly;
1474
1493
 
1475
 
        cu= ob->data;
 
1494
        cu = ob->data;
1476
1495
 
1477
1496
        if (dm == NULL) {
1478
 
                if (nurbs_to_mdata(ob, &allvert, &totvert, &alledge, &totedge, &allloop, &allpoly, &totloop, &totpoly) != 0) {
 
1497
                if (BKE_mesh_nurbs_displist_to_mdata(ob, dispbase, &allvert, &totvert,
 
1498
                                                     &alledge, &totedge, &allloop,
 
1499
                                                     &allpoly, (use_orco_uv) ? &alluv : NULL,
 
1500
                                                     &totloop, &totpoly) != 0)
 
1501
                {
1479
1502
                        /* Error initializing */
1480
1503
                        return;
1481
1504
                }
1482
1505
 
1483
1506
                /* make mesh */
1484
 
                me= add_mesh("Mesh");
1485
 
                me->totvert= totvert;
1486
 
                me->totedge= totedge;
 
1507
                me = BKE_mesh_add(G.main, "Mesh");
 
1508
                me->totvert = totvert;
 
1509
                me->totedge = totedge;
1487
1510
                me->totloop = totloop;
1488
1511
                me->totpoly = totpoly;
1489
1512
 
1490
 
                me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, allvert, me->totvert);
1491
 
                me->medge= CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, alledge, me->totedge);
1492
 
                me->mloop= CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, allloop, me->totloop);
1493
 
                me->mpoly= CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, allpoly, me->totpoly);
1494
 
 
1495
 
                mesh_calc_normals(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL);
 
1513
                me->mvert = CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, allvert, me->totvert);
 
1514
                me->medge = CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, alledge, me->totedge);
 
1515
                me->mloop = CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, allloop, me->totloop);
 
1516
                me->mpoly = CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, allpoly, me->totpoly);
 
1517
 
 
1518
                if (alluv) {
 
1519
                        const char *uvname = "Orco";
 
1520
                        me->mtpoly = CustomData_add_layer_named(&me->pdata, CD_MTEXPOLY, CD_DEFAULT, NULL, me->totpoly, uvname);
 
1521
                        me->mloopuv = CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_ASSIGN, alluv, me->totloop, uvname);
 
1522
                }
 
1523
 
 
1524
                BKE_mesh_calc_normals(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL);
1496
1525
 
1497
1526
                BKE_mesh_calc_edges(me, TRUE);
1498
1527
        }
1499
1528
        else {
1500
 
                me= add_mesh("Mesh");
 
1529
                me = BKE_mesh_add(G.main, "Mesh");
1501
1530
                DM_to_mesh(dm, me, ob);
1502
1531
        }
1503
1532
 
1504
 
        me->totcol= cu->totcol;
1505
 
        me->mat= cu->mat;
1506
 
 
1507
 
        tex_space_mesh(me);
1508
 
 
1509
 
        cu->mat= NULL;
1510
 
        cu->totcol= 0;
 
1533
        me->totcol = cu->totcol;
 
1534
        me->mat = cu->mat;
 
1535
 
 
1536
        BKE_mesh_texspace_calc(me);
 
1537
 
 
1538
        cu->mat = NULL;
 
1539
        cu->totcol = 0;
1511
1540
 
1512
1541
        if (ob->data) {
1513
 
                free_libblock(&bmain->curve, ob->data);
 
1542
                BKE_libblock_free(&bmain->curve, ob->data);
1514
1543
        }
1515
 
        ob->data= me;
1516
 
        ob->type= OB_MESH;
 
1544
        ob->data = me;
 
1545
        ob->type = OB_MESH;
1517
1546
 
1518
1547
        /* other users */
1519
 
        ob1= bmain->object.first;
 
1548
        ob1 = bmain->object.first;
1520
1549
        while (ob1) {
1521
 
                if (ob1->data==cu) {
1522
 
                        ob1->type= OB_MESH;
 
1550
                if (ob1->data == cu) {
 
1551
                        ob1->type = OB_MESH;
1523
1552
                
1524
 
                        ob1->data= ob->data;
 
1553
                        ob1->data = ob->data;
1525
1554
                        id_us_plus((ID *)ob->data);
1526
1555
                }
1527
 
                ob1= ob1->id.next;
 
1556
                ob1 = ob1->id.next;
1528
1557
        }
1529
1558
}
1530
1559
 
 
1560
void BKE_mesh_from_nurbs(Object *ob)
 
1561
{
 
1562
        BKE_mesh_from_nurbs_displist(ob, &ob->disp, false);
 
1563
}
 
1564
 
1531
1565
typedef struct EdgeLink {
1532
1566
        Link *next, *prev;
1533
1567
        void *edge;
1540
1574
 
1541
1575
static void prependPolyLineVert(ListBase *lb, unsigned int index)
1542
1576
{
1543
 
        VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
 
1577
        VertLink *vl = MEM_callocN(sizeof(VertLink), "VertLink");
1544
1578
        vl->index = index;
1545
1579
        BLI_addhead(lb, vl);
1546
1580
}
1547
1581
 
1548
1582
static void appendPolyLineVert(ListBase *lb, unsigned int index)
1549
1583
{
1550
 
        VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
 
1584
        VertLink *vl = MEM_callocN(sizeof(VertLink), "VertLink");
1551
1585
        vl->index = index;
1552
1586
        BLI_addtail(lb, vl);
1553
1587
}
1554
1588
 
1555
 
void mesh_to_curve(Scene *scene, Object *ob)
 
1589
void BKE_mesh_from_curve(Scene *scene, Object *ob)
1556
1590
{
1557
1591
        /* make new mesh data from the original copy */
1558
 
        DerivedMesh *dm= mesh_get_derived_final(scene, ob, CD_MASK_MESH);
 
1592
        DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_MESH);
1559
1593
 
1560
 
        MVert *mverts= dm->getVertArray(dm);
1561
 
        MEdge *med, *medge= dm->getEdgeArray(dm);
1562
 
        MFace *mf,  *mface= dm->getTessFaceArray(dm);
 
1594
        MVert *mverts = dm->getVertArray(dm);
 
1595
        MEdge *med, *medge = dm->getEdgeArray(dm);
 
1596
        MFace *mf,  *mface = dm->getTessFaceArray(dm);
1563
1597
 
1564
1598
        int totedge = dm->getNumEdges(dm);
1565
1599
        int totface = dm->getNumTessFaces(dm);
1574
1608
        ListBase edges = {NULL, NULL};
1575
1609
 
1576
1610
        /* create edges from all faces (so as to find edges not in any faces) */
1577
 
        mf= mface;
 
1611
        mf = mface;
1578
1612
        for (i = 0; i < totface; i++, mf++) {
1579
1613
                if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
1580
1614
                        BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
1593
1627
                }
1594
1628
        }
1595
1629
 
1596
 
        med= medge;
1597
 
        for (i=0; i<totedge; i++, med++) {
 
1630
        med = medge;
 
1631
        for (i = 0; i < totedge; i++, med++) {
1598
1632
                if (!BLI_edgehash_haskey(eh, med->v1, med->v2)) {
1599
 
                        EdgeLink *edl= MEM_callocN(sizeof(EdgeLink), "EdgeLink");
 
1633
                        EdgeLink *edl = MEM_callocN(sizeof(EdgeLink), "EdgeLink");
1600
1634
 
1601
1635
                        BLI_edgehash_insert(eh_edge, med->v1, med->v2, NULL);
1602
 
                        edl->edge= med;
 
1636
                        edl->edge = med;
1603
1637
 
1604
 
                        BLI_addtail(&edges, edl);       totedges++;
 
1638
                        BLI_addtail(&edges, edl);   totedges++;
1605
1639
                }
1606
1640
        }
1607
1641
        BLI_edgehash_free(eh_edge, NULL);
1608
1642
        BLI_edgehash_free(eh, NULL);
1609
1643
 
1610
1644
        if (edges.first) {
1611
 
                Curve *cu = add_curve(ob->id.name+2, OB_CURVE);
 
1645
                Curve *cu = BKE_curve_add(G.main, ob->id.name + 2, OB_CURVE);
1612
1646
                cu->flag |= CU_3D;
1613
1647
 
1614
1648
                while (edges.first) {
1616
1650
 
1617
1651
                        ListBase polyline = {NULL, NULL}; /* store a list of VertLink's */
1618
1652
                        int closed = FALSE;
1619
 
                        int totpoly= 0;
1620
 
                        MEdge *med_current= ((EdgeLink *)edges.last)->edge;
1621
 
                        unsigned int startVert= med_current->v1;
1622
 
                        unsigned int endVert= med_current->v2;
1623
 
                        int ok= TRUE;
 
1653
                        int totpoly = 0;
 
1654
                        MEdge *med_current = ((EdgeLink *)edges.last)->edge;
 
1655
                        unsigned int startVert = med_current->v1;
 
1656
                        unsigned int endVert = med_current->v2;
 
1657
                        int ok = TRUE;
1624
1658
 
1625
 
                        appendPolyLineVert(&polyline, startVert);       totpoly++;
1626
 
                        appendPolyLineVert(&polyline, endVert);         totpoly++;
1627
 
                        BLI_freelinkN(&edges, edges.last);                      totedges--;
 
1659
                        appendPolyLineVert(&polyline, startVert);   totpoly++;
 
1660
                        appendPolyLineVert(&polyline, endVert);     totpoly++;
 
1661
                        BLI_freelinkN(&edges, edges.last);          totedges--;
1628
1662
 
1629
1663
                        while (ok) { /* while connected edges are found... */
1630
1664
                                ok = FALSE;
1631
 
                                i= totedges;
 
1665
                                i = totedges;
1632
1666
                                while (i) {
1633
1667
                                        EdgeLink *edl;
1634
1668
 
1635
 
                                        i-=1;
1636
 
                                        edl= BLI_findlink(&edges, i);
1637
 
                                        med= edl->edge;
 
1669
                                        i -= 1;
 
1670
                                        edl = BLI_findlink(&edges, i);
 
1671
                                        med = edl->edge;
1638
1672
 
1639
 
                                        if (med->v1==endVert) {
 
1673
                                        if (med->v1 == endVert) {
1640
1674
                                                endVert = med->v2;
1641
 
                                                appendPolyLineVert(&polyline, med->v2); totpoly++;
1642
 
                                                BLI_freelinkN(&edges, edl);                             totedges--;
1643
 
                                                ok= TRUE;
 
1675
                                                appendPolyLineVert(&polyline, med->v2); totpoly++;
 
1676
                                                BLI_freelinkN(&edges, edl);             totedges--;
 
1677
                                                ok = TRUE;
1644
1678
                                        }
1645
 
                                        else if (med->v2==endVert) {
 
1679
                                        else if (med->v2 == endVert) {
1646
1680
                                                endVert = med->v1;
1647
 
                                                appendPolyLineVert(&polyline, endVert); totpoly++;
1648
 
                                                BLI_freelinkN(&edges, edl);                             totedges--;
1649
 
                                                ok= TRUE;
 
1681
                                                appendPolyLineVert(&polyline, endVert); totpoly++;
 
1682
                                                BLI_freelinkN(&edges, edl);             totedges--;
 
1683
                                                ok = TRUE;
1650
1684
                                        }
1651
 
                                        else if (med->v1==startVert) {
 
1685
                                        else if (med->v1 == startVert) {
1652
1686
                                                startVert = med->v2;
1653
 
                                                prependPolyLineVert(&polyline, startVert);      totpoly++;
1654
 
                                                BLI_freelinkN(&edges, edl);                                     totedges--;
1655
 
                                                ok= TRUE;
 
1687
                                                prependPolyLineVert(&polyline, startVert);  totpoly++;
 
1688
                                                BLI_freelinkN(&edges, edl);                 totedges--;
 
1689
                                                ok = TRUE;
1656
1690
                                        }
1657
 
                                        else if (med->v2==startVert) {
 
1691
                                        else if (med->v2 == startVert) {
1658
1692
                                                startVert = med->v1;
1659
 
                                                prependPolyLineVert(&polyline, startVert);      totpoly++;
1660
 
                                                BLI_freelinkN(&edges, edl);                                     totedges--;
1661
 
                                                ok= TRUE;
 
1693
                                                prependPolyLineVert(&polyline, startVert);  totpoly++;
 
1694
                                                BLI_freelinkN(&edges, edl);                 totedges--;
 
1695
                                                ok = TRUE;
1662
1696
                                        }
1663
1697
                                }
1664
1698
                        }
1665
1699
 
1666
1700
                        /* Now we have a polyline, make into a curve */
1667
 
                        if (startVert==endVert) {
 
1701
                        if (startVert == endVert) {
1668
1702
                                BLI_freelinkN(&polyline, polyline.last);
1669
1703
                                totpoly--;
1670
1704
                                closed = TRUE;
1679
1713
                                /* create new 'nurb' within the curve */
1680
1714
                                nu = (Nurb *)MEM_callocN(sizeof(Nurb), "MeshNurb");
1681
1715
 
1682
 
                                nu->pntsu= totpoly;
1683
 
                                nu->pntsv= 1;
1684
 
                                nu->orderu= 4;
1685
 
                                nu->flagu= CU_NURB_ENDPOINT | (closed ? CU_NURB_CYCLIC:0);      /* endpoint */
1686
 
                                nu->resolu= 12;
 
1716
                                nu->pntsu = totpoly;
 
1717
                                nu->pntsv = 1;
 
1718
                                nu->orderu = 4;
 
1719
                                nu->flagu = CU_NURB_ENDPOINT | (closed ? CU_NURB_CYCLIC : 0);  /* endpoint */
 
1720
                                nu->resolu = 12;
1687
1721
 
1688
 
                                nu->bp= (BPoint *)MEM_callocN(sizeof(BPoint)*totpoly, "bpoints");
 
1722
                                nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * totpoly, "bpoints");
1689
1723
 
1690
1724
                                /* add points */
1691
 
                                vl= polyline.first;
1692
 
                                for (i=0, bp=nu->bp; i < totpoly; i++, bp++, vl=(VertLink *)vl->next) {
 
1725
                                vl = polyline.first;
 
1726
                                for (i = 0, bp = nu->bp; i < totpoly; i++, bp++, vl = (VertLink *)vl->next) {
1693
1727
                                        copy_v3_v3(bp->vec, mverts[vl->index].co);
1694
 
                                        bp->f1= SELECT;
 
1728
                                        bp->f1 = SELECT;
1695
1729
                                        bp->radius = bp->weight = 1.0;
1696
1730
                                }
1697
1731
                                BLI_freelistN(&polyline);
1703
1737
                }
1704
1738
 
1705
1739
                ((Mesh *)ob->data)->id.us--;
1706
 
                ob->data= cu;
1707
 
                ob->type= OB_CURVE;
 
1740
                ob->data = cu;
 
1741
                ob->type = OB_CURVE;
1708
1742
 
1709
1743
                /* curve objects can't contain DM in usual cases, we could free memory */
1710
 
                needsFree= 1;
 
1744
                needsFree = 1;
1711
1745
        }
1712
1746
 
1713
1747
        dm->needsFree = needsFree;
1719
1753
                /* curve object could have got bounding box only in special cases */
1720
1754
                if (ob->bb) {
1721
1755
                        MEM_freeN(ob->bb);
1722
 
                        ob->bb= NULL;
 
1756
                        ob->bb = NULL;
1723
1757
                }
1724
1758
        }
1725
1759
}
1726
1760
 
1727
 
void mesh_delete_material_index(Mesh *me, short index)
 
1761
void BKE_mesh_delete_material_index(Mesh *me, short index)
1728
1762
{
1729
1763
        int i;
1730
1764
 
1731
 
        for (i=0; i<me->totpoly; i++) {
1732
 
                MPoly *mp = &((MPoly*) me->mpoly)[i];
1733
 
                if (mp->mat_nr && mp->mat_nr>=index) 
 
1765
        for (i = 0; i < me->totpoly; i++) {
 
1766
                MPoly *mp = &((MPoly *) me->mpoly)[i];
 
1767
                if (mp->mat_nr && mp->mat_nr >= index)
1734
1768
                        mp->mat_nr--;
1735
1769
        }
1736
1770
        
1737
 
        for (i=0; i<me->totface; i++) {
1738
 
                MFace *mf = &((MFace*) me->mface)[i];
1739
 
                if (mf->mat_nr && mf->mat_nr>=index) 
 
1771
        for (i = 0; i < me->totface; i++) {
 
1772
                MFace *mf = &((MFace *) me->mface)[i];
 
1773
                if (mf->mat_nr && mf->mat_nr >= index)
1740
1774
                        mf->mat_nr--;
1741
1775
        }
1742
1776
}
1743
1777
 
1744
 
void mesh_set_smooth_flag(Object *meshOb, int enableSmooth) 
 
1778
void BKE_mesh_smooth_flag_set(Object *meshOb, int enableSmooth) 
1745
1779
{
1746
1780
        Mesh *me = meshOb->data;
1747
1781
        int i;
1748
1782
 
1749
 
        for (i=0; i<me->totpoly; i++) {
1750
 
                MPoly *mp = &((MPoly*) me->mpoly)[i];
 
1783
        for (i = 0; i < me->totpoly; i++) {
 
1784
                MPoly *mp = &((MPoly *) me->mpoly)[i];
1751
1785
 
1752
1786
                if (enableSmooth) {
1753
1787
                        mp->flag |= ME_SMOOTH;
1757
1791
                }
1758
1792
        }
1759
1793
        
1760
 
        for (i=0; i<me->totface; i++) {
1761
 
                MFace *mf = &((MFace*) me->mface)[i];
 
1794
        for (i = 0; i < me->totface; i++) {
 
1795
                MFace *mf = &((MFace *) me->mface)[i];
1762
1796
 
1763
1797
                if (enableSmooth) {
1764
1798
                        mf->flag |= ME_SMOOTH;
1769
1803
        }
1770
1804
}
1771
1805
 
1772
 
void mesh_calc_normals_mapping(MVert *mverts, int numVerts,
1773
 
                                MLoop *mloop, MPoly *mpolys, int numLoops, int numPolys, float (*polyNors_r)[3],
1774
 
                                MFace *mfaces, int numFaces, int *origIndexFace, float (*faceNors_r)[3])
 
1806
void BKE_mesh_calc_normals_mapping(MVert *mverts, int numVerts,
 
1807
                                   MLoop *mloop, MPoly *mpolys, int numLoops, int numPolys, float (*polyNors_r)[3],
 
1808
                                   MFace *mfaces, int numFaces, int *origIndexFace, float (*faceNors_r)[3])
1775
1809
{
1776
 
        mesh_calc_normals_mapping_ex(mverts, numVerts, mloop, mpolys,
1777
 
                                      numLoops, numPolys, polyNors_r, mfaces, numFaces,
1778
 
                                      origIndexFace, faceNors_r, FALSE);
 
1810
        BKE_mesh_calc_normals_mapping_ex(mverts, numVerts, mloop, mpolys,
 
1811
                                         numLoops, numPolys, polyNors_r, mfaces, numFaces,
 
1812
                                         origIndexFace, faceNors_r, FALSE);
1779
1813
}
1780
1814
 
1781
 
void mesh_calc_normals_mapping_ex(MVert *mverts, int numVerts,
1782
 
                                   MLoop *mloop, MPoly *mpolys,
1783
 
                                   int numLoops, int numPolys, float (*polyNors_r)[3],
1784
 
                                   MFace *mfaces, int numFaces, int *origIndexFace, float (*faceNors_r)[3],
1785
 
                                   const short only_face_normals)
 
1815
void BKE_mesh_calc_normals_mapping_ex(MVert *mverts, int numVerts,
 
1816
                                      MLoop *mloop, MPoly *mpolys,
 
1817
                                      int numLoops, int numPolys, float (*polyNors_r)[3],
 
1818
                                      MFace *mfaces, int numFaces, int *origIndexFace, float (*faceNors_r)[3],
 
1819
                                      const short only_face_normals)
1786
1820
{
1787
1821
        float (*pnors)[3] = polyNors_r, (*fnors)[3] = faceNors_r;
1788
1822
        int i;
1793
1827
                return;
1794
1828
        }
1795
1829
 
1796
 
        /* if we are not calculating verts and no verts were passes thene we have nothign to do */
 
1830
        /* if we are not calculating verts and no verts were passes then we have nothing to do */
1797
1831
        if ((only_face_normals == TRUE) && (polyNors_r == NULL) && (faceNors_r == NULL)) {
1798
1832
                printf("%s: called with nothing to do\n", __func__);
1799
1833
                return;
1806
1840
        if (only_face_normals == FALSE) {
1807
1841
                /* vertex normals are optional, they require some extra calculations,
1808
1842
                 * so make them optional */
1809
 
                mesh_calc_normals(mverts, numVerts, mloop, mpolys, numLoops, numPolys, pnors);
 
1843
                BKE_mesh_calc_normals(mverts, numVerts, mloop, mpolys, numLoops, numPolys, pnors);
1810
1844
        }
1811
1845
        else {
1812
1846
                /* only calc poly normals */
1813
1847
                mp = mpolys;
1814
 
                for (i=0; i<numPolys; i++, mp++) {
1815
 
                        mesh_calc_poly_normal(mp, mloop+mp->loopstart, mverts, pnors[i]);
 
1848
                for (i = 0; i < numPolys; i++, mp++) {
 
1849
                        BKE_mesh_calc_poly_normal(mp, mloop + mp->loopstart, mverts, pnors[i]);
1816
1850
                }
1817
1851
        }
1818
1852
 
1819
 
        if ( origIndexFace &&
1820
 
             /* fnors==faceNors_r */ /* NO NEED TO ALLOC YET */
1821
 
             fnors != NULL &&
1822
 
             numFaces)
 
1853
        if (origIndexFace &&
 
1854
            /* fnors == faceNors_r */ /* NO NEED TO ALLOC YET */
 
1855
            fnors != NULL &&
 
1856
            numFaces)
1823
1857
        {
1824
1858
                mf = mfaces;
1825
 
                for (i=0; i<numFaces; i++, mf++, origIndexFace++) {
 
1859
                for (i = 0; i < numFaces; i++, mf++, origIndexFace++) {
1826
1860
                        if (*origIndexFace < numPolys) {
1827
1861
                                copy_v3_v3(fnors[i], pnors[*origIndexFace]);
1828
1862
                        }
1829
1863
                        else {
1830
1864
                                /* eek, we're not corresponding to polys */
1831
 
                                printf("error in mesh_calc_normals; tessellation face indices are incorrect.  normals may look bad.\n");
 
1865
                                printf("error in BKE_mesh_calc_normals; tessellation face indices are incorrect.  normals may look bad.\n");
1832
1866
                        }
1833
1867
                }
1834
1868
        }
1840
1874
        
1841
1875
}
1842
1876
 
1843
 
void mesh_calc_normals(MVert *mverts, int numVerts, MLoop *mloop, MPoly *mpolys,
1844
 
                       int UNUSED(numLoops), int numPolys, float (*polyNors_r)[3])
 
1877
void BKE_mesh_calc_normals(MVert *mverts, int numVerts, MLoop *mloop, MPoly *mpolys,
 
1878
                           int UNUSED(numLoops), int numPolys, float (*polyNors_r)[3])
1845
1879
{
1846
1880
        float (*pnors)[3] = polyNors_r;
1847
1881
 
1848
 
        float (*tnorms)[3], (*edgevecbuf)[3]= NULL;
 
1882
        float (*tnorms)[3], (*edgevecbuf)[3] = NULL;
1849
1883
        float **vertcos = NULL, **vertnos = NULL;
1850
1884
        BLI_array_declare(vertcos);
1851
1885
        BLI_array_declare(vertnos);
1857
1891
 
1858
1892
        if (!pnors) pnors = MEM_callocN(sizeof(float) * 3 * numPolys, "poly_nors mesh.c");
1859
1893
 
1860
 
        /*first go through and calculate normals for all the polys*/
1861
 
        tnorms = MEM_callocN(sizeof(float)*3*numVerts, "tnorms mesh.c");
 
1894
        /* first go through and calculate normals for all the polys */
 
1895
        tnorms = MEM_callocN(sizeof(float) * 3 * numVerts, "tnorms mesh.c");
1862
1896
 
1863
1897
        mp = mpolys;
1864
 
        for (i=0; i<numPolys; i++, mp++) {
1865
 
                mesh_calc_poly_normal(mp, mloop+mp->loopstart, mverts, pnors[i]);
 
1898
        for (i = 0; i < numPolys; i++, mp++) {
 
1899
                BKE_mesh_calc_poly_normal(mp, mloop + mp->loopstart, mverts, pnors[i]);
1866
1900
                ml = mloop + mp->loopstart;
1867
1901
 
1868
1902
                BLI_array_empty(vertcos);
1869
1903
                BLI_array_empty(vertnos);
1870
 
                BLI_array_growitems(vertcos, mp->totloop);
1871
 
                BLI_array_growitems(vertnos, mp->totloop);
 
1904
                BLI_array_grow_items(vertcos, mp->totloop);
 
1905
                BLI_array_grow_items(vertnos, mp->totloop);
1872
1906
 
1873
 
                for (j=0; j < mp->totloop; j++) {
 
1907
                for (j = 0; j < mp->totloop; j++) {
1874
1908
                        int vindex = ml[j].v;
1875
1909
                        vertcos[j] = mverts[vindex].co;
1876
1910
                        vertnos[j] = tnorms[vindex];
1877
1911
                }
1878
1912
 
1879
1913
                BLI_array_empty(edgevecbuf);
1880
 
                BLI_array_growitems(edgevecbuf, mp->totloop);
 
1914
                BLI_array_grow_items(edgevecbuf, mp->totloop);
1881
1915
 
1882
1916
                accumulate_vertex_normals_poly(vertnos, pnors[i], vertcos, edgevecbuf, mp->totloop);
1883
1917
        }
1887
1921
        BLI_array_free(edgevecbuf);
1888
1922
 
1889
1923
        /* following Mesh convention; we use vertex coordinate itself for normal in this case */
1890
 
        for (i=0; i<numVerts; i++) {
1891
 
                MVert *mv= &mverts[i];
1892
 
                float *no= tnorms[i];
 
1924
        for (i = 0; i < numVerts; i++) {
 
1925
                MVert *mv = &mverts[i];
 
1926
                float *no = tnorms[i];
1893
1927
 
1894
 
                if (normalize_v3(no) == 0.0f)
 
1928
                if (UNLIKELY(normalize_v3(no) == 0.0f)) {
1895
1929
                        normalize_v3_v3(no, mv->co);
 
1930
                }
1896
1931
 
1897
1932
                normal_float_to_short_v3(mv->no, no);
1898
1933
        }
1902
1937
        if (pnors != polyNors_r) MEM_freeN(pnors);
1903
1938
}
1904
1939
 
1905
 
void mesh_calc_normals_tessface(MVert *mverts, int numVerts, MFace *mfaces, int numFaces, float (*faceNors_r)[3])
 
1940
void BKE_mesh_calc_normals_tessface(MVert *mverts, int numVerts, MFace *mfaces, int numFaces, float (*faceNors_r)[3])
1906
1941
{
1907
 
        float (*tnorms)[3]= MEM_callocN(numVerts*sizeof(*tnorms), "tnorms");
1908
 
        float (*fnors)[3]= (faceNors_r)? faceNors_r: MEM_callocN(sizeof(*fnors)*numFaces, "meshnormals");
 
1942
        float (*tnorms)[3] = MEM_callocN(numVerts * sizeof(*tnorms), "tnorms");
 
1943
        float (*fnors)[3] = (faceNors_r) ? faceNors_r : MEM_callocN(sizeof(*fnors) * numFaces, "meshnormals");
1909
1944
        int i;
1910
1945
 
1911
 
        for (i=0; i<numFaces; i++) {
1912
 
                MFace *mf= &mfaces[i];
1913
 
                float *f_no= fnors[i];
1914
 
                float *n4 = (mf->v4)? tnorms[mf->v4]: NULL;
1915
 
                float *c4 = (mf->v4)? mverts[mf->v4].co: NULL;
 
1946
        for (i = 0; i < numFaces; i++) {
 
1947
                MFace *mf = &mfaces[i];
 
1948
                float *f_no = fnors[i];
 
1949
                float *n4 = (mf->v4) ? tnorms[mf->v4] : NULL;
 
1950
                float *c4 = (mf->v4) ? mverts[mf->v4].co : NULL;
1916
1951
 
1917
1952
                if (mf->v4)
1918
1953
                        normal_quad_v3(f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co);
1920
1955
                        normal_tri_v3(f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co);
1921
1956
 
1922
1957
                accumulate_vertex_normals(tnorms[mf->v1], tnorms[mf->v2], tnorms[mf->v3], n4,
1923
 
                        f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, c4);
 
1958
                                          f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, c4);
1924
1959
        }
1925
1960
 
1926
1961
        /* following Mesh convention; we use vertex coordinate itself for normal in this case */
1927
 
        for (i=0; i<numVerts; i++) {
1928
 
                MVert *mv= &mverts[i];
1929
 
                float *no= tnorms[i];
 
1962
        for (i = 0; i < numVerts; i++) {
 
1963
                MVert *mv = &mverts[i];
 
1964
                float *no = tnorms[i];
1930
1965
                
1931
 
                if (normalize_v3(no) == 0.0f)
 
1966
                if (UNLIKELY(normalize_v3(no) == 0.0f)) {
1932
1967
                        normalize_v3_v3(no, mv->co);
 
1968
                }
1933
1969
 
1934
1970
                normal_float_to_short_v3(mv->no, no);
1935
1971
        }
1940
1976
                MEM_freeN(fnors);
1941
1977
}
1942
1978
 
1943
 
 
1944
 
static void bm_corners_to_loops(Mesh *me, int findex, int loopstart, int numTex, int numCol)
 
1979
static void bm_corners_to_loops_ex(ID *id, CustomData *fdata, CustomData *ldata, CustomData *pdata,
 
1980
                                   MFace *mface, int totloop, int findex, int loopstart, int numTex, int numCol)
1945
1981
{
1946
1982
        MTFace *texface;
1947
1983
        MTexPoly *texpoly;
1951
1987
        MFace *mf;
1952
1988
        int i;
1953
1989
 
1954
 
        mf = me->mface + findex;
1955
 
 
1956
 
        for (i=0; i < numTex; i++) {
1957
 
                texface = CustomData_get_n(&me->fdata, CD_MTFACE, findex, i);
1958
 
                texpoly = CustomData_get_n(&me->pdata, CD_MTEXPOLY, findex, i); 
1959
 
                
 
1990
        mf = mface + findex;
 
1991
 
 
1992
        for (i = 0; i < numTex; i++) {
 
1993
                texface = CustomData_get_n(fdata, CD_MTFACE, findex, i);
 
1994
                texpoly = CustomData_get_n(pdata, CD_MTEXPOLY, findex, i);
 
1995
 
1960
1996
                ME_MTEXFACE_CPY(texpoly, texface);
1961
 
        
1962
 
                mloopuv = CustomData_get_n(&me->ldata, CD_MLOOPUV, loopstart, i);
 
1997
 
 
1998
                mloopuv = CustomData_get_n(ldata, CD_MLOOPUV, loopstart, i);
1963
1999
                copy_v2_v2(mloopuv->uv, texface->uv[0]); mloopuv++;
1964
2000
                copy_v2_v2(mloopuv->uv, texface->uv[1]); mloopuv++;
1965
2001
                copy_v2_v2(mloopuv->uv, texface->uv[2]); mloopuv++;
1969
2005
                }
1970
2006
        }
1971
2007
 
1972
 
        for (i=0; i < numCol; i++) {
1973
 
                mloopcol = CustomData_get_n(&me->ldata, CD_MLOOPCOL, loopstart, i);
1974
 
                mcol = CustomData_get_n(&me->fdata, CD_MCOL, findex, i);
 
2008
        for (i = 0; i < numCol; i++) {
 
2009
                mloopcol = CustomData_get_n(ldata, CD_MLOOPCOL, loopstart, i);
 
2010
                mcol = CustomData_get_n(fdata, CD_MCOL, findex, i);
1975
2011
 
1976
2012
                MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[0]); mloopcol++;
1977
2013
                MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[1]); mloopcol++;
1980
2016
                        MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[3]); mloopcol++;
1981
2017
                }
1982
2018
        }
1983
 
        
1984
 
        if (CustomData_has_layer(&me->fdata, CD_MDISPS)) {
1985
 
                MDisps *ld = CustomData_get(&me->ldata, loopstart, CD_MDISPS);
1986
 
                MDisps *fd = CustomData_get(&me->fdata, findex, CD_MDISPS);
 
2019
 
 
2020
        if (CustomData_has_layer(fdata, CD_MDISPS)) {
 
2021
                MDisps *ld = CustomData_get(ldata, loopstart, CD_MDISPS);
 
2022
                MDisps *fd = CustomData_get(fdata, findex, CD_MDISPS);
1987
2023
                float (*disps)[3] = fd->disps;
1988
 
                int i, tot = mf->v4 ? 4 : 3;
 
2024
                int tot = mf->v4 ? 4 : 3;
1989
2025
                int side, corners;
1990
2026
 
1991
 
                if (CustomData_external_test(&me->fdata, CD_MDISPS)) {
1992
 
                        CustomData_external_add(&me->ldata, &me->id, CD_MDISPS,
1993
 
                                                                        me->totloop, me->fdata.external->filename);
 
2027
                if (CustomData_external_test(fdata, CD_MDISPS)) {
 
2028
                        if (id && fdata->external) {
 
2029
                                CustomData_external_add(ldata, id, CD_MDISPS,
 
2030
                                                        totloop, fdata->external->filename);
 
2031
                        }
1994
2032
                }
1995
 
                
 
2033
 
1996
2034
                corners = multires_mdisp_corners(fd);
1997
 
                
 
2035
 
1998
2036
                if (corners == 0) {
1999
2037
                        /* Empty MDisp layers appear in at least one of the sintel.blend files.
2000
2038
                         * Not sure why this happens, but it seems fine to just ignore them here.
2001
 
                         * If corners==0 for a non-empty layer though, something went wrong. */
 
2039
                         * If (corners == 0) for a non-empty layer though, something went wrong. */
2002
2040
                        BLI_assert(fd->totdisp == 0);
2003
2041
                }
2004
2042
                else {
2005
2043
                        side = sqrt(fd->totdisp / corners);
2006
 
                
2007
 
                        for (i=0; i<tot; i++, disps += side*side, ld++) {
2008
 
                                ld->totdisp = side*side;
2009
 
                                ld->level = (int)(logf(side - 1.0f) / M_LN2) + 1;
2010
 
                        
 
2044
 
 
2045
                        for (i = 0; i < tot; i++, disps += side * side, ld++) {
 
2046
                                ld->totdisp = side * side;
 
2047
                                ld->level = (int)(logf(side - 1.0f) / (float)M_LN2) + 1;
 
2048
 
2011
2049
                                if (ld->disps)
2012
2050
                                        MEM_freeN(ld->disps);
2013
 
                        
2014
 
                                ld->disps = MEM_callocN(sizeof(float)*3*side*side, "converted loop mdisps");
 
2051
 
 
2052
                                ld->disps = MEM_callocN(sizeof(float) * 3 * side * side, "converted loop mdisps");
2015
2053
                                if (fd->disps) {
2016
 
                                        memcpy(ld->disps, disps, sizeof(float)*3*side*side);
 
2054
                                        memcpy(ld->disps, disps, sizeof(float) * 3 * side * side);
2017
2055
                                }
2018
2056
                        }
2019
2057
                }
2022
2060
 
2023
2061
void BKE_mesh_convert_mfaces_to_mpolys(Mesh *mesh)
2024
2062
{
 
2063
        BKE_mesh_convert_mfaces_to_mpolys_ex(&mesh->id, &mesh->fdata, &mesh->ldata, &mesh->pdata,
 
2064
                                             mesh->totedge, mesh->totface, mesh->totloop, mesh->totpoly,
 
2065
                                             mesh->medge, mesh->mface,
 
2066
                                             &mesh->totloop, &mesh->totpoly, &mesh->mloop, &mesh->mpoly);
 
2067
 
 
2068
        mesh_update_customdata_pointers(mesh, TRUE);
 
2069
}
 
2070
 
 
2071
/* the same as BKE_mesh_convert_mfaces_to_mpolys but oriented to be used in do_versions from readfile.c
 
2072
 * the difference is how active/render/clone/stencil indices are handled here
 
2073
 *
 
2074
 * normally thay're being set from pdata which totally makes sense for meshes which are already
 
2075
 * converted to bmesh structures, but when loading older files indices shall be updated in other
 
2076
 * way around, so newly added pdata and ldata would have this indices set based on fdata layer
 
2077
 *
 
2078
 * this is normally only needed when reading older files, in all other cases BKE_mesh_convert_mfaces_to_mpolys
 
2079
 * shall be always used
 
2080
 */
 
2081
void BKE_mesh_do_versions_convert_mfaces_to_mpolys(Mesh *mesh)
 
2082
{
 
2083
        BKE_mesh_convert_mfaces_to_mpolys_ex(&mesh->id, &mesh->fdata, &mesh->ldata, &mesh->pdata,
 
2084
                                             mesh->totedge, mesh->totface, mesh->totloop, mesh->totpoly,
 
2085
                                             mesh->medge, mesh->mface,
 
2086
                                             &mesh->totloop, &mesh->totpoly, &mesh->mloop, &mesh->mpoly);
 
2087
 
 
2088
        CustomData_bmesh_do_versions_update_active_layers(&mesh->fdata, &mesh->pdata, &mesh->ldata);
 
2089
 
 
2090
        mesh_update_customdata_pointers(mesh, TRUE);
 
2091
}
 
2092
 
 
2093
void BKE_mesh_convert_mfaces_to_mpolys_ex(ID *id, CustomData *fdata, CustomData *ldata, CustomData *pdata,
 
2094
                                          int totedge_i, int totface_i, int totloop_i, int totpoly_i,
 
2095
                                          MEdge *medge, MFace *mface,
 
2096
                                          int *totloop_r, int *totpoly_r,
 
2097
                                          MLoop **mloop_r, MPoly **mpoly_r)
 
2098
{
2025
2099
        MFace *mf;
2026
 
        MLoop *ml;
2027
 
        MPoly *mp;
 
2100
        MLoop *ml, *mloop;
 
2101
        MPoly *mp, *mpoly;
2028
2102
        MEdge *me;
2029
2103
        EdgeHash *eh;
2030
2104
        int numTex, numCol;
2031
 
        int i, j, totloop;
 
2105
        int i, j, totloop, totpoly, *polyindex;
2032
2106
 
2033
2107
        /* just in case some of these layers are filled in (can happen with python created meshes) */
2034
 
        CustomData_free(&mesh->ldata, mesh->totloop);
2035
 
        CustomData_free(&mesh->pdata, mesh->totpoly);
2036
 
        memset(&mesh->ldata, 0, sizeof(mesh->ldata));
2037
 
        memset(&mesh->pdata, 0, sizeof(mesh->pdata));
2038
 
 
2039
 
        mesh->totpoly = mesh->totface;
2040
 
        mesh->mpoly = MEM_callocN(sizeof(MPoly)*mesh->totpoly, "mpoly converted");
2041
 
        CustomData_add_layer(&mesh->pdata, CD_MPOLY, CD_ASSIGN, mesh->mpoly, mesh->totpoly);
2042
 
 
2043
 
        numTex = CustomData_number_of_layers(&mesh->fdata, CD_MTFACE);
2044
 
        numCol = CustomData_number_of_layers(&mesh->fdata, CD_MCOL);
2045
 
        
 
2108
        CustomData_free(ldata, totloop_i);
 
2109
        CustomData_free(pdata, totpoly_i);
 
2110
 
 
2111
        totpoly = totface_i;
 
2112
        mpoly = MEM_callocN(sizeof(MPoly) * totpoly, "mpoly converted");
 
2113
        CustomData_add_layer(pdata, CD_MPOLY, CD_ASSIGN, mpoly, totpoly);
 
2114
 
 
2115
        numTex = CustomData_number_of_layers(fdata, CD_MTFACE);
 
2116
        numCol = CustomData_number_of_layers(fdata, CD_MCOL);
 
2117
 
2046
2118
        totloop = 0;
2047
 
        mf = mesh->mface;
2048
 
        for (i=0; i<mesh->totface; i++, mf++) {
 
2119
        mf = mface;
 
2120
        for (i = 0; i < totface_i; i++, mf++) {
2049
2121
                totloop += mf->v4 ? 4 : 3;
2050
2122
        }
2051
 
        
2052
 
        mesh->totloop = totloop;
2053
 
        mesh->mloop = MEM_callocN(sizeof(MLoop)*mesh->totloop, "mloop converted");
2054
 
 
2055
 
        CustomData_add_layer(&mesh->ldata, CD_MLOOP, CD_ASSIGN, mesh->mloop, totloop);
2056
 
        CustomData_to_bmeshpoly(&mesh->fdata, &mesh->pdata, &mesh->ldata,
2057
 
                mesh->totloop, mesh->totpoly);
2058
 
 
2059
 
        /* ensure external data is transferred */
2060
 
        CustomData_external_read(&mesh->fdata, &mesh->id, CD_MASK_MDISPS, mesh->totface);
 
2123
 
 
2124
        mloop = MEM_callocN(sizeof(MLoop) * totloop, "mloop converted");
 
2125
 
 
2126
        CustomData_add_layer(ldata, CD_MLOOP, CD_ASSIGN, mloop, totloop);
 
2127
 
 
2128
        CustomData_to_bmeshpoly(fdata, pdata, ldata, totloop, totpoly);
 
2129
 
 
2130
        if (id) {
 
2131
                /* ensure external data is transferred */
 
2132
                CustomData_external_read(fdata, id, CD_MASK_MDISPS, totface_i);
 
2133
        }
2061
2134
 
2062
2135
        eh = BLI_edgehash_new();
2063
2136
 
2064
 
        /*build edge hash*/
2065
 
        me = mesh->medge;
2066
 
        for (i = 0; i < mesh->totedge; i++, me++) {
 
2137
        /* build edge hash */
 
2138
        me = medge;
 
2139
        for (i = 0; i < totedge_i; i++, me++) {
2067
2140
                BLI_edgehash_insert(eh, me->v1, me->v2, SET_INT_IN_POINTER(i));
2068
2141
 
2069
2142
                /* unrelated but avoid having the FGON flag enabled, so we can reuse it later for something else */
2070
2143
                me->flag &= ~ME_FGON;
2071
2144
        }
2072
2145
 
2073
 
        j = 0; /*current loop index*/
2074
 
        ml = mesh->mloop;
2075
 
        mf = mesh->mface;
2076
 
        mp = mesh->mpoly;
2077
 
        for (i=0; i<mesh->totface; i++, mf++, mp++) {
 
2146
        polyindex = CustomData_get_layer(fdata, CD_ORIGINDEX);
 
2147
 
 
2148
        j = 0; /* current loop index */
 
2149
        ml = mloop;
 
2150
        mf = mface;
 
2151
        mp = mpoly;
 
2152
        for (i = 0; i < totface_i; i++, mf++, mp++) {
2078
2153
                mp->loopstart = j;
2079
 
                
 
2154
 
2080
2155
                mp->totloop = mf->v4 ? 4 : 3;
2081
2156
 
2082
2157
                mp->mat_nr = mf->mat_nr;
2083
2158
                mp->flag = mf->flag;
2084
 
                
2085
 
#               define ML(v1, v2) {ml->v = mf->v1; ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(eh, mf->v1, mf->v2)); ml++; j++;}
2086
 
                
 
2159
 
 
2160
#       define ML(v1, v2) { \
 
2161
                        ml->v = mf->v1; ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(eh, mf->v1, mf->v2)); ml++; j++; \
 
2162
                } (void)0
 
2163
 
2087
2164
                ML(v1, v2);
2088
2165
                ML(v2, v3);
2089
2166
                if (mf->v4) {
2093
2170
                else {
2094
2171
                        ML(v3, v1);
2095
2172
                }
2096
 
                
2097
 
#               undef ML
2098
 
 
2099
 
                bm_corners_to_loops(mesh, i, mp->loopstart, numTex, numCol);
 
2173
 
 
2174
#       undef ML
 
2175
 
 
2176
                bm_corners_to_loops_ex(id, fdata, ldata, pdata, mface, totloop, i, mp->loopstart, numTex, numCol);
 
2177
 
 
2178
                if (polyindex) {
 
2179
                        *polyindex = i;
 
2180
                        polyindex++;
 
2181
                }
2100
2182
        }
2101
2183
 
2102
 
        /* note, we don't convert FGons at all, these are not even real ngons,
 
2184
        /* note, we don't convert NGons at all, these are not even real ngons,
2103
2185
         * they have their own UV's, colors etc - its more an editing feature. */
2104
2186
 
2105
 
        mesh_update_customdata_pointers(mesh, TRUE);
2106
 
 
2107
2187
        BLI_edgehash_free(eh, NULL);
 
2188
 
 
2189
        *totpoly_r = totpoly;
 
2190
        *totloop_r = totloop;
 
2191
        *mpoly_r = mpoly;
 
2192
        *mloop_r = mloop;
2108
2193
}
2109
2194
 
2110
 
float (*mesh_getVertexCos(Mesh *me, int *numVerts_r))[3]
 
2195
float (*mesh_getVertexCos(Mesh * me, int *numVerts_r))[3]
2111
2196
{
2112
2197
        int i, numVerts = me->totvert;
2113
 
        float (*cos)[3] = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos1");
 
2198
        float (*cos)[3] = MEM_mallocN(sizeof(*cos) * numVerts, "vertexcos1");
2114
2199
 
2115
2200
        if (numVerts_r) *numVerts_r = numVerts;
2116
 
        for (i=0; i<numVerts; i++)
 
2201
        for (i = 0; i < numVerts; i++)
2117
2202
                copy_v3_v3(cos[i], me->mvert[i].co);
2118
2203
 
2119
2204
        return cos;
2124
2209
/* this replaces the non bmesh function (in trunk) which takes MTFace's, if we ever need it back we could
2125
2210
 * but for now this replaces it because its unused. */
2126
2211
 
2127
 
UvVertMap *make_uv_vert_map(struct MPoly *mpoly, struct MLoop *mloop, struct MLoopUV *mloopuv, unsigned int totpoly, unsigned int totvert, int selected, float *limit)
 
2212
UvVertMap *BKE_mesh_uv_vert_map_make(struct MPoly *mpoly, struct MLoop *mloop, struct MLoopUV *mloopuv, unsigned int totpoly, unsigned int totvert, int selected, float *limit)
2128
2213
{
2129
2214
        UvVertMap *vmap;
2130
2215
        UvMapVert *buf;
2131
2216
        MPoly *mp;
2132
2217
        unsigned int a;
2133
 
        int     i, totuv, nverts;
 
2218
        int i, totuv, nverts;
2134
2219
 
2135
2220
        totuv = 0;
2136
2221
 
2137
2222
        /* generate UvMapVert array */
2138
 
        mp= mpoly;
2139
 
        for (a=0; a<totpoly; a++, mp++)
 
2223
        mp = mpoly;
 
2224
        for (a = 0; a < totpoly; a++, mp++)
2140
2225
                if (!selected || (!(mp->flag & ME_HIDE) && (mp->flag & ME_FACE_SEL)))
2141
2226
                        totuv += mp->totloop;
2142
2227
 
2143
 
        if (totuv==0)
 
2228
        if (totuv == 0)
2144
2229
                return NULL;
2145
2230
        
2146
 
        vmap= (UvVertMap*)MEM_callocN(sizeof(*vmap), "UvVertMap");
 
2231
        vmap = (UvVertMap *)MEM_callocN(sizeof(*vmap), "UvVertMap");
2147
2232
        if (!vmap)
2148
2233
                return NULL;
2149
2234
 
2150
 
        vmap->vert= (UvMapVert**)MEM_callocN(sizeof(*vmap->vert)*totvert, "UvMapVert*");
2151
 
        buf= vmap->buf= (UvMapVert*)MEM_callocN(sizeof(*vmap->buf)*totuv, "UvMapVert");
 
2235
        vmap->vert = (UvMapVert **)MEM_callocN(sizeof(*vmap->vert) * totvert, "UvMapVert*");
 
2236
        buf = vmap->buf = (UvMapVert *)MEM_callocN(sizeof(*vmap->buf) * totuv, "UvMapVert");
2152
2237
 
2153
2238
        if (!vmap->vert || !vmap->buf) {
2154
 
                free_uv_vert_map(vmap);
 
2239
                BKE_mesh_uv_vert_map_free(vmap);
2155
2240
                return NULL;
2156
2241
        }
2157
2242
 
2158
 
        mp= mpoly;
2159
 
        for (a=0; a<totpoly; a++, mp++) {
 
2243
        mp = mpoly;
 
2244
        for (a = 0; a < totpoly; a++, mp++) {
2160
2245
                if (!selected || (!(mp->flag & ME_HIDE) && (mp->flag & ME_FACE_SEL))) {
2161
 
                        nverts= mp->totloop;
 
2246
                        nverts = mp->totloop;
2162
2247
 
2163
 
                        for (i=0; i<nverts; i++) {
2164
 
                                buf->tfindex= i;
2165
 
                                buf->f= a;
 
2248
                        for (i = 0; i < nverts; i++) {
 
2249
                                buf->tfindex = i;
 
2250
                                buf->f = a;
2166
2251
                                buf->separate = 0;
2167
 
                                buf->next= vmap->vert[mloop[mp->loopstart + i].v];
2168
 
                                vmap->vert[mloop[mp->loopstart + i].v]= buf;
 
2252
                                buf->next = vmap->vert[mloop[mp->loopstart + i].v];
 
2253
                                vmap->vert[mloop[mp->loopstart + i].v] = buf;
2169
2254
                                buf++;
2170
2255
                        }
2171
2256
                }
2172
2257
        }
2173
2258
        
2174
2259
        /* sort individual uvs for each vert */
2175
 
        for (a=0; a<totvert; a++) {
2176
 
                UvMapVert *newvlist= NULL, *vlist=vmap->vert[a];
 
2260
        for (a = 0; a < totvert; a++) {
 
2261
                UvMapVert *newvlist = NULL, *vlist = vmap->vert[a];
2177
2262
                UvMapVert *iterv, *v, *lastv, *next;
2178
2263
                float *uv, *uv2, uvdiff[2];
2179
2264
 
2180
2265
                while (vlist) {
2181
 
                        v= vlist;
2182
 
                        vlist= vlist->next;
2183
 
                        v->next= newvlist;
2184
 
                        newvlist= v;
 
2266
                        v = vlist;
 
2267
                        vlist = vlist->next;
 
2268
                        v->next = newvlist;
 
2269
                        newvlist = v;
2185
2270
 
2186
 
                        uv= mloopuv[mpoly[v->f].loopstart + v->tfindex].uv;
2187
 
                        lastv= NULL;
2188
 
                        iterv= vlist;
 
2271
                        uv = mloopuv[mpoly[v->f].loopstart + v->tfindex].uv;
 
2272
                        lastv = NULL;
 
2273
                        iterv = vlist;
2189
2274
 
2190
2275
                        while (iterv) {
2191
 
                                next= iterv->next;
 
2276
                                next = iterv->next;
2192
2277
 
2193
 
                                uv2= mloopuv[mpoly[iterv->f].loopstart + iterv->tfindex].uv;
 
2278
                                uv2 = mloopuv[mpoly[iterv->f].loopstart + iterv->tfindex].uv;
2194
2279
                                sub_v2_v2v2(uvdiff, uv2, uv);
2195
2280
 
2196
2281
 
2197
 
                                if (fabsf(uv[0]-uv2[0]) < limit[0] && fabsf(uv[1]-uv2[1]) < limit[1]) {
2198
 
                                        if (lastv) lastv->next= next;
2199
 
                                        else vlist= next;
2200
 
                                        iterv->next= newvlist;
2201
 
                                        newvlist= iterv;
 
2282
                                if (fabsf(uv[0] - uv2[0]) < limit[0] && fabsf(uv[1] - uv2[1]) < limit[1]) {
 
2283
                                        if (lastv) lastv->next = next;
 
2284
                                        else vlist = next;
 
2285
                                        iterv->next = newvlist;
 
2286
                                        newvlist = iterv;
2202
2287
                                }
2203
2288
                                else
2204
 
                                        lastv=iterv;
 
2289
                                        lastv = iterv;
2205
2290
 
2206
 
                                iterv= next;
 
2291
                                iterv = next;
2207
2292
                        }
2208
2293
 
2209
2294
                        newvlist->separate = 1;
2210
2295
                }
2211
2296
 
2212
 
                vmap->vert[a]= newvlist;
 
2297
                vmap->vert[a] = newvlist;
2213
2298
        }
2214
2299
        
2215
2300
        return vmap;
2216
2301
}
2217
2302
 
2218
 
UvMapVert *get_uv_map_vert(UvVertMap *vmap, unsigned int v)
 
2303
UvMapVert *BKE_mesh_uv_vert_map_get_vert(UvVertMap *vmap, unsigned int v)
2219
2304
{
2220
2305
        return vmap->vert[v];
2221
2306
}
2222
2307
 
2223
 
void free_uv_vert_map(UvVertMap *vmap)
 
2308
void BKE_mesh_uv_vert_map_free(UvVertMap *vmap)
2224
2309
{
2225
2310
        if (vmap) {
2226
2311
                if (vmap->vert) MEM_freeN(vmap->vert);
2276
2361
/* Generates a map where the key is the vertex and the value is a list
2277
2362
 * of edges that use that vertex as an endpoint. The lists are allocated
2278
2363
 * from one memory pool. */
2279
 
void create_vert_edge_map(ListBase **map, IndexNode **mem, const MEdge *medge, const int totvert, const int totedge)
 
2364
void create_vert_edge_map(MeshElemMap **map, int **mem,
 
2365
                          const MEdge *medge, int totvert, int totedge)
2280
2366
{
2281
 
        int i, j;
2282
 
        IndexNode *node = NULL;
2283
 
 
2284
 
        (*map) = MEM_callocN(sizeof(ListBase) * totvert, "vert edge map");
2285
 
        (*mem) = MEM_callocN(sizeof(IndexNode) * totedge * 2, "vert edge map mem");
2286
 
        node = *mem;
2287
 
 
 
2367
        int i, *indices;
 
2368
 
 
2369
        (*map) = MEM_callocN(sizeof(MeshElemMap) * totvert, "vert-edge map");
 
2370
        (*mem) = MEM_mallocN(sizeof(int) * totedge * 2, "vert-edge map mem");
 
2371
 
 
2372
        /* Count number of edges for each vertex */
 
2373
        for (i = 0; i < totedge; i++) {
 
2374
                (*map)[medge[i].v1].count++;
 
2375
                (*map)[medge[i].v2].count++;
 
2376
        }
 
2377
 
 
2378
        /* Assign indices mem */
 
2379
        indices = (*mem);
 
2380
        for (i = 0; i < totvert; i++) {
 
2381
                (*map)[i].indices = indices;
 
2382
                indices += (*map)[i].count;
 
2383
 
 
2384
                /* Reset 'count' for use as index in last loop */
 
2385
                (*map)[i].count = 0;
 
2386
        }
 
2387
                
2288
2388
        /* Find the users */
2289
 
        for (i = 0; i < totedge; ++i) {
2290
 
                for (j = 0; j < 2; ++j, ++node) {
2291
 
                        node->index = i;
2292
 
                        BLI_addtail(&(*map)[((unsigned int*)(&medge[i].v1))[j]], node);
2293
 
                }
 
2389
        for (i = 0; i < totedge; i++) {
 
2390
                const int v[2] = {medge[i].v1, medge[i].v2};
 
2391
 
 
2392
                (*map)[v[0]].indices[(*map)[v[0]].count] = i;
 
2393
                (*map)[v[1]].indices[(*map)[v[1]].count] = i;
 
2394
                
 
2395
                (*map)[v[0]].count++;
 
2396
                (*map)[v[1]].count++;
2294
2397
        }
2295
2398
}
2296
2399
 
2297
 
void mesh_loops_to_mface_corners(CustomData *fdata, CustomData *ldata,
2298
 
                                 CustomData *pdata, int lindex[4], int findex,
2299
 
                                 const int polyindex,
2300
 
                                 const int mf_len, /* 3 or 4 */
 
2400
void BKE_mesh_loops_to_mface_corners(CustomData *fdata, CustomData *ldata,
 
2401
                                     CustomData *pdata, int lindex[4], int findex,
 
2402
                                     const int polyindex,
 
2403
                                     const int mf_len, /* 3 or 4 */
2301
2404
 
2302
 
                                 /* cache values to avoid lookups every time */
2303
 
                                 const int numTex, /* CustomData_number_of_layers(pdata, CD_MTEXPOLY) */
2304
 
                                 const int numCol, /* CustomData_number_of_layers(ldata, CD_MLOOPCOL) */
2305
 
                                 const int hasPCol, /* CustomData_has_layer(ldata, CD_PREVIEW_MLOOPCOL) */
2306
 
                                 const int hasOrigSpace /* CustomData_has_layer(ldata, CD_ORIGSPACE_MLOOP) */
2307
 
                                 )
 
2405
                                     /* cache values to avoid lookups every time */
 
2406
                                     const int numTex, /* CustomData_number_of_layers(pdata, CD_MTEXPOLY) */
 
2407
                                     const int numCol, /* CustomData_number_of_layers(ldata, CD_MLOOPCOL) */
 
2408
                                     const int hasPCol, /* CustomData_has_layer(ldata, CD_PREVIEW_MLOOPCOL) */
 
2409
                                     const int hasOrigSpace /* CustomData_has_layer(ldata, CD_ORIGSPACE_MLOOP) */
 
2410
                                     )
2308
2411
{
2309
2412
        MTFace *texface;
2310
2413
        MTexPoly *texpoly;
2313
2416
        MLoopUV *mloopuv;
2314
2417
        int i, j;
2315
2418
        
2316
 
        for (i=0; i < numTex; i++) {
 
2419
        for (i = 0; i < numTex; i++) {
2317
2420
                texface = CustomData_get_n(fdata, CD_MTFACE, findex, i);
2318
2421
                texpoly = CustomData_get_n(pdata, CD_MTEXPOLY, polyindex, i);
2319
2422
 
2320
2423
                ME_MTEXFACE_CPY(texface, texpoly);
2321
2424
 
2322
 
                for (j=0; j < mf_len; j++) {
 
2425
                for (j = 0; j < mf_len; j++) {
2323
2426
                        mloopuv = CustomData_get_n(ldata, CD_MLOOPUV, lindex[j], i);
2324
2427
                        copy_v2_v2(texface->uv[j], mloopuv->uv);
2325
2428
                }
2326
2429
        }
2327
2430
 
2328
 
        for (i=0; i < numCol; i++) {
 
2431
        for (i = 0; i < numCol; i++) {
2329
2432
                mcol = CustomData_get_n(fdata, CD_MCOL, findex, i);
2330
2433
 
2331
 
                for (j=0; j < mf_len; j++) {
 
2434
                for (j = 0; j < mf_len; j++) {
2332
2435
                        mloopcol = CustomData_get_n(ldata, CD_MLOOPCOL, lindex[j], i);
2333
2436
                        MESH_MLOOPCOL_TO_MCOL(mloopcol, &mcol[j]);
2334
2437
                }
2337
2440
        if (hasPCol) {
2338
2441
                mcol = CustomData_get(fdata,  findex, CD_PREVIEW_MCOL);
2339
2442
 
2340
 
                for (j=0; j < mf_len; j++) {
 
2443
                for (j = 0; j < mf_len; j++) {
2341
2444
                        mloopcol = CustomData_get(ldata, lindex[j], CD_PREVIEW_MLOOPCOL);
2342
2445
                        MESH_MLOOPCOL_TO_MCOL(mloopcol, &mcol[j]);
2343
2446
                }
2347
2450
                OrigSpaceFace *of = CustomData_get(fdata, findex, CD_ORIGSPACE);
2348
2451
                OrigSpaceLoop *lof;
2349
2452
 
2350
 
                for (j=0; j < mf_len; j++) {
 
2453
                for (j = 0; j < mf_len; j++) {
2351
2454
                        lof = CustomData_get(ldata, lindex[j], CD_ORIGSPACE_MLOOP);
2352
2455
                        copy_v2_v2(of->uv[j], lof->uv);
2353
2456
                }
2358
2461
 * this function recreates a tessellation.
2359
2462
 * returns number of tessellation faces.
2360
2463
 */
2361
 
int mesh_recalcTessellation(CustomData *fdata,
2362
 
                           CustomData *ldata, CustomData *pdata,
2363
 
                           MVert *mvert, int totface, int UNUSED(totloop),
2364
 
                           int totpoly,
2365
 
                           /* when tessellating to recalculate normals after
2366
 
                            * we can skip copying here */
2367
 
                           const int do_face_nor_cpy)
 
2464
int BKE_mesh_recalc_tessellation(CustomData *fdata,
 
2465
                                 CustomData *ldata, CustomData *pdata,
 
2466
                                 MVert *mvert, int totface, int totloop,
 
2467
                                 int totpoly,
 
2468
                                 /* when tessellating to recalculate normals after
 
2469
                                  * we can skip copying here */
 
2470
                                 const int do_face_nor_cpy)
2368
2471
{
2369
2472
        /* use this to avoid locking pthread for _every_ polygon
2370
2473
         * and calling the fill function */
2372
2475
#define USE_TESSFACE_SPEEDUP
2373
2476
#define USE_TESSFACE_QUADS // NEEDS FURTHER TESTING
2374
2477
 
2375
 
#define TESSFACE_SCANFILL (1<<0)
2376
 
#define TESSFACE_IS_QUAD  (1<<1)
 
2478
#define TESSFACE_SCANFILL (1 << 0)
 
2479
#define TESSFACE_IS_QUAD  (1 << 1)
 
2480
 
 
2481
        const int looptris_tot = poly_to_tri_count(totpoly, totloop);
2377
2482
 
2378
2483
        MPoly *mp, *mpoly;
2379
2484
        MLoop *ml, *mloop;
2380
 
        MFace *mface = NULL, *mf;
2381
 
        BLI_array_declare(mface);
 
2485
        MFace *mface, *mf;
2382
2486
        ScanFillContext sf_ctx;
2383
 
        ScanFillVert *v, *lastv, *firstv;
2384
 
        ScanFillFace *f;
2385
 
        int *mface_orig_index = NULL;
2386
 
        BLI_array_declare(mface_orig_index);
2387
 
        int *mface_to_poly_map = NULL;
2388
 
        BLI_array_declare(mface_to_poly_map);
 
2487
        ScanFillVert *sf_vert, *sf_vert_last, *sf_vert_first;
 
2488
        ScanFillFace *sf_tri;
 
2489
        int *mface_to_poly_map;
2389
2490
        int lindex[4]; /* only ever use 3 in this case */
2390
 
        int *poly_orig_index;
2391
2491
        int poly_index, j, mface_index;
2392
2492
 
2393
2493
        const int numTex = CustomData_number_of_layers(pdata, CD_MTEXPOLY);
2400
2500
 
2401
2501
        /* allocate the length of totfaces, avoid many small reallocs,
2402
2502
         * if all faces are tri's it will be correct, quads == 2x allocs */
2403
 
        BLI_array_reserve(mface_to_poly_map, totpoly);
2404
 
        BLI_array_reserve(mface, totpoly);
 
2503
        /* take care. we are _not_ calloc'ing so be sure to initialize each field */
 
2504
        mface_to_poly_map = MEM_mallocN(sizeof(*mface_to_poly_map) * looptris_tot, __func__);
 
2505
        mface             = MEM_mallocN(sizeof(*mface) *             looptris_tot, __func__);
2405
2506
 
2406
2507
        mface_index = 0;
2407
2508
        mp = mpoly;
2408
 
        poly_orig_index = CustomData_get_layer(pdata, CD_ORIGINDEX);
2409
2509
        for (poly_index = 0; poly_index < totpoly; poly_index++, mp++) {
2410
2510
                if (mp->totloop < 3) {
2411
2511
                        /* do nothing */
2414
2514
#ifdef USE_TESSFACE_SPEEDUP
2415
2515
 
2416
2516
#define ML_TO_MF(i1, i2, i3)                                                  \
2417
 
                BLI_array_growone(mface_to_poly_map);                                 \
2418
 
                BLI_array_growone(mface);                                             \
2419
2517
                mface_to_poly_map[mface_index] = poly_index;                          \
2420
 
                mf= &mface[mface_index];                                              \
 
2518
                mf = &mface[mface_index];                                             \
2421
2519
                /* set loop indices, transformed to vert indices later */             \
2422
2520
                mf->v1 = mp->loopstart + i1;                                          \
2423
2521
                mf->v2 = mp->loopstart + i2;                                          \
2425
2523
                mf->v4 = 0;                                                           \
2426
2524
                mf->mat_nr = mp->mat_nr;                                              \
2427
2525
                mf->flag = mp->flag;                                                  \
2428
 
                if (poly_orig_index) {                                                \
2429
 
                        BLI_array_append(mface_orig_index,                                \
2430
 
                                     poly_orig_index[poly_index]);                    \
2431
 
                }                                                                     \
 
2526
                mf->edcode = 0;                                                       \
2432
2527
                (void)0
2433
2528
 
2434
2529
/* ALMOST IDENTICAL TO DEFINE ABOVE (see EXCEPTION) */
2435
2530
#define ML_TO_MF_QUAD()                                                       \
2436
 
                BLI_array_growone(mface_to_poly_map);                                 \
2437
 
                BLI_array_growone(mface);                                             \
2438
2531
                mface_to_poly_map[mface_index] = poly_index;                          \
2439
 
                mf= &mface[mface_index];                                              \
 
2532
                mf = &mface[mface_index];                                             \
2440
2533
                /* set loop indices, transformed to vert indices later */             \
2441
2534
                mf->v1 = mp->loopstart + 0; /* EXCEPTION */                           \
2442
2535
                mf->v2 = mp->loopstart + 1; /* EXCEPTION */                           \
2444
2537
                mf->v4 = mp->loopstart + 3; /* EXCEPTION */                           \
2445
2538
                mf->mat_nr = mp->mat_nr;                                              \
2446
2539
                mf->flag = mp->flag;                                                  \
2447
 
                if (poly_orig_index) {                                                \
2448
 
                        BLI_array_append(mface_orig_index,                                \
2449
 
                                     poly_orig_index[poly_index]);                    \
2450
 
                }                                                                     \
2451
 
                mf->edcode |= TESSFACE_IS_QUAD; /* EXCEPTION */                       \
 
2540
                mf->edcode = TESSFACE_IS_QUAD; /* EXCEPTION */                        \
2452
2541
                (void)0
2453
2542
 
2454
2543
 
2473
2562
 
2474
2563
                        ml = mloop + mp->loopstart;
2475
2564
                        
2476
 
                        BLI_begin_edgefill(&sf_ctx);
2477
 
                        firstv = NULL;
2478
 
                        lastv = NULL;
2479
 
                        for (j=0; j<mp->totloop; j++, ml++) {
2480
 
                                v = BLI_addfillvert(&sf_ctx, mvert[ml->v].co);
2481
 
        
2482
 
                                v->keyindex = mp->loopstart + j;
2483
 
        
2484
 
                                if (lastv)
2485
 
                                        BLI_addfilledge(&sf_ctx, lastv, v);
2486
 
        
2487
 
                                if (!firstv)
2488
 
                                        firstv = v;
2489
 
                                lastv = v;
 
2565
                        BLI_scanfill_begin(&sf_ctx);
 
2566
                        sf_vert_first = NULL;
 
2567
                        sf_vert_last = NULL;
 
2568
                        for (j = 0; j < mp->totloop; j++, ml++) {
 
2569
                                sf_vert = BLI_scanfill_vert_add(&sf_ctx, mvert[ml->v].co);
 
2570
        
 
2571
                                sf_vert->keyindex = mp->loopstart + j;
 
2572
        
 
2573
                                if (sf_vert_last)
 
2574
                                        BLI_scanfill_edge_add(&sf_ctx, sf_vert_last, sf_vert);
 
2575
        
 
2576
                                if (!sf_vert_first)
 
2577
                                        sf_vert_first = sf_vert;
 
2578
                                sf_vert_last = sf_vert;
2490
2579
                        }
2491
 
                        BLI_addfilledge(&sf_ctx, lastv, firstv);
 
2580
                        BLI_scanfill_edge_add(&sf_ctx, sf_vert_last, sf_vert_first);
2492
2581
                        
2493
 
                        totfilltri = BLI_edgefill(&sf_ctx, FALSE);
2494
 
                        if (totfilltri) {
2495
 
                                BLI_array_growitems(mface_to_poly_map, totfilltri);
2496
 
                                BLI_array_growitems(mface, totfilltri);
2497
 
                                if (poly_orig_index) {
2498
 
                                        BLI_array_growitems(mface_orig_index, totfilltri);
2499
 
                                }
2500
 
 
2501
 
                                for (f = sf_ctx.fillfacebase.first; f; f = f->next, mf++) {
2502
 
                                        mface_to_poly_map[mface_index] = poly_index;
2503
 
                                        mf= &mface[mface_index];
2504
 
 
2505
 
                                        /* set loop indices, transformed to vert indices later */
2506
 
                                        mf->v1 = f->v1->keyindex;
2507
 
                                        mf->v2 = f->v2->keyindex;
2508
 
                                        mf->v3 = f->v3->keyindex;
2509
 
                                        mf->v4 = 0;
2510
 
 
2511
 
                                        mf->mat_nr = mp->mat_nr;
2512
 
                                        mf->flag = mp->flag;
 
2582
                        totfilltri = BLI_scanfill_calc(&sf_ctx, 0);
 
2583
                        BLI_assert(totfilltri <= mp->totloop - 2);
 
2584
                        (void)totfilltri;
 
2585
 
 
2586
                        for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next, mf++) {
 
2587
                                mface_to_poly_map[mface_index] = poly_index;
 
2588
                                mf = &mface[mface_index];
 
2589
 
 
2590
                                /* set loop indices, transformed to vert indices later */
 
2591
                                mf->v1 = sf_tri->v1->keyindex;
 
2592
                                mf->v2 = sf_tri->v2->keyindex;
 
2593
                                mf->v3 = sf_tri->v3->keyindex;
 
2594
                                mf->v4 = 0;
 
2595
 
 
2596
                                mf->mat_nr = mp->mat_nr;
 
2597
                                mf->flag = mp->flag;
2513
2598
 
2514
2599
#ifdef USE_TESSFACE_SPEEDUP
2515
 
                                        mf->edcode |= TESSFACE_SCANFILL; /* tag for sorting loop indices */
 
2600
                                mf->edcode = TESSFACE_SCANFILL; /* tag for sorting loop indices */
2516
2601
#endif
2517
2602
 
2518
 
                                        if (poly_orig_index) {
2519
 
                                                mface_orig_index[mface_index] = poly_orig_index[poly_index];
2520
 
                                        }
2521
 
 
2522
 
                                        mface_index++;
2523
 
                                }
 
2603
                                mface_index++;
2524
2604
                        }
2525
2605
        
2526
 
                        BLI_end_edgefill(&sf_ctx);
 
2606
                        BLI_scanfill_end(&sf_ctx);
2527
2607
                }
2528
2608
        }
2529
2609
 
2530
2610
        CustomData_free(fdata, totface);
2531
 
        memset(fdata, 0, sizeof(CustomData));
2532
2611
        totface = mface_index;
2533
2612
 
 
2613
        BLI_assert(totface <= looptris_tot);
2534
2614
 
2535
2615
        /* not essential but without this we store over-alloc'd memory in the CustomData layers */
2536
 
        if (LIKELY((MEM_allocN_len(mface) / sizeof(*mface)) != totface)) {
 
2616
        if (LIKELY(looptris_tot != totface)) {
2537
2617
                mface = MEM_reallocN(mface, sizeof(*mface) * totface);
2538
2618
                mface_to_poly_map = MEM_reallocN(mface_to_poly_map, sizeof(*mface_to_poly_map) * totface);
2539
 
                if (mface_orig_index) {
2540
 
                        mface_orig_index = MEM_reallocN(mface_orig_index, sizeof(*mface_orig_index) * totface);
2541
 
                }
2542
2619
        }
2543
2620
 
2544
2621
        CustomData_add_layer(fdata, CD_MFACE, CD_ASSIGN, mface, totface);
2545
2622
 
2546
 
        /* CD_POLYINDEX will contain an array of indices from tessfaces to the polygons
 
2623
        /* CD_ORIGINDEX will contain an array of indices from tessfaces to the polygons
2547
2624
         * they are directly tessellated from */
2548
 
        CustomData_add_layer(fdata, CD_POLYINDEX, CD_ASSIGN, mface_to_poly_map, totface);
2549
 
        if (mface_orig_index) {
2550
 
                /* If polys had a CD_ORIGINDEX layer, then the tessellated faces will get this
2551
 
                 * layer as well, pointing to polys from the original mesh (not the polys
2552
 
                 * that just got tessellated) */
2553
 
                CustomData_add_layer(fdata, CD_ORIGINDEX, CD_ASSIGN, mface_orig_index, totface);
2554
 
        }
2555
 
 
 
2625
        CustomData_add_layer(fdata, CD_ORIGINDEX, CD_ASSIGN, mface_to_poly_map, totface);
2556
2626
        CustomData_from_bmeshpoly(fdata, pdata, ldata, totface);
2557
2627
 
2558
2628
        if (do_face_nor_cpy) {
2568
2638
        }
2569
2639
 
2570
2640
        mf = mface;
2571
 
        for (mface_index=0; mface_index < totface; mface_index++, mf++) {
 
2641
        for (mface_index = 0; mface_index < totface; mface_index++, mf++) {
2572
2642
 
2573
2643
#ifdef USE_TESSFACE_QUADS
2574
2644
                const int mf_len = mf->edcode & TESSFACE_IS_QUAD ? 4 : 3;
2580
2650
#endif
2581
2651
                {
2582
2652
                        /* sort loop indices to ensure winding is correct */
2583
 
                        if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
2584
 
                        if (mf->v2 > mf->v3) SWAP(int, mf->v2, mf->v3);
2585
 
                        if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
 
2653
                        if (mf->v1 > mf->v2) SWAP(unsigned int, mf->v1, mf->v2);
 
2654
                        if (mf->v2 > mf->v3) SWAP(unsigned int, mf->v2, mf->v3);
 
2655
                        if (mf->v1 > mf->v2) SWAP(unsigned int, mf->v1, mf->v2);
2586
2656
 
2587
 
                        if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
2588
 
                        if (mf->v2 > mf->v3) SWAP(int, mf->v2, mf->v3);
2589
 
                        if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
 
2657
                        if (mf->v1 > mf->v2) SWAP(unsigned int, mf->v1, mf->v2);
 
2658
                        if (mf->v2 > mf->v3) SWAP(unsigned int, mf->v2, mf->v3);
 
2659
                        if (mf->v1 > mf->v2) SWAP(unsigned int, mf->v1, mf->v2);
2590
2660
                }
2591
2661
 
2592
2662
                /* end abusing the edcode */
2610
2680
                if (mf_len == 4) mf->v4 = mloop[mf->v4].v;
2611
2681
#endif
2612
2682
 
2613
 
                mesh_loops_to_mface_corners(fdata, ldata, pdata,
2614
 
                                            lindex, mface_index, mface_to_poly_map[mface_index],
 
2683
                BKE_mesh_loops_to_mface_corners(fdata, ldata, pdata,
 
2684
                                                lindex, mface_index, mface_to_poly_map[mface_index],
2615
2685
#ifdef USE_TESSFACE_QUADS
2616
 
                                            mf_len,
 
2686
                                                mf_len,
2617
2687
#else
2618
 
                                            3,
 
2688
                                                3,
2619
2689
#endif
2620
 
                                            numTex, numCol, hasPCol, hasOrigSpace);
 
2690
                                                numTex, numCol, hasPCol, hasOrigSpace);
2621
2691
 
2622
2692
 
2623
2693
#ifdef USE_TESSFACE_QUADS
2639
2709
 * this function recreates a tessellation.
2640
2710
 * returns number of tessellation faces.
2641
2711
 */
2642
 
int mesh_mpoly_to_mface(struct CustomData *fdata, struct CustomData *ldata,
2643
 
        struct CustomData *pdata, int totface, int UNUSED(totloop), int totpoly)
 
2712
int BKE_mesh_mpoly_to_mface(struct CustomData *fdata, struct CustomData *ldata,
 
2713
                            struct CustomData *pdata, int totface, int UNUSED(totloop), int totpoly)
2644
2714
{
2645
2715
        MLoop *mloop;
2646
2716
 
2662
2732
 
2663
2733
        mp = mpoly;
2664
2734
        k = 0;
2665
 
        for (i = 0; i<totpoly; i++, mp++) {
 
2735
        for (i = 0; i < totpoly; i++, mp++) {
2666
2736
                if (ELEM(mp->totloop, 3, 4)) {
2667
 
                        BLI_array_growone(mface);
 
2737
                        BLI_array_grow_one(mface);
2668
2738
                        mf = &mface[k];
2669
2739
 
2670
2740
                        mf->mat_nr = mp->mat_nr;
2683
2753
        }
2684
2754
 
2685
2755
        CustomData_free(fdata, totface);
2686
 
        memset(fdata, 0, sizeof(CustomData));
2687
2756
 
2688
 
        totface= k;
 
2757
        totface = k;
2689
2758
 
2690
2759
        CustomData_add_layer(fdata, CD_MFACE, CD_ASSIGN, mface, totface);
2691
2760
 
2693
2762
 
2694
2763
        mp = mpoly;
2695
2764
        k = 0;
2696
 
        for (i = 0; i<totpoly; i++, mp++) {
 
2765
        for (i = 0; i < totpoly; i++, mp++) {
2697
2766
                if (ELEM(mp->totloop, 3, 4)) {
2698
2767
                        mf = &mface[k];
2699
2768
 
2700
2769
                        if (mf->edcode == 3) {
2701
 
                                /*sort loop indices to ensure winding is correct*/
 
2770
                                /* sort loop indices to ensure winding is correct */
2702
2771
                                /* NO SORT - looks like we can skip this */
2703
2772
 
2704
2773
                                lindex[0] = mf->v1;
2706
2775
                                lindex[2] = mf->v3;
2707
2776
                                lindex[3] = 0; /* unused */
2708
2777
 
2709
 
                                /*transform loop indices to vert indices*/
 
2778
                                /* transform loop indices to vert indices */
2710
2779
                                mf->v1 = mloop[mf->v1].v;
2711
2780
                                mf->v2 = mloop[mf->v2].v;
2712
2781
                                mf->v3 = mloop[mf->v3].v;
2713
2782
 
2714
 
                                mesh_loops_to_mface_corners(fdata, ldata, pdata,
2715
 
                                                            lindex, k, i, 3,
2716
 
                                                            numTex, numCol, hasPCol, hasOrigSpace);
 
2783
                                BKE_mesh_loops_to_mface_corners(fdata, ldata, pdata,
 
2784
                                                                lindex, k, i, 3,
 
2785
                                                                numTex, numCol, hasPCol, hasOrigSpace);
2717
2786
                                test_index_face(mf, fdata, k, 3);
2718
2787
                        }
2719
2788
                        else {
2720
 
                                /*sort loop indices to ensure winding is correct*/
 
2789
                                /* sort loop indices to ensure winding is correct */
2721
2790
                                /* NO SORT - looks like we can skip this */
2722
2791
 
2723
2792
                                lindex[0] = mf->v1;
2725
2794
                                lindex[2] = mf->v3;
2726
2795
                                lindex[3] = mf->v4;
2727
2796
 
2728
 
                                /*transform loop indices to vert indices*/
 
2797
                                /* transform loop indices to vert indices */
2729
2798
                                mf->v1 = mloop[mf->v1].v;
2730
2799
                                mf->v2 = mloop[mf->v2].v;
2731
2800
                                mf->v3 = mloop[mf->v3].v;
2732
2801
                                mf->v4 = mloop[mf->v4].v;
2733
2802
 
2734
 
                                mesh_loops_to_mface_corners(fdata, ldata, pdata,
2735
 
                                                            lindex, k, i, 4,
2736
 
                                                            numTex, numCol, hasPCol, hasOrigSpace);
 
2803
                                BKE_mesh_loops_to_mface_corners(fdata, ldata, pdata,
 
2804
                                                                lindex, k, i, 4,
 
2805
                                                                numTex, numCol, hasPCol, hasOrigSpace);
2737
2806
                                test_index_face(mf, fdata, k, 4);
2738
2807
                        }
2739
2808
 
2740
 
                        mf->edcode= 0;
 
2809
                        mf->edcode = 0;
2741
2810
 
2742
2811
                        k++;
2743
2812
                }
2758
2827
static void mesh_calc_ngon_normal(MPoly *mpoly, MLoop *loopstart, 
2759
2828
                                  MVert *mvert, float normal[3])
2760
2829
{
2761
 
 
2762
 
        MVert *v1, *v2, *v3;
2763
 
        double u[3], v[3], w[3];
2764
 
        double n[3] = {0.0, 0.0, 0.0}, l;
 
2830
        const int nverts = mpoly->totloop;
 
2831
        float const *v_prev = mvert[loopstart[nverts - 1].v].co;
 
2832
        float const *v_curr;
2765
2833
        int i;
2766
2834
 
2767
 
        for (i = 0; i < mpoly->totloop; i++) {
2768
 
                v1 = mvert + loopstart[i].v;
2769
 
                v2 = mvert + loopstart[(i+1)%mpoly->totloop].v;
2770
 
                v3 = mvert + loopstart[(i+2)%mpoly->totloop].v;
2771
 
                
2772
 
                copy_v3db_v3fl(u, v1->co);
2773
 
                copy_v3db_v3fl(v, v2->co);
2774
 
                copy_v3db_v3fl(w, v3->co);
2775
 
 
2776
 
                /*this fixes some weird numerical error*/
2777
 
                if (i==0) {
2778
 
                        u[0] += 0.0001f;
2779
 
                        u[1] += 0.0001f;
2780
 
                        u[2] += 0.0001f;
2781
 
                }
2782
 
                
2783
 
                /* newell's method
2784
 
                 * 
2785
 
                 * so thats?:
2786
 
                 * (a[1] - b[1]) * (a[2] + b[2]);
2787
 
                 * a[1]*b[2] - b[1]*a[2] - b[1]*b[2] + a[1]*a[2]
2788
 
                 * 
2789
 
                 * odd.  half of that is the cross product. . .what's the
2790
 
                 * other half?
2791
 
                 * 
2792
 
                 * also could be like a[1]*(b[2] + a[2]) - b[1]*(a[2] - b[2])
2793
 
                 */
2794
 
 
2795
 
                n[0] += (u[1] - v[1]) * (u[2] + v[2]);
2796
 
                n[1] += (u[2] - v[2]) * (u[0] + v[0]);
2797
 
                n[2] += (u[0] - v[0]) * (u[1] + v[1]);
2798
 
        }
2799
 
        
2800
 
        l = n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
2801
 
        l = sqrt(l);
2802
 
 
2803
 
        if (l == 0.0) {
2804
 
                normal[0] = 0.0f;
2805
 
                normal[1] = 0.0f;
2806
 
                normal[2] = 1.0f;
2807
 
 
2808
 
                return;
2809
 
        }
2810
 
        else l = 1.0f / l;
2811
 
 
2812
 
        n[0] *= l;
2813
 
        n[1] *= l;
2814
 
        n[2] *= l;
2815
 
        
2816
 
        normal[0] = (float) n[0];
2817
 
        normal[1] = (float) n[1];
2818
 
        normal[2] = (float) n[2];
 
2835
        zero_v3(normal);
 
2836
 
 
2837
        /* Newell's Method */
 
2838
        for (i = 0; i < nverts; i++) {
 
2839
                v_curr = mvert[loopstart[i].v].co;
 
2840
                add_newell_cross_v3_v3v3(normal, v_prev, v_curr);
 
2841
                v_prev = v_curr;
 
2842
        }
 
2843
 
 
2844
        if (UNLIKELY(normalize_v3(normal) == 0.0f)) {
 
2845
                normal[2] = 1.0f; /* other axis set to 0.0 */
 
2846
        }
2819
2847
}
2820
2848
 
2821
 
void mesh_calc_poly_normal(MPoly *mpoly, MLoop *loopstart, 
2822
 
                           MVert *mvarray, float no[3])
 
2849
void BKE_mesh_calc_poly_normal(MPoly *mpoly, MLoop *loopstart,
 
2850
                               MVert *mvarray, float no[3])
2823
2851
{
2824
2852
        if (mpoly->totloop > 4) {
2825
2853
                mesh_calc_ngon_normal(mpoly, loopstart, mvarray, no);
2849
2877
static void mesh_calc_ngon_normal_coords(MPoly *mpoly, MLoop *loopstart,
2850
2878
                                         const float (*vertex_coords)[3], float normal[3])
2851
2879
{
2852
 
 
2853
 
        const float *v1, *v2, *v3;
2854
 
        double u[3], v[3], w[3];
2855
 
        double n[3] = {0.0, 0.0, 0.0}, l;
 
2880
        const int nverts = mpoly->totloop;
 
2881
        float const *v_prev = vertex_coords[loopstart[nverts - 1].v];
 
2882
        float const *v_curr;
2856
2883
        int i;
2857
2884
 
2858
 
        for (i = 0; i < mpoly->totloop; i++) {
2859
 
                v1 = (const float *)(vertex_coords + loopstart[i].v);
2860
 
                v2 = (const float *)(vertex_coords + loopstart[(i+1)%mpoly->totloop].v);
2861
 
                v3 = (const float *)(vertex_coords + loopstart[(i+2)%mpoly->totloop].v);
2862
 
 
2863
 
                copy_v3db_v3fl(u, v1);
2864
 
                copy_v3db_v3fl(v, v2);
2865
 
                copy_v3db_v3fl(w, v3);
2866
 
 
2867
 
                /*this fixes some weird numerical error*/
2868
 
                if (i==0) {
2869
 
                        u[0] += 0.0001f;
2870
 
                        u[1] += 0.0001f;
2871
 
                        u[2] += 0.0001f;
2872
 
                }
2873
 
 
2874
 
                n[0] += (u[1] - v[1]) * (u[2] + v[2]);
2875
 
                n[1] += (u[2] - v[2]) * (u[0] + v[0]);
2876
 
                n[2] += (u[0] - v[0]) * (u[1] + v[1]);
2877
 
        }
2878
 
 
2879
 
        l = n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
2880
 
        l = sqrt(l);
2881
 
 
2882
 
        if (l == 0.0) {
2883
 
                normal[0] = 0.0f;
2884
 
                normal[1] = 0.0f;
2885
 
                normal[2] = 1.0f;
2886
 
 
2887
 
                return;
2888
 
        }
2889
 
        else {
2890
 
                l = 1.0f / l;
2891
 
        }
2892
 
 
2893
 
        n[0] *= l;
2894
 
        n[1] *= l;
2895
 
        n[2] *= l;
2896
 
 
2897
 
        normal[0] = (float) n[0];
2898
 
        normal[1] = (float) n[1];
2899
 
        normal[2] = (float) n[2];
 
2885
        zero_v3(normal);
 
2886
 
 
2887
        /* Newell's Method */
 
2888
        for (i = 0; i < nverts; i++) {
 
2889
                v_curr = vertex_coords[loopstart[i].v];
 
2890
                add_newell_cross_v3_v3v3(normal, v_prev, v_curr);
 
2891
                v_prev = v_curr;
 
2892
        }
 
2893
 
 
2894
        if (UNLIKELY(normalize_v3(normal) == 0.0f)) {
 
2895
                normal[2] = 1.0f; /* other axis set to 0.0 */
 
2896
        }
2900
2897
}
2901
2898
 
2902
 
void mesh_calc_poly_normal_coords(MPoly *mpoly, MLoop *loopstart,
2903
 
                           const float (*vertex_coords)[3], float no[3])
 
2899
void BKE_mesh_calc_poly_normal_coords(MPoly *mpoly, MLoop *loopstart,
 
2900
                                      const float (*vertex_coords)[3], float no[3])
2904
2901
{
2905
2902
        if (mpoly->totloop > 4) {
2906
2903
                mesh_calc_ngon_normal_coords(mpoly, loopstart, vertex_coords, no);
2930
2927
static void mesh_calc_ngon_center(MPoly *mpoly, MLoop *loopstart,
2931
2928
                                  MVert *mvert, float cent[3])
2932
2929
{
2933
 
        const float w= 1.0f / (float)mpoly->totloop;
 
2930
        const float w = 1.0f / (float)mpoly->totloop;
2934
2931
        int i;
2935
2932
 
2936
2933
        zero_v3(cent);
2940
2937
        }
2941
2938
}
2942
2939
 
2943
 
void mesh_calc_poly_center(MPoly *mpoly, MLoop *loopstart,
2944
 
                           MVert *mvarray, float cent[3])
 
2940
void BKE_mesh_calc_poly_center(MPoly *mpoly, MLoop *loopstart,
 
2941
                               MVert *mvarray, float cent[3])
2945
2942
{
2946
2943
        if (mpoly->totloop == 3) {
2947
2944
                cent_tri_v3(cent,
2964
2961
}
2965
2962
 
2966
2963
/* note, passing polynormal is only a speedup so we can skip calculating it */
2967
 
float mesh_calc_poly_area(MPoly *mpoly, MLoop *loopstart,
2968
 
                          MVert *mvarray, float polynormal[3])
 
2964
float BKE_mesh_calc_poly_area(MPoly *mpoly, MLoop *loopstart,
 
2965
                              MVert *mvarray, const float polynormal[3])
2969
2966
{
2970
2967
        if (mpoly->totloop == 3) {
2971
2968
                return area_tri_v3(mvarray[loopstart[0].v].co,
2983
2980
        else {
2984
2981
                int i;
2985
2982
                MLoop *l_iter = loopstart;
2986
 
                float area, polynorm_local[3], (*vertexcos)[3];
2987
 
                float *no= polynormal ? polynormal : polynorm_local;
2988
 
                BLI_array_fixedstack_declare(vertexcos, BM_NGON_STACK_SIZE, mpoly->totloop, __func__);
 
2983
                float area, polynorm_local[3];
 
2984
                float (*vertexcos)[3] = BLI_array_alloca(vertexcos, mpoly->totloop);
 
2985
                const float *no = polynormal ? polynormal : polynorm_local;
2989
2986
 
2990
2987
                /* pack vertex cos into an array for area_poly_v3 */
2991
2988
                for (i = 0; i < mpoly->totloop; i++, l_iter++) {
2994
2991
 
2995
2992
                /* need normal for area_poly_v3 as well */
2996
2993
                if (polynormal == NULL) {
2997
 
                        mesh_calc_poly_normal(mpoly, loopstart, mvarray, no);
 
2994
                        BKE_mesh_calc_poly_normal(mpoly, loopstart, mvarray, polynorm_local);
2998
2995
                }
2999
2996
 
3000
2997
                /* finally calculate the area */
3001
2998
                area = area_poly_v3(mpoly->totloop, vertexcos, no);
3002
2999
 
3003
 
                BLI_array_fixedstack_free(vertexcos);
3004
 
 
3005
3000
                return area;
3006
3001
        }
3007
3002
}
3008
3003
 
 
3004
/* note, results won't be correct if polygon is non-planar */
 
3005
static float mesh_calc_poly_planar_area_centroid(MPoly *mpoly, MLoop *loopstart, MVert *mvarray, float cent[3])
 
3006
{
 
3007
        int i;
 
3008
        float tri_area;
 
3009
        float total_area = 0.0f;
 
3010
        float v1[3], v2[3], v3[3], normal[3], tri_cent[3];
 
3011
 
 
3012
        BKE_mesh_calc_poly_normal(mpoly, loopstart, mvarray, normal);
 
3013
        copy_v3_v3(v1, mvarray[loopstart[0].v].co);
 
3014
        copy_v3_v3(v2, mvarray[loopstart[1].v].co);
 
3015
        zero_v3(cent);
 
3016
 
 
3017
        for (i = 2; i < mpoly->totloop; i++) {
 
3018
                copy_v3_v3(v3, mvarray[loopstart[i].v].co);
 
3019
 
 
3020
                tri_area = area_tri_signed_v3(v1, v2, v3, normal);
 
3021
                total_area += tri_area;
 
3022
 
 
3023
                cent_tri_v3(tri_cent, v1, v2, v3);
 
3024
                madd_v3_v3fl(cent, tri_cent, tri_area);
 
3025
 
 
3026
                copy_v3_v3(v2, v3);
 
3027
        }
 
3028
 
 
3029
        mul_v3_fl(cent, 1.0f / total_area);
 
3030
 
 
3031
        return total_area;
 
3032
}
 
3033
 
 
3034
/**
 
3035
 * This function takes the difference between 2 vertex-coord-arrays
 
3036
 * (\a vert_cos_src, \a vert_cos_dst),
 
3037
 * and applies the difference to \a vert_cos_new relative to \a vert_cos_org.
 
3038
 *
 
3039
 * \param vert_cos_src reference deform source.
 
3040
 * \param vert_cos_dst reference deform destination.
 
3041
 *
 
3042
 * \param vert_cos_org reference for the output location.
 
3043
 * \param vert_cos_new resulting coords.
 
3044
 */
 
3045
void BKE_mesh_calc_relative_deform(
 
3046
        const MPoly *mpoly, const int totpoly,
 
3047
        const MLoop *mloop, const int totvert,
 
3048
 
 
3049
        const float (*vert_cos_src)[3],
 
3050
        const float (*vert_cos_dst)[3],
 
3051
 
 
3052
        const float (*vert_cos_org)[3],
 
3053
              float (*vert_cos_new)[3])
 
3054
{
 
3055
        const MPoly *mp;
 
3056
        int i;
 
3057
 
 
3058
        int *vert_accum = MEM_callocN(sizeof(*vert_accum) * totvert, __func__);
 
3059
 
 
3060
        memset(vert_cos_new, '\0', sizeof(*vert_cos_new) * totvert);
 
3061
 
 
3062
        for (i = 0, mp = mpoly; i < totpoly; i++, mp++) {
 
3063
                const MLoop *loopstart = mloop + mp->loopstart;
 
3064
                int j;
 
3065
 
 
3066
                for (j = 0; j < mp->totloop; j++) {
 
3067
                        int v_prev = (loopstart + ((mp->totloop + (j - 1)) % mp->totloop))->v;
 
3068
                        int v_curr = (loopstart + j)->v;
 
3069
                        int v_next = (loopstart + ((j + 1) % mp->totloop))->v;
 
3070
 
 
3071
                        float tvec[3];
 
3072
 
 
3073
                        barycentric_transform(
 
3074
                                    tvec, vert_cos_dst[v_curr],
 
3075
                                    vert_cos_org[v_prev], vert_cos_org[v_curr], vert_cos_org[v_next],
 
3076
                                    vert_cos_src[v_prev], vert_cos_src[v_curr], vert_cos_src[v_next]
 
3077
                                    );
 
3078
 
 
3079
                        add_v3_v3(vert_cos_new[v_curr], tvec);
 
3080
                        vert_accum[v_curr] += 1;
 
3081
                }
 
3082
        }
 
3083
 
 
3084
        for (i = 0; i < totvert; i++) {
 
3085
                if (vert_accum[i]) {
 
3086
                        mul_v3_fl(vert_cos_new[i], 1.0f / (float)vert_accum[i]);
 
3087
                }
 
3088
                else {
 
3089
                        copy_v3_v3(vert_cos_new[i], vert_cos_org[i]);
 
3090
                }
 
3091
        }
 
3092
 
 
3093
        MEM_freeN(vert_accum);
 
3094
}
 
3095
 
3009
3096
/* Find the index of the loop in 'poly' which references vertex,
3010
3097
 * returns -1 if not found */
3011
3098
int poly_find_loop_from_vert(const MPoly *poly, const MLoop *loopstart,
3012
 
                                                         unsigned vert)
 
3099
                             unsigned vert)
3013
3100
{
3014
3101
        int j;
3015
3102
        for (j = 0; j < poly->totloop; j++, loopstart++) {
3024
3111
 * vertex. Returns the index of the loop matching vertex, or -1 if the
3025
3112
 * vertex is not in 'poly' */
3026
3113
int poly_get_adj_loops_from_vert(unsigned adj_r[3], const MPoly *poly,
3027
 
                                                                 const MLoop *mloop, unsigned vert)
 
3114
                                 const MLoop *mloop, unsigned vert)
3028
3115
{
3029
3116
        int corner = poly_find_loop_from_vert(poly,
3030
 
                                                                                  &mloop[poly->loopstart],
3031
 
                                                                                  vert);
 
3117
                                              &mloop[poly->loopstart],
 
3118
                                              vert);
3032
3119
                
3033
3120
        if (corner != -1) {
3034
3121
                const MLoop *ml = &mloop[poly->loopstart + corner];
3042
3129
        return corner;
3043
3130
}
3044
3131
 
 
3132
/* Return the index of the edge vert that is not equal to 'v'. If
 
3133
 * neither edge vertex is equal to 'v', returns -1. */
 
3134
int BKE_mesh_edge_other_vert(const MEdge *e, int v)
 
3135
{
 
3136
        if (e->v1 == v)
 
3137
                return e->v2;
 
3138
        else if (e->v2 == v)
 
3139
                return e->v1;
 
3140
        else
 
3141
                return -1;
 
3142
}
 
3143
 
3045
3144
/* update the hide flag for edges and faces from the corresponding
3046
3145
 * flag in verts */
3047
 
void mesh_flush_hidden_from_verts(const MVert *mvert,
3048
 
                                                                  const MLoop *mloop,
3049
 
                                                                  MEdge *medge, int totedge,
3050
 
                                                                  MPoly *mpoly, int totpoly)
 
3146
void BKE_mesh_flush_hidden_from_verts(const MVert *mvert,
 
3147
                                      const MLoop *mloop,
 
3148
                                      MEdge *medge, int totedge,
 
3149
                                      MPoly *mpoly, int totpoly)
3051
3150
{
3052
3151
        int i, j;
3053
3152
        
3054
3153
        for (i = 0; i < totedge; i++) {
3055
3154
                MEdge *e = &medge[i];
3056
3155
                if (mvert[e->v1].flag & ME_HIDE ||
3057
 
                   mvert[e->v2].flag & ME_HIDE)
 
3156
                    mvert[e->v2].flag & ME_HIDE)
 
3157
                {
3058
3158
                        e->flag |= ME_HIDE;
3059
 
                else
 
3159
                }
 
3160
                else {
3060
3161
                        e->flag &= ~ME_HIDE;
 
3162
                }
3061
3163
        }
3062
3164
        for (i = 0; i < totpoly; i++) {
3063
3165
                MPoly *p = &mpoly[i];
3069
3171
        }
3070
3172
}
3071
3173
 
 
3174
/**
 
3175
 * simple poly -> vert/edge selection.
 
3176
 */
 
3177
void BKE_mesh_flush_select_from_polys_ex(MVert *mvert,       const int totvert,
 
3178
                                         MLoop *mloop,
 
3179
                                         MEdge *medge,       const int totedge,
 
3180
                                         const MPoly *mpoly, const int totpoly)
 
3181
{
 
3182
        MVert *mv;
 
3183
        MEdge *med;
 
3184
        const MPoly *mp;
 
3185
        int i;
 
3186
 
 
3187
        i = totvert;
 
3188
        for (mv = mvert; i--; mv++) {
 
3189
                mv->flag &= ~SELECT;
 
3190
        }
 
3191
 
 
3192
        i = totedge;
 
3193
        for (med = medge; i--; med++) {
 
3194
                med->flag &= ~SELECT;
 
3195
        }
 
3196
 
 
3197
        i = totpoly;
 
3198
        for (mp = mpoly; i--; mp++) {
 
3199
                /* assume if its selected its not hidden and none of its verts/edges are hidden
 
3200
                 * (a common assumption)*/
 
3201
                if (mp->flag & ME_FACE_SEL) {
 
3202
                        MLoop *ml;
 
3203
                        int j;
 
3204
                        j = mp->totloop;
 
3205
                        for (ml = &mloop[mp->loopstart]; j--; ml++) {
 
3206
                                mvert[ml->v].flag |= SELECT;
 
3207
                                medge[ml->e].flag |= SELECT;
 
3208
                        }
 
3209
                }
 
3210
        }
 
3211
}
 
3212
void BKE_mesh_flush_select_from_polys(Mesh *me)
 
3213
{
 
3214
        BKE_mesh_flush_select_from_polys_ex(me->mvert, me->totvert,
 
3215
                                         me->mloop,
 
3216
                                         me->medge, me->totedge,
 
3217
                                         me->mpoly, me->totpoly);
 
3218
}
 
3219
 
 
3220
void BKE_mesh_flush_select_from_verts_ex(const MVert *mvert, const int UNUSED(totvert),
 
3221
                                         MLoop *mloop,
 
3222
                                         MEdge *medge,       const int totedge,
 
3223
                                         MPoly *mpoly,       const int totpoly)
 
3224
{
 
3225
        MEdge *med;
 
3226
        MPoly *mp;
 
3227
        int i;
 
3228
 
 
3229
        /* edges */
 
3230
        i = totedge;
 
3231
        for (med = medge; i--; med++) {
 
3232
                if ((med->flag & ME_HIDE) == 0) {
 
3233
                        if ((mvert[med->v1].flag & SELECT) && (mvert[med->v2].flag & SELECT)) {
 
3234
                                med->flag |= SELECT;
 
3235
                        }
 
3236
                        else {
 
3237
                                med->flag &= ~SELECT;
 
3238
                        }
 
3239
                }
 
3240
        }
 
3241
 
 
3242
        /* polys */
 
3243
        i = totpoly;
 
3244
        for (mp = mpoly; i--; mp++) {
 
3245
                if ((mp->flag & ME_HIDE) == 0) {
 
3246
                        int ok = TRUE;
 
3247
                        MLoop *ml;
 
3248
                        int j;
 
3249
                        j = mp->totloop;
 
3250
                        for (ml = &mloop[mp->loopstart]; j--; ml++) {
 
3251
                                if ((mvert[ml->v].flag & SELECT) == 0) {
 
3252
                                        ok = FALSE;
 
3253
                                        break;
 
3254
                                }
 
3255
                        }
 
3256
 
 
3257
                        if (ok) {
 
3258
                                mp->flag |= ME_FACE_SEL;
 
3259
                        }
 
3260
                        else {
 
3261
                                mp->flag &= ~ME_FACE_SEL;
 
3262
                        }
 
3263
                }
 
3264
        }
 
3265
}
 
3266
void BKE_mesh_flush_select_from_verts(Mesh *me)
 
3267
{
 
3268
        BKE_mesh_flush_select_from_verts_ex(me->mvert, me->totvert,
 
3269
                                            me->mloop,
 
3270
                                            me->medge, me->totedge,
 
3271
                                            me->mpoly, me->totpoly);
 
3272
}
 
3273
 
 
3274
 
3072
3275
/* basic vertex data functions */
3073
 
int minmax_mesh(Mesh *me, float min[3], float max[3])
 
3276
int BKE_mesh_minmax(Mesh *me, float r_min[3], float r_max[3])
3074
3277
{
3075
 
        int i= me->totvert;
 
3278
        int i = me->totvert;
3076
3279
        MVert *mvert;
3077
 
        for (mvert= me->mvert; i--; mvert++) {
3078
 
                DO_MINMAX(mvert->co, min, max);
 
3280
        for (mvert = me->mvert; i--; mvert++) {
 
3281
                minmax_v3v3_v3(r_min, r_max, mvert->co);
3079
3282
        }
3080
3283
        
3081
3284
        return (me->totvert != 0);
3082
3285
}
3083
3286
 
3084
 
int mesh_center_median(Mesh *me, float cent[3])
 
3287
int BKE_mesh_center_median(Mesh *me, float cent[3])
3085
3288
{
3086
 
        int i= me->totvert;
 
3289
        int i = me->totvert;
3087
3290
        MVert *mvert;
3088
3291
        zero_v3(cent);
3089
 
        for (mvert= me->mvert; i--; mvert++) {
 
3292
        for (mvert = me->mvert; i--; mvert++) {
3090
3293
                add_v3_v3(cent, mvert->co);
3091
3294
        }
3092
3295
        /* otherwise we get NAN for 0 verts */
3093
3296
        if (me->totvert) {
3094
 
                mul_v3_fl(cent, 1.0f/(float)me->totvert);
 
3297
                mul_v3_fl(cent, 1.0f / (float)me->totvert);
3095
3298
        }
3096
3299
 
3097
3300
        return (me->totvert != 0);
3098
3301
}
3099
3302
 
3100
 
int mesh_center_bounds(Mesh *me, float cent[3])
 
3303
int BKE_mesh_center_bounds(Mesh *me, float cent[3])
3101
3304
{
3102
3305
        float min[3], max[3];
3103
3306
        INIT_MINMAX(min, max);
3104
 
        if (minmax_mesh(me, min, max)) {
 
3307
        if (BKE_mesh_minmax(me, min, max)) {
3105
3308
                mid_v3_v3v3(cent, min, max);
3106
3309
                return 1;
3107
3310
        }
3109
3312
        return 0;
3110
3313
}
3111
3314
 
3112
 
void mesh_translate(Mesh *me, float offset[3], int do_keys)
3113
 
{
3114
 
        int i= me->totvert;
 
3315
int BKE_mesh_center_centroid(Mesh *me, float cent[3])
 
3316
{
 
3317
        int i = me->totpoly;
 
3318
        MPoly *mpoly;
 
3319
        float poly_area;
 
3320
        float total_area = 0.0f;
 
3321
        float poly_cent[3];
 
3322
        
 
3323
        zero_v3(cent);
 
3324
        
 
3325
        /* calculate a weighted average of polygon centroids */
 
3326
        for (mpoly = me->mpoly; i--; mpoly++) {
 
3327
                poly_area = mesh_calc_poly_planar_area_centroid(mpoly, me->mloop + mpoly->loopstart, me->mvert, poly_cent);
 
3328
 
 
3329
                madd_v3_v3fl(cent, poly_cent, poly_area);
 
3330
                total_area += poly_area;
 
3331
        }
 
3332
        /* otherwise we get NAN for 0 polys */
 
3333
        if (me->totpoly) {
 
3334
                mul_v3_fl(cent, 1.0f / total_area);
 
3335
        }
 
3336
 
 
3337
        return (me->totpoly != 0);
 
3338
}
 
3339
 
 
3340
void BKE_mesh_translate(Mesh *me, float offset[3], int do_keys)
 
3341
{
 
3342
        int i = me->totvert;
3115
3343
        MVert *mvert;
3116
 
        for (mvert= me->mvert; i--; mvert++) {
 
3344
        for (mvert = me->mvert; i--; mvert++) {
3117
3345
                add_v3_v3(mvert->co, offset);
3118
3346
        }
3119
3347
        
3120
3348
        if (do_keys && me->key) {
3121
3349
                KeyBlock *kb;
3122
 
                for (kb=me->key->block.first; kb; kb=kb->next) {
3123
 
                        float *fp= kb->data;
3124
 
                        for (i= kb->totelem; i--; fp+=3) {
 
3350
                for (kb = me->key->block.first; kb; kb = kb->next) {
 
3351
                        float *fp = kb->data;
 
3352
                        for (i = kb->totelem; i--; fp += 3) {
3125
3353
                                add_v3_v3(fp, offset);
3126
3354
                        }
3127
3355
                }
3128
3356
        }
3129
3357
}
3130
3358
 
3131
 
 
3132
3359
void BKE_mesh_ensure_navmesh(Mesh *me)
3133
3360
{
3134
3361
        if (!CustomData_has_layer(&me->pdata, CD_RECAST)) {
3135
3362
                int i;
3136
3363
                int numFaces = me->totpoly;
3137
3364
                int *recastData;
3138
 
                CustomData_add_layer_named(&me->pdata, CD_RECAST, CD_CALLOC, NULL, numFaces, "recastData");
3139
 
                recastData = (int*)CustomData_get_layer(&me->pdata, CD_RECAST);
3140
 
                for (i=0; i<numFaces; i++) {
3141
 
                        recastData[i] = i+1;
 
3365
                recastData = (int *)MEM_mallocN(numFaces * sizeof(int), __func__);
 
3366
                for (i = 0; i < numFaces; i++) {
 
3367
                        recastData[i] = i + 1;
3142
3368
                }
3143
 
                CustomData_add_layer_named(&me->pdata, CD_RECAST, CD_REFERENCE, recastData, numFaces, "recastData");
 
3369
                CustomData_add_layer_named(&me->pdata, CD_RECAST, CD_ASSIGN, recastData, numFaces, "recastData");
3144
3370
        }
3145
3371
}
3146
3372
 
3147
3373
void BKE_mesh_tessface_calc(Mesh *mesh)
3148
3374
{
3149
 
        mesh->totface = mesh_recalcTessellation(&mesh->fdata, &mesh->ldata, &mesh->pdata,
3150
 
                                               mesh->mvert,
3151
 
                                               mesh->totface, mesh->totloop, mesh->totpoly,
3152
 
                                               /* calc normals right after, don't copy from polys here */
3153
 
                                               FALSE);
 
3375
        mesh->totface = BKE_mesh_recalc_tessellation(&mesh->fdata, &mesh->ldata, &mesh->pdata,
 
3376
                                                     mesh->mvert,
 
3377
                                                     mesh->totface, mesh->totloop, mesh->totpoly,
 
3378
                                                     /* calc normals right after, don't copy from polys here */
 
3379
                                                     FALSE);
3154
3380
 
3155
3381
        mesh_update_customdata_pointers(mesh, TRUE);
3156
3382
}
3166
3392
{
3167
3393
        mesh_tessface_clear_intern(mesh, TRUE);
3168
3394
}
 
3395
 
 
3396
#if 0 /* slow version of the function below */
 
3397
void BKE_mesh_poly_calc_angles(MVert *mvert, MLoop *mloop,
 
3398
                                 MPoly *mp, float angles[])
 
3399
{
 
3400
        MLoop *ml;
 
3401
 
 
3402
        int j;
 
3403
        for (j = 0, ml = mloop + mp->loopstart; j < mp->totloop; j++, ml++) {
 
3404
                MLoop *ml_prev = ME_POLY_LOOP_PREV(mloop, mp, j);
 
3405
                MLoop *ml_next = ME_POLY_LOOP_NEXT(mloop, mp, j);
 
3406
 
 
3407
                float e1[3], e2[3];
 
3408
 
 
3409
                sub_v3_v3v3(e1, mvert[ml_next->v].co, mvert[ml->v].co);
 
3410
                sub_v3_v3v3(e2, mvert[ml_prev->v].co, mvert[ml->v].co);
 
3411
 
 
3412
                angles[j] = (float)M_PI - angle_v3v3(e1, e2);
 
3413
        }
 
3414
}
 
3415
 
 
3416
#else /* equivalent the function above but avoid multiple subtractions + normalize */
 
3417
 
 
3418
void BKE_mesh_poly_calc_angles(MVert *mvert, MLoop *mloop,
 
3419
                                 MPoly *mp, float angles[])
 
3420
{
 
3421
        MLoop *ml = mloop + mp->loopstart;
 
3422
        float nor_prev[3];
 
3423
        float nor_next[3];
 
3424
 
 
3425
        int i_this = mp->totloop - 1;
 
3426
        int i_next = 0;
 
3427
 
 
3428
        sub_v3_v3v3(nor_prev, mvert[ml[i_this - 1].v].co, mvert[ml[i_this].v].co);
 
3429
        normalize_v3(nor_prev);
 
3430
 
 
3431
        while (i_next < mp->totloop) {
 
3432
                sub_v3_v3v3(nor_next, mvert[ml[i_this].v].co, mvert[ml[i_next].v].co);
 
3433
                normalize_v3(nor_next);
 
3434
                angles[i_this] = angle_normalized_v3v3(nor_prev, nor_next);
 
3435
 
 
3436
                /* step */
 
3437
                copy_v3_v3(nor_prev, nor_next);
 
3438
                i_this = i_next;
 
3439
                i_next++;
 
3440
        }
 
3441
}
 
3442
#endif
 
3443
 
 
3444
 
 
3445
void BKE_mesh_do_versions_cd_flag_init(Mesh *mesh)
 
3446
{
 
3447
        if (UNLIKELY(mesh->cd_flag)) {
 
3448
                return;
 
3449
        }
 
3450
        else {
 
3451
                MVert *mv;
 
3452
                MEdge *med;
 
3453
                int i;
 
3454
 
 
3455
                for (mv = mesh->mvert, i = 0; i < mesh->totvert; mv++, i++) {
 
3456
                        if (mv->bweight != 0) {
 
3457
                                mesh->cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
 
3458
                                break;
 
3459
                        }
 
3460
                }
 
3461
 
 
3462
                for (med = mesh->medge, i = 0; i < mesh->totedge; med++, i++) {
 
3463
                        if (med->bweight != 0) {
 
3464
                                mesh->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
 
3465
                                if (mesh->cd_flag & ME_CDFLAG_EDGE_CREASE) {
 
3466
                                        break;
 
3467
                                }
 
3468
                        }
 
3469
                        if (med->crease != 0) {
 
3470
                                mesh->cd_flag |= ME_CDFLAG_EDGE_CREASE;
 
3471
                                if (mesh->cd_flag & ME_CDFLAG_EDGE_BWEIGHT) {
 
3472
                                        break;
 
3473
                                }
 
3474
                        }
 
3475
                }
 
3476
 
 
3477
        }
 
3478
}