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