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