diff options
author | Lukas Tönne <lukas.toenne@gmail.com> | 2018-04-15 13:16:55 +0300 |
---|---|---|
committer | Lukas Tönne <lukas.toenne@gmail.com> | 2018-04-15 13:16:55 +0300 |
commit | 7e1832c8d546ec13e752b7bd42ce13e3fc10ae86 (patch) | |
tree | 0fff9d878db69b288f909ff9a0f8aefe31ab8d80 /source/blender/blenlib/intern | |
parent | c0c8df3f2cf3ab03cec1f660619b0fe2290caf2a (diff) | |
parent | 94959dba1b53640e2a36cf9b5ca46aaf49c5c74a (diff) |
Merge branch 'blender2.8' into hair_guides
Diffstat (limited to 'source/blender/blenlib/intern')
32 files changed, 544 insertions, 466 deletions
diff --git a/source/blender/blenlib/intern/BLI_array.c b/source/blender/blenlib/intern/BLI_array.c index f681d222e69..d16dd36763d 100644 --- a/source/blender/blenlib/intern/BLI_array.c +++ b/source/blender/blenlib/intern/BLI_array.c @@ -66,21 +66,23 @@ /** * This function is only to be called via macros. * - * \note The caller must adjust \a arr_count + * \note The caller must adjust \a arr_len */ -void _bli_array_grow_func(void **arr_p, const void *arr_static, - const int sizeof_arr_p, const int arr_count, const int num, - const char *alloc_str) +void _bli_array_grow_func( + void **arr_p, const void *arr_static, + const int sizeof_arr_p, const int arr_len, const int num, + const char *alloc_str) { void *arr = *arr_p; void *arr_tmp; - arr_tmp = MEM_mallocN(sizeof_arr_p * - ((num < arr_count) ? - (arr_count * 2 + 2) : (arr_count + num)), alloc_str); + arr_tmp = MEM_mallocN( + sizeof_arr_p * + ((num < arr_len) ? + (arr_len * 2 + 2) : (arr_len + num)), alloc_str); if (arr) { - memcpy(arr_tmp, arr, sizeof_arr_p * arr_count); + memcpy(arr_tmp, arr, sizeof_arr_p * arr_len); if (arr != arr_static) { MEM_freeN(arr); @@ -91,6 +93,6 @@ void _bli_array_grow_func(void **arr_p, const void *arr_static, /* caller must do */ #if 0 - arr_count += num; + arr_len += num; #endif } diff --git a/source/blender/blenlib/intern/BLI_dial.c b/source/blender/blenlib/intern/BLI_dial_2d.c index 89f18fa10b4..d31367c5e87 100644 --- a/source/blender/blenlib/intern/BLI_dial.c +++ b/source/blender/blenlib/intern/BLI_dial_2d.c @@ -18,7 +18,11 @@ * ***** END GPL LICENSE BLOCK ***** */ -#include "BLI_dial.h" +/** \file blender/blenlib/intern/BLI_dial_2d.c + * \ingroup bli + */ + +#include "BLI_dial_2d.h" #include "BLI_math.h" #include "MEM_guardedalloc.h" diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index bf2f25fae69..ec75c140159 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -43,11 +43,12 @@ #include "BLI_sys_types.h" /* for intptr_t support */ #include "BLI_utildefines.h" -#include "BLI_hash_mm2a.h" #include "BLI_mempool.h" #define GHASH_INTERNAL_API -#include "BLI_ghash.h" +#include "BLI_ghash.h" /* own include */ + +/* keep last */ #include "BLI_strict_flags.h" /* -------------------------------------------------------------------- */ @@ -741,7 +742,7 @@ void BLI_ghash_reserve(GHash *gh, const uint nentries_reserve) /** * \return size of the GHash. */ -uint BLI_ghash_size(GHash *gh) +uint BLI_ghash_len(GHash *gh) { return gh->nentries; } @@ -1010,7 +1011,7 @@ void BLI_ghash_clear(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfree */ void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp) { - BLI_assert((int)gh->nentries == BLI_mempool_count(gh->entrypool)); + BLI_assert((int)gh->nentries == BLI_mempool_len(gh->entrypool)); if (keyfreefp || valfreefp) ghash_free_cb(gh, keyfreefp, valfreefp); @@ -1044,7 +1045,7 @@ void BLI_ghash_flag_clear(GHash *gh, uint flag) /** * Create a new GHashIterator. The hash table must not be mutated * while the iterator is in use, and the iterator will step exactly - * BLI_ghash_size(gh) times before becoming done. + * BLI_ghash_len(gh) times before becoming done. * * \param gh The GHash to iterate over. * \return Pointer to a new DynStr. @@ -1059,7 +1060,7 @@ GHashIterator *BLI_ghashIterator_new(GHash *gh) /** * Init an already allocated GHashIterator. The hash table must not * be mutated while the iterator is in use, and the iterator will - * step exactly BLI_ghash_size(gh) times before becoming done. + * step exactly BLI_ghash_len(gh) times before becoming done. * * \param ghi The GHashIterator to initialize. * \param gh The GHash to iterate over. @@ -1161,219 +1162,6 @@ bool BLI_ghashIterator_done(GHashIterator *ghi) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Generic Key Hash & Comparison Functions - * \{ */ - -/***/ - -#if 0 -/* works but slower */ -uint BLI_ghashutil_ptrhash(const void *key) -{ - return (uint)(intptr_t)key; -} -#else -/* based python3.3's pointer hashing function */ -uint BLI_ghashutil_ptrhash(const void *key) -{ - size_t y = (size_t)key; - /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid - * excessive hash collisions for dicts and sets */ - y = (y >> 4) | (y << (8 * sizeof(void *) - 4)); - return (uint)y; -} -#endif -bool BLI_ghashutil_ptrcmp(const void *a, const void *b) -{ - return (a != b); -} - -uint BLI_ghashutil_uinthash_v4(const uint key[4]) -{ - uint hash; - hash = key[0]; - hash *= 37; - hash += key[1]; - hash *= 37; - hash += key[2]; - hash *= 37; - hash += key[3]; - return hash; -} -uint BLI_ghashutil_uinthash_v4_murmur(const uint key[4]) -{ - return BLI_hash_mm2((const unsigned char *)key, sizeof(int) * 4 /* sizeof(key) */, 0); -} - -bool BLI_ghashutil_uinthash_v4_cmp(const void *a, const void *b) -{ - return (memcmp(a, b, sizeof(uint[4])) != 0); -} - -uint BLI_ghashutil_uinthash(uint key) -{ - key += ~(key << 16); - key ^= (key >> 5); - key += (key << 3); - key ^= (key >> 13); - key += ~(key << 9); - key ^= (key >> 17); - - return key; -} - -uint BLI_ghashutil_inthash_p(const void *ptr) -{ - uintptr_t key = (uintptr_t)ptr; - - key += ~(key << 16); - key ^= (key >> 5); - key += (key << 3); - key ^= (key >> 13); - key += ~(key << 9); - key ^= (key >> 17); - - return (uint)(key & 0xffffffff); -} - -uint BLI_ghashutil_inthash_p_murmur(const void *ptr) -{ - uintptr_t key = (uintptr_t)ptr; - - return BLI_hash_mm2((const unsigned char *)&key, sizeof(key), 0); -} - -uint BLI_ghashutil_inthash_p_simple(const void *ptr) -{ - return GET_UINT_FROM_POINTER(ptr); -} - -bool BLI_ghashutil_intcmp(const void *a, const void *b) -{ - return (a != b); -} - -size_t BLI_ghashutil_combine_hash(size_t hash_a, size_t hash_b) -{ - return hash_a ^ (hash_b + 0x9e3779b9 + (hash_a << 6) + (hash_a >> 2)); -} - -/** - * This function implements the widely used "djb" hash apparently posted - * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit - * unsigned hash value starts at 5381 and for each byte 'c' in the - * string, is updated: ``hash = hash * 33 + c``. This - * function uses the signed value of each byte. - * - * note: this is the same hash method that glib 2.34.0 uses. - */ -uint BLI_ghashutil_strhash_n(const char *key, size_t n) -{ - const signed char *p; - uint h = 5381; - - for (p = (const signed char *)key; n-- && *p != '\0'; p++) { - h = (uint)((h << 5) + h) + (uint)*p; - } - - return h; -} -uint BLI_ghashutil_strhash_p(const void *ptr) -{ - const signed char *p; - uint h = 5381; - - for (p = ptr; *p != '\0'; p++) { - h = (uint)((h << 5) + h) + (uint)*p; - } - - return h; -} -uint BLI_ghashutil_strhash_p_murmur(const void *ptr) -{ - const unsigned char *key = ptr; - - return BLI_hash_mm2(key, strlen((const char *)key) + 1, 0); -} -bool BLI_ghashutil_strcmp(const void *a, const void *b) -{ - return (a == b) ? false : !STREQ(a, b); -} - -GHashPair *BLI_ghashutil_pairalloc(const void *first, const void *second) -{ - GHashPair *pair = MEM_mallocN(sizeof(GHashPair), "GHashPair"); - pair->first = first; - pair->second = second; - return pair; -} - -uint BLI_ghashutil_pairhash(const void *ptr) -{ - const GHashPair *pair = ptr; - uint hash = BLI_ghashutil_ptrhash(pair->first); - return hash ^ BLI_ghashutil_ptrhash(pair->second); -} - -bool BLI_ghashutil_paircmp(const void *a, const void *b) -{ - const GHashPair *A = a; - const GHashPair *B = b; - - return (BLI_ghashutil_ptrcmp(A->first, B->first) || - BLI_ghashutil_ptrcmp(A->second, B->second)); -} - -void BLI_ghashutil_pairfree(void *ptr) -{ - MEM_freeN(ptr); -} - -/** \} */ - -/* -------------------------------------------------------------------- */ -/** \name Convenience GHash Creation Functions - * \{ */ - -GHash *BLI_ghash_ptr_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_ghash_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve); -} -GHash *BLI_ghash_ptr_new(const char *info) -{ - return BLI_ghash_ptr_new_ex(info, 0); -} - -GHash *BLI_ghash_str_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_ghash_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve); -} -GHash *BLI_ghash_str_new(const char *info) -{ - return BLI_ghash_str_new_ex(info, 0); -} - -GHash *BLI_ghash_int_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_ghash_new_ex(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, info, nentries_reserve); -} -GHash *BLI_ghash_int_new(const char *info) -{ - return BLI_ghash_int_new_ex(info, 0); -} - -GHash *BLI_ghash_pair_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_ghash_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve); -} -GHash *BLI_ghash_pair_new(const char *info) -{ - return BLI_ghash_pair_new_ex(info, 0); -} - -/** \} */ - -/* -------------------------------------------------------------------- */ /** \name GSet Public API * * Use ghash API to give 'set' functionality @@ -1398,7 +1186,7 @@ GSet *BLI_gset_copy(GSet *gs, GHashKeyCopyFP keycopyfp) return (GSet *)ghash_copy((GHash *)gs, keycopyfp, NULL); } -uint BLI_gset_size(GSet *gs) +uint BLI_gset_len(GSet *gs) { return ((GHash *)gs)->nentries; } @@ -1557,7 +1345,7 @@ void *BLI_gset_lookup(GSet *gs, const void *key) /** * Returns the pointer to the key if it's found, removing it from the GSet. - * \node Caller must handle freeing. + * \note Caller must handle freeing. */ void *BLI_gset_pop_key(GSet *gs, const void *key) { @@ -1577,39 +1365,6 @@ void *BLI_gset_pop_key(GSet *gs, const void *key) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Convenience GSet Creation Functions - * \{ */ - -GSet *BLI_gset_ptr_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_gset_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve); -} -GSet *BLI_gset_ptr_new(const char *info) -{ - return BLI_gset_ptr_new_ex(info, 0); -} - -GSet *BLI_gset_str_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_gset_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve); -} -GSet *BLI_gset_str_new(const char *info) -{ - return BLI_gset_str_new_ex(info, 0); -} - -GSet *BLI_gset_pair_new_ex(const char *info, const uint nentries_reserve) -{ - return BLI_gset_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve); -} -GSet *BLI_gset_pair_new(const char *info) -{ - return BLI_gset_pair_new_ex(info, 0); -} - -/** \} */ - -/* -------------------------------------------------------------------- */ /** \name Debugging & Introspection * \{ */ @@ -1618,13 +1373,13 @@ GSet *BLI_gset_pair_new(const char *info) /** * \return number of buckets in the GHash. */ -int BLI_ghash_buckets_size(GHash *gh) +int BLI_ghash_buckets_len(GHash *gh) { return (int)gh->nbuckets; } -int BLI_gset_buckets_size(GSet *gs) +int BLI_gset_buckets_len(GSet *gs) { - return BLI_ghash_buckets_size((GHash *)gs); + return BLI_ghash_buckets_len((GHash *)gs); } /** diff --git a/source/blender/blenlib/intern/BLI_ghash_utils.c b/source/blender/blenlib/intern/BLI_ghash_utils.c new file mode 100644 index 00000000000..6554ee7c92f --- /dev/null +++ b/source/blender/blenlib/intern/BLI_ghash_utils.c @@ -0,0 +1,288 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/blenlib/intern/BLI_ghash_utils.c + * \ingroup bli + * + * Helper functions and implementations of standard data types for #GHash + * (not it's implementation). + */ + +#include <string.h> + +#include "MEM_guardedalloc.h" + +#include "BLI_utildefines.h" +#include "BLI_hash_mm2a.h" +#include "BLI_ghash.h" /* own include */ + +/* keep last */ +#include "BLI_strict_flags.h" + +/* -------------------------------------------------------------------- */ +/** \name Generic Key Hash & Comparison Functions + * \{ */ + +#if 0 +/* works but slower */ +uint BLI_ghashutil_ptrhash(const void *key) +{ + return (uint)(intptr_t)key; +} +#else +/* based python3.3's pointer hashing function */ +uint BLI_ghashutil_ptrhash(const void *key) +{ + size_t y = (size_t)key; + /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid + * excessive hash collisions for dicts and sets */ + y = (y >> 4) | (y << (8 * sizeof(void *) - 4)); + return (uint)y; +} +#endif +bool BLI_ghashutil_ptrcmp(const void *a, const void *b) +{ + return (a != b); +} + +uint BLI_ghashutil_uinthash_v4(const uint key[4]) +{ + uint hash; + hash = key[0]; + hash *= 37; + hash += key[1]; + hash *= 37; + hash += key[2]; + hash *= 37; + hash += key[3]; + return hash; +} +uint BLI_ghashutil_uinthash_v4_murmur(const uint key[4]) +{ + return BLI_hash_mm2((const unsigned char *)key, sizeof(int) * 4 /* sizeof(key) */, 0); +} + +bool BLI_ghashutil_uinthash_v4_cmp(const void *a, const void *b) +{ + return (memcmp(a, b, sizeof(uint[4])) != 0); +} + +uint BLI_ghashutil_uinthash(uint key) +{ + key += ~(key << 16); + key ^= (key >> 5); + key += (key << 3); + key ^= (key >> 13); + key += ~(key << 9); + key ^= (key >> 17); + + return key; +} + +uint BLI_ghashutil_inthash_p(const void *ptr) +{ + uintptr_t key = (uintptr_t)ptr; + + key += ~(key << 16); + key ^= (key >> 5); + key += (key << 3); + key ^= (key >> 13); + key += ~(key << 9); + key ^= (key >> 17); + + return (uint)(key & 0xffffffff); +} + +uint BLI_ghashutil_inthash_p_murmur(const void *ptr) +{ + uintptr_t key = (uintptr_t)ptr; + + return BLI_hash_mm2((const unsigned char *)&key, sizeof(key), 0); +} + +uint BLI_ghashutil_inthash_p_simple(const void *ptr) +{ + return GET_UINT_FROM_POINTER(ptr); +} + +bool BLI_ghashutil_intcmp(const void *a, const void *b) +{ + return (a != b); +} + +size_t BLI_ghashutil_combine_hash(size_t hash_a, size_t hash_b) +{ + return hash_a ^ (hash_b + 0x9e3779b9 + (hash_a << 6) + (hash_a >> 2)); +} + +/** + * This function implements the widely used "djb" hash apparently posted + * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit + * unsigned hash value starts at 5381 and for each byte 'c' in the + * string, is updated: ``hash = hash * 33 + c``. This + * function uses the signed value of each byte. + * + * note: this is the same hash method that glib 2.34.0 uses. + */ +uint BLI_ghashutil_strhash_n(const char *key, size_t n) +{ + const signed char *p; + uint h = 5381; + + for (p = (const signed char *)key; n-- && *p != '\0'; p++) { + h = (uint)((h << 5) + h) + (uint)*p; + } + + return h; +} +uint BLI_ghashutil_strhash_p(const void *ptr) +{ + const signed char *p; + uint h = 5381; + + for (p = ptr; *p != '\0'; p++) { + h = (uint)((h << 5) + h) + (uint)*p; + } + + return h; +} +uint BLI_ghashutil_strhash_p_murmur(const void *ptr) +{ + const unsigned char *key = ptr; + + return BLI_hash_mm2(key, strlen((const char *)key) + 1, 0); +} +bool BLI_ghashutil_strcmp(const void *a, const void *b) +{ + return (a == b) ? false : !STREQ(a, b); +} + +GHashPair *BLI_ghashutil_pairalloc(const void *first, const void *second) +{ + GHashPair *pair = MEM_mallocN(sizeof(GHashPair), "GHashPair"); + pair->first = first; + pair->second = second; + return pair; +} + +uint BLI_ghashutil_pairhash(const void *ptr) +{ + const GHashPair *pair = ptr; + uint hash = BLI_ghashutil_ptrhash(pair->first); + return hash ^ BLI_ghashutil_ptrhash(pair->second); +} + +bool BLI_ghashutil_paircmp(const void *a, const void *b) +{ + const GHashPair *A = a; + const GHashPair *B = b; + + return (BLI_ghashutil_ptrcmp(A->first, B->first) || + BLI_ghashutil_ptrcmp(A->second, B->second)); +} + +void BLI_ghashutil_pairfree(void *ptr) +{ + MEM_freeN(ptr); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Convenience GHash Creation Functions + * \{ */ + +GHash *BLI_ghash_ptr_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_ghash_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve); +} +GHash *BLI_ghash_ptr_new(const char *info) +{ + return BLI_ghash_ptr_new_ex(info, 0); +} + +GHash *BLI_ghash_str_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_ghash_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve); +} +GHash *BLI_ghash_str_new(const char *info) +{ + return BLI_ghash_str_new_ex(info, 0); +} + +GHash *BLI_ghash_int_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_ghash_new_ex(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, info, nentries_reserve); +} +GHash *BLI_ghash_int_new(const char *info) +{ + return BLI_ghash_int_new_ex(info, 0); +} + +GHash *BLI_ghash_pair_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_ghash_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve); +} +GHash *BLI_ghash_pair_new(const char *info) +{ + return BLI_ghash_pair_new_ex(info, 0); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Convenience GSet Creation Functions + * \{ */ + +GSet *BLI_gset_ptr_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_gset_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve); +} +GSet *BLI_gset_ptr_new(const char *info) +{ + return BLI_gset_ptr_new_ex(info, 0); +} + +GSet *BLI_gset_str_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_gset_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve); +} +GSet *BLI_gset_str_new(const char *info) +{ + return BLI_gset_str_new_ex(info, 0); +} + +GSet *BLI_gset_pair_new_ex(const char *info, const uint nentries_reserve) +{ + return BLI_gset_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve); +} +GSet *BLI_gset_pair_new(const char *info) +{ + return BLI_gset_pair_new_ex(info, 0); +} + +/** \} */ diff --git a/source/blender/blenlib/intern/BLI_heap.c b/source/blender/blenlib/intern/BLI_heap.c index 7c249344541..0c71e75e40f 100644 --- a/source/blender/blenlib/intern/BLI_heap.c +++ b/source/blender/blenlib/intern/BLI_heap.c @@ -303,7 +303,7 @@ bool BLI_heap_is_empty(const Heap *heap) return (heap->size == 0); } -uint BLI_heap_size(const Heap *heap) +uint BLI_heap_len(const Heap *heap) { return heap->size; } @@ -320,7 +320,7 @@ HeapNode *BLI_heap_top(const Heap *heap) /** * Pop the top node off the heap and return it's pointer. */ -void *BLI_heap_popmin(Heap *heap) +void *BLI_heap_pop_min(Heap *heap) { BLI_assert(heap->size != 0); @@ -348,7 +348,7 @@ void BLI_heap_remove(Heap *heap, HeapNode *node) i = p; } - BLI_heap_popmin(heap); + BLI_heap_pop_min(heap); } /** diff --git a/source/blender/blenlib/intern/BLI_kdopbvh.c b/source/blender/blenlib/intern/BLI_kdopbvh.c index 9c055a227a9..06e8ade68a2 100644 --- a/source/blender/blenlib/intern/BLI_kdopbvh.c +++ b/source/blender/blenlib/intern/BLI_kdopbvh.c @@ -856,9 +856,8 @@ static void non_recursive_bvh_div_nodes_task_cb( else { break; } - - parent->totnode = (char)(k + 1); } + parent->totnode = (char)k; } /** @@ -1147,7 +1146,7 @@ void BLI_bvhtree_update_tree(BVHTree *tree) * Number of times #BLI_bvhtree_insert has been called. * mainly useful for asserts functions to check we added the correct number. */ -int BLI_bvhtree_get_size(const BVHTree *tree) +int BLI_bvhtree_get_len(const BVHTree *tree) { return tree->totleaf; } @@ -1924,6 +1923,7 @@ void BLI_bvhtree_ray_cast_all( BLI_bvhtree_ray_cast_all_ex(tree, co, dir, radius, hit_dist, callback, userdata, BVH_RAYCAST_DEFAULT); } +/** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c index 5b13f129ad4..3a65e6c42ca 100644 --- a/source/blender/blenlib/intern/BLI_mempool.c +++ b/source/blender/blenlib/intern/BLI_mempool.c @@ -474,7 +474,7 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr) } } -int BLI_mempool_count(BLI_mempool *pool) +int BLI_mempool_len(BLI_mempool *pool) { return (int)pool->totused; } diff --git a/source/blender/blenlib/intern/array_store.c b/source/blender/blenlib/intern/array_store.c index 5b1715bbb3d..df93dad4c32 100644 --- a/source/blender/blenlib/intern/array_store.c +++ b/source/blender/blenlib/intern/array_store.c @@ -299,7 +299,7 @@ typedef struct BChunk { } BChunk; /** - * Links to store #BChunk data in #BChunkList.chunks. + * Links to store #BChunk data in #BChunkList.chunk_refs. */ typedef struct BChunkRef { struct BChunkRef *next, *prev; @@ -749,6 +749,7 @@ static void bchunk_list_fill_from_array( ASSERT_CHUNKLIST_DATA(chunk_list, data); } +/** \} */ /* --------------------------------------------------------------------------- * Internal Table Lookup Functions @@ -1013,6 +1014,10 @@ static const BChunkRef *table_lookup( /** \} */ +/** \name Main Data De-Duplication Function + * + * \{ */ + /** * \param data: Data to store in the returned value. * \param data_len_original: Length of data in bytes. @@ -1504,6 +1509,8 @@ void BLI_array_store_clear( BLI_mempool_clear(bs->memory.chunk); } +/** \} */ + /** \name BArrayStore Statistics * \{ */ @@ -1759,7 +1766,7 @@ bool BLI_array_store_is_valid( goto user_finally; } } - if (!(BLI_mempool_count(bs->memory.chunk_list) == (int)BLI_ghash_size(chunk_list_map))) { + if (!(BLI_mempool_len(bs->memory.chunk_list) == (int)BLI_ghash_len(chunk_list_map))) { ok = false; goto user_finally; } @@ -1772,11 +1779,11 @@ bool BLI_array_store_is_valid( totrefs += 1; } } - if (!(BLI_mempool_count(bs->memory.chunk) == (int)BLI_ghash_size(chunk_map))) { + if (!(BLI_mempool_len(bs->memory.chunk) == (int)BLI_ghash_len(chunk_map))) { ok = false; goto user_finally; } - if (!(BLI_mempool_count(bs->memory.chunk_ref) == totrefs)) { + if (!(BLI_mempool_len(bs->memory.chunk_ref) == totrefs)) { ok = false; goto user_finally; } diff --git a/source/blender/blenlib/intern/array_utils.c b/source/blender/blenlib/intern/array_utils.c index 32f0111babd..7b2d35a763c 100644 --- a/source/blender/blenlib/intern/array_utils.c +++ b/source/blender/blenlib/intern/array_utils.c @@ -308,3 +308,20 @@ bool _bli_array_iter_span( return false; } + +/** + * Simple utility to check memory is zeroed. + */ +bool _bli_array_is_zeroed( + const void *arr_v, + unsigned int arr_len, size_t arr_stride) +{ + const char *arr_step = (const char *)arr_v; + size_t i = arr_stride * arr_len; + while (i--) { + if (*(arr_step++)) { + return false; + } + } + return true; +} diff --git a/source/blender/blenlib/intern/astar.c b/source/blender/blenlib/intern/astar.c index 0020dbe4612..1b57dc5a683 100644 --- a/source/blender/blenlib/intern/astar.c +++ b/source/blender/blenlib/intern/astar.c @@ -231,7 +231,7 @@ bool BLI_astar_graph_solve( SET_INT_IN_POINTER(node_index_src)); while (!BLI_heap_is_empty(todo_nodes)) { - const int node_curr_idx = GET_INT_FROM_POINTER(BLI_heap_popmin(todo_nodes)); + const int node_curr_idx = GET_INT_FROM_POINTER(BLI_heap_pop_min(todo_nodes)); BLI_AStarGNode *node_curr = &as_graph->nodes[node_curr_idx]; LinkData *ld; diff --git a/source/blender/blenlib/intern/boxpack2d.c b/source/blender/blenlib/intern/boxpack_2d.c index ff17d2ac28b..cf9e29209fb 100644 --- a/source/blender/blenlib/intern/boxpack2d.c +++ b/source/blender/blenlib/intern/boxpack_2d.c @@ -20,7 +20,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/boxpack2d.c +/** \file blender/blenlib/intern/boxpack_2d.c * \ingroup bli */ @@ -30,7 +30,7 @@ #include "MEM_guardedalloc.h" #include "BLI_utildefines.h" -#include "BLI_boxpack2d.h" /* own include */ +#include "BLI_boxpack_2d.h" /* own include */ #include "BLI_sort.h" /* qsort_r */ #define qsort_r BLI_qsort_r diff --git a/source/blender/blenlib/intern/convexhull2d.c b/source/blender/blenlib/intern/convexhull_2d.c index 1f1f214ce32..38928dbbaa0 100644 --- a/source/blender/blenlib/intern/convexhull2d.c +++ b/source/blender/blenlib/intern/convexhull_2d.c @@ -18,7 +18,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/convexhull2d.c +/** \file blender/blenlib/intern/convexhull_2d.c * \ingroup bli */ @@ -28,7 +28,7 @@ #include "MEM_guardedalloc.h" -#include "BLI_convexhull2d.h" +#include "BLI_convexhull_2d.h" #include "BLI_math.h" #include "BLI_strict_flags.h" #include "BLI_utildefines.h" diff --git a/source/blender/blenlib/intern/edgehash.c b/source/blender/blenlib/intern/edgehash.c index 772c8bd6247..7074a776aaf 100644 --- a/source/blender/blenlib/intern/edgehash.c +++ b/source/blender/blenlib/intern/edgehash.c @@ -527,7 +527,7 @@ bool BLI_edgehash_haskey(EdgeHash *eh, uint v0, uint v1) /** * Return number of keys in hash. */ -int BLI_edgehash_size(EdgeHash *eh) +int BLI_edgehash_len(EdgeHash *eh) { return (int)eh->nentries; } @@ -565,7 +565,7 @@ void BLI_edgehash_clear(EdgeHash *eh, EdgeHashFreeFP valfreefp) void BLI_edgehash_free(EdgeHash *eh, EdgeHashFreeFP valfreefp) { - BLI_assert((int)eh->nentries == BLI_mempool_count(eh->epool)); + BLI_assert((int)eh->nentries == BLI_mempool_len(eh->epool)); if (valfreefp) edgehash_free_cb(eh, valfreefp); @@ -599,7 +599,7 @@ void BLI_edgehash_flag_clear(EdgeHash *eh, uint flag) /** * Create a new EdgeHashIterator. The hash table must not be mutated * while the iterator is in use, and the iterator will step exactly - * BLI_edgehash_size(eh) times before becoming done. + * BLI_edgehash_len(eh) times before becoming done. */ EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh) { @@ -611,7 +611,7 @@ EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh) /** * Init an already allocated EdgeHashIterator. The hash table must not * be mutated while the iterator is in use, and the iterator will - * step exactly BLI_edgehash_size(eh) times before becoming done. + * step exactly BLI_edgehash_len(eh) times before becoming done. * * \param ehi The EdgeHashIterator to initialize. * \param eh The EdgeHash to iterate over. @@ -729,7 +729,7 @@ EdgeSet *BLI_edgeset_new(const char *info) return BLI_edgeset_new_ex(info, 0); } -int BLI_edgeset_size(EdgeSet *es) +int BLI_edgeset_len(EdgeSet *es) { return (int)((EdgeHash *)es)->nentries; } diff --git a/source/blender/blenlib/intern/gsqueue.c b/source/blender/blenlib/intern/gsqueue.c index 25da7701924..5c8c43ab92a 100644 --- a/source/blender/blenlib/intern/gsqueue.c +++ b/source/blender/blenlib/intern/gsqueue.c @@ -81,7 +81,7 @@ bool BLI_gsqueue_is_empty(GSQueue *gq) /** * Query number elements in the queue */ -int BLI_gsqueue_size(GSQueue *gq) +int BLI_gsqueue_len(GSQueue *gq) { GSQueueElem *elem; int size = 0; @@ -162,7 +162,7 @@ void BLI_gsqueue_push(GSQueue *gq, const void *item) * \param item A pointer to an appropriately * sized structure (the size passed to BLI_gsqueue_new). */ -void BLI_gsqueue_pushback(GSQueue *gq, const void *item) +void BLI_gsqueue_push_back(GSQueue *gq, const void *item) { GSQueueElem *elem = MEM_mallocN(sizeof(*elem) + gq->elem_size, "gqueue_push"); memcpy(elem->data, item, gq->elem_size); diff --git a/source/blender/blenlib/intern/jitter.c b/source/blender/blenlib/intern/jitter_2d.c index bc2b5677fa5..26351f52c7f 100644 --- a/source/blender/blenlib/intern/jitter.c +++ b/source/blender/blenlib/intern/jitter_2d.c @@ -25,7 +25,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/jitter.c +/** \file blender/blenlib/intern/jitter_2d.c * \ingroup bli * \brief Jitter offset table */ @@ -35,7 +35,7 @@ #include "MEM_guardedalloc.h" #include "BLI_rand.h" -#include "BLI_jitter.h" +#include "BLI_jitter_2d.h" #include "BLI_strict_flags.h" diff --git a/source/blender/blenlib/intern/lasso.c b/source/blender/blenlib/intern/lasso_2d.c index 710da09521a..663f3ffea22 100644 --- a/source/blender/blenlib/intern/lasso.c +++ b/source/blender/blenlib/intern/lasso_2d.c @@ -26,7 +26,7 @@ * */ -/** \file blender/blenlib/intern/lasso.c +/** \file blender/blenlib/intern/lasso_2d.c * \ingroup bli */ @@ -35,7 +35,7 @@ #include "BLI_math.h" #include "BLI_strict_flags.h" -#include "BLI_lasso.h" /* own include */ +#include "BLI_lasso_2d.h" /* own include */ void BLI_lasso_boundbox(rcti *rect, const int mcords[][2], const unsigned int moves) { diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c index 96c3972d802..b87fed571b7 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.c @@ -484,7 +484,7 @@ void BLI_freelistN(ListBase *listbase) * * \note Use to avoid redundant looping. */ -int BLI_listbase_count_ex(const ListBase *listbase, const int count_max) +int BLI_listbase_count_at_most(const ListBase *listbase, const int count_max) { Link *link; int count = 0; diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index 2f5b0f420b1..4bedcbdf5bf 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -324,6 +324,27 @@ MINLINE size_t max_zz(size_t a, size_t b) return (b < a) ? a : b; } +MINLINE int clamp_i(int value, int min, int max) +{ + return min_ii(max_ii(value, min), max); +} + +MINLINE float clamp_f(float value, float min, float max) +{ + if (value > max) { + return max; + } + else if (value < min) { + return min; + } + return value; +} + +MINLINE size_t clamp_z(size_t value, size_t min, size_t max) +{ + return min_zz(max_zz(value, min), max); +} + /** * Almost-equal for IEEE floats, using absolute difference method. * @@ -347,10 +368,8 @@ MINLINE int compare_ff_relative(float a, float b, const float max_diff, const in { union {float f; int i;} ua, ub; -#if 0 /* No BLI_assert in INLINE :/ */ BLI_assert(sizeof(float) == sizeof(int)); BLI_assert(max_ulps < (1 << 22)); -#endif if (fabsf(a - b) <= max_diff) { return 1; @@ -454,7 +473,7 @@ MALWAYS_INLINE __m128 _bli_math_fastpow24(const __m128 arg) */ /* 0x3F4CCCCD = 4/5 */ /* 0x4F55A7FB = 2^(127/(4/5) - 127) * 0.994^(1/(4/5)) */ - /* error max = 0.17 avg = 0.0018 |avg| = 0.05 */ + /* error max = 0.17, avg = 0.0018, |avg| = 0.05 */ __m128 x = _bli_math_fastpow(0x3F4CCCCD, 0x4F55A7FB, arg); __m128 arg2 = _mm_mul_ps(arg, arg); __m128 arg4 = _mm_mul_ps(arg2, arg2); diff --git a/source/blender/blenlib/intern/math_bits_inline.c b/source/blender/blenlib/intern/math_bits_inline.c index 37fdcd7878a..9b16756134e 100644 --- a/source/blender/blenlib/intern/math_bits_inline.c +++ b/source/blender/blenlib/intern/math_bits_inline.c @@ -34,7 +34,7 @@ MINLINE int bitscan_forward_i(int a) { BLI_assert(a != 0); -# ifdef _MSC_VER +#ifdef _MSC_VER unsigned long ctz; _BitScanForward(&ctz, a); return ctz; @@ -63,7 +63,7 @@ MINLINE unsigned int bitscan_forward_clear_uint(unsigned int *a) MINLINE int bitscan_reverse_i(int a) { BLI_assert(a != 0); -# ifdef _MSC_VER +#ifdef _MSC_VER unsigned long clz; _BitScanReverse(&clz, a); return clz; diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index cb39134af45..534abeb913d 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -656,135 +656,80 @@ void rgb_to_lab(float r, float g, float b, float *ll, float *la, float *lb) xyz_to_lab(x, y, z, ll, la, lb); } -static void xyz_to_lms(float x, float y, float z, float *l, float *m, float *s) -{ - *l = 0.3897f * x + 0.6890f * y - 0.0787f * z; - *m = -0.2298f * x + 1.1834f * y + 0.0464f * z; - *s = z; -} - -static void lms_to_xyz(float l, float m, float s, float *x, float *y, float *z) -{ - *x = 1.9102f * l - 1.1121f * m + 0.2019f * s; - *y = 0.3709f * l + 0.6290f * m + 0.0000f * s; - *z = s; -} - -static void normalize_rgb(float rgb[3]) -{ - const float max = max_fff(rgb[0], rgb[1], rgb[2]); +/* ****************************** blackbody ******************************** */ - if (max > 0.0f) { - mul_v3_fl(rgb, 1.0f / max); - } -} - -/* Color rendering of spectra, adapted from public domain code by John Walker, - * http://www.fourmilab.ch/ +/* Calculate color in range 800..12000 using an approximation + * a/x+bx+c for R and G and ((at + b)t + c)t + d) for B + * Max absolute error for RGB is (0.00095, 0.00077, 0.00057), + * which is enough to get the same 8 bit/channel color. */ -static void spectrum_to_xyz(float temperature, float xyz[3]) -{ - int i; - float lambda, x = 0.0f, y = 0.0f, z = 0.0f, xyz_sum; - - /* CIE colour matching functions xBar, yBar, and zBar for wavelengths from - * 380 through 780 nanometers, every 5 nanometers. - * For a wavelength lambda in this range: - * - * cie_colour_match[(lambda - 380) / 5][0] = xBar - * cie_colour_match[(lambda - 380) / 5][1] = yBar - * cie_colour_match[(lambda - 380) / 5][2] = zBar - */ - - const float cie_colour_match[81][3] = { - {0.0014f, 0.0000f, 0.0065f}, {0.0022f, 0.0001f, 0.0105f}, {0.0042f, 0.0001f, 0.0201f}, - {0.0076f, 0.0002f, 0.0362f}, {0.0143f, 0.0004f, 0.0679f}, {0.0232f, 0.0006f, 0.1102f}, - {0.0435f, 0.0012f, 0.2074f}, {0.0776f, 0.0022f, 0.3713f}, {0.1344f, 0.0040f, 0.6456f}, - {0.2148f, 0.0073f, 1.0391f}, {0.2839f, 0.0116f, 1.3856f}, {0.3285f, 0.0168f, 1.6230f}, - {0.3483f, 0.0230f, 1.7471f}, {0.3481f, 0.0298f, 1.7826f}, {0.3362f, 0.0380f, 1.7721f}, - {0.3187f, 0.0480f, 1.7441f}, {0.2908f, 0.0600f, 1.6692f}, {0.2511f, 0.0739f, 1.5281f}, - {0.1954f, 0.0910f, 1.2876f}, {0.1421f, 0.1126f, 1.0419f}, {0.0956f, 0.1390f, 0.8130f}, - {0.0580f, 0.1693f, 0.6162f}, {0.0320f, 0.2080f, 0.4652f}, {0.0147f, 0.2586f, 0.3533f}, - {0.0049f, 0.3230f, 0.2720f}, {0.0024f, 0.4073f, 0.2123f}, {0.0093f, 0.5030f, 0.1582f}, - {0.0291f, 0.6082f, 0.1117f}, {0.0633f, 0.7100f, 0.0782f}, {0.1096f, 0.7932f, 0.0573f}, - {0.1655f, 0.8620f, 0.0422f}, {0.2257f, 0.9149f, 0.0298f}, {0.2904f, 0.9540f, 0.0203f}, - {0.3597f, 0.9803f, 0.0134f}, {0.4334f, 0.9950f, 0.0087f}, {0.5121f, 1.0000f, 0.0057f}, - {0.5945f, 0.9950f, 0.0039f}, {0.6784f, 0.9786f, 0.0027f}, {0.7621f, 0.9520f, 0.0021f}, - {0.8425f, 0.9154f, 0.0018f}, {0.9163f, 0.8700f, 0.0017f}, {0.9786f, 0.8163f, 0.0014f}, - {1.0263f, 0.7570f, 0.0011f}, {1.0567f, 0.6949f, 0.0010f}, {1.0622f, 0.6310f, 0.0008f}, - {1.0456f, 0.5668f, 0.0006f}, {1.0026f, 0.5030f, 0.0003f}, {0.9384f, 0.4412f, 0.0002f}, - {0.8544f, 0.3810f, 0.0002f}, {0.7514f, 0.3210f, 0.0001f}, {0.6424f, 0.2650f, 0.0000f}, - {0.5419f, 0.2170f, 0.0000f}, {0.4479f, 0.1750f, 0.0000f}, {0.3608f, 0.1382f, 0.0000f}, - {0.2835f, 0.1070f, 0.0000f}, {0.2187f, 0.0816f, 0.0000f}, {0.1649f, 0.0610f, 0.0000f}, - {0.1212f, 0.0446f, 0.0000f}, {0.0874f, 0.0320f, 0.0000f}, {0.0636f, 0.0232f, 0.0000f}, - {0.0468f, 0.0170f, 0.0000f}, {0.0329f, 0.0119f, 0.0000f}, {0.0227f, 0.0082f, 0.0000f}, - {0.0158f, 0.0057f, 0.0000f}, {0.0114f, 0.0041f, 0.0000f}, {0.0081f, 0.0029f, 0.0000f}, - {0.0058f, 0.0021f, 0.0000f}, {0.0041f, 0.0015f, 0.0000f}, {0.0029f, 0.0010f, 0.0000f}, - {0.0020f, 0.0007f, 0.0000f}, {0.0014f, 0.0005f, 0.0000f}, {0.0010f, 0.0004f, 0.0000f}, - {0.0007f, 0.0002f, 0.0000f}, {0.0005f, 0.0002f, 0.0000f}, {0.0003f, 0.0001f, 0.0000f}, - {0.0002f, 0.0001f, 0.0000f}, {0.0002f, 0.0001f, 0.0000f}, {0.0001f, 0.0000f, 0.0000f}, - {0.0001f, 0.0000f, 0.0000f}, {0.0001f, 0.0000f, 0.0000f}, {0.0000f, 0.0000f, 0.0000f} - }; - - for (i = 0, lambda = 380.0f; lambda < 780.1f; i++, lambda += 5.0f) { - /* wavelength in meter */ - const float wlm = lambda * 1e-9f; - const float Me = (3.74183e-16f * powf(wlm, -5.0f)) / (expf(1.4388e-2f / (wlm * temperature)) - 1.0f); - - x += Me * cie_colour_match[i][0]; - y += Me * cie_colour_match[i][1]; - z += Me * cie_colour_match[i][2]; +static const float blackbody_table_r[6][3] = { + { 2.52432244e+03f, -1.06185848e-03f, 3.11067539e+00f }, + { 3.37763626e+03f, -4.34581697e-04f, 1.64843306e+00f }, + { 4.10671449e+03f, -8.61949938e-05f, 6.41423749e-01f }, + { 4.66849800e+03f, 2.85655028e-05f, 1.29075375e-01f }, + { 4.60124770e+03f, 2.89727618e-05f, 1.48001316e-01f }, + { 3.78765709e+03f, 9.36026367e-06f, 3.98995841e-01f }, +}; + +static const float blackbody_table_g[6][3] = { + { -7.50343014e+02f, 3.15679613e-04f, 4.73464526e-01f }, + { -1.00402363e+03f, 1.29189794e-04f, 9.08181524e-01f }, + { -1.22075471e+03f, 2.56245413e-05f, 1.20753416e+00f }, + { -1.42546105e+03f, -4.01730887e-05f, 1.44002695e+00f }, + { -1.18134453e+03f, -2.18913373e-05f, 1.30656109e+00f }, + { -5.00279505e+02f, -4.59745390e-06f, 1.09090465e+00f }, +}; + +static const float blackbody_table_b[6][4] = { + { 0.0f, 0.0f, 0.0f, 0.0f }, + { 0.0f, 0.0f, 0.0f, 0.0f }, + { 0.0f, 0.0f, 0.0f, 0.0f }, + { -2.02524603e-11f, 1.79435860e-07f, -2.60561875e-04f, -1.41761141e-02f }, + { -2.22463426e-13f, -1.55078698e-08f, 3.81675160e-04f, -7.30646033e-01f }, + { 6.72595954e-13f, -2.73059993e-08f, 4.24068546e-04f, -7.52204323e-01f }, +}; + +static void blackbody_temperature_to_rgb(float rgb[3], float t) +{ + if(t >= 12000.0f) { + rgb[0] = 0.826270103f; + rgb[1] = 0.994478524f; + rgb[2] = 1.56626022f; + } + else if(t < 965.0f) { + rgb[0] = 4.70366907f; + rgb[1] = 0.0f; + rgb[2] = 0.0f; + } + else { + int i = (t >= 6365.0f)? 5: + (t >= 3315.0f)? 4: + (t >= 1902.0f)? 3: + (t >= 1449.0f)? 2: + (t >= 1167.0f)? 1: 0; + + const float *r = blackbody_table_r[i]; + const float *g = blackbody_table_g[i]; + const float *b = blackbody_table_b[i]; + + const float t_inv = 1.0f / t; + rgb[0] = r[0] * t_inv + r[1] * t + r[2]; + rgb[1] = g[0] * t_inv + g[1] * t + g[2]; + rgb[2] = ((b[0] * t + b[1]) * t + b[2]) * t + b[3]; } - - xyz_sum = (x + y + z); - - xyz[0] = x / xyz_sum; - xyz[1] = y / xyz_sum; - xyz[2] = z / xyz_sum; } void blackbody_temperature_to_rgb_table(float *r_table, int width, float min, float max) { - int i, j = 0, dj = 1; - float rgb[3], xyz[3], lms[3], lms_w[3]; - float bb_temp; - - if (min < max) { - SWAP(float, min, max); - j = width - 1; - dj = -1; - } + for (int i = 0; i < width; i++) { + float temperature = min + (max - min) / (float)width * (float)i; - for (i = 0; i < width; i++, j += dj) { - bb_temp = min + (max - min) / (float)width * (float)i; + float rgb[3]; + blackbody_temperature_to_rgb(rgb, temperature); - /* integrate blackbody radiation spectrum to XYZ */ - spectrum_to_xyz(bb_temp, xyz); - - /* normalize highest temperature to white (in LMS system) */ - xyz_to_lms(xyz[0], xyz[1], xyz[2], &lms[0], &lms[1], &lms[2]); - - if (i == 0) { - lms_w[0] = 1.0f / lms[0]; - lms_w[1] = 1.0f / lms[1]; - lms_w[2] = 1.0f / lms[2]; - } - - mul_v3_v3(lms, lms_w); - - lms_to_xyz(lms[0], lms[1], lms[2], &xyz[0], &xyz[1], &xyz[2]); - - /* convert to RGB */ - xyz_to_rgb(xyz[0], xyz[1], xyz[2], &rgb[0], &rgb[1], &rgb[2], BLI_XYZ_CIE); - constrain_rgb(&rgb[0], &rgb[1], &rgb[2]); - normalize_rgb(rgb); - - copy_v3_v3(&r_table[(j << 2)], rgb); - - if (rgb[2] > 0.1f) - r_table[(j << 2) + 3] = rgb[2]; - else - r_table[(j << 2) + 3] = 0.0f; + copy_v3_v3(&r_table[i * 4], rgb); + r_table[i * 4 + 3] = 0.0f; } } diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 78797e603be..171af1c8264 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -3813,7 +3813,7 @@ void orthographic_m4(float matrix[4][4], const float left, const float right, co matrix[3][0] = -(right + left) / Xdelta; matrix[1][1] = 2.0f / Ydelta; matrix[3][1] = -(top + bottom) / Ydelta; - matrix[2][2] = -2.0f / Zdelta; /* note: negate Z */ + matrix[2][2] = -2.0f / Zdelta; /* note: negate Z */ matrix[3][2] = -(farClip + nearClip) / Zdelta; } @@ -3832,7 +3832,7 @@ void perspective_m4(float mat[4][4], const float left, const float right, const } mat[0][0] = nearClip * 2.0f / Xdelta; mat[1][1] = nearClip * 2.0f / Ydelta; - mat[2][0] = (right + left) / Xdelta; /* note: negate Z */ + mat[2][0] = (right + left) / Xdelta; /* note: negate Z */ mat[2][1] = (top + bottom) / Ydelta; mat[2][2] = -(farClip + nearClip) / Zdelta; mat[2][3] = -1.0f; @@ -4968,6 +4968,18 @@ int is_quad_flip_v3(const float v1[3], const float v2[3], const float v3[3], con return ret; } +bool is_quad_flip_v3_first_third_fast(const float v1[3], const float v2[3], const float v3[3], const float v4[3]) +{ + float d_12[3], d_13[3], d_14[3]; + float cross_a[3], cross_b[3]; + sub_v3_v3v3(d_12, v2, v1); + sub_v3_v3v3(d_13, v3, v1); + sub_v3_v3v3(d_14, v4, v1); + cross_v3_v3v3(cross_a, d_12, d_13); + cross_v3_v3v3(cross_b, d_14, d_13); + return dot_v3v3(cross_a, cross_b) > 0.0f; +} + /** * Return the value which the distance between points will need to be scaled by, * to define a handle, given both points are on a perfect circle. diff --git a/source/blender/blenlib/intern/math_vector.c b/source/blender/blenlib/intern/math_vector.c index 05562502278..d6e48fa59e7 100644 --- a/source/blender/blenlib/intern/math_vector.c +++ b/source/blender/blenlib/intern/math_vector.c @@ -890,6 +890,8 @@ void rotate_normalized_v3_v3v3fl(float out[3], const float p[3], const float axi void rotate_v3_v3v3fl(float r[3], const float p[3], const float axis[3], const float angle) { + BLI_assert(r != p); + float axis_n[3]; normalize_v3_v3(axis_n, axis); diff --git a/source/blender/blenlib/intern/polyfill2d.c b/source/blender/blenlib/intern/polyfill_2d.c index 018e2f9be5a..8c0870f0c07 100644 --- a/source/blender/blenlib/intern/polyfill2d.c +++ b/source/blender/blenlib/intern/polyfill_2d.c @@ -18,7 +18,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/polyfill2d.c +/** \file blender/blenlib/intern/polyfill_2d.c * \ingroup bli * * An ear clipping algorithm to triangulate single boundary polygons. @@ -53,7 +53,7 @@ #include "BLI_memarena.h" #include "BLI_alloca.h" -#include "BLI_polyfill2d.h" /* own include */ +#include "BLI_polyfill_2d.h" /* own include */ #include "BLI_strict_flags.h" diff --git a/source/blender/blenlib/intern/polyfill2d_beautify.c b/source/blender/blenlib/intern/polyfill_2d_beautify.c index c0c95da5c63..17f3205aaff 100644 --- a/source/blender/blenlib/intern/polyfill2d_beautify.c +++ b/source/blender/blenlib/intern/polyfill_2d_beautify.c @@ -18,7 +18,7 @@ * ***** END GPL LICENSE BLOCK ***** */ -/** \file blender/blenlib/intern/polyfill2d_beautify.c +/** \file blender/blenlib/intern/polyfill_2d_beautify.c * \ingroup bli * * This function is to improve the tessellation resulting from polyfill2d, @@ -44,7 +44,7 @@ #include "BLI_memarena.h" #include "BLI_heap.h" -#include "BLI_polyfill2d_beautify.h" /* own include */ +#include "BLI_polyfill_2d_beautify.h" /* own include */ #include "BLI_strict_flags.h" @@ -265,7 +265,8 @@ static void polyedge_rotate( struct HalfEdge *e) { /** CCW winding, rotate internal edge to new vertical state. - * <pre> + * + * \code{.unparsed} * Before After * X X * / \ /|\ @@ -276,7 +277,7 @@ static void polyedge_rotate( * e2\ /e1 e2\ | /e1 * \ / \|/ * X X - * </pre> + * \endcode */ struct HalfEdge *ed[6]; uint ed_index[6]; @@ -401,7 +402,7 @@ void BLI_polyfill_beautify( } while (BLI_heap_is_empty(eheap) == false) { - struct HalfEdge *e = BLI_heap_popmin(eheap); + struct HalfEdge *e = BLI_heap_pop_min(eheap); eheap_table[e->base_index] = NULL; polyedge_rotate(half_edges, e); diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c index 1a178db1413..700524965f0 100644 --- a/source/blender/blenlib/intern/rand.c +++ b/source/blender/blenlib/intern/rand.c @@ -386,6 +386,8 @@ void BLI_halton_1D(unsigned int prime, double offset, int n, double *r) { const double invprime = 1.0 / (double)prime; + *r = 0.0; + for (int s = 0; s < n; s++) { *r = halton_ex(invprime, &offset); } @@ -395,6 +397,8 @@ void BLI_halton_2D(unsigned int prime[2], double offset[2], int n, double *r) { const double invprimes[2] = {1.0 / (double)prime[0], 1.0 / (double)prime[1]}; + r[0] = r[1] = 0.0; + for (int s = 0; s < n; s++) { for (int i = 0; i < 2; i++) { r[i] = halton_ex(invprimes[i], &offset[i]); @@ -406,6 +410,8 @@ void BLI_halton_3D(unsigned int prime[3], double offset[3], int n, double *r) { const double invprimes[3] = {1.0 / (double)prime[0], 1.0 / (double)prime[1], 1.0 / (double)prime[2]}; + r[0] = r[1] = r[2] = 0.0; + for (int s = 0; s < n; s++) { for (int i = 0; i < 3; i++) { r[i] = halton_ex(invprimes[i], &offset[i]); diff --git a/source/blender/blenlib/intern/smallhash.c b/source/blender/blenlib/intern/smallhash.c index 1b862ffe005..56d77abed24 100644 --- a/source/blender/blenlib/intern/smallhash.c +++ b/source/blender/blenlib/intern/smallhash.c @@ -310,7 +310,7 @@ bool BLI_smallhash_haskey(const SmallHash *sh, uintptr_t key) return (e != NULL); } -int BLI_smallhash_count(const SmallHash *sh) +int BLI_smallhash_len(const SmallHash *sh) { return (int)sh->nentries; } diff --git a/source/blender/blenlib/intern/sort_utils.c b/source/blender/blenlib/intern/sort_utils.c index c75e8e7455f..2d55e77b98b 100644 --- a/source/blender/blenlib/intern/sort_utils.c +++ b/source/blender/blenlib/intern/sort_utils.c @@ -37,6 +37,10 @@ struct SortAnyByInt { int sort_value; }; +struct SortAnyByPtr { + const void *sort_value; +}; + int BLI_sortutil_cmp_float(const void *a_, const void *b_) { const struct SortAnyByFloat *a = a_; @@ -72,3 +76,21 @@ int BLI_sortutil_cmp_int_reverse(const void *a_, const void *b_) else if (a->sort_value > b->sort_value) return -1; else return 0; } + +int BLI_sortutil_cmp_ptr(const void *a_, const void *b_) +{ + const struct SortAnyByPtr *a = a_; + const struct SortAnyByPtr *b = b_; + if (a->sort_value > b->sort_value) return 1; + else if (a->sort_value < b->sort_value) return -1; + else return 0; +} + +int BLI_sortutil_cmp_ptr_reverse(const void *a_, const void *b_) +{ + const struct SortAnyByPtr *a = a_; + const struct SortAnyByPtr *b = b_; + if (a->sort_value < b->sort_value) return 1; + else if (a->sort_value > b->sort_value) return -1; + else return 0; +} diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index c08329ef34f..f69b35ce5a9 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -185,7 +185,7 @@ size_t BLI_file_size(const char *path) } /** - * Returns the st_mode from statting the specified path name, or 0 if it couldn't be statted + * Returns the st_mode from stat-ing the specified path name, or 0 if stat fails * (most likely doesn't exist or no access). */ int BLI_exists(const char *name) diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 6022732025b..1a6fd082e95 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -208,7 +208,7 @@ size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src) } /** - * Portable replacement for #vsnprintf + * Portable replacement for `vsnprintf`. */ size_t BLI_vsnprintf(char *__restrict buffer, size_t maxncpy, const char *__restrict format, va_list arg) { @@ -503,7 +503,7 @@ int BLI_strcaseeq(const char *a, const char *b) } /** - * Portable replacement for #strcasestr (not available in MSVC) + * Portable replacement for `strcasestr` (not available in MSVC) */ char *BLI_strcasestr(const char *s, const char *find) { @@ -931,13 +931,13 @@ size_t BLI_str_partition_ex( if (end) { if (from_right) { for (tmp = end - 1; (tmp >= str) && (*tmp != *d); tmp--); - if (tmp < str) { + if (tmp < str) { tmp = NULL; } } else { tmp = func(str, *d); - if (tmp >= end) { + if (tmp >= end) { tmp = NULL; } } diff --git a/source/blender/blenlib/intern/task.c b/source/blender/blenlib/intern/task.c index 05cfe4f3972..5d3c6b35ac1 100644 --- a/source/blender/blenlib/intern/task.c +++ b/source/blender/blenlib/intern/task.c @@ -696,7 +696,7 @@ static TaskPool *task_pool_create_ex(TaskScheduler *scheduler, * and malloc could be non-thread safe at this point because * no other jobs are running. */ - BLI_begin_threaded_malloc(); + BLI_threaded_malloc_begin(); return pool; } @@ -763,7 +763,7 @@ void BLI_task_pool_free(TaskPool *pool) MEM_freeN(pool); - BLI_end_threaded_malloc(); + BLI_threaded_malloc_end(); } BLI_INLINE bool task_can_use_local_queues(TaskPool *pool, int thread_id) @@ -1310,11 +1310,11 @@ static void parallel_mempool_func( /** * This function allows to parallelize for loops over Mempool items. * - * \param pool The iterable BLI_mempool to loop over. - * \param userdata Common userdata passed to all instances of \a func. - * \param func Callback function. - * \param use_threading If \a true, actually split-execute loop in threads, else just do a sequential forloop - * (allows caller to use any kind of test to switch on parallelization or not). + * \param mempool: The iterable BLI_mempool to loop over. + * \param userdata: Common userdata passed to all instances of \a func. + * \param func: Callback function. + * \param use_threading: If \a true, actually split-execute loop in threads, else just do a sequential for loop + * (allows caller to use any kind of test to switch on parallelization or not). * * \note There is no static scheduling here. */ @@ -1329,7 +1329,7 @@ void BLI_task_parallel_mempool( ParallelMempoolState state; int i, num_threads, num_tasks; - if (BLI_mempool_count(mempool) == 0) { + if (BLI_mempool_len(mempool) == 0) { return; } diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c index 2f961701801..761f3982e28 100644 --- a/source/blender/blenlib/intern/threads.c +++ b/source/blender/blenlib/intern/threads.c @@ -83,13 +83,13 @@ static TaskScheduler *task_scheduler = NULL; * int maxthreads = 2; * int cont = 1; * - * BLI_init_threads(&lb, do_something_func, maxthreads); + * BLI_threadpool_init(&lb, do_something_func, maxthreads); * * while (cont) { * if (BLI_available_threads(&lb) && !(escape loop event)) { * // get new job (data pointer) * // tag job 'processed - * BLI_insert_thread(&lb, job); + * BLI_threadpool_insert(&lb, job); * } * else PIL_sleep_ms(50); * @@ -98,7 +98,7 @@ static TaskScheduler *task_scheduler = NULL; * for (go over all jobs) * if (job is ready) { * if (job was not removed) { - * BLI_remove_thread(&lb, job); + * BLI_threadpool_remove(&lb, job); * } * } * else cont = 1; @@ -110,7 +110,7 @@ static TaskScheduler *task_scheduler = NULL; * } * } * - * BLI_end_threads(&lb); + * BLI_threadpool_end(&lb); * ************************************************ */ static SpinLock _malloc_lock; @@ -183,7 +183,7 @@ TaskScheduler *BLI_task_scheduler_get(void) * problem otherwise: scene render will kill of the mutex! */ -void BLI_init_threads(ListBase *threadbase, void *(*do_thread)(void *), int tot) +void BLI_threadpool_init(ListBase *threadbase, void *(*do_thread)(void *), int tot) { int a; @@ -228,7 +228,7 @@ int BLI_available_threads(ListBase *threadbase) } /* returns thread number, for sample patterns or threadsafe tables */ -int BLI_available_thread_index(ListBase *threadbase) +int BLI_threadpool_available_thread_index(ListBase *threadbase) { ThreadSlot *tslot; int counter = 0; @@ -258,7 +258,7 @@ int BLI_thread_is_main(void) return pthread_equal(pthread_self(), mainid); } -void BLI_insert_thread(ListBase *threadbase, void *callerdata) +void BLI_threadpool_insert(ListBase *threadbase, void *callerdata) { ThreadSlot *tslot; @@ -273,7 +273,7 @@ void BLI_insert_thread(ListBase *threadbase, void *callerdata) printf("ERROR: could not insert thread slot\n"); } -void BLI_remove_thread(ListBase *threadbase, void *callerdata) +void BLI_threadpool_remove(ListBase *threadbase, void *callerdata) { ThreadSlot *tslot; @@ -286,7 +286,7 @@ void BLI_remove_thread(ListBase *threadbase, void *callerdata) } } -void BLI_remove_thread_index(ListBase *threadbase, int index) +void BLI_threadpool_remove_index(ListBase *threadbase, int index) { ThreadSlot *tslot; int counter = 0; @@ -301,7 +301,7 @@ void BLI_remove_thread_index(ListBase *threadbase, int index) } } -void BLI_remove_threads(ListBase *threadbase) +void BLI_threadpool_clear(ListBase *threadbase) { ThreadSlot *tslot; @@ -314,7 +314,7 @@ void BLI_remove_threads(ListBase *threadbase) } } -void BLI_end_threads(ListBase *threadbase) +void BLI_threadpool_end(ListBase *threadbase) { ThreadSlot *tslot; @@ -418,12 +418,12 @@ static ThreadMutex *global_mutex_from_type(const int type) } } -void BLI_lock_thread(int type) +void BLI_thread_lock(int type) { pthread_mutex_lock(global_mutex_from_type(type)); } -void BLI_unlock_thread(int type) +void BLI_thread_unlock(int type) { pthread_mutex_unlock(global_mutex_from_type(type)); } @@ -773,12 +773,12 @@ void *BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms) return work; } -int BLI_thread_queue_size(ThreadQueue *queue) +int BLI_thread_queue_len(ThreadQueue *queue) { int size; pthread_mutex_lock(&queue->mutex); - size = BLI_gsqueue_size(queue->queue); + size = BLI_gsqueue_len(queue->queue); pthread_mutex_unlock(&queue->mutex); return size; @@ -819,7 +819,7 @@ void BLI_thread_queue_wait_finish(ThreadQueue *queue) /* ************************************************ */ -void BLI_begin_threaded_malloc(void) +void BLI_threaded_malloc_begin(void) { unsigned int level = atomic_fetch_and_add_u(&thread_levels, 1); if (level == 0) { @@ -832,7 +832,7 @@ void BLI_begin_threaded_malloc(void) } } -void BLI_end_threaded_malloc(void) +void BLI_threaded_malloc_end(void) { unsigned int level = atomic_sub_and_fetch_u(&thread_levels, 1); if (level == 0) { diff --git a/source/blender/blenlib/intern/voronoi.c b/source/blender/blenlib/intern/voronoi_2d.c index e0cbe278ffb..40e98d5914c 100644 --- a/source/blender/blenlib/intern/voronoi.c +++ b/source/blender/blenlib/intern/voronoi_2d.c @@ -23,20 +23,18 @@ * ***** END GPL LICENSE BLOCK ***** */ -/* +/** \file blender/blenlib/intern/voronoi_2d.c + * \ingroup bli + * * Fortune's algorithm implemented using explanation and some code snippets from * http://blog.ivank.net/fortunes-algorithm-and-implementation.html */ -/** \file blender/blenlib/intern/voronoi.c - * \ingroup bli - */ - #include "MEM_guardedalloc.h" #include "BLI_listbase.h" #include "BLI_math.h" -#include "BLI_voronoi.h" +#include "BLI_voronoi_2d.h" #include "BLI_utildefines.h" #define VORONOI_EPS 1e-2f |