Java Code Examples for com.jme3.bounding.BoundingBox#getXExtent()

The following examples show how to use com.jme3.bounding.BoundingBox#getXExtent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBatchNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void updateBoindPoints(Vector3f[] points) {
    BoundingBox bb = (BoundingBox) batch.getWorldBound();
    float xe = bb.getXExtent();
    float ye = bb.getYExtent();
    float ze = bb.getZExtent();
    float x = bb.getCenter().x;
    float y = bb.getCenter().y;
    float z = bb.getCenter().z;

    points[0].set(new Vector3f(x - xe, y - ye, z - ze));
    points[1].set(new Vector3f(x - xe, y + ye, z - ze));
    points[2].set(new Vector3f(x + xe, y + ye, z - ze));
    points[3].set(new Vector3f(x + xe, y - ye, z - ze));

    points[4].set(new Vector3f(x + xe, y - ye, z + ze));
    points[5].set(new Vector3f(x - xe, y - ye, z + ze));
    points[6].set(new Vector3f(x - xe, y + ye, z + ze));
    points[7].set(new Vector3f(x + xe, y + ye, z + ze));
}
 
Example 2
Source File: TerrainQuad.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * used by attachBoundChildren()
 */
private void attachBoundingBox(BoundingBox bb, Node parent) {
    WireBox wb = new WireBox(bb.getXExtent(), bb.getYExtent(), bb.getZExtent());
    Geometry g = new Geometry();
    g.setMesh(wb);
    g.setLocalTranslation(bb.getCenter());
    parent.attachChild(g);
}
 
Example 3
Source File: WireBox.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create a geometry suitable for visualizing the specified bounding box.
 *
 * @param bbox the bounding box (not null)
 * @return a new Geometry instance in world space
 */
public static Geometry makeGeometry(BoundingBox bbox) {
    float xExtent = bbox.getXExtent();
    float yExtent = bbox.getYExtent();
    float zExtent = bbox.getZExtent();
    WireBox mesh = new WireBox(xExtent, yExtent, zExtent);
    Geometry result = new Geometry("bounding box", mesh);

    Vector3f center = bbox.getCenter();
    result.setLocalTranslation(center);

    return result;
}
 
Example 4
Source File: AreaUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static float calcScreenArea(BoundingBox bound, float distance, float screenWidth) {
    // Calc as if we are a BoundingSphere for now...
    float radiusSquare = bound.getXExtent() * bound.getXExtent()
                       + bound.getYExtent() * bound.getYExtent()
                       + bound.getZExtent() * bound.getZExtent();
    return ((radiusSquare * screenWidth * screenWidth) / (distance * distance * 4)) * FastMath.PI;
}
 
Example 5
Source File: TerrainQuad.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * used by attachBoundChildren()
 */
private void attachBoundingBox(BoundingBox bb, Node parent) {
    WireBox wb = new WireBox(bb.getXExtent(), bb.getYExtent(), bb.getZExtent());
    Geometry g = new Geometry();
    g.setMesh(wb);
    g.setLocalTranslation(bb.getCenter());
    parent.attachChild(g);
}
 
Example 6
Source File: PMDMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public BoundingVolume getBound() {
    BoundingBox bb = (BoundingBox)super.getBound();
    BoundingBox bb2 = new BoundingBox(bb.getCenter(), bb.getXExtent()*2, bb.getYExtent()*2,
            bb.getZExtent()*2);
    BoundingBox bb3 = new BoundingBox(bb.getCenter().ZERO,5,5,5);
    return bound;
}
 
Example 7
Source File: FastOctnode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static void findChildBound(BoundingBox bbox, int side){
    float extent = bbox.getXExtent() * 0.5f;
    bbox.getCenter().set(bbox.getCenter().x + extent * Octnode.extentMult[side].x,
                         bbox.getCenter().y + extent * Octnode.extentMult[side].y,
                         bbox.getCenter().z + extent * Octnode.extentMult[side].z);
    bbox.setXExtent(extent);
    bbox.setYExtent(extent);
    bbox.setZExtent(extent);
}
 
Example 8
Source File: UVProjectionGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Flat projection for 2D textures.
 * 
 * @param mesh
 *            mesh that is to be projected
 * @param bb
 *            the bounding box for projecting
 * @return UV coordinates after the projection
 */
public static float[] flatProjection(float[] positions, BoundingBox bb) {
    Vector3f min = bb.getMin(null);
    float[] ext = new float[] { bb.getXExtent() * 2.0f, bb.getZExtent() * 2.0f };
    float[] uvCoordinates = new float[positions.length / 3 * 2];
    for (int i = 0, j = 0; i < positions.length; i += 3, j += 2) {
        uvCoordinates[j] = (positions[i] - min.x) / ext[0];
        // skip the Y-coordinate
        uvCoordinates[j + 1] = (positions[i + 2] - min.z) / ext[1];
    }
    return uvCoordinates;
}
 
Example 9
Source File: TriangulatedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This method converts the given point into 3D UV coordinates.
 * 
 * @param boundingBox
 *            the bounding box of the mesh
 * @param point
 *            the point to be transformed
 * @param uvs
 *            the result UV coordinates
 */
private void toTextureUV(BoundingBox boundingBox, Vector3f point, float[] uvs) {
    uvs[0] = (point.x - boundingBox.getCenter().x) / (boundingBox.getXExtent() == 0 ? 1 : boundingBox.getXExtent());
    uvs[1] = (point.y - boundingBox.getCenter().y) / (boundingBox.getYExtent() == 0 ? 1 : boundingBox.getYExtent());
    uvs[2] = (point.z - boundingBox.getCenter().z) / (boundingBox.getZExtent() == 0 ? 1 : boundingBox.getZExtent());
    // UVS cannot go outside <0, 1> range, but since we are generating texture for triangle envelope it might happen that
    // some points of the envelope will exceet the bounding box of the mesh thus generating uvs outside the range
    for (int i = 0; i < 3; ++i) {
        uvs[i] = FastMath.clamp(uvs[i], 0, 1);
    }
}
 
Example 10
Source File: AreaUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static float calcScreenArea(BoundingBox bound, float distance, float screenWidth) {
    // Calc as if we are a BoundingSphere for now...
    float radiusSquare = bound.getXExtent() * bound.getXExtent()
                       + bound.getYExtent() * bound.getYExtent()
                       + bound.getZExtent() * bound.getZExtent();
    return ((radiusSquare * screenWidth * screenWidth) / (distance * distance * 4)) * FastMath.PI;
}
 
Example 11
Source File: BIHNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public final int intersectWhere(Collidable col,
            BoundingBox box,
            Matrix4f worldMatrix,
            BIHTree tree,
            CollisionResults results) {

        TempVars vars = TempVars.get();
        ArrayList<BIHStackData> stack = vars.bihStack;
        stack.clear();

        float[] minExts = {box.getCenter().x - box.getXExtent(),
            box.getCenter().y - box.getYExtent(),
            box.getCenter().z - box.getZExtent()};

        float[] maxExts = {box.getCenter().x + box.getXExtent(),
            box.getCenter().y + box.getYExtent(),
            box.getCenter().z + box.getZExtent()};

        stack.add(new BIHStackData(this, 0, 0));

        Triangle t = new Triangle();
        int cols = 0;

        stackloop:
        while (stack.size() > 0) {
            BIHNode node = stack.remove(stack.size() - 1).node;

            while (node.axis != 3) {
                int a = node.axis;

                float maxExt = maxExts[a];
                float minExt = minExts[a];

                if (node.leftPlane < node.rightPlane) {
                    // means there's a gap in the middle
                    // if the box is in that gap, we stop there
                    if (minExt > node.leftPlane
                            && maxExt < node.rightPlane) {
                        continue stackloop;
                    }
                }

                if (maxExt < node.rightPlane) {
                    node = node.left;
                } else if (minExt > node.leftPlane) {
                    node = node.right;
                } else {
                    stack.add(new BIHStackData(node.right, 0, 0));
                    node = node.left;
                }
//                if (maxExt < node.leftPlane
//                 && maxExt < node.rightPlane){
//                    node = node.left;
//                }else if (minExt > node.leftPlane
//                       && minExt > node.rightPlane){
//                    node = node.right;
//                }else{

//                }
            }

            for (int i = node.leftIndex; i <= node.rightIndex; i++) {
                tree.getTriangle(i, t.get1(), t.get2(), t.get3());
                if (worldMatrix != null) {
                    worldMatrix.mult(t.get1(), t.get1());
                    worldMatrix.mult(t.get2(), t.get2());
                    worldMatrix.mult(t.get3(), t.get3());
                }

                int added = col.collideWith(t, results);

                if (added > 0) {
                    int index = tree.getTriangleIndex(i);
                    int start = results.size() - added;

                    for (int j = start; j < results.size(); j++) {
                        CollisionResult cr = results.getCollisionDirect(j);
                        cr.setTriangleIndex(index);
                    }

                    cols += added;
                }
            }
        }
        vars.release();
        return cols;
    }
 
Example 12
Source File: UVProjectionGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Cube projection for 2D textures.
 * 
 * @param positions
 *            points to be projected
 * @param bb
 *            the bounding box for projecting
 * @return UV coordinates after the projection
 */
public static float[] cubeProjection(float[] positions, BoundingBox bb) {
    Triangle triangle = new Triangle();
    Vector3f x = new Vector3f(1, 0, 0);
    Vector3f y = new Vector3f(0, 1, 0);
    Vector3f z = new Vector3f(0, 0, 1);
    Vector3f min = bb.getMin(null);
    float[] ext = new float[] { bb.getXExtent() * 2.0f, bb.getYExtent() * 2.0f, bb.getZExtent() * 2.0f };

    float[] uvCoordinates = new float[positions.length / 3 * 2];
    float borderAngle = (float) Math.sqrt(2.0f) / 2.0f;
    for (int i = 0, pointIndex = 0; i < positions.length; i += 9) {
        triangle.set(0, positions[i], positions[i + 1], positions[i + 2]);
        triangle.set(1, positions[i + 3], positions[i + 4], positions[i + 5]);
        triangle.set(2, positions[i + 6], positions[i + 7], positions[i + 8]);
        Vector3f n = triangle.getNormal();
        float dotNX = Math.abs(n.dot(x));
        float dorNY = Math.abs(n.dot(y));
        float dotNZ = Math.abs(n.dot(z));
        if (dotNX > borderAngle) {
            if (dotNZ < borderAngle) {// discard X-coordinate
                uvCoordinates[pointIndex++] = (triangle.get1().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get1().z - min.z) / ext[2];
                uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get2().z - min.z) / ext[2];
                uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get3().z - min.z) / ext[2];
            } else {// discard Z-coordinate
                uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get1().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
            }
        } else {
            if (dorNY > borderAngle) {// discard Y-coordinate
                uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get1().z - min.z) / ext[2];
                uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get2().z - min.z) / ext[2];
                uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get3().z - min.z) / ext[2];
            } else {// discard Z-coordinate
                uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get1().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
            }
        }
        triangle.setNormal(null);// clear the previous normal vector
    }
    return uvCoordinates;
}
 
Example 13
Source File: UVCoordinatesGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Generates a UV coordinates for 3D texture.
 * 
 * @param mesh
 *            the mesh we generate UV's for
 * @param texco
 *            UV coordinates type
 * @param coordinatesSwappingIndexes
 *            coordinates swapping indexes
 * @param geometries
 *            the geometris the given mesh belongs to (required to compute
 *            bounding box)
 * @return UV coordinates for the given mesh
 */
public static List<Vector3f> generateUVCoordinatesFor3DTexture(Mesh mesh, UVCoordinatesType texco, int[] coordinatesSwappingIndexes, List<Geometry> geometries) {
    List<Vector3f> result = new ArrayList<Vector3f>();
    BoundingBox bb = UVCoordinatesGenerator.getBoundingBox(geometries);
    float[] inputData = null;// positions, normals, reflection vectors, etc.

    switch (texco) {
        case TEXCO_ORCO:
            inputData = BufferUtils.getFloatArray(mesh.getFloatBuffer(VertexBuffer.Type.Position));
            break;
        case TEXCO_UV:
            Vector2f[] data = new Vector2f[] { new Vector2f(0, 1), new Vector2f(0, 0), new Vector2f(1, 0) };
            for (int i = 0; i < mesh.getVertexCount(); ++i) {
                Vector2f uv = data[i % 3];
                result.add(new Vector3f(uv.x, uv.y, 0));
            }
            break;
        case TEXCO_NORM:
            inputData = BufferUtils.getFloatArray(mesh.getFloatBuffer(VertexBuffer.Type.Normal));
            break;
        case TEXCO_REFL:
        case TEXCO_GLOB:
        case TEXCO_TANGENT:
        case TEXCO_STRESS:
        case TEXCO_LAVECTOR:
        case TEXCO_OBJECT:
        case TEXCO_OSA:
        case TEXCO_PARTICLE_OR_STRAND:
        case TEXCO_SPEED:
        case TEXCO_STICKY:
        case TEXCO_VIEW:
        case TEXCO_WINDOW:
            LOGGER.warning("Texture coordinates type not currently supported: " + texco);
            break;
        default:
            throw new IllegalStateException("Unknown texture coordinates value: " + texco);
    }

    if (inputData != null) {// make calculations
        Vector3f min = bb.getMin(null);
        float[] uvCoordsResults = new float[4];// used for coordinates swapping
        float[] ext = new float[] { bb.getXExtent() * 2, bb.getYExtent() * 2, bb.getZExtent() * 2 };
        for (int i = 0; i < ext.length; ++i) {
            if (ext[i] == 0) {
                ext[i] = 1;
            }
        }
        // now transform the coordinates so that they are in the range of
        // <0; 1>
        for (int i = 0; i < inputData.length; i += 3) {
            uvCoordsResults[1] = (inputData[i] - min.x) / ext[0];
            uvCoordsResults[2] = (inputData[i + 1] - min.y) / ext[1];
            uvCoordsResults[3] = (inputData[i + 2] - min.z) / ext[2];
            result.add(new Vector3f(uvCoordsResults[coordinatesSwappingIndexes[0]], uvCoordsResults[coordinatesSwappingIndexes[1]], uvCoordsResults[coordinatesSwappingIndexes[2]]));
        }
    }
    return result;
}
 
Example 14
Source File: TriangulatedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Constructor that creates an image element from the 3D texture
 * (generated texture). It computes a flat smallest rectangle that can
 * hold a (3D) triangle defined by the given UV coordinates. Then it
 * defines the image pixels for points in 3D space that define the
 * calculated rectangle.
 * 
 * @param faceIndex
 *            the face index this image refers to
 * @param boundingBox
 *            the bounding box of the mesh
 * @param texture
 *            the texture that allows to compute a pixel value in 3D
 *            space
 * @param uv
 *            the UV coordinates of the mesh
 * @param blenderContext
 *            the blender context
 */
public TriangleTextureElement(int faceIndex, BoundingBox boundingBox, GeneratedTexture texture, Vector3f[] uv, int[] uvIndices, BlenderContext blenderContext) {
    this.faceIndex = faceIndex;

    // compute the face vertices from the UV coordinates
    float width = boundingBox.getXExtent() * 2;
    float height = boundingBox.getYExtent() * 2;
    float depth = boundingBox.getZExtent() * 2;

    Vector3f min = boundingBox.getMin(null);
    Vector3f v1 = min.add(uv[uvIndices[0]].x * width, uv[uvIndices[0]].y * height, uv[uvIndices[0]].z * depth);
    Vector3f v2 = min.add(uv[uvIndices[1]].x * width, uv[uvIndices[1]].y * height, uv[uvIndices[1]].z * depth);
    Vector3f v3 = min.add(uv[uvIndices[2]].x * width, uv[uvIndices[2]].y * height, uv[uvIndices[2]].z * depth);

    // get the rectangle envelope for the triangle
    RectangleEnvelope envelope = this.getTriangleEnvelope(v1, v2, v3);

    // create the result image
    Format imageFormat = texture.getImage().getFormat();
    int imageWidth = (int) (envelope.width * blenderContext.getBlenderKey().getGeneratedTexturePPU());
    if (imageWidth == 0) {
        imageWidth = 1;
    }
    int imageHeight = (int) (envelope.height * blenderContext.getBlenderKey().getGeneratedTexturePPU());
    if (imageHeight == 0) {
        imageHeight = 1;
    }
    ByteBuffer data = BufferUtils.createByteBuffer(imageWidth * imageHeight * (imageFormat.getBitsPerPixel() >> 3));
    image = new Image(texture.getImage().getFormat(), imageWidth, imageHeight, data);

    // computing the pixels
    PixelInputOutput pixelWriter = PixelIOFactory.getPixelIO(imageFormat);
    TexturePixel pixel = new TexturePixel();
    float[] uvs = new float[3];
    Vector3f point = new Vector3f(envelope.min);
    Vector3f vecY = new Vector3f();
    Vector3f wDelta = new Vector3f(envelope.w).multLocal(1.0f / imageWidth);
    Vector3f hDelta = new Vector3f(envelope.h).multLocal(1.0f / imageHeight);
    for (int x = 0; x < imageWidth; ++x) {
        for (int y = 0; y < imageHeight; ++y) {
            this.toTextureUV(boundingBox, point, uvs);
            texture.getPixel(pixel, uvs[0], uvs[1], uvs[2]);
            pixelWriter.write(image, 0, pixel, x, y);
            point.addLocal(hDelta);
        }

        vecY.addLocal(wDelta);
        point.set(envelope.min).addLocal(vecY);
    }

    // preparing UV coordinates for the flatted texture
    this.uv = new Vector2f[3];
    this.uv[0] = new Vector2f(FastMath.clamp(v1.subtract(envelope.min).length(), 0, Float.MAX_VALUE) / envelope.height, 0);
    Vector3f heightDropPoint = v2.subtract(envelope.w);// w is directed from the base to v2
    this.uv[1] = new Vector2f(1, heightDropPoint.subtractLocal(envelope.min).length() / envelope.height);
    this.uv[2] = new Vector2f(0, 1);
}
 
Example 15
Source File: BIHNode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public final int intersectWhere(Collidable col,
            BoundingBox box,
            Matrix4f worldMatrix,
            BIHTree tree,
            CollisionResults results) {

        TempVars vars = TempVars.get();
        ArrayList<BIHStackData> stack = vars.bihStack;
        stack.clear();

        float[] minExts = {box.getCenter().x - box.getXExtent(),
            box.getCenter().y - box.getYExtent(),
            box.getCenter().z - box.getZExtent()};

        float[] maxExts = {box.getCenter().x + box.getXExtent(),
            box.getCenter().y + box.getYExtent(),
            box.getCenter().z + box.getZExtent()};

        stack.add(new BIHStackData(this, 0, 0));

        Triangle t = new Triangle();
        int cols = 0;

        stackloop:
        while (stack.size() > 0) {
            BIHNode node = stack.remove(stack.size() - 1).node;

            while (node.axis != 3) {
                int a = node.axis;

                float maxExt = maxExts[a];
                float minExt = minExts[a];

                if (node.leftPlane < node.rightPlane) {
                    // means there's a gap in the middle
                    // if the box is in that gap, we stop there
                    if (minExt > node.leftPlane
                            && maxExt < node.rightPlane) {
                        continue stackloop;
                    }
                }

                if (maxExt < node.rightPlane) {
                    node = node.left;
                } else if (minExt > node.leftPlane) {
                    node = node.right;
                } else {
                    stack.add(new BIHStackData(node.right, 0, 0));
                    node = node.left;
                }
//                if (maxExt < node.leftPlane
//                 && maxExt < node.rightPlane){
//                    node = node.left;
//                }else if (minExt > node.leftPlane
//                       && minExt > node.rightPlane){
//                    node = node.right;
//                }else{

//                }
            }

            for (int i = node.leftIndex; i <= node.rightIndex; i++) {
                tree.getTriangle(i, t.get1(), t.get2(), t.get3());
                if (worldMatrix != null) {
                    worldMatrix.mult(t.get1(), t.get1());
                    worldMatrix.mult(t.get2(), t.get2());
                    worldMatrix.mult(t.get3(), t.get3());
                }

                int added = col.collideWith(t, results);

                if (added > 0) {
                    int index = tree.getTriangleIndex(i);
                    int start = results.size() - added;

                    for (int j = start; j < results.size(); j++) {
                        CollisionResult cr = results.getCollisionDirect(j);
                        cr.setTriangleIndex(index);
                    }

                    cols += added;
                }
            }
        }
        vars.release();
        return cols;
    }