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