Search in sources :

Example 1 with IntArrayList

use of com.bulletphysics.util.IntArrayList in project bdx by GoranM.

the class Dbvt method collideOCL.

public static void collideOCL(Node root, Vector3f[] normals, float[] offsets, Vector3f sortaxis, int count, ICollide policy, boolean fullsort) {
    //DBVT_CHECKTYPE
    if (root != null) {
        int srtsgns = (sortaxis.x >= 0 ? 1 : 0) + (sortaxis.y >= 0 ? 2 : 0) + (sortaxis.z >= 0 ? 4 : 0);
        int inside = (1 << count) - 1;
        ObjectArrayList<sStkNPS> stock = new ObjectArrayList<sStkNPS>();
        IntArrayList ifree = new IntArrayList();
        IntArrayList stack = new IntArrayList();
        int[] signs = new int[/*sizeof(unsigned)*8*/
        4 * 8];
        assert (count < (/*sizeof(signs)*/
        128 / /*sizeof(signs[0])*/
        4));
        for (int i = 0; i < count; i++) {
            signs[i] = ((normals[i].x >= 0) ? 1 : 0) + ((normals[i].y >= 0) ? 2 : 0) + ((normals[i].z >= 0) ? 4 : 0);
        }
        //stock.reserve(SIMPLE_STACKSIZE);
        //stack.reserve(SIMPLE_STACKSIZE);
        //ifree.reserve(SIMPLE_STACKSIZE);
        stack.add(allocate(ifree, stock, new sStkNPS(root, 0, root.volume.ProjectMinimum(sortaxis, srtsgns))));
        do {
            // JAVA NOTE: check
            int id = stack.remove(stack.size() - 1);
            sStkNPS se = stock.getQuick(id);
            ifree.add(id);
            if (se.mask != inside) {
                boolean out = false;
                for (int i = 0, j = 1; (!out) && (i < count); ++i, j <<= 1) {
                    if (0 == (se.mask & j)) {
                        int side = se.node.volume.Classify(normals[i], offsets[i], signs[i]);
                        switch(side) {
                            case -1:
                                out = true;
                                break;
                            case +1:
                                se.mask |= j;
                                break;
                        }
                    }
                }
                if (out) {
                    continue;
                }
            }
            if (policy.Descent(se.node)) {
                if (se.node.isinternal()) {
                    Node[] pns = new Node[] { se.node.childs[0], se.node.childs[1] };
                    sStkNPS[] nes = new sStkNPS[] { new sStkNPS(pns[0], se.mask, pns[0].volume.ProjectMinimum(sortaxis, srtsgns)), new sStkNPS(pns[1], se.mask, pns[1].volume.ProjectMinimum(sortaxis, srtsgns)) };
                    int q = nes[0].value < nes[1].value ? 1 : 0;
                    int j = stack.size();
                    if (fullsort && (j > 0)) {
                        /* Insert 0	*/
                        j = nearest(stack, stock, nes[q].value, 0, stack.size());
                        stack.add(0);
                        //#else
                        for (int k = stack.size() - 1; k > j; --k) {
                            stack.set(k, stack.get(k - 1));
                        //#endif
                        }
                        stack.set(j, allocate(ifree, stock, nes[q]));
                        /* Insert 1	*/
                        j = nearest(stack, stock, nes[1 - q].value, j, stack.size());
                        stack.add(0);
                        //#else
                        for (int k = stack.size() - 1; k > j; --k) {
                            stack.set(k, stack.get(k - 1));
                        //#endif
                        }
                        stack.set(j, allocate(ifree, stock, nes[1 - q]));
                    } else {
                        stack.add(allocate(ifree, stock, nes[q]));
                        stack.add(allocate(ifree, stock, nes[1 - q]));
                    }
                } else {
                    policy.Process(se.node, se.value);
                }
            }
        } while (stack.size() != 0);
    }
}
Also used : ObjectArrayList(com.bulletphysics.util.ObjectArrayList) Node(com.bulletphysics.collision.broadphase.Dbvt.Node) IntArrayList(com.bulletphysics.util.IntArrayList)

Example 2 with IntArrayList

use of com.bulletphysics.util.IntArrayList in project bdx by GoranM.

the class GImpactCollisionAlgorithm method gimpact_vs_shape.

public void gimpact_vs_shape(CollisionObject body0, CollisionObject body1, GImpactShapeInterface shape0, CollisionShape shape1, boolean swapped) {
    if (shape0.getGImpactShapeType() == ShapeType.TRIMESH_SHAPE) {
        GImpactMeshShape meshshape0 = (GImpactMeshShape) shape0;
        part0 = meshshape0.getMeshPartCount();
        while ((part0--) != 0) {
            gimpact_vs_shape(body0, body1, meshshape0.getMeshPart(part0), shape1, swapped);
        }
        return;
    }
    //#ifdef GIMPACT_VS_PLANE_COLLISION
    if (shape0.getGImpactShapeType() == ShapeType.TRIMESH_SHAPE_PART && shape1.getShapeType() == BroadphaseNativeType.STATIC_PLANE_PROXYTYPE) {
        GImpactMeshShapePart shapepart = (GImpactMeshShapePart) shape0;
        StaticPlaneShape planeshape = (StaticPlaneShape) shape1;
        gimpacttrimeshpart_vs_plane_collision(body0, body1, shapepart, planeshape, swapped);
        return;
    }
    if (shape1.isCompound()) {
        CompoundShape compoundshape = (CompoundShape) shape1;
        gimpact_vs_compoundshape(body0, body1, shape0, compoundshape, swapped);
        return;
    } else if (shape1.isConcave()) {
        ConcaveShape concaveshape = (ConcaveShape) shape1;
        gimpact_vs_concave(body0, body1, shape0, concaveshape, swapped);
        return;
    }
    Stack stack = Stack.enter();
    Transform orgtrans0 = body0.getWorldTransform(stack.allocTransform());
    Transform orgtrans1 = body1.getWorldTransform(stack.allocTransform());
    IntArrayList collided_results = new IntArrayList();
    gimpact_vs_shape_find_pairs(orgtrans0, orgtrans1, shape0, shape1, collided_results);
    if (collided_results.size() == 0) {
        return;
    }
    shape0.lockChildShapes();
    GIM_ShapeRetriever retriever0 = new GIM_ShapeRetriever(shape0);
    boolean child_has_transform0 = shape0.childrenHasTransform();
    Transform tmpTrans = stack.allocTransform();
    int i = collided_results.size();
    while ((i--) != 0) {
        int child_index = collided_results.get(i);
        if (swapped) {
            triface1 = child_index;
        } else {
            triface0 = child_index;
        }
        CollisionShape colshape0 = retriever0.getChildShape(child_index);
        if (child_has_transform0) {
            tmpTrans.mul(orgtrans0, shape0.getChildTransform(child_index));
            body0.setWorldTransform(tmpTrans);
        }
        // collide two shapes
        if (swapped) {
            shape_vs_shape_collision(body1, body0, shape1, colshape0);
        } else {
            shape_vs_shape_collision(body0, body1, colshape0, shape1);
        }
        // restore transforms
        if (child_has_transform0) {
            body0.setWorldTransform(orgtrans0);
        }
    }
    shape0.unlockChildShapes();
    stack.leave();
}
Also used : CollisionShape(com.bulletphysics.collision.shapes.CollisionShape) ConcaveShape(com.bulletphysics.collision.shapes.ConcaveShape) StaticPlaneShape(com.bulletphysics.collision.shapes.StaticPlaneShape) CompoundShape(com.bulletphysics.collision.shapes.CompoundShape) Transform(com.bulletphysics.linearmath.Transform) IntArrayList(com.bulletphysics.util.IntArrayList) Stack(com.bulletphysics.util.Stack)

Example 3 with IntArrayList

use of com.bulletphysics.util.IntArrayList in project bdx by GoranM.

the class HullLibrary method createConvexHull.

/**
	 * Converts point cloud to polygonal representation.
	 * 
	 * @param desc   describes the input request
	 * @param result contains the result
	 * @return whether conversion was successful
	 */
public boolean createConvexHull(HullDesc desc, HullResult result) {
    boolean ret = false;
    PHullResult hr = new PHullResult();
    int vcount = desc.vcount;
    if (vcount < 8)
        vcount = 8;
    ObjectArrayList<Vector3f> vertexSource = new ObjectArrayList<Vector3f>();
    MiscUtil.resize(vertexSource, vcount, Suppliers.NEW_VECTOR3F_SUPPLIER);
    Stack stack = Stack.enter();
    Vector3f scale = stack.allocVector3f();
    int[] ovcount = new int[1];
    // normalize point cloud, remove duplicates!
    boolean ok = cleanupVertices(desc.vcount, desc.vertices, desc.vertexStride, ovcount, vertexSource, desc.normalEpsilon, scale);
    if (ok) {
        //		if ( 1 ) // scale vertices back to their original size.
        {
            for (int i = 0; i < ovcount[0]; i++) {
                Vector3f v = vertexSource.getQuick(i);
                VectorUtil.mul(v, v, scale);
            }
        }
        ok = computeHull(ovcount[0], vertexSource, hr, desc.maxVertices);
        if (ok) {
            // re-index triangle mesh so it refers to only used vertices, rebuild a new vertex table.
            ObjectArrayList<Vector3f> vertexScratch = new ObjectArrayList<Vector3f>();
            MiscUtil.resize(vertexScratch, hr.vcount, Suppliers.NEW_VECTOR3F_SUPPLIER);
            bringOutYourDead(hr.vertices, hr.vcount, vertexScratch, ovcount, hr.indices, hr.indexCount);
            ret = true;
            if (desc.hasHullFlag(HullFlags.TRIANGLES)) {
                // if he wants the results as triangle!
                result.polygons = false;
                result.numOutputVertices = ovcount[0];
                MiscUtil.resize(result.outputVertices, ovcount[0], Suppliers.NEW_VECTOR3F_SUPPLIER);
                result.numFaces = hr.faceCount;
                result.numIndices = hr.indexCount;
                MiscUtil.resize(result.indices, hr.indexCount, 0);
                for (int i = 0; i < ovcount[0]; i++) {
                    result.outputVertices.getQuick(i).set(vertexScratch.getQuick(i));
                }
                if (desc.hasHullFlag(HullFlags.REVERSE_ORDER)) {
                    IntArrayList source_ptr = hr.indices;
                    int source_idx = 0;
                    IntArrayList dest_ptr = result.indices;
                    int dest_idx = 0;
                    for (int i = 0; i < hr.faceCount; i++) {
                        dest_ptr.set(dest_idx + 0, source_ptr.get(source_idx + 2));
                        dest_ptr.set(dest_idx + 1, source_ptr.get(source_idx + 1));
                        dest_ptr.set(dest_idx + 2, source_ptr.get(source_idx + 0));
                        dest_idx += 3;
                        source_idx += 3;
                    }
                } else {
                    for (int i = 0; i < hr.indexCount; i++) {
                        result.indices.set(i, hr.indices.get(i));
                    }
                }
            } else {
                result.polygons = true;
                result.numOutputVertices = ovcount[0];
                MiscUtil.resize(result.outputVertices, ovcount[0], Suppliers.NEW_VECTOR3F_SUPPLIER);
                result.numFaces = hr.faceCount;
                result.numIndices = hr.indexCount + hr.faceCount;
                MiscUtil.resize(result.indices, result.numIndices, 0);
                for (int i = 0; i < ovcount[0]; i++) {
                    result.outputVertices.getQuick(i).set(vertexScratch.getQuick(i));
                }
                //				if ( 1 )
                {
                    IntArrayList source_ptr = hr.indices;
                    int source_idx = 0;
                    IntArrayList dest_ptr = result.indices;
                    int dest_idx = 0;
                    for (int i = 0; i < hr.faceCount; i++) {
                        dest_ptr.set(dest_idx + 0, 3);
                        if (desc.hasHullFlag(HullFlags.REVERSE_ORDER)) {
                            dest_ptr.set(dest_idx + 1, source_ptr.get(source_idx + 2));
                            dest_ptr.set(dest_idx + 2, source_ptr.get(source_idx + 1));
                            dest_ptr.set(dest_idx + 3, source_ptr.get(source_idx + 0));
                        } else {
                            dest_ptr.set(dest_idx + 1, source_ptr.get(source_idx + 0));
                            dest_ptr.set(dest_idx + 2, source_ptr.get(source_idx + 1));
                            dest_ptr.set(dest_idx + 3, source_ptr.get(source_idx + 2));
                        }
                        dest_idx += 4;
                        source_idx += 3;
                    }
                }
            }
            releaseHull(hr);
        }
    }
    stack.leave();
    return ret;
}
Also used : ObjectArrayList(com.bulletphysics.util.ObjectArrayList) Vector3f(javax.vecmath.Vector3f) IntArrayList(com.bulletphysics.util.IntArrayList) Stack(com.bulletphysics.util.Stack)

Example 4 with IntArrayList

use of com.bulletphysics.util.IntArrayList in project bdx by GoranM.

the class HullLibrary method bringOutYourDead.

//private ConvexH test_cube();
//BringOutYourDead (John Ratcliff): When you create a convex hull you hand it a large input set of vertices forming a 'point cloud'. 
//After the hull is generated it give you back a set of polygon faces which index the *original* point cloud.
//The thing is, often times, there are many 'dead vertices' in the point cloud that are on longer referenced by the hull.
//The routine 'BringOutYourDead' find only the referenced vertices, copies them to an new buffer, and re-indexes the hull so that it is a minimal representation.
private void bringOutYourDead(ObjectArrayList<Vector3f> verts, int vcount, ObjectArrayList<Vector3f> overts, int[] ocount, IntArrayList indices, int indexcount) {
    IntArrayList tmpIndices = new IntArrayList();
    for (int i = 0; i < vertexIndexMapping.size(); i++) {
        tmpIndices.add(vertexIndexMapping.size());
    }
    IntArrayList usedIndices = new IntArrayList();
    MiscUtil.resize(usedIndices, vcount, 0);
    /*
		JAVA NOTE: redudant
		for (int i=0; i<vcount; i++) {
		usedIndices.set(i, 0);
		}
		*/
    ocount[0] = 0;
    for (int i = 0; i < indexcount; i++) {
        // original array index
        int v = indices.get(i);
        assert (v >= 0 && v < vcount);
        if (usedIndices.get(v) != 0) {
            // if already remapped
            // index to new array
            indices.set(i, usedIndices.get(v) - 1);
        } else {
            // new index mapping
            indices.set(i, ocount[0]);
            // copy old vert to new vert array
            overts.getQuick(ocount[0]).set(verts.getQuick(v));
            for (int k = 0; k < vertexIndexMapping.size(); k++) {
                if (tmpIndices.get(k) == v) {
                    vertexIndexMapping.set(k, ocount[0]);
                }
            }
            // increment output vert count
            ocount[0]++;
            assert (ocount[0] >= 0 && ocount[0] <= vcount);
            // assign new index remapping
            usedIndices.set(v, ocount[0]);
        }
    }
}
Also used : IntArrayList(com.bulletphysics.util.IntArrayList)

Example 5 with IntArrayList

use of com.bulletphysics.util.IntArrayList in project bdx by GoranM.

the class HullLibrary method calchullgen.

private int calchullgen(ObjectArrayList<Vector3f> verts, int verts_count, int vlimit) {
    if (verts_count < 4)
        return 0;
    Stack stack = Stack.enter();
    Vector3f tmp = stack.allocVector3f();
    Vector3f tmp1 = stack.allocVector3f();
    Vector3f tmp2 = stack.allocVector3f();
    if (vlimit == 0) {
        vlimit = 1000000000;
    }
    //int j;
    Vector3f bmin = stack.alloc((Vector3f) verts.getQuick(0));
    Vector3f bmax = stack.alloc((Vector3f) verts.getQuick(0));
    IntArrayList isextreme = new IntArrayList();
    //isextreme.reserve(verts_count);
    IntArrayList allow = new IntArrayList();
    for (int j = 0; j < verts_count; j++) {
        allow.add(1);
        isextreme.add(0);
        VectorUtil.setMin(bmin, verts.getQuick(j));
        VectorUtil.setMax(bmax, verts.getQuick(j));
    }
    tmp.sub(bmax, bmin);
    float epsilon = tmp.length() * 0.001f;
    assert (epsilon != 0f);
    Int4 p = findSimplex(verts, verts_count, allow, new Int4());
    if (p.x == -1) {
        stack.leave();
        // simplex failed
        return 0;
    // a valid interior point
    }
    Vector3f center = stack.allocVector3f();
    VectorUtil.add(center, verts.getQuick(p.getCoord(0)), verts.getQuick(p.getCoord(1)), verts.getQuick(p.getCoord(2)), verts.getQuick(p.getCoord(3)));
    center.scale(1f / 4f);
    Tri t0 = allocateTriangle(p.getCoord(2), p.getCoord(3), p.getCoord(1));
    t0.n.set(2, 3, 1);
    Tri t1 = allocateTriangle(p.getCoord(3), p.getCoord(2), p.getCoord(0));
    t1.n.set(3, 2, 0);
    Tri t2 = allocateTriangle(p.getCoord(0), p.getCoord(1), p.getCoord(3));
    t2.n.set(0, 1, 3);
    Tri t3 = allocateTriangle(p.getCoord(1), p.getCoord(0), p.getCoord(2));
    t3.n.set(1, 0, 2);
    isextreme.set(p.getCoord(0), 1);
    isextreme.set(p.getCoord(1), 1);
    isextreme.set(p.getCoord(2), 1);
    isextreme.set(p.getCoord(3), 1);
    checkit(t0);
    checkit(t1);
    checkit(t2);
    checkit(t3);
    Vector3f n = stack.allocVector3f();
    for (int j = 0; j < tris.size(); j++) {
        Tri t = tris.getQuick(j);
        assert (t != null);
        assert (t.vmax < 0);
        triNormal(verts.getQuick(t.getCoord(0)), verts.getQuick(t.getCoord(1)), verts.getQuick(t.getCoord(2)), n);
        t.vmax = maxdirsterid(verts, verts_count, n, allow);
        tmp.sub(verts.getQuick(t.vmax), verts.getQuick(t.getCoord(0)));
        t.rise = n.dot(tmp);
    }
    Tri te;
    vlimit -= 4;
    while (vlimit > 0 && ((te = extrudable(epsilon)) != null)) {
        Int3 ti = te;
        int v = te.vmax;
        assert (v != -1);
        // wtf we've already done this vertex
        assert (isextreme.get(v) == 0);
        isextreme.set(v, 1);
        //if(v==p0 || v==p1 || v==p2 || v==p3) continue; // done these already
        int j = tris.size();
        while ((j--) != 0) {
            if (tris.getQuick(j) == null) {
                continue;
            }
            Int3 t = tris.getQuick(j);
            if (above(verts, t, verts.getQuick(v), 0.01f * epsilon)) {
                extrude(tris.getQuick(j), v);
            }
        }
        // now check for those degenerate cases where we have a flipped triangle or a really skinny triangle
        j = tris.size();
        while ((j--) != 0) {
            if (tris.getQuick(j) == null) {
                continue;
            }
            if (!hasvert(tris.getQuick(j), v)) {
                break;
            }
            Int3 nt = tris.getQuick(j);
            tmp1.sub(verts.getQuick(nt.getCoord(1)), verts.getQuick(nt.getCoord(0)));
            tmp2.sub(verts.getQuick(nt.getCoord(2)), verts.getQuick(nt.getCoord(1)));
            tmp.cross(tmp1, tmp2);
            if (above(verts, nt, center, 0.01f * epsilon) || tmp.length() < epsilon * epsilon * 0.1f) {
                Tri nb = tris.getQuick(tris.getQuick(j).n.getCoord(0));
                assert (nb != null);
                assert (!hasvert(nb, v));
                assert (nb.id < j);
                extrude(nb, v);
                j = tris.size();
            }
        }
        j = tris.size();
        while ((j--) != 0) {
            Tri t = tris.getQuick(j);
            if (t == null) {
                continue;
            }
            if (t.vmax >= 0) {
                break;
            }
            triNormal(verts.getQuick(t.getCoord(0)), verts.getQuick(t.getCoord(1)), verts.getQuick(t.getCoord(2)), n);
            t.vmax = maxdirsterid(verts, verts_count, n, allow);
            if (isextreme.get(t.vmax) != 0) {
                // already done that vertex - algorithm needs to be able to terminate.
                t.vmax = -1;
            } else {
                tmp.sub(verts.getQuick(t.vmax), verts.getQuick(t.getCoord(0)));
                t.rise = n.dot(tmp);
            }
        }
        vlimit--;
    }
    stack.leave();
    return 1;
}
Also used : Vector3f(javax.vecmath.Vector3f) IntArrayList(com.bulletphysics.util.IntArrayList) Stack(com.bulletphysics.util.Stack)

Aggregations

IntArrayList (com.bulletphysics.util.IntArrayList)7 Stack (com.bulletphysics.util.Stack)3 Vector3f (javax.vecmath.Vector3f)3 ObjectArrayList (com.bulletphysics.util.ObjectArrayList)2 Node (com.bulletphysics.collision.broadphase.Dbvt.Node)1 CollisionShape (com.bulletphysics.collision.shapes.CollisionShape)1 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)1 ConcaveShape (com.bulletphysics.collision.shapes.ConcaveShape)1 ShapeHull (com.bulletphysics.collision.shapes.ShapeHull)1 StaticPlaneShape (com.bulletphysics.collision.shapes.StaticPlaneShape)1 Transform (com.bulletphysics.linearmath.Transform)1 FloatBuffer (java.nio.FloatBuffer)1