diff options
Diffstat (limited to 'source/blender/blenlib/intern')
20 files changed, 1328 insertions, 551 deletions
diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index d24c180dae6..b209e6803f8 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -570,12 +570,12 @@ void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh) ghi->curEntry = NULL; ghi->curBucket = UINT_MAX; /* wraps to zero */ if (gh->nentries) { - while (!ghi->curEntry) { + do { ghi->curBucket++; if (UNLIKELY(ghi->curBucket == ghi->gh->nbuckets)) break; ghi->curEntry = ghi->gh->buckets[ghi->curBucket]; - } + } while (!ghi->curEntry); } } @@ -704,6 +704,11 @@ unsigned int BLI_ghashutil_uinthash_v4(const unsigned int key[4]) return hash; } +int BLI_ghashutil_uinthash_v4_cmp(const void *a, const void *b) +{ + return memcmp(a, b, sizeof(unsigned int[4])); +} + unsigned int BLI_ghashutil_uinthash(unsigned int key) { key += ~(key << 16); diff --git a/source/blender/blenlib/intern/BLI_kdopbvh.c b/source/blender/blenlib/intern/BLI_kdopbvh.c index e4f9df5c27e..17ab55e58f4 100644 --- a/source/blender/blenlib/intern/BLI_kdopbvh.c +++ b/source/blender/blenlib/intern/BLI_kdopbvh.c @@ -45,6 +45,17 @@ #define MAX_TREETYPE 32 +/* Setting zero so we can catch bugs in OpenMP/KDOPBVH. + * TODO(sergey): Deduplicate the limits with PBVH from BKE. + */ +#ifdef _OPENMP +# ifdef DEBUG +# define KDOPBVH_OMP_LIMIT 0 +# else +# define KDOPBVH_OMP_LIMIT 1024 +# endif +#endif + typedef unsigned char axis_t; typedef struct BVHNode { @@ -749,7 +760,8 @@ static void non_recursive_bvh_div_nodes(BVHTree *tree, BVHNode *branches_array, int j; /* Loop all branches on this level */ -#pragma omp parallel for private(j) schedule(static) + +#pragma omp parallel for private(j) schedule(static) if (num_leafs > KDOPBVH_OMP_LIMIT) for (j = i; j < end_j; j++) { int k; const int parent_level_index = j - i; @@ -1102,7 +1114,7 @@ BVHTreeOverlap *BLI_bvhtree_overlap(BVHTree *tree1, BVHTree *tree2, unsigned int data[j]->stop_axis = min_axis(tree1->stop_axis, tree2->stop_axis); } -#pragma omp parallel for private(j) schedule(static) +#pragma omp parallel for private(j) schedule(static) if (tree1->totleaf > KDOPBVH_OMP_LIMIT) for (j = 0; j < MIN2(tree1->tree_type, tree1->nodes[tree1->totleaf]->totnode); j++) { traverse(data[j], tree1->nodes[tree1->totleaf]->children[j], tree2->nodes[tree2->totleaf]); } diff --git a/source/blender/blenlib/intern/BLI_linklist.c b/source/blender/blenlib/intern/BLI_linklist.c index 66fcfd21fbb..a0b61e7945c 100644 --- a/source/blender/blenlib/intern/BLI_linklist.c +++ b/source/blender/blenlib/intern/BLI_linklist.c @@ -38,17 +38,12 @@ int BLI_linklist_length(LinkNode *list) { - if (0) { - return list ? (1 + BLI_linklist_length(list->next)) : 0; - } - else { - int len; + int len; - for (len = 0; list; list = list->next) - len++; - - return len; - } + for (len = 0; list; list = list->next) + len++; + + return len; } int BLI_linklist_index(LinkNode *list, void *ptr) diff --git a/source/blender/blenlib/intern/edgehash.c b/source/blender/blenlib/intern/edgehash.c index 27c5457370b..f6e4aec9624 100644 --- a/source/blender/blenlib/intern/edgehash.c +++ b/source/blender/blenlib/intern/edgehash.c @@ -463,14 +463,14 @@ void BLI_edgehashIterator_init(EdgeHashIterator *ehi, EdgeHash *eh) ehi->curEntry = NULL; ehi->curBucket = UINT_MAX; /* wraps to zero */ if (eh->nentries) { - while (!ehi->curEntry) { + do { ehi->curBucket++; if (UNLIKELY(ehi->curBucket == ehi->eh->nbuckets)) { break; } ehi->curEntry = ehi->eh->buckets[ehi->curBucket]; - } + } while (!ehi->curEntry); } } diff --git a/source/blender/blenlib/intern/gsqueue.c b/source/blender/blenlib/intern/gsqueue.c index 67703c3f033..94d18ce3c77 100644 --- a/source/blender/blenlib/intern/gsqueue.c +++ b/source/blender/blenlib/intern/gsqueue.c @@ -27,6 +27,12 @@ /** \file blender/blenlib/intern/gsqueue.c * \ingroup bli + * + * \brief A generic structure queue + * (a queue for fixed length generally small) structures. + * + * \note Only use this if you need (first-in-first-out), + * otherwise #BLI_stack is more efficient (first-in-last-out). */ #include <string.h> diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c index b0c24899bd1..abf15d57cf7 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.c @@ -354,7 +354,7 @@ int BLI_countlist(const ListBase *listbase) } /** - * Returns the nth element of \a listbase, numbering from 1. + * Returns the nth element of \a listbase, numbering from 0. */ void *BLI_findlink(const ListBase *listbase, int number) { @@ -372,7 +372,7 @@ void *BLI_findlink(const ListBase *listbase, int number) } /** - * Returns the nth-last element of \a listbase, numbering from 1. + * Returns the nth-last element of \a listbase, numbering from 0. */ void *BLI_rfindlink(const ListBase *listbase, int number) { @@ -390,7 +390,7 @@ void *BLI_rfindlink(const ListBase *listbase, int number) } /** - * Returns the position of \a vlink within \a listbase, numbering from 1, or -1 if not found. + * Returns the position of \a vlink within \a listbase, numbering from 0, or -1 if not found. */ int BLI_findindex(const ListBase *listbase, const void *vlink) { @@ -599,7 +599,7 @@ void BLI_listbase_reverse(ListBase *lb) /** * \param vlink Link to make first. */ -void BLI_rotatelist_first(ListBase *lb, void *vlink) +void BLI_listbase_rotate_first(ListBase *lb, void *vlink) { /* make circular */ ((Link *)lb->first)->prev = lb->last; @@ -615,7 +615,7 @@ void BLI_rotatelist_first(ListBase *lb, void *vlink) /** * \param vlink Link to make last. */ -void BLI_rotatelist_last(ListBase *lb, void *vlink) +void BLI_listbase_rotate_last(ListBase *lb, void *vlink) { /* make circular */ ((Link *)lb->first)->prev = lb->last; diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index 57a48bb5fa8..828a84da6e1 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -37,112 +37,38 @@ void hsv_to_rgb(float h, float s, float v, float *r, float *g, float *b) { - if (s != 0.0f) { - float i, f, p; - h = (h - floorf(h)) * 6.0f; - - i = floorf(h); - f = h - i; - - /* avoid computing q/t when not needed */ - p = (v * (1.0f - s)); -#define q (v * (1.0f - (s * f))) -#define t (v * (1.0f - (s * (1.0f - f)))) - - /* faster to compare floats then int conversion */ - if (i < 1.0f) { - *r = v; - *g = t; - *b = p; - } - else if (i < 2.0f) { - *r = q; - *g = v; - *b = p; - } - else if (i < 3.0f) { - *r = p; - *g = v; - *b = t; - } - else if (i < 4.0f) { - *r = p; - *g = q; - *b = v; - } - else if (i < 5.0f) { - *r = t; - *g = p; - *b = v; - } - else { - *r = v; - *g = p; - *b = q; - } + float nr, ng, nb; -#undef q -#undef t + nr = fabsf(h * 6.0f - 3.0f) - 1.0f; + ng = 2.0f - fabsf(h * 6.0f - 2.0f); + nb = 2.0f - fabsf(h * 6.0f - 4.0f); - } - else { - *r = v; - *g = v; - *b = v; - } + CLAMP(nr, 0.0f, 1.0f); + CLAMP(nb, 0.0f, 1.0f); + CLAMP(ng, 0.0f, 1.0f); + + *r = ((nr - 1.0f) * s + 1.0f) * v; + *g = ((ng - 1.0f) * s + 1.0f) * v; + *b = ((nb - 1.0f) * s + 1.0f) * v; } -/* HSL to rgb conversion from https://en.wikipedia.org/wiki/HSL_and_HSV */ void hsl_to_rgb(float h, float s, float l, float *r, float *g, float *b) { - float i, f, c; - h = (h - floorf(h)) * 6.0f; - c = (l > 0.5f) ? (2.0f * (1.0f - l) * s) : (2.0f * l * s); - i = floorf(h); - f = h - i; + float nr, ng, nb, chroma; -#define x2 (c * f) -#define x1 (c * (1.0f - f)) + nr = fabsf(h * 6.0f - 3.0f) - 1.0f; + ng = 2.0f - fabsf(h * 6.0f - 2.0f); + nb = 2.0f - fabsf(h * 6.0f - 4.0f); - /* faster to compare floats then int conversion */ - if (i < 1.0f) { - *r = c; - *g = x2; - *b = 0; - } - else if (i < 2.0f) { - *r = x1; - *g = c; - *b = 0; - } - else if (i < 3.0f) { - *r = 0; - *g = c; - *b = x2; - } - else if (i < 4.0f) { - *r = 0; - *g = x1; - *b = c; - } - else if (i < 5.0f) { - *r = x2; - *g = 0; - *b = c; - } - else { - *r = c; - *g = 0; - *b = x1; - } + CLAMP(nr, 0.0f, 1.0f); + CLAMP(nb, 0.0f, 1.0f); + CLAMP(ng, 0.0f, 1.0f); -#undef x1 -#undef x2 + chroma = (1.0f - fabsf(2.0f * l - 1.0f)) * s; - f = l - 0.5f * c; - *r += f; - *g += f; - *b += f; + *r = (nr - 0.5f) * chroma + l; + *g = (ng - 0.5f) * chroma + l; + *b = (nb - 0.5f) * chroma + l; } /* convenience function for now */ @@ -187,7 +113,7 @@ void yuv_to_rgb(float y, float u, float v, float *lr, float *lg, float *lb) void rgb_to_ycc(float r, float g, float b, float *ly, float *lcb, float *lcr, int colorspace) { float sr, sg, sb; - float y = 128.f, cr = 128.f, cb = 128.f; + float y = 128.0f, cr = 128.0f, cb = 128.0f; sr = 255.0f * r; sg = 255.0f * g; @@ -226,7 +152,7 @@ void rgb_to_ycc(float r, float g, float b, float *ly, float *lcb, float *lcr, in /* FIXME comment above must be wrong because BLI_YCC_ITU_BT601 y 16.0 cr 16.0 -> r -0.7009 */ void ycc_to_rgb(float y, float cb, float cr, float *lr, float *lg, float *lb, int colorspace) { - float r = 128.f, g = 128.f, b = 128.f; + float r = 128.0f, g = 128.0f, b = 128.0f; switch (colorspace) { case BLI_YCC_ITU_BT601: @@ -284,57 +210,26 @@ void hex_to_rgb(char *hexcol, float *r, float *g, float *b) void rgb_to_hsv(float r, float g, float b, float *lh, float *ls, float *lv) { - float h, s, v; - float cmax, cmin; - - cmax = r; - cmin = r; - cmax = (g > cmax ? g : cmax); - cmin = (g < cmin ? g : cmin); - cmax = (b > cmax ? b : cmax); - cmin = (b < cmin ? b : cmin); - - v = cmax; /* value */ - if (cmax != 0.0f) { - float cdelta; - - cdelta = cmax - cmin; - s = cdelta / cmax; - - if (s != 0.0f) { - float rc, gc, bc; - - rc = (cmax - r) / cdelta; - gc = (cmax - g) / cdelta; - bc = (cmax - b) / cdelta; - - if (r == cmax) { - h = bc - gc; - if (h < 0.0f) { - h += 6.0f; - } - } - else if (g == cmax) { - h = 2.0f + rc - bc; - } - else { - h = 4.0f + gc - rc; - } - - h *= (1.0f / 6.0f); - } - else { - h = 0.0f; - } + float k = 0.0f; + float chroma; + float min_gb; + + if (g < b) { + SWAP(float, g, b); + k = -1.0f; } - else { - h = 0.0f; - s = 0.0f; + min_gb = b; + if (r < g) { + SWAP(float, r, g); + k = -2.0f / 6.0f - k; + min_gb = min_ff(g, b); } - *lh = h; - *ls = s; - *lv = v; + chroma = r - min_gb; + + *lh = fabsf(k + (g - b) / (6.0f * chroma + 1e-20f)); + *ls = chroma / (r + 1e-20f); + *lv = r; } /* convenience function for now */ @@ -374,8 +269,8 @@ void rgb_to_hsl(float r, float g, float b, float *lh, float *ls, float *ll) void rgb_to_hsl_compat(float r, float g, float b, float *lh, float *ls, float *ll) { - float orig_s = *ls; - float orig_h = *lh; + const float orig_s = *ls; + const float orig_h = *lh; rgb_to_hsl(r, g, b, lh, ls, ll); @@ -407,8 +302,8 @@ void rgb_to_hsl_v(const float rgb[3], float r_hsl[3]) void rgb_to_hsv_compat(float r, float g, float b, float *lh, float *ls, float *lv) { - float orig_h = *lh; - float orig_s = *ls; + const float orig_h = *lh; + const float orig_s = *ls; rgb_to_hsv(r, g, b, lh, ls, lv); @@ -703,11 +598,12 @@ static float index_to_float(const unsigned short i) void BLI_init_srgb_conversion(void) { - static int initialized = 0; + static bool initialized = false; unsigned int i, b; - if (initialized) return; - initialized = 1; + if (initialized) + return; + initialized = true; /* Fill in the lookup table to convert floats to bytes: */ for (i = 0; i < 0x10000; i++) { @@ -762,13 +658,13 @@ static float xyz_to_lab_component(float v) void xyz_to_lab(float x, float y, float z, float *l, float *a, float *b) { - float xr = x / 95.047f; - float yr = y / 100.0f; - float zr = z / 108.883f; + const float xr = x / 95.047f; + const float yr = y / 100.0f; + const float zr = z / 108.883f; - float fx = xyz_to_lab_component(xr); - float fy = xyz_to_lab_component(yr); - float fz = xyz_to_lab_component(zr); + const float fx = xyz_to_lab_component(xr); + const float fy = xyz_to_lab_component(yr); + const float fz = xyz_to_lab_component(zr); *l = 116.0f * fy - 16.0f; *a = 500.0f * (fx - fy); diff --git a/source/blender/blenlib/intern/math_color_blend_inline.c b/source/blender/blenlib/intern/math_color_blend_inline.c index 4810fe757fa..2522fe5f6c9 100644 --- a/source/blender/blenlib/intern/math_color_blend_inline.c +++ b/source/blender/blenlib/intern/math_color_blend_inline.c @@ -30,11 +30,16 @@ #include "BLI_math_base.h" #include "BLI_math_color.h" #include "BLI_math_color_blend.h" +#include "BLI_math_vector.h" #include "BLI_utildefines.h" #ifndef __MATH_COLOR_BLEND_INLINE_C__ #define __MATH_COLOR_BLEND_INLINE_C__ +/* don't add any saturation to a completly black and white image */ +#define EPS_SATURATION 0.0005f +#define EPS_ALPHA 0.0005f + /***************************** Color Blending ******************************** * * - byte colors are assumed to be straight alpha @@ -67,10 +72,7 @@ MINLINE void blend_color_mix_byte(unsigned char dst[4], const unsigned char src1 } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4_char((char *)dst, (char *)src1); } } @@ -92,10 +94,7 @@ MINLINE void blend_color_add_byte(unsigned char dst[4], const unsigned char src1 } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4_char((char *)dst, (char *)src1); } } @@ -117,10 +116,7 @@ MINLINE void blend_color_sub_byte(unsigned char dst[4], const unsigned char src1 } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4_char((char *)dst, (char *)src1); } } @@ -143,10 +139,7 @@ MINLINE void blend_color_mul_byte(unsigned char dst[4], const unsigned char src1 } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4_char((char *)dst, (char *)src1); } } @@ -169,10 +162,7 @@ MINLINE void blend_color_lighten_byte(unsigned char dst[4], const unsigned char } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4_char((char *)dst, (char *)src1); } } @@ -195,10 +185,7 @@ MINLINE void blend_color_darken_byte(unsigned char dst[4], const unsigned char s } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4_char((char *)dst, (char *)src1); } } @@ -215,10 +202,7 @@ MINLINE void blend_color_erase_alpha_byte(unsigned char dst[4], const unsigned c } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4_char((char *)dst, (char *)src1); } } @@ -235,11 +219,391 @@ MINLINE void blend_color_add_alpha_byte(unsigned char dst[4], const unsigned cha } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + +MINLINE void blend_color_overlay_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = (int)src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + int temp; + + if (src1[i] > 127) { + temp = 255 - ((255 - 2 * (src1[i] - 127)) * (255 - src2[i]) / 255); + } + else { + temp = (2 * src1[i] * src2[i]) >> 8; + } + dst[i] = (unsigned char)min_ii((temp * fac + src1[i] * mfac) / 255, 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + +MINLINE void blend_color_hardlight_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = (int)src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + int temp; + + if (src2[i] > 127) { + temp = 255 - ((255 - 2 * (src2[i] - 127)) * (255 - src1[i]) / 255); + } + else { + temp = (2 * src2[i] * src1[i]) >> 8; + } + dst[i] = (unsigned char)min_ii((temp * fac + src1[i] * mfac) / 255, 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + +MINLINE void blend_color_burn_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + const int temp = (src2[i] == 0) ? 0 : max_ii(255 - ((255 - src1[i]) * 255) / src2[i], 0); + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + +MINLINE void blend_color_linearburn_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + const int temp = max_ii(src1[i] + src2[i] - 255, 0); + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + +MINLINE void blend_color_dodge_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + const int temp = (src2[i] == 255) ? 255 : min_ii((src1[i] * 255) / (255 - src2[i]), 255); + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + +MINLINE void blend_color_screen_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + const int temp = max_ii(255 - (((255 - src1[i]) * (255 - src2[i])) / 255), 0); + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + +MINLINE void blend_color_softlight_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + int temp; + + if (src1[i] < 127) { + temp = ((2 * ((src2[i] / 2) + 64)) * src1[i]) / 255; + } + else { + temp = 255 - (2 * (255 - ((src2[i] / 2) + 64)) * (255 - src1[i]) / 255); + } + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + +MINLINE void blend_color_pinlight_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + int temp; + + if (src2[i] > 127) { + temp = max_ii(2 * (src2[i] - 127), src1[i]); + } + else { + temp = min_ii(2 * src2[i], src1[i]); + } + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + +MINLINE void blend_color_linearlight_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + int temp; + + if (src2[i] > 127) { + temp = min_ii(src1[i] + 2 * (src2[i] - 127), 255); + } + else { + temp = max_ii(src1[i] + 2 * src2[i] - 255, 0); + } + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + +MINLINE void blend_color_vividlight_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + int temp; + + if (src2[i] == 255) { + temp = 255; + } + else if (src2[i] == 0) { + temp = 0; + } + else if (src2[i] > 127) { + temp = min_ii(((src1[i]) * 255) / (2 * (255 - src2[i])), 255); + } + else { + temp = max_ii(255 - ((255 - src1[i]) * 255 / (2 * src2[i])), 0); + } + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + + +MINLINE void blend_color_difference_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + const int temp = abs(src1[i] - src2[i]); + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + + +MINLINE void blend_color_exclusion_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + int i = 3; + + while (i--) { + const int temp = 127 - ((2 * (src1[i] - 127) * (src2[i] - 127)) / 255); + dst[i] = (unsigned char)((temp * fac + src1[i] * mfac) / 255); + } + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + +MINLINE void blend_color_color_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + rgb_to_hsv(src1[0] / 255.0f, src1[1] / 255.0f, src1[2] / 255.0f, &h1, &s1, &v1); + rgb_to_hsv(src2[0] / 255.0f, src2[1] / 255.0f, src2[2] / 255.0f, &h2, &s2, &v2); + + + h1 = h2; + s1 = s2; + + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + dst[0] = (unsigned char)(((int)(r * 255.0f) * fac + src1[0] * mfac) / 255); + dst[1] = (unsigned char)(((int)(g * 255.0f) * fac + src1[1] * mfac) / 255); + dst[2] = (unsigned char)(((int)(b * 255.0f) * fac + src1[2] * mfac) / 255); + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + +MINLINE void blend_color_hue_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + rgb_to_hsv(src1[0] / 255.0f, src1[1] / 255.0f, src1[2] / 255.0f, &h1, &s1, &v1); + rgb_to_hsv(src2[0] / 255.0f, src2[1] / 255.0f, src2[2] / 255.0f, &h2, &s2, &v2); + + + h1 = h2; + + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + dst[0] = (unsigned char)(((int)(r * 255.0f) * fac + src1[0] * mfac) / 255); + dst[1] = (unsigned char)(((int)(g * 255.0f) * fac + src1[1] * mfac) / 255); + dst[2] = (unsigned char)(((int)(b * 255.0f) * fac + src1[2] * mfac) / 255); + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } + +} + +MINLINE void blend_color_saturation_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + rgb_to_hsv(src1[0] / 255.0f, src1[1] / 255.0f, src1[2] / 255.0f, &h1, &s1, &v1); + rgb_to_hsv(src2[0] / 255.0f, src2[1] / 255.0f, src2[2] / 255.0f, &h2, &s2, &v2); + + if (s1 > EPS_SATURATION) { + s1 = s2; + } + + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + dst[0] = (unsigned char)(((int)(r * 255.0f) * fac + src1[0] * mfac) / 255); + dst[1] = (unsigned char)(((int)(g * 255.0f) * fac + src1[1] * mfac) / 255); + dst[2] = (unsigned char)(((int)(b * 255.0f) * fac + src1[2] * mfac) / 255); + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); + } +} + +MINLINE void blend_color_luminosity_byte(unsigned char dst[4], unsigned const char src1[4], unsigned const char src2[4]) +{ + const int fac = src2[3]; + if (fac != 0) { + const int mfac = 255 - fac; + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + rgb_to_hsv(src1[0] / 255.0f, src1[1] / 255.0f, src1[2] / 255.0f, &h1, &s1, &v1); + rgb_to_hsv(src2[0] / 255.0f, src2[1] / 255.0f, src2[2] / 255.0f, &h2, &s2, &v2); + + v1 = v2; + + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + dst[0] = (unsigned char)(((int)(r * 255.0f) * fac + src1[0] * mfac) / 255); + dst[1] = (unsigned char)(((int)(g * 255.0f) * fac + src1[1] * mfac) / 255); + dst[2] = (unsigned char)(((int)(b * 255.0f) * fac + src1[2] * mfac) / 255); + + } + else { + /* no op */ + copy_v4_v4_char((char *)dst, (char *)src1); } + } MINLINE void blend_color_interpolate_byte(unsigned char dst[4], const unsigned char src1[4], const unsigned char src2[4], float ft) @@ -257,10 +621,7 @@ MINLINE void blend_color_interpolate_byte(unsigned char dst[4], const unsigned c dst[3] = (unsigned char)divide_round_i(tmp, 255); } else { - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4_char((char *)dst, (char *)src1); } } @@ -280,10 +641,7 @@ MINLINE void blend_color_mix_float(float dst[4], const float src1[4], const floa } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4(dst, src1); } } @@ -298,10 +656,7 @@ MINLINE void blend_color_add_float(float dst[4], const float src1[4], const floa } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4(dst, src1); } } @@ -316,10 +671,7 @@ MINLINE void blend_color_sub_float(float dst[4], const float src1[4], const floa } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4(dst, src1); } } @@ -337,10 +689,7 @@ MINLINE void blend_color_mul_float(float dst[4], const float src1[4], const floa } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4(dst, src1); } } @@ -360,10 +709,7 @@ MINLINE void blend_color_lighten_float(float dst[4], const float src1[4], const } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4(dst, src1); } } @@ -383,10 +729,7 @@ MINLINE void blend_color_darken_float(float dst[4], const float src1[4], const f } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4(dst, src1); } } @@ -397,8 +740,9 @@ MINLINE void blend_color_erase_alpha_float(float dst[4], const float src1[4], co float alpha = max_ff(src1[3] - src2[3], 0.0f); float map_alpha; - if (alpha <= 0.0005f) + if (alpha <= EPS_ALPHA) { alpha = 0.0f; + } map_alpha = alpha / src1[3]; @@ -409,10 +753,7 @@ MINLINE void blend_color_erase_alpha_float(float dst[4], const float src1[4], co } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4(dst, src1); } } @@ -423,8 +764,9 @@ MINLINE void blend_color_add_alpha_float(float dst[4], const float src1[4], cons float alpha = min_ff(src1[3] + src2[3], 1.0f); float map_alpha; - if (alpha >= 1.0f - 0.0005f) + if (alpha >= 1.0f - EPS_ALPHA) { alpha = 1.0f; + } map_alpha = (src1[3] > 0.0f) ? alpha / src1[3] : 1.0f; @@ -435,17 +777,390 @@ MINLINE void blend_color_add_alpha_float(float dst[4], const float src1[4], cons } else { /* no op */ - dst[0] = src1[0]; - dst[1] = src1[1]; - dst[2] = src1[2]; - dst[3] = src1[3]; + copy_v4_v4(dst, src1); + } +} + +MINLINE void blend_color_overlay_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + float temp; + + if (src1[i] > 0.5f) { + temp = 1.0f - (1.0f - 2.0f * (src1[i] - 0.5f)) * (1.0f - src2[i]); + } + else { + temp = 2.0f * src1[i] * src2[i]; + } + dst[i] = min_ff(temp * fac + src1[i] * mfac, 1.0f); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + + +MINLINE void blend_color_hardlight_float(float dst[4], const float src1[4], const float src2[2]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + float temp; + + if (src2[i] > 0.5f) { + temp = 1.0f - ((1.0f - 2.0f * (src2[i] - 0.5f)) * (1.0f - src1[i])); + } + else { + temp = 2.0f * src2[i] * src1[i]; + } + dst[i] = min_ff((temp * fac + src1[i] * mfac) / 1.0f, 1.0f); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + +MINLINE void blend_color_burn_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + const float temp = (src2[i] == 0.0f) ? 0.0f : max_ff(1.0f - ((1.0f - src1[i]) / src2[i]), 0.0f); + dst[i] = (temp * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + +MINLINE void blend_color_linearburn_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + const float temp = max_ff(src1[i] + src2[i] - 1.0f, 0.0f); + dst[i] = (temp * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + + +MINLINE void blend_color_dodge_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + const float temp = (src2[i] >= 1.0f) ? 1.0f : min_ff(src1[i] / (1.0f - src2[i]), 1.0f); + dst[i] = (temp * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + +MINLINE void blend_color_screen_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + const float temp = max_ff(1.0f - ((1.0f - src1[i]) * (1.0f - src2[i])), 0.0f); + dst[i] = (temp * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + +MINLINE void blend_color_softlight_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + float temp; + + if (src1[i] < 0.5f) { + temp = (src2[i] + 0.5f) * src1[i]; + } + else { + temp = 1.0f - ((1.0f - (src2[i] + 0.5f)) * (1.0f - src1[i])); + } + dst[i] = (temp * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + +MINLINE void blend_color_pinlight_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + float temp; + + if (src2[i] > 0.5f) { + temp = max_ff(2.0f * (src2[i] - 0.5f), src1[i]); + } + else { + temp = min_ff(2.0f * src2[i], src1[i]); + } + dst[i] = (temp * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + + +MINLINE void blend_color_linearlight_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + float temp; + + if (src2[i] > 0.5f) { + temp = min_ff(src1[i] + 2.0f * (src2[i] - 0.5f), 1.0f); + } + else { + temp = max_ff(src1[i] + 2.0f * src2[i] - 1.0f, 0.0f); + } + dst[i] = (temp * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + + +MINLINE void blend_color_vividlight_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + float temp; + + if (src2[i] == 1.0f) { + temp = 1.0f; + } + else if (src2[i] == 0.0f) { + temp = 0.0f; + } + else if (src2[i] > 0.5f) { + temp = min_ff(((src1[i]) * 1.0f) / (2.0f * (1.0f - src2[i])), 1.0f); + } + else { + temp = max_ff(1.0f - ((1.0f - src1[i]) * 1.0f / (2.0f * src2[i])), 0.0f); + } + dst[i] = (temp * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + +MINLINE void blend_color_difference_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + dst[i] = (fabsf(src1[i] - src2[i]) * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); } } + +MINLINE void blend_color_exclusion_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + int i = 3; + + while (i--) { + const float temp = 0.5f - ((2 * (src1[i] - 0.5f) * (src2[i] - 0.5f))); + dst[i] = (temp * fac + src1[i] * mfac); + } + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } + +} + +MINLINE void blend_color_color_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + + rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1); + rgb_to_hsv(src2[0], src2[1], src2[2], &h2, &s2, &v2); + + h1 = h2; + s1 = s2; + + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + dst[0] = (r * fac + src1[0] * mfac); + dst[1] = (g * fac + src1[1] * mfac); + dst[2] = (b * fac + src1[2] * mfac); + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + + +MINLINE void blend_color_hue_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + + rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1); + rgb_to_hsv(src2[0], src2[1], src2[2], &h2, &s2, &v2); + + h1 = h2; + + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + dst[0] = (r * fac + src1[0] * mfac); + dst[1] = (g * fac + src1[1] * mfac); + dst[2] = (b * fac + src1[2] * mfac); + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + +MINLINE void blend_color_saturation_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + + rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1); + rgb_to_hsv(src2[0], src2[1], src2[2], &h2, &s2, &v2); + + if (s1 > EPS_SATURATION) { + s1 = s2; + } + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + dst[0] = (r * fac + src1[0] * mfac); + dst[1] = (g * fac + src1[1] * mfac); + dst[2] = (b * fac + src1[2] * mfac); + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + +MINLINE void blend_color_luminosity_float(float dst[3], const float src1[3], const float src2[3]) +{ + const float fac = src2[3]; + if (fac != 0.0f && fac < 1.0f) { + const float mfac = 1.0f - fac; + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + + rgb_to_hsv(src1[0], src1[1], src1[2], &h1, &s1, &v1); + rgb_to_hsv(src2[0], src2[1], src2[2], &h2, &s2, &v2); + + v1 = v2; + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + dst[0] = (r * fac + src1[0] * mfac); + dst[1] = (g * fac + src1[1] * mfac); + dst[2] = (b * fac + src1[2] * mfac); + } + else { + /* no op */ + copy_v4_v4(dst, src1); + } +} + + MINLINE void blend_color_interpolate_float(float dst[4], const float src1[4], const float src2[4], float t) { /* interpolation, colors are premultiplied so it goes fine */ - float mt = 1.0f - t; + const float mt = 1.0f - t; dst[0] = mt * src1[0] + t * src2[0]; dst[1] = mt * src1[1] + t * src2[1]; @@ -453,4 +1168,7 @@ MINLINE void blend_color_interpolate_float(float dst[4], const float src1[4], co dst[3] = mt * src1[3] + t * src2[3]; } +#undef EPS_SATURATION +#undef EPS_ALPHA + #endif /* __MATH_COLOR_BLEND_INLINE_C__ */ diff --git a/source/blender/blenlib/intern/math_color_inline.c b/source/blender/blenlib/intern/math_color_inline.c index bb2201541d9..9233749d5df 100644 --- a/source/blender/blenlib/intern/math_color_inline.c +++ b/source/blender/blenlib/intern/math_color_inline.c @@ -255,11 +255,11 @@ MINLINE float rgb_to_luma_y(const float rgb[3]) MINLINE int compare_rgb_uchar(const unsigned char col_a[3], const unsigned char col_b[3], const int limit) { - int r = (int)col_a[0] - (int)col_b[0]; + const int r = (int)col_a[0] - (int)col_b[0]; if (ABS(r) < limit) { - int g = (int)col_a[1] - (int)col_b[1]; + const int g = (int)col_a[1] - (int)col_b[1]; if (ABS(g) < limit) { - int b = (int)col_a[2] - (int)col_b[2]; + const int b = (int)col_a[2] - (int)col_b[2]; if (ABS(b) < limit) { return 1; } @@ -280,7 +280,7 @@ MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4]) straight[3] = premul[3]; } else { - float alpha_inv = 1.0f / premul[3]; + const float alpha_inv = 1.0f / premul[3]; straight[0] = premul[0] * alpha_inv; straight[1] = premul[1] * alpha_inv; straight[2] = premul[2] * alpha_inv; @@ -295,7 +295,7 @@ MINLINE void premul_to_straight_v4(float color[4]) MINLINE void straight_to_premul_v4_v4(float premul[4], const float straight[4]) { - float alpha = straight[3]; + const float alpha = straight[3]; premul[0] = straight[0] * alpha; premul[1] = straight[1] * alpha; premul[2] = straight[2] * alpha; @@ -309,8 +309,8 @@ MINLINE void straight_to_premul_v4(float color[4]) MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char color[4]) { - float alpha = color[3] * (1.0f / 255.0f); - float fac = alpha * (1.0f / 255.0f); + const float alpha = color[3] * (1.0f / 255.0f); + const float fac = alpha * (1.0f / 255.0f); result[0] = color[0] * fac; result[1] = color[1] * fac; @@ -327,7 +327,7 @@ MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float c result[3] = FTOCHAR(color[3]); } else { - float alpha_inv = 1.0f / color[3]; + const float alpha_inv = 1.0f / color[3]; /* hopefully this would be optimized */ result[0] = FTOCHAR(color[0] * alpha_inv); diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index efb881cde1b..54e3545a3f1 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -568,7 +568,7 @@ int isect_seg_seg_v2_point(const float v1[2], const float v2[2], const float v3[ { float a1, a2, b1, b2, c1, c2, d; float u, v; - const float eps = 0.000001f; + const float eps = 1e-6f; const float eps_sq = eps * eps; a1 = v2[0] - v1[0]; @@ -1278,13 +1278,13 @@ bool isect_plane_plane_v3(float r_isect_co[3], float r_isect_no[3], static bool getLowestRoot(const float a, const float b, const float c, const float maxR, float *root) { /* Check if a solution exists */ - float determinant = b * b - 4.0f * a * c; + const float determinant = b * b - 4.0f * a * c; /* If determinant is negative it means no solutions. */ if (determinant >= 0.0f) { /* calculate the two roots: (if determinant == 0 then * x1==x2 but lets disregard that slight optimization) */ - float sqrtD = sqrtf(determinant); + const float sqrtD = sqrtf(determinant); float r1 = (-b - sqrtD) / (2.0f * a); float r2 = (-b + sqrtD) / (2.0f * a); @@ -1295,18 +1295,18 @@ static bool getLowestRoot(const float a, const float b, const float c, const flo /* Get lowest root: */ if (r1 > 0.0f && r1 < maxR) { *root = r1; - return 1; + return true; } /* It is possible that we want x2 - this can happen */ /* if x1 < 0 */ if (r2 > 0.0f && r2 < maxR) { *root = r2; - return 1; + return true; } } /* No (valid) solutions */ - return 0; + return false; } bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const float radius, @@ -1335,7 +1335,7 @@ bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const fl if (fabsf(nordotv) < 0.000001f) { if (fabsf(a) >= radius) { - return 0; + return false; } } else { @@ -1377,7 +1377,7 @@ bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const fl //(((unsigned int)z)& ~(((unsigned int)x)|((unsigned int)y))) & 0x80000000) { *r_lambda = t0; copy_v3_v3(ipoint, point); - return 1; + return true; } } @@ -1394,7 +1394,7 @@ bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const fl if (getLowestRoot(a, b, c, *r_lambda, r_lambda)) { copy_v3_v3(ipoint, v0); - found_by_sweep = 1; + found_by_sweep = true; } /*v1*/ @@ -1404,7 +1404,7 @@ bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const fl if (getLowestRoot(a, b, c, *r_lambda, r_lambda)) { copy_v3_v3(ipoint, v1); - found_by_sweep = 1; + found_by_sweep = true; } /*v2*/ @@ -1414,7 +1414,7 @@ bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const fl if (getLowestRoot(a, b, c, *r_lambda, r_lambda)) { copy_v3_v3(ipoint, v2); - found_by_sweep = 1; + found_by_sweep = true; } /*---test edges---*/ @@ -1440,7 +1440,7 @@ bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const fl copy_v3_v3(ipoint, e1); mul_v3_fl(ipoint, e); add_v3_v3(ipoint, v0); - found_by_sweep = 1; + found_by_sweep = true; } } @@ -1462,7 +1462,7 @@ bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const fl copy_v3_v3(ipoint, e2); mul_v3_fl(ipoint, e); add_v3_v3(ipoint, v0); - found_by_sweep = 1; + found_by_sweep = true; } } @@ -1489,7 +1489,7 @@ bool isect_sweeping_sphere_tri_v3(const float p1[3], const float p2[3], const fl copy_v3_v3(ipoint, e3); mul_v3_fl(ipoint, e); add_v3_v3(ipoint, v1); - found_by_sweep = 1; + found_by_sweep = true; } } @@ -1508,10 +1508,10 @@ bool isect_axial_line_tri_v3(const int axis, const float p1[3], const float p2[3 return isect_line_tri_v3(p1, p2, v0, v1, v2, lambda); /* first a simple bounding box test */ - if (min_fff(v0[a1], v1[a1], v2[a1]) > p1[a1]) return 0; - if (min_fff(v0[a2], v1[a2], v2[a2]) > p1[a2]) return 0; - if (max_fff(v0[a1], v1[a1], v2[a1]) < p1[a1]) return 0; - if (max_fff(v0[a2], v1[a2], v2[a2]) < p1[a2]) return 0; + if (min_fff(v0[a1], v1[a1], v2[a1]) > p1[a1]) return false; + if (min_fff(v0[a2], v1[a2], v2[a2]) > p1[a2]) return false; + if (max_fff(v0[a1], v1[a1], v2[a1]) < p1[a1]) return false; + if (max_fff(v0[a2], v1[a2], v2[a2]) < p1[a2]) return false; /* then a full intersection test */ #endif @@ -1521,7 +1521,7 @@ bool isect_axial_line_tri_v3(const int axis, const float p1[3], const float p2[3 sub_v3_v3v3(p, v0, p1); f = (e2[a1] * e1[a2] - e2[a2] * e1[a1]); - if ((f > -0.000001f) && (f < 0.000001f)) return 0; + if ((f > -0.000001f) && (f < 0.000001f)) return false; v = (p[a2] * e1[a1] - p[a1] * e1[a2]) / f; if ((v < 0.0f) || (v > 1.0f)) return 0; @@ -1529,7 +1529,7 @@ bool isect_axial_line_tri_v3(const int axis, const float p1[3], const float p2[3 f = e1[a1]; if ((f > -0.000001f) && (f < 0.000001f)) { f = e1[a2]; - if ((f > -0.000001f) && (f < 0.000001f)) return 0; + if ((f > -0.000001f) && (f < 0.000001f)) return false; u = (-p[a2] - v * e2[a2]) / f; } else @@ -1539,9 +1539,9 @@ bool isect_axial_line_tri_v3(const int axis, const float p1[3], const float p2[3 *r_lambda = (p[a0] + u * e1[a0] + v * e2[a0]) / (p2[a0] - p1[a0]); - if ((*r_lambda < 0.0f) || (*r_lambda > 1.0f)) return 0; + if ((*r_lambda < 0.0f) || (*r_lambda > 1.0f)) return false; - return 1; + return true; } /** @@ -1616,9 +1616,9 @@ int isect_line_line_v3(const float v1[3], const float v2[3], const float v3[3], } } -/* Intersection point strictly between the two lines - * 0 when no intersection is found - * */ +/** Intersection point strictly between the two lines + * \return false when no intersection is found + */ bool isect_line_line_strict_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float vi[3], float *r_lambda) @@ -1635,7 +1635,7 @@ bool isect_line_line_strict_v3(const float v1[3], const float v2[3], d = dot_v3v3(dir1, dir2); if (d == 1.0f || d == -1.0f || d == 0) { /* colinear or one vector is zero-length*/ - return 0; + return false; } cross_v3_v3v3(ab, a, b); @@ -1644,7 +1644,7 @@ bool isect_line_line_strict_v3(const float v1[3], const float v2[3], /* test zero length line */ if (UNLIKELY(div == 0.0f)) { - return 0; + return false; } /* test if the two lines are coplanar */ else if (d > -0.000001f && d < 0.000001f) { @@ -1663,14 +1663,14 @@ bool isect_line_line_strict_v3(const float v1[3], const float v2[3], if (r_lambda) *r_lambda = f1; - return 1; /* intersection found */ + return true; /* intersection found */ } else { - return 0; + return false; } } else { - return 0; + return false; } } @@ -1688,9 +1688,9 @@ void isect_ray_aabb_initialize(IsectRayAABBData *data, const float ray_start[3], data->ray_inv_dir[1] = 1.0f / ray_direction[1]; data->ray_inv_dir[2] = 1.0f / ray_direction[2]; - data->sign[0] = data->ray_inv_dir[0] < 0; - data->sign[1] = data->ray_inv_dir[1] < 0; - data->sign[2] = data->ray_inv_dir[2] < 0; + data->sign[0] = data->ray_inv_dir[0] < 0.0f; + data->sign[1] = data->ray_inv_dir[1] < 0.0f; + data->sign[2] = data->ray_inv_dir[2] < 0.0f; } /* Adapted from http://www.gamedev.net/community/forums/topic.asp?topic_id=459973 */ @@ -1808,8 +1808,9 @@ float line_plane_factor_v3(const float plane_co[3], const float plane_no[3], return (dot != 0.0f) ? -dot_v3v3(plane_no, h) / dot : 0.0f; } -/* ensure the distance between these points is no greater then 'dist' - * if it is, scale then both into the center */ +/** Ensure the distance between these points is no greater then 'dist'. + * If it is, scale then both into the center. + */ void limit_dist_v3(float v1[3], float v2[3], const float dist) { const float dist_old = len_v3v3(v1, v2); @@ -1874,8 +1875,7 @@ static bool point_in_slice(const float p[3], const float v1[3], const float l1[3 sub_v3_v3v3(rp, p, v1); h = dot_v3v3(q, rp) / dot_v3v3(q, q); - if (h < 0.0f || h > 1.0f) return 0; - return 1; + return (h < 0.0f || h > 1.0f) ? false : true; } #if 0 @@ -1904,10 +1904,43 @@ static int point_in_slice_m(float p[3], float origin[3], float normal[3], float bool isect_point_tri_prism_v3(const float p[3], const float v1[3], const float v2[3], const float v3[3]) { - if (!point_in_slice(p, v1, v2, v3)) return 0; - if (!point_in_slice(p, v2, v3, v1)) return 0; - if (!point_in_slice(p, v3, v1, v2)) return 0; - return 1; + if (!point_in_slice(p, v1, v2, v3)) return false; + if (!point_in_slice(p, v2, v3, v1)) return false; + if (!point_in_slice(p, v3, v1, v2)) return false; + return true; +} + +/** + * \param r_vi The point \a p projected onto the triangle. + * \return True when \a p is inside the triangle. + * \note Its up to the caller to check the distance between \a p and \a r_vi against an error margin. + */ +bool isect_point_tri_v3(const float p[3], const float v1[3], const float v2[3], const float v3[3], + float r_vi[3]) +{ + if (isect_point_tri_prism_v3(p, v1, v2, v3)) { + float no[3], n1[3], n2[3]; + + /* Could use normal_tri_v3, but doesn't have to be unit-length */ + sub_v3_v3v3(n1, v1, v2); + sub_v3_v3v3(n2, v2, v3); + cross_v3_v3v3(no, n1, n2); + + if (LIKELY(len_squared_v3(no) != 0.0f)) { + float plane[4]; + plane_from_point_normal_v3(plane, v1, no); + closest_to_plane_v3(r_vi, plane, p); + } + else { + /* degenerate */ + copy_v3_v3(r_vi, p); + } + + return true; + } + else { + return false; + } } bool clip_segment_v3_plane(float p1[3], float p2[3], const float plane[4]) @@ -1918,7 +1951,7 @@ bool clip_segment_v3_plane(float p1[3], float p2[3], const float plane[4]) div = dot_v3v3(dp, plane); if (div == 0.0f) /* parallel */ - return 1; + return true; t = -plane_point_side_v3(plane, p1) / div; @@ -1927,34 +1960,34 @@ bool clip_segment_v3_plane(float p1[3], float p2[3], const float plane[4]) if (t >= 1.0f) { zero_v3(p1); zero_v3(p2); - return 0; + return false; } /* intersect plane */ if (t > 0.0f) { madd_v3_v3v3fl(pc, p1, dp, t); copy_v3_v3(p1, pc); - return 1; + return true; } - return 1; + return true; } else { /* behind plane, completely clipped */ if (t <= 0.0f) { zero_v3(p1); zero_v3(p2); - return 0; + return false; } /* intersect plane */ if (t < 1.0f) { madd_v3_v3v3fl(pc, p1, dp, t); copy_v3_v3(p2, pc); - return 1; + return true; } - return 1; + return true; } } @@ -2183,12 +2216,12 @@ static bool barycentric_weights(const float v1[3], const float v2[3], const floa if (fabsf(wtot) > FLT_EPSILON) { mul_v3_fl(w, 1.0f / wtot); - return 0; + return false; } else { /* zero area triangle */ copy_v3_fl(w, 1.0f / 3.0f); - return 1; + return true; } } @@ -2239,8 +2272,9 @@ void interp_weights_face_v3(float w[4], const float v1[3], const float v2[3], co } } } - else + else { barycentric_weights(v1, v2, v3, co, n, w); + } } } @@ -2266,11 +2300,11 @@ int barycentric_inside_triangle_v2(const float w[3]) /* returns 0 for degenerated triangles */ bool barycentric_coords_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3]) { - float x = co[0], y = co[1]; - float x1 = v1[0], y1 = v1[1]; - float x2 = v2[0], y2 = v2[1]; - float x3 = v3[0], y3 = v3[1]; - float det = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3); + const float x = co[0], y = co[1]; + const float x1 = v1[0], y1 = v1[1]; + const float x2 = v2[0], y2 = v2[1]; + const float x3 = v3[0], y3 = v3[1]; + const float det = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3); if (fabsf(det) > FLT_EPSILON) { w[0] = ((y2 - y3) * (x - x3) + (x3 - x2) * (y - y3)) / det; @@ -2319,8 +2353,9 @@ void barycentric_weights_v2_persp(const float v1[4], const float v2[4], const fl if (wtot != 0.0f) { mul_v3_fl(w, 1.0f / wtot); } - else /* dummy values for zero area face */ + else { /* dummy values for zero area face */ w[0] = w[1] = w[2] = 1.0f / 3.0f; + } } /* same as #barycentric_weights_v2 but works with a quad, @@ -2355,8 +2390,7 @@ void barycentric_weights_v2_quad(const float v1[2], const float v2[2], const flo if (UNLIKELY(lens[0] < FLT_EPSILON)) { w[0] = 1.0f; w[1] = w[2] = w[3] = 0.0f; } else if (UNLIKELY(lens[1] < FLT_EPSILON)) { w[1] = 1.0f; w[0] = w[2] = w[3] = 0.0f; } else if (UNLIKELY(lens[2] < FLT_EPSILON)) { w[2] = 1.0f; w[0] = w[1] = w[3] = 0.0f; } - else if (UNLIKELY(lens[3] < FLT_EPSILON)) { w[3] = 1.0f; w[0] = w[1] = w[2] = 0.0f; - } + else if (UNLIKELY(lens[3] < FLT_EPSILON)) { w[3] = 1.0f; w[0] = w[1] = w[2] = 0.0f; } else { float wtot, area; @@ -2563,7 +2597,7 @@ static float mean_value_half_tan_v2(const float v1[2], const float v2[2], const void interp_weights_poly_v3(float *w, float v[][3], const int n, const float co[3]) { - const float eps = 0.00001f; /* take care, low values cause [#36105] */ + const float eps = 1e-5f; /* take care, low values cause [#36105] */ const float eps_sq = eps * eps; const float *v_curr, *v_next; float ht_prev, ht; /* half tangents */ @@ -2632,7 +2666,7 @@ void interp_weights_poly_v3(float *w, float v[][3], const int n, const float co[ void interp_weights_poly_v2(float *w, float v[][2], const int n, const float co[2]) { - const float eps = 0.00001f; /* take care, low values cause [#36105] */ + const float eps = 1e-5f; /* take care, low values cause [#36105] */ const float eps_sq = eps * eps; const float *v_curr, *v_next; float ht_prev, ht; /* half tangents */ @@ -2702,8 +2736,8 @@ void interp_weights_poly_v2(float *w, float v[][2], const int n, const float co[ void interp_cubic_v3(float x[3], float v[3], const float x1[3], const float v1[3], const float x2[3], const float v2[3], const float t) { float a[3], b[3]; - float t2 = t * t; - float t3 = t2 * t; + const float t2 = t * t; + const float t3 = t2 * t; /* cubic interpolation */ a[0] = v1[0] + v2[0] + 2 * (x1[0] - x2[0]); @@ -2928,11 +2962,9 @@ void orthographic_m4(float matrix[4][4], const float left, const float right, co void perspective_m4(float mat[4][4], const float left, const float right, const float bottom, const float top, const float nearClip, const float farClip) { - float Xdelta, Ydelta, Zdelta; - - Xdelta = right - left; - Ydelta = top - bottom; - Zdelta = farClip - nearClip; + const float Xdelta = right - left; + const float Ydelta = top - bottom; + const float Zdelta = farClip - nearClip; if (Xdelta == 0.0f || Ydelta == 0.0f || Zdelta == 0.0f) { return; @@ -2946,7 +2978,7 @@ void perspective_m4(float mat[4][4], const float left, const float right, const mat[3][2] = (-2.0f * nearClip * farClip) / Zdelta; mat[0][1] = mat[0][2] = mat[0][3] = mat[1][0] = mat[1][2] = mat[1][3] = - mat[3][0] = mat[3][1] = mat[3][3] = 0.0; + mat[3][0] = mat[3][1] = mat[3][3] = 0.0f; } @@ -2995,7 +3027,6 @@ static void i_multmatrix(float icand[4][4], float Vm[4][4]) void polarview_m4(float Vm[4][4], float dist, float azimuth, float incidence, float twist) { - unit_m4(Vm); translate_m4(Vm, 0.0, 0.0, -dist); @@ -3017,16 +3048,16 @@ void lookat_m4(float mat[4][4], float vx, float vy, float vz, float px, float py dx = px - vx; dy = py - vy; dz = pz - vz; - hyp = dx * dx + dz * dz; /* hyp squared */ + hyp = dx * dx + dz * dz; /* hyp squared */ hyp1 = sqrtf(dy * dy + hyp); - hyp = sqrtf(hyp); /* the real hyp */ + hyp = sqrtf(hyp); /* the real hyp */ - if (hyp1 != 0.0f) { /* rotate X */ + if (hyp1 != 0.0f) { /* rotate X */ sine = -dy / hyp1; cosine = hyp / hyp1; } else { - sine = 0; + sine = 0.0f; cosine = 1.0f; } mat1[1][1] = cosine; @@ -3036,16 +3067,16 @@ void lookat_m4(float mat[4][4], float vx, float vy, float vz, float px, float py i_multmatrix(mat1, mat); - mat1[1][1] = mat1[2][2] = 1.0f; /* be careful here to reinit */ - mat1[1][2] = mat1[2][1] = 0.0; /* those modified by the last */ + mat1[1][1] = mat1[2][2] = 1.0f; /* be careful here to reinit */ + mat1[1][2] = mat1[2][1] = 0.0f; /* those modified by the last */ - /* paragraph */ - if (hyp != 0.0f) { /* rotate Y */ + /* paragraph */ + if (hyp != 0.0f) { /* rotate Y */ sine = dx / hyp; cosine = -dz / hyp; } else { - sine = 0; + sine = 0.0f; cosine = 1.0f; } mat1[0][0] = cosine; @@ -3226,10 +3257,10 @@ void accumulate_vertex_normals_poly(float **vertnos, const float polyno[3], void tangent_from_uv(float uv1[2], float uv2[2], float uv3[3], float co1[3], float co2[3], float co3[3], float n[3], float tang[3]) { - float s1 = uv2[0] - uv1[0]; - float s2 = uv3[0] - uv1[0]; - float t1 = uv2[1] - uv1[1]; - float t2 = uv3[1] - uv1[1]; + const float s1 = uv2[0] - uv1[0]; + const float s2 = uv3[0] - uv1[0]; + const float t1 = uv2[1] - uv1[1]; + const float t2 = uv3[1] - uv1[1]; float det = (s1 * t2 - s2 * t1); if (det != 0.0f) { /* otherwise 'tang' becomes nan */ @@ -3254,7 +3285,7 @@ void tangent_from_uv(float uv1[2], float uv2[2], float uv3[3], float co1[3], flo } } else { - tang[0] = tang[1] = tang[2] = 0.0; + tang[0] = tang[1] = tang[2] = 0.0f; } } @@ -3287,7 +3318,8 @@ void vcloud_estimate_transform(int list_size, float (*pos)[3], float *weight, fl float lloc[3], float rloc[3], float lrot[3][3], float lscale[3][3]) { float accu_com[3] = {0.0f, 0.0f, 0.0f}, accu_rcom[3] = {0.0f, 0.0f, 0.0f}; - float accu_weight = 0.0f, accu_rweight = 0.0f, eps = 0.000001f; + float accu_weight = 0.0f, accu_rweight = 0.0f; + const float eps = 1e-6f; int a; /* first set up a nice default response */ @@ -3381,7 +3413,7 @@ void vcloud_estimate_transform(int list_size, float (*pos)[3], float *weight, fl /* this is pretty much Polardecompose 'inline' the algo based on Higham's thesis */ /* without the far case ... but seems to work here pretty neat */ - odet = 0.f; + odet = 0.0f; ndet = determinant_m3_array(q); while ((odet - ndet) * (odet - ndet) > eps && i < imax) { invert_m3_m3(qi, q); @@ -3422,9 +3454,8 @@ bool form_factor_visible_quad(const float p[3], const float n[3], float q0[3], float q1[3], float q2[3], float q3[3]) { static const float epsilon = 1e-6f; - float c, sd[3]; - - c = dot_v3v3(n, p); + float sd[3]; + const float c = dot_v3v3(n, p); /* signed distances from the vertices to the plane. */ sd[0] = dot_v3v3(n, v0) - c; @@ -3435,16 +3466,16 @@ bool form_factor_visible_quad(const float p[3], const float n[3], if (fabsf(sd[1]) < epsilon) sd[1] = 0.0f; if (fabsf(sd[2]) < epsilon) sd[2] = 0.0f; - if (sd[0] > 0) { - if (sd[1] > 0) { - if (sd[2] > 0) { + if (sd[0] > 0.0f) { + if (sd[1] > 0.0f) { + if (sd[2] > 0.0f) { /* +++ */ copy_v3_v3(q0, v0); copy_v3_v3(q1, v1); copy_v3_v3(q2, v2); copy_v3_v3(q3, q2); } - else if (sd[2] < 0) { + else if (sd[2] < 0.0f) { /* ++- */ copy_v3_v3(q0, v0); copy_v3_v3(q1, v1); @@ -3459,15 +3490,15 @@ bool form_factor_visible_quad(const float p[3], const float n[3], copy_v3_v3(q3, q2); } } - else if (sd[1] < 0) { - if (sd[2] > 0) { + else if (sd[1] < 0.0f) { + if (sd[2] > 0.0f) { /* +-+ */ copy_v3_v3(q0, v0); vec_add_dir(q1, v0, v1, (sd[0] / (sd[0] - sd[1]))); vec_add_dir(q2, v1, v2, (sd[1] / (sd[1] - sd[2]))); copy_v3_v3(q3, v2); } - else if (sd[2] < 0) { + else if (sd[2] < 0.0f) { /* +-- */ copy_v3_v3(q0, v0); vec_add_dir(q1, v0, v1, (sd[0] / (sd[0] - sd[1]))); @@ -3483,14 +3514,14 @@ bool form_factor_visible_quad(const float p[3], const float n[3], } } else { - if (sd[2] > 0) { + if (sd[2] > 0.0f) { /* +0+ */ copy_v3_v3(q0, v0); copy_v3_v3(q1, v1); copy_v3_v3(q2, v2); copy_v3_v3(q3, q2); } - else if (sd[2] < 0) { + else if (sd[2] < 0.0f) { /* +0- */ copy_v3_v3(q0, v0); copy_v3_v3(q1, v1); @@ -3506,16 +3537,16 @@ bool form_factor_visible_quad(const float p[3], const float n[3], } } } - else if (sd[0] < 0) { - if (sd[1] > 0) { - if (sd[2] > 0) { + else if (sd[0] < 0.0f) { + if (sd[1] > 0.0f) { + if (sd[2] > 0.0f) { /* -++ */ vec_add_dir(q0, v0, v1, (sd[0] / (sd[0] - sd[1]))); copy_v3_v3(q1, v1); copy_v3_v3(q2, v2); vec_add_dir(q3, v0, v2, (sd[0] / (sd[0] - sd[2]))); } - else if (sd[2] < 0) { + else if (sd[2] < 0.0f) { /* -+- */ vec_add_dir(q0, v0, v1, (sd[0] / (sd[0] - sd[1]))); copy_v3_v3(q1, v1); @@ -3530,15 +3561,15 @@ bool form_factor_visible_quad(const float p[3], const float n[3], copy_v3_v3(q3, q2); } } - else if (sd[1] < 0) { - if (sd[2] > 0) { + else if (sd[1] < 0.0f) { + if (sd[2] > 0.0f) { /* --+ */ vec_add_dir(q0, v0, v2, (sd[0] / (sd[0] - sd[2]))); vec_add_dir(q1, v1, v2, (sd[1] / (sd[1] - sd[2]))); copy_v3_v3(q2, v2); copy_v3_v3(q3, q2); } - else if (sd[2] < 0) { + else if (sd[2] < 0.0f) { /* --- */ return false; } @@ -3548,14 +3579,14 @@ bool form_factor_visible_quad(const float p[3], const float n[3], } } else { - if (sd[2] > 0) { + if (sd[2] > 0.0f) { /* -0+ */ vec_add_dir(q0, v0, v2, (sd[0] / (sd[0] - sd[2]))); copy_v3_v3(q1, v1); copy_v3_v3(q2, v2); copy_v3_v3(q3, q2); } - else if (sd[2] < 0) { + else if (sd[2] < 0.0f) { /* -0- */ return false; } @@ -3566,15 +3597,15 @@ bool form_factor_visible_quad(const float p[3], const float n[3], } } else { - if (sd[1] > 0) { - if (sd[2] > 0) { + if (sd[1] > 0.0f) { + if (sd[2] > 0.0f) { /* 0++ */ copy_v3_v3(q0, v0); copy_v3_v3(q1, v1); copy_v3_v3(q2, v2); copy_v3_v3(q3, q2); } - else if (sd[2] < 0) { + else if (sd[2] < 0.0f) { /* 0+- */ copy_v3_v3(q0, v0); copy_v3_v3(q1, v1); @@ -3589,15 +3620,15 @@ bool form_factor_visible_quad(const float p[3], const float n[3], copy_v3_v3(q3, q2); } } - else if (sd[1] < 0) { - if (sd[2] > 0) { + else if (sd[1] < 0.0f) { + if (sd[2] > 0.0f) { /* 0-+ */ copy_v3_v3(q0, v0); vec_add_dir(q1, v1, v2, (sd[1] / (sd[1] - sd[2]))); copy_v3_v3(q2, v2); copy_v3_v3(q3, q2); } - else if (sd[2] < 0) { + else if (sd[2] < 0.0f) { /* 0-- */ return false; } @@ -3607,14 +3638,14 @@ bool form_factor_visible_quad(const float p[3], const float n[3], } } else { - if (sd[2] > 0) { + if (sd[2] > 0.0f) { /* 00+ */ copy_v3_v3(q0, v0); copy_v3_v3(q1, v1); copy_v3_v3(q2, v2); copy_v3_v3(q3, q2); } - else if (sd[2] < 0) { + else if (sd[2] < 0.0f) { /* 00- */ return false; } @@ -3769,7 +3800,7 @@ static void ff_normalize(float n[3]) d = dot_v3v3(n, n); - if (d > 1.0e-35F) { + if (d > 1.0e-35f) { d = 1.0f / sqrtf(d); n[0] *= d; diff --git a/source/blender/blenlib/intern/math_matrix.c b/source/blender/blenlib/intern/math_matrix.c index 7fc30e3112b..152b31754bb 100644 --- a/source/blender/blenlib/intern/math_matrix.c +++ b/source/blender/blenlib/intern/math_matrix.c @@ -47,19 +47,19 @@ void zero_m4(float m[4][4]) void unit_m3(float m[3][3]) { - m[0][0] = m[1][1] = m[2][2] = 1.0; - m[0][1] = m[0][2] = 0.0; - m[1][0] = m[1][2] = 0.0; - m[2][0] = m[2][1] = 0.0; + m[0][0] = m[1][1] = m[2][2] = 1.0f; + m[0][1] = m[0][2] = 0.0f; + m[1][0] = m[1][2] = 0.0f; + m[2][0] = m[2][1] = 0.0f; } void unit_m4(float m[4][4]) { - m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0; - m[0][1] = m[0][2] = m[0][3] = 0.0; - m[1][0] = m[1][2] = m[1][3] = 0.0; - m[2][0] = m[2][1] = m[2][3] = 0.0; - m[3][0] = m[3][1] = m[3][2] = 0.0; + m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0f; + m[0][1] = m[0][2] = m[0][3] = 0.0f; + m[1][0] = m[1][2] = m[1][3] = 0.0f; + m[2][0] = m[2][1] = m[2][3] = 0.0f; + m[3][0] = m[3][1] = m[3][2] = 0.0f; } void copy_m3_m3(float m1[3][3], float m2[3][3]) @@ -103,14 +103,14 @@ void copy_m4_m3(float m1[4][4], float m2[3][3]) /* no clear */ m1[2][2] = m2[2][2]; /* Reevan's Bugfix */ - m1[0][3] = 0.0F; - m1[1][3] = 0.0F; - m1[2][3] = 0.0F; + m1[0][3] = 0.0f; + m1[1][3] = 0.0f; + m1[2][3] = 0.0f; - m1[3][0] = 0.0F; - m1[3][1] = 0.0F; - m1[3][2] = 0.0F; - m1[3][3] = 1.0F; + m1[3][0] = 0.0f; + m1[3][1] = 0.0f; + m1[3][2] = 0.0f; + m1[3][3] = 1.0f; } @@ -274,71 +274,148 @@ void mul_m4_m3m4(float m1[4][4], float m3_[3][3], float m2_[4][4]) m1[2][2] = m2[2][0] * m3[0][2] + m2[2][1] * m3[1][2] + m2[2][2] * m3[2][2]; } -void mul_serie_m3(float answ[3][3], - float m1[3][3], float m2[3][3], float m3[3][3], - float m4[3][3], float m5[3][3], float m6[3][3], - float m7[3][3], float m8[3][3]) -{ - float temp[3][3]; - - if (m1 == NULL || m2 == NULL) return; - - mul_m3_m3m3(answ, m2, m1); - if (m3) { - mul_m3_m3m3(temp, m3, answ); - if (m4) { - mul_m3_m3m3(answ, m4, temp); - if (m5) { - mul_m3_m3m3(temp, m5, answ); - if (m6) { - mul_m3_m3m3(answ, m6, temp); - if (m7) { - mul_m3_m3m3(temp, m7, answ); - if (m8) { - mul_m3_m3m3(answ, m8, temp); - } - else copy_m3_m3(answ, temp); - } - } - else copy_m3_m3(answ, temp); - } - } - else copy_m3_m3(answ, temp); - } -} -void mul_serie_m4(float answ[4][4], float m1[4][4], - float m2[4][4], float m3[4][4], float m4[4][4], - float m5[4][4], float m6[4][4], float m7[4][4], - float m8[4][4]) -{ - float temp[4][4]; - - if (m1 == NULL || m2 == NULL) return; - - mul_m4_m4m4(answ, m1, m2); - if (m3) { - mul_m4_m4m4(temp, answ, m3); - if (m4) { - mul_m4_m4m4(answ, temp, m4); - if (m5) { - mul_m4_m4m4(temp, answ, m5); - if (m6) { - mul_m4_m4m4(answ, temp, m6); - if (m7) { - mul_m4_m4m4(temp, answ, m7); - if (m8) { - mul_m4_m4m4(answ, temp, m8); - } - else copy_m4_m4(answ, temp); - } - } - else copy_m4_m4(answ, temp); - } - } - else copy_m4_m4(answ, temp); - } -} +/** \name Macro helpers for: mul_m3_series + * \{ */ +void _va_mul_m3_series_3( + float r[3][3], + float m1[3][3], float m2[3][3]) +{ + mul_m3_m3m3(r, m1, m2); +} +void _va_mul_m3_series_4( + float r[3][3], + float m1[3][3], float m2[3][3], float m3[3][3]) +{ + mul_m3_m3m3(r, m1, m2); + mul_m3_m3m3(r, r, m3); +} +void _va_mul_m3_series_5( + float r[3][3], + float m1[3][3], float m2[3][3], float m3[3][3], float m4[3][3]) +{ + mul_m3_m3m3(r, m1, m2); + mul_m3_m3m3(r, r, m3); + mul_m3_m3m3(r, r, m4); +} +void _va_mul_m3_series_6( + float r[3][3], + float m1[3][3], float m2[3][3], float m3[3][3], float m4[3][3], + float m5[3][3]) +{ + mul_m3_m3m3(r, m1, m2); + mul_m3_m3m3(r, r, m3); + mul_m3_m3m3(r, r, m4); + mul_m3_m3m3(r, r, m5); +} +void _va_mul_m3_series_7( + float r[3][3], + float m1[3][3], float m2[3][3], float m3[3][3], float m4[3][3], + float m5[3][3], float m6[3][3]) +{ + mul_m3_m3m3(r, m1, m2); + mul_m3_m3m3(r, r, m3); + mul_m3_m3m3(r, r, m4); + mul_m3_m3m3(r, r, m5); + mul_m3_m3m3(r, r, m6); +} +void _va_mul_m3_series_8( + float r[3][3], + float m1[3][3], float m2[3][3], float m3[3][3], float m4[3][3], + float m5[3][3], float m6[3][3], float m7[3][3]) +{ + mul_m3_m3m3(r, m1, m2); + mul_m3_m3m3(r, r, m3); + mul_m3_m3m3(r, r, m4); + mul_m3_m3m3(r, r, m5); + mul_m3_m3m3(r, r, m6); + mul_m3_m3m3(r, r, m7); +} +void _va_mul_m3_series_9( + float r[3][3], + float m1[3][3], float m2[3][3], float m3[3][3], float m4[3][3], + float m5[3][3], float m6[3][3], float m7[3][3], float m8[3][3]) +{ + mul_m3_m3m3(r, m1, m2); + mul_m3_m3m3(r, r, m3); + mul_m3_m3m3(r, r, m4); + mul_m3_m3m3(r, r, m5); + mul_m3_m3m3(r, r, m6); + mul_m3_m3m3(r, r, m7); + mul_m3_m3m3(r, r, m8); +} +/** \} */ + +/** \name Macro helpers for: mul_m4_series + * \{ */ +void _va_mul_m4_series_3( + float r[4][4], + float m1[4][4], float m2[4][4]) +{ + mul_m4_m4m4(r, m1, m2); +} +void _va_mul_m4_series_4( + float r[4][4], + float m1[4][4], float m2[4][4], float m3[4][4]) +{ + mul_m4_m4m4(r, m1, m2); + mul_m4_m4m4(r, r, m3); +} +void _va_mul_m4_series_5( + float r[4][4], + float m1[4][4], float m2[4][4], float m3[4][4], float m4[4][4]) +{ + mul_m4_m4m4(r, m1, m2); + mul_m4_m4m4(r, r, m3); + mul_m4_m4m4(r, r, m4); +} +void _va_mul_m4_series_6( + float r[4][4], + float m1[4][4], float m2[4][4], float m3[4][4], float m4[4][4], + float m5[4][4]) +{ + mul_m4_m4m4(r, m1, m2); + mul_m4_m4m4(r, r, m3); + mul_m4_m4m4(r, r, m4); + mul_m4_m4m4(r, r, m5); +} +void _va_mul_m4_series_7( + float r[4][4], + float m1[4][4], float m2[4][4], float m3[4][4], float m4[4][4], + float m5[4][4], float m6[4][4]) +{ + mul_m4_m4m4(r, m1, m2); + mul_m4_m4m4(r, r, m3); + mul_m4_m4m4(r, r, m4); + mul_m4_m4m4(r, r, m5); + mul_m4_m4m4(r, r, m6); +} +void _va_mul_m4_series_8( + float r[4][4], + float m1[4][4], float m2[4][4], float m3[4][4], float m4[4][4], + float m5[4][4], float m6[4][4], float m7[4][4]) +{ + mul_m4_m4m4(r, m1, m2); + mul_m4_m4m4(r, r, m3); + mul_m4_m4m4(r, r, m4); + mul_m4_m4m4(r, r, m5); + mul_m4_m4m4(r, r, m6); + mul_m4_m4m4(r, r, m7); +} +void _va_mul_m4_series_9( + float r[4][4], + float m1[4][4], float m2[4][4], float m3[4][4], float m4[4][4], + float m5[4][4], float m6[4][4], float m7[4][4], float m8[4][4]) +{ + mul_m4_m4m4(r, m1, m2); + mul_m4_m4m4(r, r, m3); + mul_m4_m4m4(r, r, m4); + mul_m4_m4m4(r, r, m5); + mul_m4_m4m4(r, r, m6); + mul_m4_m4m4(r, r, m7); + mul_m4_m4m4(r, r, m8); +} +/** \} */ void mul_v2_m3v2(float r[2], float m[3][3], float v[2]) { @@ -360,10 +437,9 @@ void mul_m3_v2(float m[3][3], float r[2]) void mul_m4_v3(float mat[4][4], float vec[3]) { - float x, y; + const float x = vec[0]; + const float y = vec[1]; - x = vec[0]; - y = vec[1]; vec[0] = x * mat[0][0] + y * mat[1][0] + mat[2][0] * vec[2] + mat[3][0]; vec[1] = x * mat[0][1] + y * mat[1][1] + mat[2][1] * vec[2] + mat[3][1]; vec[2] = x * mat[0][2] + y * mat[1][2] + mat[2][2] * vec[2] + mat[3][2]; @@ -371,10 +447,9 @@ void mul_m4_v3(float mat[4][4], float vec[3]) void mul_v3_m4v3(float r[3], float mat[4][4], const float vec[3]) { - float x, y; + const float x = vec[0]; + const float y = vec[1]; - x = vec[0]; - y = vec[1]; r[0] = x * mat[0][0] + y * mat[1][0] + mat[2][0] * vec[2] + mat[3][0]; r[1] = x * mat[0][1] + y * mat[1][1] + mat[2][1] * vec[2] + mat[3][1]; r[2] = x * mat[0][2] + y * mat[1][2] + mat[2][2] * vec[2] + mat[3][2]; @@ -382,18 +457,16 @@ void mul_v3_m4v3(float r[3], float mat[4][4], const float vec[3]) void mul_v2_m4v3(float r[2], float mat[4][4], const float vec[3]) { - float x; + const float x = vec[0]; - x = vec[0]; r[0] = x * mat[0][0] + vec[1] * mat[1][0] + mat[2][0] * vec[2] + mat[3][0]; r[1] = x * mat[0][1] + vec[1] * mat[1][1] + mat[2][1] * vec[2] + mat[3][1]; } void mul_v2_m2v2(float r[2], float mat[2][2], const float vec[2]) { - float x; + const float x = vec[0]; - x = vec[0]; r[0] = mat[0][0] * x + mat[1][0] * vec[1]; r[1] = mat[0][1] * x + mat[1][1] * vec[1]; } @@ -406,10 +479,9 @@ void mul_m2v2(float mat[2][2], float vec[2]) /* same as mul_m4_v3() but doesnt apply translation component */ void mul_mat3_m4_v3(float mat[4][4], float vec[3]) { - float x, y; + const float x = vec[0]; + const float y = vec[1]; - x = vec[0]; - y = vec[1]; vec[0] = x * mat[0][0] + y * mat[1][0] + mat[2][0] * vec[2]; vec[1] = x * mat[0][1] + y * mat[1][1] + mat[2][1] * vec[2]; vec[2] = x * mat[0][2] + y * mat[1][2] + mat[2][2] * vec[2]; @@ -436,11 +508,9 @@ void mul_v2_project_m4_v3(float r[2], float mat[4][4], const float vec[3]) void mul_v4_m4v4(float r[4], float mat[4][4], const float v[4]) { - float x, y, z; - - x = v[0]; - y = v[1]; - z = v[2]; + const float x = v[0]; + const float y = v[1]; + const float z = v[2]; r[0] = x * mat[0][0] + y * mat[1][0] + z * mat[2][0] + mat[3][0] * v[3]; r[1] = x * mat[0][1] + y * mat[1][1] + z * mat[2][1] + mat[3][1] * v[3]; @@ -455,11 +525,9 @@ void mul_m4_v4(float mat[4][4], float r[4]) void mul_v4d_m4v4d(double r[4], float mat[4][4], double v[4]) { - double x, y, z; - - x = v[0]; - y = v[1]; - z = v[2]; + const double x = v[0]; + const double y = v[1]; + const double z = v[2]; r[0] = x * (double)mat[0][0] + y * (double)mat[1][0] + z * (double)mat[2][0] + (double)mat[3][0] * v[3]; r[1] = x * (double)mat[0][1] + y * (double)mat[1][1] + z * (double)mat[2][1] + (double)mat[3][1] * v[3]; @@ -499,10 +567,9 @@ void mul_m3_v3(float M[3][3], float r[3]) void mul_transposed_m3_v3(float mat[3][3], float vec[3]) { - float x, y; + const float x = vec[0]; + const float y = vec[1]; - x = vec[0]; - y = vec[1]; vec[0] = x * mat[0][0] + y * mat[0][1] + mat[0][2] * vec[2]; vec[1] = x * mat[1][0] + y * mat[1][1] + mat[1][2] * vec[2]; vec[2] = x * mat[2][0] + y * mat[2][1] + mat[2][2] * vec[2]; @@ -510,10 +577,9 @@ void mul_transposed_m3_v3(float mat[3][3], float vec[3]) void mul_transposed_mat3_m4_v3(float mat[4][4], float vec[3]) { - float x, y; + const float x = vec[0]; + const float y = vec[1]; - x = vec[0]; - y = vec[1]; vec[0] = x * mat[0][0] + y * mat[0][1] + mat[0][2] * vec[2]; vec[1] = x * mat[1][0] + y * mat[1][1] + mat[1][2] * vec[2]; vec[2] = x * mat[2][0] + y * mat[2][1] + mat[2][2] * vec[2]; @@ -566,10 +632,9 @@ void negate_m4(float m[4][4]) void mul_m3_v3_double(float mat[3][3], double vec[3]) { - double x, y; + const double x = vec[0]; + const double y = vec[1]; - x = vec[0]; - y = vec[1]; vec[0] = x * (double)mat[0][0] + y * (double)mat[1][0] + (double)mat[2][0] * vec[2]; vec[1] = x * (double)mat[0][1] + y * (double)mat[1][1] + (double)mat[2][1] * vec[2]; vec[2] = x * (double)mat[0][2] + y * (double)mat[1][2] + (double)mat[2][2] * vec[2]; @@ -621,11 +686,9 @@ float determinant_m3_array(float m[3][3]) bool invert_m3_ex(float m[3][3], const float epsilon) { float tmp[3][3]; - bool success; + const bool success = invert_m3_m3_ex(tmp, m, epsilon); - success = invert_m3_m3_ex(tmp, m, epsilon); copy_m3_m3(m, tmp); - return success; } @@ -659,11 +722,9 @@ bool invert_m3_m3_ex(float m1[3][3], float m2[3][3], const float epsilon) bool invert_m3(float m[3][3]) { float tmp[3][3]; - bool success; + const bool success = invert_m3_m3(tmp, m); - success = invert_m3_m3(tmp, m); copy_m3_m3(m, tmp); - return success; } @@ -696,11 +757,9 @@ bool invert_m3_m3(float m1[3][3], float m2[3][3]) bool invert_m4(float m[4][4]) { float tmp[4][4]; - bool success; + const bool success = invert_m4_m4(tmp, m); - success = invert_m4_m4(tmp, m); copy_m4_m4(m, tmp); - return success; } @@ -2115,7 +2174,7 @@ void pseudoinverse_m4_m4(float Ainv[4][4], float A[4][4], float epsilon) transpose_m4(V); - mul_serie_m4(Ainv, U, Wm, V, NULL, NULL, NULL, NULL, NULL); + mul_m4_series(Ainv, U, Wm, V); } void pseudoinverse_m3_m3(float Ainv[3][3], float A[3][3], float epsilon) @@ -2156,3 +2215,51 @@ void invert_m4_m4_safe(float Ainv[4][4], float A[4][4]) } } } + +/** + * SpaceTransform struct encapsulates all needed data to convert between two coordinate spaces + * (where conversion can be represented by a matrix multiplication). + * + * A SpaceTransform is initialized using: + * BLI_SPACE_TRANSFORM_SETUP(&data, ob1, ob2) + * + * After that the following calls can be used: + * BLI_space_transform_apply(&data, co); // converts a coordinate in ob1 space to the corresponding ob2 space + * BLI_space_transform_invert(&data, co); // converts a coordinate in ob2 space to the corresponding ob1 space + * + * Same concept as BLI_space_transform_apply and BLI_space_transform_invert, but no is normalized after conversion + * (and not translated at all!): + * BLI_space_transform_apply_normal(&data, no); + * BLI_space_transform_invert_normal(&data, no); + * + */ + +void BLI_space_transform_from_matrices(SpaceTransform *data, float local[4][4], float target[4][4]) +{ + float itarget[4][4]; + invert_m4_m4(itarget, target); + mul_m4_m4m4(data->local2target, itarget, local); + invert_m4_m4(data->target2local, data->local2target); +} + +void BLI_space_transform_apply(const SpaceTransform *data, float co[3]) +{ + mul_v3_m4v3(co, ((SpaceTransform *)data)->local2target, co); +} + +void BLI_space_transform_invert(const SpaceTransform *data, float co[3]) +{ + mul_v3_m4v3(co, ((SpaceTransform *)data)->target2local, co); +} + +void BLI_space_transform_apply_normal(const SpaceTransform *data, float no[3]) +{ + mul_mat3_m4_v3(((SpaceTransform *)data)->local2target, no); + normalize_v3(no); +} + +void BLI_space_transform_invert_normal(const SpaceTransform *data, float no[3]) +{ + mul_mat3_m4_v3(((SpaceTransform *)data)->target2local, no); + normalize_v3(no); +} diff --git a/source/blender/blenlib/intern/math_rotation.c b/source/blender/blenlib/intern/math_rotation.c index dce2e9d54e3..141f9201689 100644 --- a/source/blender/blenlib/intern/math_rotation.c +++ b/source/blender/blenlib/intern/math_rotation.c @@ -139,7 +139,7 @@ float dot_qtqt(const float q1[4], const float q2[4]) void invert_qt(float q[4]) { - float f = dot_qtqt(q, q); + const float f = dot_qtqt(q, q); if (f == 0.0f) return; @@ -380,9 +380,8 @@ void mat3_to_quat_is_ok(float q[4], float wmat[3][3]) float normalize_qt(float q[4]) { - float len; + const float len = sqrtf(dot_qtqt(q, q)); - len = sqrtf(dot_qtqt(q, q)); if (len != 0.0f) { mul_qt_fl(q, 1.0f / len); } @@ -520,7 +519,7 @@ float angle_qtqt(const float q1[4], const float q2[4]) void vec_to_quat(float q[4], const float vec[3], short axis, const short upflag) { - const float eps = 0.0001f; + const float eps = 1e-4f; float nor[3], tvec[3]; float angle, si, co, len; @@ -669,7 +668,7 @@ void QuatInterpolW(float *result, float quat1[4], float quat2[4], float t) */ void interp_dot_slerp(const float t, const float cosom, float r_w[2]) { - const float eps = 0.0001f; + const float eps = 1e-4f; BLI_assert(IN_RANGE_INCL(cosom, -1.0001f, 1.0001f)); @@ -783,9 +782,8 @@ void tri_to_quat_ex(float quat[4], const float v1[3], const float v2[3], const f float tri_to_quat(float quat[4], const float v1[3], const float v2[3], const float v3[3]) { float vec[3]; - float len; + const float len = normal_tri_v3(vec, v1, v2, v3); - len = normal_tri_v3(vec, v1, v2, v3); tri_to_quat_ex(quat, v1, v2, v3, vec); return len; } @@ -1606,7 +1604,7 @@ void mat4_to_dquat(DualQuat *dq, float basemat[4][4], float mat[4][4]) mul_m4_m4m4(S, baseRinv, baseRS); /* set scaling part */ - mul_serie_m4(dq->scale, basemat, S, baseinv, NULL, NULL, NULL, NULL, NULL); + mul_m4_series(dq->scale, basemat, S, baseinv); dq->scale_weight = 1.0f; } else { @@ -1658,7 +1656,7 @@ void add_weighted_dq_dq(DualQuat *dqsum, const DualQuat *dq, float weight) /* make sure we interpolate quats in the right direction */ if (dot_qtqt(dq->quat, dqsum->quat) < 0) { - flipped = 1; + flipped = true; weight = -weight; } @@ -1689,7 +1687,7 @@ void add_weighted_dq_dq(DualQuat *dqsum, const DualQuat *dq, float weight) void normalize_dq(DualQuat *dq, float totweight) { - float scale = 1.0f / totweight; + const float scale = 1.0f / totweight; mul_qt_fl(dq->quat, scale); mul_qt_fl(dq->trans, scale); diff --git a/source/blender/blenlib/intern/math_vector.c b/source/blender/blenlib/intern/math_vector.c index 15b88fe0c7f..6b37b0d5d32 100644 --- a/source/blender/blenlib/intern/math_vector.c +++ b/source/blender/blenlib/intern/math_vector.c @@ -35,7 +35,7 @@ void interp_v2_v2v2(float target[2], const float a[2], const float b[2], const float t) { - float s = 1.0f - t; + const float s = 1.0f - t; target[0] = s * a[0] + t * b[0]; target[1] = s * a[1] + t * b[1]; @@ -51,7 +51,7 @@ void interp_v2_v2v2v2(float p[2], const float v1[2], const float v2[2], const fl void interp_v3_v3v3(float target[3], const float a[3], const float b[3], const float t) { - float s = 1.0f - t; + const float s = 1.0f - t; target[0] = s * a[0] + t * b[0]; target[1] = s * a[1] + t * b[1]; @@ -60,7 +60,7 @@ void interp_v3_v3v3(float target[3], const float a[3], const float b[3], const f void interp_v4_v4v4(float target[4], const float a[4], const float b[4], const float t) { - float s = 1.0f - t; + const float s = 1.0f - t; target[0] = s * a[0] + t * b[0]; target[1] = s * a[1] + t * b[1]; @@ -119,8 +119,7 @@ bool interp_v2_v2v2_slerp(float target[2], const float a[2], const float b[2], c } /** - * Same as #interp_v3_v3v3_slerp buy uses fallback values - * for opposite vectors. + * Same as #interp_v3_v3v3_slerp but uses fallback values for opposite vectors. */ void interp_v3_v3v3_slerp_safe(float target[3], const float a[3], const float b[3], const float t) { @@ -208,7 +207,7 @@ void interp_v3_v3v3v3_uv(float p[3], const float v1[3], const float v2[3], const void interp_v3_v3v3_uchar(char unsigned target[3], const unsigned char a[3], const unsigned char b[3], const float t) { - float s = 1.0f - t; + const float s = 1.0f - t; target[0] = (char)floorf(s * a[0] + t * b[0]); target[1] = (char)floorf(s * a[1] + t * b[1]); @@ -221,7 +220,7 @@ void interp_v3_v3v3_char(char target[3], const char a[3], const char b[3], const void interp_v4_v4v4_uchar(char unsigned target[4], const unsigned char a[4], const unsigned char b[4], const float t) { - float s = 1.0f - t; + const float s = 1.0f - t; target[0] = (char)floorf(s * a[0] + t * b[0]); target[1] = (char)floorf(s * a[1] + t * b[1]); @@ -550,8 +549,7 @@ void angle_poly_v3(float *angles, const float *verts[3], int len) /* Project v1 on v2 */ void project_v2_v2v2(float c[2], const float v1[2], const float v2[2]) { - float mul; - mul = dot_v2v2(v1, v2) / dot_v2v2(v2, v2); + const float mul = dot_v2v2(v1, v2) / dot_v2v2(v2, v2); c[0] = mul * v2[0]; c[1] = mul * v2[1]; @@ -560,8 +558,7 @@ void project_v2_v2v2(float c[2], const float v1[2], const float v2[2]) /* Project v1 on v2 */ void project_v3_v3v3(float c[3], const float v1[3], const float v2[3]) { - float mul; - mul = dot_v3v3(v1, v2) / dot_v3v3(v2, v2); + const float mul = dot_v3v3(v1, v2) / dot_v3v3(v2, v2); c[0] = mul * v2[0]; c[1] = mul * v2[1]; @@ -837,7 +834,7 @@ double len_squared_vn(const float *array, const int size) float normalize_vn_vn(float *array_tar, const float *array_src, const int size) { - double d = len_squared_vn(array_src, size); + const double d = len_squared_vn(array_src, size); float d_sqrt; if (d > 1.0e-35) { d_sqrt = (float)sqrt(d); diff --git a/source/blender/blenlib/intern/math_vector_inline.c b/source/blender/blenlib/intern/math_vector_inline.c index 135050f4a3f..0a990e0d63d 100644 --- a/source/blender/blenlib/intern/math_vector_inline.c +++ b/source/blender/blenlib/intern/math_vector_inline.c @@ -850,17 +850,17 @@ MINLINE void normal_float_to_short_v3(short out[3], const float in[3]) MINLINE bool is_zero_v2(const float v[2]) { - return (v[0] == 0 && v[1] == 0); + return (v[0] == 0.0f && v[1] == 0.0f); } MINLINE bool is_zero_v3(const float v[3]) { - return (v[0] == 0 && v[1] == 0 && v[2] == 0); + return (v[0] == 0.0f && v[1] == 0.0f && v[2] == 0.0f); } MINLINE bool is_zero_v4(const float v[4]) { - return (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0); + return (v[0] == 0.0f && v[1] == 0.0f && v[2] == 0.0f && v[3] == 0.0f); } MINLINE bool is_finite_v2(const float v[2]) @@ -880,7 +880,7 @@ MINLINE bool is_finite_v4(const float v[4]) MINLINE bool is_one_v3(const float v[3]) { - return (v[0] == 1 && v[1] == 1 && v[2] == 1); + return (v[0] == 1.0f && v[1] == 1.0f && v[2] == 1.0f); } diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 0b89ec1f0d0..a7fa443cfc4 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -1175,7 +1175,13 @@ static bool get_path_local(char *targetpath, const char *folder_name, const char } /* try EXECUTABLE_DIR/2.5x/folder_name - new default directory for local blender installed files */ +#ifdef __APPLE__ + static char osx_resourses[FILE_MAX]; /* due new codesign situation in OSX > 10.9.5 we must move the blender_version dir with contents to Resources */ + sprintf(osx_resourses, "%s../Resources", bprogdir); + return test_path(targetpath, osx_resourses, blender_version_decimal(ver), relfolder); +#else return test_path(targetpath, bprogdir, blender_version_decimal(ver), relfolder); +#endif } /** @@ -1415,7 +1421,7 @@ const char *BLI_get_folder_create(int folder_id, const char *subfolder) const char *path; /* only for user folders */ - if (!ELEM4(folder_id, BLENDER_USER_DATAFILES, BLENDER_USER_CONFIG, BLENDER_USER_SCRIPTS, BLENDER_USER_AUTOSAVE)) + if (!ELEM(folder_id, BLENDER_USER_DATAFILES, BLENDER_USER_CONFIG, BLENDER_USER_SCRIPTS, BLENDER_USER_AUTOSAVE)) return NULL; path = BLI_get_folder(folder_id, subfolder); @@ -1515,21 +1521,6 @@ void BLI_setenv_if_new(const char *env, const char *val) BLI_setenv(env, val); } - -/** - * Changes to the path separators to the native ones for this OS. - */ -void BLI_clean(char *path) -{ -#ifdef WIN32 - if (path && BLI_strnlen(path, 3) > 2) { - BLI_char_switch(path + 2, '/', '\\'); - } -#else - BLI_char_switch(path + BLI_path_unc_prefix_len(path), '\\', '/'); -#endif -} - /** * Change every \a from in \a string into \a to. The * result will be in \a string @@ -1681,7 +1672,7 @@ void BLI_make_file_string(const char *relabase, char *string, const char *dir, c strcat(string, file); /* Push all slashes to the system preferred direction */ - BLI_clean(string); + BLI_path_native_slash(string); } static bool testextensie_ex(const char *str, const size_t str_len, @@ -1783,7 +1774,7 @@ bool BLI_replace_extension(char *path, size_t maxlen, const char *ext) ssize_t a; for (a = path_len - 1; a >= 0; a--) { - if (ELEM3(path[a], '.', '/', '\\')) { + if (ELEM(path[a], '.', '/', '\\')) { break; } } @@ -2146,6 +2137,20 @@ void BLI_del_slash(char *string) } /** + * Changes to the path separators to the native ones for this OS. + */ +void BLI_path_native_slash(char *path) +{ +#ifdef WIN32 + if (path && BLI_strnlen(path, 3) > 2) { + BLI_char_switch(path + 2, '/', '\\'); + } +#else + BLI_char_switch(path + BLI_path_unc_prefix_len(path), '\\', '/'); +#endif +} + +/** * Tries appending each of the semicolon-separated extensions in the PATHEXT * environment variable (Windows-only) onto *name in turn until such a file is found. * Returns success/failure. diff --git a/source/blender/blenlib/intern/polyfill2d.c b/source/blender/blenlib/intern/polyfill2d.c index 71cda92842a..dd829e5d80a 100644 --- a/source/blender/blenlib/intern/polyfill2d.c +++ b/source/blender/blenlib/intern/polyfill2d.c @@ -387,7 +387,7 @@ static bool kdtree2d_isect_tri_recursive( (span_tri_v2_sign(tri_coords[1], tri_coords[2], co) != CONCAVE) && (span_tri_v2_sign(tri_coords[2], tri_coords[0], co) != CONCAVE)) { - if (!ELEM3(node->index, tri_index[0], tri_index[1], tri_index[2])) { + if (!ELEM(node->index, tri_index[0], tri_index[1], tri_index[2])) { return true; } } diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c index 410f98897ce..3dff0b31091 100644 --- a/source/blender/blenlib/intern/rand.c +++ b/source/blender/blenlib/intern/rand.c @@ -95,15 +95,20 @@ void BLI_rng_srandom(RNG *rng, unsigned int seed) BLI_rng_seed(rng, seed + hash[seed & 255]); } -int BLI_rng_get_int(RNG *rng) +BLI_INLINE void rng_step(RNG *rng) { rng->X = (MULTIPLIER * rng->X + ADDEND) & MASK; +} + +int BLI_rng_get_int(RNG *rng) +{ + rng_step(rng); return (int) (rng->X >> 17); } unsigned int BLI_rng_get_uint(RNG *rng) { - rng->X = (MULTIPLIER * rng->X + ADDEND) & MASK; + rng_step(rng); return (unsigned int) (rng->X >> 17); } @@ -167,10 +172,9 @@ void BLI_rng_shuffle_array(RNG *rng, void *data, unsigned int elem_size_i, unsig void BLI_rng_skip(RNG *rng, int n) { - int i; - - for (i = 0; i < n; i++) - BLI_rng_get_int(rng); + while (n--) { + rng_step(rng); + } } /***/ diff --git a/source/blender/blenlib/intern/scanfill.c b/source/blender/blenlib/intern/scanfill.c index 05e4984d9a3..3d3dfeef302 100644 --- a/source/blender/blenlib/intern/scanfill.c +++ b/source/blender/blenlib/intern/scanfill.c @@ -865,6 +865,9 @@ unsigned int BLI_scanfill_calc_ex(ScanFillContext *sf_ctx, const int flag, const /* Newell's Method */ /* 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, + * ideally we'd calcualte connectivity and calculate each polys normal, see T41047 */ const float *v_prev; zero_v3(n); diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index 453b0cc939f..f3ecc799e1e 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -515,7 +515,7 @@ int BLI_stat(const char *path, BLI_stat_t *buffer) int BLI_wstat(const wchar_t *path, BLI_stat_t *buffer) { -#if (defined(_MSC_VER) && (_MSC_VER >= 1500)) || defined(__MINGW64__) +#if defined(_MSC_VER) || defined(__MINGW64__) return _wstat64(path, buffer); #elif defined(__MINGW32__) return _wstati64(path, buffer); diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index aa705aaf63e..eeafc1a9e8f 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -648,7 +648,7 @@ int BLI_str_rstrip_float_zero(char *str, const char pad) * \param str_array_len The length of the array, or -1 for a NULL-terminated array. * \return The index of str in str_array or -1. */ -int BLI_str_index_in_array_n(const char *str, const char **str_array, const int str_array_len) +int BLI_str_index_in_array_n(const char *__restrict str, const char **__restrict str_array, const int str_array_len) { int index; const char **str_iter = str_array; @@ -668,7 +668,7 @@ int BLI_str_index_in_array_n(const char *str, const char **str_array, const int * \param str_array Array of strings, (must be NULL-terminated). * \return The index of str in str_array or -1. */ -int BLI_str_index_in_array(const char *str, const char **str_array) +int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array) { int index; const char **str_iter = str_array; |