-> Vertex Group support for bevel (editmode only)
[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
364
365
366 static void layerInterp_mcol(void **sources, float *weights,
367                              float *sub_weights, int count, void *dest)
368 {
369         MCol *mc = dest;
370         int i, j, k;
371         struct {
372                 float a;
373                 float r;
374                 float g;
375                 float b;
376         } col[4];
377         float *sub_weight;
378
379         if(count <= 0) return;
380
381         memset(col, 0, sizeof(col));
382         
383         sub_weight = sub_weights;
384         for(i = 0; i < count; ++i) {
385                 float weight = weights ? weights[i] : 1;
386
387                 for(j = 0; j < 4; ++j) {
388                         if(sub_weights) {
389                                 MCol *src = sources[i];
390                                 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
391                                         col[j].a += src->a * (*sub_weight) * weight;
392                                         col[j].r += src->r * (*sub_weight) * weight;
393                                         col[j].g += src->g * (*sub_weight) * weight;
394                                         col[j].b += src->b * (*sub_weight) * weight;
395                                 }
396                         } else {
397                                 MCol *src = sources[i];
398                                 col[j].a += src[j].a * weight;
399                                 col[j].r += src[j].r * weight;
400                                 col[j].g += src[j].g * weight;
401                                 col[j].b += src[j].b * weight;
402                         }
403                 }
404         }
405
406         for(j = 0; j < 4; ++j) {
407                 mc[j].a = (int)col[j].a;
408                 mc[j].r = (int)col[j].r;
409                 mc[j].g = (int)col[j].g;
410                 mc[j].b = (int)col[j].b;
411         }
412 }
413
414 static void layerSwap_mcol(void *data, int *corner_indices)
415 {
416         MCol *mcol = data;
417         MCol col[4];
418         int j;
419
420         for(j = 0; j < 4; ++j)
421                 col[j] = mcol[corner_indices[j]];
422
423         memcpy(mcol, col, sizeof(col));
424 }
425
426 static void layerDefault_mcol(void *data, int count)
427 {
428         static MCol default_mcol = {255, 255, 255, 255};
429         MCol *mcol = (MCol*)data;
430         int i;
431
432         for(i = 0; i < 4*count; i++)
433                 mcol[i] = default_mcol;
434 }
435
436 const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
437         {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
438         {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
439          NULL},
440         {sizeof(MDeformVert), "MDeformVert", 1, NULL, layerCopy_mdeformvert,
441          layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
442         {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL, NULL},
443         {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL, NULL},
444         {sizeof(MTFace), "MTFace", 1, "UVTex", layerCopy_tface, NULL,
445          layerInterp_tface, layerSwap_tface, layerDefault_tface},
446         /* 4 MCol structs per face */
447         {sizeof(MCol)*4, "MCol", 4, "Col", NULL, NULL, layerInterp_mcol,
448          layerSwap_mcol, layerDefault_mcol},
449         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
450         /* 3 floats per normal vector */
451         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
452         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
453         {sizeof(MFloatProperty), "MFloatProperty",1,"Float",NULL,NULL,NULL,NULL},
454         {sizeof(MIntProperty), "MIntProperty",1,"Int",NULL,NULL,NULL,NULL},
455         {sizeof(MStringProperty), "MStringProperty",1,"String",NULL,NULL,NULL,NULL},
456         {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVTex", layerCopy_origspace_face, NULL,
457          layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face},
458         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
459         {sizeof(MTexPoly), "MTexPoly", 1, "Face Texture", NULL, NULL, NULL, NULL, NULL},
460         {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, NULL, NULL, NULL},
461         {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, NULL, NULL, NULL} 
462 };
463
464 const char *LAYERTYPENAMES[CD_NUMTYPES] = {
465         "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace", "CDMTFace",
466         "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags","CDMFloatProperty",
467         "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco", "CDMTexPoly", "CDMLoopUV", "CDMloopCol"};
468
469 const CustomDataMask CD_MASK_BAREMESH =
470         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE;
471 const CustomDataMask CD_MASK_MESH =
472         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
473         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
474         CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR;
475 const CustomDataMask CD_MASK_EDITMESH =
476         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
477         CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR;
478 const CustomDataMask CD_MASK_DERIVEDMESH =
479         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
480         CD_MASK_MCOL | CD_MASK_ORIGINDEX | CD_MASK_PROP_FLT | CD_MASK_PROP_INT |
481         CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORCO;
482 const CustomDataMask CD_MASK_BMESH = 
483         CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL;
484
485 static const LayerTypeInfo *layerType_getInfo(int type)
486 {
487         if(type < 0 || type >= CD_NUMTYPES) return NULL;
488
489         return &LAYERTYPEINFO[type];
490 }
491
492 static const char *layerType_getName(int type)
493 {
494         if(type < 0 || type >= CD_NUMTYPES) return NULL;
495
496         return LAYERTYPENAMES[type];
497 }
498
499 /********************* CustomData functions *********************/
500 static void customData_update_offsets(CustomData *data);
501
502 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
503         int type, int alloctype, void *layerdata, int totelem, const char *name);
504
505 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
506                       CustomDataMask mask, int alloctype, int totelem)
507 {
508         const LayerTypeInfo *typeInfo;
509         CustomDataLayer *layer, *newlayer;
510         int i, type, number = 0, lasttype = -1, lastactive = 0, lastrender = 0;
511
512         for(i = 0; i < source->totlayer; ++i) {
513                 layer = &source->layers[i];
514                 typeInfo = layerType_getInfo(layer->type);
515
516                 type = layer->type;
517
518                 if (type != lasttype) {
519                         number = 0;
520                         lastactive = layer->active;
521                         lastrender = layer->active_rnd;
522                         lasttype = type;
523                 }
524                 else
525                         number++;
526
527                 if(layer->flag & CD_FLAG_NOCOPY) continue;
528                 else if(!(mask & (1 << type))) continue;
529                 else if(number < CustomData_number_of_layers(dest, type)) continue;
530
531                 if((alloctype == CD_ASSIGN) && (layer->flag & CD_FLAG_NOFREE))
532                         newlayer = customData_add_layer__internal(dest, type, CD_REFERENCE,
533                                 layer->data, totelem, layer->name);
534                 else
535                         newlayer = customData_add_layer__internal(dest, type, alloctype,
536                                 layer->data, totelem, layer->name);
537                 
538                 if(newlayer) {
539                         newlayer->active = lastactive;
540                         newlayer->active_rnd = lastrender;
541                 }
542         }
543 }
544
545 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
546                      CustomDataMask mask, int alloctype, int totelem)
547 {
548         memset(dest, 0, sizeof(*dest));
549
550         CustomData_merge(source, dest, mask, alloctype, totelem);
551 }
552
553 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
554 {
555         const LayerTypeInfo *typeInfo;
556
557         if(!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
558                 typeInfo = layerType_getInfo(layer->type);
559
560                 if(typeInfo->free)
561                         typeInfo->free(layer->data, totelem, typeInfo->size);
562
563                 if(layer->data)
564                         MEM_freeN(layer->data);
565         }
566 }
567
568 void CustomData_free(CustomData *data, int totelem)
569 {
570         int i;
571
572         for(i = 0; i < data->totlayer; ++i)
573                 customData_free_layer__internal(&data->layers[i], totelem);
574
575         if(data->layers)
576                 MEM_freeN(data->layers);
577         
578         memset(data, 0, sizeof(*data));
579 }
580
581 static void customData_update_offsets(CustomData *data)
582 {
583         const LayerTypeInfo *typeInfo;
584         int i, offset = 0;
585
586         for(i = 0; i < data->totlayer; ++i) {
587                 typeInfo = layerType_getInfo(data->layers[i].type);
588
589                 data->layers[i].offset = offset;
590                 offset += typeInfo->size;
591         }
592
593         data->totsize = offset;
594 }
595
596 int CustomData_get_layer_index(const CustomData *data, int type)
597 {
598         int i; 
599
600         for(i=0; i < data->totlayer; ++i)
601                 if(data->layers[i].type == type)
602                         return i;
603
604         return -1;
605 }
606
607 int CustomData_get_named_layer_index(const CustomData *data, int type, char *name)
608 {
609         int i;
610
611         for(i=0; i < data->totlayer; ++i)
612                 if(data->layers[i].type == type && strcmp(data->layers[i].name, name)==0)
613                         return i;
614
615         return -1;
616 }
617
618 int CustomData_get_active_layer_index(const CustomData *data, int type)
619 {
620         int i;
621
622         for(i=0; i < data->totlayer; ++i)
623                 if(data->layers[i].type == type)
624                         return i + data->layers[i].active;
625
626         return -1;
627 }
628
629 int CustomData_get_render_layer_index(const CustomData *data, int type)
630 {
631         int i;
632
633         for(i=0; i < data->totlayer; ++i)
634                 if(data->layers[i].type == type)
635                         return i + data->layers[i].active_rnd;
636
637         return -1;
638 }
639
640 int CustomData_get_active_layer(const CustomData *data, int type)
641 {
642         int i;
643
644         for(i=0; i < data->totlayer; ++i)
645                 if(data->layers[i].type == type)
646                         return data->layers[i].active;
647
648         return -1;
649 }
650
651 int CustomData_get_render_layer(const CustomData *data, int type)
652 {
653         int i;
654
655         for(i=0; i < data->totlayer; ++i)
656                 if(data->layers[i].type == type)
657                         return data->layers[i].active_rnd;
658
659         return -1;
660 }
661
662
663 void CustomData_set_layer_active(CustomData *data, int type, int n)
664 {
665         int i;
666
667         for(i=0; i < data->totlayer; ++i)
668                 if(data->layers[i].type == type)
669                         data->layers[i].active = n;
670 }
671
672 void CustomData_set_layer_render(CustomData *data, int type, int n)
673 {
674         int i;
675
676         for(i=0; i < data->totlayer; ++i)
677                 if(data->layers[i].type == type)
678                         data->layers[i].active_rnd = n;
679 }
680
681 /* for using with an index from CustomData_get_active_layer_index and CustomData_get_render_layer_index */
682 void CustomData_set_layer_active_index(CustomData *data, int type, int n)
683 {
684         int i;
685
686         for(i=0; i < data->totlayer; ++i)
687                 if(data->layers[i].type == type)
688                         data->layers[i].active = n-i;
689 }
690
691 void CustomData_set_layer_render_index(CustomData *data, int type, int n)
692 {
693         int i;
694
695         for(i=0; i < data->totlayer; ++i)
696                 if(data->layers[i].type == type)
697                         data->layers[i].active_rnd = n-i;
698 }
699
700
701 void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
702 {
703         int i;
704
705         for(i=0; i < data->totlayer; ++i)
706                 if(data->layers[i].type == type)
707                         data->layers[i].flag |= flag;
708 }
709
710 static int customData_resize(CustomData *data, int amount)
711 {
712         CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
713                                        "CustomData->layers");
714         if(!tmp) return 0;
715
716         data->maxlayer += amount;
717         if (data->layers) {
718                 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
719                 MEM_freeN(data->layers);
720         }
721         data->layers = tmp;
722
723         return 1;
724 }
725
726 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
727         int type, int alloctype, void *layerdata, int totelem, const char *name)
728 {
729         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
730         int size = typeInfo->size * totelem, flag = 0, index = data->totlayer;
731         void *newlayerdata;
732
733         if (!typeInfo->defaultname && CustomData_has_layer(data, type))
734                 return &data->layers[CustomData_get_layer_index(data, type)];
735
736         if((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
737                 newlayerdata = layerdata;
738         }
739         else {
740                 newlayerdata = MEM_callocN(size, layerType_getName(type));
741                 if(!newlayerdata)
742                         return NULL;
743         }
744
745         if (alloctype == CD_DUPLICATE) {
746                 if(typeInfo->copy)
747                         typeInfo->copy(layerdata, newlayerdata, totelem);
748                 else
749                         memcpy(newlayerdata, layerdata, size);
750         }
751         else if (alloctype == CD_DEFAULT) {
752                 if(typeInfo->set_default)
753                         typeInfo->set_default((char*)newlayerdata, totelem);
754         }
755         else if (alloctype == CD_REFERENCE)
756                 flag |= CD_FLAG_NOFREE;
757
758         if(index >= data->maxlayer) {
759                 if(!customData_resize(data, CUSTOMDATA_GROW)) {
760                         if(newlayerdata != layerdata)
761                                 MEM_freeN(newlayerdata);
762                         return NULL;
763                 }
764         }
765         
766         data->totlayer++;
767
768         /* keep layers ordered by type */
769         for( ; index > 0 && data->layers[index - 1].type > type; --index)
770                 data->layers[index] = data->layers[index - 1];
771
772         data->layers[index].type = type;
773         data->layers[index].flag = flag;
774         data->layers[index].data = newlayerdata;
775
776         if(name) {
777                 strcpy(data->layers[index].name, name);
778                 CustomData_set_layer_unique_name(data, index);
779         }
780         else
781                 data->layers[index].name[0] = '\0';
782
783         if(index > 0 && data->layers[index-1].type == type) {
784                 data->layers[index].active = data->layers[index-1].active;
785                 data->layers[index].active_rnd = data->layers[index-1].active_rnd;
786         } else {
787                 data->layers[index].active = 0;
788                 data->layers[index].active_rnd = 0;
789         }
790         
791         customData_update_offsets(data);
792
793         return &data->layers[index];
794 }
795
796 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
797                            void *layerdata, int totelem)
798 {
799         CustomDataLayer *layer;
800         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
801         
802         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
803                                                totelem, typeInfo->defaultname);
804
805         if(layer)
806                 return layer->data;
807
808         return NULL;
809 }
810
811 /*same as above but accepts a name*/
812 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
813                            void *layerdata, int totelem, char *name)
814 {
815         CustomDataLayer *layer;
816         
817         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
818                                                totelem, name);
819
820         if(layer)
821                 return layer->data;
822
823         return NULL;
824 }
825
826
827 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
828 {
829         int i;
830         CustomDataLayer *layer;
831         
832         if (index < 0) return 0;
833
834         layer = &data->layers[index];
835
836         customData_free_layer__internal(&data->layers[index], totelem);
837
838         for (i=index+1; i < data->totlayer; ++i)
839                 data->layers[i-1] = data->layers[i];
840
841         data->totlayer--;
842
843         /* if layer was last of type in array, set new active layer */
844         if ((index >= data->totlayer) || (data->layers[index].type != type)) {
845                 i = CustomData_get_layer_index(data, type);
846                 
847                 if (i >= 0)
848                         for (; i < data->totlayer && data->layers[i].type == type; i++) {
849                                 data->layers[i].active--;
850                                 data->layers[i].active_rnd--;
851                         }
852         }
853
854         if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
855                 customData_resize(data, -CUSTOMDATA_GROW);
856
857         customData_update_offsets(data);
858
859         return 1;
860 }
861
862 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
863 {
864         int index = 0;
865         index = CustomData_get_active_layer_index(data, type);
866         if (index < 0) return 0;
867         return CustomData_free_layer(data, type, totelem, index);
868 }
869
870
871 void CustomData_free_layers(CustomData *data, int type, int totelem)
872 {
873         while (CustomData_has_layer(data, type))
874                 CustomData_free_layer_active(data, type, totelem);
875 }
876
877 int CustomData_has_layer(const CustomData *data, int type)
878 {
879         return (CustomData_get_layer_index(data, type) != -1);
880 }
881
882 int CustomData_number_of_layers(const CustomData *data, int type)
883 {
884         int i, number = 0;
885
886         for(i = 0; i < data->totlayer; i++)
887                 if(data->layers[i].type == type)
888                         number++;
889         
890         return number;
891 }
892
893 void *CustomData_duplicate_referenced_layer(struct CustomData *data, int type)
894 {
895         CustomDataLayer *layer;
896         int layer_index;
897
898         /* get the layer index of the first layer of type */
899         layer_index = CustomData_get_active_layer_index(data, type);
900         if(layer_index < 0) return NULL;
901
902         layer = &data->layers[layer_index];
903
904         if (layer->flag & CD_FLAG_NOFREE) {
905                 layer->data = MEM_dupallocN(layer->data);
906                 layer->flag &= ~CD_FLAG_NOFREE;
907         }
908
909         return layer->data;
910 }
911
912 void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
913                                                   int type, char *name)
914 {
915         CustomDataLayer *layer;
916         int layer_index;
917
918         /* get the layer index of the desired layer */
919         layer_index = CustomData_get_named_layer_index(data, type, name);
920         if(layer_index < 0) return NULL;
921
922         layer = &data->layers[layer_index];
923
924         if (layer->flag & CD_FLAG_NOFREE) {
925                 layer->data = MEM_dupallocN(layer->data);
926                 layer->flag &= ~CD_FLAG_NOFREE;
927         }
928
929         return layer->data;
930 }
931
932 void CustomData_free_temporary(CustomData *data, int totelem)
933 {
934         CustomDataLayer *layer;
935         int i, j;
936
937         for(i = 0, j = 0; i < data->totlayer; ++i) {
938                 layer = &data->layers[i];
939
940                 if (i != j)
941                         data->layers[j] = data->layers[i];
942
943                 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
944                         customData_free_layer__internal(layer, totelem);
945                 else
946                         j++;
947         }
948
949         data->totlayer = j;
950
951         if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
952                 customData_resize(data, -CUSTOMDATA_GROW);
953
954         customData_update_offsets(data);
955 }
956
957 void CustomData_set_only_copy(const struct CustomData *data,
958                               CustomDataMask mask)
959 {
960         int i;
961
962         for(i = 0; i < data->totlayer; ++i)
963                 if(!(mask & (1 << data->layers[i].type)))
964                         data->layers[i].flag |= CD_FLAG_NOCOPY;
965 }
966
967 void CustomData_copy_data(const CustomData *source, CustomData *dest,
968                           int source_index, int dest_index, int count)
969 {
970         const LayerTypeInfo *typeInfo;
971         int src_i, dest_i;
972         int src_offset;
973         int dest_offset;
974
975         /* copies a layer at a time */
976         dest_i = 0;
977         for(src_i = 0; src_i < source->totlayer; ++src_i) {
978
979                 /* find the first dest layer with type >= the source type
980                  * (this should work because layers are ordered by type)
981                  */
982                 while(dest_i < dest->totlayer
983                       && dest->layers[dest_i].type < source->layers[src_i].type)
984                         ++dest_i;
985
986                 /* if there are no more dest layers, we're done */
987                 if(dest_i >= dest->totlayer) return;
988
989                 /* if we found a matching layer, copy the data */
990                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
991                         char *src_data = source->layers[src_i].data;
992                         char *dest_data = dest->layers[dest_i].data;
993
994                         typeInfo = layerType_getInfo(source->layers[src_i].type);
995
996                         src_offset = source_index * typeInfo->size;
997                         dest_offset = dest_index * typeInfo->size;
998
999                         if(typeInfo->copy)
1000                                 typeInfo->copy(src_data + src_offset,
1001                                                 dest_data + dest_offset,
1002                                                 count);
1003                         else
1004                                 memcpy(dest_data + dest_offset,
1005                                        src_data + src_offset,
1006                                        count * typeInfo->size);
1007
1008                         /* if there are multiple source & dest layers of the same type,
1009                          * we don't want to copy all source layers to the same dest, so
1010                          * increment dest_i
1011                          */
1012                         ++dest_i;
1013                 }
1014         }
1015 }
1016
1017 void CustomData_free_elem(CustomData *data, int index, int count)
1018 {
1019         int i;
1020         const LayerTypeInfo *typeInfo;
1021
1022         for(i = 0; i < data->totlayer; ++i) {
1023                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1024                         typeInfo = layerType_getInfo(data->layers[i].type);
1025
1026                         if(typeInfo->free) {
1027                                 int offset = typeInfo->size * index;
1028
1029                                 typeInfo->free((char *)data->layers[i].data + offset,
1030                                                count, typeInfo->size);
1031                         }
1032                 }
1033         }
1034 }
1035
1036 #define SOURCE_BUF_SIZE 100
1037
1038 void CustomData_interp(const CustomData *source, CustomData *dest,
1039                        int *src_indices, float *weights, float *sub_weights,
1040                        int count, int dest_index)
1041 {
1042         int src_i, dest_i;
1043         int dest_offset;
1044         int j;
1045         void *source_buf[SOURCE_BUF_SIZE];
1046         void **sources = source_buf;
1047
1048         /* slow fallback in case we're interpolating a ridiculous number of
1049          * elements
1050          */
1051         if(count > SOURCE_BUF_SIZE)
1052                 sources = MEM_callocN(sizeof(*sources) * count,
1053                                       "CustomData_interp sources");
1054
1055         /* interpolates a layer at a time */
1056         dest_i = 0;
1057         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1058                 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
1059                 if(!typeInfo->interp) continue;
1060
1061                 /* find the first dest layer with type >= the source type
1062                  * (this should work because layers are ordered by type)
1063                  */
1064                 while(dest_i < dest->totlayer
1065                       && dest->layers[dest_i].type < source->layers[src_i].type)
1066                         ++dest_i;
1067
1068                 /* if there are no more dest layers, we're done */
1069                 if(dest_i >= dest->totlayer) return;
1070
1071                 /* if we found a matching layer, copy the data */
1072                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1073                         void *src_data = source->layers[src_i].data;
1074
1075                         for(j = 0; j < count; ++j)
1076                                 sources[j] = (char *)src_data
1077                                                          + typeInfo->size * src_indices[j];
1078
1079                         dest_offset = dest_index * typeInfo->size;
1080
1081                         typeInfo->interp(sources, weights, sub_weights, count,
1082                                                    (char *)dest->layers[dest_i].data + dest_offset);
1083
1084                         /* if there are multiple source & dest layers of the same type,
1085                          * we don't want to copy all source layers to the same dest, so
1086                          * increment dest_i
1087                          */
1088                         ++dest_i;
1089                 }
1090         }
1091
1092         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1093 }
1094
1095 void CustomData_swap(struct CustomData *data, int index, int *corner_indices)
1096 {
1097         const LayerTypeInfo *typeInfo;
1098         int i;
1099
1100         for(i = 0; i < data->totlayer; ++i) {
1101                 typeInfo = layerType_getInfo(data->layers[i].type);
1102
1103                 if(typeInfo->swap) {
1104                         int offset = typeInfo->size * index;
1105
1106                         typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
1107                 }
1108         }
1109 }
1110
1111 void *CustomData_get(const CustomData *data, int index, int type)
1112 {
1113         int offset;
1114         int layer_index;
1115         
1116         /* get the layer index of the active layer of type */
1117         layer_index = CustomData_get_active_layer_index(data, type);
1118         if(layer_index < 0) return NULL;
1119
1120         /* get the offset of the desired element */
1121         offset = layerType_getInfo(type)->size * index;
1122
1123         return (char *)data->layers[layer_index].data + offset;
1124 }
1125
1126 void *CustomData_get_layer(const CustomData *data, int type)
1127 {
1128         /* get the layer index of the active layer of type */
1129         int layer_index = CustomData_get_active_layer_index(data, type);
1130         if(layer_index < 0) return NULL;
1131
1132         return data->layers[layer_index].data;
1133 }
1134
1135 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
1136 {
1137         /* get the layer index of the active layer of type */
1138         int layer_index = CustomData_get_layer_index(data, type);
1139         if(layer_index < 0) return NULL;
1140
1141         return data->layers[layer_index+n].data;
1142 }
1143
1144 void *CustomData_get_layer_named(const struct CustomData *data, int type,
1145                                  char *name)
1146 {
1147         int layer_index = CustomData_get_named_layer_index(data, type, name);
1148         if(layer_index < 0) return NULL;
1149
1150         return data->layers[layer_index].data;
1151 }
1152
1153 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
1154 {
1155         /* get the layer index of the first layer of type */
1156         int layer_index = CustomData_get_active_layer_index(data, type);
1157
1158         if(layer_index < 0) return NULL;
1159
1160         data->layers[layer_index].data = ptr;
1161
1162         return ptr;
1163 }
1164
1165 void *CustomData_set_layer_n(const struct CustomData *data, int type, int n, void *ptr)
1166 {
1167         /* get the layer index of the first layer of type */
1168         int layer_index = CustomData_get_layer_index(data, type);
1169         if(layer_index < 0) return NULL;
1170
1171         data->layers[layer_index+n].data = ptr;
1172
1173         return ptr;
1174 }
1175
1176 void CustomData_set(const CustomData *data, int index, int type, void *source)
1177 {
1178         void *dest = CustomData_get(data, index, type);
1179         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1180
1181         if(!dest) return;
1182
1183         if(typeInfo->copy)
1184                 typeInfo->copy(source, dest, 1);
1185         else
1186                 memcpy(dest, source, typeInfo->size);
1187 }
1188
1189 /* EditMesh functions */
1190
1191 void CustomData_em_free_block(CustomData *data, void **block)
1192 {
1193     const LayerTypeInfo *typeInfo;
1194     int i;
1195
1196         if(!*block) return;
1197
1198     for(i = 0; i < data->totlayer; ++i) {
1199         if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1200             typeInfo = layerType_getInfo(data->layers[i].type);
1201
1202             if(typeInfo->free) {
1203                                 int offset = data->layers[i].offset;
1204                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
1205                         }
1206         }
1207     }
1208
1209         MEM_freeN(*block);
1210         *block = NULL;
1211 }
1212
1213 static void CustomData_em_alloc_block(CustomData *data, void **block)
1214 {
1215         /* TODO: optimize free/alloc */
1216
1217         if (*block)
1218                 CustomData_em_free_block(data, block);
1219
1220         if (data->totsize > 0)
1221                 *block = MEM_callocN(data->totsize, "CustomData EM block");
1222         else
1223                 *block = NULL;
1224 }
1225
1226 void CustomData_em_copy_data(const CustomData *source, CustomData *dest,
1227                             void *src_block, void **dest_block)
1228 {
1229         const LayerTypeInfo *typeInfo;
1230         int dest_i, src_i;
1231
1232         if (!*dest_block)
1233                 CustomData_em_alloc_block(dest, dest_block);
1234         
1235         /* copies a layer at a time */
1236         dest_i = 0;
1237         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1238
1239                 /* find the first dest layer with type >= the source type
1240                  * (this should work because layers are ordered by type)
1241                  */
1242                 while(dest_i < dest->totlayer
1243                       && dest->layers[dest_i].type < source->layers[src_i].type)
1244                         ++dest_i;
1245
1246                 /* if there are no more dest layers, we're done */
1247                 if(dest_i >= dest->totlayer) return;
1248
1249                 /* if we found a matching layer, copy the data */
1250                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
1251                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
1252                         char *src_data = (char*)src_block + source->layers[src_i].offset;
1253                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
1254
1255                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1256
1257                         if(typeInfo->copy)
1258                                 typeInfo->copy(src_data, dest_data, 1);
1259                         else
1260                                 memcpy(dest_data, src_data, typeInfo->size);
1261
1262                         /* if there are multiple source & dest layers of the same type,
1263                          * we don't want to copy all source layers to the same dest, so
1264                          * increment dest_i
1265                          */
1266                         ++dest_i;
1267                 }
1268         }
1269 }
1270
1271 void *CustomData_em_get(const CustomData *data, void *block, int type)
1272 {
1273         int layer_index;
1274         
1275         /* get the layer index of the first layer of type */
1276         layer_index = CustomData_get_active_layer_index(data, type);
1277         if(layer_index < 0) return NULL;
1278
1279         return (char *)block + data->layers[layer_index].offset;
1280 }
1281
1282 void *CustomData_em_get_n(const CustomData *data, void *block, int type, int n)
1283 {
1284         int layer_index;
1285         
1286         /* get the layer index of the first layer of type */
1287         layer_index = CustomData_get_layer_index(data, type);
1288         if(layer_index < 0) return NULL;
1289
1290         return (char *)block + data->layers[layer_index+n].offset;
1291 }
1292
1293 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
1294 {
1295         void *dest = CustomData_em_get(data, block, type);
1296         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1297
1298         if(!dest) return;
1299
1300         if(typeInfo->copy)
1301                 typeInfo->copy(source, dest, 1);
1302         else
1303                 memcpy(dest, source, typeInfo->size);
1304 }
1305
1306 void CustomData_em_set_n(CustomData *data, void *block, int type, int n, void *source)
1307 {
1308         void *dest = CustomData_em_get_n(data, block, type, n);
1309         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1310
1311         if(!dest) return;
1312
1313         if(typeInfo->copy)
1314                 typeInfo->copy(source, dest, 1);
1315         else
1316                 memcpy(dest, source, typeInfo->size);
1317 }
1318
1319 void CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
1320                           float *sub_weights, int count, void *dest_block)
1321 {
1322         int i, j;
1323         void *source_buf[SOURCE_BUF_SIZE];
1324         void **sources = source_buf;
1325
1326         /* slow fallback in case we're interpolating a ridiculous number of
1327          * elements
1328          */
1329         if(count > SOURCE_BUF_SIZE)
1330                 sources = MEM_callocN(sizeof(*sources) * count,
1331                                       "CustomData_interp sources");
1332
1333         /* interpolates a layer at a time */
1334         for(i = 0; i < data->totlayer; ++i) {
1335                 CustomDataLayer *layer = &data->layers[i];
1336                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1337
1338                 if(typeInfo->interp) {
1339                         for(j = 0; j < count; ++j)
1340                                 sources[j] = (char *)src_blocks[j] + layer->offset;
1341
1342                         typeInfo->interp(sources, weights, sub_weights, count,
1343                                           (char *)dest_block + layer->offset);
1344                 }
1345         }
1346
1347         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1348 }
1349
1350 void CustomData_em_set_default(CustomData *data, void **block)
1351 {
1352         const LayerTypeInfo *typeInfo;
1353         int i;
1354
1355         if (!*block)
1356                 CustomData_em_alloc_block(data, block);
1357
1358         for(i = 0; i < data->totlayer; ++i) {
1359                 int offset = data->layers[i].offset;
1360
1361                 typeInfo = layerType_getInfo(data->layers[i].type);
1362
1363                 if(typeInfo->set_default)
1364                         typeInfo->set_default((char*)*block + offset, 1);
1365         }
1366 }
1367
1368 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
1369                             int src_index, void **dest_block)
1370 {
1371         const LayerTypeInfo *typeInfo;
1372         int dest_i, src_i, src_offset;
1373
1374         if (!*dest_block)
1375                 CustomData_em_alloc_block(dest, dest_block);
1376         
1377         /* copies a layer at a time */
1378         dest_i = 0;
1379         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1380
1381                 /* find the first dest layer with type >= the source type
1382                  * (this should work because layers are ordered by type)
1383                  */
1384                 while(dest_i < dest->totlayer
1385                       && dest->layers[dest_i].type < source->layers[src_i].type)
1386                         ++dest_i;
1387
1388                 /* if there are no more dest layers, we're done */
1389                 if(dest_i >= dest->totlayer) return;
1390
1391                 /* if we found a matching layer, copy the data */
1392                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1393                         int offset = dest->layers[dest_i].offset;
1394                         char *src_data = source->layers[src_i].data;
1395                         char *dest_data = (char*)*dest_block + offset;
1396
1397                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1398                         src_offset = src_index * typeInfo->size;
1399
1400                         if(typeInfo->copy)
1401                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
1402                         else
1403                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
1404
1405                         /* if there are multiple source & dest layers of the same type,
1406                          * we don't want to copy all source layers to the same dest, so
1407                          * increment dest_i
1408                          */
1409                         ++dest_i;
1410                 }
1411         }
1412 }
1413
1414 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
1415                               void *src_block, int dest_index)
1416 {
1417         const LayerTypeInfo *typeInfo;
1418         int dest_i, src_i, dest_offset;
1419
1420         /* copies a layer at a time */
1421         dest_i = 0;
1422         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1423
1424                 /* find the first dest layer with type >= the source type
1425                  * (this should work because layers are ordered by type)
1426                  */
1427                 while(dest_i < dest->totlayer
1428                       && dest->layers[dest_i].type < source->layers[src_i].type)
1429                         ++dest_i;
1430
1431                 /* if there are no more dest layers, we're done */
1432                 if(dest_i >= dest->totlayer) return;
1433
1434                 /* if we found a matching layer, copy the data */
1435                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1436                         int offset = source->layers[src_i].offset;
1437                         char *src_data = (char*)src_block + offset;
1438                         char *dest_data = dest->layers[dest_i].data;
1439
1440                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1441                         dest_offset = dest_index * typeInfo->size;
1442
1443                         if(typeInfo->copy)
1444                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
1445                         else
1446                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
1447
1448                         /* if there are multiple source & dest layers of the same type,
1449                          * we don't want to copy all source layers to the same dest, so
1450                          * increment dest_i
1451                          */
1452                         ++dest_i;
1453                 }
1454         }
1455
1456 }
1457
1458 /*Bmesh functions*/
1459 void CustomData_bmesh_free_block(CustomData *data, void **block)
1460 {
1461     const LayerTypeInfo *typeInfo;
1462     int i;
1463
1464         if(!*block) return;
1465     for(i = 0; i < data->totlayer; ++i) {
1466         if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1467             typeInfo = layerType_getInfo(data->layers[i].type);
1468
1469             if(typeInfo->free) {
1470                                 int offset = data->layers[i].offset;
1471                                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
1472                         }
1473         }
1474     }
1475
1476         BLI_mempool_free(data->pool, *block);
1477         *block = NULL;
1478 }
1479
1480 static void CustomData_bmesh_alloc_block(CustomData *data, void **block)
1481 {
1482
1483         if (*block)
1484                 CustomData_bmesh_free_block(data, block);
1485
1486         if (data->totsize > 0)
1487                 *block = BLI_mempool_alloc(data->pool);
1488         else
1489                 *block = NULL;
1490 }
1491
1492 void CustomData_bmesh_copy_data(const CustomData *source, CustomData *dest,
1493                             void *src_block, void **dest_block)
1494 {
1495         const LayerTypeInfo *typeInfo;
1496         int dest_i, src_i;
1497
1498         if (!*dest_block)
1499                 CustomData_bmesh_alloc_block(dest, dest_block);
1500         
1501         /* copies a layer at a time */
1502         dest_i = 0;
1503         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1504
1505                 /* find the first dest layer with type >= the source type
1506                  * (this should work because layers are ordered by type)
1507                  */
1508                 while(dest_i < dest->totlayer
1509                       && dest->layers[dest_i].type < source->layers[src_i].type)
1510                         ++dest_i;
1511
1512                 /* if there are no more dest layers, we're done */
1513                 if(dest_i >= dest->totlayer) return;
1514
1515                 /* if we found a matching layer, copy the data */
1516                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
1517                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
1518                         char *src_data = (char*)src_block + source->layers[src_i].offset;
1519                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
1520
1521                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1522
1523                         if(typeInfo->copy)
1524                                 typeInfo->copy(src_data, dest_data, 1);
1525                         else
1526                                 memcpy(dest_data, src_data, typeInfo->size);
1527
1528                         /* if there are multiple source & dest layers of the same type,
1529                          * we don't want to copy all source layers to the same dest, so
1530                          * increment dest_i
1531                          */
1532                         ++dest_i;
1533                 }
1534         }
1535 }
1536
1537 /*Bmesh Custom Data Functions. Should replace editmesh ones with these as well, due to more effecient memory alloc*/
1538 void *CustomData_bmesh_get(const CustomData *data, void *block, int type)
1539 {
1540         int layer_index;
1541         
1542         /* get the layer index of the first layer of type */
1543         layer_index = CustomData_get_active_layer_index(data, type);
1544         if(layer_index < 0) return NULL;
1545
1546         return (char *)block + data->layers[layer_index].offset;
1547 }
1548
1549 void *CustomData_bmesh_get_n(const CustomData *data, void *block, int type, int n)
1550 {
1551         int layer_index;
1552         
1553         /* get the layer index of the first layer of type */
1554         layer_index = CustomData_get_layer_index(data, type);
1555         if(layer_index < 0) return NULL;
1556
1557         return (char *)block + data->layers[layer_index+n].offset;
1558 }
1559
1560 void CustomData_bmesh_set(CustomData *data, void *block, int type, void *source)
1561 {
1562         void *dest = CustomData_bmesh_get(data, block, type);
1563         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1564
1565         if(!dest) return;
1566
1567         if(typeInfo->copy)
1568                 typeInfo->copy(source, dest, 1);
1569         else
1570                 memcpy(dest, source, typeInfo->size);
1571 }
1572
1573 void CustomData_bmesh_set_n(CustomData *data, void *block, int type, int n, void *source)
1574 {
1575         void *dest = CustomData_bmesh_get_n(data, block, type, n);
1576         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1577
1578         if(!dest) return;
1579
1580         if(typeInfo->copy)
1581                 typeInfo->copy(source, dest, 1);
1582         else
1583                 memcpy(dest, source, typeInfo->size);
1584 }
1585
1586 void CustomData_bmesh_interp(CustomData *data, void **src_blocks, float *weights,
1587                           float *sub_weights, int count, void *dest_block)
1588 {
1589         int i, j;
1590         void *source_buf[SOURCE_BUF_SIZE];
1591         void **sources = source_buf;
1592
1593         /* slow fallback in case we're interpolating a ridiculous number of
1594          * elements
1595          */
1596         if(count > SOURCE_BUF_SIZE)
1597                 sources = MEM_callocN(sizeof(*sources) * count,
1598                                       "CustomData_interp sources");
1599
1600         /* interpolates a layer at a time */
1601         for(i = 0; i < data->totlayer; ++i) {
1602                 CustomDataLayer *layer = &data->layers[i];
1603                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1604
1605                 if(typeInfo->interp) {
1606                         for(j = 0; j < count; ++j)
1607                                 sources[j] = (char *)src_blocks[j] + layer->offset;
1608
1609                         typeInfo->interp(sources, weights, sub_weights, count,
1610                                           (char *)dest_block + layer->offset);
1611                 }
1612         }
1613
1614         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1615 }
1616
1617 void CustomData_bmesh_set_default(CustomData *data, void **block)
1618 {
1619         const LayerTypeInfo *typeInfo;
1620         int i;
1621
1622         if (!*block)
1623                 CustomData_bmesh_alloc_block(data, block);
1624
1625         for(i = 0; i < data->totlayer; ++i) {
1626                 int offset = data->layers[i].offset;
1627
1628                 typeInfo = layerType_getInfo(data->layers[i].type);
1629
1630                 if(typeInfo->set_default)
1631                         typeInfo->set_default((char*)*block + offset, 1);
1632         }
1633 }
1634
1635 void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
1636                             int src_index, void **dest_block)
1637 {
1638         const LayerTypeInfo *typeInfo;
1639         int dest_i, src_i, src_offset;
1640
1641         if (!*dest_block)
1642                 CustomData_bmesh_alloc_block(dest, dest_block);
1643         
1644         /* copies a layer at a time */
1645         dest_i = 0;
1646         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1647
1648                 /* find the first dest layer with type >= the source type
1649                  * (this should work because layers are ordered by type)
1650                  */
1651                 while(dest_i < dest->totlayer
1652                       && dest->layers[dest_i].type < source->layers[src_i].type)
1653                         ++dest_i;
1654
1655                 /* if there are no more dest layers, we're done */
1656                 if(dest_i >= dest->totlayer) return;
1657
1658                 /* if we found a matching layer, copy the data */
1659                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1660                         int offset = dest->layers[dest_i].offset;
1661                         char *src_data = source->layers[src_i].data;
1662                         char *dest_data = (char*)*dest_block + offset;
1663
1664                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1665                         src_offset = src_index * typeInfo->size;
1666
1667                         if(typeInfo->copy)
1668                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
1669                         else
1670                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
1671
1672                         /* if there are multiple source & dest layers of the same type,
1673                          * we don't want to copy all source layers to the same dest, so
1674                          * increment dest_i
1675                          */
1676                         ++dest_i;
1677                 }
1678         }
1679 }
1680
1681 void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest,
1682                               void *src_block, int dest_index)
1683 {
1684         const LayerTypeInfo *typeInfo;
1685         int dest_i, src_i, dest_offset;
1686
1687         /* copies a layer at a time */
1688         dest_i = 0;
1689         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1690
1691                 /* find the first dest layer with type >= the source type
1692                  * (this should work because layers are ordered by type)
1693                  */
1694                 while(dest_i < dest->totlayer
1695                       && dest->layers[dest_i].type < source->layers[src_i].type)
1696                         ++dest_i;
1697
1698                 /* if there are no more dest layers, we're done */
1699                 if(dest_i >= dest->totlayer) return;
1700
1701                 /* if we found a matching layer, copy the data */
1702                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1703                         int offset = source->layers[src_i].offset;
1704                         char *src_data = (char*)src_block + offset;
1705                         char *dest_data = dest->layers[dest_i].data;
1706
1707                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1708                         dest_offset = dest_index * typeInfo->size;
1709
1710                         if(typeInfo->copy)
1711                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
1712                         else
1713                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
1714
1715                         /* if there are multiple source & dest layers of the same type,
1716                          * we don't want to copy all source layers to the same dest, so
1717                          * increment dest_i
1718                          */
1719                         ++dest_i;
1720                 }
1721         }
1722
1723 }
1724
1725 void CustomData_file_write_info(int type, char **structname, int *structnum)
1726 {
1727         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1728
1729         *structname = typeInfo->structname;
1730         *structnum = typeInfo->structnum;
1731 }
1732
1733 int CustomData_sizeof(int type)
1734 {
1735         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1736
1737         return typeInfo->size;
1738 }
1739
1740 const char *CustomData_layertype_name(int type)
1741 {
1742         return layerType_getName(type);
1743 }
1744
1745 static int  CustomData_is_property_layer(int type)
1746 {
1747         if((type == CD_PROP_FLT) || (type == CD_PROP_INT) || (type == CD_PROP_STR))
1748                 return 1;
1749         return 0;
1750 }
1751
1752 void CustomData_set_layer_unique_name(CustomData *data, int index)
1753 {
1754         char tempname[64];
1755         int number, i, type;
1756         char *dot, *name;
1757         CustomDataLayer *layer, *nlayer= &data->layers[index];
1758         const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
1759
1760         if (!typeInfo->defaultname)
1761                 return;
1762
1763         type = nlayer->type;
1764         name = nlayer->name;
1765
1766         if (name[0] == '\0')
1767                 BLI_strncpy(nlayer->name, typeInfo->defaultname, sizeof(nlayer->name));
1768         
1769         /* see if there is a duplicate */
1770         for(i=0; i<data->totlayer; i++) {
1771                 layer = &data->layers[i];
1772                 
1773                 if(CustomData_is_property_layer(type)){
1774                         if(i!=index && CustomData_is_property_layer(layer->type) && 
1775                                 strcmp(layer->name, name)==0)
1776                                         break;  
1777                 
1778                 }
1779                 else{
1780                         if(i!=index && layer->type==type && strcmp(layer->name, name)==0)
1781                                 break;
1782                 }
1783         }
1784
1785         if(i == data->totlayer)
1786                 return;
1787
1788         /* strip off the suffix */
1789         dot = strchr(nlayer->name, '.');
1790         if(dot) *dot=0;
1791         
1792         for(number=1; number <=999; number++) {
1793                 sprintf(tempname, "%s.%03d", nlayer->name, number);
1794
1795                 for(i=0; i<data->totlayer; i++) {
1796                         layer = &data->layers[i];
1797                         
1798                         if(CustomData_is_property_layer(type)){
1799                                 if(i!=index && CustomData_is_property_layer(layer->type) && 
1800                                         strcmp(layer->name, tempname)==0)
1801
1802                                 break;
1803                         }
1804                         else{
1805                                 if(i!=index && layer->type==type && strcmp(layer->name, tempname)==0)
1806                                         break;
1807                         }
1808                 }
1809
1810                 if(i == data->totlayer) {
1811                         BLI_strncpy(nlayer->name, tempname, sizeof(nlayer->name));
1812                         return;
1813                 }
1814         }       
1815 }
1816
1817 int CustomData_verify_versions(struct CustomData *data, int index)
1818 {
1819         const LayerTypeInfo *typeInfo;
1820         CustomDataLayer *layer = &data->layers[index];
1821         int i, keeplayer = 1;
1822
1823         if (layer->type >= CD_NUMTYPES) {
1824                 keeplayer = 0; /* unknown layer type from future version */
1825         }
1826         else {
1827                 typeInfo = layerType_getInfo(layer->type);
1828
1829                 if (!typeInfo->defaultname && (index > 0) &&
1830                         data->layers[index-1].type == layer->type)
1831                         keeplayer = 0; /* multiple layers of which we only support one */
1832         }
1833
1834         if (!keeplayer) {
1835             for (i=index+1; i < data->totlayer; ++i)
1836             data->layers[i-1] = data->layers[i];
1837                 data->totlayer--;
1838         }
1839
1840         return keeplayer;
1841 }
1842