d774324f1c120ee5942da20f9721d4e3a0fceb2d
[blender.git] / source / blender / blenkernel / intern / customdata.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 *
20 * The Original Code is Copyright (C) 2006 Blender Foundation.
21 * All rights reserved.
22 *
23 * The Original Code is: all of this file.
24 *
25 * Contributor(s): Ben Batt <benbatt@gmail.com>
26 *
27 * ***** END GPL LICENSE BLOCK *****
28 *
29 * Implementation of CustomData.
30 *
31 * BKE_customdata.h contains the function prototypes for this file.
32 *
33 */ 
34
35 #include "BKE_customdata.h"
36
37 #include "BLI_linklist.h"
38
39 #include "DNA_mesh_types.h"
40 #include "DNA_meshdata_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include <string.h>
45
46 /* number of layers to add when growing a CustomData object */
47 #define CUSTOMDATA_GROW 5
48
49 /* descriptor and storage for a custom data layer */
50 typedef struct LayerDesc {
51         int type;   /* type of data in layer */
52         int flag;   /* general purpose flag */
53         void *data; /* layer data */
54 } LayerDesc;
55
56 /********************* Layer type information **********************/
57 typedef struct LayerTypeInfo {
58         int size; /* the memory size of one element of this layer's data */
59
60         /* a function to copy count elements of this layer's data
61          * (deep copy if appropriate)
62          * size should be the size of one element of this layer's data (e.g.
63          * LayerTypeInfo.size)
64          * if NULL, memcpy is used
65          */
66         void (*copy)(const void *source, void *dest, int count, int size);
67
68         /* a function to free any dynamically allocated components of this
69          * layer's data (note the data pointer itself should not be freed)
70          * size should be the size of one element of this layer's data (e.g.
71          * LayerTypeInfo.size)
72          */
73         void (*free)(void *data, int count, int size);
74
75         /* a function to interpolate between count source elements of this
76          * layer's data and store the result in dest
77          * if weights == NULL or sub_weights == NULL, they should default to 1
78          *
79          * weights gives the weight for each element in sources
80          * sub_weights gives the sub-element weights for each element in sources
81          *    (there should be (sub element count)^2 weights per element)
82          * count gives the number of elements in sources
83          */
84         void (*interp)(void **sources, float *weights, float *sub_weights,
85                        int count, void *dest);
86 } LayerTypeInfo;
87
88 static void layerCopy_mdeformvert(const void *source, void *dest,
89                                   int count, int size);
90
91 static void layerFree_mdeformvert(void *data, int count, int size);
92
93 static void layerInterp_mdeformvert(void **sources, float *weights,
94                                     float *sub_weights, int count, void *dest);
95
96 static void layerInterp_tface(void **sources, float *weights,
97                               float *sub_weights, int count, void *dest);
98
99 static void layerInterp_mcol(void **sources, float *weights,
100                              float *sub_weights, int count, void *dest);
101
102 const LayerTypeInfo LAYERTYPEINFO[LAYERTYPE_NUMTYPES] = {
103         {sizeof(MVert), NULL, NULL, NULL},
104         {sizeof(MSticky), NULL, NULL, NULL},
105         {sizeof(MDeformVert),
106          layerCopy_mdeformvert, layerFree_mdeformvert, layerInterp_mdeformvert},
107         {sizeof(MEdge), NULL, NULL, NULL},
108         {sizeof(MFace), NULL, NULL, NULL},
109         {sizeof(TFace), NULL, NULL, layerInterp_tface},
110         /* 4 MCol structs per face */
111         {sizeof(MCol) * 4, NULL, NULL, layerInterp_mcol},
112         {sizeof(int), NULL, NULL, NULL},
113         /* 3 floats per normal vector */
114         {sizeof(float) * 3, NULL, NULL, NULL},
115         {sizeof(int), NULL, NULL, NULL},
116 };
117
118 const LayerTypeInfo *layerType_getInfo(int type)
119 {
120         if(type < 0 || type >= LAYERTYPE_NUMTYPES) return NULL;
121
122         return &LAYERTYPEINFO[type];
123 }
124
125 static void layerCopy_mdeformvert(const void *source, void *dest,
126                                   int count, int size)
127 {
128         int i;
129
130         memcpy(dest, source, count * size);
131
132         for(i = 0; i < count; ++i) {
133                 MDeformVert *dvert = (MDeformVert *)((char *)dest + i * size);
134                 MDeformWeight *dw = MEM_callocN(dvert->totweight * sizeof(*dw),
135                                                 "layerCopy_mdeformvert dw");
136
137                 memcpy(dw, dvert->dw, dvert->totweight * sizeof(*dw));
138                 dvert->dw = dw;
139         }
140 }
141
142 static void layerFree_mdeformvert(void *data, int count, int size)
143 {
144         int i;
145
146         for(i = 0; i < count; ++i) {
147                 MDeformVert *dvert = (MDeformVert *)((char *)data + i * size);
148
149                 if(dvert->dw) {
150                         MEM_freeN(dvert->dw);
151                         dvert->dw = NULL;
152                 }
153         }
154 }
155
156 static void linklist_free_simple(void *link)
157 {
158         MEM_freeN(link);
159 }
160
161 static void layerInterp_mdeformvert(void **sources, float *weights,
162                                     float *sub_weights, int count, void *dest)
163 {
164         MDeformVert *dvert = dest;
165         LinkNode *dest_dw = NULL; /* a list of lists of MDeformWeight pointers */
166         LinkNode *node;
167         int i, j, totweight;
168
169         if(count <= 0) return;
170
171         /* build a list of unique def_nrs for dest */
172         totweight = 0;
173         for(i = 0; i < count; ++i) {
174                 MDeformVert *source = sources[i];
175                 float interp_weight = weights ? weights[i] : 1.0f;
176
177                 for(j = 0; j < source->totweight; ++j) {
178                         MDeformWeight *dw = &source->dw[j];
179
180                         for(node = dest_dw; node; node = node->next) {
181                                 MDeformWeight *tmp_dw = (MDeformWeight *)node->link;
182
183                                 if(tmp_dw->def_nr == dw->def_nr) {
184                                         tmp_dw->weight += dw->weight * interp_weight;
185                                         break;
186                                 }
187                         }
188
189                         /* if this def_nr is not in the list, add it */
190                         if(!node) {
191                                 MDeformWeight *tmp_dw = MEM_callocN(sizeof(*tmp_dw),
192                                                             "layerInterp_mdeformvert tmp_dw");
193                                 tmp_dw->def_nr = dw->def_nr;
194                                 tmp_dw->weight = dw->weight * interp_weight;
195                                 BLI_linklist_prepend(&dest_dw, tmp_dw);
196                                 totweight++;
197                         }
198                 }
199         }
200
201         /* now we know how many unique deform weights there are, so realloc */
202         if(dvert->dw) MEM_freeN(dvert->dw);
203         dvert->dw = MEM_callocN(sizeof(*dvert->dw) * totweight,
204                                 "layerInterp_mdeformvert dvert->dw");
205         dvert->totweight = totweight;
206
207         for(i = 0, node = dest_dw; node; node = node->next, ++i)
208                 dvert->dw[i] = *((MDeformWeight *)node->link);
209
210         BLI_linklist_free(dest_dw, linklist_free_simple);
211 }
212
213 static void layerInterp_tface(void **sources, float *weights,
214                               float *sub_weights, int count, void *dest)
215 {
216         TFace *tf = dest;
217         int i, j, k;
218         float uv[4][2];
219         float col[4][4];
220
221         if(count <= 0) return;
222
223         memset(uv, 0, sizeof(uv));
224         memset(col, 0, sizeof(col));
225
226         for(i = 0; i < count; ++i) {
227                 float weight = weights ? weights[i] : 1;
228                 TFace *src = sources[i];
229
230                 for(j = 0; j < 4; ++j) {
231                         if(sub_weights) {
232                                 for(k = 0; k < 4; ++k) {
233                                         float sub_weight = sub_weights[j * 4 + k];
234                                         char *tmp_col = (char *)&src->col[k];
235                                         float *tmp_uv = src->uv[k];
236
237                                         uv[j][0] += tmp_uv[0] * sub_weight * weight;
238                                         uv[j][1] += tmp_uv[1] * sub_weight * weight;
239
240                                         col[j][0] += tmp_col[0] * sub_weight * weight;
241                                         col[j][1] += tmp_col[1] * sub_weight * weight;
242                                         col[j][2] += tmp_col[2] * sub_weight * weight;
243                                         col[j][3] += tmp_col[3] * sub_weight * weight;
244                                 }
245                         } else {
246                                 char *tmp_col = (char *)&src->col[j];
247                                 uv[j][0] += src->uv[j][0] * weight;
248                                 uv[j][1] += src->uv[j][1] * weight;
249
250                                 col[j][0] += tmp_col[0] * weight;
251                                 col[j][1] += tmp_col[1] * weight;
252                                 col[j][2] += tmp_col[2] * weight;
253                                 col[j][3] += tmp_col[3] * weight;
254                         }
255                 }
256         }
257
258         *tf = *(TFace *)sources[0];
259         for(j = 0; j < 4; ++j) {
260                 char *tmp_col = (char *)&tf->col[j];
261
262                 tf->uv[j][0] = uv[j][0];
263                 tf->uv[j][1] = uv[j][1];
264
265                 tmp_col[0] = (int)col[j][0];
266                 tmp_col[1] = (int)col[j][1];
267                 tmp_col[2] = (int)col[j][2];
268                 tmp_col[3] = (int)col[j][3];
269         }
270 }
271
272 static void layerInterp_mcol(void **sources, float *weights,
273                              float *sub_weights, int count, void *dest)
274 {
275         MCol *mc = dest;
276         int i, j, k;
277         struct {
278                 float a;
279                 float r;
280                 float g;
281                 float b;
282         } col[4];
283
284         if(count <= 0) return;
285
286         memset(col, 0, sizeof(col));
287
288         for(i = 0; i < count; ++i) {
289                 float weight = weights ? weights[i] : 1;
290                 float *sub_weight = sub_weights;
291
292                 for(j = 0; j < 4; ++j) {
293                         if(sub_weights) {
294                                 MCol *src = sources[i];
295                                 for(k = 0; k < 4; ++k, ++sub_weight, ++src) {
296                                         col[j].a += src->a * (*sub_weight) * weight;
297                                         col[j].r += src->r * (*sub_weight) * weight;
298                                         col[j].g += src->g * (*sub_weight) * weight;
299                                         col[j].b += src->b * (*sub_weight) * weight;
300                                 }
301                         } else {
302                                 MCol *src = sources[i];
303                                 col[j].a += src[j].a * weight;
304                                 col[j].r += src[j].r * weight;
305                                 col[j].g += src[j].g * weight;
306                                 col[j].b += src[j].b * weight;
307                         }
308                 }
309         }
310
311         for(j = 0; j < 4; ++j) {
312                 mc[j].a = (int)col[j].a;
313                 mc[j].r = (int)col[j].r;
314                 mc[j].g = (int)col[j].g;
315                 mc[j].b = (int)col[j].b;
316         }
317 }
318
319 /********************* CustomData functions *********************/
320 void CustomData_init(CustomData *data,
321                      int maxLayers, int maxElems, int subElems)
322 {
323         data->layers = MEM_callocN(maxLayers * sizeof(*data->layers),
324                                     "CustomData->layers");
325         data->numLayers = 0;
326         data->maxLayers = maxLayers;
327         data->numElems = maxElems;
328         data->maxElems = maxElems;
329         data->subElems = subElems;
330 }
331
332 void CustomData_from_template(const CustomData *source, CustomData *dest,
333                               int maxElems)
334 {
335         int i;
336
337         CustomData_init(dest, source->maxLayers, maxElems, source->subElems);
338
339         for(i = 0; i < source->numLayers; ++i) {
340                 if(source->layers[i].flag & LAYERFLAG_NOCOPY) continue;
341
342                 CustomData_add_layer(dest, source->layers[i].type,
343                                      source->layers[i].flag & ~LAYERFLAG_NOFREE, NULL);
344         }
345 }
346
347 void CustomData_free(CustomData *data)
348 {
349         int i;
350         const LayerTypeInfo *typeInfo;
351
352         for(i = 0; i < data->numLayers; ++i) {
353                 if(!(data->layers[i].flag & LAYERFLAG_NOFREE)) {
354                         typeInfo = layerType_getInfo(data->layers[i].type);
355                         if(typeInfo->free)
356                                 typeInfo->free(data->layers[i].data, data->numElems,
357                                                typeInfo->size);
358
359                         MEM_freeN(data->layers[i].data);
360                 }
361         }
362
363         data->numLayers = 0;
364
365         if(data->layers) {
366                 MEM_freeN(data->layers);
367                 data->layers = NULL;
368         }
369 }
370
371 static int customData_grow(CustomData *data, int amount)
372 {
373         LayerDesc *tmp = MEM_callocN(sizeof(*tmp) * (data->maxLayers + amount),
374                                  "CustomData->layers");
375         if(!tmp) return 0;
376
377         data->maxLayers += amount;
378         memcpy(tmp, data->layers, sizeof(*tmp) * data->numLayers);
379
380         MEM_freeN(data->layers);
381         data->layers = tmp;
382
383         return 1;
384 }
385
386 static int customData_add_layer__internal(CustomData *data, int type,
387                                           int flag, void *layer)
388 {
389         int index = data->numLayers;
390
391         if(index >= data->maxLayers)
392                 if(!customData_grow(data, CUSTOMDATA_GROW)) return 0;
393
394         /* keep layers ordered by type */
395         for( ; index > 0 && data->layers[index - 1].type > type; --index)
396                 data->layers[index] = data->layers[index - 1];
397
398         data->layers[index].type = type;
399         data->layers[index].flag = flag;
400         data->layers[index].data = layer;
401
402         data->numLayers++;
403
404         return 1;
405 }
406
407 int CustomData_add_layer(CustomData *data, int type, int flag, void *layer)
408 {
409         int size = layerType_getInfo(type)->size * data->numElems;
410         void *tmp_layer = layer;
411
412         if(!layer) tmp_layer = MEM_callocN(size, "LayerDesc.data");
413
414         if(!tmp_layer) return 0;
415
416         if(customData_add_layer__internal(data, type, flag, tmp_layer))
417                 return 1;
418         else {
419                 MEM_freeN(tmp_layer);
420                 return 0;
421         }
422 }
423
424 int CustomData_compat(const CustomData *data1, const CustomData *data2)
425 {
426         int i;
427
428         if(data1->numLayers != data2->numLayers) return 0;
429
430         for(i = 0; i < data1->numLayers; ++i) {
431                 if(data1->layers[i].type != data2->layers[i].type) return 0;
432                 if(data1->layers[i].flag != data2->layers[i].flag) return 0;
433         }
434
435         return 1;
436 }
437
438 /* gets index of first layer matching type after start_index
439  * if start_index < 0, starts searching at 0
440  * returns -1 if there is no layer of type
441  */
442 static int CustomData_find_next(const CustomData *data, int type,
443                                 int start_index)
444 {
445         int i = start_index + 1;
446
447         if(i < 0) i = 0;
448
449         for(; i < data->numLayers; ++i)
450                 if(data->layers[i].type == type) return i;
451
452         return -1;
453 }
454
455 int CustomData_copy_data(const CustomData *source, CustomData *dest,
456                          int source_index, int dest_index, int count)
457 {
458         const LayerTypeInfo *type_info;
459         int src_i, dest_i;
460         int src_offset;
461         int dest_offset;
462
463         if(count < 0) return 0;
464         if(source_index < 0 || (source_index + count) > source->numElems)
465                 return 0;
466         if(dest_index < 0 || (dest_index + count) > dest->numElems)
467                 return 0;
468
469         /* copies a layer at a time */
470         dest_i = 0;
471         for(src_i = 0; src_i < source->numLayers; ++src_i) {
472                 if(source->layers[src_i].flag & LAYERFLAG_NOCOPY) continue;
473
474                 /* find the first dest layer with type >= the source type
475                  * (this should work because layers are ordered by type)
476                  */
477                 while(dest_i < dest->numLayers
478                       && dest->layers[dest_i].type < source->layers[src_i].type)
479                         ++dest_i;
480
481                 /* if there are no more dest layers, we're done */
482                 if(dest_i >= dest->numLayers) return 1;
483
484                 /* if we found a matching layer, copy the data */
485                 if(dest->layers[dest_i].type == source->layers[src_i].type) {
486                         char *src_data = source->layers[src_i].data;
487                         char *dest_data = dest->layers[dest_i].data;
488
489                         type_info = layerType_getInfo(source->layers[src_i].type);
490
491                         src_offset = source_index * type_info->size;
492                         dest_offset = dest_index * type_info->size;
493
494                         if(type_info->copy)
495                                 type_info->copy(src_data + src_offset,
496                                                 dest_data + dest_offset,
497                                                 count, type_info->size);
498                         else
499                                 memcpy(dest_data + dest_offset,
500                                        src_data + src_offset,
501                                        count * type_info->size);
502
503                         /* if there are multiple source & dest layers of the same type,
504                          * we don't want to copy all source layers to the same dest, so
505                          * increment dest_i
506                          */
507                         ++dest_i;
508                 }
509         }
510
511         return 1;
512 }
513
514 int CustomData_free_elem(CustomData *data, int index, int count)
515 {
516         int i;
517         const LayerTypeInfo *typeInfo;
518
519         if(index < 0 || count <= 0 || index + count > data->numElems) return 0;
520
521         for(i = 0; i < data->numLayers; ++i) {
522                 if(!(data->layers[i].flag & LAYERFLAG_NOFREE)) {
523                         typeInfo = layerType_getInfo(data->layers[i].type);
524
525                         if(typeInfo->free) {
526                                 int offset = typeInfo->size * index;
527
528                                 typeInfo->free((char *)data->layers[i].data + offset,
529                                                count, typeInfo->size);
530                         }
531                 }
532         }
533
534         return 1;
535 }
536
537 #define SOURCE_BUF_SIZE 100
538
539 int CustomData_interp(const CustomData *source, CustomData *dest,
540                       int *src_indices, float *weights, float *sub_weights,
541                       int count, int dest_index)
542 {
543         int src_i, dest_i;
544         int dest_offset;
545         int j;
546         void *source_buf[SOURCE_BUF_SIZE];
547         void **sources = source_buf;
548
549         if(count <= 0) return 0;
550         if(dest_index < 0 || dest_index >= dest->numElems) return 0;
551
552         /* slow fallback in case we're interpolating a ridiculous number of
553          * elements
554          */
555         if(count > SOURCE_BUF_SIZE)
556                 sources = MEM_callocN(sizeof(*sources) * count,
557                                       "CustomData_interp sources");
558
559         /* interpolates a layer at a time */
560         for(src_i = 0; src_i < source->numLayers; ++src_i) {
561                 LayerDesc *source_layer = &source->layers[src_i];
562                 const LayerTypeInfo *type_info =
563                                         layerType_getInfo(source_layer->type);
564
565                 dest_i = CustomData_find_next(dest, source_layer->type, -1);
566
567                 if(dest_i >= 0 && type_info->interp) {
568                         void *src_data = source_layer->data; 
569
570                         for(j = 0; j < count; ++j)
571                                 sources[j] = (char *)src_data
572                                              + type_info->size * src_indices[j];
573
574                         dest_offset = dest_index * type_info->size;
575
576                         type_info->interp(sources, weights, sub_weights, count,
577                                        (char *)dest->layers[dest_i].data + dest_offset);
578                 }
579         }
580
581         if(count > SOURCE_BUF_SIZE) MEM_freeN(sources);
582         return 1;
583 }
584
585 void *CustomData_get(const CustomData *data, int index, int type)
586 {
587         int offset;
588         int layer_index;
589         
590         if(index < 0 || index > data->numElems) return NULL;
591
592         /* get the layer index of the first layer of type */
593         layer_index = CustomData_find_next(data, type, -1);
594         if(layer_index < 0) return NULL;
595
596         /* get the offset of the desired element */
597         offset = layerType_getInfo(type)->size * index;
598
599         return (char *)data->layers[layer_index].data + offset;
600 }
601
602 void *CustomData_get_layer(const CustomData *data, int type)
603 {
604         /* get the layer index of the first layer of type */
605         int layer_index = CustomData_find_next(data, type, -1);
606
607         if(layer_index < 0) return NULL;
608
609         return data->layers[layer_index].data;
610 }
611
612 void CustomData_set(const CustomData *data, int index, int type, void *source)
613 {
614         void *dest = CustomData_get(data, index, type);
615         const LayerTypeInfo *type_info = layerType_getInfo(type);
616
617         if(!dest) return;
618
619         if(type_info->copy)
620                 type_info->copy(source, dest, 1, type_info->size);
621         else
622                 memcpy(dest, source, type_info->size);
623 }
624
625 void CustomData_set_num_elems(CustomData *data, int numElems)
626 {
627         if(numElems < 0) return;
628         if(numElems < data->maxElems) data->numElems = numElems;
629         else data->numElems = data->maxElems;
630 }