2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2006 Blender Foundation.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Ben Batt <benbatt@gmail.com>
25 * ***** END GPL LICENSE BLOCK *****
27 * Implementation of CustomData.
29 * BKE_customdata.h contains the function prototypes for this file.
33 /** \file blender/blenkernel/intern/customdata.c
42 #include "MEM_guardedalloc.h"
44 #include "DNA_meshdata_types.h"
47 #include "BLI_utildefines.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_linklist.h"
51 #include "BLI_mempool.h"
52 #include "BLI_utildefines.h"
54 #include "BKE_customdata.h"
55 #include "BKE_customdata_file.h"
56 #include "BKE_global.h"
58 #include "BKE_multires.h"
65 /* number of layers to add when growing a CustomData object */
66 #define CUSTOMDATA_GROW 5
68 /********************* Layer type information **********************/
69 typedef struct LayerTypeInfo {
70 int size; /* the memory size of one element of this layer's data */
71 const char *structname; /* name of the struct used, for file writing */
72 int structnum; /* number of structs per element, for file writing */
73 const char *defaultname; /* default layer name */
75 /* a function to copy count elements of this layer's data
76 * (deep copy if appropriate)
77 * if NULL, memcpy is used
79 void (*copy)(const void *source, void *dest, int count);
81 /* a function to free any dynamically allocated components of this
82 * layer's data (note the data pointer itself should not be freed)
83 * size should be the size of one element of this layer's data (e.g.
86 void (*free)(void *data, int count, int size);
88 /* a function to interpolate between count source elements of this
89 * layer's data and store the result in dest
90 * if weights == NULL or sub_weights == NULL, they should default to 1
92 * weights gives the weight for each element in sources
93 * sub_weights gives the sub-element weights for each element in sources
94 * (there should be (sub element count)^2 weights per element)
95 * count gives the number of elements in sources
97 void (*interp)(void **sources, float *weights, float *sub_weights,
98 int count, void *dest);
100 /* a function to swap the data in corners of the element */
101 void (*swap)(void *data, const int *corner_indices);
103 /* a function to set a layer's data to default values. if NULL, the
104 default is assumed to be all zeros */
105 void (*set_default)(void *data, int count);
107 /* functions necassary for geometry collapse*/
108 int (*equal)(void *data1, void *data2);
109 void (*multiply)(void *data, float fac);
110 void (*initminmax)(void *min, void *max);
111 void (*add)(void *data1, void *data2);
112 void (*dominmax)(void *data1, void *min, void *max);
113 void (*copyvalue)(void *source, void *dest);
115 /* a function to read data from a cdf file */
116 int (*read)(CDataFile *cdf, void *data, int count);
118 /* a function to write data to a cdf file */
119 int (*write)(CDataFile *cdf, void *data, int count);
121 /* a function to determine file size */
122 size_t (*filesize)(CDataFile *cdf, void *data, int count);
124 /* a function to validate layer contents depending on
127 void (*validate)(void *source, int sub_elements);
130 static void layerCopy_mdeformvert(const void *source, void *dest,
133 int i, size = sizeof(MDeformVert);
135 memcpy(dest, source, count * size);
137 for(i = 0; i < count; ++i) {
138 MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
140 if(dvert->totweight) {
141 MDeformWeight *dw = MEM_callocN(dvert->totweight * sizeof(*dw),
142 "layerCopy_mdeformvert dw");
144 memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
152 static void layerFree_mdeformvert(void *data, int count, int size)
156 for(i = 0; i < count; ++i) {
157 MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
160 MEM_freeN(dvert->dw);
162 dvert->totweight = 0;
167 static void linklist_free_simple(void *link)
172 static void layerInterp_mdeformvert(void **sources, float *weights,
173 float *UNUSED(sub_weights), int count, void *dest)
175 MDeformVert *dvert = dest;
176 LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
180 if(count <= 0) return;
182 /* build a list of unique def_nrs for dest */
184 for(i = 0; i < count; ++i) {
185 MDeformVert *source = sources[i];
186 float interp_weight = weights ? weights[i] : 1.0f;
188 for(j = 0; j < source->totweight; ++j) {
189 MDeformWeight *dw = &source->dw[j];
191 for(node = dest_dw; node; node = node->next) {
192 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
194 if(tmp_dw->def_nr == dw->def_nr) {
195 tmp_dw->weight += dw->weight * interp_weight;
200 /* if this def_nr is not in the list, add it */
202 MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
203 "layerInterp_mdeformvert tmp_dw");
204 tmp_dw->def_nr = dw->def_nr;
205 tmp_dw->weight = dw->weight * interp_weight;
206 BLI_linklist_prepend(&dest_dw, tmp_dw);
212 /* now we know how many unique deform weights there are, so realloc */
213 if(dvert->dw) MEM_freeN(dvert->dw);
216 dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
217 "layerInterp_mdeformvert dvert->dw");
218 dvert->totweight = totweight;
220 for(i = 0, node = dest_dw; node; node = node->next, ++i)
221 dvert->dw[i] = *((MDeformWeight *)node->link);
224 memset(dvert, 0, sizeof(*dvert));
226 BLI_linklist_free(dest_dw, linklist_free_simple);
230 static void layerInterp_msticky(void **sources, float *weights,
231 float *UNUSED(sub_weights), int count, void *dest)
237 co[0] = co[1] = 0.0f;
238 for(i = 0; i < count; i++) {
239 w = weights ? weights[i] : 1.0f;
240 mst = (MSticky*)sources[i];
242 madd_v2_v2fl(co, mst->co, w);
245 mst = (MSticky*)dest;
246 copy_v2_v2(mst->co, co);
250 static void layerCopy_tface(const void *source, void *dest, int count)
252 const MTFace *source_tf = (const MTFace*)source;
253 MTFace *dest_tf = (MTFace*)dest;
256 for(i = 0; i < count; ++i)
257 dest_tf[i] = source_tf[i];
260 static void layerInterp_tface(void **sources, float *weights,
261 float *sub_weights, int count, void *dest)
265 float uv[4][2] = {{0.0f}};
268 if(count <= 0) return;
270 sub_weight = sub_weights;
271 for(i = 0; i < count; ++i) {
272 float weight = weights ? weights[i] : 1;
273 MTFace *src = sources[i];
275 for(j = 0; j < 4; ++j) {
277 for(k = 0; k < 4; ++k, ++sub_weight) {
278 madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
282 madd_v2_v2fl(uv[j], src->uv[j], weight);
287 *tf = *(MTFace *)(*sources);
288 memcpy(tf->uv, uv, sizeof(tf->uv));
291 static void layerSwap_tface(void *data, const int *corner_indices)
295 static const short pin_flags[4] =
296 { TF_PIN1, TF_PIN2, TF_PIN3, TF_PIN4 };
297 static const char sel_flags[4] =
298 { TF_SEL1, TF_SEL2, TF_SEL3, TF_SEL4 };
299 short unwrap = tf->unwrap & ~(TF_PIN1 | TF_PIN2 | TF_PIN3 | TF_PIN4);
300 char flag = tf->flag & ~(TF_SEL1 | TF_SEL2 | TF_SEL3 | TF_SEL4);
303 for(j = 0; j < 4; ++j) {
304 const int source_index = corner_indices[j];
306 copy_v2_v2(uv[j], tf->uv[source_index]);
308 // swap pinning flags around
309 if(tf->unwrap & pin_flags[source_index]) {
310 unwrap |= pin_flags[j];
313 // swap selection flags around
314 if(tf->flag & sel_flags[source_index]) {
315 flag |= sel_flags[j];
319 memcpy(tf->uv, uv, sizeof(tf->uv));
324 static void layerDefault_tface(void *data, int count)
326 static MTFace default_tf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, NULL,
327 0, 0, TF_DYNAMIC|TF_CONVERTED, 0, 0};
328 MTFace *tf = (MTFace*)data;
331 for(i = 0; i < count; i++)
335 static void layerCopy_propFloat(const void *source, void *dest,
338 memcpy(dest, source, sizeof(MFloatProperty)*count);
341 static void layerCopy_propInt(const void *source, void *dest,
344 memcpy(dest, source, sizeof(MIntProperty)*count);
347 static void layerCopy_propString(const void *source, void *dest,
350 memcpy(dest, source, sizeof(MStringProperty)*count);
353 static void layerCopy_origspace_face(const void *source, void *dest, int count)
355 const OrigSpaceFace *source_tf = (const OrigSpaceFace*)source;
356 OrigSpaceFace *dest_tf = (OrigSpaceFace*)dest;
359 for(i = 0; i < count; ++i)
360 dest_tf[i] = source_tf[i];
363 static void layerInterp_origspace_face(void **sources, float *weights,
364 float *sub_weights, int count, void *dest)
366 OrigSpaceFace *osf = dest;
368 float uv[4][2] = {{0.0f}};
371 if(count <= 0) return;
373 sub_weight = sub_weights;
374 for(i = 0; i < count; ++i) {
375 float weight = weights ? weights[i] : 1;
376 OrigSpaceFace *src = sources[i];
378 for(j = 0; j < 4; ++j) {
380 for(k = 0; k < 4; ++k, ++sub_weight) {
381 madd_v2_v2fl(uv[j], src->uv[k], (*sub_weight) * weight);
384 madd_v2_v2fl(uv[j], src->uv[j], weight);
389 #if 0 /* no need, this ONLY contains UV's */
390 *osf = *(OrigSpaceFace *)(*sources);
392 memcpy(osf->uv, uv, sizeof(osf->uv));
395 static void layerSwap_origspace_face(void *data, const int *corner_indices)
397 OrigSpaceFace *osf = data;
401 for(j = 0; j < 4; ++j) {
402 copy_v2_v2(uv[j], osf->uv[corner_indices[j]]);
404 memcpy(osf->uv, uv, sizeof(osf->uv));
407 static void layerDefault_origspace_face(void *data, int count)
409 static OrigSpaceFace default_osf = {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}};
410 OrigSpaceFace *osf = (OrigSpaceFace*)data;
413 for(i = 0; i < count; i++)
414 osf[i] = default_osf;
417 static void layerSwap_mdisps(void *data, const int *ci)
420 float (*d)[3] = NULL;
421 int corners, cornersize, S;
424 int nverts= (ci[1] == 3) ? 4 : 3; /* silly way to know vertex count of face */
425 corners= multires_mdisp_corners(s);
426 cornersize= s->totdisp/corners;
428 if(corners!=nverts) {
429 /* happens when face changed vertex count in edit mode
430 if it happened, just forgot displacement */
433 s->totdisp= (s->totdisp/corners)*nverts;
434 s->disps= MEM_callocN(s->totdisp*sizeof(float)*3, "mdisp swap");
438 d= MEM_callocN(sizeof(float) * 3 * s->totdisp, "mdisps swap");
440 for(S = 0; S < corners; S++)
441 memcpy(d + cornersize*S, s->disps + cornersize*ci[S], cornersize*3*sizeof(float));
448 #if 1 /* BMESH_TODO: place holder function, dont actually interp */
449 static void layerInterp_mdisps(void **sources, float *UNUSED(weights),
450 float *UNUSED(sub_weights), int UNUSED(count), void *dest)
454 /* happens when flipping normals of newly created mesh */
456 d->totdisp = ((MDisps*)sources[0])->totdisp;
459 if (!d->disps && d->totdisp)
460 d->disps = MEM_callocN(sizeof(float)*3*d->totdisp, "blank mdisps in layerInterp_mdisps");
465 static void layerInterp_mdisps(void **sources, float *UNUSED(weights),
466 float *sub_weights, int count, void *dest)
472 int side, S, dst_corners, src_corners;
473 float crn_weight[4][2];
474 float (*sw)[4] = (void*)sub_weights;
475 float (*disps)[3], (*out)[3];
477 /* happens when flipping normals of newly created mesh */
482 dst_corners = multires_mdisp_corners(d);
483 src_corners = multires_mdisp_corners(s);
485 if(sub_weights && count == 2 && src_corners == 3) {
486 src_corners = multires_mdisp_corners(sources[1]);
488 /* special case -- converting two triangles to quad */
489 if(src_corners == 3 && dst_corners == 4) {
493 for(i = 0; i < 2; i++)
494 for(y = 0; y < 4; y++)
495 for(x = 0; x < 4; x++)
499 for(i = 0; i < 2; i++) {
500 float sw_m4[4][4] = {{0}};
501 int a = 7 & ~(1 << vindex[i*2] | 1 << vindex[i*2+1]);
503 sw_m4[0][vindex[i*2+1]] = 1;
504 sw_m4[1][vindex[i*2]] = 1;
506 for(x = 0; x < 3; x++)
510 tris[i] = *((MDisps*)sources[i]);
511 tris[i].disps = MEM_dupallocN(tris[i].disps);
512 layerInterp_mdisps(&sources[i], NULL, (float*)sw_m4, 1, &tris[i]);
515 mdisp_join_tris(d, &tris[0], &tris[1]);
517 for(i = 0; i < 2; i++)
518 MEM_freeN(tris[i].disps);
524 /* For now, some restrictions on the input */
525 if(count != 1 || !sub_weights) {
526 for(i = 0; i < d->totdisp; ++i)
527 zero_v3(d->disps[i]);
532 /* Initialize the destination */
533 disps = MEM_callocN(3*d->totdisp*sizeof(float), "iterp disps");
535 side = sqrt(d->totdisp / dst_corners);
539 sw= (void*)sub_weights;
540 for(i = 0; i < 4; ++i) {
541 crn_weight[i][0] = 0 * sw[i][0] + stl * sw[i][1] + stl * sw[i][2] + 0 * sw[i][3];
542 crn_weight[i][1] = 0 * sw[i][0] + 0 * sw[i][1] + stl * sw[i][2] + stl * sw[i][3];
545 multires_mdisp_smooth_bounds(s);
548 for(S = 0; S < dst_corners; S++) {
549 float base[2], axis_x[2], axis_y[2];
551 mdisp_apply_weight(S, dst_corners, 0, 0, st, crn_weight, &base[0], &base[1]);
552 mdisp_apply_weight(S, dst_corners, side-1, 0, st, crn_weight, &axis_x[0], &axis_x[1]);
553 mdisp_apply_weight(S, dst_corners, 0, side-1, st, crn_weight, &axis_y[0], &axis_y[1]);
555 sub_v2_v2(axis_x, base);
556 sub_v2_v2(axis_y, base);
557 normalize_v2(axis_x);
558 normalize_v2(axis_y);
560 for(y = 0; y < side; ++y) {
561 for(x = 0; x < side; ++x, ++out) {
563 float face_u, face_v, crn_u, crn_v;
565 mdisp_apply_weight(S, dst_corners, x, y, st, crn_weight, &face_u, &face_v);
566 crn = mdisp_rot_face_to_quad_crn(src_corners, st, face_u, face_v, &crn_u, &crn_v);
568 old_mdisps_bilinear((*out), &s->disps[crn*side*side], side, crn_u, crn_v);
569 mdisp_flip_disp(crn, dst_corners, axis_x, axis_y, *out);
579 static void layerCopy_mdisps(const void *source, void *dest, int count)
582 const MDisps *s = source;
585 for(i = 0; i < count; ++i) {
587 d[i].disps = MEM_dupallocN(s[i].disps);
588 d[i].totdisp = s[i].totdisp;
598 static void layerValidate_mdisps(void *data, int sub_elements)
604 MDisps *disps = data;
606 int corners = multires_mdisp_corners(disps);
608 if(corners != sub_elements) {
609 MEM_freeN(disps->disps);
610 disps->totdisp = disps->totdisp / corners * sub_elements;
611 disps->disps = MEM_callocN(3*disps->totdisp*sizeof(float), "layerValidate_mdisps");
617 static void layerFree_mdisps(void *data, int count, int UNUSED(size))
622 for(i = 0; i < count; ++i) {
624 MEM_freeN(d[i].disps);
630 static int layerRead_mdisps(CDataFile *cdf, void *data, int count)
635 for(i = 0; i < count; ++i) {
637 d[i].disps = MEM_callocN(sizeof(float)*3*d[i].totdisp, "mdisps read");
639 if(!cdf_read_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
640 printf("failed to read multires displacement %d/%d %d\n", i, count, d[i].totdisp);
648 static int layerWrite_mdisps(CDataFile *cdf, void *data, int count)
653 for(i = 0; i < count; ++i) {
654 if(!cdf_write_data(cdf, d[i].totdisp*3*sizeof(float), d[i].disps)) {
655 printf("failed to write multires displacement %d/%d %d\n", i, count, d[i].totdisp);
663 static size_t layerFilesize_mdisps(CDataFile *UNUSED(cdf), void *data, int count)
669 for(i = 0; i < count; ++i)
670 size += d[i].totdisp*3*sizeof(float);
676 static void layerCopyValue_mloopcol(void *source, void *dest)
678 MLoopCol *m1 = source, *m2 = dest;
686 static int layerEqual_mloopcol(void *data1, void *data2)
688 MLoopCol *m1 = data1, *m2 = data2;
696 return r*r + g*g + b*b + a*a < 0.001;
699 static void layerMultiply_mloopcol(void *data, float fac)
703 m->r = (float)m->r * fac;
704 m->g = (float)m->g * fac;
705 m->b = (float)m->b * fac;
706 m->a = (float)m->a * fac;
709 static void layerAdd_mloopcol(void *data1, void *data2)
711 MLoopCol *m = data1, *m2 = data2;
719 static void layerDoMinMax_mloopcol(void *data, void *vmin, void *vmax)
722 MLoopCol *min = vmin, *max = vmax;
724 if (m->r < min->r) min->r = m->r;
725 if (m->g < min->g) min->g = m->g;
726 if (m->b < min->b) min->b = m->b;
727 if (m->a < min->a) min->a = m->a;
729 if (m->r > max->r) max->r = m->r;
730 if (m->g > max->g) max->g = m->g;
731 if (m->b > max->b) max->b = m->b;
732 if (m->a > max->a) max->a = m->a;
735 static void layerInitMinMax_mloopcol(void *vmin, void *vmax)
737 MLoopCol *min = vmin, *max = vmax;
750 static void layerDefault_mloopcol(void *data, int count)
752 MLoopCol default_mloopcol = {255,255,255,255};
753 MLoopCol *mlcol = (MLoopCol*)data;
755 for(i = 0; i < count; i++)
756 mlcol[i] = default_mloopcol;
760 static void layerInterp_mloopcol(void **sources, float *weights,
761 float *sub_weights, int count, void *dest)
772 col.a = col.r = col.g = col.b = 0;
774 sub_weight = sub_weights;
775 for(i = 0; i < count; ++i){
776 float weight = weights ? weights[i] : 1;
777 MLoopCol *src = sources[i];
779 col.a += src->a * (*sub_weight) * weight;
780 col.r += src->r * (*sub_weight) * weight;
781 col.g += src->g * (*sub_weight) * weight;
782 col.b += src->b * (*sub_weight) * weight;
785 col.a += src->a * weight;
786 col.r += src->r * weight;
787 col.g += src->g * weight;
788 col.b += src->b * weight;
792 /* Subdivide smooth or fractal can cause problems without clamping
793 * although weights should also not cause this situation */
794 CLAMP(col.a, 0.0f, 255.0f);
795 CLAMP(col.r, 0.0f, 255.0f);
796 CLAMP(col.g, 0.0f, 255.0f);
797 CLAMP(col.b, 0.0f, 255.0f);
805 static void layerCopyValue_mloopuv(void *source, void *dest)
807 MLoopUV *luv1 = source, *luv2 = dest;
809 copy_v2_v2(luv2->uv, luv1->uv);
812 static int layerEqual_mloopuv(void *data1, void *data2)
814 MLoopUV *luv1 = data1, *luv2 = data2;
816 return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
819 static void layerMultiply_mloopuv(void *data, float fac)
823 mul_v2_fl(luv->uv, fac);
826 static void layerInitMinMax_mloopuv(void *vmin, void *vmax)
828 MLoopUV *min = vmin, *max = vmax;
830 INIT_MINMAX2(min->uv, max->uv);
833 static void layerDoMinMax_mloopuv(void *data, void *vmin, void *vmax)
835 MLoopUV *min = vmin, *max = vmax, *luv = data;
837 DO_MINMAX2(luv->uv, min->uv, max->uv);
840 static void layerAdd_mloopuv(void *data1, void *data2)
842 MLoopUV *l1 = data1, *l2 = data2;
844 add_v2_v2(l1->uv, l2->uv);
847 static void layerInterp_mloopuv(void **sources, float *weights,
848 float *sub_weights, int count, void *dest)
850 MLoopUV *mluv = dest;
857 const float *sub_weight = sub_weights;
858 for(i = 0; i < count; i++) {
859 float weight = weights ? weights[i] : 1.0f;
860 MLoopUV *src = sources[i];
861 madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
866 for(i = 0; i < count; i++) {
867 float weight = weights ? weights[i] : 1;
868 MLoopUV *src = sources[i];
869 madd_v2_v2fl(uv, src->uv, weight);
874 /* origspace is almost exact copy of mloopuv's, keep in sync */
875 static void layerCopyValue_mloop_origspace(void *source, void *dest)
877 OrigSpaceLoop *luv1 = source, *luv2 = dest;
879 copy_v2_v2(luv2->uv, luv1->uv);
882 static int layerEqual_mloop_origspace(void *data1, void *data2)
884 OrigSpaceLoop *luv1 = data1, *luv2 = data2;
886 return len_squared_v2v2(luv1->uv, luv2->uv) < 0.00001f;
889 static void layerMultiply_mloop_origspace(void *data, float fac)
891 OrigSpaceLoop *luv = data;
893 mul_v2_fl(luv->uv, fac);
896 static void layerInitMinMax_mloop_origspace(void *vmin, void *vmax)
898 OrigSpaceLoop *min = vmin, *max = vmax;
900 INIT_MINMAX2(min->uv, max->uv);
903 static void layerDoMinMax_mloop_origspace(void *data, void *vmin, void *vmax)
905 OrigSpaceLoop *min = vmin, *max = vmax, *luv = data;
907 DO_MINMAX2(luv->uv, min->uv, max->uv);
910 static void layerAdd_mloop_origspace(void *data1, void *data2)
912 OrigSpaceLoop *l1 = data1, *l2 = data2;
914 add_v2_v2(l1->uv, l2->uv);
917 static void layerInterp_mloop_origspace(void **sources, float *weights,
918 float *sub_weights, int count, void *dest)
920 OrigSpaceLoop *mluv = dest;
927 const float *sub_weight = sub_weights;
928 for(i = 0; i < count; i++) {
929 float weight = weights ? weights[i] : 1.0f;
930 OrigSpaceLoop *src = sources[i];
931 madd_v2_v2fl(uv, src->uv, (*sub_weight) * weight);
936 for(i = 0; i < count; i++) {
937 float weight = weights ? weights[i] : 1;
938 OrigSpaceLoop *src = sources[i];
939 madd_v2_v2fl(uv, src->uv, weight);
945 static void layerInterp_mcol(void **sources, float *weights,
946 float *sub_weights, int count, void *dest)
959 if(count <= 0) return;
961 sub_weight = sub_weights;
962 for(i = 0; i < count; ++i) {
963 float weight = weights ? weights[i] : 1;
965 for(j = 0; j < 4; ++j) {
967 MCol *src = sources[i];
968 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
969 const float w= (*sub_weight) * weight;
970 col[j].a += src->a * w;
971 col[j].r += src->r * w;
972 col[j].g += src->g * w;
973 col[j].b += src->b * w;
976 MCol *src = sources[i];
977 col[j].a += src[j].a * weight;
978 col[j].r += src[j].r * weight;
979 col[j].g += src[j].g * weight;
980 col[j].b += src[j].b * weight;
985 for(j = 0; j < 4; ++j) {
987 /* Subdivide smooth or fractal can cause problems without clamping
988 * although weights should also not cause this situation */
989 CLAMP(col[j].a, 0.0f, 255.0f);
990 CLAMP(col[j].r, 0.0f, 255.0f);
991 CLAMP(col[j].g, 0.0f, 255.0f);
992 CLAMP(col[j].b, 0.0f, 255.0f);
994 mc[j].a = (int)col[j].a;
995 mc[j].r = (int)col[j].r;
996 mc[j].g = (int)col[j].g;
997 mc[j].b = (int)col[j].b;
1001 static void layerSwap_mcol(void *data, const int *corner_indices)
1007 for(j = 0; j < 4; ++j)
1008 col[j] = mcol[corner_indices[j]];
1010 memcpy(mcol, col, sizeof(col));
1013 static void layerDefault_mcol(void *data, int count)
1015 static MCol default_mcol = {255, 255, 255, 255};
1016 MCol *mcol = (MCol*)data;
1019 for(i = 0; i < 4*count; i++) {
1020 mcol[i] = default_mcol;
1024 static void layerInterp_bweight(void **sources, float *weights,
1025 float *UNUSED(sub_weights), int count, void *dest)
1028 float **in = (float **)sources;
1031 if(count <= 0) return;
1036 for(i = 0; i < count; ++i) {
1037 *f += *in[i] * weights[i];
1041 for(i = 0; i < count; ++i) {
1047 static void layerInterp_shapekey(void **sources, float *weights,
1048 float *UNUSED(sub_weights), int count, void *dest)
1051 float **in = (float **)sources;
1054 if(count <= 0) return;
1059 for(i = 0; i < count; ++i) {
1060 madd_v3_v3fl(co, in[i], weights[i]);
1064 for(i = 0; i < count; ++i) {
1065 add_v3_v3(co, in[i]);
1070 static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
1072 {sizeof(MVert), "MVert", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1074 {sizeof(MSticky), "MSticky", 1, NULL, NULL, NULL, layerInterp_msticky, NULL,
1076 /* 2: CD_MDEFORMVERT */
1077 {sizeof(MDeformVert), "MDeformVert", 1, NULL, layerCopy_mdeformvert,
1078 layerFree_mdeformvert, layerInterp_mdeformvert, NULL, NULL},
1080 {sizeof(MEdge), "MEdge", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1082 {sizeof(MFace), "MFace", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1084 {sizeof(MTFace), "MTFace", 1, "UVMap", layerCopy_tface, NULL,
1085 layerInterp_tface, layerSwap_tface, layerDefault_tface},
1087 /* 4 MCol structs per face */
1088 {sizeof(MCol)*4, "MCol", 4, "Col", NULL, NULL, layerInterp_mcol,
1089 layerSwap_mcol, layerDefault_mcol},
1090 /* 7: CD_ORIGINDEX */
1091 {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1093 /* 3 floats per normal vector */
1094 {sizeof(float)*3, "vec3f", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1095 /* 9: CD_POLYINDEX */
1096 {sizeof(int), "MIntProperty", 1, NULL, NULL, NULL, NULL, NULL, NULL},
1097 /* 10: CD_PROP_FLT */
1098 {sizeof(MFloatProperty), "MFloatProperty",1,"Float", layerCopy_propFloat,NULL,NULL,NULL},
1099 /* 11: CD_PROP_INT */
1100 {sizeof(MIntProperty), "MIntProperty",1,"Int",layerCopy_propInt,NULL,NULL,NULL},
1101 /* 12: CD_PROP_STR */
1102 {sizeof(MStringProperty), "MStringProperty",1,"String",layerCopy_propString,NULL,NULL,NULL},
1103 /* 13: CD_ORIGSPACE */
1104 {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVMap", layerCopy_origspace_face, NULL,
1105 layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face},
1107 {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1108 /* 15: CD_MTEXPOLY */
1109 {sizeof(MTexPoly), "MTexPoly", 1, "Face Texture", NULL, NULL, NULL, NULL, NULL},
1110 /* 16: CD_MLOOPUV */
1111 {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, layerInterp_mloopuv, NULL, NULL,
1112 layerEqual_mloopuv, layerMultiply_mloopuv, layerInitMinMax_mloopuv,
1113 layerAdd_mloopuv, layerDoMinMax_mloopuv, layerCopyValue_mloopuv},
1114 /* 17: CD_MLOOPCOL */
1115 {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, layerInterp_mloopcol, NULL,
1116 layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol,
1117 layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
1118 /* 18: CD_TANGENT */
1119 {sizeof(float)*4*4, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1121 {sizeof(MDisps), "MDisps", 1, NULL, layerCopy_mdisps,
1122 layerFree_mdisps, layerInterp_mdisps, layerSwap_mdisps, NULL,
1123 NULL, NULL, NULL, NULL, NULL, NULL,
1124 layerRead_mdisps, layerWrite_mdisps, layerFilesize_mdisps, layerValidate_mdisps},
1125 /* 20: CD_WEIGHT_MCOL */
1126 {sizeof(MCol)*4, "MCol", 4, "WeightCol", NULL, NULL, layerInterp_mcol,
1127 layerSwap_mcol, layerDefault_mcol},
1128 /* 21: CD_ID_MCOL */
1129 {sizeof(MCol)*4, "MCol", 4, "IDCol", NULL, NULL, layerInterp_mcol,
1130 layerSwap_mcol, layerDefault_mcol},
1131 /* 22: CD_TEXTURE_MCOL */
1132 {sizeof(MCol)*4, "MCol", 4, "TexturedCol", NULL, NULL, layerInterp_mcol,
1133 layerSwap_mcol, layerDefault_mcol},
1134 /* 23: CD_CLOTH_ORCO */
1135 {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1137 {sizeof(MRecast), "MRecast", 1,"Recast",NULL,NULL,NULL,NULL}
1142 {sizeof(MPoly), "MPoly", 1, "NGon Face", NULL, NULL, NULL, NULL, NULL},
1144 {sizeof(MLoop), "MLoop", 1, "NGon Face-Vertex", NULL, NULL, NULL, NULL, NULL},
1145 /* 27: CD_SHAPE_KEYINDEX */
1146 {sizeof(int), "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
1147 /* 28: CD_SHAPEKEY */
1148 {sizeof(float)*3, "", 0, "ShapeKey", NULL, NULL, layerInterp_shapekey},
1149 /* 29: CD_BWEIGHT */
1150 {sizeof(float), "", 0, "BevelWeight", NULL, NULL, layerInterp_bweight},
1152 {sizeof(float), "", 0, "SubSurfCrease", NULL, NULL, layerInterp_bweight},
1153 /* 31: CD_ORIGSPACE_MLOOP */
1154 {sizeof(OrigSpaceLoop), "OrigSpaceLoop", 1, "OS Loop", NULL, NULL, layerInterp_mloop_origspace, NULL, NULL,
1155 layerEqual_mloop_origspace, layerMultiply_mloop_origspace, layerInitMinMax_mloop_origspace,
1156 layerAdd_mloop_origspace, layerDoMinMax_mloop_origspace, layerCopyValue_mloop_origspace},
1157 /* 32: CD_WEIGHT_MLOOPCOL */
1158 {sizeof(MLoopCol), "MLoopCol", 1, "WeightLoopCol", NULL, NULL, layerInterp_mloopcol, NULL,
1159 layerDefault_mloopcol, layerEqual_mloopcol, layerMultiply_mloopcol, layerInitMinMax_mloopcol,
1160 layerAdd_mloopcol, layerDoMinMax_mloopcol, layerCopyValue_mloopcol},
1161 /* END BMESH ONLY */
1166 /* note, numbers are from trunk and need updating for bmesh */
1168 static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
1169 /* 0-4 */ "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace",
1170 /* 5-9 */ "CDMTFace", "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags",
1171 /* 10-14 */ "CDMFloatProperty", "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco",
1172 /* 15-19 */ "CDMTexPoly", "CDMLoopUV", "CDMloopCol", "CDTangent", "CDMDisps",
1173 /* 20-24 */"CDWeightMCol", "CDIDMCol", "CDTextureMCol", "CDClothOrco", "CDMRecast"
1177 /* 25-29 */ "CDMPoly", "CDMLoop", "CDShapeKeyIndex", "CDShapeKey", "CDBevelWeight",
1178 /* 30-32 */ "CDSubSurfCrease", "CDOrigSpaceLoop", "CDWeightLoopCol"
1179 /* END BMESH ONLY */
1184 const CustomDataMask CD_MASK_BAREMESH =
1185 CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE | CD_MASK_MLOOP | CD_MASK_MPOLY | CD_MASK_BWEIGHT;
1186 const CustomDataMask CD_MASK_MESH =
1187 CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
1188 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
1189 CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS |
1190 CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MPOLY | CD_MASK_MLOOP |
1191 CD_MASK_MTEXPOLY | CD_MASK_NORMAL | CD_MASK_RECAST;
1192 const CustomDataMask CD_MASK_EDITMESH =
1193 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MLOOPUV |
1194 CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_SHAPE_KEYINDEX |
1195 CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR |
1196 CD_MASK_MDISPS | CD_MASK_SHAPEKEY | CD_MASK_RECAST;
1197 const CustomDataMask CD_MASK_DERIVEDMESH =
1198 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
1199 CD_MASK_MCOL | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_CLOTH_ORCO |
1200 CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY | CD_MASK_WEIGHT_MLOOPCOL |
1201 CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORIGSPACE_MLOOP | CD_MASK_ORCO | CD_MASK_TANGENT |
1202 CD_MASK_WEIGHT_MCOL | CD_MASK_NORMAL | CD_MASK_SHAPEKEY | CD_MASK_RECAST |
1203 CD_MASK_ORIGINDEX | CD_MASK_POLYINDEX;
1204 const CustomDataMask CD_MASK_BMESH = CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL | CD_MASK_MTEXPOLY |
1205 CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_PROP_FLT | CD_MASK_PROP_INT |
1206 CD_MASK_PROP_STR | CD_MASK_SHAPEKEY | CD_MASK_SHAPE_KEYINDEX | CD_MASK_MDISPS;
1207 const CustomDataMask CD_MASK_FACECORNERS =
1208 CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_MTEXPOLY | CD_MASK_MLOOPUV |
1211 static const LayerTypeInfo *layerType_getInfo(int type)
1213 if(type < 0 || type >= CD_NUMTYPES) return NULL;
1215 return &LAYERTYPEINFO[type];
1218 static const char *layerType_getName(int type)
1220 if(type < 0 || type >= CD_NUMTYPES) return NULL;
1222 return LAYERTYPENAMES[type];
1225 /********************* CustomData functions *********************/
1226 static void customData_update_offsets(CustomData *data);
1228 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1229 int type, int alloctype, void *layerdata, int totelem, const char *name);
1231 void CustomData_update_typemap(CustomData *data)
1233 int i, lasttype = -1;
1235 /* since we cant do in a pre-processor do here as an assert */
1236 BLI_assert(sizeof(data->typemap) / sizeof(int) >= CD_NUMTYPES);
1238 for (i=0; i<CD_NUMTYPES; i++) {
1239 data->typemap[i] = -1;
1242 for (i=0; i<data->totlayer; i++) {
1243 if (data->layers[i].type != lasttype) {
1244 data->typemap[data->layers[i].type] = i;
1246 lasttype = data->layers[i].type;
1250 void CustomData_merge(const struct CustomData *source, struct CustomData *dest,
1251 CustomDataMask mask, int alloctype, int totelem)
1253 /*const LayerTypeInfo *typeInfo;*/
1254 CustomDataLayer *layer, *newlayer;
1256 int i, type, number = 0, lasttype = -1, lastactive = 0, lastrender = 0, lastclone = 0, lastmask = 0, lastflag = 0;
1258 for(i = 0; i < source->totlayer; ++i) {
1259 layer = &source->layers[i];
1260 /*typeInfo = layerType_getInfo(layer->type);*/ /*UNUSED*/
1264 if (type != lasttype) {
1266 lastactive = layer->active;
1267 lastrender = layer->active_rnd;
1268 lastclone = layer->active_clone;
1269 lastmask = layer->active_mask;
1271 lastflag = layer->flag;
1276 if(lastflag & CD_FLAG_NOCOPY) continue;
1277 else if(!(mask & CD_TYPE_AS_MASK(type))) continue;
1278 else if(number < CustomData_number_of_layers(dest, type)) continue;
1280 switch (alloctype) {
1291 if((alloctype == CD_ASSIGN) && (lastflag & CD_FLAG_NOFREE))
1292 newlayer = customData_add_layer__internal(dest, type, CD_REFERENCE,
1293 data, totelem, layer->name);
1295 newlayer = customData_add_layer__internal(dest, type, alloctype,
1296 data, totelem, layer->name);
1299 newlayer->uid = layer->uid;
1301 newlayer->active = lastactive;
1302 newlayer->active_rnd = lastrender;
1303 newlayer->active_clone = lastclone;
1304 newlayer->active_mask = lastmask;
1305 newlayer->flag |= lastflag & (CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY);
1309 CustomData_update_typemap(dest);
1312 void CustomData_copy(const struct CustomData *source, struct CustomData *dest,
1313 CustomDataMask mask, int alloctype, int totelem)
1315 memset(dest, 0, sizeof(*dest));
1317 if(source->external)
1318 dest->external= MEM_dupallocN(source->external);
1320 CustomData_merge(source, dest, mask, alloctype, totelem);
1323 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
1325 const LayerTypeInfo *typeInfo;
1327 if(!(layer->flag & CD_FLAG_NOFREE) && layer->data) {
1328 typeInfo = layerType_getInfo(layer->type);
1331 typeInfo->free(layer->data, totelem, typeInfo->size);
1334 MEM_freeN(layer->data);
1338 static void CustomData_external_free(CustomData *data)
1340 if(data->external) {
1341 MEM_freeN(data->external);
1342 data->external= NULL;
1346 void CustomData_free(CustomData *data, int totelem)
1350 for(i = 0; i < data->totlayer; ++i)
1351 customData_free_layer__internal(&data->layers[i], totelem);
1354 MEM_freeN(data->layers);
1356 CustomData_external_free(data);
1358 memset(data, 0, sizeof(*data));
1361 static void customData_update_offsets(CustomData *data)
1363 const LayerTypeInfo *typeInfo;
1366 for(i = 0; i < data->totlayer; ++i) {
1367 typeInfo = layerType_getInfo(data->layers[i].type);
1369 data->layers[i].offset = offset;
1370 offset += typeInfo->size;
1373 data->totsize = offset;
1374 CustomData_update_typemap(data);
1377 int CustomData_get_layer_index(const CustomData *data, int type)
1381 for(i=0; i < data->totlayer; ++i)
1382 if(data->layers[i].type == type)
1388 int CustomData_get_layer_index_n(const struct CustomData *data, int type, int n)
1390 int i = CustomData_get_layer_index(data, type);
1393 i = (data->layers[i + n].type == type) ? (i + n) : (-1);
1399 int CustomData_get_named_layer_index(const CustomData *data, int type, const char *name)
1403 for(i=0; i < data->totlayer; ++i)
1404 if(data->layers[i].type == type && strcmp(data->layers[i].name, name)==0)
1410 int CustomData_get_active_layer_index(const CustomData *data, int type)
1412 if (!data->totlayer)
1415 if (data->typemap[type] != -1) {
1416 return data->typemap[type] + data->layers[data->typemap[type]].active;
1422 int CustomData_get_render_layer_index(const CustomData *data, int type)
1426 for(i=0; i < data->totlayer; ++i)
1427 if(data->layers[i].type == type)
1428 return i + data->layers[i].active_rnd;
1433 int CustomData_get_clone_layer_index(const CustomData *data, int type)
1437 for(i=0; i < data->totlayer; ++i)
1438 if(data->layers[i].type == type)
1439 return i + data->layers[i].active_clone;
1444 int CustomData_get_stencil_layer_index(const CustomData *data, int type)
1448 for(i=0; i < data->totlayer; ++i)
1449 if(data->layers[i].type == type)
1450 return i + data->layers[i].active_mask;
1455 int CustomData_get_active_layer(const CustomData *data, int type)
1459 for(i=0; i < data->totlayer; ++i)
1460 if(data->layers[i].type == type)
1461 return data->layers[i].active;
1466 int CustomData_get_render_layer(const CustomData *data, int type)
1470 for(i=0; i < data->totlayer; ++i)
1471 if(data->layers[i].type == type)
1472 return data->layers[i].active_rnd;
1477 int CustomData_get_clone_layer(const CustomData *data, int type)
1481 for(i=0; i < data->totlayer; ++i)
1482 if(data->layers[i].type == type)
1483 return data->layers[i].active_clone;
1488 int CustomData_get_stencil_layer(const CustomData *data, int type)
1492 for(i=0; i < data->totlayer; ++i)
1493 if(data->layers[i].type == type)
1494 return data->layers[i].active_mask;
1499 void CustomData_set_layer_active(CustomData *data, int type, int n)
1503 for(i=0; i < data->totlayer; ++i)
1504 if(data->layers[i].type == type)
1505 data->layers[i].active = n;
1508 void CustomData_set_layer_render(CustomData *data, int type, int n)
1512 for(i=0; i < data->totlayer; ++i)
1513 if(data->layers[i].type == type)
1514 data->layers[i].active_rnd = n;
1517 void CustomData_set_layer_clone(CustomData *data, int type, int n)
1521 for(i=0; i < data->totlayer; ++i)
1522 if(data->layers[i].type == type)
1523 data->layers[i].active_clone = n;
1526 void CustomData_set_layer_stencil(CustomData *data, int type, int n)
1530 for(i=0; i < data->totlayer; ++i)
1531 if(data->layers[i].type == type)
1532 data->layers[i].active_mask = n;
1535 /* for using with an index from CustomData_get_active_layer_index and CustomData_get_render_layer_index */
1536 void CustomData_set_layer_active_index(CustomData *data, int type, int n)
1540 for(i=0; i < data->totlayer; ++i)
1541 if(data->layers[i].type == type)
1542 data->layers[i].active = n-i;
1545 void CustomData_set_layer_render_index(CustomData *data, int type, int n)
1549 for(i=0; i < data->totlayer; ++i)
1550 if(data->layers[i].type == type)
1551 data->layers[i].active_rnd = n-i;
1554 void CustomData_set_layer_clone_index(CustomData *data, int type, int n)
1558 for(i=0; i < data->totlayer; ++i)
1559 if(data->layers[i].type == type)
1560 data->layers[i].active_clone = n-i;
1563 void CustomData_set_layer_stencil_index(CustomData *data, int type, int n)
1567 for(i=0; i < data->totlayer; ++i)
1568 if(data->layers[i].type == type)
1569 data->layers[i].active_mask = n-i;
1572 void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
1576 for(i=0; i < data->totlayer; ++i)
1577 if(data->layers[i].type == type)
1578 data->layers[i].flag |= flag;
1581 static int customData_resize(CustomData *data, int amount)
1583 CustomDataLayer *tmp = MEM_callocN(sizeof(*tmp)*(data->maxlayer + amount),
1584 "CustomData->layers");
1587 data->maxlayer += amount;
1589 memcpy(tmp, data->layers, sizeof(*tmp) * data->totlayer);
1590 MEM_freeN(data->layers);
1597 static CustomDataLayer *customData_add_layer__internal(CustomData *data,
1598 int type, int alloctype, void *layerdata, int totelem, const char *name)
1600 const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1601 int size = typeInfo->size * totelem, flag = 0, index = data->totlayer;
1602 void *newlayerdata = NULL;
1604 /* Passing a layerdata to copy from with an alloctype that won't copy is
1605 most likely a bug */
1606 BLI_assert(!layerdata ||
1607 (alloctype == CD_ASSIGN) ||
1608 (alloctype == CD_DUPLICATE) ||
1609 (alloctype == CD_REFERENCE));
1611 if (!typeInfo->defaultname && CustomData_has_layer(data, type))
1612 return &data->layers[CustomData_get_layer_index(data, type)];
1614 if((alloctype == CD_ASSIGN) || (alloctype == CD_REFERENCE)) {
1615 newlayerdata = layerdata;
1617 else if (size > 0) {
1618 newlayerdata = MEM_callocN(size, layerType_getName(type));
1623 if (alloctype == CD_DUPLICATE && layerdata) {
1625 typeInfo->copy(layerdata, newlayerdata, totelem);
1627 memcpy(newlayerdata, layerdata, size);
1629 else if (alloctype == CD_DEFAULT) {
1630 if(typeInfo->set_default)
1631 typeInfo->set_default((char*)newlayerdata, totelem);
1633 else if (alloctype == CD_REFERENCE)
1634 flag |= CD_FLAG_NOFREE;
1636 if(index >= data->maxlayer) {
1637 if(!customData_resize(data, CUSTOMDATA_GROW)) {
1638 if(newlayerdata != layerdata)
1639 MEM_freeN(newlayerdata);
1646 /* keep layers ordered by type */
1647 for( ; index > 0 && data->layers[index - 1].type > type; --index)
1648 data->layers[index] = data->layers[index - 1];
1650 data->layers[index].type = type;
1651 data->layers[index].flag = flag;
1652 data->layers[index].data = newlayerdata;
1654 if(name || (name=typeInfo->defaultname)) {
1655 BLI_strncpy(data->layers[index].name, name, sizeof(data->layers[index].name));
1656 CustomData_set_layer_unique_name(data, index);
1659 data->layers[index].name[0] = '\0';
1661 if(index > 0 && data->layers[index-1].type == type) {
1662 data->layers[index].active = data->layers[index-1].active;
1663 data->layers[index].active_rnd = data->layers[index-1].active_rnd;
1664 data->layers[index].active_clone = data->layers[index-1].active_clone;
1665 data->layers[index].active_mask = data->layers[index-1].active_mask;
1667 data->layers[index].active = 0;
1668 data->layers[index].active_rnd = 0;
1669 data->layers[index].active_clone = 0;
1670 data->layers[index].active_mask = 0;
1673 customData_update_offsets(data);
1675 return &data->layers[index];
1678 void *CustomData_add_layer(CustomData *data, int type, int alloctype,
1679 void *layerdata, int totelem)
1681 CustomDataLayer *layer;
1682 const LayerTypeInfo *typeInfo= layerType_getInfo(type);
1684 layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1685 totelem, typeInfo->defaultname);
1686 CustomData_update_typemap(data);
1694 /*same as above but accepts a name*/
1695 void *CustomData_add_layer_named(CustomData *data, int type, int alloctype,
1696 void *layerdata, int totelem, const char *name)
1698 CustomDataLayer *layer;
1700 layer = customData_add_layer__internal(data, type, alloctype, layerdata,
1702 CustomData_update_typemap(data);
1711 int CustomData_free_layer(CustomData *data, int type, int totelem, int index)
1715 if (index < 0) return 0;
1717 customData_free_layer__internal(&data->layers[index], totelem);
1719 for (i=index+1; i < data->totlayer; ++i)
1720 data->layers[i-1] = data->layers[i];
1724 /* if layer was last of type in array, set new active layer */
1725 if ((index >= data->totlayer) || (data->layers[index].type != type)) {
1726 i = CustomData_get_layer_index(data, type);
1729 for (; i < data->totlayer && data->layers[i].type == type; i++) {
1730 data->layers[i].active--;
1731 data->layers[i].active_rnd--;
1732 data->layers[i].active_clone--;
1733 data->layers[i].active_mask--;
1737 if (data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1738 customData_resize(data, -CUSTOMDATA_GROW);
1740 customData_update_offsets(data);
1741 CustomData_update_typemap(data);
1746 int CustomData_free_layer_active(CustomData *data, int type, int totelem)
1749 index = CustomData_get_active_layer_index(data, type);
1750 if (index < 0) return 0;
1751 return CustomData_free_layer(data, type, totelem, index);
1755 void CustomData_free_layers(CustomData *data, int type, int totelem)
1757 while (CustomData_has_layer(data, type))
1758 CustomData_free_layer_active(data, type, totelem);
1761 int CustomData_has_layer(const CustomData *data, int type)
1763 return (CustomData_get_layer_index(data, type) != -1);
1766 int CustomData_number_of_layers(const CustomData *data, int type)
1770 for(i = 0; i < data->totlayer; i++)
1771 if(data->layers[i].type == type)
1777 void *CustomData_duplicate_referenced_layer(struct CustomData *data, const int type, const int totelem)
1779 CustomDataLayer *layer;
1782 /* get the layer index of the first layer of type */
1783 layer_index = CustomData_get_active_layer_index(data, type);
1784 if(layer_index < 0) return NULL;
1786 layer = &data->layers[layer_index];
1788 if (layer->flag & CD_FLAG_NOFREE) {
1789 /* MEM_dupallocN won’t work in case of complex layers, like e.g.
1790 * CD_MDEFORMVERT, which has pointers to allocated data...
1791 * So in case a custom copy function is defined, use it!
1793 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1795 if(typeInfo->copy) {
1796 char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1797 typeInfo->copy(layer->data, dest_data, totelem);
1798 layer->data = dest_data;
1801 layer->data = MEM_dupallocN(layer->data);
1803 layer->flag &= ~CD_FLAG_NOFREE;
1809 void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
1810 const int type, const char *name, const int totelem)
1812 CustomDataLayer *layer;
1815 /* get the layer index of the desired layer */
1816 layer_index = CustomData_get_named_layer_index(data, type, name);
1817 if(layer_index < 0) return NULL;
1819 layer = &data->layers[layer_index];
1821 if (layer->flag & CD_FLAG_NOFREE) {
1822 /* MEM_dupallocN won’t work in case of complex layers, like e.g.
1823 * CD_MDEFORMVERT, which has pointers to allocated data...
1824 * So in case a custom copy function is defined, use it!
1826 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
1828 if(typeInfo->copy) {
1829 char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
1830 typeInfo->copy(layer->data, dest_data, totelem);
1831 layer->data = dest_data;
1834 layer->data = MEM_dupallocN(layer->data);
1836 layer->flag &= ~CD_FLAG_NOFREE;
1842 int CustomData_is_referenced_layer(struct CustomData *data, int type)
1844 CustomDataLayer *layer;
1847 /* get the layer index of the first layer of type */
1848 layer_index = CustomData_get_active_layer_index(data, type);
1849 if(layer_index < 0) return 0;
1851 layer = &data->layers[layer_index];
1853 return (layer->flag & CD_FLAG_NOFREE) != 0;
1856 void CustomData_free_temporary(CustomData *data, int totelem)
1858 CustomDataLayer *layer;
1861 for(i = 0, j = 0; i < data->totlayer; ++i) {
1862 layer = &data->layers[i];
1865 data->layers[j] = data->layers[i];
1867 if ((layer->flag & CD_FLAG_TEMPORARY) == CD_FLAG_TEMPORARY)
1868 customData_free_layer__internal(layer, totelem);
1875 if(data->totlayer <= data->maxlayer-CUSTOMDATA_GROW)
1876 customData_resize(data, -CUSTOMDATA_GROW);
1878 customData_update_offsets(data);
1881 void CustomData_set_only_copy(const struct CustomData *data,
1882 CustomDataMask mask)
1886 for(i = 0; i < data->totlayer; ++i)
1887 if(!(mask & CD_TYPE_AS_MASK(data->layers[i].type)))
1888 data->layers[i].flag |= CD_FLAG_NOCOPY;
1891 void CustomData_copy_elements(int type, void *source, void *dest, int count)
1893 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
1896 typeInfo->copy(source, dest, count);
1898 memcpy(dest, source, typeInfo->size*count);
1901 void CustomData_copy_data(const CustomData *source, CustomData *dest,
1902 int source_index, int dest_index, int count)
1904 const LayerTypeInfo *typeInfo;
1909 /* copies a layer at a time */
1911 for(src_i = 0; src_i < source->totlayer; ++src_i) {
1913 /* find the first dest layer with type >= the source type
1914 * (this should work because layers are ordered by type)
1916 while(dest_i < dest->totlayer
1917 && dest->layers[dest_i].type < source->layers[src_i].type)
1920 /* if there are no more dest layers, we're done */
1921 if(dest_i >= dest->totlayer) return;
1923 /* if we found a matching layer, copy the data */
1924 if(dest->layers[dest_i].type == source->layers[src_i].type) {
1925 char *src_data = source->layers[src_i].data;
1926 char *dest_data = dest->layers[dest_i].data;
1928 typeInfo = layerType_getInfo(source->layers[src_i].type);
1930 src_offset = source_index * typeInfo->size;
1931 dest_offset = dest_index * typeInfo->size;
1933 if (!src_data || !dest_data) {
1934 printf("%s: warning null data for %s type (%p --> %p), skipping\n",
1935 __func__, layerType_getName(source->layers[src_i].type),
1936 (void *)src_data, (void *)dest_data);
1941 typeInfo->copy(src_data + src_offset,
1942 dest_data + dest_offset,
1945 memcpy(dest_data + dest_offset,
1946 src_data + src_offset,
1947 count * typeInfo->size);
1949 /* if there are multiple source & dest layers of the same type,
1950 * we don't want to copy all source layers to the same dest, so
1958 void CustomData_free_elem(CustomData *data, int index, int count)
1961 const LayerTypeInfo *typeInfo;
1963 for(i = 0; i < data->totlayer; ++i) {
1964 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
1965 typeInfo = layerType_getInfo(data->layers[i].type);
1967 if(typeInfo->free) {
1968 int offset = typeInfo->size * index;
1970 typeInfo->free((char *)data->layers[i].data + offset,
1971 count, typeInfo->size);
1977 #define SOURCE_BUF_SIZE 100
1979 void CustomData_interp(const CustomData *source, CustomData *dest,
1980 int *src_indices, float *weights, float *sub_weights,
1981 int count, int dest_index)
1986 void *source_buf[SOURCE_BUF_SIZE];
1987 void **sources = source_buf;
1989 /* slow fallback in case we're interpolating a ridiculous number of
1992 if(count > SOURCE_BUF_SIZE)
1993 sources = MEM_callocN(sizeof(*sources) * count,
1994 "CustomData_interp sources");
1996 /* interpolates a layer at a time */
1998 for(src_i = 0; src_i < source->totlayer; ++src_i) {
1999 const LayerTypeInfo *typeInfo= layerType_getInfo(source->layers[src_i].type);
2000 if(!typeInfo->interp) continue;
2002 /* find the first dest layer with type >= the source type
2003 * (this should work because layers are ordered by type)
2005 while(dest_i < dest->totlayer
2006 && dest->layers[dest_i].type < source->layers[src_i].type)
2009 /* if there are no more dest layers, we're done */
2010 if(dest_i >= dest->totlayer) return;
2012 /* if we found a matching layer, copy the data */
2013 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2014 void *src_data = source->layers[src_i].data;
2016 for(j = 0; j < count; ++j)
2017 sources[j] = (char *)src_data
2018 + typeInfo->size * src_indices[j];
2020 dest_offset = dest_index * typeInfo->size;
2022 typeInfo->interp(sources, weights, sub_weights, count,
2023 (char *)dest->layers[dest_i].data + dest_offset);
2025 /* if there are multiple source & dest layers of the same type,
2026 * we don't want to copy all source layers to the same dest, so
2033 if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2036 void CustomData_swap(struct CustomData *data, int index, const int *corner_indices)
2038 const LayerTypeInfo *typeInfo;
2041 for(i = 0; i < data->totlayer; ++i) {
2042 typeInfo = layerType_getInfo(data->layers[i].type);
2044 if(typeInfo->swap) {
2045 int offset = typeInfo->size * index;
2047 typeInfo->swap((char *)data->layers[i].data + offset, corner_indices);
2052 void *CustomData_get(const CustomData *data, int index, int type)
2057 /* get the layer index of the active layer of type */
2058 layer_index = CustomData_get_active_layer_index(data, type);
2059 if(layer_index < 0) return NULL;
2061 /* get the offset of the desired element */
2062 offset = layerType_getInfo(type)->size * index;
2064 return (char *)data->layers[layer_index].data + offset;
2067 void *CustomData_get_n(const CustomData *data, int type, int index, int n)
2072 /* get the layer index of the first layer of type */
2073 layer_index = data->typemap[type];
2074 if(layer_index < 0) return NULL;
2076 offset = layerType_getInfo(type)->size * index;
2077 return (char *)data->layers[layer_index+n].data + offset;
2080 void *CustomData_get_layer(const CustomData *data, int type)
2082 /* get the layer index of the active layer of type */
2083 int layer_index = CustomData_get_active_layer_index(data, type);
2084 if(layer_index < 0) return NULL;
2086 return data->layers[layer_index].data;
2089 void *CustomData_get_layer_n(const CustomData *data, int type, int n)
2091 /* get the layer index of the active layer of type */
2092 int layer_index = CustomData_get_layer_index_n(data, type, n);
2093 if(layer_index < 0) return NULL;
2095 return data->layers[layer_index].data;
2098 void *CustomData_get_layer_named(const struct CustomData *data, int type,
2101 int layer_index = CustomData_get_named_layer_index(data, type, name);
2102 if(layer_index < 0) return NULL;
2104 return data->layers[layer_index].data;
2108 int CustomData_set_layer_name(const CustomData *data, int type, int n, const char *name)
2110 /* get the layer index of the first layer of type */
2111 int layer_index = CustomData_get_layer_index_n(data, type, n);
2113 if(layer_index < 0) return 0;
2114 if (!name) return 0;
2116 strcpy(data->layers[layer_index].name, name);
2121 void *CustomData_set_layer(const CustomData *data, int type, void *ptr)
2123 /* get the layer index of the first layer of type */
2124 int layer_index = CustomData_get_active_layer_index(data, type);
2126 if(layer_index < 0) return NULL;
2128 data->layers[layer_index].data = ptr;
2133 void *CustomData_set_layer_n(const struct CustomData *data, int type, int n, void *ptr)
2135 /* get the layer index of the first layer of type */
2136 int layer_index = CustomData_get_layer_index_n(data, type, n);
2137 if(layer_index < 0) return NULL;
2139 data->layers[layer_index].data = ptr;
2144 void CustomData_set(const CustomData *data, int index, int type, void *source)
2146 void *dest = CustomData_get(data, index, type);
2147 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2152 typeInfo->copy(source, dest, 1);
2154 memcpy(dest, source, typeInfo->size);
2157 /* EditMesh functions */
2159 void CustomData_em_free_block(CustomData *data, void **block)
2161 const LayerTypeInfo *typeInfo;
2166 for(i = 0; i < data->totlayer; ++i) {
2167 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
2168 typeInfo = layerType_getInfo(data->layers[i].type);
2170 if(typeInfo->free) {
2171 int offset = data->layers[i].offset;
2172 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
2181 static void CustomData_em_alloc_block(CustomData *data, void **block)
2183 /* TODO: optimize free/alloc */
2186 CustomData_em_free_block(data, block);
2188 if (data->totsize > 0)
2189 *block = MEM_callocN(data->totsize, "CustomData EM block");
2194 void CustomData_em_copy_data(const CustomData *source, CustomData *dest,
2195 void *src_block, void **dest_block)
2197 const LayerTypeInfo *typeInfo;
2201 CustomData_em_alloc_block(dest, dest_block);
2203 /* copies a layer at a time */
2205 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2207 /* find the first dest layer with type >= the source type
2208 * (this should work because layers are ordered by type)
2210 while(dest_i < dest->totlayer
2211 && dest->layers[dest_i].type < source->layers[src_i].type)
2214 /* if there are no more dest layers, we're done */
2215 if(dest_i >= dest->totlayer) return;
2217 /* if we found a matching layer, copy the data */
2218 if(dest->layers[dest_i].type == source->layers[src_i].type &&
2219 strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
2220 char *src_data = (char*)src_block + source->layers[src_i].offset;
2221 char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
2223 typeInfo = layerType_getInfo(source->layers[src_i].type);
2226 typeInfo->copy(src_data, dest_data, 1);
2228 memcpy(dest_data, src_data, typeInfo->size);
2230 /* if there are multiple source & dest layers of the same type,
2231 * we don't want to copy all source layers to the same dest, so
2239 void CustomData_em_validate_data(CustomData *data, void *block, int sub_elements)
2242 for(i = 0; i < data->totlayer; i++) {
2243 const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[i].type);
2244 char *leayer_data = (char*)block + data->layers[i].offset;
2246 if(typeInfo->validate)
2247 typeInfo->validate(leayer_data, sub_elements);
2251 void *CustomData_em_get(const CustomData *data, void *block, int type)
2255 /* get the layer index of the first layer of type */
2256 layer_index = CustomData_get_active_layer_index(data, type);
2257 if(layer_index < 0) return NULL;
2259 return (char *)block + data->layers[layer_index].offset;
2262 void *CustomData_em_get_n(const CustomData *data, void *block, int type, int n)
2266 /* get the layer index of the first layer of type */
2267 layer_index = CustomData_get_layer_index_n(data, type, n);
2268 if(layer_index < 0) return NULL;
2270 return (char *)block + data->layers[layer_index].offset;
2273 void CustomData_em_set(CustomData *data, void *block, int type, void *source)
2275 void *dest = CustomData_em_get(data, block, type);
2276 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2281 typeInfo->copy(source, dest, 1);
2283 memcpy(dest, source, typeInfo->size);
2286 void CustomData_em_set_n(CustomData *data, void *block, int type, int n, void *source)
2288 void *dest = CustomData_em_get_n(data, block, type, n);
2289 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2294 typeInfo->copy(source, dest, 1);
2296 memcpy(dest, source, typeInfo->size);
2299 void CustomData_em_interp(CustomData *data, void **src_blocks, float *weights,
2300 float *sub_weights, int count, void *dest_block)
2303 void *source_buf[SOURCE_BUF_SIZE];
2304 void **sources = source_buf;
2306 /* slow fallback in case we're interpolating a ridiculous number of
2309 if(count > SOURCE_BUF_SIZE)
2310 sources = MEM_callocN(sizeof(*sources) * count,
2311 "CustomData_interp sources");
2313 /* interpolates a layer at a time */
2314 for(i = 0; i < data->totlayer; ++i) {
2315 CustomDataLayer *layer = &data->layers[i];
2316 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
2318 if(typeInfo->interp) {
2319 for(j = 0; j < count; ++j)
2320 sources[j] = (char *)src_blocks[j] + layer->offset;
2322 typeInfo->interp(sources, weights, sub_weights, count,
2323 (char *)dest_block + layer->offset);
2327 if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2330 void CustomData_em_set_default(CustomData *data, void **block)
2332 const LayerTypeInfo *typeInfo;
2336 CustomData_em_alloc_block(data, block);
2338 for(i = 0; i < data->totlayer; ++i) {
2339 int offset = data->layers[i].offset;
2341 typeInfo = layerType_getInfo(data->layers[i].type);
2343 if(typeInfo->set_default)
2344 typeInfo->set_default((char*)*block + offset, 1);
2348 void CustomData_to_em_block(const CustomData *source, CustomData *dest,
2349 int src_index, void **dest_block)
2351 const LayerTypeInfo *typeInfo;
2352 int dest_i, src_i, src_offset;
2355 CustomData_em_alloc_block(dest, dest_block);
2357 /* copies a layer at a time */
2359 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2361 /* find the first dest layer with type >= the source type
2362 * (this should work because layers are ordered by type)
2364 while(dest_i < dest->totlayer
2365 && dest->layers[dest_i].type < source->layers[src_i].type)
2368 /* if there are no more dest layers, we're done */
2369 if(dest_i >= dest->totlayer) return;
2371 /* if we found a matching layer, copy the data */
2372 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2373 int offset = dest->layers[dest_i].offset;
2374 char *src_data = source->layers[src_i].data;
2375 char *dest_data = (char*)*dest_block + offset;
2377 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2378 src_offset = src_index * typeInfo->size;
2381 typeInfo->copy(src_data + src_offset, dest_data, 1);
2383 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2385 /* if there are multiple source & dest layers of the same type,
2386 * we don't want to copy all source layers to the same dest, so
2394 void CustomData_from_em_block(const CustomData *source, CustomData *dest,
2395 void *src_block, int dest_index)
2397 const LayerTypeInfo *typeInfo;
2398 int dest_i, src_i, dest_offset;
2400 /* copies a layer at a time */
2402 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2404 /* find the first dest layer with type >= the source type
2405 * (this should work because layers are ordered by type)
2407 while(dest_i < dest->totlayer
2408 && dest->layers[dest_i].type < source->layers[src_i].type)
2411 /* if there are no more dest layers, we're done */
2412 if(dest_i >= dest->totlayer) return;
2414 /* if we found a matching layer, copy the data */
2415 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2416 int offset = source->layers[src_i].offset;
2417 char *src_data = (char*)src_block + offset;
2418 char *dest_data = dest->layers[dest_i].data;
2420 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2421 dest_offset = dest_index * typeInfo->size;
2424 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2426 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2428 /* if there are multiple source & dest layers of the same type,
2429 * we don't want to copy all source layers to the same dest, so
2439 /*needed to convert to/from different face reps*/
2440 void CustomData_to_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata,
2441 int totloop, int totpoly)
2444 for(i=0; i < fdata->totlayer; i++) {
2445 if(fdata->layers[i].type == CD_MTFACE) {
2446 CustomData_add_layer_named(pdata, CD_MTEXPOLY, CD_CALLOC, NULL, totpoly, fdata->layers[i].name);
2447 CustomData_add_layer_named(ldata, CD_MLOOPUV, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2449 else if (fdata->layers[i].type == CD_MCOL) {
2450 CustomData_add_layer_named(ldata, CD_MLOOPCOL, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2452 else if (fdata->layers[i].type == CD_MDISPS) {
2453 CustomData_add_layer_named(ldata, CD_MDISPS, CD_CALLOC, NULL, totloop, fdata->layers[i].name);
2458 void CustomData_from_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata, int total)
2461 for(i=0; i < pdata->totlayer; i++) {
2462 if (pdata->layers[i].type == CD_MTEXPOLY) {
2463 CustomData_add_layer_named(fdata, CD_MTFACE, CD_CALLOC, NULL, total, pdata->layers[i].name);
2466 for(i=0; i < ldata->totlayer; i++) {
2467 if (ldata->layers[i].type == CD_MLOOPCOL) {
2468 CustomData_add_layer_named(fdata, CD_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2470 else if (ldata->layers[i].type == CD_WEIGHT_MLOOPCOL) {
2471 CustomData_add_layer_named(fdata, CD_WEIGHT_MCOL, CD_CALLOC, NULL, total, ldata->layers[i].name);
2473 else if (ldata->layers[i].type == CD_ORIGSPACE_MLOOP) {
2474 CustomData_add_layer_named(fdata, CD_ORIGSPACE, CD_CALLOC, NULL, total, ldata->layers[i].name);
2479 void CustomData_bmesh_update_active_layers(CustomData *fdata, CustomData *pdata, CustomData *ldata)
2483 if (CustomData_has_layer(pdata, CD_MTEXPOLY)) {
2484 act = CustomData_get_active_layer(pdata, CD_MTEXPOLY);
2485 CustomData_set_layer_active(ldata, CD_MLOOPUV, act);
2486 CustomData_set_layer_active(fdata, CD_MTFACE, act);
2488 act = CustomData_get_render_layer(pdata, CD_MTEXPOLY);
2489 CustomData_set_layer_render(ldata, CD_MLOOPUV, act);
2490 CustomData_set_layer_render(fdata, CD_MTFACE, act);
2492 act = CustomData_get_clone_layer(pdata, CD_MTEXPOLY);
2493 CustomData_set_layer_clone(ldata, CD_MLOOPUV, act);
2494 CustomData_set_layer_clone(fdata, CD_MTFACE, act);
2496 act = CustomData_get_stencil_layer(pdata, CD_MTEXPOLY);
2497 CustomData_set_layer_stencil(ldata, CD_MLOOPUV, act);
2498 CustomData_set_layer_stencil(fdata, CD_MTFACE, act);
2501 if (CustomData_has_layer(ldata, CD_MLOOPCOL)) {
2502 act = CustomData_get_active_layer(ldata, CD_MLOOPCOL);
2503 CustomData_set_layer_active(fdata, CD_MCOL, act);
2505 act = CustomData_get_render_layer(ldata, CD_MLOOPCOL);
2506 CustomData_set_layer_render(fdata, CD_MCOL, act);
2508 act = CustomData_get_clone_layer(ldata, CD_MLOOPCOL);
2509 CustomData_set_layer_clone(fdata, CD_MCOL, act);
2511 act = CustomData_get_stencil_layer(ldata, CD_MLOOPCOL);
2512 CustomData_set_layer_stencil(fdata, CD_MCOL, act);
2516 void CustomData_bmesh_init_pool(CustomData *data, int allocsize)
2518 /* Dispose old pools before calling here to avoid leaks */
2519 BLI_assert(data->pool == NULL);
2521 /* If there are no layers, no pool is needed just yet */
2522 if (data->totlayer) {
2523 data->pool = BLI_mempool_create(data->totsize, allocsize, allocsize, TRUE, FALSE);
2527 void CustomData_bmesh_merge(CustomData *source, CustomData *dest,
2528 int mask, int alloctype, BMesh *bm, int type)
2532 CustomData destold = *dest;
2536 CustomData_merge(source, dest, mask, alloctype, 0);
2537 CustomData_bmesh_init_pool(dest, 512);
2541 t = BM_VERTS_OF_MESH; break;
2543 t = BM_EDGES_OF_MESH; break;
2545 t = BM_LOOPS_OF_FACE; break;
2547 t = BM_FACES_OF_MESH; break;
2548 default: /* should never happen */
2549 BLI_assert(!"invalid type given");
2550 t = BM_VERTS_OF_MESH;
2553 if (t != BM_LOOPS_OF_FACE) {
2554 /*ensure all current elements follow new customdata layout*/
2555 BM_ITER(h, &iter, bm, t, NULL) {
2556 CustomData_bmesh_copy_data(&destold, dest, h->data, &tmp);
2557 CustomData_bmesh_free_block(&destold, &h->data);
2565 /*ensure all current elements follow new customdata layout*/
2566 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
2567 BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
2568 CustomData_bmesh_copy_data(&destold, dest, l->head.data, &tmp);
2569 CustomData_bmesh_free_block(&destold, &l->head.data);
2575 if (destold.pool) BLI_mempool_destroy(destold.pool);
2578 void CustomData_bmesh_free_block(CustomData *data, void **block)
2580 const LayerTypeInfo *typeInfo;
2584 for(i = 0; i < data->totlayer; ++i) {
2585 if(!(data->layers[i].flag & CD_FLAG_NOFREE)) {
2586 typeInfo = layerType_getInfo(data->layers[i].type);
2588 if(typeInfo->free) {
2589 int offset = data->layers[i].offset;
2590 typeInfo->free((char*)*block + offset, 1, typeInfo->size);
2596 BLI_mempool_free(data->pool, *block);
2601 static void CustomData_bmesh_alloc_block(CustomData *data, void **block)
2605 CustomData_bmesh_free_block(data, block);
2607 if (data->totsize > 0)
2608 *block = BLI_mempool_alloc(data->pool);
2613 void CustomData_bmesh_copy_data(const CustomData *source, CustomData *dest,
2614 void *src_block, void **dest_block)
2616 const LayerTypeInfo *typeInfo;
2620 CustomData_bmesh_alloc_block(dest, dest_block);
2622 /* copies a layer at a time */
2624 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2626 /* find the first dest layer with type >= the source type
2627 * (this should work because layers are ordered by type)
2629 while(dest_i < dest->totlayer
2630 && dest->layers[dest_i].type < source->layers[src_i].type)
2633 /* if there are no more dest layers, we're done */
2634 if(dest_i >= dest->totlayer) return;
2636 /* if we found a matching layer, copy the data */
2637 if(dest->layers[dest_i].type == source->layers[src_i].type &&
2638 strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
2639 char *src_data = (char*)src_block + source->layers[src_i].offset;
2640 char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
2642 typeInfo = layerType_getInfo(source->layers[src_i].type);
2645 typeInfo->copy(src_data, dest_data, 1);
2647 memcpy(dest_data, src_data, typeInfo->size);
2649 /* if there are multiple source & dest layers of the same type,
2650 * we don't want to copy all source layers to the same dest, so
2658 /*Bmesh Custom Data Functions. Should replace editmesh ones with these as well, due to more effecient memory alloc*/
2659 void *CustomData_bmesh_get(const CustomData *data, void *block, int type)
2663 /* get the layer index of the first layer of type */
2664 layer_index = CustomData_get_active_layer_index(data, type);
2665 if(layer_index < 0) return NULL;
2667 return (char *)block + data->layers[layer_index].offset;
2670 void *CustomData_bmesh_get_n(const CustomData *data, void *block, int type, int n)
2674 /* get the layer index of the first layer of type */
2675 layer_index = CustomData_get_layer_index(data, type);
2676 if(layer_index < 0) return NULL;
2678 return (char *)block + data->layers[layer_index+n].offset;
2681 /*gets from the layer at physical index n, note: doesn't check type.*/
2682 void *CustomData_bmesh_get_layer_n(const CustomData *data, void *block, int n)
2684 if(n < 0 || n >= data->totlayer) return NULL;
2686 return (char *)block + data->layers[n].offset;
2689 int CustomData_layer_has_math(struct CustomData *data, int layern)
2691 const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[layern].type);
2693 if (typeInfo->equal && typeInfo->add && typeInfo->multiply &&
2694 typeInfo->initminmax && typeInfo->dominmax) return 1;
2699 /*copies the "value" (e.g. mloopuv uv or mloopcol colors) from one block to
2700 another, while not overwriting anything else (e.g. flags)*/
2701 void CustomData_data_copy_value(int type, void *source, void *dest)
2703 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2707 if(typeInfo->copyvalue)
2708 typeInfo->copyvalue(source, dest);
2710 memcpy(dest, source, typeInfo->size);
2713 int CustomData_data_equals(int type, void *data1, void *data2)
2715 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2717 if (typeInfo->equal)
2718 return typeInfo->equal(data1, data2);
2719 else return !memcmp(data1, data2, typeInfo->size);
2722 void CustomData_data_initminmax(int type, void *min, void *max)
2724 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2726 if (typeInfo->initminmax)
2727 typeInfo->initminmax(min, max);
2731 void CustomData_data_dominmax(int type, void *data, void *min, void *max)
2733 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2735 if (typeInfo->dominmax)
2736 typeInfo->dominmax(data, min, max);
2740 void CustomData_data_multiply(int type, void *data, float fac)
2742 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2744 if (typeInfo->multiply)
2745 typeInfo->multiply(data, fac);
2749 void CustomData_data_add(int type, void *data1, void *data2)
2751 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2754 typeInfo->add(data1, data2);
2757 void CustomData_bmesh_set(const CustomData *data, void *block, int type, void *source)
2759 void *dest = CustomData_bmesh_get(data, block, type);
2760 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2765 typeInfo->copy(source, dest, 1);
2767 memcpy(dest, source, typeInfo->size);
2770 void CustomData_bmesh_set_n(CustomData *data, void *block, int type, int n, void *source)
2772 void *dest = CustomData_bmesh_get_n(data, block, type, n);
2773 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2778 typeInfo->copy(source, dest, 1);
2780 memcpy(dest, source, typeInfo->size);
2783 void CustomData_bmesh_set_layer_n(CustomData *data, void *block, int n, void *source)
2785 void *dest = CustomData_bmesh_get_layer_n(data, block, n);
2786 const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[n].type);
2791 typeInfo->copy(source, dest, 1);
2793 memcpy(dest, source, typeInfo->size);
2796 void CustomData_bmesh_interp(CustomData *data, void **src_blocks, float *weights,
2797 float *sub_weights, int count, void *dest_block)
2800 void *source_buf[SOURCE_BUF_SIZE];
2801 void **sources = source_buf;
2803 /* slow fallback in case we're interpolating a ridiculous number of
2806 if(count > SOURCE_BUF_SIZE)
2807 sources = MEM_callocN(sizeof(*sources) * count,
2808 "CustomData_interp sources");
2810 /* interpolates a layer at a time */
2811 for(i = 0; i < data->totlayer; ++i) {
2812 CustomDataLayer *layer = &data->layers[i];
2813 const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
2814 if(typeInfo->interp) {
2815 for(j = 0; j < count; ++j)
2816 sources[j] = (char *)src_blocks[j] + layer->offset;
2818 typeInfo->interp(sources, weights, sub_weights, count,
2819 (char *)dest_block + layer->offset);
2823 if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
2826 void CustomData_bmesh_set_default(CustomData *data, void **block)
2828 const LayerTypeInfo *typeInfo;
2832 CustomData_bmesh_alloc_block(data, block);
2834 for(i = 0; i < data->totlayer; ++i) {
2835 int offset = data->layers[i].offset;
2837 typeInfo = layerType_getInfo(data->layers[i].type);
2839 if(typeInfo->set_default)
2840 typeInfo->set_default((char*)*block + offset, 1);
2841 else memset((char*)*block + offset, 0, typeInfo->size);
2845 void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest,
2846 int src_index, void **dest_block)
2848 const LayerTypeInfo *typeInfo;
2849 int dest_i, src_i, src_offset;
2852 CustomData_bmesh_alloc_block(dest, dest_block);
2854 /* copies a layer at a time */
2856 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2858 /* find the first dest layer with type >= the source type
2859 * (this should work because layers are ordered by type)
2861 while(dest_i < dest->totlayer
2862 && dest->layers[dest_i].type < source->layers[src_i].type)
2865 /* if there are no more dest layers, we're done */
2866 if(dest_i >= dest->totlayer) return;
2868 /* if we found a matching layer, copy the data */
2869 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2870 int offset = dest->layers[dest_i].offset;
2871 char *src_data = source->layers[src_i].data;
2872 char *dest_data = (char*)*dest_block + offset;
2874 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2875 src_offset = src_index * typeInfo->size;
2878 typeInfo->copy(src_data + src_offset, dest_data, 1);
2880 memcpy(dest_data, src_data + src_offset, typeInfo->size);
2882 /* if there are multiple source & dest layers of the same type,
2883 * we don't want to copy all source layers to the same dest, so
2891 void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest,
2892 void *src_block, int dest_index)
2894 const LayerTypeInfo *typeInfo;
2895 int dest_i, src_i, dest_offset;
2897 /* copies a layer at a time */
2899 for(src_i = 0; src_i < source->totlayer; ++src_i) {
2901 /* find the first dest layer with type >= the source type
2902 * (this should work because layers are ordered by type)
2904 while(dest_i < dest->totlayer
2905 && dest->layers[dest_i].type < source->layers[src_i].type)
2908 /* if there are no more dest layers, we're done */
2909 if(dest_i >= dest->totlayer) return;
2911 /* if we found a matching layer, copy the data */
2912 if(dest->layers[dest_i].type == source->layers[src_i].type) {
2913 int offset = source->layers[src_i].offset;
2914 char *src_data = (char*)src_block + offset;
2915 char *dest_data = dest->layers[dest_i].data;
2917 typeInfo = layerType_getInfo(dest->layers[dest_i].type);
2918 dest_offset = dest_index * typeInfo->size;
2921 typeInfo->copy(src_data, dest_data + dest_offset, 1);
2923 memcpy(dest_data + dest_offset, src_data, typeInfo->size);
2925 /* if there are multiple source & dest layers of the same type,
2926 * we don't want to copy all source layers to the same dest, so
2935 void CustomData_file_write_info(int type, const char **structname, int *structnum)
2937 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2939 *structname = typeInfo->structname;
2940 *structnum = typeInfo->structnum;
2943 int CustomData_sizeof(int type)
2945 const LayerTypeInfo *typeInfo = layerType_getInfo(type);
2947 return typeInfo->size;
2950 const char *CustomData_layertype_name(int type)
2952 return layerType_getName(type);
2955 static int CustomData_is_property_layer(int type)
2957 if((type == CD_PROP_FLT) || (type == CD_PROP_INT) || (type == CD_PROP_STR))
2962 static int cd_layer_find_dupe(CustomData *data, const char *name, int type, int index)
2965 /* see if there is a duplicate */
2966 for(i=0; i<data->totlayer; i++) {
2968 CustomDataLayer *layer= &data->layers[i];
2970 if(CustomData_is_property_layer(type)) {
2971 if(CustomData_is_property_layer(layer->type) && strcmp(layer->name, name)==0) {
2976 if(i!=index && layer->type==type && strcmp(layer->name, name)==0) {
2986 static int customdata_unique_check(void *arg, const char *name)
2988 struct {CustomData *data; int type; int index;} *data_arg= arg;
2989 return cd_layer_find_dupe(data_arg->data, name, data_arg->type, data_arg->index);
2992 void CustomData_set_layer_unique_name(CustomData *data, int index)
2994 CustomDataLayer *nlayer= &data->layers[index];
2995 const LayerTypeInfo *typeInfo= layerType_getInfo(nlayer->type);
2997 struct {CustomData *data; int type; int index;} data_arg;
2998 data_arg.data= data;
2999 data_arg.type= nlayer->type;
3000 data_arg.index= index;
3002 if (!typeInfo->defaultname)
3005 BLI_uniquename_cb(customdata_unique_check, &data_arg, typeInfo->defaultname, '.', nlayer->name, sizeof(nlayer->name));
3008 void CustomData_validate_layer_name(const CustomData *data, int type, char *name, char *outname)
3012 /* if a layer name was given, try to find that layer */
3014 index = CustomData_get_named_layer_index(data, type, name);
3017 /* either no layer was specified, or the layer we want has been
3018 * deleted, so assign the active layer to name
3020 index = CustomData_get_active_layer_index(data, type);
3021 strcpy(outname, data->layers[index].name);
3024 strcpy(outname, name);
3027 int CustomData_verify_versions(struct CustomData *data, int index)
3029 const LayerTypeInfo *typeInfo;
3030 CustomDataLayer *layer = &data->layers[index];
3031 int i, keeplayer = 1;
3033 if (layer->type >= CD_NUMTYPES) {
3034 keeplayer = 0; /* unknown layer type from future version */
3037 typeInfo = layerType_getInfo(layer->type);
3039 if (!typeInfo->defaultname && (index > 0) &&
3040 data->layers[index-1].type == layer->type)
3041 keeplayer = 0; /* multiple layers of which we only support one */
3045 for (i=index+1; i < data->totlayer; ++i)
3046 data->layers[i-1] = data->layers[i];
3053 /****************************** External Files *******************************/
3055 static void customdata_external_filename(char filename[FILE_MAX], ID *id, CustomDataExternal *external)
3057 BLI_strncpy(filename, external->filename, FILE_MAX);
3058 BLI_path_abs(filename, ID_BLEND_PATH(G.main, id));
3061 void CustomData_external_reload(CustomData *data, ID *UNUSED(id), CustomDataMask mask, int totelem)
3063 CustomDataLayer *layer;
3064 const LayerTypeInfo *typeInfo;
3067 for(i=0; i<data->totlayer; i++) {
3068 layer = &data->layers[i];
3069 typeInfo = layerType_getInfo(layer->type);
3071 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3072 else if((layer->flag & CD_FLAG_EXTERNAL) && (layer->flag & CD_FLAG_IN_MEMORY)) {
3074 typeInfo->free(layer->data, totelem, typeInfo->size);
3075 layer->flag &= ~CD_FLAG_IN_MEMORY;
3080 void CustomData_external_read(CustomData *data, ID *id, CustomDataMask mask, int totelem)
3082 CustomDataExternal *external= data->external;
3083 CustomDataLayer *layer;
3085 CDataFileLayer *blay;
3086 char filename[FILE_MAX];
3087 const LayerTypeInfo *typeInfo;
3093 for(i=0; i<data->totlayer; i++) {
3094 layer = &data->layers[i];
3095 typeInfo = layerType_getInfo(layer->type);
3097 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3098 else if(layer->flag & CD_FLAG_IN_MEMORY);
3099 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read)
3106 customdata_external_filename(filename, id, external);
3108 cdf= cdf_create(CDF_TYPE_MESH);
3109 if(!cdf_read_open(cdf, filename)) {
3110 fprintf(stderr, "Failed to read %s layer from %s.\n", layerType_getName(layer->type), filename);
3114 for(i=0; i<data->totlayer; i++) {
3115 layer = &data->layers[i];
3116 typeInfo = layerType_getInfo(layer->type);
3118 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3119 else if(layer->flag & CD_FLAG_IN_MEMORY);
3120 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->read) {
3121 blay= cdf_layer_find(cdf, layer->type, layer->name);
3124 if(cdf_read_layer(cdf, blay)) {
3125 if(typeInfo->read(cdf, layer->data, totelem));
3127 layer->flag |= CD_FLAG_IN_MEMORY;
3135 cdf_read_close(cdf);
3139 void CustomData_external_write(CustomData *data, ID *id, CustomDataMask mask, int totelem, int free)
3141 CustomDataExternal *external= data->external;
3142 CustomDataLayer *layer;
3144 CDataFileLayer *blay;
3145 const LayerTypeInfo *typeInfo;
3147 char filename[FILE_MAX];
3152 /* test if there is anything to write */
3153 for(i=0; i<data->totlayer; i++) {
3154 layer = &data->layers[i];
3155 typeInfo = layerType_getInfo(layer->type);
3157 if(!(mask & CD_TYPE_AS_MASK(layer->type)));
3158 else if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write)
3165 /* make sure data is read before we try to write */
3166 CustomData_external_read(data, id, mask, totelem);
3167 customdata_external_filename(filename, id, external);
3169 cdf= cdf_create(CDF_TYPE_MESH);
3171 for(i=0; i<data->totlayer; i++) {
3172 layer = &data->layers[i];
3173 typeInfo = layerType_getInfo(layer->type);
3175 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->filesize) {
3176 if(layer->flag & CD_FLAG_IN_MEMORY) {
3177 cdf_layer_add(cdf, layer->type, layer->name,
3178 typeInfo->filesize(cdf, layer->data, totelem));
3182 return; /* read failed for a layer! */
3187 if(!cdf_write_open(cdf, filename)) {
3188 fprintf(stderr, "Failed to open %s for writing.\n", filename);
3192 for(i=0; i<data->totlayer; i++) {
3193 layer = &data->layers[i];
3194 typeInfo = layerType_getInfo(layer->type);
3196 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
3197 blay= cdf_layer_find(cdf, layer->type, layer->name);
3199 if(cdf_write_layer(cdf, blay)) {
3200 if(typeInfo->write(cdf, layer->data, totelem));
3208 if(i != data->totlayer) {
3209 fprintf(stderr, "Failed to write data to %s.\n", filename);
3214 for(i=0; i<data->totlayer; i++) {
3215 layer = &data->layers[i];
3216 typeInfo = layerType_getInfo(layer->type);
3218 if((layer->flag & CD_FLAG_EXTERNAL) && typeInfo->write) {
3221 typeInfo->free(layer->data, totelem, typeInfo->size);
3222 layer->flag &= ~CD_FLAG_IN_MEMORY;
3227 cdf_write_close(cdf);
3231 void CustomData_external_add(CustomData *data, ID *UNUSED(id), int type, int UNUSED(totelem), const char *filename)
3233 CustomDataExternal *external= data->external;
3234 CustomDataLayer *layer;
3237 layer_index = CustomData_get_active_layer_index(data, type);
3238 if(layer_index < 0) return;
3240 layer = &data->layers[layer_index];
3242 if(layer->flag & CD_FLAG_EXTERNAL)
3246 external= MEM_callocN(sizeof(CustomDataExternal), "CustomDataExternal");
3247 data->external= external;
3249 BLI_strncpy(external->filename, filename, sizeof(external->filename));
3251 layer->flag |= CD_FLAG_EXTERNAL|CD_FLAG_IN_MEMORY;
3254 void CustomData_external_remove(CustomData *data, ID *id, int type, int totelem)
3256 CustomDataExternal *external= data->external;
3257 CustomDataLayer *layer;
3258 //char filename[FILE_MAX];
3259 int layer_index; // i, remove_file;
3261 layer_index = CustomData_get_active_layer_index(data, type);
3262 if(layer_index < 0) return;
3264 layer = &data->layers[layer_index];
3269 if(layer->flag & CD_FLAG_EXTERNAL) {
3270 if(!(layer->flag & CD_FLAG_IN_MEMORY))
3271 CustomData_external_read(data, id, CD_TYPE_AS_MASK(layer->type), totelem);
3273 layer->flag &= ~CD_FLAG_EXTERNAL;
3277 for(i=0; i<data->totlayer; i++)
3278 if(data->layers[i].flag & CD_FLAG_EXTERNAL)
3282 customdata_external_filename(filename, id, external);
3283 cdf_remove(filename);
3284 CustomData_external_free(data);
3290 int CustomData_external_test(CustomData *data, int type)
3292 CustomDataLayer *layer;
3295 layer_index = CustomData_get_active_layer_index(data, type);
3296 if(layer_index < 0) return 0;