Search in sources :

Example 66 with Box

use of com.jme3.scene.shape.Box in project jmonkeyengine by jMonkeyEngine.

the class BoundingBox method intersects.

/**
     * determines if this bounding box intersects with a given oriented bounding
     * box.
     * 
     * @see com.jme.bounding.BoundingVolume#intersectsOrientedBoundingBox(com.jme.bounding.OrientedBoundingBox)
     */
//    public boolean intersectsOrientedBoundingBox(OrientedBoundingBox obb) {
//        return obb.intersectsBoundingBox(this);
//    }
/**
     * determines if this bounding box intersects with a given ray object. If an
     * intersection has occurred, true is returned, otherwise false is returned.
     * 
     * @see BoundingVolume#intersects(com.jme3.math.Ray) 
     */
public boolean intersects(Ray ray) {
    assert Vector3f.isValidVector(center);
    float rhs;
    TempVars vars = TempVars.get();
    Vector3f diff = ray.origin.subtract(getCenter(vars.vect2), vars.vect1);
    final float[] fWdU = vars.fWdU;
    final float[] fAWdU = vars.fAWdU;
    final float[] fDdU = vars.fDdU;
    final float[] fADdU = vars.fADdU;
    final float[] fAWxDdU = vars.fAWxDdU;
    fWdU[0] = ray.getDirection().dot(Vector3f.UNIT_X);
    fAWdU[0] = FastMath.abs(fWdU[0]);
    fDdU[0] = diff.dot(Vector3f.UNIT_X);
    fADdU[0] = FastMath.abs(fDdU[0]);
    if (fADdU[0] > xExtent && fDdU[0] * fWdU[0] >= 0.0) {
        vars.release();
        return false;
    }
    fWdU[1] = ray.getDirection().dot(Vector3f.UNIT_Y);
    fAWdU[1] = FastMath.abs(fWdU[1]);
    fDdU[1] = diff.dot(Vector3f.UNIT_Y);
    fADdU[1] = FastMath.abs(fDdU[1]);
    if (fADdU[1] > yExtent && fDdU[1] * fWdU[1] >= 0.0) {
        vars.release();
        return false;
    }
    fWdU[2] = ray.getDirection().dot(Vector3f.UNIT_Z);
    fAWdU[2] = FastMath.abs(fWdU[2]);
    fDdU[2] = diff.dot(Vector3f.UNIT_Z);
    fADdU[2] = FastMath.abs(fDdU[2]);
    if (fADdU[2] > zExtent && fDdU[2] * fWdU[2] >= 0.0) {
        vars.release();
        return false;
    }
    Vector3f wCrossD = ray.getDirection().cross(diff, vars.vect2);
    fAWxDdU[0] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_X));
    rhs = yExtent * fAWdU[2] + zExtent * fAWdU[1];
    if (fAWxDdU[0] > rhs) {
        vars.release();
        return false;
    }
    fAWxDdU[1] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Y));
    rhs = xExtent * fAWdU[2] + zExtent * fAWdU[0];
    if (fAWxDdU[1] > rhs) {
        vars.release();
        return false;
    }
    fAWxDdU[2] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Z));
    rhs = xExtent * fAWdU[1] + yExtent * fAWdU[0];
    if (fAWxDdU[2] > rhs) {
        vars.release();
        return false;
    }
    vars.release();
    return true;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 67 with Box

use of com.jme3.scene.shape.Box in project jmonkeyengine by jMonkeyEngine.

the class BoundingBox method computeFromTris.

/**
     * <code>computeFromTris</code> creates a new Bounding Box from a given
     * set of triangles. It is used in OBBTree calculations.
     * 
     * @param tris
     * @param start
     * @param end
     */
public void computeFromTris(Triangle[] tris, int start, int end) {
    if (end - start <= 0) {
        return;
    }
    TempVars vars = TempVars.get();
    Vector3f min = vars.vect1.set(new Vector3f(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
    Vector3f max = vars.vect2.set(new Vector3f(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));
    Vector3f point;
    for (int i = start; i < end; i++) {
        point = tris[i].get(0);
        checkMinMax(min, max, point);
        point = tris[i].get(1);
        checkMinMax(min, max, point);
        point = tris[i].get(2);
        checkMinMax(min, max, point);
    }
    center.set(min.addLocal(max));
    center.multLocal(0.5f);
    xExtent = max.x - center.x;
    yExtent = max.y - center.y;
    zExtent = max.z - center.z;
    vars.release();
}
Also used : TempVars(com.jme3.util.TempVars)

Example 68 with Box

use of com.jme3.scene.shape.Box in project jmonkeyengine by jMonkeyEngine.

the class BoundingBox method transform.

public BoundingVolume transform(Matrix4f trans, BoundingVolume store) {
    BoundingBox box;
    if (store == null || store.getType() != Type.AABB) {
        box = new BoundingBox();
    } else {
        box = (BoundingBox) store;
    }
    TempVars vars = TempVars.get();
    float w = trans.multProj(center, box.center);
    box.center.divideLocal(w);
    Matrix3f transMatrix = vars.tempMat3;
    trans.toRotationMatrix(transMatrix);
    // Make the rotation matrix all positive to get the maximum x/y/z extent
    transMatrix.absoluteLocal();
    vars.vect1.set(xExtent, yExtent, zExtent);
    transMatrix.mult(vars.vect1, vars.vect1);
    // Assign the biggest rotations after scales.
    box.xExtent = FastMath.abs(vars.vect1.getX());
    box.yExtent = FastMath.abs(vars.vect1.getY());
    box.zExtent = FastMath.abs(vars.vect1.getZ());
    vars.release();
    return box;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 69 with Box

use of com.jme3.scene.shape.Box in project jmonkeyengine by jMonkeyEngine.

the class BoundingBox method containAABB.

/**
     * <code>containAABB</code> creates a minimum-volume axis-aligned bounding
     * box of the points, then selects the smallest enclosing sphere of the box
     * with the sphere centered at the boxes center.
     * 
     * @param points
     *            the list of points.
     */
public void containAABB(FloatBuffer points) {
    if (points == null) {
        return;
    }
    points.rewind();
    if (// we need at least a 3 float vector
    points.remaining() <= 2) {
        return;
    }
    TempVars vars = TempVars.get();
    float[] tmpArray = vars.skinPositions;
    float minX = Float.POSITIVE_INFINITY, minY = Float.POSITIVE_INFINITY, minZ = Float.POSITIVE_INFINITY;
    float maxX = Float.NEGATIVE_INFINITY, maxY = Float.NEGATIVE_INFINITY, maxZ = Float.NEGATIVE_INFINITY;
    int iterations = (int) FastMath.ceil(points.limit() / ((float) tmpArray.length));
    for (int i = iterations - 1; i >= 0; i--) {
        int bufLength = Math.min(tmpArray.length, points.remaining());
        points.get(tmpArray, 0, bufLength);
        for (int j = 0; j < bufLength; j += 3) {
            vars.vect1.x = tmpArray[j];
            vars.vect1.y = tmpArray[j + 1];
            vars.vect1.z = tmpArray[j + 2];
            if (vars.vect1.x < minX) {
                minX = vars.vect1.x;
            }
            if (vars.vect1.x > maxX) {
                maxX = vars.vect1.x;
            }
            if (vars.vect1.y < minY) {
                minY = vars.vect1.y;
            }
            if (vars.vect1.y > maxY) {
                maxY = vars.vect1.y;
            }
            if (vars.vect1.z < minZ) {
                minZ = vars.vect1.z;
            }
            if (vars.vect1.z > maxZ) {
                maxZ = vars.vect1.z;
            }
        }
    }
    vars.release();
    center.set(minX + maxX, minY + maxY, minZ + maxZ);
    center.multLocal(0.5f);
    xExtent = maxX - center.x;
    yExtent = maxY - center.y;
    zExtent = maxZ - center.z;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 70 with Box

use of com.jme3.scene.shape.Box in project jmonkeyengine by jMonkeyEngine.

the class MikktspaceTangentGenerator method generateSharedVerticesIndexList.

static void generateSharedVerticesIndexList(int[] piTriList_in_and_out, final MikkTSpaceContext mikkTSpace, final int iNrTrianglesIn) {
    // Generate bounding box
    TmpVert[] pTmpVert;
    Vector3f vMin = getPosition(mikkTSpace, 0);
    Vector3f vMax = vMin.clone();
    Vector3f vDim;
    float fMin, fMax;
    for (int i = 1; i < (iNrTrianglesIn * 3); i++) {
        final int index = piTriList_in_and_out[i];
        final Vector3f vP = getPosition(mikkTSpace, index);
        if (vMin.x > vP.x) {
            vMin.x = vP.x;
        } else if (vMax.x < vP.x) {
            vMax.x = vP.x;
        }
        if (vMin.y > vP.y) {
            vMin.y = vP.y;
        } else if (vMax.y < vP.y) {
            vMax.y = vP.y;
        }
        if (vMin.z > vP.z) {
            vMin.z = vP.z;
        } else if (vMax.z < vP.z) {
            vMax.z = vP.z;
        }
    }
    vDim = vMax.subtract(vMin);
    int iChannel = 0;
    fMin = vMin.x;
    fMax = vMax.x;
    if (vDim.y > vDim.x && vDim.y > vDim.z) {
        iChannel = 1;
        fMin = vMin.y;
        fMax = vMax.y;
    } else if (vDim.z > vDim.x) {
        iChannel = 2;
        fMin = vMin.z;
        fMax = vMax.z;
    }
    //TODO Nehon: this is really fishy... seems like a hashtable implementation with nasty array manipulation...
    int[] piHashTable = new int[iNrTrianglesIn * 3];
    int[] piHashCount = new int[CELLS];
    int[] piHashOffsets = new int[CELLS];
    int[] piHashCount2 = new int[CELLS];
    // count amount of elements in each cell unit
    for (int i = 0; i < (iNrTrianglesIn * 3); i++) {
        final int index = piTriList_in_and_out[i];
        final Vector3f vP = getPosition(mikkTSpace, index);
        final float fVal = iChannel == 0 ? vP.x : (iChannel == 1 ? vP.y : vP.z);
        final int iCell = findGridCell(fMin, fMax, fVal);
        ++piHashCount[iCell];
    }
    // evaluate start index of each cell.
    piHashOffsets[0] = 0;
    for (int k = 1; k < CELLS; k++) {
        piHashOffsets[k] = piHashOffsets[k - 1] + piHashCount[k - 1];
    }
    // insert vertices
    for (int i = 0; i < (iNrTrianglesIn * 3); i++) {
        final int index = piTriList_in_and_out[i];
        final Vector3f vP = getPosition(mikkTSpace, index);
        final float fVal = iChannel == 0 ? vP.x : (iChannel == 1 ? vP.y : vP.z);
        final int iCell = findGridCell(fMin, fMax, fVal);
        assert (piHashCount2[iCell] < piHashCount[iCell]);
        //    int * pTable = &piHashTable[piHashOffsets[iCell]];
        //    pTable[piHashCount2[iCell]] = i;  // vertex i has been inserted.
        // vertex i has been inserted.     
        piHashTable[piHashOffsets[iCell] + piHashCount2[iCell]] = i;
        ++piHashCount2[iCell];
    }
    for (int k = 0; k < CELLS; k++) {
        // verify the count
        assert (piHashCount2[k] == piHashCount[k]);
    }
    // find maximum amount of entries in any hash entry
    int iMaxCount = piHashCount[0];
    for (int k = 1; k < CELLS; k++) {
        if (iMaxCount < piHashCount[k]) {
            iMaxCount = piHashCount[k];
        }
    }
    pTmpVert = new TmpVert[iMaxCount];
    // complete the merge
    for (int k = 0; k < CELLS; k++) {
        // extract table of cell k and amount of entries in it
        // int * pTable = &piHashTable[piHashOffsets[k]];
        final int iEntries = piHashCount[k];
        if (iEntries < 2) {
            continue;
        }
        if (pTmpVert != null) {
            for (int e = 0; e < iEntries; e++) {
                int j = piHashTable[piHashOffsets[k] + e];
                final Vector3f vP = getPosition(mikkTSpace, piTriList_in_and_out[j]);
                pTmpVert[e] = new TmpVert();
                pTmpVert[e].vert[0] = vP.x;
                pTmpVert[e].vert[1] = vP.y;
                pTmpVert[e].vert[2] = vP.z;
                pTmpVert[e].index = j;
            }
            MergeVertsFast(piTriList_in_and_out, pTmpVert, mikkTSpace, 0, iEntries - 1);
        } else {
            //TODO Nehon: pTempVert is very unlikely to be null...maybe remove this...
            int[] pTable = Arrays.copyOfRange(piHashTable, piHashOffsets[k], piHashOffsets[k] + iEntries);
            MergeVertsSlow(piTriList_in_and_out, mikkTSpace, pTable, iEntries);
        }
    }
}
Also used : Vector3f(com.jme3.math.Vector3f)

Aggregations

Geometry (com.jme3.scene.Geometry)106 Box (com.jme3.scene.shape.Box)99 Material (com.jme3.material.Material)83 Vector3f (com.jme3.math.Vector3f)77 Node (com.jme3.scene.Node)31 DirectionalLight (com.jme3.light.DirectionalLight)27 Sphere (com.jme3.scene.shape.Sphere)23 Quaternion (com.jme3.math.Quaternion)20 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)19 Spatial (com.jme3.scene.Spatial)19 BoundingBox (com.jme3.bounding.BoundingBox)17 Vector2f (com.jme3.math.Vector2f)15 Texture (com.jme3.texture.Texture)15 AmbientLight (com.jme3.light.AmbientLight)13 TempVars (com.jme3.util.TempVars)12 BulletAppState (com.jme3.bullet.BulletAppState)11 KeyTrigger (com.jme3.input.controls.KeyTrigger)11 FilterPostProcessor (com.jme3.post.FilterPostProcessor)11 BoundingSphere (com.jme3.bounding.BoundingSphere)8 BoxCollisionShape (com.jme3.bullet.collision.shapes.BoxCollisionShape)8