d2c00d21df3e81327348e52c23747f3f51305e25
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <string.h>
38
39 #include "MEM_guardedalloc.h"
40 #include "BKE_bmeshCustomData.h"
41 #include "bmesh_private.h"
42
43 /********************* Layer type information **********************/
44 typedef struct BME_LayerTypeInfo {
45         int size;
46         const char *defaultname;
47         void (*copy)(const void *source, void *dest, int count);
48         void (*free)(void *data, int count, int size);
49         void (*interp)(void **sources, float *weights, float *sub_weights, int count, void *dest);
50         void (*set_default)(void *data, int count);
51 } BME_LayerTypeInfo;
52 const BME_LayerTypeInfo BMELAYERTYPEINFO[BME_CD_NUMTYPES] = {
53         {sizeof(BME_facetex), "TexFace", NULL, NULL, NULL, NULL},
54         {sizeof(BME_looptex), "UV", NULL, NULL, NULL, NULL},
55         {sizeof(BME_loopcol), "VCol", NULL, NULL, NULL, NULL},
56         {sizeof(BME_DeformVert), "Group", NULL, NULL, NULL, NULL}
57 };
58 static const BME_LayerTypeInfo *BME_layerType_getInfo(int type)
59 {
60         if(type < 0 || type >= CD_NUMTYPES) return NULL;
61
62         return &BMELAYERTYPEINFO[type];
63 }
64 void BME_CD_Create(BME_CustomData *data, BME_CustomDataInit *init, int initalloc)
65 {
66         int i, j, offset=0;
67         const BME_LayerTypeInfo *info;
68         
69         /*initialize data members*/
70         data->layers = NULL;
71         data->pool = NULL;
72         data->totlayer = 0;
73         data->totsize = 0;
74
75         /*first count how many layers to alloc*/
76         for(i=0; i < BME_CD_NUMTYPES; i++){
77                 info = BME_layerType_getInfo(i);
78                 data->totlayer += init->layout[i];
79                 data->totsize  += (init->layout[i] * info->size);
80         }
81         /*alloc our layers*/
82         if(data->totlayer){
83                 /*alloc memory*/
84                 data->layers = MEM_callocN(sizeof(BME_CustomDataLayer)*data->totlayer, "BMesh Custom Data Layers");
85                 data->pool = BLI_mempool_create(data->totsize, initalloc, initalloc, 0);
86                 /*initialize layer data*/
87                 for(i=0; i < BME_CD_NUMTYPES; i++){
88                         if(init->layout[i]){
89                                 info = BME_layerType_getInfo(i);
90                                 for(j=0; j < init->layout[i]; j++){
91                                         if(j==0) data->layers[j+i].active = init->active[i];
92                                         data->layers[j+i].type = i;
93                                         data->layers[j+i].offset = offset;      
94                                         strcpy(data->layers[j+i].name, &(init->nametemplate[j+i]));
95                                         offset += info->size;
96                                 }
97                         }
98                 }
99         }
100 }
101
102 void BME_CD_Free(BME_CustomData *data)
103 {
104         if(data->pool) BLI_mempool_destroy(data->pool);
105 }
106
107 /*Block level ops*/
108 void BME_CD_free_block(BME_CustomData *data, void **block)
109 {
110         const BME_LayerTypeInfo *typeInfo;
111         int i;
112
113         if(!*block) return;
114         for(i = 0; i < data->totlayer; ++i) {
115                 typeInfo = BME_layerType_getInfo(data->layers[i].type);
116                 if(typeInfo->free) {
117                         int offset = data->layers[i].offset;
118                         typeInfo->free((char*)*block + offset, 1, typeInfo->size);
119                 }
120         }
121         BLI_mempool_free(data->pool, *block);
122         *block = NULL;
123 }
124
125
126 static void BME_CD_alloc_block(BME_CustomData *data, void **block)
127 {       
128         
129         if (*block) BME_CD_free_block(data, block); //if we copy layers that have their own free functions like deformverts
130         
131         if (data->totsize > 0)
132                 *block = BLI_mempool_alloc(data->pool); 
133         else
134                 *block = NULL;
135 }
136
137 void BME_CD_copy_data(const BME_CustomData *source, BME_CustomData *dest,
138                                                         void *src_block, void **dest_block)
139 {
140         const BME_LayerTypeInfo *typeInfo;
141         int dest_i, src_i;
142
143         if (!*dest_block) /*for addXXXlist functions!*/
144                 BME_CD_alloc_block(dest, dest_block);
145         
146         /* copies a layer at a time */
147         dest_i = 0;
148         for(src_i = 0; src_i < source->totlayer; ++src_i) {
149
150                 /* find the first dest layer with type >= the source type
151                  * (this should work because layers are ordered by type)
152                  */
153                 while(dest_i < dest->totlayer
154                           && dest->layers[dest_i].type < source->layers[src_i].type)
155                         ++dest_i;
156
157                 /* if there are no more dest layers, we're done */
158                 if(dest_i >= dest->totlayer) return;
159
160                 /* if we found a matching layer, copy the data */
161                 if(dest->layers[dest_i].type == source->layers[src_i].type &&
162                         strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) {
163                         char *src_data = (char*)src_block + source->layers[src_i].offset;
164                         char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset;
165
166                         typeInfo = BME_layerType_getInfo(source->layers[src_i].type);
167
168                         if(typeInfo->copy)
169                                 typeInfo->copy(src_data, dest_data, 1);
170                         else
171                                 memcpy(dest_data, src_data, typeInfo->size);
172
173                         /* if there are multiple source & dest layers of the same type,
174                          * we don't want to copy all source layers to the same dest, so
175                          * increment dest_i
176                          */
177                         ++dest_i;
178                 }
179         }
180 }
181 void BME_CD_set_default(BME_CustomData *data, void **block)
182 {
183         const BME_LayerTypeInfo *typeInfo;
184         int i;
185
186         if (!*block)
187                 BME_CD_alloc_block(data, block); //for addXXXlist functions...
188
189         for(i = 0; i < data->totlayer; ++i) {
190                 int offset = data->layers[i].offset;
191
192                 typeInfo = BME_layerType_getInfo(data->layers[i].type);
193
194                 if(typeInfo->set_default)
195                         typeInfo->set_default((char*)*block + offset, 1);
196         }
197 }