part 1 of cleaning up my little array macro library to be a formal API. also removed...
[blender.git] / source / blender / blenkernel / intern / customdata.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 *
20 * The Original Code is Copyright (C) 2006 Blender Foundation.
21 * All rights reserved.
22 *
23 * The Original Code is: all of this file.
24 *
25 * Contributor(s): Ben Batt <benbatt@gmail.com>
26 *
27 * ***** END GPL LICENSE BLOCK *****
28 *
29 * Implementation of CustomData.
30 *
31 * BKE_customdata.h contains the function prototypes for this file.
32 *
33 */ 
34
35 #include "BKE_customdata.h"
36 #include "BKE_utildefines.h" // CLAMP
37 #include "BLI_arithb.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_linklist.h"
40 #include "BLI_mempool.h"
41 #include "BLI_cellalloc.h"
42
43 #include "DNA_customdata_types.h"
44 #include "DNA_listBase.h"
45 #include "DNA_meshdata_types.h"
46
47 #include "MEM_guardedalloc.h"
48
49 #include "bmesh.h"
50
51 #include <math.h>
52 #include <string.h>
53
54 /* number of layers to add when growing a CustomData object */
55 #define CUSTOMDATA_GROW 5
56
57 /********************* Layer type information **********************/
58 typedef struct LayerTypeInfo {
59         int size;          /* the memory size of one element of this layer's data */
60         char *structname;  /* name of the struct used, for file writing */
61         int structnum;     /* number of structs per element, for file writing */
62         char *defaultname; /* default layer name */
63
64         /* a function to copy count elements of this layer's data
65          * (deep copy if appropriate)
66          * if NULL, memcpy is used
67          */
68         void (*copy)(const void *source, void *dest, int count);
69
70         /* a function to free any dynamically allocated components of this
71          * layer's data (note the data pointer itself should not be freed)
72          * size should be the size of one element of this layer's data (e.g.
73          * LayerTypeInfo.size)
74          */
75         void (*free)(void *data, int count, int size);
76
77         /* a function to interpolate between count source elements of this
78          * layer's data and store the result in dest
79          * if weights == NULL or sub_weights == NULL, they should default to 1
80          *
81          * weights gives the weight for each element in sources
82          * sub_weights gives the sub-element weights for each element in sources
83          *    (there should be (sub element count)^2 weights per element)
84          * count gives the number of elements in sources
85          */
86         void (*interp)(void **sources, float *weights, float *sub_weights,
87                        int count, void *dest);
88
89     /* a function to swap the data in corners of the element */
90         void (*swap)(void *data, int *corner_indices);
91
92     /* a function to set a layer's data to default values. if NULL, the
93            default is assumed to be all zeros */
94         void (*set_default)(void *data, int count);
95
96     /* functions necassary for geometry collapse*/
97         int (*equal)(void *data1, void *data2);
98         void (*multiply)(void *data, float fac);
99         void (*initminmax)(void *min, void *max);
100         void (*add)(void *data1, void *data2);
101         void (*dominmax)(void *data1, void *min, void *max);
102         void (*copyvalue)(void *source, void *dest);
103 } LayerTypeInfo;
104
105 static void layerCopy_mdeformvert(const void *source, void *dest,
106                                   int count)
107 {
108         int i, size = sizeof(MDeformVert);
109
110         memcpy(dest, source, count * size);
111
112         for(i = 0; i < count; ++i) {
113                 MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
114
115                 if(dvert->totweight) {
116                         MDeformWeight *dw = BLI_cellalloc_calloc(dvert->totweight * sizeof(*dw),
117                                                                                         "layerCopy_mdeformvert dw");
118
119                         memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
120                         dvert->dw = dw;
121                 }
122                 else
123                         dvert->dw = NULL;
124         }
125 }
126
127 static void layerFree_mdeformvert(void *data, int count, int size)
128 {
129         int i;
130
131         for(i = 0; i < count; ++i) {
132                 MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
133
134                 if(dvert->dw) {
135                         BLI_cellalloc_free(dvert->dw);
136                         dvert->dw = NULL;
137                         dvert->totweight = 0;
138                 }
139         }
140 }
141
142 static void linklist_free_simple(void *link)
143 {
144         MEM_freeN(link);
145 }
146
147 static void layerInterp_mdeformvert(void **sources, float *weights,
148                                     float *sub_weights, int count, void *dest)
149 {
150         MDeformVert *dvert = dest;
151         LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
152         LinkNode *node;
153         int i, j, totweight;
154
155         if(count <= 0) return;
156
157         /* build a list of unique def_nrs for dest */
158         totweight = 0;
159         for(i = 0; i < count; ++i) {
160                 MDeformVert *source = sources[i];
161                 float interp_weight = weights ? weights[i] : 1.0f;
162
163                 for(j = 0; j < source->totweight; ++j) {
164                         MDeformWeight *dw = &source->dw[j];
165
166                         for(node = dest_dw; node; node = node->next) {
167                                 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
168
169                                 if(tmp_dw->def_nr == dw->def_nr) {
170                                         tmp_dw->weight += dw->weight * interp_weight;
171                                         break;
172                                 }
173                         }
174
175                         /* if this def_nr is not in the list, add it */
176                         if(!node) {
177                                 MDeformWeight *tmp_dw = BLI_cellalloc_calloc(sizeof(*tmp_dw),
178                                                             "layerInterp_mdeformvert tmp_dw");
179                                 tmp_dw->def_nr = dw->def_nr;
180                                 tmp_dw->weight = dw->weight * interp_weight;
181                                 BLI_linklist_prepend(&dest_dw, tmp_dw);
182                                 totweight++;
183                         }
184                 }
185         }
186
187         /* now we know how many unique deform weights there are, so realloc */
188         if(dvert->dw) BLI_cellalloc_free(dvert->dw);
189
190         if(totweight) {
191                 dvert->dw = BLI_cellalloc_calloc(sizeof(*dvert->dw) * totweight,
192                                         "layerInterp_mdeformvert dvert->dw");
193                 dvert->totweight = totweight;
194
195                 for(i = 0, node = dest_dw; node; node = node->next, ++i)
196                         dvert->dw[i] = *((MDeformWeight *)node->link);
197         }
198         else
199                 memset(dvert, 0, sizeof(*dvert));
200
201         BLI_linklist_free(dest_dw, linklist_free_simple);
202 }
203
204
205 static void layerInterp_msticky(void **sources, float *weights,
206                                 float *sub_weights, int count, void *dest)
207 {
208         float co[2], w;
209         MSticky *mst;
210         int i;
211
212         co[0] = co[1] = 0.0f;
213         for(i = 0; i < count; i++) {
214                 w = weights ? weights[i] : 1.0f;
215                 mst = (MSticky*)sources[i];
216
217                 co[0] += w*mst->co[0];
218                 co[1] += w*mst->co[1];
219         }
220
221         mst = (MSticky*)dest;
222         mst->co[0] = co[0];
223         mst->co[1] = co[1];
224 }
225
226
227 static void layerCopy_tface(const void *source, void *dest, int count)
228 {
229         const MTFace *source_tf = (const MTFace*)source;
230         MTFace *dest_tf = (MTFace*)dest;
231         int i;
232
233         for(i = 0; i < count; ++i)
234                 dest_tf[i] = source_tf[i];
235 }
236
237 static void layerInterp_tface(void **sources, float *weights,
238                               float *sub_weights, int count, void *dest)
239 {
240         MTFace *tf = dest;
241         int i, j, k;
242         float uv[4][2];
243         float *sub_weight;
244
245         if(count <= 0) return;
246
247         memset(uv, 0, sizeof(uv));
248
249         sub_weight = sub_weights;
250         for(i = 0; i < count; ++i) {
251                 float weight = weights ? weights[i] : 1;
252                 MTFace *src = sources[i];
253
254                 for(j = 0; j < 4; ++j) {
255                         if(sub_weights) {
256                                 for(k = 0; k < 4; ++k, ++sub_weight) {
257                                         float w = (*sub_weight) * weight;
258                                         float *tmp_uv = src->uv[k];
259
260                                         uv[j][0] += tmp_uv[0] * w;
261                                         uv[j][1] += tmp_uv[1] * w;
262                                 }
263                         } else {
264                                 uv[j][0] += src->uv[j][0] * weight;
265                                 uv[j][1] += src->uv[j][1] * weight;
266                         }
267                 }
268         }
269
270         *tf = *(MTFace *)sources[0];
271
272         for(j = 0; j < 4; ++j) {
273                 tf->uv[j][0] = uv[j][0];
274                 tf->uv[j][1] = uv[j][1];
275         }
276 }
277
278 static void layerSwap_tface(void *data, int *corner_indices)
279 {
280         MTFace *tf = data;
281         float uv[4][2];
282         static const short pin_flags[4] =
283             { TF_PIN1, TF_PIN2, TF_PIN3, TF_PIN4 };
284         static const char sel_flags[4] =
285             { TF_SEL1, TF_SEL2, TF_SEL3, TF_SEL4 };
286         short unwrap = tf->unwrap & ~(TF_PIN1 | TF_PIN2 | TF_PIN3 | TF_PIN4);
287         char flag = tf->flag & ~(TF_SEL1 | TF_SEL2 | TF_SEL3 | TF_SEL4);
288         int j;
289
290         for(j = 0; j < 4; ++j) {
291                 int source_index = corner_indices[j];
292
293                 uv[j][0] = tf->uv[source_index][0];
294                 uv[j][1] = tf->uv[source_index][1];
295
296                 // swap pinning flags around
297                 if(tf->unwrap & pin_flags[source_index]) {
298                         unwrap |= pin_flags[j];
299                 }
300
301                 // swap selection flags around
302                 if(tf->flag & sel_flags[source_index]) {
303                         flag |= sel_flags[j];
304                 }
305         }
306
307         memcpy(tf->uv, uv, sizeof(tf->uv));
308         tf->unwrap = unwrap;
309         tf->flag = flag;
310 }
311
312 static void layerDefault_tface(void *data, int count)
313 {
314         static MTFace default_tf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, NULL,
315                                    0, 0, TF_DYNAMIC, 0, 0};
316         MTFace *tf = (MTFace*)data;
317         int i;
318
319         for(i = 0; i < count; i++)
320                 tf[i] = default_tf;
321 }
322
323 static void layerCopy_origspace_face(const void *source, void *dest, int count)
324 {
325         const OrigSpaceFace *source_tf = (const OrigSpaceFace*)source;
326         OrigSpaceFace *dest_tf = (OrigSpaceFace*)dest;
327         int i;
328
329         for(i = 0; i < count; ++i)
330                 dest_tf[i] = source_tf[i];
331 }
332
333 static void layerInterp_origspace_face(void **sources, float *weights,
334                                                           float *sub_weights, int count, void *dest)
335 {
336         OrigSpaceFace *osf = dest;
337         int i, j, k;
338         float uv[4][2];
339         float *sub_weight;
340
341         if(count <= 0) return;
342
343         memset(uv, 0, sizeof(uv));
344
345         sub_weight = sub_weights;
346         for(i = 0; i < count; ++i) {
347                 float weight = weights ? weights[i] : 1;
348                 OrigSpaceFace *src = sources[i];
349
350                 for(j = 0; j < 4; ++j) {
351                         if(sub_weights) {
352                                 for(k = 0; k < 4; ++k, ++sub_weight) {
353                                         float w = (*sub_weight) * weight;
354                                         float *tmp_uv = src->uv[k];
355
356                                         uv[j][0] += tmp_uv[0] * w;
357                                         uv[j][1] += tmp_uv[1] * w;
358                                 }
359                         } else {
360                                 uv[j][0] += src->uv[j][0] * weight;
361                                 uv[j][1] += src->uv[j][1] * weight;
362                         }
363                 }
364         }
365
366         *osf = *(OrigSpaceFace *)sources[0];
367         for(j = 0; j < 4; ++j) {
368                 osf->uv[j][0] = uv[j][0];
369                 osf->uv[j][1] = uv[j][1];
370         }
371 }
372
373 static void layerSwap_origspace_face(void *data, int *corner_indices)
374 {
375         OrigSpaceFace *osf = data;
376         float uv[4][2];
377         int j;
378
379         for(j = 0; j < 4; ++j) {
380                 uv[j][0] = osf->uv[corner_indices[j]][0];
381                 uv[j][1] = osf->uv[corner_indices[j]][1];
382         }
383         memcpy(osf->uv, uv, sizeof(osf->uv));
384 }
385
386 static void layerDefault_origspace_face(void *data, int count)
387 {
388         static OrigSpaceFace default_osf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
389         OrigSpaceFace *osf = (OrigSpaceFace*)data;
390         int i;
391
392         for(i = 0; i < count; i++)
393                 osf[i] = default_osf;
394 }
395
396 /* Adapted from sculptmode.c */
397 static void mdisps_bilinear(float out[3], float (*disps)[3], int st, float u, float v)
398 {
399         int x, y, x2, y2;
400         const int st_max = st - 1;
401         float urat, vrat, uopp;
402         float d[4][3], d2[2][3];
403
404         if(u < 0)
405                 u = 0;
406         else if(u >= st)
407                 u = st_max;
408         if(v < 0)
409                 v = 0;
410         else if(v >= st)
411                 v = st_max;
412
413         x = floor(u);
414         y = floor(v);
415         x2 = x + 1;
416         y2 = y + 1;
417
418         if(x2 >= st) x2 = st_max;
419         if(y2 >= st) y2 = st_max;
420         
421         urat = u - x;
422         vrat = v - y;
423         uopp = 1 - urat;
424
425         VecCopyf(d[0], disps[y * st + x]);
426         VecCopyf(d[1], disps[y * st + x2]);
427         VecCopyf(d[2], disps[y2 * st + x]);
428         VecCopyf(d[3], disps[y2 * st + x2]);
429         VecMulf(d[0], uopp);
430         VecMulf(d[1], urat);
431         VecMulf(d[2], uopp);
432         VecMulf(d[3], urat);
433
434         VecAddf(d2[0], d[0], d[1]);
435         VecAddf(d2[1], d[2], d[3]);
436         VecMulf(d2[0], 1 - vrat);
437         VecMulf(d2[1], vrat);
438
439         VecAddf(out, d2[0], d2[1]);
440 }
441
442 static void layerSwap_mdisps(void *data, int *ci)
443 {
444         MDisps *s = data;
445         float (*d)[3] = NULL;
446         int x, y, st;
447
448         if(!(ci[0] == 2 && ci[1] == 3 && ci[2] == 0 && ci[3] == 1)) return;
449
450         d = MEM_callocN(sizeof(float) * 3 * s->totdisp, "mdisps swap");
451         st = sqrt(s->totdisp);
452
453         for(y = 0; y < st; ++y) {
454                 for(x = 0; x < st; ++x) {
455                         VecCopyf(d[(st - y - 1) * st + (st - x - 1)], s->disps[y * st + x]);
456                 }
457         }
458         
459         if(s->disps)
460                 MEM_freeN(s->disps);
461         s->disps = d;
462 }
463
464 static void layerInterp_mdisps(void **sources, float *weights, float *sub_weights,
465                                int count, void *dest)
466 {
467         MDisps *d = dest;
468         MDisps *s = NULL;
469         int st, stl;
470         int i, x, y;
471         float crn[4][2];
472         float (*sw)[4] = NULL;
473
474         /* Initialize the destination */
475         for(i = 0; i < d->totdisp; ++i) {
476                 float z[3] = {0,0,0};
477                 VecCopyf(d->disps[i], z);
478         }
479
480         /* For now, some restrictions on the input */
481         if(count != 1 || !sub_weights) return;
482
483         st = sqrt(d->totdisp);
484         stl = st - 1;
485
486         sw = (void*)sub_weights;
487         for(i = 0; i < 4; ++i) {
488                 crn[i][0] = 0 * sw[i][0] + stl * sw[i][1] + stl * sw[i][2] + 0 * sw[i][3];
489                 crn[i][1] = 0 * sw[i][0] + 0 * sw[i][1] + stl * sw[i][2] + stl * sw[i][3];
490         }
491
492         s = sources[0];
493         for(y = 0; y < st; ++y) {
494                 for(x = 0; x < st; ++x) {
495                         /* One suspects this code could be cleaner. */
496                         float xl = (float)x / (st - 1);
497                         float yl = (float)y / (st - 1);
498                         float mid1[2] = {crn[0][0] * (1 - xl) + crn[1][0] * xl,
499                                          crn[0][1] * (1 - xl) + crn[1][1] * xl};
500                         float mid2[2] = {crn[3][0] * (1 - xl) + crn[2][0] * xl,
501                                          crn[3][1] * (1 - xl) + crn[2][1] * xl};
502                         float mid3[2] = {mid1[0] * (1 - yl) + mid2[0] * yl,
503                                          mid1[1] * (1 - yl) + mid2[1] * yl};
504
505                         float srcdisp[3];
506
507                         mdisps_bilinear(srcdisp, s->disps, st, mid3[0], mid3[1]);
508                         VecCopyf(d->disps[y * st + x], srcdisp);
509                 }
510         }
511 }
512
513 static void layerCopy_mdisps(const void *source, void *dest, int count)
514 {
515         int i;
516         const MDisps *s = source;
517         MDisps *d = dest;
518
519         for(i = 0; i < count; ++i) {
520                 if(s[i].disps) {
521                         d[i].disps = MEM_dupallocN(s[i].disps);
522                         d[i].totdisp = s[i].totdisp;
523                 }
524                 else {
525                         d[i].disps = NULL;
526                         d[i].totdisp = 0;
527                 }
528                 
529         }
530 }
531
532 static void layerFree_mdisps(void *data, int count, int size)
533 {
534         int i;
535         MDisps *d = data;
536
537         for(i = 0; i < count; ++i) {
538                 if(d[i].disps)
539                         MEM_freeN(d[i].disps);
540                 d[i].disps = NULL;
541                 d[i].totdisp = 0;
542         }
543 }
544
545 /* --------- */
546 static void layerCopyValue_mloopcol(void *source, void *dest)
547 {
548         MLoopCol *m1 = source, *m2 = dest;
549         
550         m2->r = m1->r;
551         m2->g = m1->g;
552         m2->b = m1->b;
553         m2->a = m1->a;
554 }
555
556 static int layerEqual_mloopcol(void *data1, void *data2)
557 {
558         MLoopCol *m1 = data1, *m2 = data2;
559         float r, g, b, a;
560
561         r = m1->r - m2->r;
562         g = m1->g - m2->g;
563         b = m1->b - m2->b;
564         a = m1->a - m2->a;
565
566         return r*r + g*g + b*b + a*a < 0.001;
567 }
568
569 static void layerMultiply_mloopcol(void *data, float fac)
570 {
571         MLoopCol *m = data;
572
573         m->r = (float)m->r * fac;
574         m->g = (float)m->g * fac;
575         m->b = (float)m->b * fac;
576         m->a = (float)m->a * fac;
577 }
578
579 static void layerAdd_mloopcol(void *data1, void *data2)
580 {
581         MLoopCol *m = data1, *m2 = data2;
582
583         m->r += m2->r;
584         m->g += m2->g;
585         m->b += m2->b;
586         m->a += m2->a;
587 }
588
589 static void layerDoMinMax_mloopcol(void *data, void *vmin, void *vmax)
590 {
591         MLoopCol *m = data;
592         MLoopCol *min = vmin, *max = vmax;
593
594         if (m->r < min->r) min->r = m->r;
595         if (m->g < min->g) min->g = m->g;
596         if (m->b < min->b) min->b = m->b;
597         if (m->a < min->a) min->a = m->a;
598         
599         if (m->r > max->r) max->r = m->r;
600         if (m->g > max->g) max->g = m->g;
601         if (m->b > max->b) max->b = m->b;
602         if (m->a > max->a) max->a = m->a;
603 }
604
605 static void layerInitMinMax_mloopcol(void *vmin, void *vmax)
606 {
607         MLoopCol *min = vmin, *max = vmax;
608
609         min->r = 255;
610         min->g = 255;
611         min->b = 255;
612         min->a = 255;
613
614         max->r = 0;
615         max->g = 0;
616         max->b = 0;
617         max->a = 0;
618 }
619
620 static void layerDefault_mloopcol(void *data, int count)
621 {
622         MLoopCol default_mloopcol = {255,255,255,255};
623         MLoopCol *mlcol = (MLoopCol*)data;
624         int i;
625
626         for(i = 0; i < count; i++)
627                 mlcol[i] = default_mloopcol;
628
629 }
630
631 static void layerInterp_mloopcol(void **sources, float *weights,
632                                 float *sub_weights, int count, void *dest)
633 {
634         MLoopCol *mc = dest;
635         int i;
636         float *sub_weight;
637         struct {
638                 float a;
639                 float r;
640                 float g;
641                 float b;
642         } col;
643         col.a = col.r = col.g = col.b = 0;
644
645         sub_weight = sub_weights;
646         for(i = 0; i < count; ++i){
647                 float weight = weights ? weights[i] : 1;
648                 MLoopCol *src = sources[i];
649                 if(sub_weights){
650                         col.a += src->a * (*sub_weight) * weight;
651                         col.r += src->r * (*sub_weight) * weight;
652                         col.g += src->g * (*sub_weight) * weight;
653                         col.b += src->b * (*sub_weight) * weight;
654                         sub_weight++;           
655                 } else {
656                         col.a += src->a * weight;
657                         col.r += src->r * weight;
658                         col.g += src->g * weight;
659                         col.b += src->b * weight;
660                 }
661         }
662         
663         /* Subdivide smooth or fractal can cause problems without clamping
664          * although weights should also not cause this situation */
665         CLAMP(col.a, 0.0f, 255.0f);
666         CLAMP(col.r, 0.0f, 255.0f);
667         CLAMP(col.g, 0.0f, 255.0f);
668         CLAMP(col.b, 0.0f, 255.0f);
669         
670         mc->a = (int)col.a;
671         mc->r = (int)col.r;
672         mc->g = (int)col.g;
673         mc->b = (int)col.b;
674 }
675
676 static void layerCopyValue_mloopuv(void *source, void *dest)
677 {
678         MLoopUV *luv1 = source, *luv2 = dest;
679         
680         luv2->uv[0] = luv1->uv[0];
681         luv2->uv[1] = luv1->uv[1];
682 }
683
684 static int layerEqual_mloopuv(void *data1, void *data2)
685 {
686         MLoopUV *luv1 = data1, *luv2 = data2;
687         float u, v;
688
689         u = luv1->uv[0] - luv2->uv[0];
690         v = luv1->uv[1] - luv2->uv[1];
691
692         return u*u + v*v < 0.00001;
693 }
694
695 static void layerMultiply_mloopuv(void *data, float fac)
696 {
697         MLoopUV *luv = data;
698
699         luv->uv[0] *= fac;
700         luv->uv[1] *= fac;
701 }
702
703 static void layerInitMinMax_mloopuv(void *vmin, void *vmax)
704 {
705         MLoopUV *min = vmin, *max = vmax;
706
707         INIT_MINMAX2(min->uv, max->uv);
708 }
709
710 static void layerDoMinMax_mloopuv(void *data, void *vmin, void *vmax)
711 {
712         MLoopUV *min = vmin, *max = vmax, *luv = data;
713
714         DO_MINMAX2(luv->uv, min->uv, max->uv);
715 }
716
717 static void layerAdd_mloopuv(void *data1, void *data2)
718 {
719         MLoopUV *l1 = data1, *l2 = data2;
720
721         l1->uv[0] += l2->uv[0];
722         l1->uv[1] += l2->uv[1];
723 }
724
725 static void layerInterp_mloopuv(void **sources, float *weights,
726                                 float *sub_weights, int count, void *dest)
727 {
728         MLoopUV *mluv = dest;
729         int i;
730         float *sub_weight;
731         struct {
732                 float u;
733                 float v;
734         }uv;
735         uv.u = uv.v = 0.0;
736
737         sub_weight = sub_weights;
738         for(i = 0; i < count; ++i){
739                 float weight = weights ? weights[i] : 1;
740                 MLoopUV *src = sources[i];
741                 if(sub_weights){
742                         uv.u += src->uv[0] * (*sub_weight) * weight;
743                         uv.v += src->uv[1] * (*sub_weight) * weight;
744                         sub_weight++;           
745                 } else {
746                         uv.u += src->uv[0] * weight;
747                         uv.v += src->uv[1] * weight;
748                 }
749         }
750         mluv->uv[0] = uv.u;
751         mluv->uv[1] = uv.v;
752 }
753
754 static void layerInterp_mcol(void **sources, float *weights,
755                              float *sub_weights, int count, void *dest)
756 {
757         MCol *mc = dest;
758         int i, j, k;
759         struct {
760                 float a;
761                 float r;
762                 float g;
763                 float b;
764         } col[4];
765         float *sub_weight;
766
767         if(count <= 0) return;
768
769         memset(col, 0, sizeof(col));
770         
771         sub_weight = sub_weights;
772         for(i = 0; i < count; ++i) {
773                 float weight = weights ? weights[i] : 1;
774
775                 for(j = 0; j < 4; ++j) {
776                         if(sub_weights) {
777                                 MCol *src = sources[i];
778                                 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
779                                         col[j].a += src->a * (*sub_weight) * weight;
780                                         col[j].r += src->r * (*sub_weight) * weight;
781                                         col[j].g += src->g * (*sub_weight) * weight;
782                                         col[j].b += src->b * (*sub_weight) * weight;
783                                 }
784                         } else {
785                                 MCol *src = sources[i];
786                                 col[j].a += src[j].a * weight;
787                                 col[j].r += src[j].r * weight;
788                                 col[j].g += src[j].g * weight;
789                                 col[j].b += src[j].b * weight;
790                         }
791                 }
792         }
793
794         for(j = 0; j < 4; ++j) {
795                 
796                 /* Subdivide smooth or fractal can cause problems without clamping
797                  * although weights should also not cause this situation */
798                 CLAMP(col[j].a, 0.0f, 255.0f);
799                 CLAMP(col[j].r, 0.0f, 255.0f);
800                 CLAMP(col[j].g, 0.0f, 255.0f);
801                 CLAMP(col[j].b, 0.0f, 255.0f);
802                 
803                 mc[j].a = (int)col[j].a;
804                 mc[j].r = (int)col[j].r;
805                 mc[j].g = (int)col[j].g;
806                 mc[j].b = (int)col[j].b;
807         }
808 }
809
810 static void layerSwap_mcol(void *data, int *corner_indices)
811 {
812         MCol *mcol = data;
813         MCol col[4];
814         int j;
815
816         for(j = 0; j < 4; ++j)
817                 col[j] = mcol[corner_indices[j]];
818
819         memcpy(mcol, col, sizeof(col));
820 }
821
822 static void layerDefault_mcol(void *data, int count)
823 {
824         static MCol default_mcol = {255, 255, 255, 255};
825         MCol *mcol = (MCol*)data;
826         int i;
827
828         for(i = 0; i < 4*count; i++)
829                 mcol[i] = default_mcol;
830 }
831
832
833
834 const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
835         {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
836         {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
837          NULL},
838         {sizeof(MDeformVert), "MDeformVert", 1, NULL, layerCopy_mdeformvert,
839          layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
840         {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL, NULL},
841         {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL, NULL},
842         {sizeof(MTFace), "MTFace", 1, "UVTex", layerCopy_tface, NULL,
843          layerInterp_tface, layerSwap_tface, layerDefault_tface},
844         /* 4 MCol structs per face */
845         {sizeof(MCol)*4, "MCol", 4, "Col", NULL, NULL, layerInterp_mcol,
846          layerSwap_mcol, layerDefault_mcol},
847         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
848         /* 3 floats per normal vector */
849         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
850         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
851         {sizeof(MFloatProperty), "MFloatProperty",1,"Float",NULL,NULL,NULL,NULL},
852         {sizeof(MIntProperty), "MIntProperty",1,"Int",NULL,NULL,NULL,NULL},
853         {sizeof(MStringProperty), "MStringProperty",1,"String",NULL,NULL,NULL,NULL},
854         {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVTex", layerCopy_origspace_face, NULL,
855          layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face},
856         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
857         {sizeof(MTexPoly), "MTexPoly", 1, "Face Texture", NULL, NULL, NULL, NULL, NULL},
858         {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, layerInterp_mloopuv, NULL, NULL,
859          layerEqual_mloopuv, layerMultiply_mloopuv, layerInitMinMax_mloopuv, 
860          layerAdd_mloopuv, layerDoMinMax_mloopuv, layerCopyValue_mloopuv},
861         {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL, 
862          layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol, 
863          layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
864         {sizeof(float)*3*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
865         {sizeof(MDisps), "MDisps", 1, NULL, layerCopy_mdisps,
866          layerFree_mdisps, layerInterp_mdisps, layerSwap_mdisps, NULL},
867         {sizeof(MCol)*4, "MCol", 4, "WeightCol", NULL, NULL, layerInterp_mcol,
868          layerSwap_mcol, layerDefault_mcol},
869         {sizeof(MPoly), "MPoly", 1, "NGon Face", NULL, NULL, NULL, NULL, NULL},
870         {sizeof(MLoop), "MLoop", 1, "NGon Face-Vertex", NULL, NULL, NULL, NULL, NULL},
871         {sizeof(MLoopCol), "MLoopCol", 1, "WeightLoopCol", NULL, NULL, layerInterp_mloopcol, NULL, 
872          layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol, 
873          layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
874 };
875
876 const char *LAYERTYPENAMES[CD_NUMTYPES] = {
877         "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace", "CDMTFace",
878         "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags","CDMFloatProperty",
879         "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco", "CDMTexPoly", "CDMLoopUV",
880         "CDMloopCol", "CDTangent", "CDMDisps", "CDWeightMCol"};
881
882 const CustomDataMask CD_MASK_BAREMESH =
883         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE | CD_MASK_MLOOP | CD_MASK_MPOLY;
884 const CustomDataMask CD_MASK_MESH =
885         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
886         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
887         CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS |
888         CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MPOLY | CD_MASK_MLOOP |
889         CD_MASK_MTEXPOLY;
890 const CustomDataMask CD_MASK_EDITMESH =
891         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MLOOPUV |
892         CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY |
893         CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS;
894 const CustomDataMask CD_MASK_DERIVEDMESH =
895         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
896         CD_MASK_MCOL | CD_MASK_ORIGINDEX | CD_MASK_PROP_FLT | CD_MASK_PROP_INT |
897         CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_WEIGHT_MLOOPCOL |
898         CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORCO | CD_MASK_TANGENT | CD_MASK_WEIGHT_MCOL;
899 const CustomDataMask CD_MASK_BMESH = CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY |
900         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR;
901 const CustomDataMask CD_MASK_FACECORNERS =
902         CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_MTEXPOLY | CD_MASK_MLOOPUV |
903         CD_MASK_MLOOPCOL;
904
905 static const LayerTypeInfo *layerType_getInfo(int type)
906 {
907         if(type < 0 || type >= CD_NUMTYPES) return NULL;
908
909         return &LAYERTYPEINFO[type];
910 }
911
912 static const char *layerType_getName(int type)
913 {
914         if(type < 0 || type >= CD_NUMTYPES) return NULL;
915
916         return LAYERTYPENAMES[type];
917 }
918
919 /********************* CustomData functions *********************/
920 static void customData_update_offsets(CustomData *data);
921
922 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
923         int type, int alloctype, void *layerdata, int totelem, const char *name);
924
925 void customData_update_typemap(CustomData *data)
926 {
927         int i, lasttype = -1;
928
929         for (i=0; i<CD_NUMTYPES; i++) {
930                 data->typemap[i] = -1;
931         }
932
933         for (i=0; i<data->totlayer; i++) {
934                 if (data->layers[i].type != lasttype) {
935                         data->typemap[data->layers[i].type] = i;
936                 }
937                 lasttype = data->layers[i].type;
938         }
939 }
940
941 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
942                       CustomDataMask mask, int alloctype, int totelem)
943 {
944         const LayerTypeInfo *typeInfo;
945         CustomDataLayer *layer, *newlayer;
946         int i, type, number = 0, lasttype = -1, lastactive = 0, lastrender = 0, lastclone = 0, lastmask = 0;
947         
948         for(i = 0; i < source->totlayer; ++i) {
949                 layer = &source->layers[i];
950                 typeInfo = layerType_getInfo(layer->type);
951
952                 type = layer->type;
953
954                 if (type != lasttype) {
955                         number = 0;
956                         lastactive = layer->active;
957                         lastrender = layer->active_rnd;
958                         lastclone = layer->active_clone;
959                         lastmask = layer->active_mask;
960                         lasttype = type;
961                 }
962                 else
963                         number++;
964
965                 if(layer->flag & CD_FLAG_NOCOPY) continue;
966                 else if(!((int)mask & (int)(1 << (int)type))) continue;
967                 else if(number+1 < CustomData_number_of_layers(dest, type)) continue;
968
969                 if((alloctype == CD_ASSIGN) && (layer->flag & CD_FLAG_NOFREE))
970                         newlayer = customData_add_layer__internal(dest, type, CD_REFERENCE,
971                                 layer->data, totelem, layer->name);
972                 else
973                         newlayer = customData_add_layer__internal(dest, type, alloctype,
974                                 layer->data, totelem, layer->name);
975                 
976                 if(newlayer) {
977                         newlayer->active = lastactive;
978                         newlayer->active_rnd = lastrender;
979                         newlayer->active_clone = lastclone;
980                         newlayer->active_mask = lastmask;
981                 }
982         }
983
984         customData_update_typemap(dest);
985 }
986
987 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
988                      CustomDataMask mask, int alloctype, int totelem)
989 {
990         memset(dest, 0, sizeof(*dest));
991
992         CustomData_merge(source, dest, mask, alloctype, totelem);
993 }
994
995 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
996 {
997         const LayerTypeInfo *typeInfo;
998
999         if(!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
1000                 typeInfo = layerType_getInfo(layer->type);
1001
1002                 if(typeInfo->free)
1003                         typeInfo->free(layer->data, totelem, typeInfo->size);
1004
1005                 if(layer->data)
1006                         MEM_freeN(layer->data);
1007         }
1008 }
1009
1010 void CustomData_free(CustomData *data, int totelem)
1011 {
1012         int i;
1013
1014         for(i = 0; i < data->totlayer; ++i)
1015                 customData_free_layer__internal(&data->layers[i], totelem);
1016
1017         if(data->layers)
1018                 MEM_freeN(data->layers);
1019         
1020         memset(data, 0, sizeof(*data));
1021 }
1022
1023 static void customData_update_offsets(CustomData *data)
1024 {
1025         const LayerTypeInfo *typeInfo;
1026         int i, offset = 0;
1027
1028         for(i = 0; i < data->totlayer; ++i) {
1029                 typeInfo = layerType_getInfo(data->layers[i].type);
1030
1031                 data->layers[i].offset = offset;
1032                 offset += typeInfo->size;
1033         }
1034
1035         data->totsize = offset;
1036 }
1037
1038 int CustomData_get_layer_index(const CustomData *data, int type)
1039 {
1040         int i; 
1041
1042         for(i=0; i < data->totlayer; ++i)
1043                 if(data->layers[i].type == type)
1044                         return i;
1045
1046         return -1;
1047 }
1048
1049 int CustomData_get_named_layer_index(const CustomData *data, int type, char *name)
1050 {
1051         int i;
1052
1053         for(i=0; i < data->totlayer; ++i)
1054                 if(data->layers[i].type == type && strcmp(data->layers[i].name, name)==0)
1055                         return i;
1056
1057         return -1;
1058 }
1059
1060 int CustomData_get_active_layer_index(const CustomData *data, int type)
1061 {
1062         if (!data->totlayer)
1063                 return -1;
1064
1065         if (data->typemap[type] != -1) {
1066                 return data->typemap[type] + data->layers[data->typemap[type]].active;
1067         }
1068
1069         return -1;
1070 }
1071
1072 int CustomData_get_render_layer_index(const CustomData *data, int type)
1073 {
1074         int i;
1075
1076         for(i=0; i < data->totlayer; ++i)
1077                 if(data->layers[i].type == type)
1078                         return i + data->layers[i].active_rnd;
1079
1080         return -1;
1081 }
1082
1083 int CustomData_get_clone_layer_index(const CustomData *data, int type)
1084 {
1085         int i;
1086
1087         for(i=0; i < data->totlayer; ++i)
1088                 if(data->layers[i].type == type)
1089                         return i + data->layers[i].active_clone;
1090
1091         return -1;
1092 }
1093
1094 int CustomData_get_mask_layer_index(const CustomData *data, int type)
1095 {
1096         int i;
1097
1098         for(i=0; i < data->totlayer; ++i)
1099                 if(data->layers[i].type == type)
1100                         return i + data->layers[i].active_mask;
1101
1102         return -1;
1103 }
1104
1105 int CustomData_get_active_layer(const CustomData *data, int type)
1106 {
1107         int i;
1108
1109         for(i=0; i < data->totlayer; ++i)
1110                 if(data->layers[i].type == type)
1111                         return data->layers[i].active;
1112
1113         return -1;
1114 }
1115
1116 int CustomData_get_render_layer(const CustomData *data, int type)
1117 {
1118         int i;
1119
1120         for(i=0; i < data->totlayer; ++i)
1121                 if(data->layers[i].type == type)
1122                         return data->layers[i].active_rnd;
1123
1124         return -1;
1125 }
1126
1127 int CustomData_get_clone_layer(const CustomData *data, int type)
1128 {
1129         int i;
1130
1131         for(i=0; i < data->totlayer; ++i)
1132                 if(data->layers[i].type == type)
1133                         return data->layers[i].active_clone;
1134
1135         return -1;
1136 }
1137
1138 int CustomData_get_mask_layer(const CustomData *data, int type)
1139 {
1140         int i;
1141
1142         for(i=0; i < data->totlayer; ++i)
1143                 if(data->layers[i].type == type)
1144                         return data->layers[i].active_mask;
1145
1146         return -1;
1147 }
1148
1149 void CustomData_set_layer_active(CustomData *data, int type, int n)
1150 {
1151         int i;
1152
1153         for(i=0; i < data->totlayer; ++i)
1154                 if(data->layers[i].type == type)
1155                         data->layers[i].active = n;
1156 }
1157
1158 void CustomData_set_layer_render(CustomData *data, int type, int n)
1159 {
1160         int i;
1161
1162         for(i=0; i < data->totlayer; ++i)
1163                 if(data->layers[i].type == type)
1164                         data->layers[i].active_rnd = n;
1165 }
1166
1167 void CustomData_set_layer_clone(CustomData *data, int type, int n)
1168 {
1169         int i;
1170
1171         for(i=0; i < data->totlayer; ++i)
1172                 if(data->layers[i].type == type)
1173                         data->layers[i].active_clone = n;
1174 }
1175
1176 void CustomData_set_layer_mask(CustomData *data, int type, int n)
1177 {
1178         int i;
1179
1180         for(i=0; i < data->totlayer; ++i)
1181                 if(data->layers[i].type == type)
1182                         data->layers[i].active_mask = n;
1183 }
1184
1185 /* for using with an index from CustomData_get_active_layer_index and CustomData_get_render_layer_index */
1186 void CustomData_set_layer_active_index(CustomData *data, int type, int n)
1187 {
1188         int i;
1189
1190         for(i=0; i < data->totlayer; ++i)
1191                 if(data->layers[i].type == type)
1192                         data->layers[i].active = n-i;
1193 }
1194
1195 void CustomData_set_layer_render_index(CustomData *data, int type, int n)
1196 {
1197         int i;
1198
1199         for(i=0; i < data->totlayer; ++i)
1200                 if(data->layers[i].type == type)
1201                         data->layers[i].active_rnd = n-i;
1202 }
1203
1204 void CustomData_set_layer_clone_index(CustomData *data, int type, int n)
1205 {
1206         int i;
1207
1208         for(i=0; i < data->totlayer; ++i)
1209                 if(data->layers[i].type == type)
1210                         data->layers[i].active_clone = n-i;
1211 }
1212
1213 void CustomData_set_layer_mask_index(CustomData *data, int type, int n)
1214 {
1215         int i;
1216
1217         for(i=0; i < data->totlayer; ++i)
1218                 if(data->layers[i].type == type)
1219                         data->layers[i].active_mask = n-i;
1220 }
1221
1222 void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
1223 {
1224         int i;
1225
1226         for(i=0; i < data->totlayer; ++i)
1227                 if(data->layers[i].type == type)
1228                         data->layers[i].flag |= flag;
1229 }
1230
1231 static int customData_resize(CustomData *data, int amount)
1232 {
1233         CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
1234                                        "CustomData->layers");
1235         if(!tmp) return 0;
1236
1237         data->maxlayer += amount;
1238         if (data->layers) {
1239                 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
1240                 MEM_freeN(data->layers);
1241         }
1242         data->layers = tmp;
1243
1244         return 1;
1245 }
1246
1247 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1248         int type, int alloctype, void *layerdata, int totelem, const char *name)
1249 {
1250         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1251         int size = typeInfo->size * totelem, flag = 0, index = data->totlayer;
1252         void *newlayerdata;
1253
1254         if (!typeInfo->defaultname && CustomData_has_layer(data, type))
1255                 return &data->layers[CustomData_get_layer_index(data, type)];
1256
1257         if((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
1258                 newlayerdata = layerdata;
1259         }
1260         else {
1261                 newlayerdata = MEM_callocN(size, layerType_getName(type));
1262                 if(!newlayerdata)
1263                         return NULL;
1264         }
1265
1266         if (alloctype == CD_DUPLICATE) {
1267                 if(typeInfo->copy)
1268                         typeInfo->copy(layerdata, newlayerdata, totelem);
1269                 else
1270                         memcpy(newlayerdata, layerdata, size);
1271         }
1272         else if (alloctype == CD_DEFAULT) {
1273                 if(typeInfo->set_default)
1274                         typeInfo->set_default((char*)newlayerdata, totelem);
1275         }
1276         else if (alloctype == CD_REFERENCE)
1277                 flag |= CD_FLAG_NOFREE;
1278
1279         if(index >= data->maxlayer) {
1280                 if(!customData_resize(data, CUSTOMDATA_GROW)) {
1281                         if(newlayerdata != layerdata)
1282                                 MEM_freeN(newlayerdata);
1283                         return NULL;
1284                 }
1285         }
1286         
1287         data->totlayer++;
1288
1289         /* keep layers ordered by type */
1290         for( ; index > 0 && data->layers[index - 1].type > type; --index)
1291                 data->layers[index] = data->layers[index - 1];
1292
1293         data->layers[index].type = type;
1294         data->layers[index].flag = flag;
1295         data->layers[index].data = newlayerdata;
1296
1297         if(name) {
1298                 strcpy(data->layers[index].name, name);
1299                 CustomData_set_layer_unique_name(data, index);
1300         }
1301         else
1302                 data->layers[index].name[0] = '\0';
1303
1304         if(index > 0 && data->layers[index-1].type == type) {
1305                 data->layers[index].active = data->layers[index-1].active;
1306                 data->layers[index].active_rnd = data->layers[index-1].active_rnd;
1307                 data->layers[index].active_clone = data->layers[index-1].active_clone;
1308                 data->layers[index].active_mask = data->layers[index-1].active_mask;
1309         } else {
1310                 data->layers[index].active = 0;
1311                 data->layers[index].active_rnd = 0;
1312                 data->layers[index].active_clone = 0;
1313                 data->layers[index].active_mask = 0;
1314         }
1315         
1316         customData_update_offsets(data);
1317
1318         return &data->layers[index];
1319 }
1320
1321 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
1322                            void *layerdata, int totelem)
1323 {
1324         CustomDataLayer *layer;
1325         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1326         
1327         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1328                                                totelem, typeInfo->defaultname);
1329         customData_update_typemap(data);
1330
1331         if(layer)
1332                 return layer->data;
1333
1334         return NULL;
1335 }
1336
1337 /*same as above but accepts a name*/
1338 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
1339                            void *layerdata, int totelem, char *name)
1340 {
1341         CustomDataLayer *layer;
1342         
1343         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1344                                                totelem, name);
1345         customData_update_typemap(data);
1346
1347         if(layer)
1348                 return layer->data;
1349
1350         return NULL;
1351 }
1352
1353
1354 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
1355 {
1356         int i;
1357         
1358         if (index < 0) return 0;
1359
1360         customData_free_layer__internal(&data->layers[index], totelem);
1361
1362         for (i=index+1; i < data->totlayer; ++i)
1363                 data->layers[i-1] = data->layers[i];
1364
1365         data->totlayer--;
1366
1367         /* if layer was last of type in array, set new active layer */
1368         if ((index >= data->totlayer) || (data->layers[index].type != type)) {
1369                 i = CustomData_get_layer_index(data, type);
1370                 
1371                 if (i >= 0)
1372                         for (; i < data->totlayer && data->layers[i].type == type; i++) {
1373                                 data->layers[i].active--;
1374                                 data->layers[i].active_rnd--;
1375                                 data->layers[i].active_clone--;
1376                                 data->layers[i].active_mask--;
1377                         }
1378         }
1379
1380         if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1381                 customData_resize(data, -CUSTOMDATA_GROW);
1382
1383         customData_update_offsets(data);
1384         customData_update_typemap(data);
1385
1386         return 1;
1387 }
1388
1389 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
1390 {
1391         int index = 0;
1392         index = CustomData_get_active_layer_index(data, type);
1393         if (index < 0) return 0;
1394         return CustomData_free_layer(data, type, totelem, index);
1395 }
1396
1397
1398 void CustomData_free_layers(CustomData *data, int type, int totelem)
1399 {
1400         while (CustomData_has_layer(data, type))
1401                 CustomData_free_layer_active(data, type, totelem);
1402 }
1403
1404 int CustomData_has_layer(const CustomData *data, int type)
1405 {
1406         return (CustomData_get_layer_index(data, type) != -1);
1407 }
1408
1409 int CustomData_number_of_layers(const CustomData *data, int type)
1410 {
1411         int i, number = 0;
1412
1413         for(i = 0; i < data->totlayer; i++)
1414                 if(data->layers[i].type == type)
1415                         number++;
1416         
1417         return number;
1418 }
1419
1420 void *CustomData_duplicate_referenced_layer(struct CustomData *data, int type)
1421 {
1422         CustomDataLayer *layer;
1423         int layer_index;
1424
1425         /* get the layer index of the first layer of type */
1426         layer_index = CustomData_get_active_layer_index(data, type);
1427         if(layer_index < 0) return NULL;
1428
1429         layer = &data->layers[layer_index];
1430
1431         if (layer->flag & CD_FLAG_NOFREE) {
1432                 layer->data = MEM_dupallocN(layer->data);
1433                 layer->flag &= ~CD_FLAG_NOFREE;
1434         }
1435
1436         return layer->data;
1437 }
1438
1439 void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
1440                                                   int type, char *name)
1441 {
1442         CustomDataLayer *layer;
1443         int layer_index;
1444
1445         /* get the layer index of the desired layer */
1446         layer_index = CustomData_get_named_layer_index(data, type, name);
1447         if(layer_index < 0) return NULL;
1448
1449         layer = &data->layers[layer_index];
1450
1451         if (layer->flag & CD_FLAG_NOFREE) {
1452                 layer->data = MEM_dupallocN(layer->data);
1453                 layer->flag &= ~CD_FLAG_NOFREE;
1454         }
1455
1456         return layer->data;
1457 }
1458
1459 void CustomData_free_temporary(CustomData *data, int totelem)
1460 {
1461         CustomDataLayer *layer;
1462         int i, j;
1463
1464         for(i = 0, j = 0; i < data->totlayer; ++i) {
1465                 layer = &data->layers[i];
1466
1467                 if (i != j)
1468                         data->layers[j] = data->layers[i];
1469
1470                 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
1471                         customData_free_layer__internal(layer, totelem);
1472                 else
1473                         j++;
1474         }
1475
1476         data->totlayer = j;
1477
1478         if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1479                 customData_resize(data, -CUSTOMDATA_GROW);
1480
1481         customData_update_offsets(data);
1482 }
1483
1484 void CustomData_set_only_copy(const struct CustomData *data,
1485                               CustomDataMask mask)
1486 {
1487         int i;
1488
1489         for(i = 0; i < data->totlayer; ++i)
1490                 if(!((int)mask & (int)(1 << (int)data->layers[i].type)))
1491                         data->layers[i].flag |= CD_FLAG_NOCOPY;
1492 }
1493
1494 void CustomData_copy_elements(int type, void *source, void *dest, int count)
1495 {
1496         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1497
1498         if (typeInfo->copy)
1499                 typeInfo->copy(source, dest, count);
1500         else
1501                 memcpy(dest, source, typeInfo->size*count);
1502 }
1503
1504 void CustomData_copy_data(const CustomData *source, CustomData *dest,
1505                           int source_index, int dest_index, int count)
1506 {
1507         const LayerTypeInfo *typeInfo;
1508         int src_i, dest_i;
1509         int src_offset;
1510         int dest_offset;
1511
1512         /* copies a layer at a time */
1513         dest_i = 0;
1514         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1515
1516                 /* find the first dest layer with type >= the source type
1517                  * (this should work because layers are ordered by type)
1518                  */
1519                 while(dest_i < dest->totlayer
1520                       && dest->layers[dest_i].type < source->layers[src_i].type)
1521                         ++dest_i;
1522
1523                 /* if there are no more dest layers, we're done */
1524                 if(dest_i >= dest->totlayer) return;
1525
1526                 /* if we found a matching layer, copy the data */
1527                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1528                         char *src_data = source->layers[src_i].data;
1529                         char *dest_data = dest->layers[dest_i].data;
1530
1531                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1532
1533                         src_offset = source_index * typeInfo->size;
1534                         dest_offset = dest_index * typeInfo->size;
1535
1536                         if(typeInfo->copy)
1537                                 typeInfo->copy(src_data + src_offset,
1538                                                 dest_data + dest_offset,
1539                                                 count);
1540                         else
1541                                 memcpy(dest_data + dest_offset,
1542                                        src_data + src_offset,
1543                                        count * typeInfo->size);
1544
1545                         /* if there are multiple source & dest layers of the same type,
1546                          * we don't want to copy all source layers to the same dest, so
1547                          * increment dest_i
1548                          */
1549                         ++dest_i;
1550                 }
1551         }
1552 }
1553
1554 void CustomData_free_elem(CustomData *data, int index, int count)
1555 {
1556         int i;
1557         const LayerTypeInfo *typeInfo;
1558
1559         for(i = 0; i < data->totlayer; ++i) {
1560                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1561                         typeInfo = layerType_getInfo(data->layers[i].type);
1562
1563                         if(typeInfo->free) {
1564                                 int offset = typeInfo->size * index;
1565
1566                                 typeInfo->free((char *)data->layers[i].data + offset,
1567                                                count, typeInfo->size);
1568                         }
1569                 }
1570         }
1571 }
1572
1573 #define SOURCE_BUF_SIZE 100
1574
1575 void CustomData_interp(const CustomData *source, CustomData *dest,
1576                        int *src_indices, float *weights, float *sub_weights,
1577                        int count, int dest_index)
1578 {
1579         int src_i, dest_i;
1580         int dest_offset;
1581         int j;
1582         void *source_buf[SOURCE_BUF_SIZE];
1583         void **sources = source_buf;
1584
1585         /* slow fallback in case we're interpolating a ridiculous number of
1586          * elements
1587          */
1588         if(count > SOURCE_BUF_SIZE)
1589                 sources = MEM_callocN(sizeof(*sources) * count,
1590                                       "CustomData_interp sources");
1591
1592         /* interpolates a layer at a time */
1593         dest_i = 0;
1594         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1595                 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
1596                 if(!typeInfo->interp) continue;
1597
1598                 /* find the first dest layer with type >= the source type
1599                  * (this should work because layers are ordered by type)
1600                  */
1601                 while(dest_i < dest->totlayer
1602                       && dest->layers[dest_i].type < source->layers[src_i].type)
1603                         ++dest_i;
1604
1605                 /* if there are no more dest layers, we're done */
1606                 if(dest_i >= dest->totlayer) return;
1607
1608                 /* if we found a matching layer, copy the data */
1609                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1610                         void *src_data = source->layers[src_i].data;
1611
1612                         for(j = 0; j < count; ++j)
1613                                 sources[j] = (char *)src_data
1614                                                          + typeInfo->size * src_indices[j];
1615
1616                         dest_offset = dest_index * typeInfo->size;
1617
1618                         typeInfo->interp(sources, weights, sub_weights, count,
1619                                                    (char *)dest->layers[dest_i].data + dest_offset);
1620
1621                         /* if there are multiple source & dest layers of the same type,
1622                          * we don't want to copy all source layers to the same dest, so
1623                          * increment dest_i
1624                          */
1625                         ++dest_i;
1626                 }
1627         }
1628
1629         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1630 }
1631
1632 void CustomData_swap(struct CustomData *data, int index, int *corner_indices)
1633 {
1634         const LayerTypeInfo *typeInfo;
1635         int i;
1636
1637         for(i = 0; i < data->totlayer; ++i) {
1638                 typeInfo = layerType_getInfo(data->layers[i].type);
1639
1640                 if(typeInfo->swap) {
1641                         int offset = typeInfo->size * index;
1642
1643                         typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
1644                 }
1645         }
1646 }
1647
1648 void *CustomData_get(const CustomData *data, int index, int type)
1649 {
1650         int offset;
1651         int layer_index;
1652         
1653         /* get the layer index of the active layer of type */
1654         layer_index = CustomData_get_active_layer_index(data, type);
1655         if(layer_index < 0) return NULL;
1656
1657         /* get the offset of the desired element */
1658         offset = layerType_getInfo(type)->size * index;
1659
1660         return (char *)data->layers[layer_index].data + offset;
1661 }
1662
1663 void *CustomData_get_n(const CustomData *data, int type, int index, int n)
1664 {
1665         int layer_index;
1666         int offset;
1667
1668         /* get the layer index of the first layer of type */
1669         layer_index = data->typemap[type];
1670         if(layer_index < 0) return NULL;
1671         
1672         offset = layerType_getInfo(type)->size * index;
1673         return (char *)data->layers[layer_index+n].data + offset;
1674 }
1675
1676 void *CustomData_get_layer(const CustomData *data, int type)
1677 {
1678         /* get the layer index of the active layer of type */
1679         int layer_index = CustomData_get_active_layer_index(data, type);
1680         if(layer_index < 0) return NULL;
1681
1682         return data->layers[layer_index].data;
1683 }
1684
1685 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
1686 {
1687         /* get the layer index of the active layer of type */
1688         int layer_index = CustomData_get_layer_index(data, type);
1689         if(layer_index < 0) return NULL;
1690
1691         return data->layers[layer_index+n].data;
1692 }
1693
1694 void *CustomData_get_layer_named(const struct CustomData *data, int type,
1695                                  char *name)
1696 {
1697         int layer_index = CustomData_get_named_layer_index(data, type, name);
1698         if(layer_index < 0) return NULL;
1699
1700         return data->layers[layer_index].data;
1701 }
1702
1703 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
1704 {
1705         /* get the layer index of the first layer of type */
1706         int layer_index = CustomData_get_active_layer_index(data, type);
1707
1708         if(layer_index < 0) return NULL;
1709
1710         data->layers[layer_index].data = ptr;
1711
1712         return ptr;
1713 }
1714
1715 void *CustomData_set_layer_n(const struct CustomData *data, int type, int n, void *ptr)
1716 {
1717         /* get the layer index of the first layer of type */
1718         int layer_index = CustomData_get_layer_index(data, type);
1719         if(layer_index < 0) return NULL;
1720
1721         data->layers[layer_index+n].data = ptr;
1722
1723         return ptr;
1724 }
1725
1726 void CustomData_set(const CustomData *data, int index, int type, void *source)
1727 {
1728         void *dest = CustomData_get(data, index, type);
1729         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1730
1731         if(!dest) return;
1732
1733         if(typeInfo->copy)
1734                 typeInfo->copy(source, dest, 1);
1735         else
1736                 memcpy(dest, source, typeInfo->size);
1737 }
1738
1739 /* EditMesh functions */
1740
1741 void CustomData_em_free_block(CustomData *data, void **block)
1742 {
1743     const LayerTypeInfo *typeInfo;
1744     int i;
1745
1746         if(!*block) return;
1747
1748     for(i = 0; i < data->totlayer; ++i) {
1749         if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1750             typeInfo = layerType_getInfo(data->layers[i].type);
1751
1752             if(typeInfo->free) {
1753                                 int offset = data->layers[i].offset;
1754                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
1755                         }
1756         }
1757     }
1758
1759         MEM_freeN(*block);
1760         *block = NULL;
1761 }
1762
1763 static void CustomData_em_alloc_block(CustomData *data, void **block)
1764 {
1765         /* TODO: optimize free/alloc */
1766
1767         if (*block)
1768                 CustomData_em_free_block(data, block);
1769
1770         if (data->totsize > 0)
1771                 *block = MEM_callocN(data->totsize, "CustomData EM block");
1772         else
1773                 *block = NULL;
1774 }
1775
1776 void CustomData_em_copy_data(const CustomData *source, CustomData *dest,
1777                             void *src_block, void **dest_block)
1778 {
1779         const LayerTypeInfo *typeInfo;
1780         int dest_i, src_i;
1781
1782         if (!*dest_block)
1783                 CustomData_em_alloc_block(dest, dest_block);
1784         
1785         /* copies a layer at a time */
1786         dest_i = 0;
1787         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1788
1789                 /* find the first dest layer with type >= the source type
1790                  * (this should work because layers are ordered by type)
1791                  */
1792                 while(dest_i < dest->totlayer
1793                       && dest->layers[dest_i].type < source->layers[src_i].type)
1794                         ++dest_i;
1795
1796                 /* if there are no more dest layers, we're done */
1797                 if(dest_i >= dest->totlayer) return;
1798
1799                 /* if we found a matching layer, copy the data */
1800                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
1801                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
1802                         char *src_data = (char*)src_block + source->layers[src_i].offset;
1803                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
1804
1805                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1806
1807                         if(typeInfo->copy)
1808                                 typeInfo->copy(src_data, dest_data, 1);
1809                         else
1810                                 memcpy(dest_data, src_data, typeInfo->size);
1811
1812                         /* if there are multiple source & dest layers of the same type,
1813                          * we don't want to copy all source layers to the same dest, so
1814                          * increment dest_i
1815                          */
1816                         ++dest_i;
1817                 }
1818         }
1819 }
1820
1821 void *CustomData_em_get(const CustomData *data, void *block, int type)
1822 {
1823         int layer_index;
1824         
1825         /* get the layer index of the first layer of type */
1826         layer_index = CustomData_get_active_layer_index(data, type);
1827         if(layer_index < 0) return NULL;
1828
1829         return (char *)block + data->layers[layer_index].offset;
1830 }
1831
1832 void *CustomData_em_get_n(const CustomData *data, void *block, int type, int n)
1833 {
1834         int layer_index;
1835         
1836         /* get the layer index of the first layer of type */
1837         layer_index = CustomData_get_layer_index(data, type);
1838         if(layer_index < 0) return NULL;
1839
1840         return (char *)block + data->layers[layer_index+n].offset;
1841 }
1842
1843 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
1844 {
1845         void *dest = CustomData_em_get(data, block, type);
1846         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1847
1848         if(!dest) return;
1849
1850         if(typeInfo->copy)
1851                 typeInfo->copy(source, dest, 1);
1852         else
1853                 memcpy(dest, source, typeInfo->size);
1854 }
1855
1856 void CustomData_em_set_n(CustomData *data, void *block, int type, int n, void *source)
1857 {
1858         void *dest = CustomData_em_get_n(data, block, type, n);
1859         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1860
1861         if(!dest) return;
1862
1863         if(typeInfo->copy)
1864                 typeInfo->copy(source, dest, 1);
1865         else
1866                 memcpy(dest, source, typeInfo->size);
1867 }
1868
1869 void CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
1870                           float *sub_weights, int count, void *dest_block)
1871 {
1872         int i, j;
1873         void *source_buf[SOURCE_BUF_SIZE];
1874         void **sources = source_buf;
1875
1876         /* slow fallback in case we're interpolating a ridiculous number of
1877          * elements
1878          */
1879         if(count > SOURCE_BUF_SIZE)
1880                 sources = MEM_callocN(sizeof(*sources) * count,
1881                                       "CustomData_interp sources");
1882
1883         /* interpolates a layer at a time */
1884         for(i = 0; i < data->totlayer; ++i) {
1885                 CustomDataLayer *layer = &data->layers[i];
1886                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1887
1888                 if(typeInfo->interp) {
1889                         for(j = 0; j < count; ++j)
1890                                 sources[j] = (char *)src_blocks[j] + layer->offset;
1891
1892                         typeInfo->interp(sources, weights, sub_weights, count,
1893                                           (char *)dest_block + layer->offset);
1894                 }
1895         }
1896
1897         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1898 }
1899
1900 void CustomData_em_set_default(CustomData *data, void **block)
1901 {
1902         const LayerTypeInfo *typeInfo;
1903         int i;
1904
1905         if (!*block)
1906                 CustomData_em_alloc_block(data, block);
1907
1908         for(i = 0; i < data->totlayer; ++i) {
1909                 int offset = data->layers[i].offset;
1910
1911                 typeInfo = layerType_getInfo(data->layers[i].type);
1912
1913                 if(typeInfo->set_default)
1914                         typeInfo->set_default((char*)*block + offset, 1);
1915         }
1916 }
1917
1918 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
1919                             int src_index, void **dest_block)
1920 {
1921         const LayerTypeInfo *typeInfo;
1922         int dest_i, src_i, src_offset;
1923
1924         if (!*dest_block)
1925                 CustomData_em_alloc_block(dest, dest_block);
1926         
1927         /* copies a layer at a time */
1928         dest_i = 0;
1929         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1930
1931                 /* find the first dest layer with type >= the source type
1932                  * (this should work because layers are ordered by type)
1933                  */
1934                 while(dest_i < dest->totlayer
1935                       && dest->layers[dest_i].type < source->layers[src_i].type)
1936                         ++dest_i;
1937
1938                 /* if there are no more dest layers, we're done */
1939                 if(dest_i >= dest->totlayer) return;
1940
1941                 /* if we found a matching layer, copy the data */
1942                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1943                         int offset = dest->layers[dest_i].offset;
1944                         char *src_data = source->layers[src_i].data;
1945                         char *dest_data = (char*)*dest_block + offset;
1946
1947                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1948                         src_offset = src_index * typeInfo->size;
1949
1950                         if(typeInfo->copy)
1951                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
1952                         else
1953                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
1954
1955                         /* if there are multiple source & dest layers of the same type,
1956                          * we don't want to copy all source layers to the same dest, so
1957                          * increment dest_i
1958                          */
1959                         ++dest_i;
1960                 }
1961         }
1962 }
1963
1964 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
1965                               void *src_block, int dest_index)
1966 {
1967         const LayerTypeInfo *typeInfo;
1968         int dest_i, src_i, dest_offset;
1969
1970         /* copies a layer at a time */
1971         dest_i = 0;
1972         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1973
1974                 /* find the first dest layer with type >= the source type
1975                  * (this should work because layers are ordered by type)
1976                  */
1977                 while(dest_i < dest->totlayer
1978                       && dest->layers[dest_i].type < source->layers[src_i].type)
1979                         ++dest_i;
1980
1981                 /* if there are no more dest layers, we're done */
1982                 if(dest_i >= dest->totlayer) return;
1983
1984                 /* if we found a matching layer, copy the data */
1985                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1986                         int offset = source->layers[src_i].offset;
1987                         char *src_data = (char*)src_block + offset;
1988                         char *dest_data = dest->layers[dest_i].data;
1989
1990                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1991                         dest_offset = dest_index * typeInfo->size;
1992
1993                         if(typeInfo->copy)
1994                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
1995                         else
1996                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
1997
1998                         /* if there are multiple source & dest layers of the same type,
1999                          * we don't want to copy all source layers to the same dest, so
2000                          * increment dest_i
2001                          */
2002                         ++dest_i;
2003                 }
2004         }
2005
2006 }
2007
2008 /*Bmesh functions*/
2009 /*needed to convert to/from different face reps*/
2010 void CustomData_to_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata,
2011                              int totloop, int totpoly)
2012 {
2013         int i;
2014         for(i=0; i < fdata->totlayer; i++){
2015                 if(fdata->layers[i].type == CD_MTFACE){
2016                         CustomData_add_layer(pdata, CD_MTEXPOLY, CD_CALLOC, &(fdata->layers[i].name), totpoly);
2017                         CustomData_add_layer(ldata, CD_MLOOPUV, CD_CALLOC, &(fdata->layers[i].name), totloop);
2018                 }
2019                 else if(fdata->layers[i].type == CD_MCOL)
2020                         CustomData_add_layer(ldata, CD_MLOOPCOL, CD_CALLOC, &(fdata->layers[i].name), totloop);
2021         }
2022 }
2023 void CustomData_from_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata, int total){
2024         int i;
2025         for(i=0; i < pdata->totlayer; i++){
2026                 if(pdata->layers[i].type == CD_MTEXPOLY)
2027                         CustomData_add_layer(fdata, CD_MTFACE, CD_CALLOC, &(pdata->layers[i].name), total);
2028         }
2029         for(i=0; i < ldata->totlayer; i++){
2030                 if(ldata->layers[i].type == CD_MLOOPCOL)
2031                         CustomData_add_layer(fdata, CD_MCOL, CD_CALLOC, &(ldata->layers[i].name), total);
2032                 if (ldata->layers[i].type == CD_WEIGHT_MLOOPCOL)
2033                         CustomData_add_layer(fdata, CD_WEIGHT_MCOL, CD_CALLOC, &(ldata->layers[i].name), total);
2034         }
2035 }
2036
2037
2038 void CustomData_bmesh_init_pool(CustomData *data, int allocsize){
2039         if(data->totlayer)data->pool = BLI_mempool_create(data->totsize, allocsize, allocsize);
2040 }
2041
2042 void CustomData_bmesh_merge(CustomData *source, CustomData *dest, 
2043                             int mask, int alloctype, BMesh *bm, int type)
2044 {
2045         BMHeader *h;
2046         BMIter iter;
2047         CustomData destold = *dest;
2048         void *tmp;
2049         int t;
2050         
2051         CustomData_merge(source, dest, mask, alloctype, 0);
2052         CustomData_bmesh_init_pool(dest, 512);
2053
2054         switch (type) {
2055                 case BM_VERT:
2056                         t = BM_VERTS_OF_MESH; break;
2057                 case BM_EDGE:
2058                         t = BM_EDGES_OF_MESH; break;
2059                 case BM_LOOP:
2060                         t = BM_LOOPS_OF_FACE; break;
2061                 case BM_FACE:
2062                         t = BM_FACES_OF_MESH; break;
2063         }
2064
2065         if (t != BM_LOOPS_OF_FACE) {
2066                 /*ensure all current elements follow new customdata layout*/
2067                 BM_ITER(h, &iter, bm, t, NULL) {
2068                         CustomData_bmesh_copy_data(&destold, dest, h->data, &tmp);
2069                         CustomData_bmesh_free_block(&destold, &h->data);
2070                         h->data = tmp;
2071                 }
2072         } else {
2073                 BMFace *f;
2074                 BMLoop *l;
2075                 BMIter liter;
2076
2077                 /*ensure all current elements follow new customdata layout*/
2078                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
2079                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
2080                                 CustomData_bmesh_copy_data(&destold, dest, l->head.data, &tmp);
2081                                 CustomData_bmesh_free_block(&destold, &l->head.data);
2082                                 l->head.data = tmp;
2083                         }
2084                 }
2085         }
2086
2087         if (destold.pool) BLI_mempool_destroy(destold.pool);
2088 }
2089
2090 void CustomData_bmesh_free_block(CustomData *data, void **block)
2091 {
2092     const LayerTypeInfo *typeInfo;
2093     int i;
2094
2095         if(!*block) return;
2096     for(i = 0; i < data->totlayer; ++i) {
2097         if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
2098             typeInfo = layerType_getInfo(data->layers[i].type);
2099
2100             if(typeInfo->free) {
2101                                 int offset = data->layers[i].offset;
2102                                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
2103                         }
2104         }
2105     }
2106
2107         BLI_mempool_free(data->pool, *block);
2108         *block = NULL;
2109 }
2110
2111 static void CustomData_bmesh_alloc_block(CustomData *data, void **block)
2112 {
2113
2114         if (*block)
2115                 CustomData_bmesh_free_block(data, block);
2116
2117         if (data->totsize > 0)
2118                 *block = BLI_mempool_calloc(data->pool);
2119         else
2120                 *block = NULL;
2121 }
2122
2123 void CustomData_bmesh_copy_data(const CustomData *source, CustomData *dest,
2124                             void *src_block, void **dest_block)
2125 {
2126         const LayerTypeInfo *typeInfo;
2127         int dest_i, src_i;
2128
2129         if (!*dest_block)
2130                 CustomData_bmesh_alloc_block(dest, dest_block);
2131         
2132         /* copies a layer at a time */
2133         dest_i = 0;
2134         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2135
2136                 /* find the first dest layer with type >= the source type
2137                  * (this should work because layers are ordered by type)
2138                  */
2139                 while(dest_i < dest->totlayer
2140                       && dest->layers[dest_i].type < source->layers[src_i].type)
2141                         ++dest_i;
2142
2143                 /* if there are no more dest layers, we're done */
2144                 if(dest_i >= dest->totlayer) return;
2145
2146                 /* if we found a matching layer, copy the data */
2147                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
2148                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
2149                         char *src_data = (char*)src_block + source->layers[src_i].offset;
2150                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
2151
2152                         typeInfo = layerType_getInfo(source->layers[src_i].type);
2153
2154                         if(typeInfo->copy)
2155                                 typeInfo->copy(src_data, dest_data, 1);
2156                         else
2157                                 memcpy(dest_data, src_data, typeInfo->size);
2158
2159                         /* if there are multiple source & dest layers of the same type,
2160                          * we don't want to copy all source layers to the same dest, so
2161                          * increment dest_i
2162                          */
2163                         ++dest_i;
2164                 }
2165         }
2166 }
2167
2168 /*Bmesh Custom Data Functions. Should replace editmesh ones with these as well, due to more effecient memory alloc*/
2169 void *CustomData_bmesh_get(const CustomData *data, void *block, int type)
2170 {
2171         int layer_index;
2172         
2173         /* get the layer index of the first layer of type */
2174         layer_index = CustomData_get_active_layer_index(data, type);
2175         if(layer_index < 0) return NULL;
2176
2177         return (char *)block + data->layers[layer_index].offset;
2178 }
2179
2180 void *CustomData_bmesh_get_n(const CustomData *data, void *block, int type, int n)
2181 {
2182         int layer_index;
2183         
2184         /* get the layer index of the first layer of type */
2185         layer_index = CustomData_get_layer_index(data, type);
2186         if(layer_index < 0) return NULL;
2187
2188         return (char *)block + data->layers[layer_index+n].offset;
2189 }
2190
2191 /*gets from the layer at physical index n, note: doesn't check type.*/
2192 void *CustomData_bmesh_get_layer_n(const CustomData *data, void *block, int n)
2193 {
2194         if(n < 0 || n >= data->totlayer) return NULL;
2195
2196         return (char *)block + data->layers[n].offset;
2197 }
2198
2199 int CustomData_layer_has_math(struct CustomData *data, int layern)
2200 {
2201         const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[layern].type);
2202         
2203         if (typeInfo->equal && typeInfo->add && typeInfo->multiply && 
2204             typeInfo->initminmax && typeInfo->dominmax) return 1;
2205         
2206         return 0;
2207 }
2208
2209 /*copies the "value" (e.g. mloopuv uv or mloopcol colors) from one block to
2210   another, while not overwriting anything else (e.g. flags)*/
2211 void CustomData_data_copy_value(int type, void *source, void *dest)
2212 {
2213         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2214
2215         if(!dest) return;
2216
2217         if(typeInfo->copyvalue)
2218                 typeInfo->copyvalue(source, dest);
2219         else
2220                 memcpy(dest, source, typeInfo->size);
2221 }
2222
2223 int CustomData_data_equals(int type, void *data1, void *data2)
2224 {
2225         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2226
2227         if (typeInfo->equal)
2228                 return typeInfo->equal(data1, data2);
2229         else return !memcmp(data1, data2, typeInfo->size);
2230 }
2231
2232 void CustomData_data_initminmax(int type, void *min, void *max)
2233 {
2234         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2235
2236         if (typeInfo->initminmax)
2237                 typeInfo->initminmax(min, max);
2238 }
2239
2240
2241 void CustomData_data_dominmax(int type, void *data, void *min, void *max)
2242 {
2243         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2244
2245         if (typeInfo->dominmax)
2246                 typeInfo->dominmax(data, min, max);
2247 }
2248
2249
2250 void CustomData_data_multiply(int type, void *data, float fac)
2251 {
2252         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2253
2254         if (typeInfo->multiply)
2255                 typeInfo->multiply(data, fac);
2256 }
2257
2258
2259 void CustomData_data_add(int type, void *data1, void *data2)
2260 {
2261         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2262
2263         if (typeInfo->add)
2264                 typeInfo->add(data1, data2);
2265 }
2266
2267 void CustomData_bmesh_set(const CustomData *data, void *block, int type, void *source)
2268 {
2269         void *dest = CustomData_bmesh_get(data, block, type);
2270         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2271
2272         if(!dest) return;
2273
2274         if(typeInfo->copy)
2275                 typeInfo->copy(source, dest, 1);
2276         else
2277                 memcpy(dest, source, typeInfo->size);
2278 }
2279
2280 void CustomData_bmesh_set_n(CustomData *data, void *block, int type, int n, void *source)
2281 {
2282         void *dest = CustomData_bmesh_get_n(data, block, type, n);
2283         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2284
2285         if(!dest) return;
2286
2287         if(typeInfo->copy)
2288                 typeInfo->copy(source, dest, 1);
2289         else
2290                 memcpy(dest, source, typeInfo->size);
2291 }
2292
2293 void CustomData_bmesh_set_layer_n(CustomData *data, void *block, int n, void *source)
2294 {
2295         void *dest = CustomData_bmesh_get_layer_n(data, block, n);
2296         const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[n].type);
2297
2298         if(!dest) return;
2299
2300         if(typeInfo->copy)
2301                 typeInfo->copy(source, dest, 1);
2302         else
2303                 memcpy(dest, source, typeInfo->size);
2304 }
2305
2306 void CustomData_bmesh_interp(CustomData *data, void **src_blocks, float *weights,
2307                           float *sub_weights, int count, void *dest_block)
2308 {
2309         int i, j;
2310         void *source_buf[SOURCE_BUF_SIZE];
2311         void **sources = source_buf;
2312
2313         /* slow fallback in case we're interpolating a ridiculous number of
2314          * elements
2315          */
2316         if(count > SOURCE_BUF_SIZE)
2317                 sources = MEM_callocN(sizeof(*sources) * count,
2318                                       "CustomData_interp sources");
2319
2320         /* interpolates a layer at a time */
2321         for(i = 0; i < data->totlayer; ++i) {
2322                 CustomDataLayer *layer = &data->layers[i];
2323                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
2324                 if(typeInfo->interp) {
2325                         for(j = 0; j < count; ++j)
2326                                 sources[j] = (char *)src_blocks[j] + layer->offset;
2327
2328                         typeInfo->interp(sources, weights, sub_weights, count,
2329                                           (char *)dest_block + layer->offset);
2330                 }
2331         }
2332
2333         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2334 }
2335
2336 void CustomData_bmesh_set_default(CustomData *data, void **block)
2337 {
2338         const LayerTypeInfo *typeInfo;
2339         int i;
2340
2341         if (!*block)
2342                 CustomData_bmesh_alloc_block(data, block);
2343
2344         for(i = 0; i < data->totlayer; ++i) {
2345                 int offset = data->layers[i].offset;
2346
2347                 typeInfo = layerType_getInfo(data->layers[i].type);
2348
2349                 if(typeInfo->set_default)
2350                         typeInfo->set_default((char*)*block + offset, 1);
2351         }
2352 }
2353
2354 void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
2355                             int src_index, void **dest_block)
2356 {
2357         const LayerTypeInfo *typeInfo;
2358         int dest_i, src_i, src_offset;
2359
2360         if (!*dest_block)
2361                 CustomData_bmesh_alloc_block(dest, dest_block);
2362         
2363         /* copies a layer at a time */
2364         dest_i = 0;
2365         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2366
2367                 /* find the first dest layer with type >= the source type
2368                  * (this should work because layers are ordered by type)
2369                  */
2370                 while(dest_i < dest->totlayer
2371                       && dest->layers[dest_i].type < source->layers[src_i].type)
2372                         ++dest_i;
2373
2374                 /* if there are no more dest layers, we're done */
2375                 if(dest_i >= dest->totlayer) return;
2376
2377                 /* if we found a matching layer, copy the data */
2378                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2379                         int offset = dest->layers[dest_i].offset;
2380                         char *src_data = source->layers[src_i].data;
2381                         char *dest_data = (char*)*dest_block + offset;
2382
2383                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2384                         src_offset = src_index * typeInfo->size;
2385
2386                         if(typeInfo->copy)
2387                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
2388                         else
2389                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2390
2391                         /* if there are multiple source & dest layers of the same type,
2392                          * we don't want to copy all source layers to the same dest, so
2393                          * increment dest_i
2394                          */
2395                         ++dest_i;
2396                 }
2397         }
2398 }
2399
2400 void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest,
2401                               void *src_block, int dest_index)
2402 {
2403         const LayerTypeInfo *typeInfo;
2404         int dest_i, src_i, dest_offset;
2405
2406         /* copies a layer at a time */
2407         dest_i = 0;
2408         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2409
2410                 /* find the first dest layer with type >= the source type
2411                  * (this should work because layers are ordered by type)
2412                  */
2413                 while(dest_i < dest->totlayer
2414                       && dest->layers[dest_i].type < source->layers[src_i].type)
2415                         ++dest_i;
2416
2417                 /* if there are no more dest layers, we're done */
2418                 if(dest_i >= dest->totlayer) return;
2419
2420                 /* if we found a matching layer, copy the data */
2421                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2422                         int offset = source->layers[src_i].offset;
2423                         char *src_data = (char*)src_block + offset;
2424                         char *dest_data = dest->layers[dest_i].data;
2425
2426                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2427                         dest_offset = dest_index * typeInfo->size;
2428
2429                         if(typeInfo->copy)
2430                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2431                         else
2432                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2433
2434                         /* if there are multiple source & dest layers of the same type,
2435                          * we don't want to copy all source layers to the same dest, so
2436                          * increment dest_i
2437                          */
2438                         ++dest_i;
2439                 }
2440         }
2441
2442 }
2443
2444 void CustomData_file_write_info(int type, char **structname, int *structnum)
2445 {
2446         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2447
2448         *structname = typeInfo->structname;
2449         *structnum = typeInfo->structnum;
2450 }
2451
2452 int CustomData_sizeof(int type)
2453 {
2454         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2455
2456         return typeInfo->size;
2457 }
2458
2459 const char *CustomData_layertype_name(int type)
2460 {
2461         return layerType_getName(type);
2462 }
2463
2464 static int  CustomData_is_property_layer(int type)
2465 {
2466         if((type == CD_PROP_FLT) || (type == CD_PROP_INT) || (type == CD_PROP_STR))
2467                 return 1;
2468         return 0;
2469 }
2470
2471 void CustomData_set_layer_unique_name(CustomData *data, int index)
2472 {
2473         char tempname[64];
2474         int number, i, type;
2475         char *dot, *name;
2476         CustomDataLayer *layer, *nlayer= &data->layers[index];
2477         const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
2478
2479         if (!typeInfo->defaultname)
2480                 return;
2481
2482         type = nlayer->type;
2483         name = nlayer->name;
2484
2485         if (name[0] == '\0')
2486                 BLI_strncpy(nlayer->name, typeInfo->defaultname, sizeof(nlayer->name));
2487         
2488         /* see if there is a duplicate */
2489         for(i=0; i<data->totlayer; i++) {
2490                 layer = &data->layers[i];
2491                 
2492                 if(CustomData_is_property_layer(type)){
2493                         if(i!=index && CustomData_is_property_layer(layer->type) && 
2494                                 strcmp(layer->name, name)==0)
2495                                         break;  
2496                 
2497                 }
2498                 else{
2499                         if(i!=index && layer->type==type && strcmp(layer->name, name)==0)
2500                                 break;
2501                 }
2502         }
2503
2504         if(i == data->totlayer)
2505                 return;
2506
2507         /* strip off the suffix */
2508         dot = strchr(nlayer->name, '.');
2509         if(dot) *dot=0;
2510         
2511         for(number=1; number <=999; number++) {
2512                 sprintf(tempname, "%s.%03d", nlayer->name, number);
2513
2514                 for(i=0; i<data->totlayer; i++) {
2515                         layer = &data->layers[i];
2516                         
2517                         if(CustomData_is_property_layer(type)){
2518                                 if(i!=index && CustomData_is_property_layer(layer->type) && 
2519                                         strcmp(layer->name, tempname)==0)
2520
2521                                 break;
2522                         }
2523                         else{
2524                                 if(i!=index && layer->type==type && strcmp(layer->name, tempname)==0)
2525                                         break;
2526                         }
2527                 }
2528
2529                 if(i == data->totlayer) {
2530                         BLI_strncpy(nlayer->name, tempname, sizeof(nlayer->name));
2531                         return;
2532                 }
2533         }       
2534 }
2535
2536 int CustomData_verify_versions(struct CustomData *data, int index)
2537 {
2538         const LayerTypeInfo *typeInfo;
2539         CustomDataLayer *layer = &data->layers[index];
2540         int i, keeplayer = 1;
2541
2542         if (layer->type >= CD_NUMTYPES) {
2543                 keeplayer = 0; /* unknown layer type from future version */
2544         }
2545         else {
2546                 typeInfo = layerType_getInfo(layer->type);
2547
2548                 if (!typeInfo->defaultname && (index > 0) &&
2549                         data->layers[index-1].type == layer->type)
2550                         keeplayer = 0; /* multiple layers of which we only support one */
2551         }
2552
2553         if (!keeplayer) {
2554             for (i=index+1; i < data->totlayer; ++i)
2555             data->layers[i-1] = data->layers[i];
2556                 data->totlayer--;
2557         }
2558
2559         return keeplayer;
2560 }
2561