pointFunctions.cc 1.01 KB
Newer Older
1
2
3
/**
 * Compute the angle between two vectors.  The w component of each vector should contain the squared magnitude.
 */
4
DEVICE real computeAngle(real4 vec1, real4 vec2) {
5
6
7
8
9
10
    real dotProduct = vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z;
    real cosine = dotProduct*RSQRT(vec1.w*vec2.w);
    real angle;
    if (cosine > 0.99f || cosine < -0.99f) {
        // We're close to the singularity in acos(), so take the cross product and use asin() instead.

11
        real3 crossProduct = cross(trimTo3(vec1), trimTo3(vec2));
12
13
        real scale = vec1.w*vec2.w;
        angle = ASIN(SQRT(dot(crossProduct, crossProduct)/scale));
14
        if (cosine < 0)
15
16
17
18
19
20
21
22
23
24
            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.
 */
25
DEVICE real4 computeCross(real4 vec1, real4 vec2) {
26
    real3 cp = cross(trimTo3(vec1), trimTo3(vec2));
27
28
    return make_real4(cp.x, cp.y, cp.z, cp.x*cp.x+cp.y*cp.y+cp.z*cp.z);
}
29