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