From 52d73e8342150507bc09565e5e495246b9bff277 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Sun, 1 Sep 2019 14:52:31 +0200 Subject: [PATCH] floating operations --- src/vkvg_context.c | 35 +++++++++++----------- src/vkvg_context_internal.c | 16 +++++----- src/vkvg_context_internal.h | 2 +- src/vkvg_internal.h | 1 + tests/line_join_3.c | 58 +++++++++++++++++++++++++++++++++++++ tests/random_rects.c | 8 ++--- 6 files changed, 90 insertions(+), 30 deletions(-) create mode 100644 tests/line_join_3.c diff --git a/src/vkvg_context.c b/src/vkvg_context.c index d4ecc15..a0a12ee 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -553,13 +553,13 @@ void vkvg_stroke_preserve (VkvgContext ctx) Vertex v = {}; v.uv.z = -1; - float hw = ctx->lineWidth / 2.0; - int i = 0, ptrPath = 0; + float hw = ctx->lineWidth / 2.0f; + uint i = 0, ptrPath = 0; uint32_t lastPathPointIdx, iL, iR; while (ptrPath < ctx->pathPtr){ - int ptrCurve = 0; + uint ptrCurve = 0; uint32_t firstIdx = ctx->vertCount; i = ctx->pathes[ptrPath]&PATH_ELT_MASK; @@ -588,19 +588,19 @@ void vkvg_stroke_preserve (VkvgContext ctx) vhw = vec2_perp(vhw); if (ctx->lineCap == VKVG_LINE_CAP_ROUND){ - float step = M_PI / hw; - float a = acos(n.x) + M_PI_2; + float step = M_PIF / hw; + float a = acosf(n.x) + M_PIF_2; if (n.y < 0) - a = M_PI-a; - float a1 = a + M_PI; + a = M_PIF-a; + float a1 = a + M_PIF; a+=step; while (a < a1){ - _add_vertexf(ctx, cos(a) * hw + p0.x, sin(a) * hw + p0.y); + _add_vertexf(ctx, cosf(a) * hw + p0.x, sinf(a) * hw + p0.y); a+=step; } uint32_t p0Idx = ctx->vertCount; - for (int p = firstIdx; p < p0Idx; p++) + for (uint p = firstIdx; p < p0Idx; p++) _add_triangle_indices(ctx, p0Idx+1, p, p+1); firstIdx = p0Idx; } @@ -616,18 +616,19 @@ void vkvg_stroke_preserve (VkvgContext ctx) } if (_path_has_curves (ctx,ptrPath)) { + while (i < lastPathPointIdx){ if (ptrPath + ptrCurve + 2 < ctx->pathPtr && (ctx->pathes [ptrPath + 2 + ptrCurve]&PATH_ELT_MASK) == i){ uint32_t lastCurvePoint = ctx->pathes[ptrPath + 3 + ptrCurve]&PATH_ELT_MASK; while (ilineCap == VKVG_LINE_CAP_ROUND){ firstIdx = ctx->vertCount; - float step = M_PI / hw; - float a = acos(n.x)+ M_PI_2; + float step = M_PIF / hw; + float a = acosf(n.x)+ M_PIF_2; if (n.y < 0) - a = M_PI-a; - float a1 = a - M_PI; + a = M_PIF-a; + float a1 = a - M_PIF; a-=step; while ( a > a1){ - _add_vertexf(ctx, cos(a) * hw + p0.x, sin(a) * hw + p0.y); + _add_vertexf(ctx, cosf(a) * hw + p0.x, sinf(a) * hw + p0.y); a-=step; } uint32_t p0Idx = ctx->vertCount-1; - for (int p = firstIdx-1 ; p < p0Idx; p++) + for (uint p = firstIdx-1 ; p < p0Idx; p++) _add_triangle_indices(ctx, p+1, p, firstIdx-2); } diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index 82a2721..c28a20f 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -68,7 +68,7 @@ inline void _set_curve_end (VkvgContext ctx) { ctx->curvePtr+=2; } //path start pointed at ptrPath has curve bit -inline bool _path_has_curves (VkvgContext ctx, int ptrPath) { +inline bool _path_has_curves (VkvgContext ctx, uint ptrPath) { return ctx->pathes[ptrPath] & PATH_HAS_CURVES_BIT; } //this function expect that current path is empty @@ -527,13 +527,13 @@ void _build_vb_step (vkvg_context* ctx, Vertex v, float hw, uint32_t iL, uint32_ vec2 bisec = vec2_norm(vec2_add(v0n,v1n)); - float alpha = acos(v0n.x * v1n.x + v0n.y * v1n.y)/2; + float alpha = acosf(v0n.x * v1n.x + v0n.y * v1n.y)/2; float cross = v0n.x * v1n.y - v0n.y * v1n.x; if (cross<0) alpha = -alpha; - float lh = hw / cos(alpha); + float lh = hw / cosf(alpha); bisec = vec2_perp(bisec); bisec = vec2_mult(bisec,lh); @@ -570,7 +570,7 @@ void _build_vb_step (vkvg_context* ctx, Vertex v, float hw, uint32_t iL, uint32_ } }else if (ctx->lineJoin == VKVG_LINE_JOIN_ROUND){ float step = M_PIF / hw; - float a = acos(vp.x); + float a = acosf(vp.x); if (vp.y < 0) a = -a; @@ -579,26 +579,26 @@ void _build_vb_step (vkvg_context* ctx, Vertex v, float hw, uint32_t iL, uint32_ float a1 = a + alpha*2; a-=step; while (a > a1){ - _add_vertexf(ctx, cos(a) * hw + ctx->points[i].x, sin(a) * hw + ctx->points[i].y); + _add_vertexf(ctx, cosf(a) * hw + ctx->points[i].x, sinf(a) * hw + ctx->points[i].y); a-=step; } }else{ float a1 = a + alpha*2; a+=step; while (a < a1){ - _add_vertexf(ctx, cos(a) * hw + ctx->points[i].x, sin(a) * hw + ctx->points[i].y); + _add_vertexf(ctx, cosf(a) * hw + ctx->points[i].x, sinf(a) * hw + ctx->points[i].y); a+=step; } } uint32_t p0Idx = ctx->vertCount; _add_triangle_indices(ctx, idx, idx+2, idx+1); if (cross<0){ - for (int p = idx+2; p < p0Idx; p++) + for (uint p = idx+2; p < p0Idx; p++) _add_triangle_indices(ctx, p, p+1, idx); _add_triangle_indices(ctx, p0Idx, p0Idx+2, idx); _add_triangle_indices(ctx, idx, p0Idx+1, p0Idx+2); }else{ - for (int p = idx+2; p < p0Idx; p++) + for (uint p = idx+2; p < p0Idx; p++) _add_triangle_indices(ctx, p, p+1, idx+1); _add_triangle_indices(ctx, p0Idx, p0Idx+1, idx+1); _add_triangle_indices(ctx, idx+1, p0Idx+1, p0Idx+2); diff --git a/src/vkvg_context_internal.h b/src/vkvg_context_internal.h index d48bdbb..54cd41e 100644 --- a/src/vkvg_context_internal.h +++ b/src/vkvg_context_internal.h @@ -153,7 +153,7 @@ void _clear_path (VkvgContext ctx); bool _path_is_closed (VkvgContext ctx, uint32_t ptrPath); void _set_curve_start (VkvgContext ctx); void _set_curve_end (VkvgContext ctx); -bool _path_has_curves (VkvgContext ctx, int ptrPath); +bool _path_has_curves (VkvgContext ctx, uint ptrPath); float _normalizeAngle (float a); diff --git a/src/vkvg_internal.h b/src/vkvg_internal.h index e36fbb7..459e9ae 100644 --- a/src/vkvg_internal.h +++ b/src/vkvg_internal.h @@ -31,6 +31,7 @@ #include #define M_PIF 3.14159265358979323846f /* float pi */ +#define M_PIF_2 1.57079632679489661923f #define PATH_CLOSED_BIT 0x80000000 /* most significant bit of path elmts is closed/open path state */ #define PATH_HAS_CURVES_BIT 0x40000000 /* most significant bit of path elmts end is continue path bit Used to handle curved/line transition */ diff --git a/tests/line_join_3.c b/tests/line_join_3.c new file mode 100644 index 0000000..5e0e48b --- /dev/null +++ b/tests/line_join_3.c @@ -0,0 +1,58 @@ +#include "test.h" + +void test(){ + VkvgContext ctx = vkvg_create(surf); + + vkvg_set_fill_rule(ctx, VKVG_FILL_RULE_EVEN_ODD); + vkvg_set_line_width(ctx, 30); + + vkvg_set_line_join(ctx, VKVG_LINE_JOIN_ROUND); + + //vkvg_arc (ctx, 200, 500, 100, 0, M_PI); + + vkvg_translate(ctx,-50,-50); + + vkvg_set_source_rgb (ctx, 0.5,0,0); + + for (int j=0;j<2;j++) { + int i=0; + vkvg_move_to(ctx,100,100); + for (i=0;i<4;i++) { + vkvg_rel_line_to(ctx,50,70); + vkvg_rel_line_to(ctx,50,-70); + } + vkvg_stroke(ctx); + + vkvg_move_to(ctx,500,200); + for (i=0;i<4;i++) { + vkvg_rel_line_to(ctx,-50,70); + vkvg_rel_line_to(ctx,-50,-70); + } + vkvg_stroke(ctx); + + vkvg_move_to(ctx,100,400); + for (i=0;i<4;i++) { + vkvg_rel_line_to(ctx,50,-70); + vkvg_rel_line_to(ctx,50,70); + } + vkvg_stroke(ctx); + + vkvg_move_to(ctx,500,500); + for (i=0;i<4;i++) { + vkvg_rel_line_to(ctx,-50,-70); + vkvg_rel_line_to(ctx,-50,70); + } + vkvg_stroke(ctx); + vkvg_set_line_join(ctx, VKVG_LINE_JOIN_BEVEL); + vkvg_translate(ctx,450,0); + } + + vkvg_destroy(ctx); +} + +int main(int argc, char *argv[]) { + + perform_test (test, 1024, 768); + + return 0; +} diff --git a/tests/random_rects.c b/tests/random_rects.c index 3ad539b..fc69207 100644 --- a/tests/random_rects.c +++ b/tests/random_rects.c @@ -1,7 +1,7 @@ #include "test.h" void test(){ - vkvg_surface_clear(surf); + //vkvg_surface_clear(surf); struct timeval currentTime; gettimeofday(¤tTime, NULL); @@ -12,8 +12,8 @@ void test(){ vkvg_set_fill_rule(ctx, VKVG_FILL_RULE_NON_ZERO); vkvg_set_line_width(ctx,2); vkvg_set_source_rgba(ctx,1.0,0.0,0.0,0.1); - for (int i=0; i<1500; i++) { - //randomize_color(ctx); + for (int i=0; i<500; i++) { + randomize_color(ctx); float x = trunc( (0.5*(float)w*rand())/RAND_MAX ); float y = trunc( (0.5*(float)w*rand())/RAND_MAX ); float z = trunc( (0.5*(float)w*rand())/RAND_MAX ) + 1; @@ -21,7 +21,7 @@ void test(){ vkvg_rectangle(ctx, x+1, y+1, z, v); vkvg_fill_preserve(ctx); - //randomize_color(ctx); + randomize_color(ctx); vkvg_stroke(ctx); } vkvg_destroy(ctx); -- 2.47.3