Search in sources :

Example 6 with ObjectArrayList

use of com.bulletphysics.util.ObjectArrayList 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 7 with ObjectArrayList

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

the class Dbvt method collideRAY.

public static void collideRAY(Node root, Vector3f origin, Vector3f direction, ICollide policy) {
    //DBVT_CHECKTYPE
    if (root != null) {
        Stack st = Stack.enter();
        Vector3f normal = st.allocVector3f();
        normal.normalize(direction);
        Vector3f invdir = st.allocVector3f();
        invdir.set(1f / normal.x, 1f / normal.y, 1f / normal.z);
        int[] signs = new int[] { direction.x < 0 ? 1 : 0, direction.y < 0 ? 1 : 0, direction.z < 0 ? 1 : 0 };
        ObjectArrayList<Node> stack = new ObjectArrayList<Node>(SIMPLE_STACKSIZE);
        stack.add(root);
        do {
            Node node = stack.remove(stack.size() - 1);
            if (DbvtAabbMm.Intersect(node.volume, origin, invdir, signs)) {
                if (node.isinternal()) {
                    stack.add(node.childs[0]);
                    stack.add(node.childs[1]);
                } else {
                    policy.Process(node);
                }
            }
        } while (stack.size() != 0);
        st.leave();
    }
}
Also used : ObjectArrayList(com.bulletphysics.util.ObjectArrayList) Vector3f(javax.vecmath.Vector3f) Node(com.bulletphysics.collision.broadphase.Dbvt.Node) Stack(com.bulletphysics.util.Stack)

Example 8 with ObjectArrayList

use of com.bulletphysics.util.ObjectArrayList in project jmonkeyengine by jMonkeyEngine.

the class HullCollisionShape method createShape.

protected void createShape(float[] points) {
    ObjectArrayList<Vector3f> pointList = new ObjectArrayList<Vector3f>();
    for (int i = 0; i < points.length; i += 3) {
        pointList.add(new Vector3f(points[i], points[i + 1], points[i + 2]));
    }
    cShape = new ConvexHullShape(pointList);
    cShape.setLocalScaling(Converter.convert(getScale()));
    cShape.setMargin(margin);
}
Also used : ObjectArrayList(com.bulletphysics.util.ObjectArrayList) Vector3f(javax.vecmath.Vector3f) ConvexHullShape(com.bulletphysics.collision.shapes.ConvexHullShape)

Aggregations

ObjectArrayList (com.bulletphysics.util.ObjectArrayList)8 Node (com.bulletphysics.collision.broadphase.Dbvt.Node)5 Vector3f (javax.vecmath.Vector3f)5 Stack (com.bulletphysics.util.Stack)4 IntArrayList (com.bulletphysics.util.IntArrayList)2 ConvexHullShape (com.bulletphysics.collision.shapes.ConvexHullShape)1