]> O.S.I.I.S - jp/vkvg.git/commitdiff
floating operations
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sun, 1 Sep 2019 12:52:31 +0000 (14:52 +0200)
committerj-p <jp_bruyere@hotmail.com>
Tue, 3 Sep 2019 18:36:09 +0000 (20:36 +0200)
src/vkvg_context.c
src/vkvg_context_internal.c
src/vkvg_context_internal.h
src/vkvg_internal.h
tests/line_join_3.c [new file with mode: 0644]
tests/random_rects.c

index d4ecc15aae1124a4aa089f94d7e227ff8105f052..a0a12ee72d17f0228327ae3144f1bd828c3b256c 100644 (file)
@@ -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 (i<lastCurvePoint){
                         iR = i+1;
-                        _build_vb_step(ctx,v,hw,iL,i,iR,true);
+                        _build_vb_step (ctx, v, hw, iL, i, iR, true);
                         iL = i++;
                     }
                     ptrCurve += 2;
                 }else{
                     iR = i+1;
-                    _build_vb_step(ctx,v,hw,iL,i,iR, false);
+                    _build_vb_step (ctx, v, hw, iL, i, iR, false);
                     iL = i++;
                 }
             }
@@ -656,19 +657,19 @@ void vkvg_stroke_preserve (VkvgContext ctx)
 
             if (ctx->lineCap == 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);
             }
 
index 82a2721d8d7f4f31a5fa701191fc2940a37f43e3..c28a20f8ca40d6a23d5b5d7dcd6690961f78a20d 100644 (file)
@@ -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);
index d48bdbbacff8055e0b6bf6968cb8f3bf7920bc95..54cd41e098e4ee28663fcab40b0090e23be9ec3c 100644 (file)
@@ -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);
 
index e36fbb7bad739891e289170e93baba4cc5186702..459e9aebfea2126ed32dfe504c58d0ef3f3a8cc8 100644 (file)
@@ -31,6 +31,7 @@
 #include <math.h>
 
 #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 (file)
index 0000000..5e0e48b
--- /dev/null
@@ -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;
+}
index 3ad539bca8004326c396940b4f843b801943668b..fc69207181b171bc6fa49dbf3547ccb06beec003 100644 (file)
@@ -1,7 +1,7 @@
 #include "test.h"
 
 void test(){
-    vkvg_surface_clear(surf);
+    //vkvg_surface_clear(surf);
     struct timeval currentTime;
     gettimeofday(&currentTime, 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);