Remove unused code left from old multires interpolation stuff
[blender.git] / source / blender / blenkernel / intern / customdata.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Ben Batt <benbatt@gmail.com>
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  * Implementation of CustomData.
28  *
29  * BKE_customdata.h contains the function prototypes for this file.
30  *
31  */
32
33 /** \file blender/blenkernel/intern/customdata.c
34  *  \ingroup bke
35  */
36  
37
38 #include <math.h>
39 #include <string.h>
40 #include <assert.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "DNA_meshdata_types.h"
45 #include "DNA_ID.h"
46
47 #include "BLI_utildefines.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_linklist.h"
50 #include "BLI_math.h"
51 #include "BLI_mempool.h"
52 #include "BLI_utildefines.h"
53
54 #include "BKE_customdata.h"
55 #include "BKE_customdata_file.h"
56 #include "BKE_global.h"
57 #include "BKE_main.h"
58 #include "BKE_multires.h"
59
60 #include "bmesh.h"
61
62 #include <math.h>
63 #include <string.h>
64
65 /* number of layers to add when growing a CustomData object */
66 #define CUSTOMDATA_GROW 5
67
68 /********************* Layer type information **********************/
69 typedef struct LayerTypeInfo {
70         int size;          /* the memory size of one element of this layer's data */
71         const char *structname;  /* name of the struct used, for file writing */
72         int structnum;     /* number of structs per element, for file writing */
73         const char *defaultname; /* default layer name */
74
75         /* a function to copy count elements of this layer's data
76          * (deep copy if appropriate)
77          * if NULL, memcpy is used
78          */
79         void (*copy)(const void *source, void *dest, int count);
80
81         /* a function to free any dynamically allocated components of this
82          * layer's data (note the data pointer itself should not be freed)
83          * size should be the size of one element of this layer's data (e.g.
84          * LayerTypeInfo.size)
85          */
86         void (*free)(void *data, int count, int size);
87
88         /* a function to interpolate between count source elements of this
89          * layer's data and store the result in dest
90          * if weights == NULL or sub_weights == NULL, they should default to 1
91          *
92          * weights gives the weight for each element in sources
93          * sub_weights gives the sub-element weights for each element in sources
94          *    (there should be (sub element count)^2 weights per element)
95          * count gives the number of elements in sources
96          */
97         void (*interp)(void **sources, float *weights, float *sub_weights,
98                        int count, void *dest);
99
100         /* a function to swap the data in corners of the element */
101         void (*swap)(void *data, const int *corner_indices);
102
103         /* a function to set a layer's data to default values. if NULL, the
104            default is assumed to be all zeros */
105         void (*set_default)(void *data, int count);
106
107     /* functions necassary for geometry collapse*/
108         int (*equal)(void *data1, void *data2);
109         void (*multiply)(void *data, float fac);
110         void (*initminmax)(void *min, void *max);
111         void (*add)(void *data1, void *data2);
112         void (*dominmax)(void *data1, void *min, void *max);
113         void (*copyvalue)(void *source, void *dest);
114
115         /* a function to read data from a cdf file */
116         int (*read)(CDataFile *cdf, void *data, int count);
117
118         /* a function to write data to a cdf file */
119         int (*write)(CDataFile *cdf, void *data, int count);
120
121         /* a function to determine file size */
122         size_t (*filesize)(CDataFile *cdf, void *data, int count);
123 } LayerTypeInfo;
124
125 static void layerCopy_mdeformvert(const void *source, void *dest,
126                                                                   int count)
127 {
128         int i, size = sizeof(MDeformVert);
129
130         memcpy(dest, source, count * size);
131
132         for(i = 0; i < count; ++i) {
133                 MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
134
135                 if(dvert->totweight) {
136                         MDeformWeight *dw = MEM_callocN(dvert->totweight * sizeof(*dw),
137                                                                                         "layerCopy_mdeformvert dw");
138
139                         memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
140                         dvert->dw = dw;
141                 }
142                 else
143                         dvert->dw = NULL;
144         }
145 }
146
147 static void layerFree_mdeformvert(void *data, int count, int size)
148 {
149         int i;
150
151         for(i = 0; i < count; ++i) {
152                 MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
153
154                 if(dvert->dw) {
155                         MEM_freeN(dvert->dw);
156                         dvert->dw = NULL;
157                         dvert->totweight = 0;
158                 }
159         }
160 }
161
162 static void linklist_free_simple(void *link)
163 {
164         MEM_freeN(link);
165 }
166
167 static void layerInterp_mdeformvert(void **sources, float *weights,
168                                                                         float *UNUSED(sub_weights), int count, void *dest)
169 {
170         MDeformVert *dvert = dest;
171         LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
172         LinkNode *node;
173         int i, j, totweight;
174
175         if(count <= 0) return;
176
177         /* build a list of unique def_nrs for dest */
178         totweight = 0;
179         for(i = 0; i < count; ++i) {
180                 MDeformVert *source = sources[i];
181                 float interp_weight = weights ? weights[i] : 1.0f;
182
183                 for(j = 0; j < source->totweight; ++j) {
184                         MDeformWeight *dw = &source->dw[j];
185
186                         for(node = dest_dw; node; node = node->next) {
187                                 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
188
189                                 if(tmp_dw->def_nr == dw->def_nr) {
190                                         tmp_dw->weight += dw->weight * interp_weight;
191                                         break;
192                                 }
193                         }
194
195                         /* if this def_nr is not in the list, add it */
196                         if(!node) {
197                                 MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
198                                                                                         "layerInterp_mdeformvert tmp_dw");
199                                 tmp_dw->def_nr = dw->def_nr;
200                                 tmp_dw->weight = dw->weight * interp_weight;
201                                 BLI_linklist_prepend(&dest_dw, tmp_dw);
202                                 totweight++;
203                         }
204                 }
205         }
206
207         /* now we know how many unique deform weights there are, so realloc */
208         if(dvert->dw) MEM_freeN(dvert->dw);
209
210         if(totweight) {
211                 dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
212                                                                 "layerInterp_mdeformvert dvert->dw");
213                 dvert->totweight = totweight;
214
215                 for(i = 0, node = dest_dw; node; node = node->next, ++i)
216                         dvert->dw[i] = *((MDeformWeight *)node->link);
217         }
218         else
219                 memset(dvert, 0, sizeof(*dvert));
220
221         BLI_linklist_free(dest_dw, linklist_free_simple);
222 }
223
224
225 static void layerInterp_msticky(void **sources, float *weights,
226                                                                 float *UNUSED(sub_weights), int count, void *dest)
227 {
228         float co[2], w;
229         MSticky *mst;
230         int i;
231
232         co[0] = co[1] = 0.0f;
233         for(i = 0; i < count; i++) {
234                 w = weights ? weights[i] : 1.0f;
235                 mst = (MSticky*)sources[i];
236
237                 madd_v2_v2fl(co, mst->co, w);
238         }
239
240         mst = (MSticky*)dest;
241         copy_v2_v2(mst->co, co);
242 }
243
244
245 static void layerCopy_tface(const void *source, void *dest, int count)
246 {
247         const MTFace *source_tf = (const MTFace*)source;
248         MTFace *dest_tf = (MTFace*)dest;
249         int i;
250
251         for(i = 0; i < count; ++i)
252                 dest_tf[i] = source_tf[i];
253 }
254
255 static void layerInterp_tface(void **sources, float *weights,
256                                                           float *sub_weights, int count, void *dest)
257 {
258         MTFace *tf = dest;
259         int i, j, k;
260         float uv[4][2] = {{0.0f}};
261         float *sub_weight;
262
263         if(count <= 0) return;
264
265         sub_weight = sub_weights;
266         for(i = 0; i < count; ++i) {
267                 float weight = weights ? weights[i] : 1;
268                 MTFace *src = sources[i];
269
270                 for(j = 0; j < 4; ++j) {
271                         if(sub_weights) {
272                                 for(k = 0; k < 4; ++k, ++sub_weight) {
273                                         madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
274                                 }
275                         }
276                         else {
277                                 madd_v2_v2fl(uv[j], src->uv[j], weight);
278                         }
279                 }
280         }
281
282         *tf = *(MTFace *)(*sources);
283         memcpy(tf->uv, uv, sizeof(tf->uv));
284 }
285
286 static void layerSwap_tface(void *data, const int *corner_indices)
287 {
288         MTFace *tf = data;
289         float uv[4][2];
290         static const short pin_flags[4] =
291                 { TF_PIN1, TF_PIN2, TF_PIN3, TF_PIN4 };
292         static const char sel_flags[4] =
293                 { TF_SEL1, TF_SEL2, TF_SEL3, TF_SEL4 };
294         short unwrap = tf->unwrap & ~(TF_PIN1 | TF_PIN2 | TF_PIN3 | TF_PIN4);
295         char flag = tf->flag & ~(TF_SEL1 | TF_SEL2 | TF_SEL3 | TF_SEL4);
296         int j;
297
298         for(j = 0; j < 4; ++j) {
299                 const int source_index = corner_indices[j];
300
301                 copy_v2_v2(uv[j], tf->uv[source_index]);
302
303                 // swap pinning flags around
304                 if(tf->unwrap & pin_flags[source_index]) {
305                         unwrap |= pin_flags[j];
306                 }
307
308                 // swap selection flags around
309                 if(tf->flag & sel_flags[source_index]) {
310                         flag |= sel_flags[j];
311                 }
312         }
313
314         memcpy(tf->uv, uv, sizeof(tf->uv));
315         tf->unwrap = unwrap;
316         tf->flag = flag;
317 }
318
319 static void layerDefault_tface(void *data, int count)
320 {
321         static MTFace default_tf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, NULL,
322                                                            0, 0, TF_DYNAMIC|TF_CONVERTED, 0, 0};
323         MTFace *tf = (MTFace*)data;
324         int i;
325
326         for(i = 0; i < count; i++)
327                 tf[i] = default_tf;
328 }
329
330 static void layerCopy_propFloat(const void *source, void *dest,
331                                                                   int count)
332 {
333         memcpy(dest, source, sizeof(MFloatProperty)*count);
334 }
335
336 static void layerCopy_propInt(const void *source, void *dest,
337                                                                   int count)
338 {
339         memcpy(dest, source, sizeof(MIntProperty)*count);
340 }
341
342 static void layerCopy_propString(const void *source, void *dest,
343                                                                   int count)
344 {
345         memcpy(dest, source, sizeof(MStringProperty)*count);
346 }
347
348 static void layerCopy_origspace_face(const void *source, void *dest, int count)
349 {
350         const OrigSpaceFace *source_tf = (const OrigSpaceFace*)source;
351         OrigSpaceFace *dest_tf = (OrigSpaceFace*)dest;
352         int i;
353
354         for(i = 0; i < count; ++i)
355                 dest_tf[i] = source_tf[i];
356 }
357
358 static void layerInterp_origspace_face(void **sources, float *weights,
359                                                           float *sub_weights, int count, void *dest)
360 {
361         OrigSpaceFace *osf = dest;
362         int i, j, k;
363         float uv[4][2] = {{0.0f}};
364         float *sub_weight;
365
366         if(count <= 0) return;
367
368         sub_weight = sub_weights;
369         for(i = 0; i < count; ++i) {
370                 float weight = weights ? weights[i] : 1;
371                 OrigSpaceFace *src = sources[i];
372
373                 for(j = 0; j < 4; ++j) {
374                         if(sub_weights) {
375                                 for(k = 0; k < 4; ++k, ++sub_weight) {
376                                         madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
377                                 }
378                         } else {
379                                 madd_v2_v2fl(uv[j], src->uv[j], weight);
380                         }
381                 }
382         }
383
384 #if 0 /* no need, this ONLY contains UV's */
385         *osf = *(OrigSpaceFace *)(*sources);
386 #endif
387         memcpy(osf->uv, uv, sizeof(osf->uv));
388 }
389
390 static void layerSwap_origspace_face(void *data, const int *corner_indices)
391 {
392         OrigSpaceFace *osf = data;
393         float uv[4][2];
394         int j;
395
396         for(j = 0; j < 4; ++j) {
397                 copy_v2_v2(uv[j], osf->uv[corner_indices[j]]);
398         }
399         memcpy(osf->uv, uv, sizeof(osf->uv));
400 }
401
402 static void layerDefault_origspace_face(void *data, int count)
403 {
404         static OrigSpaceFace default_osf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
405         OrigSpaceFace *osf = (OrigSpaceFace*)data;
406         int i;
407
408         for(i = 0; i < count; i++)
409                 osf[i] = default_osf;
410 }
411
412 static void layerSwap_mdisps(void *data, const int *ci)
413 {
414         MDisps *s = data;
415         float (*d)[3] = NULL;
416         int corners, cornersize, S;
417
418         if(s->disps) {
419                 int nverts= (ci[1] == 3) ? 4 : 3; /* silly way to know vertex count of face */
420                 corners= multires_mdisp_corners(s);
421                 cornersize= s->totdisp/corners;
422
423                 if(corners!=nverts) {
424                         /* happens when face changed vertex count in edit mode
425                            if it happened, just forgot displacement */
426
427                         MEM_freeN(s->disps);
428                         s->totdisp= (s->totdisp/corners)*nverts;
429                         s->disps= MEM_callocN(s->totdisp*sizeof(float)*3, "mdisp swap");
430                         return;
431                 }
432
433                 d= MEM_callocN(sizeof(float) * 3 * s->totdisp, "mdisps swap");
434
435                 for(S = 0; S < corners; S++)
436                         memcpy(d + cornersize*S, s->disps + cornersize*ci[S], cornersize*3*sizeof(float));
437                 
438                 MEM_freeN(s->disps);
439                 s->disps= d;
440         }
441 }
442
443 static void layerCopy_mdisps(const void *source, void *dest, int count)
444 {
445         int i;
446         const MDisps *s = source;
447         MDisps *d = dest;
448
449         for(i = 0; i < count; ++i) {
450                 if(s[i].disps) {
451                         d[i].disps = MEM_dupallocN(s[i].disps);
452                         d[i].totdisp = s[i].totdisp;
453                 }
454                 else {
455                         d[i].disps = NULL;
456                         d[i].totdisp = 0;
457                 }
458                 
459         }
460 }
461
462 static void layerFree_mdisps(void *data, int count, int UNUSED(size))
463 {
464         int i;
465         MDisps *d = data;
466
467         for(i = 0; i < count; ++i) {
468                 if(d[i].disps)
469                         MEM_freeN(d[i].disps);
470                 d[i].disps = NULL;
471                 d[i].totdisp = 0;
472         }
473 }
474
475 static int layerRead_mdisps(CDataFile *cdf, void *data, int count)
476 {
477         MDisps *d = data;
478         int i;
479
480         for(i = 0; i < count; ++i) {
481                 if(!d[i].disps)
482                         d[i].disps = MEM_callocN(sizeof(float)*3*d[i].totdisp, "mdisps read");
483
484                 if(!cdf_read_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
485                         printf("failed to read multires displacement %d/%d %d\n", i, count, d[i].totdisp);
486                         return 0;
487                 }
488         }
489
490         return 1;
491 }
492
493 static int layerWrite_mdisps(CDataFile *cdf, void *data, int count)
494 {
495         MDisps *d = data;
496         int i;
497
498         for(i = 0; i < count; ++i) {
499                 if(!cdf_write_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
500                         printf("failed to write multires displacement %d/%d %d\n", i, count, d[i].totdisp);
501                         return 0;
502                 }
503         }
504
505         return 1;
506 }
507
508 static size_t layerFilesize_mdisps(CDataFile *UNUSED(cdf), void *data, int count)
509 {
510         MDisps *d = data;
511         size_t size = 0;
512         int i;
513
514         for(i = 0; i < count; ++i)
515                 size += d[i].totdisp*3*sizeof(float);
516
517         return size;
518 }
519
520 /* --------- */
521 static void layerCopyValue_mloopcol(void *source, void *dest)
522 {
523         MLoopCol *m1 = source, *m2 = dest;
524         
525         m2->r = m1->r;
526         m2->g = m1->g;
527         m2->b = m1->b;
528         m2->a = m1->a;
529 }
530
531 static int layerEqual_mloopcol(void *data1, void *data2)
532 {
533         MLoopCol *m1 = data1, *m2 = data2;
534         float r, g, b, a;
535
536         r = m1->r - m2->r;
537         g = m1->g - m2->g;
538         b = m1->b - m2->b;
539         a = m1->a - m2->a;
540
541         return r*r + g*g + b*b + a*a < 0.001;
542 }
543
544 static void layerMultiply_mloopcol(void *data, float fac)
545 {
546         MLoopCol *m = data;
547
548         m->r = (float)m->r * fac;
549         m->g = (float)m->g * fac;
550         m->b = (float)m->b * fac;
551         m->a = (float)m->a * fac;
552 }
553
554 static void layerAdd_mloopcol(void *data1, void *data2)
555 {
556         MLoopCol *m = data1, *m2 = data2;
557
558         m->r += m2->r;
559         m->g += m2->g;
560         m->b += m2->b;
561         m->a += m2->a;
562 }
563
564 static void layerDoMinMax_mloopcol(void *data, void *vmin, void *vmax)
565 {
566         MLoopCol *m = data;
567         MLoopCol *min = vmin, *max = vmax;
568
569         if (m->r < min->r) min->r = m->r;
570         if (m->g < min->g) min->g = m->g;
571         if (m->b < min->b) min->b = m->b;
572         if (m->a < min->a) min->a = m->a;
573         
574         if (m->r > max->r) max->r = m->r;
575         if (m->g > max->g) max->g = m->g;
576         if (m->b > max->b) max->b = m->b;
577         if (m->a > max->a) max->a = m->a;
578 }
579
580 static void layerInitMinMax_mloopcol(void *vmin, void *vmax)
581 {
582         MLoopCol *min = vmin, *max = vmax;
583
584         min->r = 255;
585         min->g = 255;
586         min->b = 255;
587         min->a = 255;
588
589         max->r = 0;
590         max->g = 0;
591         max->b = 0;
592         max->a = 0;
593 }
594
595 static void layerDefault_mloopcol(void *data, int count)
596 {
597         MLoopCol default_mloopcol = {255,255,255,255};
598         MLoopCol *mlcol = (MLoopCol*)data;
599         int i;
600         for(i = 0; i < count; i++)
601                 mlcol[i] = default_mloopcol;
602
603 }
604
605 static void layerInterp_mloopcol(void **sources, float *weights,
606                                 float *sub_weights, int count, void *dest)
607 {
608         MLoopCol *mc = dest;
609         int i;
610         float *sub_weight;
611         struct {
612                 float a;
613                 float r;
614                 float g;
615                 float b;
616         } col;
617         col.a = col.r = col.g = col.b = 0;
618
619         sub_weight = sub_weights;
620         for(i = 0; i < count; ++i){
621                 float weight = weights ? weights[i] : 1;
622                 MLoopCol *src = sources[i];
623                 if(sub_weights){
624                         col.a += src->a * (*sub_weight) * weight;
625                         col.r += src->r * (*sub_weight) * weight;
626                         col.g += src->g * (*sub_weight) * weight;
627                         col.b += src->b * (*sub_weight) * weight;
628                         sub_weight++;
629                 } else {
630                         col.a += src->a * weight;
631                         col.r += src->r * weight;
632                         col.g += src->g * weight;
633                         col.b += src->b * weight;
634                 }
635         }
636         
637         /* Subdivide smooth or fractal can cause problems without clamping
638          * although weights should also not cause this situation */
639         CLAMP(col.a, 0.0f, 255.0f);
640         CLAMP(col.r, 0.0f, 255.0f);
641         CLAMP(col.g, 0.0f, 255.0f);
642         CLAMP(col.b, 0.0f, 255.0f);
643         
644         mc->a = (int)col.a;
645         mc->r = (int)col.r;
646         mc->g = (int)col.g;
647         mc->b = (int)col.b;
648 }
649
650 static void layerCopyValue_mloopuv(void *source, void *dest)
651 {
652         MLoopUV *luv1 = source, *luv2 = dest;
653
654         copy_v2_v2(luv2->uv, luv1->uv);
655 }
656
657 static int layerEqual_mloopuv(void *data1, void *data2)
658 {
659         MLoopUV *luv1 = data1, *luv2 = data2;
660
661         return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
662 }
663
664 static void layerMultiply_mloopuv(void *data, float fac)
665 {
666         MLoopUV *luv = data;
667
668         mul_v2_fl(luv->uv, fac);
669 }
670
671 static void layerInitMinMax_mloopuv(void *vmin, void *vmax)
672 {
673         MLoopUV *min = vmin, *max = vmax;
674
675         INIT_MINMAX2(min->uv, max->uv);
676 }
677
678 static void layerDoMinMax_mloopuv(void *data, void *vmin, void *vmax)
679 {
680         MLoopUV *min = vmin, *max = vmax, *luv = data;
681
682         DO_MINMAX2(luv->uv, min->uv, max->uv);
683 }
684
685 static void layerAdd_mloopuv(void *data1, void *data2)
686 {
687         MLoopUV *l1 = data1, *l2 = data2;
688
689         add_v2_v2(l1->uv, l2->uv);
690 }
691
692 static void layerInterp_mloopuv(void **sources, float *weights,
693                                 float *sub_weights, int count, void *dest)
694 {
695         MLoopUV *mluv = dest;
696         float *uv= mluv->uv;
697         int i;
698
699         zero_v2(uv);
700
701         if (sub_weights) {
702                 const float *sub_weight = sub_weights;
703                 for(i = 0; i < count; i++) {
704                         float weight = weights ? weights[i] : 1.0f;
705                         MLoopUV *src = sources[i];
706                         madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
707                         sub_weight++;
708                 }
709         }
710         else {
711                 for(i = 0; i < count; i++) {
712                         float weight = weights ? weights[i] : 1;
713                         MLoopUV *src = sources[i];
714                         madd_v2_v2fl(uv, src->uv, weight);
715                 }
716         }
717 }
718
719 /* origspace is almost exact copy of mloopuv's, keep in sync */
720 static void layerCopyValue_mloop_origspace(void *source, void *dest)
721 {
722         OrigSpaceLoop *luv1 = source, *luv2 = dest;
723
724         copy_v2_v2(luv2->uv, luv1->uv);
725 }
726
727 static int layerEqual_mloop_origspace(void *data1, void *data2)
728 {
729         OrigSpaceLoop *luv1 = data1, *luv2 = data2;
730
731         return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
732 }
733
734 static void layerMultiply_mloop_origspace(void *data, float fac)
735 {
736         OrigSpaceLoop *luv = data;
737
738         mul_v2_fl(luv->uv, fac);
739 }
740
741 static void layerInitMinMax_mloop_origspace(void *vmin, void *vmax)
742 {
743         OrigSpaceLoop *min = vmin, *max = vmax;
744
745         INIT_MINMAX2(min->uv, max->uv);
746 }
747
748 static void layerDoMinMax_mloop_origspace(void *data, void *vmin, void *vmax)
749 {
750         OrigSpaceLoop *min = vmin, *max = vmax, *luv = data;
751
752         DO_MINMAX2(luv->uv, min->uv, max->uv);
753 }
754
755 static void layerAdd_mloop_origspace(void *data1, void *data2)
756 {
757         OrigSpaceLoop *l1 = data1, *l2 = data2;
758
759         add_v2_v2(l1->uv, l2->uv);
760 }
761
762 static void layerInterp_mloop_origspace(void **sources, float *weights,
763                                 float *sub_weights, int count, void *dest)
764 {
765         OrigSpaceLoop *mluv = dest;
766         float *uv= mluv->uv;
767         int i;
768
769         zero_v2(uv);
770
771         if (sub_weights) {
772                 const float *sub_weight = sub_weights;
773                 for(i = 0; i < count; i++) {
774                         float weight = weights ? weights[i] : 1.0f;
775                         OrigSpaceLoop *src = sources[i];
776                         madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
777                         sub_weight++;
778                 }
779         }
780         else {
781                 for(i = 0; i < count; i++) {
782                         float weight = weights ? weights[i] : 1;
783                         OrigSpaceLoop *src = sources[i];
784                         madd_v2_v2fl(uv, src->uv, weight);
785                 }
786         }
787 }
788 /* --- end copy */
789
790 static void layerInterp_mcol(void **sources, float *weights,
791                                                          float *sub_weights, int count, void *dest)
792 {
793         MCol *mc = dest;
794         int i, j, k;
795         struct {
796                 float a;
797                 float r;
798                 float g;
799                 float b;
800         } col[4] = {{0.0f}};
801
802         float *sub_weight;
803
804         if(count <= 0) return;
805         
806         sub_weight = sub_weights;
807         for(i = 0; i < count; ++i) {
808                 float weight = weights ? weights[i] : 1;
809
810                 for(j = 0; j < 4; ++j) {
811                         if(sub_weights) {
812                                 MCol *src = sources[i];
813                                 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
814                                         const float w= (*sub_weight) * weight;
815                                         col[j].a += src->a * w;
816                                         col[j].r += src->r * w;
817                                         col[j].g += src->g * w;
818                                         col[j].b += src->b * w;
819                                 }
820                         } else {
821                                 MCol *src = sources[i];
822                                 col[j].a += src[j].a * weight;
823                                 col[j].r += src[j].r * weight;
824                                 col[j].g += src[j].g * weight;
825                                 col[j].b += src[j].b * weight;
826                         }
827                 }
828         }
829
830         for(j = 0; j < 4; ++j) {
831                 
832                 /* Subdivide smooth or fractal can cause problems without clamping
833                  * although weights should also not cause this situation */
834                 CLAMP(col[j].a, 0.0f, 255.0f);
835                 CLAMP(col[j].r, 0.0f, 255.0f);
836                 CLAMP(col[j].g, 0.0f, 255.0f);
837                 CLAMP(col[j].b, 0.0f, 255.0f);
838                 
839                 mc[j].a = (int)col[j].a;
840                 mc[j].r = (int)col[j].r;
841                 mc[j].g = (int)col[j].g;
842                 mc[j].b = (int)col[j].b;
843         }
844 }
845
846 static void layerSwap_mcol(void *data, const int *corner_indices)
847 {
848         MCol *mcol = data;
849         MCol col[4];
850         int j;
851
852         for(j = 0; j < 4; ++j)
853                 col[j] = mcol[corner_indices[j]];
854
855         memcpy(mcol, col, sizeof(col));
856 }
857
858 static void layerDefault_mcol(void *data, int count)
859 {
860         static MCol default_mcol = {255, 255, 255, 255};
861         MCol *mcol = (MCol*)data;
862         int i;
863
864         for(i = 0; i < 4*count; i++) {
865                 mcol[i] = default_mcol;
866         }
867 }
868
869 static void layerInterp_bweight(void **sources, float *weights,
870                                 float *UNUSED(sub_weights), int count, void *dest)
871 {
872         float *f = dest;
873         float **in = (float **)sources;
874         int i;
875         
876         if(count <= 0) return;
877
878         *f = 0.0f;
879
880         if (weights) {
881                 for(i = 0; i < count; ++i) {
882                         *f += *in[i] * weights[i];
883                 }
884         }
885         else {
886                 for(i = 0; i < count; ++i) {
887                         *f += *in[i];
888                 }
889         }
890 }
891
892 static void layerInterp_shapekey(void **sources, float *weights,
893                                  float *UNUSED(sub_weights), int count, void *dest)
894 {
895         float *co = dest;
896         float **in = (float **)sources;
897         int i;
898
899         if(count <= 0) return;
900
901         zero_v3(co);
902
903         if (weights) {
904                 for(i = 0; i < count; ++i) {
905                         madd_v3_v3fl(co, in[i], weights[i]);
906                 }
907         }
908         else {
909                 for(i = 0; i < count; ++i) {
910                         add_v3_v3(co, in[i]);
911                 }
912         }
913 }
914
915 static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
916         /* 0: CD_MVERT */
917         {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
918         /* 1: CD_MSTICKY */
919         {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
920          NULL},
921         /* 2: CD_MDEFORMVERT */
922         {sizeof(MDeformVert), "MDeformVert", 1, NULL, layerCopy_mdeformvert,
923          layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
924         /* 3: CD_MEDGE */
925         {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL, NULL},
926         /* 4: CD_MFACE */
927         {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL, NULL},
928         /* 5: CD_MTFACE */
929         {sizeof(MTFace), "MTFace", 1, "UVMap", layerCopy_tface, NULL,
930          layerInterp_tface, layerSwap_tface, layerDefault_tface},
931         /* 6: CD_MCOL */
932         /* 4 MCol structs per face */
933         {sizeof(MCol)*4, "MCol", 4, "Col", NULL, NULL, layerInterp_mcol,
934          layerSwap_mcol, layerDefault_mcol},
935         /* 7: CD_ORIGINDEX */
936         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
937         /* 8: CD_NORMAL */
938         /* 3 floats per normal vector */
939         {sizeof(float)*3, "vec3f", 1, NULL, NULL, NULL, NULL, NULL, NULL},
940         /* 9: CD_POLYINDEX */
941         {sizeof(int), "MIntProperty", 1, NULL, NULL, NULL, NULL, NULL, NULL},
942         /* 10: CD_PROP_FLT */
943         {sizeof(MFloatProperty), "MFloatProperty",1,"Float", layerCopy_propFloat,NULL,NULL,NULL},
944         /* 11: CD_PROP_INT */
945         {sizeof(MIntProperty), "MIntProperty",1,"Int",layerCopy_propInt,NULL,NULL,NULL},
946         /* 12: CD_PROP_STR */
947         {sizeof(MStringProperty), "MStringProperty",1,"String",layerCopy_propString,NULL,NULL,NULL},
948         /* 13: CD_ORIGSPACE */
949         {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVMap", layerCopy_origspace_face, NULL,
950          layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face},
951         /* 14: CD_ORCO */
952         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
953         /* 15: CD_MTEXPOLY */
954         {sizeof(MTexPoly), "MTexPoly", 1, "Face Texture", NULL, NULL, NULL, NULL, NULL},
955         /* 16: CD_MLOOPUV */
956         {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, layerInterp_mloopuv, NULL, NULL,
957          layerEqual_mloopuv, layerMultiply_mloopuv, layerInitMinMax_mloopuv, 
958          layerAdd_mloopuv, layerDoMinMax_mloopuv, layerCopyValue_mloopuv},
959         /* 17: CD_MLOOPCOL */
960         {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL, 
961          layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol, 
962          layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
963         /* 18: CD_TANGENT */
964         {sizeof(float)*4*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
965         /* 19: CD_MDISPS */
966         {sizeof(MDisps), "MDisps", 1, NULL, layerCopy_mdisps,
967          layerFree_mdisps, NULL, layerSwap_mdisps, NULL,
968          NULL, NULL, NULL, NULL, NULL, NULL, 
969          layerRead_mdisps, layerWrite_mdisps, layerFilesize_mdisps},
970         /* 20: CD_WEIGHT_MCOL */
971         {sizeof(MCol)*4, "MCol", 4, "WeightCol", NULL, NULL, layerInterp_mcol,
972          layerSwap_mcol, layerDefault_mcol},
973         /* 21: CD_ID_MCOL */
974         {sizeof(MCol)*4, "MCol", 4, "IDCol", NULL, NULL, layerInterp_mcol,
975          layerSwap_mcol, layerDefault_mcol},
976         /* 22: CD_TEXTURE_MCOL */
977         {sizeof(MCol)*4, "MCol", 4, "TexturedCol", NULL, NULL, layerInterp_mcol,
978          layerSwap_mcol, layerDefault_mcol},
979         /* 23: CD_CLOTH_ORCO */
980         {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
981         /* 24: CD_RECAST */
982         {sizeof(MRecast), "MRecast", 1,"Recast",NULL,NULL,NULL,NULL}
983
984 /* BMESH ONLY */
985         ,
986         /* 25: CD_MPOLY */
987         {sizeof(MPoly), "MPoly", 1, "NGon Face", NULL, NULL, NULL, NULL, NULL},
988         /* 26: CD_MLOOP */
989         {sizeof(MLoop), "MLoop", 1, "NGon Face-Vertex", NULL, NULL, NULL, NULL, NULL},
990         /* 27: CD_SHAPE_KEYINDEX */
991         {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
992         /* 28: CD_SHAPEKEY */
993         {sizeof(float)*3, "", 0, "ShapeKey", NULL, NULL, layerInterp_shapekey},
994         /* 29: CD_BWEIGHT */
995         {sizeof(float), "", 0, "BevelWeight", NULL, NULL, layerInterp_bweight},
996         /* 30: CD_CREASE */
997         {sizeof(float), "", 0, "SubSurfCrease", NULL, NULL, layerInterp_bweight},
998     /* 31: CD_ORIGSPACE_MLOOP */
999         {sizeof(OrigSpaceLoop), "OrigSpaceLoop", 1, "OS Loop", NULL, NULL, layerInterp_mloop_origspace, NULL, NULL,
1000          layerEqual_mloop_origspace, layerMultiply_mloop_origspace, layerInitMinMax_mloop_origspace,
1001          layerAdd_mloop_origspace, layerDoMinMax_mloop_origspace, layerCopyValue_mloop_origspace},
1002         /* 32: CD_WEIGHT_MLOOPCOL */
1003         {sizeof(MLoopCol), "MLoopCol", 1, "WeightLoopCol", NULL, NULL, layerInterp_mloopcol, NULL,
1004          layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol,
1005          layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
1006 /* END BMESH ONLY */
1007
1008
1009 };
1010
1011 /* note, numbers are from trunk and need updating for bmesh */
1012
1013 static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
1014         /*   0-4 */ "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace",
1015         /*   5-9 */ "CDMTFace", "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags",
1016         /* 10-14 */ "CDMFloatProperty", "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco",
1017         /* 15-19 */ "CDMTexPoly", "CDMLoopUV", "CDMloopCol", "CDTangent", "CDMDisps",
1018         /* 20-24 */"CDWeightMCol", "CDIDMCol", "CDTextureMCol", "CDClothOrco", "CDMRecast"
1019
1020 /* BMESH ONLY */
1021         ,
1022         /* 25-29 */ "CDMPoly", "CDMLoop", "CDShapeKeyIndex", "CDShapeKey", "CDBevelWeight",
1023         /* 30-32 */ "CDSubSurfCrease", "CDOrigSpaceLoop", "CDWeightLoopCol"
1024 /* END BMESH ONLY */
1025
1026 };
1027
1028
1029 const CustomDataMask CD_MASK_BAREMESH =
1030         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE | CD_MASK_MLOOP | CD_MASK_MPOLY | CD_MASK_BWEIGHT;
1031 const CustomDataMask CD_MASK_MESH =
1032         CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
1033         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
1034         CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS |
1035         CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MPOLY | CD_MASK_MLOOP |
1036         CD_MASK_MTEXPOLY | CD_MASK_NORMAL | CD_MASK_RECAST;
1037 const CustomDataMask CD_MASK_EDITMESH =
1038         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MLOOPUV |
1039         CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_SHAPE_KEYINDEX |
1040         CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR |
1041         CD_MASK_MDISPS | CD_MASK_SHAPEKEY | CD_MASK_RECAST;
1042 const CustomDataMask CD_MASK_DERIVEDMESH =
1043         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
1044         CD_MASK_MCOL | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_CLOTH_ORCO |
1045         CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_WEIGHT_MLOOPCOL |
1046         CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORIGSPACE_MLOOP | CD_MASK_ORCO | CD_MASK_TANGENT |
1047         CD_MASK_WEIGHT_MCOL | CD_MASK_NORMAL | CD_MASK_SHAPEKEY | CD_MASK_RECAST |
1048         CD_MASK_ORIGINDEX | CD_MASK_POLYINDEX;
1049 const CustomDataMask CD_MASK_BMESH = CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY |
1050         CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | 
1051         CD_MASK_PROP_STR | CD_MASK_SHAPEKEY | CD_MASK_SHAPE_KEYINDEX | CD_MASK_MDISPS;
1052 const CustomDataMask CD_MASK_FACECORNERS =
1053         CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_MTEXPOLY | CD_MASK_MLOOPUV |
1054         CD_MASK_MLOOPCOL;
1055
1056 static const LayerTypeInfo *layerType_getInfo(int type)
1057 {
1058         if(type < 0 || type >= CD_NUMTYPES) return NULL;
1059
1060         return &LAYERTYPEINFO[type];
1061 }
1062
1063 static const char *layerType_getName(int type)
1064 {
1065         if(type < 0 || type >= CD_NUMTYPES) return NULL;
1066
1067         return LAYERTYPENAMES[type];
1068 }
1069
1070 /********************* CustomData functions *********************/
1071 static void customData_update_offsets(CustomData *data);
1072
1073 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1074         int type, int alloctype, void *layerdata, int totelem, const char *name);
1075
1076 void CustomData_update_typemap(CustomData *data)
1077 {
1078         int i, lasttype = -1;
1079
1080         /* since we cant do in a pre-processor do here as an assert */
1081         BLI_assert(sizeof(data->typemap) / sizeof(int) >= CD_NUMTYPES);
1082
1083         for (i=0; i<CD_NUMTYPES; i++) {
1084                 data->typemap[i] = -1;
1085         }
1086
1087         for (i=0; i<data->totlayer; i++) {
1088                 if (data->layers[i].type != lasttype) {
1089                         data->typemap[data->layers[i].type] = i;
1090                 }
1091                 lasttype = data->layers[i].type;
1092         }
1093 }
1094
1095 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
1096                                           CustomDataMask mask, int alloctype, int totelem)
1097 {
1098         /*const LayerTypeInfo *typeInfo;*/
1099         CustomDataLayer *layer, *newlayer;
1100         void *data;
1101         int i, type, number = 0, lasttype = -1, lastactive = 0, lastrender = 0, lastclone = 0, lastmask = 0, lastflag = 0;
1102
1103         for(i = 0; i < source->totlayer; ++i) {
1104                 layer = &source->layers[i];
1105                 /*typeInfo = layerType_getInfo(layer->type);*/ /*UNUSED*/
1106
1107                 type = layer->type;
1108
1109                 if (type != lasttype) {
1110                         number = 0;
1111                         lastactive = layer->active;
1112                         lastrender = layer->active_rnd;
1113                         lastclone = layer->active_clone;
1114                         lastmask = layer->active_mask;
1115                         lasttype = type;
1116                         lastflag = layer->flag;
1117                 }
1118                 else
1119                         number++;
1120
1121                 if(lastflag & CD_FLAG_NOCOPY) continue;
1122                 else if(!(mask & CD_TYPE_AS_MASK(type))) continue;
1123                 else if(number < CustomData_number_of_layers(dest, type)) continue;
1124
1125                 switch (alloctype) {
1126                         case CD_ASSIGN:
1127                         case CD_REFERENCE:
1128                         case CD_DUPLICATE:
1129                                 data = layer->data;
1130                                 break;
1131                         default:
1132                                 data = NULL;
1133                                 break;
1134                 }
1135
1136                 if((alloctype == CD_ASSIGN) && (lastflag & CD_FLAG_NOFREE))
1137                         newlayer = customData_add_layer__internal(dest, type, CD_REFERENCE,
1138                                 data, totelem, layer->name);
1139                 else
1140                         newlayer = customData_add_layer__internal(dest, type, alloctype,
1141                                 data, totelem, layer->name);
1142                 
1143                 if(newlayer) {
1144                         newlayer->uid = layer->uid;
1145                         
1146                         newlayer->active = lastactive;
1147                         newlayer->active_rnd = lastrender;
1148                         newlayer->active_clone = lastclone;
1149                         newlayer->active_mask = lastmask;
1150                         newlayer->flag |= lastflag & (CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY);
1151                 }
1152         }
1153
1154         CustomData_update_typemap(dest);
1155 }
1156
1157 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
1158                                          CustomDataMask mask, int alloctype, int totelem)
1159 {
1160         memset(dest, 0, sizeof(*dest));
1161
1162         if(source->external)
1163                 dest->external= MEM_dupallocN(source->external);
1164
1165         CustomData_merge(source, dest, mask, alloctype, totelem);
1166 }
1167
1168 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
1169 {
1170         const LayerTypeInfo *typeInfo;
1171
1172         if(!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
1173                 typeInfo = layerType_getInfo(layer->type);
1174
1175                 if(typeInfo->free)
1176                         typeInfo->free(layer->data, totelem, typeInfo->size);
1177
1178                 if(layer->data)
1179                         MEM_freeN(layer->data);
1180         }
1181 }
1182
1183 static void CustomData_external_free(CustomData *data)
1184 {
1185         if(data->external) {
1186                 MEM_freeN(data->external);
1187                 data->external= NULL;
1188         }
1189 }
1190
1191 void CustomData_free(CustomData *data, int totelem)
1192 {
1193         int i;
1194
1195         for(i = 0; i < data->totlayer; ++i)
1196                 customData_free_layer__internal(&data->layers[i], totelem);
1197
1198         if(data->layers)
1199                 MEM_freeN(data->layers);
1200         
1201         CustomData_external_free(data);
1202         
1203         memset(data, 0, sizeof(*data));
1204 }
1205
1206 static void customData_update_offsets(CustomData *data)
1207 {
1208         const LayerTypeInfo *typeInfo;
1209         int i, offset = 0;
1210
1211         for(i = 0; i < data->totlayer; ++i) {
1212                 typeInfo = layerType_getInfo(data->layers[i].type);
1213
1214                 data->layers[i].offset = offset;
1215                 offset += typeInfo->size;
1216         }
1217
1218         data->totsize = offset;
1219         CustomData_update_typemap(data);
1220 }
1221
1222 int CustomData_get_layer_index(const CustomData *data, int type)
1223 {
1224         int i; 
1225
1226         for(i=0; i < data->totlayer; ++i)
1227                 if(data->layers[i].type == type)
1228                         return i;
1229
1230         return -1;
1231 }
1232
1233 int CustomData_get_layer_index_n(const struct CustomData *data, int type, int n)
1234 {
1235         int i = CustomData_get_layer_index(data, type);
1236
1237         if (i != -1) {
1238                 i = (data->layers[i + n].type == type) ? (i + n) : (-1);
1239         }
1240
1241         return i;
1242 }
1243
1244 int CustomData_get_named_layer_index(const CustomData *data, int type, const char *name)
1245 {
1246         int i;
1247
1248         for(i=0; i < data->totlayer; ++i)
1249                 if(data->layers[i].type == type && strcmp(data->layers[i].name, name)==0)
1250                         return i;
1251
1252         return -1;
1253 }
1254
1255 int CustomData_get_active_layer_index(const CustomData *data, int type)
1256 {
1257         if (!data->totlayer)
1258                 return -1;
1259
1260         if (data->typemap[type] != -1) {
1261                 return data->typemap[type] + data->layers[data->typemap[type]].active;
1262         }
1263
1264         return -1;
1265 }
1266
1267 int CustomData_get_render_layer_index(const CustomData *data, int type)
1268 {
1269         int i;
1270
1271         for(i=0; i < data->totlayer; ++i)
1272                 if(data->layers[i].type == type)
1273                         return i + data->layers[i].active_rnd;
1274
1275         return -1;
1276 }
1277
1278 int CustomData_get_clone_layer_index(const CustomData *data, int type)
1279 {
1280         int i;
1281
1282         for(i=0; i < data->totlayer; ++i)
1283                 if(data->layers[i].type == type)
1284                         return i + data->layers[i].active_clone;
1285
1286         return -1;
1287 }
1288
1289 int CustomData_get_stencil_layer_index(const CustomData *data, int type)
1290 {
1291         int i;
1292
1293         for(i=0; i < data->totlayer; ++i)
1294                 if(data->layers[i].type == type)
1295                         return i + data->layers[i].active_mask;
1296
1297         return -1;
1298 }
1299
1300 int CustomData_get_active_layer(const CustomData *data, int type)
1301 {
1302         int i;
1303
1304         for(i=0; i < data->totlayer; ++i)
1305                 if(data->layers[i].type == type)
1306                         return data->layers[i].active;
1307
1308         return -1;
1309 }
1310
1311 int CustomData_get_render_layer(const CustomData *data, int type)
1312 {
1313         int i;
1314
1315         for(i=0; i < data->totlayer; ++i)
1316                 if(data->layers[i].type == type)
1317                         return data->layers[i].active_rnd;
1318
1319         return -1;
1320 }
1321
1322 int CustomData_get_clone_layer(const CustomData *data, int type)
1323 {
1324         int i;
1325
1326         for(i=0; i < data->totlayer; ++i)
1327                 if(data->layers[i].type == type)
1328                         return data->layers[i].active_clone;
1329
1330         return -1;
1331 }
1332
1333 int CustomData_get_stencil_layer(const CustomData *data, int type)
1334 {
1335         int i;
1336
1337         for(i=0; i < data->totlayer; ++i)
1338                 if(data->layers[i].type == type)
1339                         return data->layers[i].active_mask;
1340
1341         return -1;
1342 }
1343
1344 void CustomData_set_layer_active(CustomData *data, int type, int n)
1345 {
1346         int i;
1347
1348         for(i=0; i < data->totlayer; ++i)
1349                 if(data->layers[i].type == type)
1350                         data->layers[i].active = n;
1351 }
1352
1353 void CustomData_set_layer_render(CustomData *data, int type, int n)
1354 {
1355         int i;
1356
1357         for(i=0; i < data->totlayer; ++i)
1358                 if(data->layers[i].type == type)
1359                         data->layers[i].active_rnd = n;
1360 }
1361
1362 void CustomData_set_layer_clone(CustomData *data, int type, int n)
1363 {
1364         int i;
1365
1366         for(i=0; i < data->totlayer; ++i)
1367                 if(data->layers[i].type == type)
1368                         data->layers[i].active_clone = n;
1369 }
1370
1371 void CustomData_set_layer_stencil(CustomData *data, int type, int n)
1372 {
1373         int i;
1374
1375         for(i=0; i < data->totlayer; ++i)
1376                 if(data->layers[i].type == type)
1377                         data->layers[i].active_mask = n;
1378 }
1379
1380 /* for using with an index from CustomData_get_active_layer_index and CustomData_get_render_layer_index */
1381 void CustomData_set_layer_active_index(CustomData *data, int type, int n)
1382 {
1383         int i;
1384
1385         for(i=0; i < data->totlayer; ++i)
1386                 if(data->layers[i].type == type)
1387                         data->layers[i].active = n-i;
1388 }
1389
1390 void CustomData_set_layer_render_index(CustomData *data, int type, int n)
1391 {
1392         int i;
1393
1394         for(i=0; i < data->totlayer; ++i)
1395                 if(data->layers[i].type == type)
1396                         data->layers[i].active_rnd = n-i;
1397 }
1398
1399 void CustomData_set_layer_clone_index(CustomData *data, int type, int n)
1400 {
1401         int i;
1402
1403         for(i=0; i < data->totlayer; ++i)
1404                 if(data->layers[i].type == type)
1405                         data->layers[i].active_clone = n-i;
1406 }
1407
1408 void CustomData_set_layer_stencil_index(CustomData *data, int type, int n)
1409 {
1410         int i;
1411
1412         for(i=0; i < data->totlayer; ++i)
1413                 if(data->layers[i].type == type)
1414                         data->layers[i].active_mask = n-i;
1415 }
1416
1417 void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
1418 {
1419         int i;
1420
1421         for(i=0; i < data->totlayer; ++i)
1422                 if(data->layers[i].type == type)
1423                         data->layers[i].flag |= flag;
1424 }
1425
1426 static int customData_resize(CustomData *data, int amount)
1427 {
1428         CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
1429                                                                            "CustomData->layers");
1430         if(!tmp) return 0;
1431
1432         data->maxlayer += amount;
1433         if (data->layers) {
1434                 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
1435                 MEM_freeN(data->layers);
1436         }
1437         data->layers = tmp;
1438
1439         return 1;
1440 }
1441
1442 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1443         int type, int alloctype, void *layerdata, int totelem, const char *name)
1444 {
1445         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1446         int size = typeInfo->size * totelem, flag = 0, index = data->totlayer;
1447         void *newlayerdata = NULL;
1448
1449         /* Passing a layerdata to copy from with an alloctype that won't copy is
1450            most likely a bug */
1451         BLI_assert(!layerdata ||
1452                    (alloctype == CD_ASSIGN) ||
1453                    (alloctype == CD_DUPLICATE) ||
1454                    (alloctype == CD_REFERENCE));
1455
1456         if (!typeInfo->defaultname && CustomData_has_layer(data, type))
1457                 return &data->layers[CustomData_get_layer_index(data, type)];
1458
1459         if((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
1460                 newlayerdata = layerdata;
1461         }
1462         else if (size > 0) {
1463                 newlayerdata = MEM_callocN(size, layerType_getName(type));
1464                 if(!newlayerdata)
1465                         return NULL;
1466         }
1467
1468         if (alloctype == CD_DUPLICATE && layerdata) {
1469                 if(typeInfo->copy)
1470                         typeInfo->copy(layerdata, newlayerdata, totelem);
1471                 else
1472                         memcpy(newlayerdata, layerdata, size);
1473         }
1474         else if (alloctype == CD_DEFAULT) {
1475                 if(typeInfo->set_default)
1476                         typeInfo->set_default((char*)newlayerdata, totelem);
1477         }
1478         else if (alloctype == CD_REFERENCE)
1479                 flag |= CD_FLAG_NOFREE;
1480
1481         if(index >= data->maxlayer) {
1482                 if(!customData_resize(data, CUSTOMDATA_GROW)) {
1483                         if(newlayerdata != layerdata)
1484                                 MEM_freeN(newlayerdata);
1485                         return NULL;
1486                 }
1487         }
1488         
1489         data->totlayer++;
1490
1491         /* keep layers ordered by type */
1492         for( ; index > 0 && data->layers[index - 1].type > type; --index)
1493                 data->layers[index] = data->layers[index - 1];
1494
1495         data->layers[index].type = type;
1496         data->layers[index].flag = flag;
1497         data->layers[index].data = newlayerdata;
1498
1499         if(name || (name=typeInfo->defaultname)) {
1500                 BLI_strncpy(data->layers[index].name, name, sizeof(data->layers[index].name));
1501                 CustomData_set_layer_unique_name(data, index);
1502         }
1503         else
1504                 data->layers[index].name[0] = '\0';
1505
1506         if(index > 0 && data->layers[index-1].type == type) {
1507                 data->layers[index].active = data->layers[index-1].active;
1508                 data->layers[index].active_rnd = data->layers[index-1].active_rnd;
1509                 data->layers[index].active_clone = data->layers[index-1].active_clone;
1510                 data->layers[index].active_mask = data->layers[index-1].active_mask;
1511         } else {
1512                 data->layers[index].active = 0;
1513                 data->layers[index].active_rnd = 0;
1514                 data->layers[index].active_clone = 0;
1515                 data->layers[index].active_mask = 0;
1516         }
1517         
1518         customData_update_offsets(data);
1519
1520         return &data->layers[index];
1521 }
1522
1523 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
1524                                                    void *layerdata, int totelem)
1525 {
1526         CustomDataLayer *layer;
1527         const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1528         
1529         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1530                                                                                    totelem, typeInfo->defaultname);
1531         CustomData_update_typemap(data);
1532
1533         if(layer)
1534                 return layer->data;
1535
1536         return NULL;
1537 }
1538
1539 /*same as above but accepts a name*/
1540 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
1541                                                    void *layerdata, int totelem, const char *name)
1542 {
1543         CustomDataLayer *layer;
1544         
1545         layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1546                                                                                    totelem, name);
1547         CustomData_update_typemap(data);
1548
1549         if(layer)
1550                 return layer->data;
1551
1552         return NULL;
1553 }
1554
1555
1556 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
1557 {
1558         int i;
1559         
1560         if (index < 0) return 0;
1561
1562         customData_free_layer__internal(&data->layers[index], totelem);
1563
1564         for (i=index+1; i < data->totlayer; ++i)
1565                 data->layers[i-1] = data->layers[i];
1566
1567         data->totlayer--;
1568
1569         /* if layer was last of type in array, set new active layer */
1570         if ((index >= data->totlayer) || (data->layers[index].type != type)) {
1571                 i = CustomData_get_layer_index(data, type);
1572                 
1573                 if (i >= 0)
1574                         for (; i < data->totlayer && data->layers[i].type == type; i++) {
1575                                 data->layers[i].active--;
1576                                 data->layers[i].active_rnd--;
1577                                 data->layers[i].active_clone--;
1578                                 data->layers[i].active_mask--;
1579                         }
1580         }
1581
1582         if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1583                 customData_resize(data, -CUSTOMDATA_GROW);
1584
1585         customData_update_offsets(data);
1586         CustomData_update_typemap(data);
1587
1588         return 1;
1589 }
1590
1591 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
1592 {
1593         int index = 0;
1594         index = CustomData_get_active_layer_index(data, type);
1595         if (index < 0) return 0;
1596         return CustomData_free_layer(data, type, totelem, index);
1597 }
1598
1599
1600 void CustomData_free_layers(CustomData *data, int type, int totelem)
1601 {
1602         while (CustomData_has_layer(data, type))
1603                 CustomData_free_layer_active(data, type, totelem);
1604 }
1605
1606 int CustomData_has_layer(const CustomData *data, int type)
1607 {
1608         return (CustomData_get_layer_index(data, type) != -1);
1609 }
1610
1611 int CustomData_number_of_layers(const CustomData *data, int type)
1612 {
1613         int i, number = 0;
1614
1615         for(i = 0; i < data->totlayer; i++)
1616                 if(data->layers[i].type == type)
1617                         number++;
1618         
1619         return number;
1620 }
1621
1622 void *CustomData_duplicate_referenced_layer(struct CustomData *data, const int type, const int totelem)
1623 {
1624         CustomDataLayer *layer;
1625         int layer_index;
1626
1627         /* get the layer index of the first layer of type */
1628         layer_index = CustomData_get_active_layer_index(data, type);
1629         if(layer_index < 0) return NULL;
1630
1631         layer = &data->layers[layer_index];
1632
1633         if (layer->flag & CD_FLAG_NOFREE) {
1634                 /* MEM_dupallocN won’t work in case of complex layers, like e.g.
1635                  * CD_MDEFORMVERT, which has pointers to allocated data...
1636                  * So in case a custom copy function is defined, use it!
1637                  */
1638                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1639
1640                 if(typeInfo->copy) {
1641                         char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1642                         typeInfo->copy(layer->data, dest_data, totelem);
1643                         layer->data = dest_data;
1644                 }
1645                 else
1646                         layer->data = MEM_dupallocN(layer->data);
1647
1648                 layer->flag &= ~CD_FLAG_NOFREE;
1649         }
1650
1651         return layer->data;
1652 }
1653
1654 void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
1655                                                                                                   const int type, const char *name, const int totelem)
1656 {
1657         CustomDataLayer *layer;
1658         int layer_index;
1659
1660         /* get the layer index of the desired layer */
1661         layer_index = CustomData_get_named_layer_index(data, type, name);
1662         if(layer_index < 0) return NULL;
1663
1664         layer = &data->layers[layer_index];
1665
1666         if (layer->flag & CD_FLAG_NOFREE) {
1667                 /* MEM_dupallocN won’t work in case of complex layers, like e.g.
1668                  * CD_MDEFORMVERT, which has pointers to allocated data...
1669                  * So in case a custom copy function is defined, use it!
1670                  */
1671                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1672
1673                 if(typeInfo->copy) {
1674                         char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1675                         typeInfo->copy(layer->data, dest_data, totelem);
1676                         layer->data = dest_data;
1677                 }
1678                 else
1679                         layer->data = MEM_dupallocN(layer->data);
1680
1681                 layer->flag &= ~CD_FLAG_NOFREE;
1682         }
1683
1684         return layer->data;
1685 }
1686
1687 int CustomData_is_referenced_layer(struct CustomData *data, int type)
1688 {
1689         CustomDataLayer *layer;
1690         int layer_index;
1691
1692         /* get the layer index of the first layer of type */
1693         layer_index = CustomData_get_active_layer_index(data, type);
1694         if(layer_index < 0) return 0;
1695
1696         layer = &data->layers[layer_index];
1697
1698         return (layer->flag & CD_FLAG_NOFREE) != 0;
1699 }
1700
1701 void CustomData_free_temporary(CustomData *data, int totelem)
1702 {
1703         CustomDataLayer *layer;
1704         int i, j;
1705
1706         for(i = 0, j = 0; i < data->totlayer; ++i) {
1707                 layer = &data->layers[i];
1708
1709                 if (i != j)
1710                         data->layers[j] = data->layers[i];
1711
1712                 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
1713                         customData_free_layer__internal(layer, totelem);
1714                 else
1715                         j++;
1716         }
1717
1718         data->totlayer = j;
1719
1720         if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1721                 customData_resize(data, -CUSTOMDATA_GROW);
1722
1723         customData_update_offsets(data);
1724 }
1725
1726 void CustomData_set_only_copy(const struct CustomData *data,
1727                               CustomDataMask mask)
1728 {
1729         int i;
1730
1731         for(i = 0; i < data->totlayer; ++i)
1732                 if(!(mask & CD_TYPE_AS_MASK(data->layers[i].type)))
1733                         data->layers[i].flag |= CD_FLAG_NOCOPY;
1734 }
1735
1736 void CustomData_copy_elements(int type, void *source, void *dest, int count)
1737 {
1738         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1739
1740         if (typeInfo->copy)
1741                 typeInfo->copy(source, dest, count);
1742         else
1743                 memcpy(dest, source, typeInfo->size*count);
1744 }
1745
1746 void CustomData_copy_data(const CustomData *source, CustomData *dest,
1747                                                   int source_index, int dest_index, int count)
1748 {
1749         const LayerTypeInfo *typeInfo;
1750         int src_i, dest_i;
1751         int src_offset;
1752         int dest_offset;
1753
1754         /* copies a layer at a time */
1755         dest_i = 0;
1756         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1757
1758                 /* find the first dest layer with type >= the source type
1759                  * (this should work because layers are ordered by type)
1760                  */
1761                 while(dest_i < dest->totlayer
1762                           && dest->layers[dest_i].type < source->layers[src_i].type)
1763                         ++dest_i;
1764
1765                 /* if there are no more dest layers, we're done */
1766                 if(dest_i >= dest->totlayer) return;
1767
1768                 /* if we found a matching layer, copy the data */
1769                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1770                         char *src_data = source->layers[src_i].data;
1771                         char *dest_data = dest->layers[dest_i].data;
1772
1773                         typeInfo = layerType_getInfo(source->layers[src_i].type);
1774
1775                         src_offset = source_index * typeInfo->size;
1776                         dest_offset = dest_index * typeInfo->size;
1777                         
1778                         if (!src_data || !dest_data) {
1779                                 printf("%s: warning null data for %s type (%p --> %p), skipping\n",
1780                                        __func__, layerType_getName(source->layers[src_i].type),
1781                                        (void *)src_data, (void *)dest_data);
1782                                 continue;
1783                         }
1784                         
1785                         if(typeInfo->copy)
1786                                 typeInfo->copy(src_data + src_offset,
1787                                                                 dest_data + dest_offset,
1788                                                                 count);
1789                         else
1790                                 memcpy(dest_data + dest_offset,
1791                                            src_data + src_offset,
1792                                            count * typeInfo->size);
1793
1794                         /* if there are multiple source & dest layers of the same type,
1795                          * we don't want to copy all source layers to the same dest, so
1796                          * increment dest_i
1797                          */
1798                         ++dest_i;
1799                 }
1800         }
1801 }
1802
1803 void CustomData_free_elem(CustomData *data, int index, int count)
1804 {
1805         int i;
1806         const LayerTypeInfo *typeInfo;
1807
1808         for(i = 0; i < data->totlayer; ++i) {
1809                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1810                         typeInfo = layerType_getInfo(data->layers[i].type);
1811
1812                         if(typeInfo->free) {
1813                                 int offset = typeInfo->size * index;
1814
1815                                 typeInfo->free((char *)data->layers[i].data + offset,
1816                                                            count, typeInfo->size);
1817                         }
1818                 }
1819         }
1820 }
1821
1822 #define SOURCE_BUF_SIZE 100
1823
1824 void CustomData_interp(const CustomData *source, CustomData *dest,
1825                                            int *src_indices, float *weights, float *sub_weights,
1826                                            int count, int dest_index)
1827 {
1828         int src_i, dest_i;
1829         int dest_offset;
1830         int j;
1831         void *source_buf[SOURCE_BUF_SIZE];
1832         void **sources = source_buf;
1833
1834         /* slow fallback in case we're interpolating a ridiculous number of
1835          * elements
1836          */
1837         if(count > SOURCE_BUF_SIZE)
1838                 sources = MEM_callocN(sizeof(*sources) * count,
1839                                                           "CustomData_interp sources");
1840
1841         /* interpolates a layer at a time */
1842         dest_i = 0;
1843         for(src_i = 0; src_i < source->totlayer; ++src_i) {
1844                 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
1845                 if(!typeInfo->interp) continue;
1846
1847                 /* find the first dest layer with type >= the source type
1848                  * (this should work because layers are ordered by type)
1849                  */
1850                 while(dest_i < dest->totlayer
1851                           && dest->layers[dest_i].type < source->layers[src_i].type)
1852                         ++dest_i;
1853
1854                 /* if there are no more dest layers, we're done */
1855                 if(dest_i >= dest->totlayer) return;
1856
1857                 /* if we found a matching layer, copy the data */
1858                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1859                         void *src_data = source->layers[src_i].data;
1860
1861                         for(j = 0; j < count; ++j)
1862                                 sources[j] = (char *)src_data
1863                                                          + typeInfo->size * src_indices[j];
1864
1865                         dest_offset = dest_index * typeInfo->size;
1866
1867                         typeInfo->interp(sources, weights, sub_weights, count,
1868                                                    (char *)dest->layers[dest_i].data + dest_offset);
1869
1870                         /* if there are multiple source & dest layers of the same type,
1871                          * we don't want to copy all source layers to the same dest, so
1872                          * increment dest_i
1873                          */
1874                         ++dest_i;
1875                 }
1876         }
1877
1878         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
1879 }
1880
1881 void CustomData_swap(struct CustomData *data, int index, const int *corner_indices)
1882 {
1883         const LayerTypeInfo *typeInfo;
1884         int i;
1885
1886         for(i = 0; i < data->totlayer; ++i) {
1887                 typeInfo = layerType_getInfo(data->layers[i].type);
1888
1889                 if(typeInfo->swap) {
1890                         int offset = typeInfo->size * index;
1891
1892                         typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
1893                 }
1894         }
1895 }
1896
1897 void *CustomData_get(const CustomData *data, int index, int type)
1898 {
1899         int offset;
1900         int layer_index;
1901         
1902         /* get the layer index of the active layer of type */
1903         layer_index = CustomData_get_active_layer_index(data, type);
1904         if(layer_index < 0) return NULL;
1905
1906         /* get the offset of the desired element */
1907         offset = layerType_getInfo(type)->size * index;
1908
1909         return (char *)data->layers[layer_index].data + offset;
1910 }
1911
1912 void *CustomData_get_n(const CustomData *data, int type, int index, int n)
1913 {
1914         int layer_index;
1915         int offset;
1916
1917         /* get the layer index of the first layer of type */
1918         layer_index = data->typemap[type];
1919         if(layer_index < 0) return NULL;
1920
1921         offset = layerType_getInfo(type)->size * index;
1922         return (char *)data->layers[layer_index+n].data + offset;
1923 }
1924
1925 void *CustomData_get_layer(const CustomData *data, int type)
1926 {
1927         /* get the layer index of the active layer of type */
1928         int layer_index = CustomData_get_active_layer_index(data, type);
1929         if(layer_index < 0) return NULL;
1930
1931         return data->layers[layer_index].data;
1932 }
1933
1934 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
1935 {
1936         /* get the layer index of the active layer of type */
1937         int layer_index = CustomData_get_layer_index_n(data, type, n);
1938         if(layer_index < 0) return NULL;
1939
1940         return data->layers[layer_index].data;
1941 }
1942
1943 void *CustomData_get_layer_named(const struct CustomData *data, int type,
1944                                                                  const char *name)
1945 {
1946         int layer_index = CustomData_get_named_layer_index(data, type, name);
1947         if(layer_index < 0) return NULL;
1948
1949         return data->layers[layer_index].data;
1950 }
1951
1952
1953 int CustomData_set_layer_name(const CustomData *data, int type, int n, const char *name)
1954 {
1955         /* get the layer index of the first layer of type */
1956         int layer_index = CustomData_get_layer_index_n(data, type, n);
1957
1958         if(layer_index < 0) return 0;
1959         if (!name) return 0;
1960         
1961         strcpy(data->layers[layer_index].name, name);
1962         
1963         return 1;
1964 }
1965
1966 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
1967 {
1968         /* get the layer index of the first layer of type */
1969         int layer_index = CustomData_get_active_layer_index(data, type);
1970
1971         if(layer_index < 0) return NULL;
1972
1973         data->layers[layer_index].data = ptr;
1974
1975         return ptr;
1976 }
1977
1978 void *CustomData_set_layer_n(const struct CustomData *data, int type, int n, void *ptr)
1979 {
1980         /* get the layer index of the first layer of type */
1981         int layer_index = CustomData_get_layer_index_n(data, type, n);
1982         if(layer_index < 0) return NULL;
1983
1984         data->layers[layer_index].data = ptr;
1985
1986         return ptr;
1987 }
1988
1989 void CustomData_set(const CustomData *data, int index, int type, void *source)
1990 {
1991         void *dest = CustomData_get(data, index, type);
1992         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1993
1994         if(!dest) return;
1995
1996         if(typeInfo->copy)
1997                 typeInfo->copy(source, dest, 1);
1998         else
1999                 memcpy(dest, source, typeInfo->size);
2000 }
2001
2002 /*Bmesh functions*/
2003 /*needed to convert to/from different face reps*/
2004 void CustomData_to_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata,
2005                              int totloop, int totpoly)
2006 {
2007         int i;
2008         for(i=0; i < fdata->totlayer; i++) {
2009                 if(fdata->layers[i].type == CD_MTFACE) {
2010                         CustomData_add_layer_named(pdata, CD_MTEXPOLY, CD_CALLOC, NULL, totpoly, fdata->layers[i].name);
2011                         CustomData_add_layer_named(ldata, CD_MLOOPUV, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2012                 }
2013                 else if (fdata->layers[i].type == CD_MCOL) {
2014                         CustomData_add_layer_named(ldata, CD_MLOOPCOL, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2015                 }
2016                 else if (fdata->layers[i].type == CD_MDISPS) {
2017                         CustomData_add_layer_named(ldata, CD_MDISPS, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2018                 }
2019         }
2020 }
2021
2022 void CustomData_from_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata, int total)
2023 {
2024         int i;
2025         for(i=0; i < pdata->totlayer; i++) {
2026                 if (pdata->layers[i].type == CD_MTEXPOLY) {
2027                         CustomData_add_layer_named(fdata, CD_MTFACE, CD_CALLOC, NULL, total, pdata->layers[i].name);
2028                 }
2029         }
2030         for(i=0; i < ldata->totlayer; i++) {
2031                 if (ldata->layers[i].type == CD_MLOOPCOL) {
2032                         CustomData_add_layer_named(fdata, CD_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2033                 }
2034                 else if (ldata->layers[i].type == CD_WEIGHT_MLOOPCOL) {
2035                         CustomData_add_layer_named(fdata, CD_WEIGHT_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2036                 }
2037                 else if (ldata->layers[i].type == CD_ORIGSPACE_MLOOP) {
2038                         CustomData_add_layer_named(fdata, CD_ORIGSPACE, CD_CALLOC, NULL, total, ldata->layers[i].name);
2039                 }
2040         }
2041 }
2042
2043 void CustomData_bmesh_update_active_layers(CustomData *fdata, CustomData *pdata, CustomData *ldata)
2044 {
2045         int act;
2046
2047         if (CustomData_has_layer(pdata, CD_MTEXPOLY)) {
2048                 act = CustomData_get_active_layer(pdata, CD_MTEXPOLY);
2049                 CustomData_set_layer_active(ldata, CD_MLOOPUV, act);
2050                 CustomData_set_layer_active(fdata, CD_MTFACE, act);
2051
2052                 act = CustomData_get_render_layer(pdata, CD_MTEXPOLY);
2053                 CustomData_set_layer_render(ldata, CD_MLOOPUV, act);
2054                 CustomData_set_layer_render(fdata, CD_MTFACE, act);
2055
2056                 act = CustomData_get_clone_layer(pdata, CD_MTEXPOLY);
2057                 CustomData_set_layer_clone(ldata, CD_MLOOPUV, act);
2058                 CustomData_set_layer_clone(fdata, CD_MTFACE, act);
2059
2060                 act = CustomData_get_stencil_layer(pdata, CD_MTEXPOLY);
2061                 CustomData_set_layer_stencil(ldata, CD_MLOOPUV, act);
2062                 CustomData_set_layer_stencil(fdata, CD_MTFACE, act);
2063         }
2064
2065         if (CustomData_has_layer(ldata, CD_MLOOPCOL)) {
2066                 act = CustomData_get_active_layer(ldata, CD_MLOOPCOL);
2067                 CustomData_set_layer_active(fdata, CD_MCOL, act);
2068
2069                 act = CustomData_get_render_layer(ldata, CD_MLOOPCOL);
2070                 CustomData_set_layer_render(fdata, CD_MCOL, act);
2071
2072                 act = CustomData_get_clone_layer(ldata, CD_MLOOPCOL);
2073                 CustomData_set_layer_clone(fdata, CD_MCOL, act);
2074
2075                 act = CustomData_get_stencil_layer(ldata, CD_MLOOPCOL);
2076                 CustomData_set_layer_stencil(fdata, CD_MCOL, act);
2077         }
2078 }
2079
2080 void CustomData_bmesh_init_pool(CustomData *data, int allocsize)
2081 {
2082         /* Dispose old pools before calling here to avoid leaks */
2083         BLI_assert(data->pool == NULL);
2084
2085         /* If there are no layers, no pool is needed just yet */
2086         if (data->totlayer) {
2087                 data->pool = BLI_mempool_create(data->totsize, allocsize, allocsize, TRUE, FALSE);
2088         }
2089 }
2090
2091 void CustomData_bmesh_merge(CustomData *source, CustomData *dest, 
2092                             int mask, int alloctype, BMesh *bm, int type)
2093 {
2094         BMHeader *h;
2095         BMIter iter;
2096         CustomData destold = *dest;
2097         void *tmp;
2098         int t;
2099         
2100         CustomData_merge(source, dest, mask, alloctype, 0);
2101         CustomData_bmesh_init_pool(dest, 512);
2102
2103         switch (type) {
2104                 case BM_VERT:
2105                         t = BM_VERTS_OF_MESH; break;
2106                 case BM_EDGE:
2107                         t = BM_EDGES_OF_MESH; break;
2108                 case BM_LOOP:
2109                         t = BM_LOOPS_OF_FACE; break;
2110                 case BM_FACE:
2111                         t = BM_FACES_OF_MESH; break;
2112                 default: /* should never happen */
2113                         BLI_assert(!"invalid type given");
2114                         t = BM_VERTS_OF_MESH;
2115         }
2116
2117         if (t != BM_LOOPS_OF_FACE) {
2118                 /*ensure all current elements follow new customdata layout*/
2119                 BM_ITER(h, &iter, bm, t, NULL) {
2120                         CustomData_bmesh_copy_data(&destold, dest, h->data, &tmp);
2121                         CustomData_bmesh_free_block(&destold, &h->data);
2122                         h->data = tmp;
2123                 }
2124         } else {
2125                 BMFace *f;
2126                 BMLoop *l;
2127                 BMIter liter;
2128
2129                 /*ensure all current elements follow new customdata layout*/
2130                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
2131                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
2132                                 CustomData_bmesh_copy_data(&destold, dest, l->head.data, &tmp);
2133                                 CustomData_bmesh_free_block(&destold, &l->head.data);
2134                                 l->head.data = tmp;
2135                         }
2136                 }
2137         }
2138
2139         if (destold.pool) BLI_mempool_destroy(destold.pool);
2140 }
2141
2142 void CustomData_bmesh_free_block(CustomData *data, void **block)
2143 {
2144         const LayerTypeInfo *typeInfo;
2145         int i;
2146
2147         if(!*block) return;
2148         for(i = 0; i < data->totlayer; ++i) {
2149                 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
2150                         typeInfo = layerType_getInfo(data->layers[i].type);
2151
2152                         if(typeInfo->free) {
2153                                 int offset = data->layers[i].offset;
2154                                 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
2155                         }
2156                 }
2157         }
2158
2159         if (data->totsize)
2160                 BLI_mempool_free(data->pool, *block);
2161
2162         *block = NULL;
2163 }
2164
2165 static void CustomData_bmesh_alloc_block(CustomData *data, void **block)
2166 {
2167
2168         if (*block)
2169                 CustomData_bmesh_free_block(data, block);
2170
2171         if (data->totsize > 0)
2172                 *block = BLI_mempool_alloc(data->pool);
2173         else
2174                 *block = NULL;
2175 }
2176
2177 void CustomData_bmesh_copy_data(const CustomData *source, CustomData *dest,
2178                                                         void *src_block, void **dest_block)
2179 {
2180         const LayerTypeInfo *typeInfo;
2181         int dest_i, src_i;
2182
2183         if (!*dest_block)
2184                 CustomData_bmesh_alloc_block(dest, dest_block);
2185         
2186         /* copies a layer at a time */
2187         dest_i = 0;
2188         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2189
2190                 /* find the first dest layer with type >= the source type
2191                  * (this should work because layers are ordered by type)
2192                  */
2193                 while(dest_i < dest->totlayer
2194                           && dest->layers[dest_i].type < source->layers[src_i].type)
2195                         ++dest_i;
2196
2197                 /* if there are no more dest layers, we're done */
2198                 if(dest_i >= dest->totlayer) return;
2199
2200                 /* if we found a matching layer, copy the data */
2201                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
2202                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
2203                         char *src_data = (char*)src_block + source->layers[src_i].offset;
2204                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
2205
2206                         typeInfo = layerType_getInfo(source->layers[src_i].type);
2207
2208                         if(typeInfo->copy)
2209                                 typeInfo->copy(src_data, dest_data, 1);
2210                         else
2211                                 memcpy(dest_data, src_data, typeInfo->size);
2212
2213                         /* if there are multiple source & dest layers of the same type,
2214                          * we don't want to copy all source layers to the same dest, so
2215                          * increment dest_i
2216                          */
2217                         ++dest_i;
2218                 }
2219         }
2220 }
2221
2222 /*Bmesh Custom Data Functions. Should replace editmesh ones with these as well, due to more effecient memory alloc*/
2223 void *CustomData_bmesh_get(const CustomData *data, void *block, int type)
2224 {
2225         int layer_index;
2226         
2227         /* get the layer index of the first layer of type */
2228         layer_index = CustomData_get_active_layer_index(data, type);
2229         if(layer_index < 0) return NULL;
2230
2231         return (char *)block + data->layers[layer_index].offset;
2232 }
2233
2234 void *CustomData_bmesh_get_n(const CustomData *data, void *block, int type, int n)
2235 {
2236         int layer_index;
2237         
2238         /* get the layer index of the first layer of type */
2239         layer_index = CustomData_get_layer_index(data, type);
2240         if(layer_index < 0) return NULL;
2241
2242         return (char *)block + data->layers[layer_index+n].offset;
2243 }
2244
2245 /*gets from the layer at physical index n, note: doesn't check type.*/
2246 void *CustomData_bmesh_get_layer_n(const CustomData *data, void *block, int n)
2247 {
2248         if(n < 0 || n >= data->totlayer) return NULL;
2249
2250         return (char *)block + data->layers[n].offset;
2251 }
2252
2253 int CustomData_layer_has_math(struct CustomData *data, int layern)
2254 {
2255         const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[layern].type);
2256         
2257         if (typeInfo->equal && typeInfo->add && typeInfo->multiply && 
2258             typeInfo->initminmax && typeInfo->dominmax) return 1;
2259         
2260         return 0;
2261 }
2262
2263 /*copies the "value" (e.g. mloopuv uv or mloopcol colors) from one block to
2264   another, while not overwriting anything else (e.g. flags)*/
2265 void CustomData_data_copy_value(int type, void *source, void *dest)
2266 {
2267         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2268
2269         if(!dest) return;
2270
2271         if(typeInfo->copyvalue)
2272                 typeInfo->copyvalue(source, dest);
2273         else
2274                 memcpy(dest, source, typeInfo->size);
2275 }
2276
2277 int CustomData_data_equals(int type, void *data1, void *data2)
2278 {
2279         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2280
2281         if (typeInfo->equal)
2282                 return typeInfo->equal(data1, data2);
2283         else return !memcmp(data1, data2, typeInfo->size);
2284 }
2285
2286 void CustomData_data_initminmax(int type, void *min, void *max)
2287 {
2288         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2289
2290         if (typeInfo->initminmax)
2291                 typeInfo->initminmax(min, max);
2292 }
2293
2294
2295 void CustomData_data_dominmax(int type, void *data, void *min, void *max)
2296 {
2297         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2298
2299         if (typeInfo->dominmax)
2300                 typeInfo->dominmax(data, min, max);
2301 }
2302
2303
2304 void CustomData_data_multiply(int type, void *data, float fac)
2305 {
2306         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2307
2308         if (typeInfo->multiply)
2309                 typeInfo->multiply(data, fac);
2310 }
2311
2312
2313 void CustomData_data_add(int type, void *data1, void *data2)
2314 {
2315         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2316
2317         if (typeInfo->add)
2318                 typeInfo->add(data1, data2);
2319 }
2320
2321 void CustomData_bmesh_set(const CustomData *data, void *block, int type, void *source)
2322 {
2323         void *dest = CustomData_bmesh_get(data, block, type);
2324         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2325
2326         if(!dest) return;
2327
2328         if(typeInfo->copy)
2329                 typeInfo->copy(source, dest, 1);
2330         else
2331                 memcpy(dest, source, typeInfo->size);
2332 }
2333
2334 void CustomData_bmesh_set_n(CustomData *data, void *block, int type, int n, void *source)
2335 {
2336         void *dest = CustomData_bmesh_get_n(data, block, type, n);
2337         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2338
2339         if(!dest) return;
2340
2341         if(typeInfo->copy)
2342                 typeInfo->copy(source, dest, 1);
2343         else
2344                 memcpy(dest, source, typeInfo->size);
2345 }
2346
2347 void CustomData_bmesh_set_layer_n(CustomData *data, void *block, int n, void *source)
2348 {
2349         void *dest = CustomData_bmesh_get_layer_n(data, block, n);
2350         const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[n].type);
2351
2352         if(!dest) return;
2353
2354         if(typeInfo->copy)
2355                 typeInfo->copy(source, dest, 1);
2356         else
2357                 memcpy(dest, source, typeInfo->size);
2358 }
2359
2360 void CustomData_bmesh_interp(CustomData *data, void **src_blocks, float *weights,
2361                                                   float *sub_weights, int count, void *dest_block)
2362 {
2363         int i, j;
2364         void *source_buf[SOURCE_BUF_SIZE];
2365         void **sources = source_buf;
2366
2367         /* slow fallback in case we're interpolating a ridiculous number of
2368          * elements
2369          */
2370         if(count > SOURCE_BUF_SIZE)
2371                 sources = MEM_callocN(sizeof(*sources) * count,
2372                                                           "CustomData_interp sources");
2373
2374         /* interpolates a layer at a time */
2375         for(i = 0; i < data->totlayer; ++i) {
2376                 CustomDataLayer *layer = &data->layers[i];
2377                 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
2378                 if(typeInfo->interp) {
2379                         for(j = 0; j < count; ++j)
2380                                 sources[j] = (char *)src_blocks[j] + layer->offset;
2381
2382                         typeInfo->interp(sources, weights, sub_weights, count,
2383                                                           (char *)dest_block + layer->offset);
2384                 }
2385         }
2386
2387         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2388 }
2389
2390 void CustomData_bmesh_set_default(CustomData *data, void **block)
2391 {
2392         const LayerTypeInfo *typeInfo;
2393         int i;
2394
2395         if (!*block)
2396                 CustomData_bmesh_alloc_block(data, block);
2397
2398         for(i = 0; i < data->totlayer; ++i) {
2399                 int offset = data->layers[i].offset;
2400
2401                 typeInfo = layerType_getInfo(data->layers[i].type);
2402
2403                 if(typeInfo->set_default)
2404                         typeInfo->set_default((char*)*block + offset, 1);
2405                 else memset((char*)*block + offset, 0, typeInfo->size);
2406         }
2407 }
2408
2409 void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
2410                                                         int src_index, void **dest_block)
2411 {
2412         const LayerTypeInfo *typeInfo;
2413         int dest_i, src_i, src_offset;
2414
2415         if (!*dest_block)
2416                 CustomData_bmesh_alloc_block(dest, dest_block);
2417         
2418         /* copies a layer at a time */
2419         dest_i = 0;
2420         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2421
2422                 /* find the first dest layer with type >= the source type
2423                  * (this should work because layers are ordered by type)
2424                  */
2425                 while(dest_i < dest->totlayer
2426                           && dest->layers[dest_i].type < source->layers[src_i].type)
2427                         ++dest_i;
2428
2429                 /* if there are no more dest layers, we're done */
2430                 if(dest_i >= dest->totlayer) return;
2431
2432                 /* if we found a matching layer, copy the data */
2433                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2434                         int offset = dest->layers[dest_i].offset;
2435                         char *src_data = source->layers[src_i].data;
2436                         char *dest_data = (char*)*dest_block + offset;
2437
2438                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2439                         src_offset = src_index * typeInfo->size;
2440
2441                         if(typeInfo->copy)
2442                                 typeInfo->copy(src_data + src_offset, dest_data, 1);
2443                         else
2444                                 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2445
2446                         /* if there are multiple source & dest layers of the same type,
2447                          * we don't want to copy all source layers to the same dest, so
2448                          * increment dest_i
2449                          */
2450                         ++dest_i;
2451                 }
2452         }
2453 }
2454
2455 void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest,
2456                                                           void *src_block, int dest_index)
2457 {
2458         const LayerTypeInfo *typeInfo;
2459         int dest_i, src_i, dest_offset;
2460
2461         /* copies a layer at a time */
2462         dest_i = 0;
2463         for(src_i = 0; src_i < source->totlayer; ++src_i) {
2464
2465                 /* find the first dest layer with type >= the source type
2466                  * (this should work because layers are ordered by type)
2467                  */
2468                 while(dest_i < dest->totlayer
2469                           && dest->layers[dest_i].type < source->layers[src_i].type)
2470                         ++dest_i;
2471
2472                 /* if there are no more dest layers, we're done */
2473                 if(dest_i >= dest->totlayer) return;
2474
2475                 /* if we found a matching layer, copy the data */
2476                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2477                         int offset = source->layers[src_i].offset;
2478                         char *src_data = (char*)src_block + offset;
2479                         char *dest_data = dest->layers[dest_i].data;
2480
2481                         typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2482                         dest_offset = dest_index * typeInfo->size;
2483
2484                         if(typeInfo->copy)
2485                                 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2486                         else
2487                                 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2488
2489                         /* if there are multiple source & dest layers of the same type,
2490                          * we don't want to copy all source layers to the same dest, so
2491                          * increment dest_i
2492                          */
2493                         ++dest_i;
2494                 }
2495         }
2496
2497 }
2498
2499 void CustomData_file_write_info(int type, const char **structname, int *structnum)
2500 {
2501         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2502
2503         *structname = typeInfo->structname;
2504         *structnum = typeInfo->structnum;
2505 }
2506
2507 int CustomData_sizeof(int type)
2508 {
2509         const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2510
2511         return typeInfo->size;
2512 }
2513
2514 const char *CustomData_layertype_name(int type)
2515 {
2516         return layerType_getName(type);
2517 }
2518
2519 static int  CustomData_is_property_layer(int type)
2520 {
2521         if((type == CD_PROP_FLT) || (type == CD_PROP_INT) || (type == CD_PROP_STR))
2522                 return 1;
2523         return 0;
2524 }
2525
2526 static int cd_layer_find_dupe(CustomData *data, const char *name, int type, int index)
2527 {
2528         int i;
2529         /* see if there is a duplicate */
2530         for(i=0; i<data->totlayer; i++) {
2531                 if(i != index) {
2532                         CustomDataLayer *layer= &data->layers[i];
2533                         
2534                         if(CustomData_is_property_layer(type)) {
2535                                 if(CustomData_is_property_layer(layer->type) && strcmp(layer->name, name)==0) {
2536                                         return 1;
2537                                 }
2538                         }
2539                         else{
2540                                 if(i!=index && layer->type==type && strcmp(layer->name, name)==0) {
2541                                         return 1;
2542                                 }
2543                         }
2544                 }
2545         }
2546         
2547         return 0;
2548 }
2549
2550 static int customdata_unique_check(void *arg, const char *name)
2551 {
2552         struct {CustomData *data; int type; int index;} *data_arg= arg;
2553         return cd_layer_find_dupe(data_arg->data, name, data_arg->type, data_arg->index);
2554 }
2555
2556 void CustomData_set_layer_unique_name(CustomData *data, int index)
2557 {       
2558         CustomDataLayer *nlayer= &data->layers[index];
2559         const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
2560
2561         struct {CustomData *data; int type; int index;} data_arg;
2562         data_arg.data= data;
2563         data_arg.type= nlayer->type;
2564         data_arg.index= index;
2565
2566         if (!typeInfo->defaultname)
2567                 return;
2568         
2569         BLI_uniquename_cb(customdata_unique_check, &data_arg, typeInfo->defaultname, '.', nlayer->name, sizeof(nlayer->name));
2570 }
2571
2572 void CustomData_validate_layer_name(const CustomData *data, int type, char *name, char *outname)
2573 {
2574         int index = -1;
2575
2576         /* if a layer name was given, try to find that layer */
2577         if(name[0])
2578                 index = CustomData_get_named_layer_index(data, type, name);
2579
2580         if(index < 0) {
2581                 /* either no layer was specified, or the layer we want has been
2582                 * deleted, so assign the active layer to name
2583                 */
2584                 index = CustomData_get_active_layer_index(data, type);
2585                 strcpy(outname, data->layers[index].name);
2586         }
2587         else
2588                 strcpy(outname, name);
2589 }
2590
2591 int CustomData_verify_versions(struct CustomData *data, int index)
2592 {
2593         const LayerTypeInfo *typeInfo;
2594         CustomDataLayer *layer = &data->layers[index];
2595         int i, keeplayer = 1;
2596
2597         if (layer->type >= CD_NUMTYPES) {
2598                 keeplayer = 0; /* unknown layer type from future version */
2599         }
2600         else {
2601                 typeInfo = layerType_getInfo(layer->type);
2602
2603                 if (!typeInfo->defaultname && (index > 0) &&
2604                         data->layers[index-1].type == layer->type)
2605                         keeplayer = 0; /* multiple layers of which we only support one */
2606         }
2607
2608         if (!keeplayer) {
2609                 for (i=index+1; i < data->totlayer; ++i)
2610                         data->layers[i-1] = data->layers[i];
2611                 data->totlayer--;
2612         }
2613
2614         return keeplayer;
2615 }
2616
2617 /****************************** External Files *******************************/
2618
2619 static void customdata_external_filename(char filename[FILE_MAX], ID *id, CustomDataExternal *external)
2620 {
2621         BLI_strncpy(filename, external->filename, FILE_MAX);
2622         BLI_path_abs(filename, ID_BLEND_PATH(G.main, id));
2623 }
2624
2625 void CustomData_external_reload(CustomData *data, ID *UNUSED(id), CustomDataMask mask, int totelem)
2626 {
2627         CustomDataLayer *layer;
2628         const LayerTypeInfo *typeInfo;
2629         int i;
2630
2631         for(i=0; i<data->totlayer; i++) {
2632                 layer = &data->layers[i];
2633                 typeInfo = layerType_getInfo(layer->type);
2634
2635                 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
2636                 else if((layer->flag & CD_FLAG_EXTERNAL) && (layer->flag & CD_FLAG_IN_MEMORY)) {
2637                         if(typeInfo->free)
2638                                 typeInfo->free(layer->data, totelem, typeInfo->size);
2639                         layer->flag &= ~CD_FLAG_IN_MEMORY;
2640                 }
2641         }
2642 }
2643
2644 void CustomData_external_read(CustomData *data, ID *id, CustomDataMask mask, int totelem)
2645 {
2646         CustomDataExternal *external= data->external;
2647         CustomDataLayer *layer;
2648         CDataFile *cdf;
2649         CDataFileLayer *blay;
2650         char filename[FILE_MAX];
2651         const LayerTypeInfo *typeInfo;
2652         int i, update = 0;
2653
2654         if(!external)
2655                 return;
2656         
2657         for(i=0; i<data->totlayer; i++) {
2658                 layer = &data->layers[i];
2659                 typeInfo = layerType_getInfo(layer->type);
2660
2661                 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
2662                 else if(layer->flag & CD_FLAG_IN_MEMORY);
2663                 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read)
2664                         update= 1;
2665         }
2666
2667         if(!update)
2668                 return;
2669
2670         customdata_external_filename(filename, id, external);
2671
2672         cdf= cdf_create(CDF_TYPE_MESH);
2673         if(!cdf_read_open(cdf, filename)) {
2674                 fprintf(stderr, "Failed to read %s layer from %s.\n", layerType_getName(layer->type), filename);
2675                 return;
2676         }
2677
2678         for(i=0; i<data->totlayer; i++) {
2679                 layer = &data->layers[i];
2680                 typeInfo = layerType_getInfo(layer->type);
2681
2682                 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
2683                 else if(layer->flag & CD_FLAG_IN_MEMORY);
2684                 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read) {
2685                         blay= cdf_layer_find(cdf, layer->type, layer->name);
2686
2687                         if(blay) {
2688                                 if(cdf_read_layer(cdf, blay)) {
2689                                         if(typeInfo->read(cdf, layer->data, totelem));
2690                                         else break;
2691                                         layer->flag |= CD_FLAG_IN_MEMORY;
2692                                 }
2693                                 else
2694                                         break;
2695                         }
2696                 }
2697         }
2698
2699         cdf_read_close(cdf);
2700         cdf_free(cdf);
2701 }
2702
2703 void CustomData_external_write(CustomData *data, ID *id, CustomDataMask mask, int totelem, int free)
2704 {
2705         CustomDataExternal *external= data->external;
2706         CustomDataLayer *layer;
2707         CDataFile *cdf;
2708         CDataFileLayer *blay;
2709         const LayerTypeInfo *typeInfo;
2710         int i, update = 0;
2711         char filename[FILE_MAX];
2712
2713         if(!external)
2714                 return;
2715
2716         /* test if there is anything to write */
2717         for(i=0; i<data->totlayer; i++) {
2718                 layer = &data->layers[i];
2719                 typeInfo = layerType_getInfo(layer->type);
2720
2721                 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
2722                 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write)
2723                         update= 1;
2724         }
2725
2726         if(!update)
2727                 return;
2728
2729         /* make sure data is read before we try to write */
2730         CustomData_external_read(data, id, mask, totelem);
2731         customdata_external_filename(filename, id, external);
2732
2733         cdf= cdf_create(CDF_TYPE_MESH);
2734
2735         for(i=0; i<data->totlayer; i++) {
2736                 layer = &data->layers[i];
2737                 typeInfo = layerType_getInfo(layer->type);
2738
2739                 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->filesize) {
2740                         if(layer->flag & CD_FLAG_IN_MEMORY) {
2741                                 cdf_layer_add(cdf, layer->type, layer->name,
2742                                         typeInfo->filesize(cdf, layer->data, totelem));
2743                         }
2744                         else {
2745                                 cdf_free(cdf);
2746                                 return; /* read failed for a layer! */
2747                         }
2748                 }
2749         }
2750
2751         if(!cdf_write_open(cdf, filename)) {
2752                 fprintf(stderr, "Failed to open %s for writing.\n", filename);
2753                 return;
2754         }
2755
2756         for(i=0; i<data->totlayer; i++) {
2757                 layer = &data->layers[i];
2758                 typeInfo = layerType_getInfo(layer->type);
2759
2760                 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
2761                         blay= cdf_layer_find(cdf, layer->type, layer->name);
2762
2763                         if(cdf_write_layer(cdf, blay)) {
2764                                 if(typeInfo->write(cdf, layer->data, totelem));
2765                                 else break;
2766                         }
2767                         else
2768                                 break;
2769                 }
2770         }
2771
2772         if(i != data->totlayer) {
2773                 fprintf(stderr, "Failed to write data to %s.\n", filename);
2774                 cdf_free(cdf);
2775                 return;
2776         }
2777
2778         for(i=0; i<data->totlayer; i++) {
2779                 layer = &data->layers[i];
2780                 typeInfo = layerType_getInfo(layer->type);
2781
2782                 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
2783                         if(free) {
2784                                 if(typeInfo->free)
2785                                         typeInfo->free(layer->data, totelem, typeInfo->size);
2786                                 layer->flag &= ~CD_FLAG_IN_MEMORY;
2787                         }
2788                 }
2789         }
2790
2791         cdf_write_close(cdf);
2792         cdf_free(cdf);
2793 }
2794
2795 void CustomData_external_add(CustomData *data, ID *UNUSED(id), int type, int UNUSED(totelem), const char *filename)
2796 {
2797         CustomDataExternal *external= data->external;
2798         CustomDataLayer *layer;
2799         int layer_index;
2800
2801         layer_index = CustomData_get_active_layer_index(data, type);
2802         if(layer_index < 0) return;
2803
2804         layer = &data->layers[layer_index];
2805
2806         if(layer->flag & CD_FLAG_EXTERNAL)
2807                 return;
2808
2809         if(!external) {
2810                 external= MEM_callocN(sizeof(CustomDataExternal), "CustomDataExternal");
2811                 data->external= external;
2812         }
2813         BLI_strncpy(external->filename, filename, sizeof(external->filename));
2814
2815         layer->flag |= CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY;
2816 }
2817
2818 void CustomData_external_remove(CustomData *data, ID *id, int type, int totelem)
2819 {
2820         CustomDataExternal *external= data->external;
2821         CustomDataLayer *layer;
2822         //char filename[FILE_MAX];
2823         int layer_index; // i, remove_file;
2824
2825         layer_index = CustomData_get_active_layer_index(data, type);
2826         if(layer_index < 0) return;
2827
2828         layer = &data->layers[layer_index];
2829
2830         if(!external)
2831                 return;
2832
2833         if(layer->flag & CD_FLAG_EXTERNAL) {
2834                 if(!(layer->flag & CD_FLAG_IN_MEMORY))
2835                         CustomData_external_read(data, id, CD_TYPE_AS_MASK(layer->type), totelem);
2836
2837                 layer->flag &= ~CD_FLAG_EXTERNAL;
2838
2839 #if 0
2840                 remove_file= 1;
2841                 for(i=0; i<data->totlayer; i++)
2842                         if(data->layers[i].flag & CD_FLAG_EXTERNAL)
2843                                 remove_file= 0;
2844
2845                 if(remove_file) {
2846                         customdata_external_filename(filename, id, external);
2847                         cdf_remove(filename);
2848                         CustomData_external_free(data);
2849                 }
2850 #endif
2851         }
2852 }
2853
2854 int CustomData_external_test(CustomData *data, int type)
2855 {
2856         CustomDataLayer *layer;
2857         int layer_index;
2858
2859         layer_index = CustomData_get_active_layer_index(data, type);
2860         if(layer_index < 0) return 0;
2861
2862         layer = &data->layers[layer_index];
2863         return (layer->flag & CD_FLAG_EXTERNAL);
2864 }
2865
2866 #if 0
2867 void CustomData_external_remove_object(CustomData *data, ID *id)
2868 {
2869         CustomDataExternal *external= data->external;
2870         char filename[FILE_MAX];
2871
2872         if(!external)
2873                 return;
2874
2875         customdata_external_filename(filename, id, external);
2876         cdf_remove(filename);
2877         CustomData_external_free(data);
2878 }
2879 #endif
2880