# all files / src/math/ Ray.js

10.56% Statements 15/142
5.19% Branches 4/77
30.77% Functions 4/13
10.56% Lines 15/142

    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
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321

    1×

    1×

    1×

    3×

    3×

    1×

    1×
    1×

    1×
    1×

    1×
    1×
    1×
    1×

    1×

     import Vector3 from './Vector3';
    import glMatrix from '../dep/glmatrix';
    var vec3 = glMatrix.vec3;

    var EPSILON = 1e-5;

    /**
     * @constructor
     * @alias clay.Ray
     * @param {clay.Vector3} [origin]
     * @param {clay.Vector3} [direction]
     */
    var Ray = function (origin, direction) {
        /**
         * @type {clay.Vector3}
         */
        this.origin = origin || new Vector3();
        /**
         * @type {clay.Vector3}
         */
        this.direction = direction || new Vector3();
    };

    Ray.prototype = {

        constructor: Ray,

        // http://www.siggraph.org/education/materials/HyperGraph/raytrace/rayplane_intersection.htm
        /**
         * Calculate intersection point between ray and a give plane
         * @param  {clay.Plane} plane
         * @param  {clay.Vector3} [out]
         * @return {clay.Vector3}
         */
        intersectPlane: function (plane, out) {
            var pn = plane.normal.array;
            var d = plane.distance;
            var ro = this.origin.array;
            var rd = this.direction.array;

            var divider = vec3.dot(pn, rd);
            // ray is parallel to the plane
            if (divider === 0) {
                return null;
            }
            if (!out) {
                out = new Vector3();
            }
            var t = (vec3.dot(pn, ro) - d) / divider;
            vec3.scaleAndAdd(out.array, ro, rd, -t);
            out._dirty = true;
            return out;
        },

        /**
         * Mirror the ray against plane
         * @param  {clay.Plane} plane
         */
        mirrorAgainstPlane: function (plane) {
            // Distance to plane
            var d = vec3.dot(plane.normal.array, this.direction.array);
            vec3.scaleAndAdd(this.direction.array, this.direction.array, plane.normal.array, -d * 2);
            this.direction._dirty = true;
        },

        distanceToPoint: (function () {
            var v = vec3.create();
            return function (point) {
                vec3.sub(v, point, this.origin.array);
                // Distance from projection point to origin
                var b = vec3.dot(v, this.direction.array);
                if (b < 0) {
                    return vec3.distance(this.origin.array, point);
                }
                // Squared distance from center to origin
                var c2 = vec3.lenSquared(v);
                // Squared distance from center to projection point
                return Math.sqrt(c2 - b * b);
            };
        })(),

        /**
         * Calculate intersection point between ray and sphere
         * @param  {clay.Vector3} center
         * @param  {number} radius
         * @param  {clay.Vector3} out
         * @return {clay.Vector3}
         */
        intersectSphere: (function () {
            var v = vec3.create();
            return function (center, radius, out) {
                var origin = this.origin.array;
                var direction = this.direction.array;
                center = center.array;
                vec3.sub(v, center, origin);
                // Distance from projection point to origin
                var b = vec3.dot(v, direction);
                // Squared distance from center to origin
                var c2 = vec3.squaredLength(v);
                // Squared distance from center to projection point
                var d2 = c2 - b * b;

                var r2 = radius * radius;
                // No intersection
                if (d2 > r2) {
                    return;
                }

                var a = Math.sqrt(r2 - d2);
                // First intersect point
                var t0 = b - a;
                // Second intersect point
                var t1 = b + a;

                if (!out) {
                    out = new Vector3();
                }
                if (t0 < 0) {
                    if (t1 < 0) {
                        return null;
                    }
                    else {
                        vec3.scaleAndAdd(out.array, origin, direction, t1);
                        return out;
                    }
                }
                else {
                    vec3.scaleAndAdd(out.array, origin, direction, t0);
                    return out;
                }
            };
        })(),

        // http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-box-intersection/
        /**
         * Calculate intersection point between ray and bounding box
         * @param {clay.BoundingBox} bbox
         * @param {clay.Vector3}
         * @return {clay.Vector3}
         */
        intersectBoundingBox: function (bbox, out) {
            var dir = this.direction.array;
            var origin = this.origin.array;
            var min = bbox.min.array;
            var max = bbox.max.array;

            var invdirx = 1 / dir[0];
            var invdiry = 1 / dir[1];
            var invdirz = 1 / dir[2];

            var tmin, tmax, tymin, tymax, tzmin, tzmax;
            if (invdirx >= 0) {
                tmin = (min[0] - origin[0]) * invdirx;
                tmax = (max[0] - origin[0]) * invdirx;
            }
            else {
                tmax = (min[0] - origin[0]) * invdirx;
                tmin = (max[0] - origin[0]) * invdirx;
            }
            if (invdiry >= 0) {
                tymin = (min[1] - origin[1]) * invdiry;
                tymax = (max[1] - origin[1]) * invdiry;
            }
            else {
                tymax = (min[1] - origin[1]) * invdiry;
                tymin = (max[1] - origin[1]) * invdiry;
            }

            if ((tmin > tymax) || (tymin > tmax)) {
                return null;
            }

            if (tymin > tmin || tmin !== tmin) {
                tmin = tymin;
            }
            if (tymax < tmax || tmax !== tmax) {
                tmax = tymax;
            }

            if (invdirz >= 0) {
                tzmin = (min[2] - origin[2]) * invdirz;
                tzmax = (max[2] - origin[2]) * invdirz;
            }
            else {
                tzmax = (min[2] - origin[2]) * invdirz;
                tzmin = (max[2] - origin[2]) * invdirz;
            }

            if ((tmin > tzmax) || (tzmin > tmax)) {
                return null;
            }

            if (tzmin > tmin || tmin !== tmin) {
                tmin = tzmin;
            }
            if (tzmax < tmax || tmax !== tmax) {
                tmax = tzmax;
            }
            if (tmax < 0) {
                return null;
            }

            var t = tmin >= 0 ? tmin : tmax;

            if (!out) {
                out = new Vector3();
            }
            vec3.scaleAndAdd(out.array, origin, dir, t);
            return out;
        },

        // http://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
        /**
         * Calculate intersection point between ray and three triangle vertices
         * @param {clay.Vector3} a
         * @param {clay.Vector3} b
         * @param {clay.Vector3} c
         * @param {boolean}           singleSided, CW triangle will be ignored
         * @param {clay.Vector3} [out]
         * @param {clay.Vector3} [barycenteric] barycentric coords
         * @return {clay.Vector3}
         */
        intersectTriangle: (function () {

            var eBA = vec3.create();
            var eCA = vec3.create();
            var AO = vec3.create();
            var vCross = vec3.create();

            return function (a, b, c, singleSided, out, barycenteric) {
                var dir = this.direction.array;
                var origin = this.origin.array;
                a = a.array;
                b = b.array;
                c = c.array;

                vec3.sub(eBA, b, a);
                vec3.sub(eCA, c, a);

                vec3.cross(vCross, eCA, dir);

                var det = vec3.dot(eBA, vCross);

                if (singleSided) {
                    if (det > -EPSILON) {
                        return null;
                    }
                }
                else {
                    if (det > -EPSILON && det < EPSILON) {
                        return null;
                    }
                }

                vec3.sub(AO, origin, a);
                var u = vec3.dot(vCross, AO) / det;
                if (u < 0 || u > 1) {
                    return null;
                }

                vec3.cross(vCross, eBA, AO);
                var v = vec3.dot(dir, vCross) / det;

                if (v < 0 || v > 1 || (u + v > 1)) {
                    return null;
                }

                vec3.cross(vCross, eBA, eCA);
                var t = -vec3.dot(AO, vCross) / det;

                if (t < 0) {
                    return null;
                }

                if (!out) {
                    out = new Vector3();
                }
                if (barycenteric) {
                    Vector3.set(barycenteric, (1 - u - v), u, v);
                }
                vec3.scaleAndAdd(out.array, origin, dir, t);

                return out;
            };
        })(),

        /**
         * Apply an affine transform matrix to the ray
         * @return {clay.Matrix4} matrix
         */
        applyTransform: function (matrix) {
            Vector3.add(this.direction, this.direction, this.origin);
            Vector3.transformMat4(this.origin, this.origin, matrix);
            Vector3.transformMat4(this.direction, this.direction, matrix);

            Vector3.sub(this.direction, this.direction, this.origin);
            Vector3.normalize(this.direction, this.direction);
        },

        /**
         * Copy values from another ray
         * @param {clay.Ray} ray
         */
        copy: function (ray) {
            Vector3.copy(this.origin, ray.origin);
            Vector3.copy(this.direction, ray.direction);
        },

        /**
         * Clone a new ray
         * @return {clay.Ray}
         */
        clone: function () {
            var ray = new Ray();
            ray.copy(this);
            return ray;
        }
    };

    export default Ray;