Added custom vertex/edge/face data for meshes:
[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_linklist.h"
38
39 #include "DNA_customdata_types.h"
40 #include "DNA_listbase.h"
41 #include "DNA_meshdata_types.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include <string.h>
46
47 /* number of layers to add when growing a CustomData object */
48 #define CUSTOMDATA_GROW 5
49
50 /********************* Layer type information **********************/
51 typedef struct LayerTypeInfo {
52         int size;          /* the memory size of one element of this layer's data */
53         char *structname;  /* name of the struct used, for file writing */
54         int structnum;     /* number of structs per element, for file writing */
55
56         /* a function to copy count elements of this layer's data
57          * (deep copy if appropriate)
58          * size should be the size of one element of this layer's data (e.g.
59          * LayerTypeInfo.size)
60          * if NULL, memcpy is used
61          */
62         void (*copy)(const void *source, void *dest, int count, int size);
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, int size)
93 {
94         int i;
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, int size)
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                 dest_tf[i].flag &= ~TF_ACTIVE;
217         }
218 }
219
220 static void layerInterp_tface(void **sources, float *weights,
221                               float *sub_weights, int count, void *dest)
222 {
223         MTFace *tf = dest;
224         int i, j, k;
225         float uv[4][2];
226         float *sub_weight;
227
228         if(count <= 0) return;
229
230         memset(uv, 0, sizeof(uv));
231
232         sub_weight = sub_weights;
233         for(i = 0; i < count; ++i) {
234                 float weight = weights ? weights[i] : 1;
235                 MTFace *src = sources[i];
236
237                 for(j = 0; j < 4; ++j) {
238                         if(sub_weights) {
239                                 for(k = 0; k < 4; ++k, ++sub_weight) {
240                                         float w = (*sub_weight) * weight;
241                                         float *tmp_uv = src->uv[k];
242
243                                         uv[j][0] += tmp_uv[0] * w;
244                                         uv[j][1] += tmp_uv[1] * w;
245                                 }
246                         } else {
247                                 uv[j][0] += src->uv[j][0] * weight;
248                                 uv[j][1] += src->uv[j][1] * weight;
249                         }
250                 }
251         }
252
253         *tf = *(MTFace *)sources[0];
254         for(j = 0; j < 4; ++j) {
255                 tf->uv[j][0] = uv[j][0];
256                 tf->uv[j][1] = uv[j][1];
257         }
258 }
259
260 static void layerSwap_tface(void *data, int *corner_indices)
261 {
262         MTFace *tf = data;
263         float uv[4][2];
264         int j;
265
266         for(j = 0; j < 4; ++j) {
267                 uv[j][0] = tf->uv[corner_indices[j]][0];
268                 uv[j][1] = tf->uv[corner_indices[j]][1];
269         }
270
271         memcpy(tf->uv, uv, sizeof(tf->uv));
272 }
273
274 static void layerDefault_tface(void *data, int count)
275 {
276         static MTFace default_tf = {{{0, 1}, {0, 0}, {1, 0}, {1, 1}}, NULL,
277                                    TF_SELECT, 0, TF_DYNAMIC, 0, 0};
278         MTFace *tf = (MTFace*)data;
279         int i;
280
281         for(i = 0; i < count; i++)
282                 tf[i] = default_tf;
283 }
284
285 static void layerInterp_mcol(void **sources, float *weights,
286                              float *sub_weights, int count, void *dest)
287 {
288         MCol *mc = dest;
289         int i, j, k;
290         struct {
291                 float a;
292                 float r;
293                 float g;
294                 float b;
295         } col[4];
296         float *sub_weight;
297
298         if(count <= 0) return;
299
300         memset(col, 0, sizeof(col));
301         
302         sub_weight = sub_weights;
303         for(i = 0; i < count; ++i) {
304                 float weight = weights ? weights[i] : 1;
305
306                 for(j = 0; j < 4; ++j) {
307                         if(sub_weights) {
308                                 MCol *src = sources[i];
309                                 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
310                                         col[j].a += src->a * (*sub_weight) * weight;
311                                         col[j].r += src->r * (*sub_weight) * weight;
312                                         col[j].g += src->g * (*sub_weight) * weight;
313                                         col[j].b += src->b * (*sub_weight) * weight;
314                                 }
315                         } else {
316                                 MCol *src = sources[i];
317                                 col[j].a += src[j].a * weight;
318                                 col[j].r += src[j].r * weight;
319                                 col[j].g += src[j].g * weight;
320                                 col[j].b += src[j].b * weight;
321                         }
322                 }
323         }
324
325         for(j = 0; j < 4; ++j) {
326                 mc[j].a = (int)col[j].a;
327                 mc[j].r = (int)col[j].r;
328                 mc[j].g = (int)col[j].g;
329                 mc[j].b = (int)col[j].b;
330         }
331 }
332
333 static void layerSwap_mcol(void *data, int *corner_indices)
334 {
335         MCol *mcol = data;
336         MCol col[4];
337         int j;
338
339         for(j = 0; j < 4; ++j)
340                 col[j] = mcol[corner_indices[j]];
341
342         memcpy(mcol, col, sizeof(col));
343 }
344
345 static void layerDefault_mcol(void *data, int count)
346 {
347         static MCol default_mcol = {255, 255, 255, 255};
348         MCol *mcol = (MCol*)data;
349         int i;
350
351         for(i = 0; i < 4*count; i++)
352                 mcol[i] = default_mcol;
353 }
354
355 const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
356         {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL},
357         {sizeof(MSticky), "MSticky", 1, NULL, NULL, layerInterp_msticky, NULL, NULL},
358         {sizeof(MDeformVert), "MDeformVert", 1, layerCopy_mdeformvert,
359          layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
360         {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL},
361         {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL},
362         {sizeof(MTFace), "MTFace", 1, layerCopy_tface, NULL, layerInterp_tface,
363          layerSwap_tface, layerDefault_tface},
364         /* 4 MCol structs per face */
365         {sizeof(MCol)*4, "MCol", 4, NULL, NULL, layerInterp_mcol, layerSwap_mcol,
366          layerDefault_mcol},
367         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL},
368         /* 3 floats per normal vector */
369         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL},
370         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL},
371 };
372
373 const char *LAYERTYPENAMES[CD_NUMTYPES] = {
374         "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace", "CDMTFace",
375         "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags"};
376
377 CustomDataMask CD_MASK_MESH[CD_NUMTYPES] = {
378         1, 1, 1, 1, 1, 1, 1, 0, 0, 0};
379 CustomDataMask CD_MASK_EDITMESH[CD_NUMTYPES] = {
380         0, 1, 1, 0, 0, 1, 1, 0, 0, 0};
381 CustomDataMask CD_MASK_DERIVEDMESH[CD_NUMTYPES] = {
382         0, 1, 1, 0, 0, 1, 1, 1, 0, 0};
383
384 static const LayerTypeInfo *layerType_getInfo(int type)
385 {
386         if(type < 0 || type >= CD_NUMTYPES) return NULL;
387
388         return &LAYERTYPEINFO[type];
389 }
390
391 static const char *layerType_getName(int type)
392 {
393         if(type < 0 || type >= CD_NUMTYPES) return NULL;
394
395         return LAYERTYPENAMES[type];
396 }
397
398 /********************* CustomData functions *********************/
399 static void CustomData_update_offsets(CustomData *data)
400 {
401         const LayerTypeInfo *typeInfo;
402         int i, offset = 0;
403
404         for(i = 0; i < data->totlayer; ++i) {
405                 typeInfo = layerType_getInfo(data->layers[i].type);
406
407                 data->layers[i].offset = offset;
408                 offset += typeInfo->size;
409         }
410
411         data->totsize = offset;
412 }
413
414 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
415                       CustomDataMask *mask, int alloctype, int totelem)
416 {
417         const LayerTypeInfo *typeInfo;
418         CustomDataLayer *layer;
419         int i, flag, type;
420         void *data;
421
422         for(i = 0; i < source->totlayer; ++i) {
423                 layer = &source->layers[i];
424                 typeInfo = layerType_getInfo(layer->type);
425
426                 if(layer->flag & CD_FLAG_NOCOPY) continue;
427                 else if(mask && !mask[layer->type]) continue;
428                 else if(CustomData_has_layer(dest, layer->type)) continue;
429
430                 type = layer->type;
431                 flag = layer->flag & ~CD_FLAG_NOFREE;
432                 data = layer->data;
433
434                 if (alloctype == CD_CALLOC) {
435                         CustomData_add_layer(dest, type, flag, NULL, totelem);
436                 }
437                 else if (alloctype == CD_REFERENCE) {
438                         CustomData_add_layer(dest, type, flag|CD_FLAG_NOFREE, data, totelem);
439                 }
440                 else if (alloctype == CD_DUPLICATE) {
441                         CustomData_add_layer(dest, type, flag, MEM_dupallocN(data), totelem);
442                 }
443                 else if (alloctype == CD_DEFAULT) {
444                         data = CustomData_add_layer(dest, type, flag, NULL, totelem);
445                         if(typeInfo->set_default)
446                                 typeInfo->set_default((char*)data, totelem);
447                 }
448         }
449
450         CustomData_update_offsets(dest);
451
452 }
453
454 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
455                      CustomDataMask *mask, int alloctype, int totelem)
456 {
457         memset(dest, 0, sizeof(*dest));
458
459         CustomData_merge(source, dest, mask, alloctype, totelem);
460 }
461
462 static void CustomData_free_layer__internal(CustomDataLayer *layer, int totelem)
463 {
464         const LayerTypeInfo *typeInfo;
465
466         if(!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
467                 typeInfo = layerType_getInfo(layer->type);
468
469                 if(typeInfo->free)
470                         typeInfo->free(layer->data, totelem, typeInfo->size);
471
472                 if(layer->data)
473                         MEM_freeN(layer->data);
474         }
475 }
476
477 void CustomData_free(CustomData *data, int totelem)
478 {
479         int i;
480
481         for(i = 0; i < data->totlayer; ++i)
482                 CustomData_free_layer__internal(&data->layers[i], totelem);
483
484         if(data->layers)
485                 MEM_freeN(data->layers);
486         
487         memset(data, 0, sizeof(*data));
488 }
489
490
491 /* gets index of first layer matching type after start_index
492  * if start_index < 0, starts searching at 0
493  * returns -1 if there is no layer of type
494  */
495 static int CustomData_find_next(const CustomData *data, int type,
496                                 int start_index)
497 {
498         int i = start_index + 1;
499
500         if(i < 0) i = 0;
501
502         for(; i < data->totlayer; ++i)
503                 if(data->layers[i].type == type) return i;
504
505         return -1;
506 }
507
508 static int customData_resize(CustomData *data, int amount)
509 {
510         CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
511                                        "CustomData->layers");
512         if(!tmp) return 0;
513
514         data->maxlayer += amount;
515         if (data->layers) {
516                 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
517                 MEM_freeN(data->layers);
518         }
519         data->layers = tmp;
520
521         return 1;
522 }
523
524 static int customData_add_layer__internal(CustomData *data, int type, int flag,
525                                           void *layer)
526 {
527         int index = data->totlayer;
528
529         if(index >= data->maxlayer)
530                 if(!customData_resize(data, CUSTOMDATA_GROW))
531                         return 0;
532         
533         /* keep layers ordered by type */
534         for( ; index > 0 && data->layers[index - 1].type > type; --index)
535                 data->layers[index] = data->layers[index - 1];
536
537         data->layers[index].type = type;
538         data->layers[index].flag = flag;
539         data->layers[index].data = layer;
540
541         data->totlayer++;
542
543         CustomData_update_offsets(data);
544
545         return 1;
546 }
547
548 void *CustomData_add_layer(CustomData *data, int type, int flag,
549                            void *layerdata, int totelem)
550 {
551         int size = layerType_getInfo(type)->size * totelem;
552         void *tmpdata = layerdata;
553
554         if(!tmpdata)
555                 tmpdata = MEM_callocN(size, layerType_getName(type));
556         if(!tmpdata)
557                 return NULL;
558
559         if(!customData_add_layer__internal(data, type, flag, tmpdata)) {
560                 MEM_freeN(tmpdata);
561                 return NULL;
562         }
563         
564         return tmpdata;
565 }
566
567 int CustomData_free_layer(CustomData *data, int type, int totelem)
568 {
569         int index = CustomData_find_next(data, type, -1);
570
571         if (index < 0) return 0;
572
573         CustomData_free_layer__internal(&data->layers[index], totelem);
574
575         for(++index; index < data->totlayer; ++index)
576                 data->layers[index - 1] = data->layers[index];
577
578         data->totlayer--;
579
580         if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
581                 customData_resize(data, -CUSTOMDATA_GROW);
582
583         CustomData_update_offsets(data);
584
585         return 1;
586 }
587
588 int CustomData_has_layer(const CustomData *data, int type)
589 {
590         return (CustomData_find_next(data, type, -1) != -1);
591 }
592
593 void *CustomData_duplicate_referenced_layer(struct CustomData *data, int type)
594 {
595         CustomDataLayer *layer;
596         int layer_index;
597
598         /* get the layer index of the first layer of type */
599         layer_index = CustomData_find_next(data, type, -1);
600         if(layer_index < 0) return NULL;
601
602         layer = &data->layers[layer_index];
603
604         if (layer->flag & CD_FLAG_NOFREE) {
605                 layer->data = MEM_dupallocN(layer->data);
606                 layer->flag &= ~CD_FLAG_NOFREE;
607         }
608
609         return layer->data;
610 }
611
612 void CustomData_free_temporary(CustomData *data, int totelem)
613 {
614         CustomDataLayer *layer;
615         int i, j;
616
617         for(i = 0, j = 0; i < data->totlayer; ++i) {
618                 layer = &data->layers[i];
619
620                 if (i != j)
621                         data->layers[j] = data->layers[i];
622
623                 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
624                         CustomData_free_layer__internal(layer, totelem);
625                 else
626                         j++;
627         }
628
629         data->totlayer = j;
630
631         if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
632                 customData_resize(data, -CUSTOMDATA_GROW);
633
634         CustomData_update_offsets(data);
635 }
636
637 int CustomData_compat(const CustomData *data1, const CustomData *data2)
638 {
639         int i;
640
641         if(data1->totlayer != data2->totlayer) return 0;
642
643         for(i = 0; i < data1->totlayer; ++i) {
644                 if(data1->layers[i].type != data2->layers[i].type) return 0;
645                 if(data1->layers[i].flag != data2->layers[i].flag) return 0;
646         }
647
648         return 1;
649 }
650
651 void CustomData_copy_data(const CustomData *source, CustomData *dest,
652                           int source_index, int dest_index, int count)
653 {
654         const LayerTypeInfo *type_info;
655         int src_i, dest_i;
656         int src_offset;
657         int dest_offset;
658
659         /* copies a layer at a time */
660         dest_i = 0;
661         for(src_i = 0; src_i < source->totlayer; ++src_i) {
662                 if(source->layers[src_i].flag & CD_FLAG_NOCOPY) continue;
663
664                 /* find the first dest layer with type >= the source type
665                  * (this should work because layers are ordered by type)
666                  */
667                 while(dest_i < dest->totlayer
668                       && dest->layers[dest_i].type < source->layers[src_i].type)
669                         ++dest_i;
670
671                 /* if there are no more dest layers, we're done */
672                 if(dest_i >= dest->totlayer) return;
673
674                 /* if we found a matching layer, copy the data */
675                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
676                         char *src_data = source->layers[src_i].data;
677                         char *dest_data = dest->layers[dest_i].data;
678
679                         type_info = layerType_getInfo(source->layers[src_i].type);
680
681                         src_offset = source_index * type_info->size;
682                         dest_offset = dest_index * type_info->size;
683
684                         if(type_info->copy)
685                                 type_info->copy(src_data + src_offset,
686                                                 dest_data + dest_offset,
687                                                 count, type_info->size);
688                         else
689                                 memcpy(dest_data + dest_offset,
690                                        src_data + src_offset,
691                                        count * type_info->size);
692
693                         /* if there are multiple source & dest layers of the same type,
694                          * we don't want to copy all source layers to the same dest, so
695                          * increment dest_i
696                          */
697                         ++dest_i;
698                 }
699         }
700 }
701
702 void CustomData_free_elem(CustomData *data, int index, int count)
703 {
704         int i;
705         const LayerTypeInfo *typeInfo;
706
707         for(i = 0; i < data->totlayer; ++i) {
708                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
709                         typeInfo = layerType_getInfo(data->layers[i].type);
710
711                         if(typeInfo->free) {
712                                 int offset = typeInfo->size * index;
713
714                                 typeInfo->free((char *)data->layers[i].data + offset,
715                                                count, typeInfo->size);
716                         }
717                 }
718         }
719 }
720
721 #define SOURCE_BUF_SIZE 100
722
723 void CustomData_interp(const CustomData *source, CustomData *dest,
724                        int *src_indices, float *weights, float *sub_weights,
725                        int count, int dest_index)
726 {
727         int src_i, dest_i;
728         int dest_offset;
729         int j;
730         void *source_buf[SOURCE_BUF_SIZE];
731         void **sources = source_buf;
732
733         /* slow fallback in case we're interpolating a ridiculous number of
734          * elements
735          */
736         if(count > SOURCE_BUF_SIZE)
737                 sources = MEM_callocN(sizeof(*sources) * count,
738                                       "CustomData_interp sources");
739
740         /* interpolates a layer at a time */
741         for(src_i = 0; src_i < source->totlayer; ++src_i) {
742                 CustomDataLayer *source_layer = &source->layers[src_i];
743                 const LayerTypeInfo *type_info =
744                                         layerType_getInfo(source_layer->type);
745
746                 dest_i = CustomData_find_next(dest, source_layer->type, -1);
747
748                 if(dest_i >= 0 && type_info->interp) {
749                         void *src_data = source_layer->data; 
750
751                         for(j = 0; j < count; ++j)
752                                 sources[j] = (char *)src_data
753                                              + type_info->size * src_indices[j];
754
755                         dest_offset = dest_index * type_info->size;
756
757                         type_info->interp(sources, weights, sub_weights, count,
758                                        (char *)dest->layers[dest_i].data + dest_offset);
759                 }
760         }
761
762         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
763 }
764
765 void CustomData_swap(struct CustomData *data, int index, int *corner_indices)
766 {
767         const LayerTypeInfo *typeInfo;
768         int i;
769
770         for(i = 0; i < data->totlayer; ++i) {
771                 typeInfo = layerType_getInfo(data->layers[i].type);
772
773                 if(typeInfo->swap) {
774                         int offset = typeInfo->size * index;
775
776                         typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
777                 }
778         }
779 }
780
781 void *CustomData_get(const CustomData *data, int index, int type)
782 {
783         int offset;
784         int layer_index;
785         
786         /* get the layer index of the first layer of type */
787         layer_index = CustomData_find_next(data, type, -1);
788         if(layer_index < 0) return NULL;
789
790         /* get the offset of the desired element */
791         offset = layerType_getInfo(type)->size * index;
792
793         return (char *)data->layers[layer_index].data + offset;
794 }
795
796 void *CustomData_get_layer(const CustomData *data, int type)
797 {
798         /* get the layer index of the first layer of type */
799         int layer_index = CustomData_find_next(data, type, -1);
800
801         if(layer_index < 0) return NULL;
802
803         return data->layers[layer_index].data;
804 }
805
806 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
807 {
808         /* get the layer index of the first layer of type */
809         int layer_index = CustomData_find_next(data, type, -1);
810
811         if(layer_index < 0) return NULL;
812
813         data->layers[layer_index].data = ptr;
814
815         return ptr;
816 }
817
818 void CustomData_set(const CustomData *data, int index, int type, void *source)
819 {
820         void *dest = CustomData_get(data, index, type);
821         const LayerTypeInfo *type_info = layerType_getInfo(type);
822
823         if(!dest) return;
824
825         if(type_info->copy)
826                 type_info->copy(source, dest, 1, type_info->size);
827         else
828                 memcpy(dest, source, type_info->size);
829 }
830
831 void CustomData_set_default(CustomData *data, int index, int count)
832 {
833         const LayerTypeInfo *typeInfo;
834         int i;
835
836         for(i = 0; i < data->totlayer; ++i) {
837                 typeInfo = layerType_getInfo(data->layers[i].type);
838
839                 if(typeInfo->set_default) {
840                         int offset = typeInfo->size * index;
841
842                         typeInfo->set_default((char *)data->layers[i].data + offset, count);
843                 }
844         }
845 }
846
847 /* EditMesh functions */
848
849 void CustomData_em_free_block(CustomData *data, void **block)
850 {
851     const LayerTypeInfo *typeInfo;
852     int i;
853
854         if(!*block) return;
855
856     for(i = 0; i < data->totlayer; ++i) {
857         if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
858             typeInfo = layerType_getInfo(data->layers[i].type);
859
860             if(typeInfo->free) {
861                                 int offset = data->layers[i].offset;
862                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
863                         }
864         }
865     }
866
867         MEM_freeN(*block);
868         *block = NULL;
869 }
870
871 static void CustomData_em_alloc_block(CustomData *data, void **block)
872 {
873         /* TODO: optimize free/alloc */
874
875         if (*block)
876                 CustomData_em_free_block(data, block);
877
878         if (data->totsize > 0)
879                 *block = MEM_callocN(data->totsize, "CustomData EM block");
880         else
881                 *block = NULL;
882 }
883
884 void CustomData_em_copy_data(const CustomData *source, CustomData *dest,
885                             void *src_block, void **dest_block)
886 {
887         const LayerTypeInfo *type_info;
888         int dest_i, src_i;
889
890         if (!*dest_block)
891                 CustomData_em_alloc_block(dest, dest_block);
892         
893         /* copies a layer at a time */
894         dest_i = 0;
895         for(src_i = 0; src_i < source->totlayer; ++src_i) {
896                 if(source->layers[src_i].flag & CD_FLAG_NOCOPY) continue;
897
898                 /* find the first dest layer with type >= the source type
899                  * (this should work because layers are ordered by type)
900                  */
901                 while(dest_i < dest->totlayer
902                       && dest->layers[dest_i].type < source->layers[src_i].type)
903                         ++dest_i;
904
905                 /* if there are no more dest layers, we're done */
906                 if(dest_i >= dest->totlayer) return;
907
908                 /* if we found a matching layer, copy the data */
909                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
910                         char *src_data = (char*)src_block + source->layers[src_i].offset;
911                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
912
913                         type_info = layerType_getInfo(source->layers[src_i].type);
914
915                         if(type_info->copy)
916                                 type_info->copy(src_data, dest_data, 1, type_info->size);
917                         else
918                                 memcpy(dest_data, src_data, type_info->size);
919
920                         /* if there are multiple source & dest layers of the same type,
921                          * we don't want to copy all source layers to the same dest, so
922                          * increment dest_i
923                          */
924                         ++dest_i;
925                 }
926         }
927 }
928
929 void *CustomData_em_get(const CustomData *data, void *block, int type)
930 {
931         int layer_index;
932         
933         /* get the layer index of the first layer of type */
934         layer_index = CustomData_find_next(data, type, -1);
935         if(layer_index < 0) return NULL;
936
937         return (char *)block + data->layers[layer_index].offset;
938 }
939
940 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
941 {
942         void *dest = CustomData_em_get(data, block, type);
943         const LayerTypeInfo *type_info = layerType_getInfo(type);
944
945         if(!dest) return;
946
947         if(type_info->copy)
948                 type_info->copy(source, dest, 1, type_info->size);
949         else
950                 memcpy(dest, source, type_info->size);
951 }
952
953 void CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
954                           float *sub_weights, int count, void *dest_block)
955 {
956         int i, j;
957         void *source_buf[SOURCE_BUF_SIZE];
958         void **sources = source_buf;
959
960         /* slow fallback in case we're interpolating a ridiculous number of
961          * elements
962          */
963         if(count > SOURCE_BUF_SIZE)
964                 sources = MEM_callocN(sizeof(*sources) * count,
965                                       "CustomData_interp sources");
966
967         /* interpolates a layer at a time */
968         for(i = 0; i < data->totlayer; ++i) {
969                 CustomDataLayer *layer = &data->layers[i];
970                 const LayerTypeInfo *type_info = layerType_getInfo(layer->type);
971
972                 if(type_info->interp) {
973                         for(j = 0; j < count; ++j)
974                                 sources[j] = (char *)src_blocks[j] + layer->offset;
975
976                         type_info->interp(sources, weights, sub_weights, count,
977                                           (char *)dest_block + layer->offset);
978                 }
979         }
980
981         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
982 }
983
984 void CustomData_em_set_default(CustomData *data, void **block)
985 {
986         const LayerTypeInfo *type_info;
987         int i;
988
989         if (!*block)
990                 CustomData_em_alloc_block(data, block);
991
992         for(i = 0; i < data->totlayer; ++i) {
993                 int offset = data->layers[i].offset;
994
995                 type_info = layerType_getInfo(data->layers[i].type);
996
997                 if(type_info->set_default)
998                         type_info->set_default((char*)*block + offset, 1);
999         }
1000 }
1001
1002 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
1003                             int src_index, void **dest_block)
1004 {
1005         const LayerTypeInfo *type_info;
1006         int dest_i, src_i, src_offset;
1007
1008         if (!*dest_block)
1009                 CustomData_em_alloc_block(dest, dest_block);
1010         
1011         /* copies a layer at a time */
1012         dest_i = 0;
1013         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1014                 if(source->layers[src_i].flag & CD_FLAG_NOCOPY) continue;
1015
1016                 /* find the first dest layer with type >= the source type
1017                  * (this should work because layers are ordered by type)
1018                  */
1019                 while(dest_i < dest->totlayer
1020                       && dest->layers[dest_i].type < source->layers[src_i].type)
1021                         ++dest_i;
1022
1023                 /* if there are no more dest layers, we're done */
1024                 if(dest_i >= dest->totlayer) return;
1025
1026                 /* if we found a matching layer, copy the data */
1027                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1028                         int offset = dest->layers[dest_i].offset;
1029                         char *src_data = source->layers[src_i].data;
1030                         char *dest_data = (char*)*dest_block + offset;
1031
1032                         type_info = layerType_getInfo(dest->layers[dest_i].type);
1033                         src_offset = src_index * type_info->size;
1034
1035                         if(type_info->copy)
1036                                 type_info->copy(src_data + src_offset, dest_data, 1,
1037                                                                 type_info->size);
1038                         else
1039                                 memcpy(dest_data, src_data + src_offset, type_info->size);
1040
1041                         /* if there are multiple source & dest layers of the same type,
1042                          * we don't want to copy all source layers to the same dest, so
1043                          * increment dest_i
1044                          */
1045                         ++dest_i;
1046                 }
1047         }
1048 }
1049
1050 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
1051                               void *src_block, int dest_index)
1052 {
1053         const LayerTypeInfo *type_info;
1054         int dest_i, src_i, dest_offset;
1055
1056         /* copies a layer at a time */
1057         dest_i = 0;
1058         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1059                 if(source->layers[src_i].flag & CD_FLAG_NOCOPY) continue;
1060
1061                 /* find the first dest layer with type >= the source type
1062                  * (this should work because layers are ordered by type)
1063                  */
1064                 while(dest_i < dest->totlayer
1065                       && dest->layers[dest_i].type < source->layers[src_i].type)
1066                         ++dest_i;
1067
1068                 /* if there are no more dest layers, we're done */
1069                 if(dest_i >= dest->totlayer) return;
1070
1071                 /* if we found a matching layer, copy the data */
1072                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1073                         int offset = source->layers[src_i].offset;
1074                         char *src_data = (char*)src_block + offset;
1075                         char *dest_data = dest->layers[dest_i].data;
1076
1077                         type_info = layerType_getInfo(dest->layers[dest_i].type);
1078                         dest_offset = dest_index * type_info->size;
1079
1080                         if(type_info->copy)
1081                                 type_info->copy(src_data, dest_data + dest_offset, 1,
1082                                                                 type_info->size);
1083                         else
1084                                 memcpy(dest_data + dest_offset, src_data, type_info->size);
1085
1086                         /* if there are multiple source & dest layers of the same type,
1087                          * we don't want to copy all source layers to the same dest, so
1088                          * increment dest_i
1089                          */
1090                         ++dest_i;
1091                 }
1092         }
1093
1094 }
1095
1096 void CustomData_file_write_info(int type, char **structname, int *structnum)
1097 {
1098         const LayerTypeInfo *type_info = layerType_getInfo(type);
1099
1100         *structname = type_info->structname;
1101         *structnum = type_info->structnum;
1102 }
1103
1104 int CustomData_sizeof(int type)
1105 {
1106         const LayerTypeInfo *type_info = layerType_getInfo(type);
1107
1108         return type_info->size;
1109 }