Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 00c20c91c5
Fetching contributors…

Cannot retrieve contributors at this time

file 127 lines (96 sloc) 3.398 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/**
Authors: David Herberth
*/


module gl3n.interpolate;

private {
    import gl3n.linalg : Vector;
    import gl3n.util : is_vector;
    
    version(unittest) {
        import gl3n.linalg : vec2, vec3, vec4;
    }
}



T interp(T)(T x, T y, float a) if(!is_vector!T) {
    return x + a*(y - x);
}

T interp(T)(T x, T y, float a) if(is_vector!T) {
    return T.mix(x, y, a);
}

alias interp interp_linear;
alias interp mix;

unittest {
    vec2 v2_1 = vec2(1.0f);
    vec2 v2_2 = vec2(0.0f);
    vec3 v3_1 = vec3(1.0f);
    vec3 v3_2 = vec3(0.0f);
    vec4 v4_1 = vec4(1.0f);
    vec4 v4_2 = vec4(0.0f);
    
    assert(interp(v2_1, v2_2, 0.5f).vector == [0.5f, 0.5f]);
    assert(interp(v2_1, v2_2, 0.0f) == v2_1);
    assert(interp(v2_1, v2_2, 1.0f) == v2_2);
    assert(interp(v3_1, v3_2, 0.5f).vector == [0.5f, 0.5f, 0.5f]);
    assert(interp(v3_1, v3_2, 0.0f) == v3_1);
    assert(interp(v3_1, v3_2, 1.0f) == v3_2);
    assert(interp(v4_1, v4_2, 0.5f).vector == [0.5f, 0.5f, 0.5f, 0.5f]);
    assert(interp(v4_1, v4_2, 0.0f) == v4_1);
    assert(interp(v4_1, v4_2, 1.0f) == v4_2);
    

    real r1 = 0.0;
    real r2 = 1.0;
    assert(interp(r1, r2, 0.5f) == 0.5);
    assert(interp(r1, r2, 0.0f) == r1);
    assert(interp(r1, r2, 1.0f) == r2);
    
    assert(interp(0.0, 1.0, 0.5f) == 0.5);
    assert(interp(0.0, 1.0, 0.0f) == 0.0);
    assert(interp(0.0, 1.0, 1.0f) == 1.0);
    
    assert(interp(0.0f, 1.0f, 0.5f) == 0.5f);
    assert(interp(0.0f, 1.0f, 0.0f) == 0.0f);
    assert(interp(0.0f, 1.0f, 1.0f) == 1.0f);
}

T interp_nearest(T)(T x, T y, float a) {
    if(a < 0.5f) { return x; }
    else { return y; }
}

unittest {
    assert(interp_nearest(0.0, 1.0, 0.5f) == 1.0);
    assert(interp_nearest(0.0, 1.0, 0.4f) == 0.0);
    assert(interp_nearest(0.0, 1.0, 0.6f) == 1.0);
}

T interp_catmullrom(T)(T p0, T p1, T p2, T p3, float a) if(!is_vector!T){
    return 0.5f * ((2 * p1) +
                   (-p0 + p2) * a +
                   (2 * p0 - 5 * p1 + 4 * p2 - p3) * a^^2 +
                   (-p0 + 3 * p1 - 3 * p2 + p3) * a^^3);
}

T interp_catmullrom(T)(T p0, T p1, T p2, T p3, float a) if(is_vector!T){
    T ret;
    
    for(int i = 0; i < ret.vector.length; i++) {
        ret.vector[i] = interp_catmullrom(p0.vector[i], p1.vector[i], p2.vector[i], p3.vector[i], a);
    }
    
    return ret;
}


T catmullrom_derivative(T)(T p0, T p1, T p2, T p3, float a) if(!is_vector!T) {
    return 0.5f * ((2 * p1) +
                   (-p0 + p2) +
                   2 * (2 * p0 - 5 * p1 + 4 * p2 - p3) * a +
                   3 * (-p0 + 3 * p1 - 3 * p2 + p3) * a^^2);
}

T catmullrom_derivative(T)(T p0, T p1, T p2, T p3, float a) if(is_vector!T) {
    T ret;
    
    for(int i = 0; i < ret.vector.length; i++) {
        ret.vector[i] = catmullrom_derivative(p0.vector[i], p1.vector[i], p2.vector[i], p3.vector[i], a);
    }
    
    return ret;
}


T interp_hermite(T)(T x, T tx, T y, T ty, float a) if(!is_vector!T) {
    float h1 = 2 * a^^3 - 3 * a^^2 + 1;
    float h2 = -2* a^^3 + 3 * a^^2;
    float h3 = a^^3 - 2 * a^^2 + a;
    float h4 = a^^3 - a^^2;
    return h1 * x + h3 * tx + h2 * y + h4 * ty;
}

T interp_hermite(T)(T x, T tx, T y, T ty, float a) if(is_vector!T) {
    T ret;
    
    for(int i = 0; i < ret.vector.length; i++) {
        ret.vector[i] = interp_hermite(x.vector[i], tx.vector[i], y.vector[i], ty.vector[i], a);
    }
    
    return ret;
}
Something went wrong with that request. Please try again.