Potential fix for bug #5359:
[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
40 #include "DNA_customdata_types.h"
41 #include "DNA_listBase.h"
42 #include "DNA_meshdata_types.h"
43
44 #include "MEM_guardedalloc.h"
45
46 #include <string.h>
47
48 /* number of layers to add when growing a CustomData object */
49 #define CUSTOMDATA_GROW 5
50
51 /********************* Layer type information **********************/
52 typedef struct LayerTypeInfo {
53         int size;          /* the memory size of one element of this layer's data */
54         char *structname;  /* name of the struct used, for file writing */
55         int structnum;     /* number of structs per element, for file writing */
56         char *defaultname; /* default layer name */
57
58         /* a function to copy count elements of this layer's data
59          * (deep copy if appropriate)
60          * if NULL, memcpy is used
61          */
62         void (*copy)(const void *source, void *dest, int count);
63
64         /* a function to free any dynamically allocated components of this
65          * layer's data (note the data pointer itself should not be freed)
66          * size should be the size of one element of this layer's data (e.g.
67          * LayerTypeInfo.size)
68          */
69         void (*free)(void *data, int count, int size);
70
71         /* a function to interpolate between count source elements of this
72          * layer's data and store the result in dest
73          * if weights == NULL or sub_weights == NULL, they should default to 1
74          *
75          * weights gives the weight for each element in sources
76          * sub_weights gives the sub-element weights for each element in sources
77          *    (there should be (sub element count)^2 weights per element)
78          * count gives the number of elements in sources
79          */
80         void (*interp)(void **sources, float *weights, float *sub_weights,
81                        int count, void *dest);
82
83     /* a function to swap the data in corners of the element */
84         void (*swap)(void *data, int *corner_indices);
85
86     /* a function to set a layer's data to default values. if NULL, the
87            default is assumed to be all zeros */
88         void (*set_default)(void *data, int count);
89 } LayerTypeInfo;
90
91 static void layerCopy_mdeformvert(const void *source, void *dest,
92                                   int count)
93 {
94         int i, size = sizeof(MDeformVert);
95
96         memcpy(dest, source, count * size);
97
98         for(i = 0; i < count; ++i) {
99                 MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
100                 MDeformWeight *dw = MEM_callocN(dvert->totweight * sizeof(*dw),
101                                                 "layerCopy_mdeformvert dw");
102
103                 memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
104                 dvert->dw = dw;
105         }
106 }
107
108 static void layerFree_mdeformvert(void *data, int count, int size)
109 {
110         int i;
111
112         for(i = 0; i < count; ++i) {
113                 MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
114
115                 if(dvert->dw) {
116                         MEM_freeN(dvert->dw);
117                         dvert->dw = NULL;
118                         dvert->totweight = 0;
119                 }
120         }
121 }
122
123 static void linklist_free_simple(void *link)
124 {
125         MEM_freeN(link);
126 }
127
128 static void layerInterp_mdeformvert(void **sources, float *weights,
129                                     float *sub_weights, int count, void *dest)
130 {
131         MDeformVert *dvert = dest;
132         LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
133         LinkNode *node;
134         int i, j, totweight;
135
136         if(count <= 0) return;
137
138         /* build a list of unique def_nrs for dest */
139         totweight = 0;
140         for(i = 0; i < count; ++i) {
141                 MDeformVert *source = sources[i];
142                 float interp_weight = weights ? weights[i] : 1.0f;
143
144                 for(j = 0; j < source->totweight; ++j) {
145                         MDeformWeight *dw = &source->dw[j];
146
147                         for(node = dest_dw; node; node = node->next) {
148                                 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
149
150                                 if(tmp_dw->def_nr == dw->def_nr) {
151                                         tmp_dw->weight += dw->weight * interp_weight;
152                                         break;
153                                 }
154                         }
155
156                         /* if this def_nr is not in the list, add it */
157                         if(!node) {
158                                 MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
159                                                             "layerInterp_mdeformvert tmp_dw");
160                                 tmp_dw->def_nr = dw->def_nr;
161                                 tmp_dw->weight = dw->weight * interp_weight;
162                                 BLI_linklist_prepend(&dest_dw, tmp_dw);
163                                 totweight++;
164                         }
165                 }
166         }
167
168         /* now we know how many unique deform weights there are, so realloc */
169         if(dvert->dw) MEM_freeN(dvert->dw);
170
171         if(totweight) {
172                 dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
173                                         "layerInterp_mdeformvert dvert->dw");
174                 dvert->totweight = totweight;
175
176                 for(i = 0, node = dest_dw; node; node = node->next, ++i)
177                         dvert->dw[i] = *((MDeformWeight *)node->link);
178         }
179         else
180                 memset(dvert, 0, sizeof(*dvert));
181
182         BLI_linklist_free(dest_dw, linklist_free_simple);
183 }
184
185
186 static void layerInterp_msticky(void **sources, float *weights,
187                                 float *sub_weights, int count, void *dest)
188 {
189         float co[2], w;
190         MSticky *mst;
191         int i;
192
193         co[0] = co[1] = 0.0f;
194         for(i = 0; i < count; i++) {
195                 w = weights ? weights[i] : 1.0f;
196                 mst = (MSticky*)sources[i];
197
198                 co[0] += w*mst->co[0];
199                 co[1] += w*mst->co[1];
200         }
201
202         mst = (MSticky*)dest;
203         mst->co[0] = co[0];
204         mst->co[1] = co[1];
205 }
206
207
208 static void layerCopy_tface(const void *source, void *dest, int count)
209 {
210         const MTFace *source_tf = (const MTFace*)source;
211         MTFace *dest_tf = (MTFace*)dest;
212         int i;
213
214         for(i = 0; i < count; ++i)
215                 dest_tf[i] = source_tf[i];
216 }
217
218 static void layerInterp_tface(void **sources, float *weights,
219                               float *sub_weights, int count, void *dest)
220 {
221         MTFace *tf = dest;
222         int i, j, k;
223         float uv[4][2];
224         float *sub_weight;
225
226         if(count <= 0) return;
227
228         memset(uv, 0, sizeof(uv));
229
230         sub_weight = sub_weights;
231         for(i = 0; i < count; ++i) {
232                 float weight = weights ? weights[i] : 1;
233                 MTFace *src = sources[i];
234
235                 for(j = 0; j < 4; ++j) {
236                         if(sub_weights) {
237                                 for(k = 0; k < 4; ++k, ++sub_weight) {
238                                         float w = (*sub_weight) * weight;
239                                         float *tmp_uv = src->uv[k];
240
241                                         uv[j][0] += tmp_uv[0] * w;
242                                         uv[j][1] += tmp_uv[1] * w;
243                                 }
244                         } else {
245                                 uv[j][0] += src->uv[j][0] * weight;
246                                 uv[j][1] += src->uv[j][1] * weight;
247                         }
248                 }
249         }
250
251         *tf = *(MTFace *)sources[0];
252         for(j = 0; j < 4; ++j) {
253                 tf->uv[j][0] = uv[j][0];
254                 tf->uv[j][1] = uv[j][1];
255         }
256 }
257
258 static void layerSwap_tface(void *data, int *corner_indices)
259 {
260         MTFace *tf = data;
261         float uv[4][2];
262         int j;
263
264         for(j = 0; j < 4; ++j) {
265                 uv[j][0] = tf->uv[corner_indices[j]][0];
266                 uv[j][1] = tf->uv[corner_indices[j]][1];
267         }
268
269         memcpy(tf->uv, uv, sizeof(tf->uv));
270 }
271
272 static void layerDefault_tface(void *data, int count)
273 {
274         static MTFace default_tf = {{{0, 1}, {0, 0}, {1, 0}, {1, 1}}, NULL,
275                                    TF_SELECT, 0, TF_DYNAMIC, 0, 0};
276         MTFace *tf = (MTFace*)data;
277         int i;
278
279         for(i = 0; i < count; i++)
280                 tf[i] = default_tf;
281 }
282
283 static void layerInterp_mcol(void **sources, float *weights,
284                              float *sub_weights, int count, void *dest)
285 {
286         MCol *mc = dest;
287         int i, j, k;
288         struct {
289                 float a;
290                 float r;
291                 float g;
292                 float b;
293         } col[4];
294         float *sub_weight;
295
296         if(count <= 0) return;
297
298         memset(col, 0, sizeof(col));
299         
300         sub_weight = sub_weights;
301         for(i = 0; i < count; ++i) {
302                 float weight = weights ? weights[i] : 1;
303
304                 for(j = 0; j < 4; ++j) {
305                         if(sub_weights) {
306                                 MCol *src = sources[i];
307                                 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
308                                         col[j].a += src->a * (*sub_weight) * weight;
309                                         col[j].r += src->r * (*sub_weight) * weight;
310                                         col[j].g += src->g * (*sub_weight) * weight;
311                                         col[j].b += src->b * (*sub_weight) * weight;
312                                 }
313                         } else {
314                                 MCol *src = sources[i];
315                                 col[j].a += src[j].a * weight;
316                                 col[j].r += src[j].r * weight;
317                                 col[j].g += src[j].g * weight;
318                                 col[j].b += src[j].b * weight;
319                         }
320                 }
321         }
322
323         for(j = 0; j < 4; ++j) {
324                 mc[j].a = (int)col[j].a;
325                 mc[j].r = (int)col[j].r;
326                 mc[j].g = (int)col[j].g;
327                 mc[j].b = (int)col[j].b;
328         }
329 }
330
331 static void layerSwap_mcol(void *data, int *corner_indices)
332 {
333         MCol *mcol = data;
334         MCol col[4];
335         int j;
336
337         for(j = 0; j < 4; ++j)
338                 col[j] = mcol[corner_indices[j]];
339
340         memcpy(mcol, col, sizeof(col));
341 }
342
343 static void layerDefault_mcol(void *data, int count)
344 {
345         static MCol default_mcol = {255, 255, 255, 255};
346         MCol *mcol = (MCol*)data;
347         int i;
348
349         for(i = 0; i < 4*count; i++)
350                 mcol[i] = default_mcol;
351 }
352
353 const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
354         {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
355         {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
356          NULL},
357         {sizeof(MDeformVert), "MDeformVert", 1, NULL, layerCopy_mdeformvert,
358          layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
359         {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL, NULL},
360         {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL, NULL},
361         {sizeof(MTFace), "MTFace", 1, "UVTex", layerCopy_tface, NULL,
362          layerInterp_tface, layerSwap_tface, layerDefault_tface},
363         /* 4 MCol structs per face */
364         {sizeof(MCol)*4, "MCol", 4, "Col", NULL, NULL, layerInterp_mcol,
365          layerSwap_mcol, layerDefault_mcol},
366         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
367         /* 3 floats per normal vector */
368         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
369         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
370 };
371
372 const char *LAYERTYPENAMES[CD_NUMTYPES] = {
373         "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace", "CDMTFace",
374         "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags"};
375
376 const CustomDataMask CD_MASK_BAREMESH =
377         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE;
378 const CustomDataMask CD_MASK_MESH =
379         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
380         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL;
381 const CustomDataMask CD_MASK_EDITMESH =
382         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
383         CD_MASK_MCOL;
384 const CustomDataMask CD_MASK_DERIVEDMESH =
385         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
386         CD_MASK_MCOL | CD_MASK_ORIGINDEX;
387
388 static const LayerTypeInfo *layerType_getInfo(int type)
389 {
390         if(type < 0 || type >= CD_NUMTYPES) return NULL;
391
392         return &LAYERTYPEINFO[type];
393 }
394
395 static const char *layerType_getName(int type)
396 {
397         if(type < 0 || type >= CD_NUMTYPES) return NULL;
398
399         return LAYERTYPENAMES[type];
400 }
401
402 /********************* CustomData functions *********************/
403 static void customData_update_offsets(CustomData *data);
404
405 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
406         int type, int alloctype, void *layerdata, int totelem, const char *name);
407
408 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
409                       CustomDataMask mask, int alloctype, int totelem)
410 {
411         const LayerTypeInfo *typeInfo;
412         CustomDataLayer *layer, *newlayer;
413         int i, type, number = 0, lasttype = -1, lastactive = 0;
414
415         for(i = 0; i < source->totlayer; ++i) {
416                 layer = &source->layers[i];
417                 typeInfo = layerType_getInfo(layer->type);
418
419                 type = layer->type;
420
421                 if (type != lasttype) {
422                         number = 0;
423                         lastactive = layer->active;
424                         lasttype = type;
425                 }
426                 else
427                         number++;
428
429                 if(layer->flag & CD_FLAG_NOCOPY) continue;
430                 else if(!(mask & (1 << type))) continue;
431                 else if(number < CustomData_number_of_layers(dest, type)) continue;
432
433                 if((alloctype == CD_ASSIGN) && (layer->flag & CD_FLAG_NOFREE))
434                         newlayer = customData_add_layer__internal(dest, type, CD_REFERENCE,
435                                 layer->data, totelem, layer->name);
436                 else
437                         newlayer = customData_add_layer__internal(dest, type, alloctype,
438                                 layer->data, totelem, layer->name);
439                 
440                 if(newlayer)
441                         newlayer->active = lastactive;
442         }
443 }
444
445 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
446                      CustomDataMask mask, int alloctype, int totelem)
447 {
448         memset(dest, 0, sizeof(*dest));
449
450         CustomData_merge(source, dest, mask, alloctype, totelem);
451 }
452
453 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
454 {
455         const LayerTypeInfo *typeInfo;
456
457         if(!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
458                 typeInfo = layerType_getInfo(layer->type);
459
460                 if(typeInfo->free)
461                         typeInfo->free(layer->data, totelem, typeInfo->size);
462
463                 if(layer->data)
464                         MEM_freeN(layer->data);
465         }
466 }
467
468 void CustomData_free(CustomData *data, int totelem)
469 {
470         int i;
471
472         for(i = 0; i < data->totlayer; ++i)
473                 customData_free_layer__internal(&data->layers[i], totelem);
474
475         if(data->layers)
476                 MEM_freeN(data->layers);
477         
478         memset(data, 0, sizeof(*data));
479 }
480
481 static void customData_update_offsets(CustomData *data)
482 {
483         const LayerTypeInfo *typeInfo;
484         int i, offset = 0;
485
486         for(i = 0; i < data->totlayer; ++i) {
487                 typeInfo = layerType_getInfo(data->layers[i].type);
488
489                 data->layers[i].offset = offset;
490                 offset += typeInfo->size;
491         }
492
493         data->totsize = offset;
494 }
495
496 int CustomData_get_layer_index(const CustomData *data, int type)
497 {
498         int i; 
499
500         for(i=0; i < data->totlayer; ++i)
501                 if(data->layers[i].type == type)
502                         return i;
503
504         return -1;
505 }
506
507 int CustomData_get_named_layer_index(const CustomData *data, int type, char *name)
508 {
509         int i;
510
511         for(i=0; i < data->totlayer; ++i)
512                 if(data->layers[i].type == type && strcmp(data->layers[i].name, name)==0)
513                         return i;
514
515         return -1;
516 }
517
518 int CustomData_get_active_layer_index(const CustomData *data, int type)
519 {
520         int i;
521
522         for(i=0; i < data->totlayer; ++i)
523                 if(data->layers[i].type == type)
524                         return i + data->layers[i].active;
525
526         return -1;
527 }
528
529 void CustomData_set_layer_active(CustomData *data, int type, int n)
530 {
531         int i;
532
533         for(i=0; i < data->totlayer; ++i)
534                 if(data->layers[i].type == type)
535                         data->layers[i].active = n;
536 }
537
538 void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
539 {
540         int i;
541
542         for(i=0; i < data->totlayer; ++i)
543                 if(data->layers[i].type == type)
544                         data->layers[i].flag |= flag;
545 }
546
547 static int customData_resize(CustomData *data, int amount)
548 {
549         CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
550                                        "CustomData->layers");
551         if(!tmp) return 0;
552
553         data->maxlayer += amount;
554         if (data->layers) {
555                 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
556                 MEM_freeN(data->layers);
557         }
558         data->layers = tmp;
559
560         return 1;
561 }
562
563 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
564         int type, int alloctype, void *layerdata, int totelem, const char *name)
565 {
566         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
567         int size = typeInfo->size * totelem, flag = 0, index = data->totlayer;
568         void *newlayerdata;
569
570         if (!typeInfo->defaultname && CustomData_has_layer(data, type))
571                 return &data->layers[CustomData_get_layer_index(data, type)];
572
573         if((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
574                 newlayerdata = layerdata;
575         }
576         else {
577                 newlayerdata = MEM_callocN(size, layerType_getName(type));
578                 if(!newlayerdata)
579                         return NULL;
580         }
581
582         if (alloctype == CD_DUPLICATE) {
583                 if(typeInfo->copy)
584                         typeInfo->copy(layerdata, newlayerdata, totelem);
585                 else
586                         memcpy(newlayerdata, layerdata, size);
587         }
588         else if (alloctype == CD_DEFAULT) {
589                 if(typeInfo->set_default)
590                         typeInfo->set_default((char*)newlayerdata, totelem);
591         }
592         else if (alloctype == CD_REFERENCE)
593                 flag |= CD_FLAG_NOFREE;
594
595         if(index >= data->maxlayer) {
596                 if(!customData_resize(data, CUSTOMDATA_GROW)) {
597                         if(newlayerdata != layerdata)
598                                 MEM_freeN(newlayerdata);
599                         return NULL;
600                 }
601         }
602         
603         data->totlayer++;
604
605         /* keep layers ordered by type */
606         for( ; index > 0 && data->layers[index - 1].type > type; --index)
607                 data->layers[index] = data->layers[index - 1];
608
609         data->layers[index].type = type;
610         data->layers[index].flag = flag;
611         data->layers[index].data = newlayerdata;
612
613         if(name) {
614                 strcpy(data->layers[index].name, name);
615                 CustomData_set_layer_unique_name(data, index);
616         }
617         else
618                 data->layers[index].name[0] = '\0';
619
620         if(index > 0 && data->layers[index-1].type == type)
621                 data->layers[index].active = data->layers[index-1].active;
622         else
623                 data->layers[index].active = 0;
624
625         customData_update_offsets(data);
626
627         return &data->layers[index];
628 }
629
630 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
631                            void *layerdata, int totelem)
632 {
633         CustomDataLayer *layer;
634         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
635         
636         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
637                                                totelem, typeInfo->defaultname);
638
639         if(layer)
640                 return layer->data;
641
642         return NULL;
643 }
644
645 /*same as above but accepts a name*/
646 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
647                            void *layerdata, int totelem, char *name)
648 {
649         CustomDataLayer *layer;
650         
651         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
652                                                totelem, name);
653
654         if(layer)
655                 return layer->data;
656
657         return NULL;
658 }
659
660
661 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
662 {
663         int i;
664         CustomDataLayer *layer;
665         
666         if (index < 0) return 0;
667
668         layer = &data->layers[index];
669
670         customData_free_layer__internal(&data->layers[index], totelem);
671
672         for (i=index+1; i < data->totlayer; ++i)
673                 data->layers[i-1] = data->layers[i];
674
675         data->totlayer--;
676
677         /* if layer was last of type in array, set new active layer */
678         if ((index >= data->totlayer) || (data->layers[index].type != type)) {
679                 i = CustomData_get_layer_index(data, type);
680                 
681                 if (i >= 0)
682                         for (; i < data->totlayer && data->layers[i].type == type; i++)
683                                 data->layers[i].active--;
684         }
685
686         if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
687                 customData_resize(data, -CUSTOMDATA_GROW);
688
689         customData_update_offsets(data);
690
691         return 1;
692 }
693
694 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
695 {
696         int index = 0;
697         index = CustomData_get_active_layer_index(data, type);
698         if (index < 0) return 0;
699         return CustomData_free_layer(data, type, totelem, index);
700 }
701
702
703 void CustomData_free_layers(CustomData *data, int type, int totelem)
704 {
705         while (CustomData_has_layer(data, type))
706                 CustomData_free_layer_active(data, type, totelem);
707 }
708
709 int CustomData_has_layer(const CustomData *data, int type)
710 {
711         return (CustomData_get_layer_index(data, type) != -1);
712 }
713
714 int CustomData_number_of_layers(const CustomData *data, int type)
715 {
716         int i, number = 0;
717
718         for(i = 0; i < data->totlayer; i++)
719                 if(data->layers[i].type == type)
720                         number++;
721         
722         return number;
723 }
724
725 void *CustomData_duplicate_referenced_layer(struct CustomData *data, int type)
726 {
727         CustomDataLayer *layer;
728         int layer_index;
729
730         /* get the layer index of the first layer of type */
731         layer_index = CustomData_get_active_layer_index(data, type);
732         if(layer_index < 0) return NULL;
733
734         layer = &data->layers[layer_index];
735
736         if (layer->flag & CD_FLAG_NOFREE) {
737                 layer->data = MEM_dupallocN(layer->data);
738                 layer->flag &= ~CD_FLAG_NOFREE;
739         }
740
741         return layer->data;
742 }
743
744 void CustomData_free_temporary(CustomData *data, int totelem)
745 {
746         CustomDataLayer *layer;
747         int i, j;
748
749         for(i = 0, j = 0; i < data->totlayer; ++i) {
750                 layer = &data->layers[i];
751
752                 if (i != j)
753                         data->layers[j] = data->layers[i];
754
755                 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
756                         customData_free_layer__internal(layer, totelem);
757                 else
758                         j++;
759         }
760
761         data->totlayer = j;
762
763         if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
764                 customData_resize(data, -CUSTOMDATA_GROW);
765
766         customData_update_offsets(data);
767 }
768
769 void CustomData_set_only_copy(const struct CustomData *data,
770                               CustomDataMask mask)
771 {
772         int i;
773
774         for(i = 0; i < data->totlayer; ++i)
775                 if(!(mask & (1 << data->layers[i].type)))
776                         data->layers[i].flag |= CD_FLAG_NOCOPY;
777 }
778
779 void CustomData_copy_data(const CustomData *source, CustomData *dest,
780                           int source_index, int dest_index, int count)
781 {
782         const LayerTypeInfo *typeInfo;
783         int src_i, dest_i;
784         int src_offset;
785         int dest_offset;
786
787         /* copies a layer at a time */
788         dest_i = 0;
789         for(src_i = 0; src_i < source->totlayer; ++src_i) {
790
791                 /* find the first dest layer with type >= the source type
792                  * (this should work because layers are ordered by type)
793                  */
794                 while(dest_i < dest->totlayer
795                       && dest->layers[dest_i].type < source->layers[src_i].type)
796                         ++dest_i;
797
798                 /* if there are no more dest layers, we're done */
799                 if(dest_i >= dest->totlayer) return;
800
801                 /* if we found a matching layer, copy the data */
802                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
803                         char *src_data = source->layers[src_i].data;
804                         char *dest_data = dest->layers[dest_i].data;
805
806                         typeInfo = layerType_getInfo(source->layers[src_i].type);
807
808                         src_offset = source_index * typeInfo->size;
809                         dest_offset = dest_index * typeInfo->size;
810
811                         if(typeInfo->copy)
812                                 typeInfo->copy(src_data + src_offset,
813                                                 dest_data + dest_offset,
814                                                 count);
815                         else
816                                 memcpy(dest_data + dest_offset,
817                                        src_data + src_offset,
818                                        count * typeInfo->size);
819
820                         /* if there are multiple source & dest layers of the same type,
821                          * we don't want to copy all source layers to the same dest, so
822                          * increment dest_i
823                          */
824                         ++dest_i;
825                 }
826         }
827 }
828
829 void CustomData_free_elem(CustomData *data, int index, int count)
830 {
831         int i;
832         const LayerTypeInfo *typeInfo;
833
834         for(i = 0; i < data->totlayer; ++i) {
835                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
836                         typeInfo = layerType_getInfo(data->layers[i].type);
837
838                         if(typeInfo->free) {
839                                 int offset = typeInfo->size * index;
840
841                                 typeInfo->free((char *)data->layers[i].data + offset,
842                                                count, typeInfo->size);
843                         }
844                 }
845         }
846 }
847
848 #define SOURCE_BUF_SIZE 100
849
850 void CustomData_interp(const CustomData *source, CustomData *dest,
851                        int *src_indices, float *weights, float *sub_weights,
852                        int count, int dest_index)
853 {
854         int src_i, dest_i;
855         int dest_offset;
856         int j;
857         void *source_buf[SOURCE_BUF_SIZE];
858         void **sources = source_buf;
859
860         /* slow fallback in case we're interpolating a ridiculous number of
861          * elements
862          */
863         if(count > SOURCE_BUF_SIZE)
864                 sources = MEM_callocN(sizeof(*sources) * count,
865                                       "CustomData_interp sources");
866
867         /* interpolates a layer at a time */
868         dest_i = 0;
869         for(src_i = 0; src_i < source->totlayer; ++src_i) {
870                 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
871                 if(!typeInfo->interp) continue;
872
873                 /* find the first dest layer with type >= the source type
874                  * (this should work because layers are ordered by type)
875                  */
876                 while(dest_i < dest->totlayer
877                       && dest->layers[dest_i].type < source->layers[src_i].type)
878                         ++dest_i;
879
880                 /* if there are no more dest layers, we're done */
881                 if(dest_i >= dest->totlayer) return;
882
883                 /* if we found a matching layer, copy the data */
884                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
885                         void *src_data = source->layers[src_i].data;
886
887                         for(j = 0; j < count; ++j)
888                                 sources[j] = (char *)src_data
889                                                          + typeInfo->size * src_indices[j];
890
891                         dest_offset = dest_index * typeInfo->size;
892
893                         typeInfo->interp(sources, weights, sub_weights, count,
894                                                    (char *)dest->layers[dest_i].data + dest_offset);
895
896                         /* if there are multiple source & dest layers of the same type,
897                          * we don't want to copy all source layers to the same dest, so
898                          * increment dest_i
899                          */
900                         ++dest_i;
901                 }
902         }
903
904         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
905 }
906
907 void CustomData_swap(struct CustomData *data, int index, int *corner_indices)
908 {
909         const LayerTypeInfo *typeInfo;
910         int i;
911
912         for(i = 0; i < data->totlayer; ++i) {
913                 typeInfo = layerType_getInfo(data->layers[i].type);
914
915                 if(typeInfo->swap) {
916                         int offset = typeInfo->size * index;
917
918                         typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
919                 }
920         }
921 }
922
923 void *CustomData_get(const CustomData *data, int index, int type)
924 {
925         int offset;
926         int layer_index;
927         
928         /* get the layer index of the active layer of type */
929         layer_index = CustomData_get_active_layer_index(data, type);
930         if(layer_index < 0) return NULL;
931
932         /* get the offset of the desired element */
933         offset = layerType_getInfo(type)->size * index;
934
935         return (char *)data->layers[layer_index].data + offset;
936 }
937
938 void *CustomData_get_layer(const CustomData *data, int type)
939 {
940         /* get the layer index of the active layer of type */
941         int layer_index = CustomData_get_active_layer_index(data, type);
942         if(layer_index < 0) return NULL;
943
944         return data->layers[layer_index].data;
945 }
946
947 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
948 {
949         /* get the layer index of the active layer of type */
950         int layer_index = CustomData_get_layer_index(data, type);
951         if(layer_index < 0) return NULL;
952
953         return data->layers[layer_index+n].data;
954 }
955
956 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
957 {
958         /* get the layer index of the first layer of type */
959         int layer_index = CustomData_get_active_layer_index(data, type);
960
961         if(layer_index < 0) return NULL;
962
963         data->layers[layer_index].data = ptr;
964
965         return ptr;
966 }
967
968 void CustomData_set(const CustomData *data, int index, int type, void *source)
969 {
970         void *dest = CustomData_get(data, index, type);
971         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
972
973         if(!dest) return;
974
975         if(typeInfo->copy)
976                 typeInfo->copy(source, dest, 1);
977         else
978                 memcpy(dest, source, typeInfo->size);
979 }
980
981 /* EditMesh functions */
982
983 void CustomData_em_free_block(CustomData *data, void **block)
984 {
985     const LayerTypeInfo *typeInfo;
986     int i;
987
988         if(!*block) return;
989
990     for(i = 0; i < data->totlayer; ++i) {
991         if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
992             typeInfo = layerType_getInfo(data->layers[i].type);
993
994             if(typeInfo->free) {
995                                 int offset = data->layers[i].offset;
996                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
997                         }
998         }
999     }
1000
1001         MEM_freeN(*block);
1002         *block = NULL;
1003 }
1004
1005 static void CustomData_em_alloc_block(CustomData *data, void **block)
1006 {
1007         /* TODO: optimize free/alloc */
1008
1009         if (*block)
1010                 CustomData_em_free_block(data, block);
1011
1012         if (data->totsize > 0)
1013                 *block = MEM_callocN(data->totsize, "CustomData EM block");
1014         else
1015                 *block = NULL;
1016 }
1017
1018 void CustomData_em_copy_data(const CustomData *source, CustomData *dest,
1019                             void *src_block, void **dest_block)
1020 {
1021         const LayerTypeInfo *typeInfo;
1022         int dest_i, src_i;
1023
1024         if (!*dest_block)
1025                 CustomData_em_alloc_block(dest, dest_block);
1026         
1027         /* copies a layer at a time */
1028         dest_i = 0;
1029         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1030
1031                 /* find the first dest layer with type >= the source type
1032                  * (this should work because layers are ordered by type)
1033                  */
1034                 while(dest_i < dest->totlayer
1035                       && dest->layers[dest_i].type < source->layers[src_i].type)
1036                         ++dest_i;
1037
1038                 /* if there are no more dest layers, we're done */
1039                 if(dest_i >= dest->totlayer) return;
1040
1041                 /* if we found a matching layer, copy the data */
1042                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1043                         char *src_data = (char*)src_block + source->layers[src_i].offset;
1044                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
1045
1046                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1047
1048                         if(typeInfo->copy)
1049                                 typeInfo->copy(src_data, dest_data, 1);
1050                         else
1051                                 memcpy(dest_data, src_data, typeInfo->size);
1052
1053                         /* if there are multiple source & dest layers of the same type,
1054                          * we don't want to copy all source layers to the same dest, so
1055                          * increment dest_i
1056                          */
1057                         ++dest_i;
1058                 }
1059         }
1060 }
1061
1062 void *CustomData_em_get(const CustomData *data, void *block, int type)
1063 {
1064         int layer_index;
1065         
1066         /* get the layer index of the first layer of type */
1067         layer_index = CustomData_get_active_layer_index(data, type);
1068         if(layer_index < 0) return NULL;
1069
1070         return (char *)block + data->layers[layer_index].offset;
1071 }
1072
1073 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
1074 {
1075         void *dest = CustomData_em_get(data, block, type);
1076         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1077
1078         if(!dest) return;
1079
1080         if(typeInfo->copy)
1081                 typeInfo->copy(source, dest, 1);
1082         else
1083                 memcpy(dest, source, typeInfo->size);
1084 }
1085
1086 void CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
1087                           float *sub_weights, int count, void *dest_block)
1088 {
1089         int i, j;
1090         void *source_buf[SOURCE_BUF_SIZE];
1091         void **sources = source_buf;
1092
1093         /* slow fallback in case we're interpolating a ridiculous number of
1094          * elements
1095          */
1096         if(count > SOURCE_BUF_SIZE)
1097                 sources = MEM_callocN(sizeof(*sources) * count,
1098                                       "CustomData_interp sources");
1099
1100         /* interpolates a layer at a time */
1101         for(i = 0; i < data->totlayer; ++i) {
1102                 CustomDataLayer *layer = &data->layers[i];
1103                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1104
1105                 if(typeInfo->interp) {
1106                         for(j = 0; j < count; ++j)
1107                                 sources[j] = (char *)src_blocks[j] + layer->offset;
1108
1109                         typeInfo->interp(sources, weights, sub_weights, count,
1110                                           (char *)dest_block + layer->offset);
1111                 }
1112         }
1113
1114         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1115 }
1116
1117 void CustomData_em_set_default(CustomData *data, void **block)
1118 {
1119         const LayerTypeInfo *typeInfo;
1120         int i;
1121
1122         if (!*block)
1123                 CustomData_em_alloc_block(data, block);
1124
1125         for(i = 0; i < data->totlayer; ++i) {
1126                 int offset = data->layers[i].offset;
1127
1128                 typeInfo = layerType_getInfo(data->layers[i].type);
1129
1130                 if(typeInfo->set_default)
1131                         typeInfo->set_default((char*)*block + offset, 1);
1132         }
1133 }
1134
1135 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
1136                             int src_index, void **dest_block)
1137 {
1138         const LayerTypeInfo *typeInfo;
1139         int dest_i, src_i, src_offset;
1140
1141         if (!*dest_block)
1142                 CustomData_em_alloc_block(dest, dest_block);
1143         
1144         /* copies a layer at a time */
1145         dest_i = 0;
1146         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1147
1148                 /* find the first dest layer with type >= the source type
1149                  * (this should work because layers are ordered by type)
1150                  */
1151                 while(dest_i < dest->totlayer
1152                       && dest->layers[dest_i].type < source->layers[src_i].type)
1153                         ++dest_i;
1154
1155                 /* if there are no more dest layers, we're done */
1156                 if(dest_i >= dest->totlayer) return;
1157
1158                 /* if we found a matching layer, copy the data */
1159                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1160                         int offset = dest->layers[dest_i].offset;
1161                         char *src_data = source->layers[src_i].data;
1162                         char *dest_data = (char*)*dest_block + offset;
1163
1164                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1165                         src_offset = src_index * typeInfo->size;
1166
1167                         if(typeInfo->copy)
1168                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
1169                         else
1170                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
1171
1172                         /* if there are multiple source & dest layers of the same type,
1173                          * we don't want to copy all source layers to the same dest, so
1174                          * increment dest_i
1175                          */
1176                         ++dest_i;
1177                 }
1178         }
1179 }
1180
1181 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
1182                               void *src_block, int dest_index)
1183 {
1184         const LayerTypeInfo *typeInfo;
1185         int dest_i, src_i, dest_offset;
1186
1187         /* copies a layer at a time */
1188         dest_i = 0;
1189         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1190
1191                 /* find the first dest layer with type >= the source type
1192                  * (this should work because layers are ordered by type)
1193                  */
1194                 while(dest_i < dest->totlayer
1195                       && dest->layers[dest_i].type < source->layers[src_i].type)
1196                         ++dest_i;
1197
1198                 /* if there are no more dest layers, we're done */
1199                 if(dest_i >= dest->totlayer) return;
1200
1201                 /* if we found a matching layer, copy the data */
1202                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1203                         int offset = source->layers[src_i].offset;
1204                         char *src_data = (char*)src_block + offset;
1205                         char *dest_data = dest->layers[dest_i].data;
1206
1207                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1208                         dest_offset = dest_index * typeInfo->size;
1209
1210                         if(typeInfo->copy)
1211                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
1212                         else
1213                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
1214
1215                         /* if there are multiple source & dest layers of the same type,
1216                          * we don't want to copy all source layers to the same dest, so
1217                          * increment dest_i
1218                          */
1219                         ++dest_i;
1220                 }
1221         }
1222
1223 }
1224
1225 void CustomData_file_write_info(int type, char **structname, int *structnum)
1226 {
1227         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1228
1229         *structname = typeInfo->structname;
1230         *structnum = typeInfo->structnum;
1231 }
1232
1233 int CustomData_sizeof(int type)
1234 {
1235         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1236
1237         return typeInfo->size;
1238 }
1239
1240 const char *CustomData_layertype_name(int type)
1241 {
1242         return layerType_getName(type);
1243 }
1244
1245 void CustomData_set_layer_unique_name(CustomData *data, int index)
1246 {
1247         char tempname[64];
1248         int number, i, type;
1249         char *dot, *name;
1250         CustomDataLayer *layer, *nlayer= &data->layers[index];
1251         const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
1252
1253         if (!typeInfo->defaultname)
1254                 return;
1255
1256         type = nlayer->type;
1257         name = nlayer->name;
1258
1259         if (name[0] == '\0')
1260                 BLI_strncpy(nlayer->name, typeInfo->defaultname, sizeof(nlayer->name));
1261         
1262         /* see if there is a duplicate */
1263         for(i=0; i<data->totlayer; i++) {
1264                 layer = &data->layers[i];
1265
1266                 if(i!=index && layer->type==type && strcmp(layer->name, name)==0)
1267                         break;
1268         }
1269
1270         if(i == data->totlayer)
1271                 return;
1272
1273         /* strip off the suffix */
1274         dot = strchr(nlayer->name, '.');
1275         if(dot) *dot=0;
1276         
1277         for(number=1; number <=999; number++) {
1278                 sprintf(tempname, "%s.%03d", nlayer->name, number);
1279
1280                 for(i=0; i<data->totlayer; i++) {
1281                         layer = &data->layers[i];
1282                         
1283                         if(i!=index && layer->type==type && strcmp(layer->name, tempname)==0)
1284                                 break;
1285                 }
1286
1287                 if(i == data->totlayer) {
1288                         BLI_strncpy(nlayer->name, tempname, sizeof(nlayer->name));
1289                         return;
1290                 }
1291         }       
1292 }
1293
1294 int CustomData_verify_versions(struct CustomData *data, int index)
1295 {
1296         const LayerTypeInfo *typeInfo;
1297         CustomDataLayer *layer = &data->layers[index];
1298         int i, keeplayer = 1;
1299
1300         if (layer->type >= CD_NUMTYPES) {
1301                 keeplayer = 0; /* unknown layer type from future version */
1302         }
1303         else {
1304                 typeInfo = layerType_getInfo(layer->type);
1305
1306                 if (!typeInfo->defaultname && (index > 0) &&
1307                         data->layers[index-1].type == layer->type)
1308                         keeplayer = 0; /* multiple layers of which we only support one */
1309         }
1310
1311         if (!keeplayer) {
1312             for (i=index+1; i < data->totlayer; ++i)
1313             data->layers[i-1] = data->layers[i];
1314                 data->totlayer--;
1315         }
1316
1317         return keeplayer;
1318 }
1319