From 877f44162853664791f0ff4fa93f856384d0eed7 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Sun, 28 Feb 2016 15:29:56 +0100 Subject: BKE_mesh: add polygon flipping tools. Those new functions invert the winding of polygons, effectively inverting their normals. A helper was also added to allow swapping two items in customdata layers. Being able to invert normals outside of BMesh area is very important in several places, like IO scripts or customnormals modifiers... Reviewers: campbellbarton Differential Revision: https://developer.blender.org/D1814 --- source/blender/blenkernel/intern/customdata.c | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'source/blender/blenkernel/intern/customdata.c') diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c index c120509b769..1ed7c989075 100644 --- a/source/blender/blenkernel/intern/customdata.c +++ b/source/blender/blenkernel/intern/customdata.c @@ -2356,6 +2356,35 @@ void CustomData_swap_corners(struct CustomData *data, int index, const int *corn } } +/** + * Swap two items of given custom data, in all available layers. + */ +void CustomData_swap(struct CustomData *data, const int index_a, const int index_b) +{ + int i; + char buff_static[256]; + + if (index_a == index_b) { + return; + } + + for (i = 0; i < data->totlayer; ++i) { + const LayerTypeInfo *typeInfo = layerType_getInfo(data->layers[i].type); + const size_t size = typeInfo->size; + const size_t offset_a = size * index_a; + const size_t offset_b = size * index_b; + + void *buff = size <= sizeof(buff_static) ? buff_static : MEM_mallocN(size, __func__); + memcpy(buff, POINTER_OFFSET(data->layers[i].data, offset_a), size); + memcpy(POINTER_OFFSET(data->layers[i].data, offset_a), POINTER_OFFSET(data->layers[i].data, offset_b), size); + memcpy(POINTER_OFFSET(data->layers[i].data, offset_b), buff, size); + + if (buff != buff_static) { + MEM_freeN(buff); + } + } +} + void *CustomData_get(const CustomData *data, int index, int type) { int offset; -- cgit v1.2.3