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