From e527ce552e9e3864c7a2f5bb688ffa1d4cd0d5f1 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 19 Oct 2012 10:40:32 +0000 Subject: [PATCH] add option to initialize heap with a known number of elements, since this may be known in advance - it avoids re-allocing too much. --- source/blender/blenlib/BLI_heap.h | 1 + source/blender/blenlib/intern/BLI_heap.c | 12 +++++-- source/blender/bmesh/intern/bmesh_decimate.c | 2 +- .../blender/modifiers/intern/MOD_decimate.c | 32 +++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/source/blender/blenlib/BLI_heap.h b/source/blender/blenlib/BLI_heap.h index b378f2bb365..9d7e6107f19 100644 --- a/source/blender/blenlib/BLI_heap.h +++ b/source/blender/blenlib/BLI_heap.h @@ -42,6 +42,7 @@ typedef void (*HeapFreeFP)(void *ptr); /* Creates a new heap. BLI_memarena is used for allocating nodes. Removed nodes * are recycled, so memory usage will not shrink. */ +Heap *BLI_heap_new_ex(unsigned int tot_reserve); Heap *BLI_heap_new(void); void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp); diff --git a/source/blender/blenlib/intern/BLI_heap.c b/source/blender/blenlib/intern/BLI_heap.c index ee7d93ea1a9..5e0762a5d68 100644 --- a/source/blender/blenlib/intern/BLI_heap.c +++ b/source/blender/blenlib/intern/BLI_heap.c @@ -67,16 +67,22 @@ struct Heap { /***/ -Heap *BLI_heap_new(void) +/* use when the size of the heap is known in advance */ +Heap *BLI_heap_new_ex(unsigned int tot_reserve) { Heap *heap = (Heap *)MEM_callocN(sizeof(Heap), __func__); - heap->bufsize = 1; - heap->tree = (HeapNode **)MEM_mallocN(sizeof(HeapNode *), "BLIHeapTree"); + heap->bufsize = tot_reserve; + heap->tree = (HeapNode **)MEM_mallocN(tot_reserve * sizeof(HeapNode *), "BLIHeapTree"); heap->arena = BLI_memarena_new(1 << 16, "heap arena"); return heap; } +Heap *BLI_heap_new(void) +{ + return BLI_heap_new_ex(1); +} + void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp) { int i; diff --git a/source/blender/bmesh/intern/bmesh_decimate.c b/source/blender/bmesh/intern/bmesh_decimate.c index d8aa8adef55..122f24955c6 100644 --- a/source/blender/bmesh/intern/bmesh_decimate.c +++ b/source/blender/bmesh/intern/bmesh_decimate.c @@ -531,7 +531,7 @@ void BM_mesh_decimate(BMesh *bm, const float factor) /* alloc vars */ vquadrics = MEM_callocN(sizeof(Quadric) * bm->totvert, __func__); - eheap = BLI_heap_new(); + eheap = BLI_heap_new_ex(bm->totedge); eheap_table = MEM_callocN(sizeof(HeapNode *) * bm->totedge, __func__); tot_edge_orig = bm->totedge; diff --git a/source/blender/modifiers/intern/MOD_decimate.c b/source/blender/modifiers/intern/MOD_decimate.c index cb6681bfa68..171d601ea6d 100644 --- a/source/blender/modifiers/intern/MOD_decimate.c +++ b/source/blender/modifiers/intern/MOD_decimate.c @@ -47,6 +47,10 @@ #include "BKE_particle.h" #include "BKE_cdderivedmesh.h" +#include "BKE_tessmesh.h" + +/* testing only! - Campbell */ +// #define USE_DECIMATE_BMESH #ifdef WITH_MOD_DECIMATE #include "LOD_decimation.h" @@ -70,6 +74,33 @@ static void copyData(ModifierData *md, ModifierData *target) } #ifdef WITH_MOD_DECIMATE +#ifdef USE_DECIMATE_BMESH + +#include "bmesh.h" + +static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob), + DerivedMesh *derivedData, + ModifierApplyFlag UNUSED(flag)) +{ + DecimateModifierData *dmd = (DecimateModifierData *) md; + DerivedMesh *dm = derivedData, *result = NULL; + BMEditMesh *em; + BMesh *bm; + + em = DM_to_editbmesh(dm, NULL, FALSE); + bm = em->bm; + + BM_mesh_decimate(bm, dmd->percent); + + BLI_assert(em->looptris == NULL); + result = CDDM_from_BMEditMesh(em, NULL, TRUE, FALSE); + BMEdit_Free(em); + MEM_freeN(em); + + return result; +} + +#else static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob), DerivedMesh *derivedData, ModifierApplyFlag UNUSED(flag)) @@ -192,6 +223,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob), return dm; } } +#endif // USE_DECIMATE_BMESH #else // WITH_MOD_DECIMATE static DerivedMesh *applyModifier(ModifierData *UNUSED(md), Object *UNUSED(ob), DerivedMesh *derivedData,