customCompoundBond.cl 1.31 KB
Newer Older
1
2
3
/**
 * Compute the difference between two vectors, setting the fourth component to the squared magnitude.
 */
4
5
real4 ccb_delta(real4 vec1, real4 vec2) {
    real4 result = (real4) (vec1.x-vec2.x, vec1.y-vec2.y, vec1.z-vec2.z, 0);
6
7
8
9
10
11
12
    result.w = result.x*result.x + result.y*result.y + result.z*result.z;
    return result;
}

/**
 * Compute the angle between two vectors.  The w component of each vector should contain the squared magnitude.
 */
13
14
15
16
real ccb_computeAngle(real4 vec1, real4 vec2) {
    real dotProduct = vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z;
    real cosine = dotProduct*RSQRT(vec1.w*vec2.w);
    real angle;
17
18
19
    if (cosine > 0.99f || cosine < -0.99f) {
        // We're close to the singularity in acos(), so take the cross product and use asin() instead.

20
21
        real4 crossProduct = cross(vec1, vec2);
        real scale = vec1.w*vec2.w;
22
        angle = asin(SQRT(dot(crossProduct, crossProduct)/scale));
23
        if (cosine < 0)
24
25
26
27
28
29
30
31
32
33
            angle = M_PI-angle;
    }
    else
       angle = acos(cosine);
    return angle;
}

/**
 * Compute the cross product of two vectors, setting the fourth component to the squared magnitude.
 */
34
35
real4 ccb_computeCross(real4 vec1, real4 vec2) {
    real4 result = cross(vec1, vec2);
36
37
38
    result.w = result.x*result.x + result.y*result.y + result.z*result.z;
    return result;
}