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