]> O.S.I.I.S - jp/vkvg.git/commitdiff
vector code clean
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sun, 12 Dec 2021 04:37:03 +0000 (05:37 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sun, 12 Dec 2021 04:37:03 +0000 (05:37 +0100)
src/vectors.h
src/vkvg_context.c
src/vkvg_context_internal.c

index 872c2eee218c3cf8ef746d170437e3b34a74eafa..d8fd6a259e774222411c5c6290244b5bfcd77f8f 100644 (file)
@@ -95,68 +95,69 @@ vkvg_inline vec2 vec2_line_norm(vec2 a, vec2 b)
        d.y/=md;
        return d;
 }
+// compute sum of two double precision vectors
+vkvg_inline    vec2d vec2d_add (vec2d a, vec2d b){
+       return (vec2d){a.x + b.x, a.y + b.y};
+}
+// compute subbstraction of two double precision vectors
+vkvg_inline    vec2d vec2d_sub (vec2d a, vec2d b){
+       return (vec2d){a.x - b.x, a.y - b.y};
+}
+// multiply 2d vector by scalar
+vkvg_inline    vec2d vec2d_mult_s(vec2d a, double m){
+       return (vec2d){a.x*m,a.y*m};
+}
+vkvg_inline    vec2d vec2d_div_s(vec2d a, double m){
+       return (vec2d){a.x/m,a.y/m};
+}
 // compute length of double vector 2d
 vkvg_inline    double vec2d_length(vec2d v){
        return sqrt (v.x*v.x + v.y*v.y);
 }
-// normalize float vector
-vkvg_inline    vec2 vec2_norm(vec2 a)
-{
-       float m = sqrtf (a.x*a.x + a.y*a.y);
-       return (vec2){a.x/m, a.y/m};
-}
 // normalize double vector
 vkvg_inline    vec2d vec2d_norm(vec2d a)
 {
        double m = sqrt (a.x*a.x + a.y*a.y);
        return (vec2d){a.x/m, a.y/m};
 }
-// multiply 2d vector by scalar
-vkvg_inline    vec2d vec2d_mult(vec2d a, double m){
-       return (vec2d){a.x*m,a.y*m};
-}
-// devide 2d vector by scalar
-vkvg_inline    vec2 vec2_div(vec2 a, float m){
-       return (vec2){a.x/m,a.y/m};
-}
-vkvg_inline    vec2d vec2d_div(vec2d a, double m){
-       return (vec2d){a.x/m,a.y/m};
-}
-// multiply 2d vector by scalar
-vkvg_inline    vec2 vec2_mult(vec2 a, float m){
-       return (vec2){a.x*m,a.y*m};
-}
 // compute perpendicular vector
 vkvg_inline    vec2d vec2d_perp (vec2d a){
        return (vec2d){a.y, -a.x};
 }
-// compute perpendicular vector
-vkvg_inline    vec2 vec2_perp (vec2 a){
-       return (vec2){a.y, -a.x};
+vkvg_inline    bool vec2d_isnan (vec2d v){
+       return (bool)(isnan (v.x) || isnan (v.y));
 }
-// convert double precision vector to single precision
-vkvg_inline    vec2 vec2d_to_vec2(vec2d vd){
-       return (vec2){(float)vd.x,(float)vd.y};
+
+
+// test equality of two single precision vectors
+vkvg_inline    bool vec2_equ (vec2 a, vec2 b){
+       return (EQUF(a.x,b.x)&EQUF(a.y,b.y));
 }
 // compute sum of two single precision vectors
 vkvg_inline    vec2 vec2_add (vec2 a, vec2 b){
        return (vec2){a.x + b.x, a.y + b.y};
 }
-// compute sum of two double precision vectors
-vkvg_inline    vec2d vec2d_add (vec2d a, vec2d b){
-       return (vec2d){a.x + b.x, a.y + b.y};
-}
 // compute subbstraction of two single precision vectors
 vkvg_inline    vec2 vec2_sub (vec2 a, vec2 b){
        return (vec2){a.x - b.x, a.y - b.y};
 }
-// compute subbstraction of two double precision vectors
-vkvg_inline    vec2d vec2d_sub (vec2d a, vec2d b){
-       return (vec2d){a.x - b.x, a.y - b.y};
+// multiply 2d vector by scalar
+vkvg_inline    vec2 vec2_mult_s(vec2 a, float m){
+       return (vec2){a.x*m,a.y*m};
 }
-// test equality of two single precision vectors
-vkvg_inline    bool vec2_equ (vec2 a, vec2 b){
-       return (EQUF(a.x,b.x)&EQUF(a.y,b.y));
+// devide 2d vector by scalar
+vkvg_inline    vec2 vec2_div_s(vec2 a, float m){
+       return (vec2){a.x/m,a.y/m};
+}
+// normalize float vector
+vkvg_inline    vec2 vec2_norm(vec2 a)
+{
+       float m = sqrtf (a.x*a.x + a.y*a.y);
+       return (vec2){a.x/m, a.y/m};
+}
+// compute perpendicular vector
+vkvg_inline    vec2 vec2_perp (vec2 a){
+       return (vec2){a.y, -a.x};
 }
 // compute opposite of single precision vector
 vkvg_inline    void vec2_inv (vec2* v){
@@ -168,9 +169,6 @@ vkvg_inline bool vec2_isnan (vec2 v){
        return (bool)(isnanf (v.x) || isnanf (v.y));
 }
 // test if one component of double vector is nan
-vkvg_inline    bool vec2d_isnan (vec2d v){
-       return (bool)(isnan (v.x) || isnan (v.y));
-}
 vkvg_inline float vec2_dot (vec2 a, vec2 b) {
        return (a.x * b.x) + (a.y * b.y);
 }
@@ -182,6 +180,10 @@ vkvg_inline float vec2_slope (vec2 a, vec2 b) {
 }
 
 
+// convert double precision vector to single precision
+vkvg_inline    vec2 vec2d_to_vec2(vec2d vd){
+       return (vec2){(float)vd.x,(float)vd.y};
+}
 vkvg_inline    bool vec4_equ (vec4 a, vec4 b){
        return (EQUF(a.x,b.x)&EQUF(a.y,b.y)&EQUF(a.z,b.z)&EQUF(a.w,b.w));
 }
index b074472cb1e3e95079d55f17cb52d411e6ed0e08..8a21539734ad157d24fb9dcc829ba3a8396fc585 100644 (file)
@@ -765,7 +765,7 @@ void vkvg_stroke_preserve (VkvgContext ctx)
                                if (prevDash < 0)
                                        dc.curDash = ctx->dashCount-1;
                                float m = fminf (ctx->dashes[prevDash] - dc.curDashOffset, ctx->dashes[dc.curDash]);
-                               vec2 p = vec2_sub(ctx->points[str.iR], vec2_mult(dc.normal, m));
+                               vec2 p = vec2_sub(ctx->points[str.iR], vec2_mult_s(dc.normal, m));
                                _draw_stoke_cap (ctx, hw, p, dc.normal, false);
                        }
                } else if (_path_is_closed(ctx,ptrPath)){
index 7d4485b3f047161bc9dc2f3e967a41fed4b01592..7a2ef352187a04f458ea12201a349e4f30490f96 100644 (file)
@@ -748,8 +748,8 @@ float _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool i
        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 v0n = vec2_div_s (v0, length_v0);
+       vec2 v1n = vec2_div_s (v1, length_v1);
 
        vec2 bisec_n = vec2_norm(vec2_add(v0n,v1n));
 
@@ -774,16 +774,16 @@ float _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool i
        bool reducedLH = EQUF(dot,-1) || (lh > fminf (lh, fminf (length_v0, length_v1)));
        //---
 
-       vec2 bisec = vec2_mult(bisec_n,lh);
+       vec2 bisec = vec2_mult_s(bisec_n,lh);
 
        VKVG_IBO_INDEX_TYPE idx = (VKVG_IBO_INDEX_TYPE)(ctx->vertCount - ctx->curVertOffset);
 
        if (join == VKVG_LINE_JOIN_MITER || isCurve){
                if (dot < 0 && reducedLH && det < 0) {
                        if (length_v0 < length_v1)
-                               v.pos = vec2_add (p0, vec2_mult (vec2_perp(v1n), hw));
+                               v.pos = vec2_add (p0, vec2_mult_s (vec2_perp(v1n), hw));
                        else
-                               v.pos = vec2_sub (p0, vec2_mult (vec2_perp(v1n), hw));
+                               v.pos = vec2_sub (p0, vec2_mult_s (vec2_perp(v1n), hw));
                } else
                        v.pos = vec2_add(p0, bisec);
 
@@ -791,9 +791,9 @@ float _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool i
 
                if (dot < 0 && reducedLH && det > 0) {
                        if (length_v0 < length_v1)
-                               v.pos = vec2_sub (p0, vec2_mult (vec2_perp(v1n), hw));
+                               v.pos = vec2_sub (p0, vec2_mult_s (vec2_perp(v1n), hw));
                        else
-                               v.pos = vec2_add (p0, vec2_mult (vec2_perp(v1n), hw));
+                               v.pos = vec2_add (p0, vec2_mult_s (vec2_perp(v1n), hw));
                } else
                        v.pos = vec2_sub(p0, bisec);
 
@@ -804,16 +804,16 @@ float _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool i
                vec2 vp = vec2_perp(v0n);
                if (det<0){
                        if (dot < 0 && reducedLH)
-                               v.pos = vec2_sub (p0, vec2_mult (vec2_perp(v1n), hw));
+                               v.pos = vec2_sub (p0, vec2_mult_s (vec2_perp(v1n), hw));
                        else
                                v.pos = vec2_add (p0, bisec);
                        _add_vertex(ctx, v);
-                       v.pos = vec2_sub (p0, vec2_mult (vp, hw));
+                       v.pos = vec2_sub (p0, vec2_mult_s (vp, hw));
                }else{
-                       v.pos = vec2_add (p0, vec2_mult (vp, hw));
+                       v.pos = vec2_add (p0, vec2_mult_s (vp, hw));
                        _add_vertex(ctx, v);
                        if (dot < 0 && reducedLH)
-                               v.pos = vec2_add (p0, vec2_mult (vec2_perp(v1n), hw));
+                               v.pos = vec2_add (p0, vec2_mult_s (vec2_perp(v1n), hw));
                        else
                                v.pos = vec2_sub (p0, bisec);
                }
@@ -891,7 +891,7 @@ float _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool i
 
                }
 
-               vp = vec2_mult (vec2_perp(v1n), hw);
+               vp = vec2_mult_s (vec2_perp(v1n), hw);
                if (det < 0)
                        v.pos = vec2_sub (p0, vp);
                else
@@ -924,7 +924,7 @@ void _draw_stoke_cap (VkvgContext ctx, float hw, vec2 p0, vec2 n, bool isStart)
        VKVG_IBO_INDEX_TYPE firstIdx = (VKVG_IBO_INDEX_TYPE)(ctx->vertCount - ctx->curVertOffset);
 
        if (isStart){
-               vec2 vhw = vec2_mult(n,hw);
+               vec2 vhw = vec2_mult_s(n,hw);
 
                if (ctx->lineCap == VKVG_LINE_CAP_SQUARE)
                        p0 = vec2_sub(p0, vhw);
@@ -956,7 +956,7 @@ void _draw_stoke_cap (VkvgContext ctx, float hw, vec2 p0, vec2 n, bool isStart)
 
                _add_tri_indices_for_rect(ctx, firstIdx);
        }else{
-               vec2 vhw = vec2_mult(n, hw);
+               vec2 vhw = vec2_mult_s(n, hw);
 
                if (ctx->lineCap == VKVG_LINE_CAP_SQUARE)
                        p0 = vec2_add(p0, vhw);
@@ -1002,7 +1002,7 @@ float _draw_dashed_segment (VkvgContext ctx, float hw, stroke_context_t* str, da
        float segmentLength = vec2_length(d);
 
        while (dc->curDashOffset < segmentLength){
-               vec2 p0 = vec2_add (p, vec2_mult(dc->normal, dc->curDashOffset));
+               vec2 p0 = vec2_add (p, vec2_mult_s(dc->normal, dc->curDashOffset));
 
                _draw_stoke_cap (ctx, hw, p0, dc->normal, dc->dashOn);
                dc->dashOn ^= true;