Skip to content

Commit

Permalink
limit bisectrice length in strokes, prevent oversized line joins, may…
Browse files Browse the repository at this point in the history
… be more accurate
  • Loading branch information
jpbruyere committed Sep 3, 2020
1 parent ba1aef7 commit 88ebe9f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
11 changes: 9 additions & 2 deletions src/vectors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
2 changes: 2 additions & 0 deletions src/vectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
27 changes: 16 additions & 11 deletions src/vkvg_context_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -751,16 +752,20 @@ 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);

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){
Expand All @@ -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){
Expand Down

0 comments on commit 88ebe9f

Please sign in to comment.