From 88ebe9f5296149e51cacdc400c26bff1d63c9fa0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Thu, 3 Sep 2020 16:39:32 +0000 Subject: [PATCH] limit bisectrice length in strokes, prevent oversized line joins, may be more accurate --- src/vectors.c | 11 +++++++++-- src/vectors.h | 2 ++ src/vkvg_context_internal.c | 27 ++++++++++++++++----------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/vectors.c b/src/vectors.c index 14d045b..812d197 100644 --- a/src/vectors.c +++ b/src/vectors.c @@ -62,11 +62,18 @@ vec2d vec2d_norm(vec2d a) } // multiply 2d vector by scalar vec2d vec2d_mult(vec2d a, double m){ - return (vec2d){a.x*m,a.y*m}; + return (vec2d){a.x*m,a.y*m}; +} +// devide 2d vector by scalar +vec2 vec2_div(vec2 a, float m){ + return (vec2){a.x/m,a.y/m}; +} +vec2d vec2d_div(vec2d a, double m){ + return (vec2d){a.x/m,a.y/m}; } // multiply 2d vector by scalar vec2 vec2_mult(vec2 a, float m){ - return (vec2){a.x*m,a.y*m}; + return (vec2){a.x*m,a.y*m}; } // compute perpendicular vector vec2d vec2d_perp (vec2d a){ diff --git a/src/vectors.h b/src/vectors.h index 3cf2500..c639bd5 100644 --- a/src/vectors.h +++ b/src/vectors.h @@ -78,6 +78,7 @@ vec2 vec2_perp (vec2 a); vec2 vec2_add (vec2 a, vec2 b); vec2 vec2_sub (vec2 a, vec2 b); vec2 vec2_mult (vec2 a, float m); +vec2 vec2_div (vec2 a, float m); bool vec2_equ (vec2 a, vec2 b); vec2 vec2_line_norm (vec2 a, vec2 b); bool vec2_isnan (vec2 v); @@ -88,6 +89,7 @@ vec2d vec2d_perp (vec2d a); vec2d vec2d_add (vec2d a, vec2d b); vec2d vec2d_sub (vec2d a, vec2d b); vec2d vec2d_mult (vec2d a, double m); +vec2d vec2d_div (vec2d a, double m); vec2d vec2d_line_norm(vec2d a, vec2d b); bool vec2d_isnan (vec2d v); diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index e2c0171..4793d7a 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -732,13 +732,14 @@ void _init_descriptor_sets (VkvgContext ctx){ float _build_vb_step (vkvg_context* ctx, float hw, vec2 pL, vec2 p0, vec2 pR, bool isCurve){ Vertex v = {{0},ctx->curColor, {0,0,-1}}; - //if two of the three points are equal, normal is null - vec2 v0n = vec2_line_norm(pL, p0); - if (vec2_isnan(v0n)) - return 0; - vec2 v1n = vec2_line_norm(p0, pR); - if (vec2_isnan(v1n)) - return 0; + vec2 v0 = vec2_sub(p0, pL); + vec2 v1 = vec2_sub(pR, p0); + float length_v0 = vec2_length(v0); + float length_v1 = vec2_length(v1); + if (length_v0 < FLT_EPSILON || length_v1 < FLT_EPSILON) + return 0; + vec2 v0n = vec2_div (v0, length_v0); + vec2 v1n = vec2_div (v1, length_v1); vec2 bisec = vec2_norm(vec2_add(v0n,v1n)); @@ -751,6 +752,10 @@ float _build_vb_step (vkvg_context* ctx, float hw, vec2 pL, vec2 p0, vec2 pR, bo float lh = hw / cosf(alpha); bisec = vec2_perp(bisec); + + //limit bisectrice lenght, may be improved but ok for perf + lh=fminf (lh, fminf (sqrtf(length_v0*length_v0+hw*hw), sqrtf(length_v1*length_v1+hw*hw))); + bisec = vec2_mult(bisec,lh); VKVG_IBO_INDEX_TYPE idx = (VKVG_IBO_INDEX_TYPE)(ctx->vertCount - ctx->curVertOffset); @@ -758,9 +763,9 @@ float _build_vb_step (vkvg_context* ctx, float hw, vec2 pL, vec2 p0, vec2 pR, bo if (ctx->lineJoin == VKVG_LINE_JOIN_MITER || isCurve){ v.pos = vec2_add(p0, bisec); _add_vertex(ctx, v); - v.pos = vec2_sub(p0, bisec); - _add_vertex(ctx, v); - _add_tri_indices_for_rect(ctx, idx); + v.pos = vec2_sub(p0, bisec); + _add_vertex(ctx, v); + _add_tri_indices_for_rect(ctx, idx); }else{ vec2 vp = vec2_perp(v0n); if (cross<0){ @@ -771,7 +776,7 @@ float _build_vb_step (vkvg_context* ctx, float hw, vec2 pL, vec2 p0, vec2 pR, bo v.pos = vec2_add (p0, vec2_mult (vp, hw)); _add_vertex(ctx, v); v.pos = vec2_sub (p0, bisec); - } + } _add_vertex(ctx, v); if (ctx->lineJoin == VKVG_LINE_JOIN_BEVEL){ -- 2.47.3