e93a58bc9510e496b2c5b0f79acd60c40c8335de
[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((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
571                 newlayerdata = layerdata;
572         }
573         else {
574                 newlayerdata = MEM_callocN(size, layerType_getName(type));
575                 if(!newlayerdata)
576                         return NULL;
577         }
578
579         if (alloctype == CD_DUPLICATE) {
580                 if(typeInfo->copy)
581                         typeInfo->copy(layerdata, newlayerdata, totelem);
582                 else
583                         memcpy(newlayerdata, layerdata, size);
584         }
585         else if (alloctype == CD_DEFAULT) {
586                 if(typeInfo->set_default)
587                         typeInfo->set_default((char*)newlayerdata, totelem);
588         }
589         else if (alloctype == CD_REFERENCE)
590                 flag |= CD_FLAG_NOFREE;
591
592         if(index >= data->maxlayer) {
593                 if(!customData_resize(data, CUSTOMDATA_GROW)) {
594                         if(newlayerdata != layerdata)
595                                 MEM_freeN(newlayerdata);
596                         return NULL;
597                 }
598         }
599         
600         data->totlayer++;
601
602         /* keep layers ordered by type */
603         for( ; index > 0 && data->layers[index - 1].type > type; --index)
604                 data->layers[index] = data->layers[index - 1];
605
606         data->layers[index].type = type;
607         data->layers[index].flag = flag;
608         data->layers[index].data = newlayerdata;
609
610         if(name) {
611                 strcpy(data->layers[index].name, name);
612                 CustomData_set_layer_unique_name(data, index);
613         }
614         else
615                 data->layers[index].name[0] = '\0';
616
617         if(index > 0 && data->layers[index-1].type == type)
618                 data->layers[index].active = data->layers[index-1].active;
619         else
620                 data->layers[index].active = 0;
621
622         customData_update_offsets(data);
623
624         return &data->layers[index];
625 }
626
627 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
628                            void *layerdata, int totelem)
629 {
630         CustomDataLayer *layer;
631         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
632         
633         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
634                                                totelem, typeInfo->defaultname);
635
636         if(layer)
637                 return layer->data;
638
639         return NULL;
640 }
641
642 /*same as above but accepts a name*/
643 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
644                            void *layerdata, int totelem, char *name)
645 {
646         CustomDataLayer *layer;
647         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
648         
649         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
650                                                totelem, name);
651
652         if(layer)
653                 return layer->data;
654
655         return NULL;
656 }
657
658
659 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
660 {
661         int i;
662         CustomDataLayer *layer;
663         
664         if (index < 0) return 0;
665
666         layer = &data->layers[index];
667
668         customData_free_layer__internal(&data->layers[index], totelem);
669
670         for (i=index+1; i < data->totlayer; ++i)
671                 data->layers[i-1] = data->layers[i];
672
673         data->totlayer--;
674
675         /* if layer was last of type in array, set new active layer */
676         if ((index >= data->totlayer) || (data->layers[index].type != type)) {
677                 i = CustomData_get_layer_index(data, type);
678                 
679                 if (i >= 0)
680                         for (; i < data->totlayer && data->layers[i].type == type; i++)
681                                 data->layers[i].active--;
682         }
683
684         if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
685                 customData_resize(data, -CUSTOMDATA_GROW);
686
687         customData_update_offsets(data);
688
689         return 1;
690 }
691
692 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
693 {
694         int index = 0;
695         index = CustomData_get_active_layer_index(data, type);
696         if (index < 0) return 0;
697         return CustomData_free_layer(data, type, totelem, index);
698 }
699
700
701 void CustomData_free_layers(CustomData *data, int type, int totelem)
702 {
703         while (CustomData_has_layer(data, type))
704                 CustomData_free_layer_active(data, type, totelem);
705 }
706
707 int CustomData_has_layer(const CustomData *data, int type)
708 {
709         return (CustomData_get_layer_index(data, type) != -1);
710 }
711
712 int CustomData_number_of_layers(const CustomData *data, int type)
713 {
714         int i, number = 0;
715
716         for(i = 0; i < data->totlayer; i++)
717                 if(data->layers[i].type == type)
718                         number++;
719         
720         return number;
721 }
722
723 void *CustomData_duplicate_referenced_layer(struct CustomData *data, int type)
724 {
725         CustomDataLayer *layer;
726         int layer_index;
727
728         /* get the layer index of the first layer of type */
729         layer_index = CustomData_get_active_layer_index(data, type);
730         if(layer_index < 0) return NULL;
731
732         layer = &data->layers[layer_index];
733
734         if (layer->flag & CD_FLAG_NOFREE) {
735                 layer->data = MEM_dupallocN(layer->data);
736                 layer->flag &= ~CD_FLAG_NOFREE;
737         }
738
739         return layer->data;
740 }
741
742 void CustomData_free_temporary(CustomData *data, int totelem)
743 {
744         CustomDataLayer *layer;
745         int i, j;
746
747         for(i = 0, j = 0; i < data->totlayer; ++i) {
748                 layer = &data->layers[i];
749
750                 if (i != j)
751                         data->layers[j] = data->layers[i];
752
753                 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
754                         customData_free_layer__internal(layer, totelem);
755                 else
756                         j++;
757         }
758
759         data->totlayer = j;
760
761         if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
762                 customData_resize(data, -CUSTOMDATA_GROW);
763
764         customData_update_offsets(data);
765 }
766
767 void CustomData_set_only_copy(const struct CustomData *data,
768                               CustomDataMask mask)
769 {
770         int i;
771
772         for(i = 0; i < data->totlayer; ++i)
773                 if(!(mask & (1 << data->layers[i].type)))
774                         data->layers[i].flag |= CD_FLAG_NOCOPY;
775 }
776
777 void CustomData_copy_data(const CustomData *source, CustomData *dest,
778                           int source_index, int dest_index, int count)
779 {
780         const LayerTypeInfo *typeInfo;
781         int src_i, dest_i;
782         int src_offset;
783         int dest_offset;
784
785         /* copies a layer at a time */
786         dest_i = 0;
787         for(src_i = 0; src_i < source->totlayer; ++src_i) {
788
789                 /* find the first dest layer with type >= the source type
790                  * (this should work because layers are ordered by type)
791                  */
792                 while(dest_i < dest->totlayer
793                       && dest->layers[dest_i].type < source->layers[src_i].type)
794                         ++dest_i;
795
796                 /* if there are no more dest layers, we're done */
797                 if(dest_i >= dest->totlayer) return;
798
799                 /* if we found a matching layer, copy the data */
800                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
801                         char *src_data = source->layers[src_i].data;
802                         char *dest_data = dest->layers[dest_i].data;
803
804                         typeInfo = layerType_getInfo(source->layers[src_i].type);
805
806                         src_offset = source_index * typeInfo->size;
807                         dest_offset = dest_index * typeInfo->size;
808
809                         if(typeInfo->copy)
810                                 typeInfo->copy(src_data + src_offset,
811                                                 dest_data + dest_offset,
812                                                 count);
813                         else
814                                 memcpy(dest_data + dest_offset,
815                                        src_data + src_offset,
816                                        count * typeInfo->size);
817
818                         /* if there are multiple source & dest layers of the same type,
819                          * we don't want to copy all source layers to the same dest, so
820                          * increment dest_i
821                          */
822                         ++dest_i;
823                 }
824         }
825 }
826
827 void CustomData_free_elem(CustomData *data, int index, int count)
828 {
829         int i;
830         const LayerTypeInfo *typeInfo;
831
832         for(i = 0; i < data->totlayer; ++i) {
833                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
834                         typeInfo = layerType_getInfo(data->layers[i].type);
835
836                         if(typeInfo->free) {
837                                 int offset = typeInfo->size * index;
838
839                                 typeInfo->free((char *)data->layers[i].data + offset,
840                                                count, typeInfo->size);
841                         }
842                 }
843         }
844 }
845
846 #define SOURCE_BUF_SIZE 100
847
848 void CustomData_interp(const CustomData *source, CustomData *dest,
849                        int *src_indices, float *weights, float *sub_weights,
850                        int count, int dest_index)
851 {
852         int src_i, dest_i;
853         int dest_offset;
854         int j;
855         void *source_buf[SOURCE_BUF_SIZE];
856         void **sources = source_buf;
857
858         /* slow fallback in case we're interpolating a ridiculous number of
859          * elements
860          */
861         if(count > SOURCE_BUF_SIZE)
862                 sources = MEM_callocN(sizeof(*sources) * count,
863                                       "CustomData_interp sources");
864
865         /* interpolates a layer at a time */
866         dest_i = 0;
867         for(src_i = 0; src_i < source->totlayer; ++src_i) {
868                 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
869                 if(!typeInfo->interp) continue;
870
871                 /* find the first dest layer with type >= the source type
872                  * (this should work because layers are ordered by type)
873                  */
874                 while(dest_i < dest->totlayer
875                       && dest->layers[dest_i].type < source->layers[src_i].type)
876                         ++dest_i;
877
878                 /* if there are no more dest layers, we're done */
879                 if(dest_i >= dest->totlayer) return;
880
881                 /* if we found a matching layer, copy the data */
882                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
883                         void *src_data = source->layers[src_i].data;
884
885                         for(j = 0; j < count; ++j)
886                                 sources[j] = (char *)src_data
887                                                          + typeInfo->size * src_indices[j];
888
889                         dest_offset = dest_index * typeInfo->size;
890
891                         typeInfo->interp(sources, weights, sub_weights, count,
892                                                    (char *)dest->layers[dest_i].data + dest_offset);
893
894                         /* if there are multiple source & dest layers of the same type,
895                          * we don't want to copy all source layers to the same dest, so
896                          * increment dest_i
897                          */
898                         ++dest_i;
899                 }
900         }
901
902         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
903 }
904
905 void CustomData_swap(struct CustomData *data, int index, int *corner_indices)
906 {
907         const LayerTypeInfo *typeInfo;
908         int i;
909
910         for(i = 0; i < data->totlayer; ++i) {
911                 typeInfo = layerType_getInfo(data->layers[i].type);
912
913                 if(typeInfo->swap) {
914                         int offset = typeInfo->size * index;
915
916                         typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
917                 }
918         }
919 }
920
921 void *CustomData_get(const CustomData *data, int index, int type)
922 {
923         int offset;
924         int layer_index;
925         
926         /* get the layer index of the active layer of type */
927         layer_index = CustomData_get_active_layer_index(data, type);
928         if(layer_index < 0) return NULL;
929
930         /* get the offset of the desired element */
931         offset = layerType_getInfo(type)->size * index;
932
933         return (char *)data->layers[layer_index].data + offset;
934 }
935
936 void *CustomData_get_layer(const CustomData *data, int type)
937 {
938         /* get the layer index of the active layer of type */
939         int layer_index = CustomData_get_active_layer_index(data, type);
940         if(layer_index < 0) return NULL;
941
942         return data->layers[layer_index].data;
943 }
944
945 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
946 {
947         /* get the layer index of the active layer of type */
948         int layer_index = CustomData_get_layer_index(data, type);
949         if(layer_index < 0) return NULL;
950
951         return data->layers[layer_index+n].data;
952 }
953
954 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
955 {
956         /* get the layer index of the first layer of type */
957         int layer_index = CustomData_get_active_layer_index(data, type);
958
959         if(layer_index < 0) return NULL;
960
961         data->layers[layer_index].data = ptr;
962
963         return ptr;
964 }
965
966 void CustomData_set(const CustomData *data, int index, int type, void *source)
967 {
968         void *dest = CustomData_get(data, index, type);
969         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
970
971         if(!dest) return;
972
973         if(typeInfo->copy)
974                 typeInfo->copy(source, dest, 1);
975         else
976                 memcpy(dest, source, typeInfo->size);
977 }
978
979 /* EditMesh functions */
980
981 void CustomData_em_free_block(CustomData *data, void **block)
982 {
983     const LayerTypeInfo *typeInfo;
984     int i;
985
986         if(!*block) return;
987
988     for(i = 0; i < data->totlayer; ++i) {
989         if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
990             typeInfo = layerType_getInfo(data->layers[i].type);
991
992             if(typeInfo->free) {
993                                 int offset = data->layers[i].offset;
994                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
995                         }
996         }
997     }
998
999         MEM_freeN(*block);
1000         *block = NULL;
1001 }
1002
1003 static void CustomData_em_alloc_block(CustomData *data, void **block)
1004 {
1005         /* TODO: optimize free/alloc */
1006
1007         if (*block)
1008                 CustomData_em_free_block(data, block);
1009
1010         if (data->totsize > 0)
1011                 *block = MEM_callocN(data->totsize, "CustomData EM block");
1012         else
1013                 *block = NULL;
1014 }
1015
1016 void CustomData_em_copy_data(const CustomData *source, CustomData *dest,
1017                             void *src_block, void **dest_block)
1018 {
1019         const LayerTypeInfo *typeInfo;
1020         int dest_i, src_i;
1021
1022         if (!*dest_block)
1023                 CustomData_em_alloc_block(dest, dest_block);
1024         
1025         /* copies a layer at a time */
1026         dest_i = 0;
1027         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1028
1029                 /* find the first dest layer with type >= the source type
1030                  * (this should work because layers are ordered by type)
1031                  */
1032                 while(dest_i < dest->totlayer
1033                       && dest->layers[dest_i].type < source->layers[src_i].type)
1034                         ++dest_i;
1035
1036                 /* if there are no more dest layers, we're done */
1037                 if(dest_i >= dest->totlayer) return;
1038
1039                 /* if we found a matching layer, copy the data */
1040                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1041                         char *src_data = (char*)src_block + source->layers[src_i].offset;
1042                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
1043
1044                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1045
1046                         if(typeInfo->copy)
1047                                 typeInfo->copy(src_data, dest_data, 1);
1048                         else
1049                                 memcpy(dest_data, src_data, typeInfo->size);
1050
1051                         /* if there are multiple source & dest layers of the same type,
1052                          * we don't want to copy all source layers to the same dest, so
1053                          * increment dest_i
1054                          */
1055                         ++dest_i;
1056                 }
1057         }
1058 }
1059
1060 void *CustomData_em_get(const CustomData *data, void *block, int type)
1061 {
1062         int layer_index;
1063         
1064         /* get the layer index of the first layer of type */
1065         layer_index = CustomData_get_active_layer_index(data, type);
1066         if(layer_index < 0) return NULL;
1067
1068         return (char *)block + data->layers[layer_index].offset;
1069 }
1070
1071 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
1072 {
1073         void *dest = CustomData_em_get(data, block, type);
1074         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1075
1076         if(!dest) return;
1077
1078         if(typeInfo->copy)
1079                 typeInfo->copy(source, dest, 1);
1080         else
1081                 memcpy(dest, source, typeInfo->size);
1082 }
1083
1084 void CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
1085                           float *sub_weights, int count, void *dest_block)
1086 {
1087         int i, j;
1088         void *source_buf[SOURCE_BUF_SIZE];
1089         void **sources = source_buf;
1090
1091         /* slow fallback in case we're interpolating a ridiculous number of
1092          * elements
1093          */
1094         if(count > SOURCE_BUF_SIZE)
1095                 sources = MEM_callocN(sizeof(*sources) * count,
1096                                       "CustomData_interp sources");
1097
1098         /* interpolates a layer at a time */
1099         for(i = 0; i < data->totlayer; ++i) {
1100                 CustomDataLayer *layer = &data->layers[i];
1101                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1102
1103                 if(typeInfo->interp) {
1104                         for(j = 0; j < count; ++j)
1105                                 sources[j] = (char *)src_blocks[j] + layer->offset;
1106
1107                         typeInfo->interp(sources, weights, sub_weights, count,
1108                                           (char *)dest_block + layer->offset);
1109                 }
1110         }
1111
1112         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1113 }
1114
1115 void CustomData_em_set_default(CustomData *data, void **block)
1116 {
1117         const LayerTypeInfo *typeInfo;
1118         int i;
1119
1120         if (!*block)
1121                 CustomData_em_alloc_block(data, block);
1122
1123         for(i = 0; i < data->totlayer; ++i) {
1124                 int offset = data->layers[i].offset;
1125
1126                 typeInfo = layerType_getInfo(data->layers[i].type);
1127
1128                 if(typeInfo->set_default)
1129                         typeInfo->set_default((char*)*block + offset, 1);
1130         }
1131 }
1132
1133 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
1134                             int src_index, void **dest_block)
1135 {
1136         const LayerTypeInfo *typeInfo;
1137         int dest_i, src_i, src_offset;
1138
1139         if (!*dest_block)
1140                 CustomData_em_alloc_block(dest, dest_block);
1141         
1142         /* copies a layer at a time */
1143         dest_i = 0;
1144         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1145
1146                 /* find the first dest layer with type >= the source type
1147                  * (this should work because layers are ordered by type)
1148                  */
1149                 while(dest_i < dest->totlayer
1150                       && dest->layers[dest_i].type < source->layers[src_i].type)
1151                         ++dest_i;
1152
1153                 /* if there are no more dest layers, we're done */
1154                 if(dest_i >= dest->totlayer) return;
1155
1156                 /* if we found a matching layer, copy the data */
1157                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1158                         int offset = dest->layers[dest_i].offset;
1159                         char *src_data = source->layers[src_i].data;
1160                         char *dest_data = (char*)*dest_block + offset;
1161
1162                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1163                         src_offset = src_index * typeInfo->size;
1164
1165                         if(typeInfo->copy)
1166                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
1167                         else
1168                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
1169
1170                         /* if there are multiple source & dest layers of the same type,
1171                          * we don't want to copy all source layers to the same dest, so
1172                          * increment dest_i
1173                          */
1174                         ++dest_i;
1175                 }
1176         }
1177 }
1178
1179 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
1180                               void *src_block, int dest_index)
1181 {
1182         const LayerTypeInfo *typeInfo;
1183         int dest_i, src_i, dest_offset;
1184
1185         /* copies a layer at a time */
1186         dest_i = 0;
1187         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1188
1189                 /* find the first dest layer with type >= the source type
1190                  * (this should work because layers are ordered by type)
1191                  */
1192                 while(dest_i < dest->totlayer
1193                       && dest->layers[dest_i].type < source->layers[src_i].type)
1194                         ++dest_i;
1195
1196                 /* if there are no more dest layers, we're done */
1197                 if(dest_i >= dest->totlayer) return;
1198
1199                 /* if we found a matching layer, copy the data */
1200                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1201                         int offset = source->layers[src_i].offset;
1202                         char *src_data = (char*)src_block + offset;
1203                         char *dest_data = dest->layers[dest_i].data;
1204
1205                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
1206                         dest_offset = dest_index * typeInfo->size;
1207
1208                         if(typeInfo->copy)
1209                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
1210                         else
1211                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
1212
1213                         /* if there are multiple source & dest layers of the same type,
1214                          * we don't want to copy all source layers to the same dest, so
1215                          * increment dest_i
1216                          */
1217                         ++dest_i;
1218                 }
1219         }
1220
1221 }
1222
1223 void CustomData_file_write_info(int type, char **structname, int *structnum)
1224 {
1225         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1226
1227         *structname = typeInfo->structname;
1228         *structnum = typeInfo->structnum;
1229 }
1230
1231 int CustomData_sizeof(int type)
1232 {
1233         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1234
1235         return typeInfo->size;
1236 }
1237
1238 const char *CustomData_layertype_name(int type)
1239 {
1240         return layerType_getName(type);
1241 }
1242
1243 void CustomData_set_layer_unique_name(CustomData *data, int index)
1244 {
1245         char tempname[64];
1246         int number, i, type;
1247         char *dot, *name;
1248         CustomDataLayer *layer, *nlayer= &data->layers[index];
1249         const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
1250
1251         if (!typeInfo->defaultname)
1252                 return;
1253
1254         type = nlayer->type;
1255         name = nlayer->name;
1256
1257         if (name[0] == '\0')
1258                 BLI_strncpy(nlayer->name, typeInfo->defaultname, sizeof(nlayer->name));
1259         
1260         /* see if there is a duplicate */
1261         for(i=0; i<data->totlayer; i++) {
1262                 layer = &data->layers[i];
1263
1264                 if(i!=index && layer->type==type && strcmp(layer->name, name)==0)
1265                         break;
1266         }
1267
1268         if(i == data->totlayer)
1269                 return;
1270
1271         /* strip off the suffix */
1272         dot = strchr(nlayer->name, '.');
1273         if(dot) *dot=0;
1274         
1275         for(number=1; number <=999; number++) {
1276                 sprintf(tempname, "%s.%03d", nlayer->name, number);
1277
1278                 for(i=0; i<data->totlayer; i++) {
1279                         layer = &data->layers[i];
1280                         
1281                         if(i!=index && layer->type==type && strcmp(layer->name, tempname)==0)
1282                                 break;
1283                 }
1284
1285                 if(i == data->totlayer) {
1286                         BLI_strncpy(nlayer->name, tempname, sizeof(nlayer->name));
1287                         return;
1288                 }
1289         }       
1290 }
1291