svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22130:22205
[blender.git] / source / blender / blenkernel / intern / BME_Customdata.c
1 /**
2  * BME_customdata.c    jan 2007
3  *
4  *      Custom Data functions for Bmesh
5  *
6  * $Id$
7  *
8  * ***** BEGIN GPL LICENSE BLOCK *****
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version. The Blender
14  * Foundation also sells licenses for use in proprietary software under
15  * the Blender License.  See http://www.blender.org/BL/ for information
16  * about this.  
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  * The Original Code is Copyright (C) 2004 Blender Foundation.
28  * All rights reserved.
29  *
30  * The Original Code is: all of this file.
31  *
32  * Contributor(s): Geoffrey Bantle, Brecht Van Lommel, Ben Batt
33  *
34  * ***** END GPL LICENSE BLOCK *****
35  */
36
37
38 #include "BKE_bmesh.h"
39 #include "BKE_bmeshCustomData.h"
40 #include "bmesh_private.h"
41 #include <string.h>
42 #include "MEM_guardedalloc.h"
43 #include "BLI_mempool.h"
44
45 /********************* Layer type information **********************/
46 typedef struct BME_LayerTypeInfo {
47         int size;
48         char *defaultname;
49         void (*copy)(const void *source, void *dest, int count);
50         void (*free)(void *data, int count, int size);
51         void (*interp)(void **sources, float *weights, float *sub_weights, int count, void *dest);
52         void (*set_default)(void *data, int count);
53 } BME_LayerTypeInfo;
54 const BME_LayerTypeInfo BMELAYERTYPEINFO[BME_CD_NUMTYPES] = {
55         {sizeof(BME_facetex), "TexFace", NULL, NULL, NULL, NULL},
56         {sizeof(BME_looptex), "UV", NULL, NULL, NULL, NULL},
57         {sizeof(BME_loopcol), "VCol", NULL, NULL, NULL, NULL},
58         {sizeof(BME_DeformVert), "Group", NULL, NULL, NULL, NULL}
59 };
60 static const BME_LayerTypeInfo *BME_layerType_getInfo(int type)
61 {
62         if(type < 0 || type >= CD_NUMTYPES) return NULL;
63
64         return &BMELAYERTYPEINFO[type];
65 }
66 void BME_CD_Create(BME_CustomData *data, BME_CustomDataInit *init, int initalloc)
67 {
68         int i, j, offset=0;
69         const BME_LayerTypeInfo *info;
70         
71         /*initialize data members*/
72         data->layers = NULL;
73         data->pool = NULL;
74         data->totlayer = 0;
75         data->totsize = 0;
76
77         /*first count how many layers to alloc*/
78         for(i=0; i < BME_CD_NUMTYPES; i++){
79                 info = BME_layerType_getInfo(i);
80                 data->totlayer += init->layout[i];
81                 data->totsize  += (init->layout[i] * info->size);
82         }
83         /*alloc our layers*/
84         if(data->totlayer){
85                 /*alloc memory*/
86                 data->layers = MEM_callocN(sizeof(BME_CustomDataLayer)*data->totlayer, "BMesh Custom Data Layers");
87                 data->pool = BLI_mempool_create(data->totsize, initalloc, initalloc);
88                 /*initialize layer data*/
89                 for(i=0; i < BME_CD_NUMTYPES; i++){
90                         if(init->layout[i]){
91                                 info = BME_layerType_getInfo(i);
92                                 for(j=0; j < init->layout[i]; j++){
93                                         if(j==0) data->layers[j+i].active = init->active[i];
94                                         data->layers[j+i].type = i;
95                                         data->layers[j+i].offset = offset;      
96                                         strcpy(data->layers[j+i].name, &(init->nametemplate[j+i]));
97                                         offset += info->size;
98                                 }
99                         }
100                 }
101         }
102 }
103
104 void BME_CD_Free(BME_CustomData *data)
105 {
106         if(data->pool) BLI_mempool_destroy(data->pool);
107 }
108
109 /*Block level ops*/
110 void BME_CD_free_block(BME_CustomData *data, void **block)
111 {
112         const BME_LayerTypeInfo *typeInfo;
113         int i;
114
115         if(!*block) return;
116         for(i = 0; i < data->totlayer; ++i) {
117                 typeInfo = BME_layerType_getInfo(data->layers[i].type);
118                 if(typeInfo->free) {
119                         int offset = data->layers[i].offset;
120                         typeInfo->free((char*)*block + offset, 1, typeInfo->size);
121                 }
122         }
123         BLI_mempool_free(data->pool, *block);
124         *block = NULL;
125 }
126
127
128 static void BME_CD_alloc_block(BME_CustomData *data, void **block)
129 {       
130         
131         if (*block) BME_CD_free_block(data, block); //if we copy layers that have their own free functions like deformverts
132         
133         if (data->totsize > 0)
134                 *block = BLI_mempool_alloc(data->pool); 
135         else
136                 *block = NULL;
137 }
138
139 void BME_CD_copy_data(const BME_CustomData *source, BME_CustomData *dest,
140                             void *src_block, void **dest_block)
141 {
142         const BME_LayerTypeInfo *typeInfo;
143         int dest_i, src_i;
144
145         if (!*dest_block) /*for addXXXlist functions!*/
146                 BME_CD_alloc_block(dest, dest_block);
147         
148         /* copies a layer at a time */
149         dest_i = 0;
150         for(src_i = 0; src_i < source->totlayer; ++src_i) {
151
152                 /* find the first dest layer with type >= the source type
153                  * (this should work because layers are ordered by type)
154                  */
155                 while(dest_i < dest->totlayer
156                       && dest->layers[dest_i].type < source->layers[src_i].type)
157                         ++dest_i;
158
159                 /* if there are no more dest layers, we're done */
160                 if(dest_i >= dest->totlayer) return;
161
162                 /* if we found a matching layer, copy the data */
163                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
164                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
165                         char *src_data = (char*)src_block + source->layers[src_i].offset;
166                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
167
168                         typeInfo = BME_layerType_getInfo(source->layers[src_i].type);
169
170                         if(typeInfo->copy)
171                                 typeInfo->copy(src_data, dest_data, 1);
172                         else
173                                 memcpy(dest_data, src_data, typeInfo->size);
174
175                         /* if there are multiple source & dest layers of the same type,
176                          * we don't want to copy all source layers to the same dest, so
177                          * increment dest_i
178                          */
179                         ++dest_i;
180                 }
181         }
182 }
183 void BME_CD_set_default(BME_CustomData *data, void **block)
184 {
185         const BME_LayerTypeInfo *typeInfo;
186         int i;
187
188         if (!*block)
189                 BME_CD_alloc_block(data, block); //for addXXXlist functions...
190
191         for(i = 0; i < data->totlayer; ++i) {
192                 int offset = data->layers[i].offset;
193
194                 typeInfo = BME_layerType_getInfo(data->layers[i].type);
195
196                 if(typeInfo->set_default)
197                         typeInfo->set_default((char*)*block + offset, 1);
198         }
199 }