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I,OADiB,IAAjB,EAAQ,GAAG,EACA,KAAK,qBAAqB,EAAgB,EAAgB,EAAS,GAEnE,KAAK,yBAAyB,EAAgB,EAAgB,EAAU,GAEzE,CACV,IAAI,EAAO,EAAS,QAAQ,YAAY,aAAa,KAAK,SACtD,EAAoB,EAAS,SAAS,EAAM,GAChD,KAAK,aAAa,EAAW,EACjC,CACA,GACJ,CACA,OAAO,CACX,EAOA,aAAc,SAAS,EAAW,GAC9B,IAAK,IAAI,EAAI,EAAG,EAAI,EAAU,OAAQ,IAClC,GAAI,EAAU,GAAG,SAAS,GAAU,gBAAkB,KAAK,QACvD,OAIR,EAAU,KAAK,EACnB,EAUA,eAAgB,SAAS,EAAgB,GACrC,QAAS,KAAK,MAAM,EAAgB,EACxC,EAWA,UAAW,SAAS,EAAgB,GAChC,IAAI,EAAU,KAAK,MAAM,EAAgB,GAGzC,QAAI,GACO,KAAK,YAAY,EAAgB,EAAgB,EAIhE,EAYA,kBAAmB,SAAS,EAAgB,EAAgB,GACxD,IAAI,EAAU,KAAK,MAAM,EAAgB,GAEzC,QAAI,GACO,KAAK,aAAa,EAAgB,EAAgB,EAAS,EAI1E,GAGJ,OAAO,QAAU","file":"/npm/collision-gjk-epa@0.2.0/collision-gjk-epa.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.37.0.\n * Original file: /npm/collision-gjk-epa@0.2.0/collision-gjk-epa.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","'use strict';\r\n\r\n/**\r\n * Class to help in the collision in 2D and 3D.\r\n * To works the algorithm needs two convexe point cloud\r\n * like bounding box or smthg like this.\r\n * The function functions intersect and isIntersecting\r\n * helps to have informations about the collision between two object.\r\n *\r\n * @class wnp.helpers.CollisionGjkEpa\r\n * @constructor\r\n */\r\nvar Triangle = function(a, b, c) {\r\n      this.a = a;\r\n      this.b = b;\r\n      this.c = c;\r\n      this.n = a.constructor.Cross(b.subtract(a), c.subtract(a)).normalize();\r\n  };\r\n\r\n\r\nvar CollisionGjkEpa = {\r\n\r\n    EPSILON: 0.000001,\r\n\r\n    /**\r\n     * Method to get a normal of 3 points in 2D and 3D.\r\n     *\r\n     * @method _getNormal\r\n     * @private\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} a\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} b\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} c\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3} The normal.\r\n     */\r\n    _getNormal: function(a, b, c) {\r\n        var Dot = a.constructor.Dot;\r\n        var ac = Dot(a, c); // perform aDot(c)\r\n        var bc = Dot(b, c); // perform bDot(c)\r\n\r\n        // perform b * a.Dot(c) - a * b.Dot(c)\r\n        var r = b.scale(ac).subtract(a.scale(bc));\r\n        if (r.lengthSquared() < this.EPSILON * this.EPSILON) {\r\n            return r.scale(0);\r\n        }\r\n        return r.normalize();\r\n    },\r\n\r\n    /**\r\n     * Gets the barycenter of a cloud points.\r\n     *\r\n     * @method _getBarycenter\r\n     * @private\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} vertices the cloud points\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3} The barycenter.\r\n     */\r\n    _getBarycenter: function(vertices) {\r\n        var avg = vertices[0].constructor.Zero();\r\n        for (var i = 0; i < vertices.length; i++) {\r\n            avg.addToRef(vertices[i], avg);\r\n        }\r\n        avg.scaleInPlace(1 / vertices.length, avg);\r\n        return avg;\r\n    },\r\n\r\n    /**\r\n     * Gets the farthest point of a cloud points.\r\n     *\r\n     * @method _getFarthestPointInDirection\r\n     * @private\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} vertices the cloud points.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} d The direction to search.\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3} The barycenter.\r\n     */\r\n    _getFarthestPointInDirection: function(vertices, d) {\r\n        var Dot = vertices[0].constructor.Dot;\r\n        var maxProduct = Dot(vertices[0], d);\r\n        var index = 0;\r\n        for (var i = 1; i < vertices.length; i++) {\r\n            var product = Dot(vertices[i], d);\r\n            if (product > maxProduct) {\r\n                maxProduct = product;\r\n                index = i;\r\n            }\r\n        }\r\n        return vertices[index];\r\n    },\r\n\r\n    /**\r\n     * Gets the nearest edge of the simplex.\r\n     *\r\n     * @method _getNearestEdge\r\n     * @private\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} simplex The simplex.\r\n     * @param {number[]} [except] Exceptions: edge indices to ignore\r\n     * @return {Object} Informations about the nearest edge (distance, index and normal).\r\n     */\r\n    _getNearestEdge: function(simplex, except) {\r\n        if (except === undefined) except = [];\r\n\r\n        var distance = Infinity,\r\n            index, normal;\r\n\r\n        for (var i = 0; i < simplex.length; i++) {\r\n            var j = (i + 1) % simplex.length;\r\n            if (except.includes(j)) continue; // ignore this edge\r\n\r\n            var v1 = simplex[i];\r\n            var v2 = simplex[j];\r\n\r\n            var edge = v2.subtract(v1);\r\n            if (edge.lengthSquared() === 0) {\r\n                continue;\r\n            }\r\n\r\n            var originTov1 = v1;\r\n\r\n            var n = this._getNormal(edge, originTov1, edge);\r\n\r\n            if (n.lengthSquared() === 0) {\r\n                // Origin is on the edge\r\n                n.y = -edge.x;\r\n                n.x = edge.y;\r\n                // normal should go outside the simplex\r\n                var center = this._getBarycenter(simplex)\r\n                var centerTov1 = v1.subtract(center);\r\n                if (n.constructor.Dot(n, centerTov1) < 0) {\r\n                    n.y = -n.y;\r\n                    n.x = -n.x;\r\n                }\r\n            }\r\n\r\n            //n = n.scale(1 / n.length()); //normalize\r\n            var dist = Math.abs(n.constructor.Dot(n, v1)); //distance from origin to edge\r\n\r\n            if (dist < distance) {\r\n                distance = dist;\r\n                index = j;\r\n                normal = n;\r\n            }\r\n        }\r\n\r\n        return {\r\n            distance: distance,\r\n            index: index,\r\n            normal: normal\r\n        };\r\n\r\n    },\r\n\r\n    /**\r\n     * Gets the nearest Triangle of the polytope.\r\n     *\r\n     * @method _getNearestTriangle\r\n     * @private\r\n     * @param {BABYLON.Triangle[]} polytope The polytope.\r\n     * @param {number[]} [except] Exceptions: triangle indices to ignore\r\n     * @return {Object} Informations about the nearest edge (distance and index).\r\n     */\r\n    _getNearestTriangle: function(polytope, except) {\r\n        if (except === undefined) except = [];\r\n\r\n        var distance = Infinity,\r\n            index;\r\n\r\n        for (var i = 0; i < polytope.length; i++) {\r\n            if (except.includes(i)) continue;\r\n\r\n            var triangle = polytope[i];\r\n            var dist = Math.abs(triangle.n.constructor.Dot(triangle.n, triangle.a));\r\n\r\n            if (dist < distance) {\r\n                distance = dist;\r\n                index = i;\r\n            }\r\n\r\n        }\r\n\r\n        return {\r\n            distance: distance,\r\n            index: index\r\n        };\r\n    },\r\n\r\n    /**\r\n     * Checks if the origin is in a line.\r\n     *\r\n     * @method _containsLine\r\n     * @private\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} simplex The simplex.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} dir The direction.\r\n     * @return {Boolean} False in any case because the algorithm just begin.\r\n     */\r\n    _containsLine: function(simplex, dir) {\r\n        var a = simplex[1];\r\n        var b = simplex[0];\r\n        var ab = b.subtract(a);\r\n        var ao = a.scale(-1);\r\n        if (ab.lengthSquared() !== 0) {\r\n            dir.copyFrom(this._getNormal(ab, ao, ab));\r\n        } else {\r\n            dir.copyFrom(ao);\r\n        }\r\n\r\n        return false;\r\n    },\r\n\r\n    /**\r\n     * Checks if the origin is in a triangle.\r\n     *\r\n     * @method _containsTriangle\r\n     * @private\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} simplex The simplex.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} dir The direction.\r\n     * @return {Boolean} If in 2D case, may return true if the origin is in the triangle.\r\n     */\r\n    _containsTriangle: function(simplex, dir) {\r\n        var a = simplex[2];\r\n        var Dot = simplex[2].constructor.Dot;\r\n        var b = simplex[1];\r\n        var c = simplex[0];\r\n        var ab = b.subtract(a);\r\n        var ac = c.subtract(a);\r\n        var ao = a.scale(-1);\r\n\r\n        var abp = this._getNormal(ac, ab, ab);\r\n        var acp = this._getNormal(ab, ac, ac);\r\n        if (Dot(abp, ao) > 0) {\r\n            simplex.splice(0, 1); // remove C\r\n            dir.copyFrom(abp);\r\n        } else if (Dot(acp, ao) > 0) {\r\n            simplex.splice(1, 1); // remove B\r\n            dir.copyFrom(acp);\r\n        } else {\r\n            if (dir.z === undefined) {\r\n                return true;\r\n            } else {\r\n                var abc = simplex[2].constructor.Cross(ab, ac);\r\n                dir.copyFrom(abc);\r\n                if (Dot(abc, ao) <= 0) {\r\n                    //upside down tetrahedron\r\n                    simplex[0] = b;\r\n                    simplex[1] = c;\r\n                    simplex[2] = a;\r\n                    dir.copyFrom(abc.scale(-1));\r\n                }\r\n\r\n                return false;\r\n            }\r\n        }\r\n    },\r\n\r\n    /**\r\n     * Checks if the origin is in a tetrahedron.\r\n     *\r\n     * @method _containsTetrahedron\r\n     * @private\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} simplex The simplex.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} dir The direction.\r\n     * @return {Boolean} Return true if the origin is in the tetrahedron.\r\n     */\r\n    _containsTetrahedron: function(simplex, dir) {\r\n        var a = simplex[3];\r\n        var Dot = a.constructor.Dot;\r\n        var Cross = a.constructor.Cross;\r\n        var b = simplex[2];\r\n        var c = simplex[1];\r\n        var d = simplex[0];\r\n        var ab = b.subtract(a);\r\n        var ac = c.subtract(a);\r\n        var ad = d.subtract(a);\r\n        var ao = a.scale(-1);\r\n\r\n        var abc = Cross(ab, ac);\r\n        var acd = Cross(ac, ad);\r\n        var adb = Cross(ad, ab);\r\n\r\n        var abcTest = 0x1,\r\n            acdTest = 0x2,\r\n            adbTest = 0x4;\r\n\r\n        var planeTests = (Dot(abc, ao) > 0 ? abcTest : 0) |\r\n            (Dot(acd, ao) > 0 ? acdTest : 0) |\r\n            (Dot(adb, ao) > 0 ? adbTest : 0);\r\n\r\n        switch (planeTests) {\r\n            case abcTest:\r\n                return this._checkTetrahedron(ao, ab, ac, abc, dir, simplex);\r\n            case acdTest:\r\n                simplex[2] = c;\r\n                simplex[1] = d;\r\n                return this._checkTetrahedron(ao, ac, ad, acd, dir, simplex);\r\n            case adbTest:\r\n                //in front of triangle ADB\r\n                simplex[1] = b;\r\n                simplex[2] = d;\r\n                return this._checkTetrahedron(ao, ad, ab, adb, dir, simplex);\r\n            case abcTest | acdTest:\r\n                return this._checkTwoTetrahedron(ao, ab, ac, abc, dir, simplex);\r\n            case acdTest | adbTest:\r\n                simplex[2] = c;\r\n                simplex[1] = d;\r\n                simplex[0] = b;\r\n                return this._checkTwoTetrahedron(ao, ac, ad, acd, dir, simplex);\r\n            case adbTest | abcTest:\r\n                simplex[1] = b;\r\n                simplex[2] = d;\r\n                simplex[0] = c;\r\n                return this._checkTwoTetrahedron(ao, ad, ab, adb, dir, simplex);\r\n            default:\r\n                break;\r\n        }\r\n\r\n        //origin in tetrahedron\r\n        return true;\r\n    },\r\n\r\n    /**\r\n     * @method _checkTwoTetrahedron\r\n     * @private\r\n     */\r\n    _checkTwoTetrahedron: function(ao, ab, ac, abc, dir, simplex) {\r\n        var abc_ac = abc.constructor.Cross(abc, ac);\r\n\r\n        if (abc_ac.constructor.Dot(abc_ac, ao) > 0) {\r\n            //the origin is beyond AC from ABC's\r\n            //perspective, effectively excluding\r\n            //ACD from consideration\r\n\r\n            //we thus need test only ACD\r\n            simplex[2] = simplex[1];\r\n            simplex[1] = simplex[0];\r\n\r\n            ab = simplex[2].subtract(simplex[3]);\r\n            ac = simplex[1].subtract(simplex[3]);\r\n            abc = ab.constructor.Cross(ab, ac);\r\n\r\n            return this._checkTetrahedron(ao, ab, ac, abc, dir, simplex);\r\n        }\r\n\r\n        var ab_abc = abc.constructor.Cross(ab, abc);\r\n\r\n        if (ab_abc.constructor.Dot(ab_abc, ao) > 0) {\r\n\r\n            simplex.splice(0, 2);\r\n            //dir is not ab_abc because it's not point towards the origin;\r\n            //ABxA0xAB direction we are looking for\r\n            dir.copyFrom(this._getNormal(ab, ao, ab));\r\n\r\n            return false;\r\n        }\r\n\r\n    },\r\n\r\n    /**\r\n     * @method _checkTetrahedron\r\n     * @private\r\n     */\r\n    _checkTetrahedron: function(ao, ab, ac, abc, dir, simplex) {\r\n\r\n        var acp = abc.constructor.Cross(abc, ac);\r\n\r\n        if (acp.constructor.Dot(acp, ao) > 0) {\r\n\r\n            simplex[2] = simplex[3];\r\n            simplex.splice(3, 1);\r\n            simplex.splice(0, 1);\r\n            //dir is not abc_ac because it's not point towards the origin;\r\n            //ACxA0xAC direction we are looking for\r\n            dir.copyFrom(this._getNormal(ac, ao, ac));\r\n\r\n            return false;\r\n        }\r\n\r\n        //almost the same like triangle checks\r\n        var ab_abc = ab.constructor.Cross(ab, abc);\r\n\r\n        if (ab_abc.constructor.Dot(ab_abc, ao) > 0) {\r\n\r\n            simplex.splice(0, 2);\r\n            //dir is not ab_abc because it's not point towards the origin;\r\n            //ABxA0xAB direction we are looking for\r\n            dir.copyFrom(this._getNormal(ab, ao, ab));\r\n\r\n            return false;\r\n        }\r\n\r\n        //build new tetrahedron with new base\r\n        simplex.splice(0, 1);\r\n\r\n        dir.copyFrom(abc);\r\n\r\n        return false;\r\n    },\r\n\r\n    /**\r\n     * Adds adge to the list and checks if the edge if not in.\r\n     *\r\n     * @method _addEdge\r\n     * @private\r\n     * @param {Object[]} edges The edges.\r\n     * @param {Object} dir The edge to check.\r\n     */\r\n    _addEdge: function(edges, edge) {\r\n        for (var j = 0; j < edges.length; j++) {\r\n            if (edges[j].a === edge.b && edges[j].b === edge.a) {\r\n                edges.splice(j, 1);\r\n                return;\r\n            }\r\n        }\r\n        edges.push(edge);\r\n    },\r\n\r\n    /**\r\n     * The support function.\r\n     *\r\n     * @method support\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} colliderPoints The convexe collider object.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} collidedPoints The convexe collided object.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} direction The direction.\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3} The support points.\r\n     */\r\n    support: function(colliderPoints, collidedPoints, direction) {\r\n        // d is a vector direction (doesn't have to be normalized)\r\n        // get points on the edge of the shapes in opposite directions\r\n        direction.normalize();\r\n        var p1 = this._getFarthestPointInDirection(colliderPoints, direction);\r\n        var p2 = this._getFarthestPointInDirection(collidedPoints, direction.scale(-1));\r\n        // perform the Minkowski Difference\r\n        var p3 = p1.subtract(p2);\r\n        // p3 is now a point in Minkowski space on the edge of the Minkowski Difference\r\n        return p3;\r\n    },\r\n\r\n    /**\r\n     * Checks if the simplex contains the origin.\r\n     *\r\n     * @method containsOrigin\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} simplexThe simplex or false if no intersection.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} dir The direction to test.\r\n     * @return {Boolean} Contains or not.\r\n     */\r\n    containsOrigin: function(simplex, dir) {\r\n        if (simplex.length === 2) {\r\n            return this._containsLine(simplex, dir);\r\n        }\r\n        if (simplex.length === 3) {\r\n            return this._containsTriangle(simplex, dir);\r\n        }\r\n        if (simplex.length === 4) {\r\n            return this._containsTetrahedron(simplex, dir);\r\n        }\r\n        return false;\r\n    },\r\n\r\n    /**\r\n     * The GJK (Gilbert–Johnson–Keerthi) algorithm.\r\n     * Computes support points to build the Minkowsky difference and\r\n     * create a simplex. The points of the collider and the collided object\r\n     * must be convexe.\r\n     *\r\n     * @method check\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} colliderPoints The convexe collider object.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} collidedPoints The convexe collided object.\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3[]} The simplex or false if no intersection.\r\n     */\r\n    check: function(colliderPoints, collidedPoints) {\r\n        var it = 0;\r\n        var a, simplex = [];\r\n\r\n        var colliderCenter = this._getBarycenter(colliderPoints);\r\n        var collidedCenter = this._getBarycenter(collidedPoints);\r\n\r\n        // initial direction from the center of 1st body to the center of 2nd body\r\n        var dir = colliderCenter.subtract(collidedCenter).normalize();\r\n\r\n        // if initial direction is zero – set it to any arbitrary axis (we choose X)\r\n        if (dir.lengthSquared() < this.EPSILON) {\r\n            dir.x = 1;\r\n        }\r\n\r\n        // set the first support as initial point of the new simplex\r\n        a = simplex[0] = this.support(colliderPoints, collidedPoints, dir);\r\n\r\n        if (a.constructor.Dot(a, dir) <= 0) {\r\n            return false;\r\n        }\r\n\r\n        dir.scaleInPlace(-1, dir); // we will be searching in the opposite direction next\r\n\r\n        var max = collidedPoints.length * colliderPoints.length;\r\n        while (it < max) {\r\n            if (dir.lengthSquared() === 0 && simplex.length >= 2) {\r\n                // Get perpendicular direction to last simplex\r\n                dir = simplex[simplex.length - 1].subtract(simplex[simplex.length - 2]);\r\n                var tmp = dir.y;\r\n                dir.y = -dir.x;\r\n                dir.x = tmp;\r\n            }\r\n\r\n            a = this.support(colliderPoints, collidedPoints, dir);\r\n\r\n            // make sure that the last point we added actually passed the origin\r\n            if (a.constructor.Dot(a, dir) <= 0) {\r\n                // if the point added last was not past the origin in the direction of d\r\n                // then the Minkowski Sum cannot possibly contain the origin since\r\n                // the last point added is on the edge of the Minkowski Difference\r\n                return false;\r\n            }\r\n\r\n            simplex.push(a);\r\n            // otherwise we need to determine if the origin is in\r\n            // the current simplex\r\n\r\n            if (this.containsOrigin(simplex, dir)) {\r\n                return simplex;\r\n            }\r\n            it++;\r\n        }\r\n    },\r\n\r\n    /**\r\n     * Finds the response with the simplex (edges) of the gjk algorithm.\r\n     *\r\n     * @method findResponseWithEdge\r\n     * @param {BABYLON.Vector2[]} colliderPoints The convexe collider object.\r\n     * @param {BABYLON.Vector2[]} collidedPoints The convexe collided object.\r\n     * @param {BABYLON.Vector2[]} simplex The simplex.\r\n     * @param {number[]} [except] Exceptions: edge indices to ignore\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3} The penetration vector.\r\n     */\r\n    findResponseWithEdge: function(colliderPoints, collidedPoints, simplex, except) {\r\n        if (except === undefined) except = [];\r\n\r\n        var edge = this._getNearestEdge(simplex, except);\r\n        if (edge.index === undefined) return false;\r\n\r\n        var sup = this.support(colliderPoints, collidedPoints, edge.normal); //get support point in direction of edge's normal\r\n        var d = Math.abs(sup.constructor.Dot(sup, edge.normal));\r\n\r\n        if (d - edge.distance <= this.EPSILON) {\r\n            except.push(edge.index);\r\n            return edge.normal.scale(edge.distance);\r\n        }\r\n        // add new support point in simplex\r\n        simplex.splice(edge.index, 0, sup);\r\n        // update indices of edges exceptions\r\n        this._updateExceptionsByInsert(except, edge.index, simplex);\r\n\r\n        return false;\r\n    },\r\n\r\n    /**\r\n     * Finds the response with the polytope done with the simplex of the gjk algorithm.\r\n     *\r\n     * @method findResponseWithTriangle\r\n     * @param {BABYLON.Vector3[]} colliderPoints The convexe collider object.\r\n     * @param {BABYLON.Vector3[]} collidedPoints The convexe collided object.\r\n     * @param {Triangle[]} polytope The polytope done with the simplex.\r\n     * @param {number[]} [except] Exceptions: triangle indices to ignore\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3} The penetration vector.\r\n     */\r\n    findResponseWithTriangle: function(colliderPoints, collidedPoints, polytope, except) {\r\n        if (except === undefined) except = [];\r\n\r\n        if (polytope.length === 0) {\r\n            return false;\r\n        }\r\n\r\n        var nearest = this._getNearestTriangle(polytope, except);\r\n        if (nearest.index === undefined) return false;\r\n\r\n        var triangle = polytope[nearest.index];\r\n\r\n        var sup = this.support(colliderPoints, collidedPoints, triangle.n);\r\n\r\n        var d = Math.abs(sup.constructor.Dot(sup, triangle.n));\r\n\r\n        if ((d - nearest.distance <= this.EPSILON)) {\r\n            except.push(nearest.index);\r\n            return triangle.n.scale(nearest.distance);\r\n        }\r\n\r\n        var edges = [];\r\n        for (var i = polytope.length - 1; i >= 0; i--) {\r\n            triangle = polytope[i];\r\n            // can this face be 'seen' by entry_cur_support?\r\n            if (triangle.n.constructor.Dot(triangle.n, sup.subtract(polytope[i].a)) > 0) {\r\n                this._addEdge(edges, {\r\n                    a: triangle.a,\r\n                    b: triangle.b\r\n                });\r\n                this._addEdge(edges, {\r\n                    a: triangle.b,\r\n                    b: triangle.c\r\n                });\r\n                this._addEdge(edges, {\r\n                    a: triangle.c,\r\n                    b: triangle.a\r\n                });\r\n                // remove a triangle\r\n                polytope.splice(i, 1);\r\n                // update exceptions with removed triangle index\r\n                this._removeException(except, i);\r\n            }\r\n        }\r\n\r\n        // create new triangles from the edges in the edge list\r\n        for (var i = 0; i < edges.length; i++) {\r\n            triangle = new Triangle(sup, edges[i].a, edges[i].b);\r\n            if (triangle.n.length() !== 0) {\r\n                polytope.push(triangle);\r\n            }\r\n        }\r\n\r\n        return false;\r\n    },\r\n\r\n    /**\r\n     * Update exception from insertion of new edge\r\n     * @param {number[]} except \r\n     * @param {number} edgeIndex inserted edge index\r\n     * @param {BABYLON.Vector2[]} simplex \r\n     */\r\n    _updateExceptionsByInsert: function(except, edgeIndex, simplex) {\r\n        for (var i = 0; i < except.length; i++) {\r\n            if (except[i] >= edgeIndex) {\r\n                except[i] = (except[i] + 1) % simplex.length;\r\n            }\r\n        }\r\n    },\r\n\r\n    /**\r\n     * Update exceptions when an index has been removed\r\n     * @param {number[]} except \r\n     * @param {number} indexToRemove \r\n     */\r\n    _removeException: function(except, indexToRemove) {\r\n        for (var i = except.length - 1; i >= 0; i--) {\r\n            if(except[i] === indexToRemove) {\r\n                except.splice(i, 1);\r\n            } else if (except[i] > indexToRemove) {\r\n                except[i]--;\r\n            }\r\n        }\r\n    },\r\n\r\n    /**\r\n     * Gets the response of the penetration vector with the simplex.\r\n     *\r\n     * @method getResponse\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} colliderPoints The convexe collider object.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} collidedPoints The convexe collided object.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} simplex The simplex of the Minkowsky difference.\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3} The penetration vector.\r\n     */\r\n    getResponse: function(colliderPoints, collidedPoints, simplex) {\r\n        var responses = this.getResponses(colliderPoints, collidedPoints, simplex, { maxResponses: 1 });\r\n        if (!responses) return responses;\r\n        if (responses[0]) return responses[0];\r\n        return false;\r\n    },\r\n\r\n    getResponses: function(colliderPoints, collidedPoints, simplex, options) {\r\n        if (options === undefined) options = {};\r\n        options = Object.assign({ maxResponses: -1 }, options);\r\n\r\n        var it = 0,\r\n            response;\r\n        var polytope = simplex[0].z !== undefined ? [new Triangle(simplex[0], simplex[1], simplex[2]),\r\n            new Triangle(simplex[0], simplex[2], simplex[3]),\r\n            new Triangle(simplex[0], simplex[3], simplex[1]),\r\n            new Triangle(simplex[1], simplex[3], simplex[2])\r\n        ] : null;\r\n\r\n        var max = collidedPoints.length * colliderPoints.length;\r\n        var except = [];\r\n        var responses = [];\r\n        while (it < max && responses.length !== options.maxResponses) {\r\n            if (simplex[0].z === undefined) {\r\n                response = this.findResponseWithEdge(colliderPoints, collidedPoints, simplex, except);\r\n            } else {\r\n                response = this.findResponseWithTriangle(colliderPoints, collidedPoints, polytope, except);\r\n            }\r\n            if (response) {\r\n                var norm = response.clone().normalize().scaleInPlace(this.EPSILON);\r\n                var reponseWithOffset = response.addToRef(norm, response);\r\n                this._addResponse(responses, reponseWithOffset);\r\n            }\r\n            it++;\r\n        }\r\n        return responses;\r\n    },\r\n\r\n    /**\r\n     * Add response to responses if not already present\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} responses\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3} response\r\n     */\r\n    _addResponse: function(responses, response) {\r\n        for (var i = 0; i < responses.length; i++) {\r\n            if (responses[i].subtract(response).lengthSquared() < this.EPSILON) {\r\n                return;\r\n            }\r\n        }\r\n\r\n        responses.push(response);\r\n    },\r\n\r\n    /**\r\n     * Checks if the collider and the collided object are intersecting.\r\n     *\r\n     * @method isIntersecting\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} colliderPoints The convexe collider object.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} collidedPoints The convexe collided object.\r\n     * @return {Boolean} Is intersecting or not.\r\n     */\r\n    isIntersecting: function(colliderPoints, collidedPoints) {\r\n        return !!this.check(colliderPoints, collidedPoints);\r\n    },\r\n\r\n    /**\r\n     * Checks if the collider and the collided object are intersecting\r\n     * and give the response to be out of the object.\r\n     *\r\n     * @method intersect\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} colliderPoints The convexe collider object.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} collidedPoints The convexe collided object.\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3} The penetration vector.\r\n     */\r\n    intersect: function(colliderPoints, collidedPoints) {\r\n        var simplex = this.check(colliderPoints, collidedPoints);\r\n\r\n        //this.cube = this.cube || [];\r\n        if (simplex) {\r\n            return this.getResponse(colliderPoints, collidedPoints, simplex);\r\n        }\r\n\r\n        return false;\r\n    },\r\n\r\n    /**\r\n     * Checks if the collider and the collided object are intersecting\r\n     * and give the multiple responses to be out of the object.\r\n     *\r\n     * @method intersectMultiple\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} colliderPoints The convexe collider object.\r\n     * @param {BABYLON.Vector2|BABYLON.Vector3[]} collidedPoints The convexe collided object.\r\n     * @param {Object} [options]\r\n     * @return {BABYLON.Vector2|BABYLON.Vector3[]} The penetration vectors.\r\n     */\r\n    intersectMultiple: function(colliderPoints, collidedPoints, options) {\r\n        var simplex = this.check(colliderPoints, collidedPoints);\r\n\r\n        if (simplex) {\r\n            return this.getResponses(colliderPoints, collidedPoints, simplex, options);\r\n        }\r\n\r\n        return false;\r\n    }\r\n};\r\n\r\nmodule.exports = CollisionGjkEpa;\r\n"]}