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