diff options
Diffstat (limited to 'source/blender/blenlib')
31 files changed, 466 insertions, 140 deletions
diff --git a/source/blender/blenlib/BLI_any.hh b/source/blender/blenlib/BLI_any.hh index ca3d5756c52..e80dad82d01 100644 --- a/source/blender/blenlib/BLI_any.hh +++ b/source/blender/blenlib/BLI_any.hh @@ -184,7 +184,7 @@ class Any { } /** - * \note: Only needed because the template below does not count as copy assignment operator. + * \note Only needed because the template below does not count as copy assignment operator. */ Any &operator=(const Any &other) { diff --git a/source/blender/blenlib/BLI_bitmap.h b/source/blender/blenlib/BLI_bitmap.h index ca11cd6574e..19d8525311c 100644 --- a/source/blender/blenlib/BLI_bitmap.h +++ b/source/blender/blenlib/BLI_bitmap.h @@ -15,7 +15,7 @@ extern "C" { typedef unsigned int BLI_bitmap; -/* warning: the bitmap does not keep track of its own size or check +/* WARNING: the bitmap does not keep track of its own size or check * for out-of-bounds access */ /* internal use */ diff --git a/source/blender/blenlib/BLI_color_mix.hh b/source/blender/blenlib/BLI_color_mix.hh index 4989ddc609e..322da2bf112 100644 --- a/source/blender/blenlib/BLI_color_mix.hh +++ b/source/blender/blenlib/BLI_color_mix.hh @@ -1042,7 +1042,7 @@ BLI_INLINE Color BLI_mix_colors(const IMB_BlendMode tool, case IMB_BLEND_COLOR: return mix_color<Color, Traits>(a, b, alpha); default: - BLI_assert(0); + BLI_assert_unreachable(); return Color(0, 0, 0, 0); } } diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h index 3ce2b90e729..063e60ecf03 100644 --- a/source/blender/blenlib/BLI_fileops.h +++ b/source/blender/blenlib/BLI_fileops.h @@ -178,7 +178,7 @@ void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries); * Convert given entry's size into human-readable strings. */ void BLI_filelist_entry_size_to_string(const struct stat *st, - uint64_t sz, + uint64_t st_size_fallback, bool compact, char r_size[FILELIST_DIRENTRY_SIZE_LEN]); /** @@ -215,10 +215,10 @@ void BLI_filelist_entry_datetime_to_string(const struct stat *st, /** \name Files * \{ */ -FILE *BLI_fopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -void *BLI_gzopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -int BLI_open(const char *filename, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -int BLI_access(const char *filename, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +FILE *BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +void *BLI_gzopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); /** * Returns true if the file with the specified name can be written. @@ -226,7 +226,7 @@ int BLI_access(const char *filename, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONN * to the real UID and GID of the process, not its effective UID and GID. * This shouldn't matter for Blender, which is not going to run privileged anyway. */ -bool BLI_file_is_writable(const char *file) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +bool BLI_file_is_writable(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); /** * Creates the file with nothing in it, or updates its last-modified date if it already exists. * Returns true if successful (like the unix touch command). @@ -319,7 +319,7 @@ const char *BLI_expand_tilde(const char *path_with_tilde); # define O_BINARY 0 # endif #else -void BLI_get_short_name(char short_name[256], const char *filename); +void BLI_get_short_name(char short_name[256], const char *filepath); #endif /** \} */ diff --git a/source/blender/blenlib/BLI_float3x3.hh b/source/blender/blenlib/BLI_float3x3.hh new file mode 100644 index 00000000000..62478556d9b --- /dev/null +++ b/source/blender/blenlib/BLI_float3x3.hh @@ -0,0 +1,192 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma once + +#include <cmath> +#include <cstdint> + +#include "BLI_assert.h" +#include "BLI_math_base.h" +#include "BLI_math_matrix.h" +#include "BLI_math_vec_types.hh" +#include "BLI_math_vector.h" + +namespace blender { + +struct float3x3 { + /* A 3x3 matrix in column major order. */ + float values[3][3]; + + float3x3() = default; + + float3x3(const float *matrix) + { + memcpy(values, matrix, sizeof(float) * 3 * 3); + } + + float3x3(const float matrix[3][3]) : float3x3(static_cast<const float *>(matrix[0])) + { + } + + static float3x3 zero() + { + float3x3 result; + zero_m3(result.values); + return result; + } + + static float3x3 identity() + { + float3x3 result; + unit_m3(result.values); + return result; + } + + static float3x3 from_translation(const float2 translation) + { + float3x3 result = identity(); + result.values[2][0] = translation.x; + result.values[2][1] = translation.y; + return result; + } + + static float3x3 from_rotation(float rotation) + { + float3x3 result = zero(); + const float cosine = std::cos(rotation); + const float sine = std::sin(rotation); + result.values[0][0] = cosine; + result.values[0][1] = sine; + result.values[1][0] = -sine; + result.values[1][1] = cosine; + result.values[2][2] = 1.0f; + return result; + } + + static float3x3 from_translation_rotation_scale(const float2 translation, + float rotation, + const float2 scale) + { + float3x3 result; + const float cosine = std::cos(rotation); + const float sine = std::sin(rotation); + result.values[0][0] = scale.x * cosine; + result.values[0][1] = scale.x * sine; + result.values[0][2] = 0.0f; + result.values[1][0] = scale.y * -sine; + result.values[1][1] = scale.y * cosine; + result.values[1][2] = 0.0f; + result.values[2][0] = translation.x; + result.values[2][1] = translation.y; + result.values[2][2] = 1.0f; + return result; + } + + static float3x3 from_normalized_axes(const float2 translation, + const float2 horizontal, + const float2 vertical) + { + BLI_ASSERT_UNIT_V2(horizontal); + BLI_ASSERT_UNIT_V2(vertical); + + float3x3 result; + result.values[0][0] = horizontal.x; + result.values[0][1] = horizontal.y; + result.values[0][2] = 0.0f; + result.values[1][0] = vertical.x; + result.values[1][1] = vertical.y; + result.values[1][2] = 0.0f; + result.values[2][0] = translation.x; + result.values[2][1] = translation.y; + result.values[2][2] = 1.0f; + return result; + } + + /* Construct a transformation that is pivoted around the given origin point. So for instance, + * from_origin_transformation(from_rotation(M_PI_2), float2(0.0f, 2.0f)) + * will construct a transformation representing a 90 degree rotation around the point (0, 2). */ + static float3x3 from_origin_transformation(const float3x3 &transformation, const float2 origin) + { + return from_translation(origin) * transformation * from_translation(-origin); + } + + operator float *() + { + return &values[0][0]; + } + + operator const float *() const + { + return &values[0][0]; + } + + float *operator[](const int64_t index) + { + BLI_assert(index >= 0); + BLI_assert(index < 3); + return &values[index][0]; + } + + const float *operator[](const int64_t index) const + { + BLI_assert(index >= 0); + BLI_assert(index < 3); + return &values[index][0]; + } + + using c_style_float3x3 = float[3][3]; + c_style_float3x3 &ptr() + { + return values; + } + + const c_style_float3x3 &ptr() const + { + return values; + } + + friend float3x3 operator*(const float3x3 &a, const float3x3 &b) + { + float3x3 result; + mul_m3_m3m3(result.values, a.values, b.values); + return result; + } + + void operator*=(const float3x3 &other) + { + mul_m3_m3_post(values, other.values); + } + + friend float2 operator*(const float3x3 &transformation, const float2 &vector) + { + float2 result; + mul_v2_m3v2(result, transformation.values, vector); + return result; + } + + friend float2 operator*(const float3x3 &transformation, const float (*vector)[2]) + { + return transformation * float2(vector); + } + + float3x3 transposed() const + { + float3x3 result; + transpose_m3_m3(result.values, values); + return result; + } + + float3x3 inverted() const + { + float3x3 result; + invert_m3_m3(result.values, values); + return result; + } + + friend bool operator==(const float3x3 &a, const float3x3 &b) + { + return equals_m3m3(a.values, b.values); + } +}; + +} // namespace blender diff --git a/source/blender/blenlib/BLI_length_parameterize.hh b/source/blender/blenlib/BLI_length_parameterize.hh index 6fe1c6513a2..f13641c3a65 100644 --- a/source/blender/blenlib/BLI_length_parameterize.hh +++ b/source/blender/blenlib/BLI_length_parameterize.hh @@ -17,7 +17,7 @@ namespace blender::length_parameterize { * Return the size of the necessary lengths array for a group of points, taking into account the * possible last cyclic segment. * - * \note This is the same as #bke::curves::curve_segment_size. + * \note This is the same as #bke::curves::curve_segment_num. */ inline int lengths_num(const int points_num, const bool cyclic) { diff --git a/source/blender/blenlib/BLI_math_rotation.h b/source/blender/blenlib/BLI_math_rotation.h index 7d10e52f699..192ad482a69 100644 --- a/source/blender/blenlib/BLI_math_rotation.h +++ b/source/blender/blenlib/BLI_math_rotation.h @@ -7,6 +7,7 @@ * \ingroup bli */ +#include "BLI_math_base.h" #include "BLI_utildefines.h" #include "DNA_vec_types.h" diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index 0344622e81d..15926e8f2d2 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -307,7 +307,7 @@ void BLI_str_format_byte_unit(char dst[15], long long int bytes, bool base_10) A * * Length of 7 is the maximum of the resulting string, for example, `-15.5K\0`. */ -void BLI_str_format_attribute_domain_size(char dst[7], int number_to_format) ATTR_NONNULL(); +void BLI_str_format_decimal_unit(char dst[7], int number_to_format) ATTR_NONNULL(); /** * Compare two strings without regard to case. * diff --git a/source/blender/blenlib/BLI_task.hh b/source/blender/blenlib/BLI_task.hh index d87a86ce696..904dea66f7a 100644 --- a/source/blender/blenlib/BLI_task.hh +++ b/source/blender/blenlib/BLI_task.hh @@ -77,17 +77,19 @@ Value parallel_reduce(IndexRange range, const Reduction &reduction) { #ifdef WITH_TBB - return tbb::parallel_reduce( - tbb::blocked_range<int64_t>(range.first(), range.one_after_last(), grain_size), - identity, - [&](const tbb::blocked_range<int64_t> &subrange, const Value &ident) { - return function(IndexRange(subrange.begin(), subrange.size()), ident); - }, - reduction); + if (range.size() >= grain_size) { + return tbb::parallel_reduce( + tbb::blocked_range<int64_t>(range.first(), range.one_after_last(), grain_size), + identity, + [&](const tbb::blocked_range<int64_t> &subrange, const Value &ident) { + return function(IndexRange(subrange.begin(), subrange.size()), ident); + }, + reduction); + } #else UNUSED_VARS(grain_size, reduction); - return function(range, identity); #endif + return function(range, identity); } /** diff --git a/source/blender/blenlib/BLI_virtual_array.hh b/source/blender/blenlib/BLI_virtual_array.hh index 7aa221f62ce..6efd1d6d769 100644 --- a/source/blender/blenlib/BLI_virtual_array.hh +++ b/source/blender/blenlib/BLI_virtual_array.hh @@ -463,7 +463,7 @@ template<typename T, typename GetFunc> class VArrayImpl_For_Func final : public }; /** - * \note: This is `final` so that #may_have_ownership can be implemented reliably. + * \note This is `final` so that #may_have_ownership can be implemented reliably. */ template<typename StructT, typename ElemT, @@ -725,7 +725,7 @@ template<typename T> class VArrayCommon { /** * Get the element at a specific index. - * \note: This can't return a reference because the value may be computed on the fly. This also + * \note This can't return a reference because the value may be computed on the fly. This also * implies that one can not use this method for assignments. */ T operator[](const int64_t index) const diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index e0f28522d6c..109230ebfa7 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -79,8 +79,8 @@ set(SRC intern/kdtree_3d.c intern/kdtree_4d.c intern/lasso_2d.c - intern/listbase.c intern/length_parameterize.cc + intern/listbase.c intern/math_base.c intern/math_base_inline.c intern/math_base_safe_inline.c @@ -199,6 +199,7 @@ set(SRC BLI_fileops.hh BLI_fileops_types.h BLI_filereader.h + BLI_float3x3.hh BLI_float4x4.hh BLI_fnmatch.h BLI_function_ref.hh @@ -274,8 +275,8 @@ set(SRC BLI_multi_value_map.hh BLI_noise.h BLI_noise.hh - BLI_path_util.h BLI_parameter_pack_utils.hh + BLI_path_util.h BLI_polyfill_2d.h BLI_polyfill_2d_beautify.h BLI_probing_strategies.hh @@ -431,6 +432,7 @@ if(WITH_GTESTS) tests/BLI_edgehash_test.cc tests/BLI_expr_pylike_eval_test.cc tests/BLI_fileops_test.cc + tests/BLI_float3x3_test.cc tests/BLI_function_ref_test.cc tests/BLI_generic_array_test.cc tests/BLI_generic_span_test.cc diff --git a/source/blender/blenlib/intern/BLI_assert.c b/source/blender/blenlib/intern/BLI_assert.c index 2ebeba43dbe..96c16b47214 100644 --- a/source/blender/blenlib/intern/BLI_assert.c +++ b/source/blender/blenlib/intern/BLI_assert.c @@ -40,7 +40,7 @@ void _BLI_assert_abort(void) /* Wrap to remove 'noreturn' attribute since this suppresses missing return statements, * allowing changes to debug builds to accidentally to break release builds. * - * For example `BLI_assert(0);` at the end of a function that returns a value, + * For example `BLI_assert_unreachable();` at the end of a function that returns a value, * will hide that it's missing a return. */ abort(); diff --git a/source/blender/blenlib/intern/BLI_filelist.c b/source/blender/blenlib/intern/BLI_filelist.c index 76fc5b6342a..c6178ebb3a0 100644 --- a/source/blender/blenlib/intern/BLI_filelist.c +++ b/source/blender/blenlib/intern/BLI_filelist.c @@ -237,7 +237,7 @@ unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_ } void BLI_filelist_entry_size_to_string(const struct stat *st, - const uint64_t sz, + const uint64_t st_size_fallback, /* Used to change MB -> M, etc. - is that really useful? */ const bool UNUSED(compact), char r_size[FILELIST_DIRENTRY_SIZE_LEN]) @@ -247,7 +247,7 @@ void BLI_filelist_entry_size_to_string(const struct stat *st, * will buy us some time until files get bigger than 4GB or until * everyone starts using __USE_FILE_OFFSET64 or equivalent. */ - double size = (double)(st ? st->st_size : sz); + double size = (double)(st ? st->st_size : st_size_fallback); #ifdef WIN32 BLI_str_format_byte_unit(r_size, size, false); #else diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index 57e05233efa..e6ff5bab8a1 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -173,7 +173,7 @@ BLI_INLINE uint ghash_find_next_bucket_index(const GHash *gh, uint curr_bucket) return curr_bucket; } } - BLI_assert(0); + BLI_assert_unreachable(); return 0; } diff --git a/source/blender/blenlib/intern/BLI_kdopbvh.c b/source/blender/blenlib/intern/BLI_kdopbvh.c index 0f52c84c45e..62bf17bd415 100644 --- a/source/blender/blenlib/intern/BLI_kdopbvh.c +++ b/source/blender/blenlib/intern/BLI_kdopbvh.c @@ -893,7 +893,7 @@ BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis) } else { /* should never happen! */ - BLI_assert(0); + BLI_assert_unreachable(); goto fail; } diff --git a/source/blender/blenlib/intern/array_utils.c b/source/blender/blenlib/intern/array_utils.c index f1f1dd60ddf..a401059755d 100644 --- a/source/blender/blenlib/intern/array_utils.c +++ b/source/blender/blenlib/intern/array_utils.c @@ -53,7 +53,7 @@ void _bli_array_wrap(void *arr_v, uint arr_len, size_t arr_stride, int dir) memcpy(arr, buf, arr_stride); } else { - BLI_assert(0); + BLI_assert_unreachable(); } } diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c index 5ca6fe2efd0..3abd482d6b3 100644 --- a/source/blender/blenlib/intern/fileops.c +++ b/source/blender/blenlib/intern/fileops.c @@ -164,10 +164,10 @@ bool BLI_file_magic_is_zstd(const char header[4]) return false; } -bool BLI_file_is_writable(const char *filename) +bool BLI_file_is_writable(const char *filepath) { bool writable; - if (BLI_access(filename, W_OK) == 0) { + if (BLI_access(filepath, W_OK) == 0) { /* file exists and I can write to it */ writable = true; } @@ -178,7 +178,7 @@ bool BLI_file_is_writable(const char *filename) else { /* file doesn't exist -- check I can create it in parent directory */ char parent[FILE_MAX]; - BLI_split_dirfile(filename, parent, NULL, sizeof(parent), 0); + BLI_split_dirfile(filepath, parent, NULL, sizeof(parent), 0); #ifdef WIN32 /* windows does not have X_OK */ writable = BLI_access(parent, W_OK) == 0; @@ -224,38 +224,38 @@ static void callLocalErrorCallBack(const char *err) printf("%s\n", err); } -FILE *BLI_fopen(const char *filename, const char *mode) +FILE *BLI_fopen(const char *filepath, const char *mode) { - BLI_assert(!BLI_path_is_rel(filename)); + BLI_assert(!BLI_path_is_rel(filepath)); - return ufopen(filename, mode); + return ufopen(filepath, mode); } -void BLI_get_short_name(char short_name[256], const char *filename) +void BLI_get_short_name(char short_name[256], const char *filepath) { wchar_t short_name_16[256]; int i = 0; - UTF16_ENCODE(filename); + UTF16_ENCODE(filepath); - GetShortPathNameW(filename_16, short_name_16, 256); + GetShortPathNameW(filepath_16, short_name_16, 256); for (i = 0; i < 256; i++) { short_name[i] = (char)short_name_16[i]; } - UTF16_UN_ENCODE(filename); + UTF16_UN_ENCODE(filepath); } -void *BLI_gzopen(const char *filename, const char *mode) +void *BLI_gzopen(const char *filepath, const char *mode) { gzFile gzfile; - BLI_assert(!BLI_path_is_rel(filename)); + BLI_assert(!BLI_path_is_rel(filepath)); /* XXX: Creates file before transcribing the path. */ if (mode[0] == 'w') { - FILE *file = ufopen(filename, "a"); + FILE *file = ufopen(filepath, "a"); if (file == NULL) { /* File couldn't be opened, e.g. due to permission error. */ return NULL; @@ -266,15 +266,15 @@ void *BLI_gzopen(const char *filename, const char *mode) /* temporary #if until we update all libraries to 1.2.7 * for correct wide char path handling */ # if ZLIB_VERNUM >= 0x1270 - UTF16_ENCODE(filename); + UTF16_ENCODE(filepath); - gzfile = gzopen_w(filename_16, mode); + gzfile = gzopen_w(filepath_16, mode); - UTF16_UN_ENCODE(filename); + UTF16_UN_ENCODE(filepath); # else { char short_name[256]; - BLI_get_short_name(short_name, filename); + BLI_get_short_name(short_name, filepath); gzfile = gzopen(short_name, mode); } # endif @@ -282,18 +282,18 @@ void *BLI_gzopen(const char *filename, const char *mode) return gzfile; } -int BLI_open(const char *filename, int oflag, int pmode) +int BLI_open(const char *filepath, int oflag, int pmode) { - BLI_assert(!BLI_path_is_rel(filename)); + BLI_assert(!BLI_path_is_rel(filepath)); - return uopen(filename, oflag, pmode); + return uopen(filepath, oflag, pmode); } -int BLI_access(const char *filename, int mode) +int BLI_access(const char *filepath, int mode) { - BLI_assert(!BLI_path_is_rel(filename)); + BLI_assert(!BLI_path_is_rel(filepath)); - return uaccess(filename, mode); + return uaccess(filepath, mode); } static bool delete_soft(const wchar_t *path_16, const char **error_message) @@ -466,8 +466,8 @@ int BLI_move(const char *file, const char *to) int err; /* windows doesn't support moving to a directory - * it has to be 'mv filename filename' and not - * 'mv filename destination_directory' */ + * it has to be 'mv filepath filepath' and not + * 'mv filepath destination_directory' */ BLI_strncpy(str, to, sizeof(str)); /* points 'to' to a directory ? */ @@ -498,8 +498,8 @@ int BLI_copy(const char *file, const char *to) int err; /* windows doesn't support copying to a directory - * it has to be 'cp filename filename' and not - * 'cp filename destdir' */ + * it has to be 'cp filepath filepath' and not + * 'cp filepath destdir' */ BLI_strncpy(str, to, sizeof(str)); /* points 'to' to a directory ? */ @@ -587,7 +587,7 @@ int BLI_rename(const char *from, const char *to) return 0; } - /* make sure the filenames are different (case insensitive) before removing */ + /* Make sure `from` & `to` are different (case insensitive) before removing. */ if (BLI_exists(to) && BLI_strcasecmp(from, to)) { if (BLI_delete(to, false, false)) { return 1; @@ -728,9 +728,9 @@ static int recursive_operation(const char *startfrom, # ifdef __HAIKU__ { struct stat st_dir; - char filename[FILE_MAX]; - BLI_path_join(filename, sizeof(filename), startfrom, dirent->d_name, NULL); - lstat(filename, &st_dir); + char filepath[FILE_MAX]; + BLI_path_join(filepath, sizeof(filepath), startfrom, dirent->d_name, NULL); + lstat(filepath, &st_dir); is_dir = S_ISDIR(st_dir.st_mode); } # else @@ -903,32 +903,32 @@ static int delete_soft(const char *file, const char **error_message) } # endif -FILE *BLI_fopen(const char *filename, const char *mode) +FILE *BLI_fopen(const char *filepath, const char *mode) { - BLI_assert(!BLI_path_is_rel(filename)); + BLI_assert(!BLI_path_is_rel(filepath)); - return fopen(filename, mode); + return fopen(filepath, mode); } -void *BLI_gzopen(const char *filename, const char *mode) +void *BLI_gzopen(const char *filepath, const char *mode) { - BLI_assert(!BLI_path_is_rel(filename)); + BLI_assert(!BLI_path_is_rel(filepath)); - return gzopen(filename, mode); + return gzopen(filepath, mode); } -int BLI_open(const char *filename, int oflag, int pmode) +int BLI_open(const char *filepath, int oflag, int pmode) { - BLI_assert(!BLI_path_is_rel(filename)); + BLI_assert(!BLI_path_is_rel(filepath)); - return open(filename, oflag, pmode); + return open(filepath, oflag, pmode); } -int BLI_access(const char *filename, int mode) +int BLI_access(const char *filepath, int mode) { - BLI_assert(!BLI_path_is_rel(filename)); + BLI_assert(!BLI_path_is_rel(filepath)); - return access(filename, mode); + return access(filepath, mode); } int BLI_delete(const char *file, bool dir, bool recursive) diff --git a/source/blender/blenlib/intern/hash_md5.c b/source/blender/blenlib/intern/hash_md5.c index cc59662b6de..9da8c0a0941 100644 --- a/source/blender/blenlib/intern/hash_md5.c +++ b/source/blender/blenlib/intern/hash_md5.c @@ -271,7 +271,7 @@ static void *md5_read_ctx(const struct md5_ctx *ctx, void *resbuf) int BLI_hash_md5_stream(FILE *stream, void *resblock) { -#define BLOCKSIZE 4096 /* Important: must be a multiple of 64. */ +#define BLOCKSIZE 4096 /* IMPORTANT: must be a multiple of 64. */ struct md5_ctx ctx; md5_uint32 len[2]; char buffer[BLOCKSIZE + 72]; diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c index e2044955e48..3932e5eb051 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.c @@ -193,9 +193,19 @@ void BLI_listbases_swaplinks(ListBase *listbasea, ListBase *listbaseb, void *vli return; } + /* The reference to `linkc` assigns NULL, not a dangling pointer so it can be ignored. */ +#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 1201 /* gcc12.1+ only */ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdangling-pointer" +#endif + /* Temporary link to use as placeholder of the links positions */ BLI_insertlinkafter(listbasea, linka, &linkc); +#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 1201 /* gcc12.1+ only */ +# pragma GCC diagnostic pop +#endif + /* Bring linka into linkb position */ BLI_remlink(listbasea, linka); BLI_insertlinkafter(listbaseb, linkb, linka); diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index aeeaf47d813..52cbda82268 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -166,7 +166,7 @@ void ycc_to_rgb(float y, float cb, float cr, float *r_r, float *r_g, float *r_b, b = y + 1.772f * cb - 226.816f; break; default: - BLI_assert(0); + BLI_assert_unreachable(); break; } *r_r = r / 255.0f; @@ -238,7 +238,7 @@ void rgb_to_hsl(float r, float g, float b, float *r_h, float *r_s, float *r_l) { const float cmax = max_fff(r, g, b); const float cmin = min_fff(r, g, b); - float h, s, l = min_ff(1.0, (cmax + cmin) / 2.0f); + float h, s, l = min_ff(1.0f, (cmax + cmin) / 2.0f); if (cmax == cmin) { h = s = 0.0f; /* achromatic */ diff --git a/source/blender/blenlib/intern/math_matrix.c b/source/blender/blenlib/intern/math_matrix.c index f295734706f..ce9abc36cad 100644 --- a/source/blender/blenlib/intern/math_matrix.c +++ b/source/blender/blenlib/intern/math_matrix.c @@ -1496,7 +1496,7 @@ void orthogonalize_m3(float R[3][3], int axis) } break; default: - BLI_assert(0); + BLI_assert_unreachable(); break; } mul_v3_fl(R[0], size[0]); @@ -1580,7 +1580,7 @@ void orthogonalize_m4(float R[4][4], int axis) } break; default: - BLI_assert(0); + BLI_assert_unreachable(); break; } mul_v3_fl(R[0], size[0]); @@ -1654,7 +1654,7 @@ void orthogonalize_m3_stable(float R[3][3], int axis, bool normalize) orthogonalize_stable(R[2], R[0], R[1], normalize); break; default: - BLI_assert(0); + BLI_assert_unreachable(); break; } } @@ -1672,7 +1672,7 @@ void orthogonalize_m4_stable(float R[4][4], int axis, bool normalize) orthogonalize_stable(R[2], R[0], R[1], normalize); break; default: - BLI_assert(0); + BLI_assert_unreachable(); break; } } @@ -1734,7 +1734,7 @@ static bool orthogonalize_m3_zero_axes_impl(float *mat[3], const float unit_leng break; } default: { - BLI_assert(0); /* Unreachable! */ + BLI_assert_unreachable(); } } @@ -2338,7 +2338,7 @@ void rotate_m4(float mat[4][4], const char axis, const float angle) } break; default: - BLI_assert(0); + BLI_assert_unreachable(); break; } } diff --git a/source/blender/blenlib/intern/math_rotation.c b/source/blender/blenlib/intern/math_rotation.c index 4cd377b109e..92223bdf1d5 100644 --- a/source/blender/blenlib/intern/math_rotation.c +++ b/source/blender/blenlib/intern/math_rotation.c @@ -1143,7 +1143,7 @@ void axis_angle_to_mat3_single(float R[3][3], const char axis, const float angle R[2][2] = 1.0f; break; default: - BLI_assert(0); + BLI_assert_unreachable(); break; } } diff --git a/source/blender/blenlib/intern/scanfill.c b/source/blender/blenlib/intern/scanfill.c index bc07669687e..92fd7f5937b 100644 --- a/source/blender/blenlib/intern/scanfill.c +++ b/source/blender/blenlib/intern/scanfill.c @@ -871,7 +871,7 @@ unsigned int BLI_scanfill_calc_ex(ScanFillContext *sf_ctx, const int flag, const /* Similar code used elsewhere, but this checks for double ups * which historically this function supports so better not change */ - /* warning: this only gives stable direction with single polygons, + /* WARNING: this only gives stable direction with single polygons, * ideally we'd calculate connectivity and each polys normal, see T41047 */ const float *v_prev; diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 8387eb5f4f9..976b9a5cd02 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -1155,7 +1155,7 @@ void BLI_str_format_byte_unit(char dst[15], long long int bytes, const bool base BLI_strncpy(dst + len, base_10 ? units_base_10[order] : units_base_2[order], dst_len - len); } -void BLI_str_format_attribute_domain_size(char dst[7], int number_to_format) +void BLI_str_format_decimal_unit(char dst[7], int number_to_format) { float number_to_format_converted = number_to_format; int order = 0; diff --git a/source/blender/blenlib/intern/string_cursor_utf8.c b/source/blender/blenlib/intern/string_cursor_utf8.c index a6e68401368..7a23b4bb4ad 100644 --- a/source/blender/blenlib/intern/string_cursor_utf8.c +++ b/source/blender/blenlib/intern/string_cursor_utf8.c @@ -198,7 +198,7 @@ void BLI_str_cursor_step_utf8(const char *str, } } else { - BLI_assert(0); + BLI_assert_unreachable(); } } @@ -296,6 +296,6 @@ void BLI_str_cursor_step_utf32(const char32_t *str, } } else { - BLI_assert(0); + BLI_assert_unreachable(); } } diff --git a/source/blender/blenlib/intern/threads.cc b/source/blender/blenlib/intern/threads.cc index 70c1e701348..37fccf6f4fe 100644 --- a/source/blender/blenlib/intern/threads.cc +++ b/source/blender/blenlib/intern/threads.cc @@ -348,7 +348,7 @@ static ThreadMutex *global_mutex_from_type(const int type) case LOCK_VIEW3D: return &_view3d_lock; default: - BLI_assert(0); + BLI_assert_unreachable(); return nullptr; } } diff --git a/source/blender/blenlib/tests/BLI_array_store_test.cc b/source/blender/blenlib/tests/BLI_array_store_test.cc index aa7291e1b41..20e2a4d88f8 100644 --- a/source/blender/blenlib/tests/BLI_array_store_test.cc +++ b/source/blender/blenlib/tests/BLI_array_store_test.cc @@ -644,7 +644,7 @@ static void testbuffer_list_state_random_data(ListBase *lb, break; } default: - BLI_assert(0); + BLI_assert_unreachable(); } } } diff --git a/source/blender/blenlib/tests/BLI_float3x3_test.cc b/source/blender/blenlib/tests/BLI_float3x3_test.cc new file mode 100644 index 00000000000..d22993ee69e --- /dev/null +++ b/source/blender/blenlib/tests/BLI_float3x3_test.cc @@ -0,0 +1,119 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +#include "testing/testing.h" + +#include "BLI_float3x3.hh" +#include "BLI_math_base.h" +#include "BLI_math_vec_types.hh" + +namespace blender::tests { + +TEST(float3x3, Identity) +{ + float2 point(1.0f, 2.0f); + float3x3 transformation = float3x3::identity(); + float2 result = transformation * point; + EXPECT_EQ(result, point); +} + +TEST(float3x3, Translation) +{ + float2 point(1.0f, 2.0f); + float3x3 transformation = float3x3::from_translation(float2(5.0f, 3.0f)); + float2 result = transformation * point; + EXPECT_FLOAT_EQ(result[0], 6.0f); + EXPECT_FLOAT_EQ(result[1], 5.0f); +} + +TEST(float3x3, Rotation) +{ + float2 point(1.0f, 2.0f); + float3x3 transformation = float3x3::from_rotation(M_PI_2); + float2 result = transformation * point; + EXPECT_FLOAT_EQ(result[0], -2.0f); + EXPECT_FLOAT_EQ(result[1], 1.0f); +} + +TEST(float3x3, TranslationRotationScale) +{ + float2 point(1.0f, 2.0f); + float3x3 transformation = float3x3::from_translation_rotation_scale( + float2(1.0f, 3.0f), M_PI_2, float2(2.0f, 3.0f)); + float2 result = transformation * point; + EXPECT_FLOAT_EQ(result[0], -5.0f); + EXPECT_FLOAT_EQ(result[1], 5.0f); +} + +TEST(float3x3, NormalizedAxes) +{ + float2 point(1.0f, 2.0f); + + /* The horizontal is aligned with (1, 1) and vertical is aligned with (-1, 1), in other words, a + * Pi / 4 rotation. */ + float value = std::sqrt(2.0f) / 2.0f; + float3x3 transformation = float3x3::from_normalized_axes( + float2(1.0f, 3.0f), float2(value), float2(-value, value)); + float2 result = transformation * point; + + float3x3 expected_transformation = float3x3::from_translation_rotation_scale( + float2(1.0f, 3.0f), M_PI_4, float2(1.0f)); + float2 expected = expected_transformation * point; + + EXPECT_FLOAT_EQ(result[0], expected[0]); + EXPECT_FLOAT_EQ(result[1], expected[1]); +} + +TEST(float3x3, PostTransformationMultiplication) +{ + float2 point(1.0f, 2.0f); + float3x3 translation = float3x3::from_translation(float2(5.0f, 3.0f)); + float3x3 rotation = float3x3::from_rotation(M_PI_2); + float3x3 transformation = translation * rotation; + float2 result = transformation * point; + EXPECT_FLOAT_EQ(result[0], 3.0f); + EXPECT_FLOAT_EQ(result[1], 4.0f); +} + +TEST(float3x3, PreTransformationMultiplication) +{ + float2 point(1.0f, 2.0f); + float3x3 translation = float3x3::from_translation(float2(5.0f, 3.0f)); + float3x3 rotation = float3x3::from_rotation(M_PI_2); + float3x3 transformation = rotation * translation; + float2 result = transformation * point; + EXPECT_FLOAT_EQ(result[0], -5.0f); + EXPECT_FLOAT_EQ(result[1], 6.0f); +} + +TEST(float3x3, TransformationMultiplicationAssignment) +{ + float2 point(1.0f, 2.0f); + float3x3 transformation = float3x3::from_translation(float2(5.0f, 3.0f)); + transformation *= float3x3::from_rotation(M_PI_2); + float2 result = transformation * point; + EXPECT_FLOAT_EQ(result[0], 3.0f); + EXPECT_FLOAT_EQ(result[1], 4.0f); +} + +TEST(float3x3, Inverted) +{ + float2 point(1.0f, 2.0f); + float3x3 transformation = float3x3::from_translation_rotation_scale( + float2(1.0f, 3.0f), M_PI_4, float2(1.0f)); + transformation *= transformation.inverted(); + float2 result = transformation * point; + EXPECT_FLOAT_EQ(result[0], 1.0f); + EXPECT_FLOAT_EQ(result[1], 2.0f); +} + +TEST(float3x3, Origin) +{ + float2 point(1.0f, 2.0f); + float3x3 rotation = float3x3::from_rotation(M_PI_2); + float3x3 transformation = float3x3::from_origin_transformation(rotation, float2(0.0f, 2.0f)); + float2 result = transformation * point; + EXPECT_FLOAT_EQ(result[0], 0.0f); + EXPECT_FLOAT_EQ(result[1], 3.0f); +} + +} // namespace blender::tests diff --git a/source/blender/blenlib/tests/BLI_path_util_test.cc b/source/blender/blenlib/tests/BLI_path_util_test.cc index bfd297214c0..4f6f4a5c413 100644 --- a/source/blender/blenlib/tests/BLI_path_util_test.cc +++ b/source/blender/blenlib/tests/BLI_path_util_test.cc @@ -663,7 +663,7 @@ TEST(path_util, PathContains) EXPECT_TRUE(BLI_path_contains("/some/path", "/some/path/inside")) << "A path contains its subdirectory"; EXPECT_TRUE(BLI_path_contains("/some/path", "/some/path/../path/inside")) - << "Paths should be normalised"; + << "Paths should be normalized"; EXPECT_TRUE(BLI_path_contains("C:\\some\\path", "C:\\some\\path\\inside")) << "Windows paths should be supported as well"; @@ -672,7 +672,7 @@ TEST(path_util, PathContains) EXPECT_FALSE(BLI_path_contains("/some/path", "/")) << "Root directory not be contained in a subdirectory"; EXPECT_FALSE(BLI_path_contains("/some/path", "/some/path/../outside")) - << "Paths should be normalised"; + << "Paths should be normalized"; EXPECT_FALSE(BLI_path_contains("/some/path", "/some/path_library")) << "Just sharing a suffix is not enough, path semantics should be followed"; EXPECT_FALSE(BLI_path_contains("/some/path", "./contents")) diff --git a/source/blender/blenlib/tests/BLI_string_test.cc b/source/blender/blenlib/tests/BLI_string_test.cc index 6c16af5767c..eaaa65dd39f 100644 --- a/source/blender/blenlib/tests/BLI_string_test.cc +++ b/source/blender/blenlib/tests/BLI_string_test.cc @@ -420,98 +420,98 @@ TEST(string, StrFormatByteUnits) EXPECT_STREQ("-8191.8472 PiB", size_str); } -/* BLI_str_format_attribute_domain_size */ -TEST(string, StrFormatAttributeDomainSize) +/* BLI_str_format_decimal_unit */ +TEST(string, StrFormatDecimalUnits) { char size_str[7]; int size; - BLI_str_format_attribute_domain_size(size_str, size = 0); + BLI_str_format_decimal_unit(size_str, size = 0); EXPECT_STREQ("0", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 1); + BLI_str_format_decimal_unit(size_str, size = 1); EXPECT_STREQ("1", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 10); + BLI_str_format_decimal_unit(size_str, size = 10); EXPECT_STREQ("10", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 15); + BLI_str_format_decimal_unit(size_str, size = 15); EXPECT_STREQ("15", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 100); + BLI_str_format_decimal_unit(size_str, size = 100); EXPECT_STREQ("100", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 155); + BLI_str_format_decimal_unit(size_str, size = 155); EXPECT_STREQ("155", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 1000); + BLI_str_format_decimal_unit(size_str, size = 1000); EXPECT_STREQ("1.0K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 1555); + BLI_str_format_decimal_unit(size_str, size = 1555); EXPECT_STREQ("1.6K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 10000); + BLI_str_format_decimal_unit(size_str, size = 10000); EXPECT_STREQ("10.0K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 15555); + BLI_str_format_decimal_unit(size_str, size = 15555); EXPECT_STREQ("15.6K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 100000); + BLI_str_format_decimal_unit(size_str, size = 100000); EXPECT_STREQ("100K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 100000); + BLI_str_format_decimal_unit(size_str, size = 100000); EXPECT_STREQ("100K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 155555); + BLI_str_format_decimal_unit(size_str, size = 155555); EXPECT_STREQ("156K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 1000000); + BLI_str_format_decimal_unit(size_str, size = 1000000); EXPECT_STREQ("1.0M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 1555555); + BLI_str_format_decimal_unit(size_str, size = 1555555); EXPECT_STREQ("1.6M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 10000000); + BLI_str_format_decimal_unit(size_str, size = 10000000); EXPECT_STREQ("10.0M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 15555555); + BLI_str_format_decimal_unit(size_str, size = 15555555); EXPECT_STREQ("15.6M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 100000000); + BLI_str_format_decimal_unit(size_str, size = 100000000); EXPECT_STREQ("100M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 155555555); + BLI_str_format_decimal_unit(size_str, size = 155555555); EXPECT_STREQ("156M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = 1000000000); + BLI_str_format_decimal_unit(size_str, size = 1000000000); EXPECT_STREQ("1.0B", size_str); /* Largest possible value. */ - BLI_str_format_attribute_domain_size(size_str, size = INT32_MAX); + BLI_str_format_decimal_unit(size_str, size = INT32_MAX); EXPECT_STREQ("2.1B", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -0); + BLI_str_format_decimal_unit(size_str, size = -0); EXPECT_STREQ("0", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -1); + BLI_str_format_decimal_unit(size_str, size = -1); EXPECT_STREQ("-1", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -10); + BLI_str_format_decimal_unit(size_str, size = -10); EXPECT_STREQ("-10", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -15); + BLI_str_format_decimal_unit(size_str, size = -15); EXPECT_STREQ("-15", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -100); + BLI_str_format_decimal_unit(size_str, size = -100); EXPECT_STREQ("-100", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -155); + BLI_str_format_decimal_unit(size_str, size = -155); EXPECT_STREQ("-155", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -1000); + BLI_str_format_decimal_unit(size_str, size = -1000); EXPECT_STREQ("-1.0K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -1555); + BLI_str_format_decimal_unit(size_str, size = -1555); EXPECT_STREQ("-1.6K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -10000); + BLI_str_format_decimal_unit(size_str, size = -10000); EXPECT_STREQ("-10.0K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -15555); + BLI_str_format_decimal_unit(size_str, size = -15555); EXPECT_STREQ("-15.6K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -100000); + BLI_str_format_decimal_unit(size_str, size = -100000); EXPECT_STREQ("-100K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -155555); + BLI_str_format_decimal_unit(size_str, size = -155555); EXPECT_STREQ("-156K", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -1000000); + BLI_str_format_decimal_unit(size_str, size = -1000000); EXPECT_STREQ("-1.0M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -1555555); + BLI_str_format_decimal_unit(size_str, size = -1555555); EXPECT_STREQ("-1.6M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -10000000); + BLI_str_format_decimal_unit(size_str, size = -10000000); EXPECT_STREQ("-10.0M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -15555555); + BLI_str_format_decimal_unit(size_str, size = -15555555); EXPECT_STREQ("-15.6M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -100000000); + BLI_str_format_decimal_unit(size_str, size = -100000000); EXPECT_STREQ("-100M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -155555555); + BLI_str_format_decimal_unit(size_str, size = -155555555); EXPECT_STREQ("-156M", size_str); - BLI_str_format_attribute_domain_size(size_str, size = -1000000000); + BLI_str_format_decimal_unit(size_str, size = -1000000000); EXPECT_STREQ("-1.0B", size_str); /* Smallest possible value. */ - BLI_str_format_attribute_domain_size(size_str, size = -INT32_MAX); + BLI_str_format_decimal_unit(size_str, size = -INT32_MAX); EXPECT_STREQ("-2.1B", size_str); } diff --git a/source/blender/blenlib/tests/performance/BLI_ghash_performance_test.cc b/source/blender/blenlib/tests/performance/BLI_ghash_performance_test.cc index 0ff488202c2..09bb1e7239f 100644 --- a/source/blender/blenlib/tests/performance/BLI_ghash_performance_test.cc +++ b/source/blender/blenlib/tests/performance/BLI_ghash_performance_test.cc @@ -57,23 +57,23 @@ static void str_ghash_tests(GHash *ghash, const char *id) printf("\n========== STARTING %s ==========\n", id); #ifdef TEXT_CORPUS_PATH - size_t sz = 0; + size_t data_size = 0; char *data; { struct stat st; if (stat(TEXT_CORPUS_PATH, &st) == 0) - sz = st.st_size; + data_size = st.st_size; } - if (sz != 0) { + if (data_size != 0) { FILE *f = fopen(TEXT_CORPUS_PATH, "r"); - data = (char *)MEM_mallocN(sz + 1, __func__); - if (fread(data, sizeof(*data), sz, f) != sz) { + data = (char *)MEM_mallocN(data_size + 1, __func__); + if (fread(data, sizeof(*data), data_size, f) != data_size) { printf("ERROR in reading file %s!", TEXT_CORPUS_PATH); MEM_freeN(data); data = BLI_strdup(words10k); } - data[sz] = '\0'; + data[data_size] = '\0'; fclose(f); } else { |