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