cocos2d-js icon indicating copy to clipboard operation
cocos2d-js copied to clipboard

bug: cp.convexHull is unavailable on JSB

Open zhaijialong opened this issue 10 years ago • 1 comments

zhaijialong avatar Feb 05 '15 07:02 zhaijialong

i just copy html5 for jsb and work well

` if(!cc.convexHull) {

cc.loopIndexes = function(verts)
{
    var start = 0, end = 0;
    var minx, miny, maxx, maxy;
    minx = maxx = verts[0];
    miny = maxy = verts[1];

    var count = verts.length >> 1;
    for(var i=1; i<count; i++){
        var x = verts[i*2];
        var y = verts[i*2 + 1];

        if(x < minx || (x == minx && y < miny)){
            minx = x;
            miny = y;
            start = i;
        } else if(x > maxx || (x == maxx && y > maxy)){
            maxx = x;
            maxy = y;
            end = i;
        }
    }
    return [start, end];
};

cc.SWAP = function(arr, idx1, idx2)
{
    var tmp = arr[idx1*2];
    arr[idx1*2] = arr[idx2*2];
    arr[idx2*2] = tmp;

    tmp = arr[idx1*2+1];
    arr[idx1*2+1] = arr[idx2*2+1];
    arr[idx2*2+1] = tmp;
};

cc.QHullPartition = function(verts, offs, count, a, b, tol)
{
    if(count === 0) return 0;

    var max = 0;
    var pivot = offs;

    var delta = cp.v.sub(b, a);
    var valueTol = tol * cp.v.len(delta);

    var head = offs;
    for(var tail = offs+count-1; head <= tail;){
        var v = new cp.Vect(verts[head * 2], verts[head * 2 + 1]);
        var value = cp.v.cross(delta, cp.v.sub(v, a));
        if(value > valueTol){
            if(value > max){
                max = value;
                pivot = head;
            }

            head++;
        } else {
            cc.SWAP(verts, head, tail);
            tail--;
        }
    }

    // move the new pivot to the front if it's not already there.
    if(pivot != offs) cc.SWAP(verts, offs, pivot);
    return head - offs;
};

cc.QHullReduce = function(tol, verts, offs, count, a, pivot, b, resultPos)
{
    if(count < 0){
        return 0;
    } else if(count == 0) {
        verts[resultPos*2] = pivot.x;
        verts[resultPos*2+1] = pivot.y;
        return 1;
    } else {
        var left_count = cc.QHullPartition(verts, offs, count, a, pivot, tol);
        var left = new cp.Vect(verts[offs*2], verts[offs*2+1]);
        var index = cc.QHullReduce(tol, verts, offs + 1, left_count - 1, a, left, pivot, resultPos);

        var pivotPos = resultPos + index++;
        verts[pivotPos*2] = pivot.x;
        verts[pivotPos*2+1] = pivot.y;

        var right_count = cc.QHullPartition(verts, offs + left_count, count - left_count, pivot, b, tol);
        var right = new cp.Vect(verts[(offs+left_count)*2], verts[(offs+left_count)*2+1]);
        return index + cc.QHullReduce(tol, verts, offs + left_count + 1, right_count - 1, pivot, right, b, resultPos + index);
    }
};

cc.convexHull = function(verts, result, tolerance)
{
    if(result){
        // Copy the line vertexes into the empty part of the result polyline to use as a scratch buffer.
        for (var i = 0; i < verts.length; i++){
            result[i] = verts[i];
        }
    } else {
        // If a result array was not specified, reduce the input instead.
        result = verts;
    }

    // Degenerate case, all points are the same.
    var indexes = cc.loopIndexes(verts);
    var start = indexes[0], end = indexes[1];
    if(start == end){
        //if(first) (*first) = 0;
        result.length = 2;
        return result;
    }

    cc.SWAP(result, 0, start);
    cc.SWAP(result, 1, end == 0 ? start : end);

    var a = new cp.Vect(result[0], result[1]);
    var b = new cp.Vect(result[2], result[3]);

    var count = verts.length >> 1;
    //if(first) (*first) = start;
    var resultCount = cc.QHullReduce(tolerance, result, 2, count - 2, a, b, a, 1) + 1;
    result.length = resultCount*2;

    // assertSoft(polyValidate(result),
    //     "Internal error: cpConvexHull() and cpPolyValidate() did not agree." +
    //     "Please report this error with as much info as you can.");
    return result;
};

}`

3dseals avatar Aug 18 '16 06:08 3dseals