Added custom face data support in edit mode. The code used to do this is
[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_mesh_types.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
54         /* a function to copy count elements of this layer's data
55          * (deep copy if appropriate)
56          * size should be the size of one element of this layer's data (e.g.
57          * LayerTypeInfo.size)
58          * if NULL, memcpy is used
59          */
60         void (*copy)(const void *source, void *dest, int count, int size);
61
62         /* a function to free any dynamically allocated components of this
63          * layer's data (note the data pointer itself should not be freed)
64          * size should be the size of one element of this layer's data (e.g.
65          * LayerTypeInfo.size)
66          */
67         void (*free)(void *data, int count, int size);
68
69         /* a function to interpolate between count source elements of this
70          * layer's data and store the result in dest
71          * if weights == NULL or sub_weights == NULL, they should default to 1
72          *
73          * weights gives the weight for each element in sources
74          * sub_weights gives the sub-element weights for each element in sources
75          *    (there should be (sub element count)^2 weights per element)
76          * count gives the number of elements in sources
77          */
78         void (*interp)(void **sources, float *weights, float *sub_weights,
79                        int count, void *dest);
80
81     /* a function to set a layer's data to default values. if NULL, the
82            default is assumed to be all zeros */
83         void (*set_default)(void *data);
84 } LayerTypeInfo;
85
86 static void layerCopy_mdeformvert(const void *source, void *dest,
87                                   int count, int size)
88 {
89         int i;
90
91         memcpy(dest, source, count * size);
92
93         for(i = 0; i < count; ++i) {
94                 MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
95                 MDeformWeight *dw = MEM_callocN(dvert->totweight * sizeof(*dw),
96                                                 "layerCopy_mdeformvert dw");
97
98                 memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
99                 dvert->dw = dw;
100         }
101 }
102
103 static void layerFree_mdeformvert(void *data, int count, int size)
104 {
105         int i;
106
107         for(i = 0; i < count; ++i) {
108                 MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
109
110                 if(dvert->dw) {
111                         MEM_freeN(dvert->dw);
112                         dvert->dw = NULL;
113                 }
114         }
115 }
116
117 static void linklist_free_simple(void *link)
118 {
119         MEM_freeN(link);
120 }
121
122 static void layerInterp_mdeformvert(void **sources, float *weights,
123                                     float *sub_weights, int count, void *dest)
124 {
125         MDeformVert *dvert = dest;
126         LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
127         LinkNode *node;
128         int i, j, totweight;
129
130         if(count <= 0) return;
131
132         /* build a list of unique def_nrs for dest */
133         totweight = 0;
134         for(i = 0; i < count; ++i) {
135                 MDeformVert *source = sources[i];
136                 float interp_weight = weights ? weights[i] : 1.0f;
137
138                 for(j = 0; j < source->totweight; ++j) {
139                         MDeformWeight *dw = &source->dw[j];
140
141                         for(node = dest_dw; node; node = node->next) {
142                                 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
143
144                                 if(tmp_dw->def_nr == dw->def_nr) {
145                                         tmp_dw->weight += dw->weight * interp_weight;
146                                         break;
147                                 }
148                         }
149
150                         /* if this def_nr is not in the list, add it */
151                         if(!node) {
152                                 MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
153                                                             "layerInterp_mdeformvert tmp_dw");
154                                 tmp_dw->def_nr = dw->def_nr;
155                                 tmp_dw->weight = dw->weight * interp_weight;
156                                 BLI_linklist_prepend(&dest_dw, tmp_dw);
157                                 totweight++;
158                         }
159                 }
160         }
161
162         /* now we know how many unique deform weights there are, so realloc */
163         if(dvert->dw) MEM_freeN(dvert->dw);
164         dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
165                                 "layerInterp_mdeformvert dvert->dw");
166         dvert->totweight = totweight;
167
168         for(i = 0, node = dest_dw; node; node = node->next, ++i)
169                 dvert->dw[i] = *((MDeformWeight *)node->link);
170
171         BLI_linklist_free(dest_dw, linklist_free_simple);
172 }
173
174 static void layerCopy_tface(const void *source, void *dest, int count, int size)
175 {
176         const TFace *source_tf = (const TFace*)source;
177         TFace *dest_tf = (TFace*)dest;
178         int i;
179
180         for(i = 0; i < count; ++i) {
181                 dest_tf[i] = source_tf[i];
182                 dest_tf[i].flag &= ~TF_ACTIVE;
183         }
184 }
185
186 static void layerInterp_tface(void **sources, float *weights,
187                               float *sub_weights, int count, void *dest)
188 {
189         TFace *tf = dest;
190         int i, j, k;
191         float uv[4][2];
192         float col[4][4];
193         float *sub_weight;
194
195         if(count <= 0) return;
196
197         memset(uv, 0, sizeof(uv));
198         memset(col, 0, sizeof(col));
199
200         sub_weight = sub_weights;
201         for(i = 0; i < count; ++i) {
202                 float weight = weights ? weights[i] : 1;
203                 TFace *src = sources[i];
204
205                 for(j = 0; j < 4; ++j) {
206                         if(sub_weights) {
207                                 for(k = 0; k < 4; ++k, ++sub_weight) {
208                                         float w = (*sub_weight) * weight;
209                                         char *tmp_col = (char *)&src->col[k];
210                                         float *tmp_uv = src->uv[k];
211
212                                         uv[j][0] += tmp_uv[0] * w;
213                                         uv[j][1] += tmp_uv[1] * w;
214
215                                         col[j][0] += tmp_col[0] * w;
216                                         col[j][1] += tmp_col[1] * w;
217                                         col[j][2] += tmp_col[2] * w;
218                                         col[j][3] += tmp_col[3] * w;
219                                 }
220                         } else {
221                                 char *tmp_col = (char *)&src->col[j];
222                                 uv[j][0] += src->uv[j][0] * weight;
223                                 uv[j][1] += src->uv[j][1] * weight;
224
225                                 col[j][0] += tmp_col[0] * weight;
226                                 col[j][1] += tmp_col[1] * weight;
227                                 col[j][2] += tmp_col[2] * weight;
228                                 col[j][3] += tmp_col[3] * weight;
229                         }
230                 }
231         }
232
233         *tf = *(TFace *)sources[0];
234         for(j = 0; j < 4; ++j) {
235                 char *tmp_col = (char *)&tf->col[j];
236
237                 tf->uv[j][0] = uv[j][0];
238                 tf->uv[j][1] = uv[j][1];
239
240                 tmp_col[0] = (int)col[j][0];
241                 tmp_col[1] = (int)col[j][1];
242                 tmp_col[2] = (int)col[j][2];
243                 tmp_col[3] = (int)col[j][3];
244         }
245 }
246
247 static void layerDefault_tface(void *data)
248 {
249         static TFace default_tf = {NULL, {{0, 1}, {0, 0}, {1, 0}, {1, 1}},
250                                    {~0, ~0, ~0, ~0}, TF_SELECT, 0, TF_DYNAMIC, 0, 0};
251
252         *((TFace*)data) = default_tf;
253 }
254
255 static void layerInterp_mcol(void **sources, float *weights,
256                              float *sub_weights, int count, void *dest)
257 {
258         MCol *mc = dest;
259         int i, j, k;
260         struct {
261                 float a;
262                 float r;
263                 float g;
264                 float b;
265         } col[4];
266         float *sub_weight;
267
268         if(count <= 0) return;
269
270         memset(col, 0, sizeof(col));
271         
272         sub_weight = sub_weights;
273         for(i = 0; i < count; ++i) {
274                 float weight = weights ? weights[i] : 1;
275
276                 for(j = 0; j < 4; ++j) {
277                         if(sub_weights) {
278                                 MCol *src = sources[i];
279                                 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
280                                         col[j].a += src->a * (*sub_weight) * weight;
281                                         col[j].r += src->r * (*sub_weight) * weight;
282                                         col[j].g += src->g * (*sub_weight) * weight;
283                                         col[j].b += src->b * (*sub_weight) * weight;
284                                 }
285                         } else {
286                                 MCol *src = sources[i];
287                                 col[j].a += src[j].a * weight;
288                                 col[j].r += src[j].r * weight;
289                                 col[j].g += src[j].g * weight;
290                                 col[j].b += src[j].b * weight;
291                         }
292                 }
293         }
294
295         for(j = 0; j < 4; ++j) {
296                 mc[j].a = (int)col[j].a;
297                 mc[j].r = (int)col[j].r;
298                 mc[j].g = (int)col[j].g;
299                 mc[j].b = (int)col[j].b;
300         }
301 }
302
303 static void layerDefault_mcol(void *data)
304 {
305         static MCol default_mcol = {255, 255, 255, 255};
306         MCol *mcol = (MCol*)data;
307
308         mcol[0]= default_mcol;
309         mcol[1]= default_mcol;
310         mcol[2]= default_mcol;
311         mcol[3]= default_mcol;
312 }
313
314 const LayerTypeInfo LAYERTYPEINFO[LAYERTYPE_NUMTYPES] = {
315         {sizeof(MVert), NULL, NULL, NULL, NULL},
316         {sizeof(MSticky), NULL, NULL, NULL, NULL},
317         {sizeof(MDeformVert), layerCopy_mdeformvert,
318          layerFree_mdeformvert, layerInterp_mdeformvert, NULL},
319         {sizeof(MEdge), NULL, NULL, NULL, NULL},
320         {sizeof(MFace), NULL, NULL, NULL, NULL},
321         {sizeof(TFace), layerCopy_tface, NULL, layerInterp_tface,
322          layerDefault_tface},
323         /* 4 MCol structs per face */
324         {sizeof(MCol) * 4, NULL, NULL, layerInterp_mcol, layerDefault_mcol},
325         {sizeof(int), NULL, NULL, NULL, NULL},
326         /* 3 floats per normal vector */
327         {sizeof(float) * 3, NULL, NULL, NULL, NULL},
328         {sizeof(int), NULL, NULL, NULL, NULL},
329 };
330
331 static const LayerTypeInfo *layerType_getInfo(int type)
332 {
333         if(type < 0 || type >= LAYERTYPE_NUMTYPES) return NULL;
334
335         return &LAYERTYPEINFO[type];
336 }
337
338 /********************* CustomData functions *********************/
339 void CustomData_init(CustomData *data,
340                      int maxLayers, int maxElems, int subElems)
341 {
342         data->layers = MEM_callocN(maxLayers * sizeof(*data->layers),
343                                     "CustomData->layers");
344         data->numLayers = 0;
345         data->maxLayers = maxLayers;
346         data->numElems = maxElems;
347         data->maxElems = maxElems;
348         data->subElems = subElems;
349 }
350
351 static void CustomData_update_offsets(CustomData *data)
352 {
353         const LayerTypeInfo *typeInfo;
354         int i, offset = 0;
355
356         for(i = 0; i < data->numLayers; ++i) {
357                 typeInfo = layerType_getInfo(data->layers[i].type);
358
359                 data->layers[i].offset = offset;
360                 offset += typeInfo->size;
361         }
362
363         data->totSize = offset;
364 }
365
366 void CustomData_from_template(const CustomData *source, CustomData *dest,
367                               int flag, int maxElems)
368 {
369         int i, layerflag;
370
371         CustomData_init(dest, source->maxLayers, maxElems, source->subElems);
372
373         for(i = 0; i < source->numLayers; ++i) {
374                 if(source->layers[i].flag & LAYERFLAG_NOCOPY) continue;
375
376                 layerflag = (source->layers[i].flag & ~LAYERFLAG_NOFREE) | flag;
377                 CustomData_add_layer(dest, source->layers[i].type, layerflag, NULL);
378         }
379
380         CustomData_update_offsets(dest);
381 }
382
383 void CustomData_free(CustomData *data)
384 {
385         int i;
386         const LayerTypeInfo *typeInfo;
387
388         for(i = 0; i < data->numLayers; ++i) {
389                 if(!(data->layers[i].flag & LAYERFLAG_NOFREE)) {
390                         typeInfo = layerType_getInfo(data->layers[i].type);
391                         if(typeInfo->free)
392                                 typeInfo->free(data->layers[i].data, data->numElems,
393                                                typeInfo->size);
394
395                         if(data->layers[i].data)
396                                 MEM_freeN(data->layers[i].data);
397                 }
398         }
399
400         data->numLayers = 0;
401
402         if(data->layers) {
403                 MEM_freeN(data->layers);
404                 data->layers = NULL;
405         }
406 }
407
408 /* gets index of first layer matching type after start_index
409  * if start_index < 0, starts searching at 0
410  * returns -1 if there is no layer of type
411  */
412 static int CustomData_find_next(const CustomData *data, int type,
413                                 int start_index)
414 {
415         int i = start_index + 1;
416
417         if(i < 0) i = 0;
418
419         for(; i < data->numLayers; ++i)
420                 if(data->layers[i].type == type) return i;
421
422         return -1;
423 }
424
425 static int customData_resize(CustomData *data, int amount)
426 {
427         CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxLayers + amount),
428                                        "CustomData->layers");
429         if(!tmp) return 0;
430
431         data->maxLayers += amount;
432         memcpy(tmp, data->layers, sizeof(*tmp) * data->numLayers);
433
434         MEM_freeN(data->layers);
435         data->layers = tmp;
436
437         return 1;
438 }
439
440 static int customData_add_layer__internal(CustomData *data, int type,
441                                           int flag, void *layer)
442 {
443         int index = data->numLayers;
444
445         if(index >= data->maxLayers)
446                 if(!customData_resize(data, CUSTOMDATA_GROW)) return 0;
447         
448         /* keep layers ordered by type */
449         for( ; index > 0 && data->layers[index - 1].type > type; --index)
450                 data->layers[index] = data->layers[index - 1];
451
452         data->layers[index].type = type;
453         data->layers[index].flag = flag;
454         data->layers[index].data = layer;
455
456         data->numLayers++;
457
458         CustomData_update_offsets(data);
459
460         return 1;
461 }
462
463 int CustomData_add_layer(CustomData *data, int type, int flag, void *layer)
464 {
465         int size = layerType_getInfo(type)->size * data->numElems;
466         void *tmp_layer = layer;
467
468         if(!layer) tmp_layer = MEM_callocN(size, "CustomDataLayer.data");
469
470         if(!tmp_layer) return 0;
471
472         if(customData_add_layer__internal(data, type, flag, tmp_layer))
473                 return 1;
474         else {
475                 MEM_freeN(tmp_layer);
476                 return 0;
477         }
478 }
479
480 int CustomData_free_layer(CustomData *data, int type)
481 {
482         int index = CustomData_find_next(data, type, -1);
483
484         if (index < 0) return 0;
485
486         for(++index; index < data->numLayers; ++index)
487                 data->layers[index - 1] = data->layers[index];
488
489         data->numLayers--;
490
491         if(data->numLayers <= data->maxLayers-CUSTOMDATA_GROW)
492                 customData_resize(data, -CUSTOMDATA_GROW);
493
494         CustomData_update_offsets(data);
495
496         return 1;
497 }
498
499 int CustomData_has_layer(const struct CustomData *data, int type)
500 {
501         return (CustomData_find_next(data, type, -1) != -1);
502 }
503
504 int CustomData_compat(const CustomData *data1, const CustomData *data2)
505 {
506         int i;
507
508         if(data1->numLayers != data2->numLayers) return 0;
509
510         for(i = 0; i < data1->numLayers; ++i) {
511                 if(data1->layers[i].type != data2->layers[i].type) return 0;
512                 if(data1->layers[i].flag != data2->layers[i].flag) return 0;
513         }
514
515         return 1;
516 }
517
518 int CustomData_copy_data(const CustomData *source, CustomData *dest,
519                          int source_index, int dest_index, int count)
520 {
521         const LayerTypeInfo *type_info;
522         int src_i, dest_i;
523         int src_offset;
524         int dest_offset;
525
526         if(count < 0) return 0;
527         if(source_index < 0 || (source_index + count) > source->numElems)
528                 return 0;
529         if(dest_index < 0 || (dest_index + count) > dest->numElems)
530                 return 0;
531
532         /* copies a layer at a time */
533         dest_i = 0;
534         for(src_i = 0; src_i < source->numLayers; ++src_i) {
535                 if(source->layers[src_i].flag & LAYERFLAG_NOCOPY) continue;
536
537                 /* find the first dest layer with type >= the source type
538                  * (this should work because layers are ordered by type)
539                  */
540                 while(dest_i < dest->numLayers
541                       && dest->layers[dest_i].type < source->layers[src_i].type)
542                         ++dest_i;
543
544                 /* if there are no more dest layers, we're done */
545                 if(dest_i >= dest->numLayers) return 1;
546
547                 /* if we found a matching layer, copy the data */
548                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
549                         char *src_data = source->layers[src_i].data;
550                         char *dest_data = dest->layers[dest_i].data;
551
552                         type_info = layerType_getInfo(source->layers[src_i].type);
553
554                         src_offset = source_index * type_info->size;
555                         dest_offset = dest_index * type_info->size;
556
557                         if(type_info->copy)
558                                 type_info->copy(src_data + src_offset,
559                                                 dest_data + dest_offset,
560                                                 count, type_info->size);
561                         else
562                                 memcpy(dest_data + dest_offset,
563                                        src_data + src_offset,
564                                        count * type_info->size);
565
566                         /* if there are multiple source & dest layers of the same type,
567                          * we don't want to copy all source layers to the same dest, so
568                          * increment dest_i
569                          */
570                         ++dest_i;
571                 }
572         }
573
574         return 1;
575 }
576
577 int CustomData_free_elem(CustomData *data, int index, int count)
578 {
579         int i;
580         const LayerTypeInfo *typeInfo;
581
582         if(index < 0 || count <= 0 || index + count > data->numElems) return 0;
583
584         for(i = 0; i < data->numLayers; ++i) {
585                 if(!(data->layers[i].flag & LAYERFLAG_NOFREE)) {
586                         typeInfo = layerType_getInfo(data->layers[i].type);
587
588                         if(typeInfo->free) {
589                                 int offset = typeInfo->size * index;
590
591                                 typeInfo->free((char *)data->layers[i].data + offset,
592                                                count, typeInfo->size);
593                         }
594                 }
595         }
596
597         return 1;
598 }
599
600 #define SOURCE_BUF_SIZE 100
601
602 int CustomData_interp(const CustomData *source, CustomData *dest,
603                       int *src_indices, float *weights, float *sub_weights,
604                       int count, int dest_index)
605 {
606         int src_i, dest_i;
607         int dest_offset;
608         int j;
609         void *source_buf[SOURCE_BUF_SIZE];
610         void **sources = source_buf;
611
612         if(count <= 0) return 0;
613         if(dest_index < 0 || dest_index >= dest->numElems) return 0;
614
615         /* slow fallback in case we're interpolating a ridiculous number of
616          * elements
617          */
618         if(count > SOURCE_BUF_SIZE)
619                 sources = MEM_callocN(sizeof(*sources) * count,
620                                       "CustomData_interp sources");
621
622         /* interpolates a layer at a time */
623         for(src_i = 0; src_i < source->numLayers; ++src_i) {
624                 CustomDataLayer *source_layer = &source->layers[src_i];
625                 const LayerTypeInfo *type_info =
626                                         layerType_getInfo(source_layer->type);
627
628                 dest_i = CustomData_find_next(dest, source_layer->type, -1);
629
630                 if(dest_i >= 0 && type_info->interp) {
631                         void *src_data = source_layer->data; 
632
633                         for(j = 0; j < count; ++j)
634                                 sources[j] = (char *)src_data
635                                              + type_info->size * src_indices[j];
636
637                         dest_offset = dest_index * type_info->size;
638
639                         type_info->interp(sources, weights, sub_weights, count,
640                                        (char *)dest->layers[dest_i].data + dest_offset);
641                 }
642         }
643
644         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
645         return 1;
646 }
647
648 void *CustomData_get(const CustomData *data, int index, int type)
649 {
650         int offset;
651         int layer_index;
652         
653         if(index < 0 || index > data->numElems) return NULL;
654
655         /* get the layer index of the first layer of type */
656         layer_index = CustomData_find_next(data, type, -1);
657         if(layer_index < 0) return NULL;
658
659         /* get the offset of the desired element */
660         offset = layerType_getInfo(type)->size * index;
661
662         return (char *)data->layers[layer_index].data + offset;
663 }
664
665 void *CustomData_get_layer(const CustomData *data, int type)
666 {
667         /* get the layer index of the first layer of type */
668         int layer_index = CustomData_find_next(data, type, -1);
669
670         if(layer_index < 0) return NULL;
671
672         return data->layers[layer_index].data;
673 }
674
675 void CustomData_set(const CustomData *data, int index, int type, void *source)
676 {
677         void *dest = CustomData_get(data, index, type);
678         const LayerTypeInfo *type_info = layerType_getInfo(type);
679
680         if(!dest) return;
681
682         if(type_info->copy)
683                 type_info->copy(source, dest, 1, type_info->size);
684         else
685                 memcpy(dest, source, type_info->size);
686 }
687
688 void CustomData_set_num_elems(CustomData *data, int numElems)
689 {
690         if(numElems < 0) return;
691         if(numElems < data->maxElems) data->numElems = numElems;
692         else data->numElems = data->maxElems;
693 }
694
695 /* EditMesh functions */
696
697 static void CustomData_em_alloc_block(CustomData *data, void **block)
698 {
699         /* TODO: optimize free/alloc */
700
701         if (*block)
702                 MEM_freeN(*block);
703
704         if (data->totSize > 0)
705                 *block = MEM_callocN(data->totSize, "CustomData EM block");
706         else
707                 *block = NULL;
708 }
709
710 void CustomData_em_free_block(CustomData *data, void **block)
711 {
712         if (*block) {
713                 MEM_freeN(*block);
714                 *block = NULL;
715         }
716 }
717
718 int CustomData_em_copy_data(CustomData *data, void *src_block, void **dest_block)
719 {
720         const LayerTypeInfo *type_info;
721         int i;
722
723         if (!*dest_block)
724                 CustomData_em_alloc_block(data, dest_block);
725         
726         /* copies a layer at a time */
727         for(i = 0; i < data->numLayers; ++i) {
728                 int offset = data->layers[i].offset;
729                 char *src_data = (char*)src_block + offset;
730                 char *dest_data = (char*)*dest_block + offset;
731
732                 type_info = layerType_getInfo(data->layers[i].type);
733
734                 if(type_info->copy)
735                         type_info->copy(src_data, dest_data, 1, type_info->size);
736                 else
737                         memcpy(dest_data, src_data, type_info->size);
738         }
739
740         return 1;
741 }
742
743 void *CustomData_em_get(const CustomData *data, void *block, int type)
744 {
745         int layer_index;
746         
747         /* get the layer index of the first layer of type */
748         layer_index = CustomData_find_next(data, type, -1);
749         if(layer_index < 0) return NULL;
750
751         return (char *)block + data->layers[layer_index].offset;
752 }
753
754 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
755 {
756         void *dest = CustomData_em_get(data, index, type);
757         const LayerTypeInfo *type_info = layerType_getInfo(type);
758
759         if(!dest) return;
760
761         if(type_info->copy)
762                 type_info->copy(source, dest, 1, type_info->size);
763         else
764                 memcpy(dest, source, type_info->size);
765 }
766
767 int CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
768                          float *sub_weights, int count, void *dest_block)
769 {
770         int i, j;
771         void *source_buf[SOURCE_BUF_SIZE];
772         void **sources = source_buf;
773
774         if(count <= 0) return 0;
775
776         /* slow fallback in case we're interpolating a ridiculous number of
777          * elements
778          */
779         if(count > SOURCE_BUF_SIZE)
780                 sources = MEM_callocN(sizeof(*sources) * count,
781                                       "CustomData_interp sources");
782
783         /* interpolates a layer at a time */
784         for(i = 0; i < data->numLayers; ++i) {
785                 CustomDataLayer *layer = &data->layers[i];
786                 const LayerTypeInfo *type_info = layerType_getInfo(layer->type);
787
788                 if(type_info->interp) {
789                         for(j = 0; j < count; ++j)
790                                 sources[j] = (char *)src_blocks[j] + layer->offset;
791
792                         type_info->interp(sources, weights, sub_weights, count,
793                                           (char *)dest_block + layer->offset);
794                 }
795         }
796
797         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
798         return 1;
799 }
800
801 void CustomData_em_set_default(CustomData *data, void **block)
802 {
803         const LayerTypeInfo *type_info;
804         int i;
805
806         if (!*block)
807                 CustomData_em_alloc_block(data, block);
808
809         for(i = 0; i < data->numLayers; ++i) {
810                 int offset = data->layers[i].offset;
811
812                 type_info = layerType_getInfo(data->layers[i].type);
813
814                 if(type_info->set_default)
815                         type_info->set_default((char*)*block + offset);
816         }
817 }
818
819 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
820                             int src_index, void **dest_block)
821 {
822         const LayerTypeInfo *type_info;
823         int i, src_offset;
824
825         if (!*dest_block)
826                 CustomData_em_alloc_block(dest, dest_block);
827         
828         /* copies a layer at a time */
829         for(i = 0; i < dest->numLayers; ++i) {
830                 int offset = dest->layers[i].offset;
831                 char *src_data = source->layers[i].data;
832                 char *dest_data = (char*)*dest_block + offset;
833
834                 type_info = layerType_getInfo(dest->layers[i].type);
835                 src_offset = src_index * type_info->size;
836
837                 if(type_info->copy)
838                         type_info->copy(src_data + src_offset, dest_data, 1,
839                                         type_info->size);
840                 else
841                         memcpy(dest_data, src_data + src_offset, type_info->size);
842         }
843 }
844
845 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
846                               void *src_block, int dest_index)
847 {
848         const LayerTypeInfo *type_info;
849         int i, dest_offset;
850
851         /* copies a layer at a time */
852         for(i = 0; i < dest->numLayers; ++i) {
853                 int offset = source->layers[i].offset;
854                 char *src_data = (char*)src_block + offset;
855                 char *dest_data = dest->layers[i].data;
856
857                 type_info = layerType_getInfo(dest->layers[i].type);
858                 dest_offset = dest_index * type_info->size;
859
860                 if(type_info->copy)
861                         type_info->copy(src_data, dest_data + dest_offset, 1,
862                                         type_info->size);
863                 else
864                         memcpy(dest_data + dest_offset, src_data, type_info->size);
865         }
866 }
867