diff options
author | Lukas Tönne <lukas.toenne@gmail.com> | 2018-05-06 12:00:02 +0300 |
---|---|---|
committer | Lukas Tönne <lukas.toenne@gmail.com> | 2018-05-06 12:29:52 +0300 |
commit | 1447fecbbf571e076e12b3bbde73a90ea1105c33 (patch) | |
tree | d1bf5bcb09cfbca9d7448a149dad1e09478ce581 /source/blender/blenlib/intern/math_geom.c | |
parent | 99b9636bf0175402ecc676a9677429b8bdf418e6 (diff) | |
parent | 466125f3d6325204711ecbcc39db2827b928e4a9 (diff) |
Merge branch 'blender2.8' into hair_guides
Diffstat (limited to 'source/blender/blenlib/intern/math_geom.c')
-rw-r--r-- | source/blender/blenlib/intern/math_geom.c | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c index 171af1c8264..062c8e0fe77 100644 --- a/source/blender/blenlib/intern/math_geom.c +++ b/source/blender/blenlib/intern/math_geom.c @@ -3086,7 +3086,9 @@ bool barycentric_coords_v2(const float v1[2], const float v2[2], const float v3[ * \note This is *exactly* the same calculation as #resolve_tri_uv_v2, * although it has double precision and is used for texture baking, so keep both. */ -void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3]) +void barycentric_weights_v2( + const float v1[2], const float v2[2], const float v3[2], + const float co[2], float w[3]) { float wtot; @@ -3104,10 +3106,35 @@ void barycentric_weights_v2(const float v1[2], const float v2[2], const float v3 } /** + * A version of #barycentric_weights_v2 that doesn't allow negative weights. + * Useful when negative values cause problems and points are only ever slightly outside of the triangle. + */ +void barycentric_weights_v2_clamped( + const float v1[2], const float v2[2], const float v3[2], + const float co[2], float w[3]) +{ + float wtot; + + w[0] = max_ff(cross_tri_v2(v2, v3, co), 0.0f); + w[1] = max_ff(cross_tri_v2(v3, v1, co), 0.0f); + w[2] = max_ff(cross_tri_v2(v1, v2, co), 0.0f); + wtot = w[0] + w[1] + w[2]; + + if (wtot != 0.0f) { + mul_v3_fl(w, 1.0f / wtot); + } + else { /* dummy values for zero area face */ + copy_v3_fl(w, 1.0f / 3.0f); + } +} + +/** * still use 2D X,Y space but this works for verts transformed by a perspective matrix, * using their 4th component as a weight */ -void barycentric_weights_v2_persp(const float v1[4], const float v2[4], const float v3[4], const float co[2], float w[3]) +void barycentric_weights_v2_persp( + const float v1[4], const float v2[4], const float v3[4], + const float co[2], float w[3]) { float wtot; @@ -3129,8 +3156,9 @@ void barycentric_weights_v2_persp(const float v1[4], const float v2[4], const fl * note: untested for values outside the quad's bounds * this is #interp_weights_poly_v2 expanded for quads only */ -void barycentric_weights_v2_quad(const float v1[2], const float v2[2], const float v3[2], const float v4[2], - const float co[2], float w[4]) +void barycentric_weights_v2_quad( + const float v1[2], const float v2[2], const float v3[2], const float v4[2], + const float co[2], float w[4]) { /* note: fabsf() here is not needed for convex quads (and not used in interp_weights_poly_v2). * but in the case of concave/bow-tie quads for the mask rasterizer it gives unreliable results |