Package com.jme3.math

Examples of com.jme3.math.Triangle


        float extent = Math.max(bbox.getXExtent(), Math.max(bbox.getYExtent(), bbox.getZExtent()));
        bbox.setXExtent(extent);
        bbox.setYExtent(extent);
        bbox.setZExtent(extent);

        Triangle t = new Triangle();
        for (int g = 0; g < geoms.length; g++){
            Mesh m = geoms[g].getMesh();
            for (int i = 0; i < m.getTriangleCount(); i++){
                m.getTriangle(i, t);
                OCTTriangle ot = new OCTTriangle(t.get1(), t.get2(), t.get3(), i, g);
                allTris.add(ot);
                // convert triangle to world space
//                geom.getWorldTransform().transformVector(t.get1(), t.get1());
//                geom.getWorldTransform().transformVector(t.get2(), t.get2());
//                geom.getWorldTransform().transformVector(t.get3(), t.get3());
View Full Code Here


            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) {
View Full Code Here

        this.triangleIndex = index;
    }

    public Triangle getTriangle(Triangle store){
        if (store == null)
            store = new Triangle();

        Mesh m = geometry.getMesh();
        m.getTriangle(triangleIndex, store);
        store.calculateCenter();
        store.calculateNormal();
View Full Code Here

     * @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;
    }
View Full Code Here

            float y = positions[i + 1];
            uvCoordinates[j + 1] = (y - vBase) / bt.getHeight();
        }

        // looking for splitted triangles
        Triangle triangle = new Triangle();
        for (int i = 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]);
            float sgn1 = Math.signum(triangle.get1().x - cx);
            float sgn2 = Math.signum(triangle.get2().x - cx);
            float sgn3 = Math.signum(triangle.get3().x - cx);
            float xSideFactor = sgn1 + sgn2 + sgn3;
            float ySideFactor = Math.signum(triangle.get1().z - cz) + Math.signum(triangle.get2().z - cz) + Math.signum(triangle.get3().z - cz);
            if ((xSideFactor > -3 || xSideFactor < 3) && ySideFactor < 0) {// the triangle is on the splitting plane
                if (sgn1 == 1.0f) {
                    uvCoordinates[i / 3 * 2] += 1.0f;
                }
                if (sgn2 == 1.0f) {
View Full Code Here

            angle = v.angleBetween(vBase);// result between [0; PI]
            uvCoordinates[j + 1] = angle / FastMath.PI;
        }

        // looking for splitted triangles
        Triangle triangle = new Triangle();
        for (int i = 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]);
            float sgn1 = Math.signum(triangle.get1().x - cx);
            float sgn2 = Math.signum(triangle.get2().x - cx);
            float sgn3 = Math.signum(triangle.get3().x - cx);
            float xSideFactor = sgn1 + sgn2 + sgn3;
            float ySideFactor = Math.signum(triangle.get1().y - cy) + Math.signum(triangle.get2().y - cy) + Math.signum(triangle.get3().y - cy);
            if ((xSideFactor > -3 || xSideFactor < 3) && ySideFactor < 0) {// the triangle is on the splitting plane
                if (sgn1 == 1.0f) {
                    uvCoordinates[i / 3 * 2] += 1.0f;
                }
                if (sgn2 == 1.0f) {
View Full Code Here

            final Vector3f intersection = new Vector3f();
            final Vector2f loc = tracer.getGridLocation();

            if (tracer.isRayPerpendicularToGrid()) {
                Triangle hit = new Triangle();
                checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit);
                float distance = worldPickRay.origin.distance(intersection);
                CollisionResult cr = new CollisionResult(intersection, distance);
                cr.setGeometry(patch);
                cr.setContactNormal(hit.getNormal());
                results.addCollision(cr);
                return intersection;
            }
           
           

            while (loc.x >= -1 && loc.x <= patch.getSize() &&
                   loc.y >= -1 && loc.y <= patch.getSize()) {

                //System.out.print(loc.x+","+loc.y+" : ");
                // check the triangles of main square for intersection.
                Triangle hit = new Triangle();
                if (checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit)) {
                    // we found an intersection, so return that!
                    float distance = worldPickRay.origin.distance(intersection);
                    CollisionResult cr = new CollisionResult(intersection, distance);
                    cr.setGeometry(patch);
                    results.addCollision(cr);
                    cr.setContactNormal(hit.getNormal());
                    return intersection;
                }

                // because of how we get our height coords, we will
                // sometimes be off by a grid spot, so we check the next
                // grid space up.
                int dx = 0, dz = 0;
                Direction d = tracer.getLastStepDirection();
                switch (d) {
                case PositiveX:
                case NegativeX:
                    dx = 0;
                    dz = 1;
                    break;
                case PositiveZ:
                case NegativeZ:
                    dx = 1;
                    dz = 0;
                    break;
                }

                if (checkTriangles(loc.x + dx, loc.y + dz, workRay, intersection, patch, hit)) {
                    // we found an intersection, so return that!
                    float distance = worldPickRay.origin.distance(intersection);
                    CollisionResult cr = new CollisionResult(intersection, distance);
                    results.addCollision(cr);
                    cr.setGeometry(patch);
                    cr.setContactNormal(hit.getNormal());
                    return intersection;
                }

                tracer.next();
            }
View Full Code Here

     * @param x local x coordinate
     * @param z local z coordinate
     * @return a triangle in world space not local space
     */
    protected Triangle getTriangleAtPoint(float x, float z, Vector3f scale, Vector3f translation) {
        Triangle tri = getTriangleAtPoint(x, z);
        if (tri != null) {
            tri.get1().multLocal(scale).addLocal(translation);
            tri.get2().multLocal(scale).addLocal(translation);
            tri.get3().multLocal(scale).addLocal(translation);
        }
        return tri;
    }
View Full Code Here

        int index = findClosestHeightIndex(gridX, gridY);
        if (index < 0) {
            return null;
        }

        Triangle t = new Triangle(new Vector3f(), new Vector3f(), new Vector3f());
        Triangle t2 = new Triangle(new Vector3f(), new Vector3f(), new Vector3f());

        float h1 = hdata[index];                // top left
        float h2 = hdata[index + 1];            // top right
        float h3 = hdata[index + width];        // bottom left
        float h4 = hdata[index + width + 1];    // bottom right


        if ((gridX == 0 && gridY == 0) || (gridX == width - 2 && gridY == width - 2)) {
            // top left or bottom right grid point
            t.get(0).x = (gridX);
            t.get(0).y = (h1);
            t.get(0).z = (gridY);

            t.get(1).x = (gridX);
            t.get(1).y = (h3);
            t.get(1).z = (gridY + 1);

            t.get(2).x = (gridX + 1);
            t.get(2).y = (h4);
            t.get(2).z = (gridY + 1);

            t2.get(0).x = (gridX);
            t2.get(0).y = (h1);
            t2.get(0).z = (gridY);

            t2.get(1).x = (gridX + 1);
            t2.get(1).y = (h4);
            t2.get(1).z = (gridY + 1);

            t2.get(2).x = (gridX + 1);
            t2.get(2).y = (h2);
            t2.get(2).z = (gridY);
        } else {
            // all other grid points
            t.get(0).x = (gridX);
            t.get(0).y = (h1);
            t.get(0).z = (gridY);

            t.get(1).x = (gridX);
            t.get(1).y = (h3);
            t.get(1).z = (gridY + 1);

            t.get(2).x = (gridX + 1);
            t.get(2).y = (h2);
            t.get(2).z = (gridY);

            t2.get(0).x = (gridX + 1);
            t2.get(0).y = (h2);
            t2.get(0).z = (gridY);

            t2.get(1).x = (gridX);
            t2.get(1).y = (h3);
            t2.get(1).z = (gridY + 1);

            t2.get(2).x = (gridX + 1);
            t2.get(2).y = (h4);
            t2.get(2).z = (gridY + 1);
        }

        return new Triangle[]{t, t2};
    }
View Full Code Here

        return binormals;
    }

    public static ScenegraphNode buildWireframe(Geometry refGeom) {
        Line line;
        Triangle tri;
        Geometry g;
        ScenegraphNode tris = new ScenegraphNode("Wireframe");
        for (int i = 0; i < refGeom.getMesh().getTriangleCount(); i++) {
            tri = new Triangle();
            refGeom.getMesh().getTriangle(i, tri);

            // bc = new BillboardControl();
            // helloText = new BitmapText(guiFont, false);
            // helloText.setSize(.05f);
            // helloText.setText("tri" + i);
            // helloText.setLocalTranslation(tri.getCenter());
            // helloText.addControl(bc);
            // myApp.getRootNode().attachChild(helloText);

            line = new Line(tri.get1(), tri.get2());
            g = new Geometry("Line" + tri.get1() + "->" + tri.get2(), line);
            g.setMaterial(DebugMaterials.wireMat);
            tris.attachChild(g);

            line = new Line(tri.get2(), tri.get3());
            g = new Geometry("Line" + tri.get2() + "->" + tri.get3(), line);
            g.setMaterial(DebugMaterials.wireMat);
            tris.attachChild(g);

            line = new Line(tri.get3(), tri.get1());
            g = new Geometry("Line" + tri.get3() + "->" + tri.get1(), line);
            g.setMaterial(DebugMaterials.wireMat);
            tris.attachChild(g);
        }
        GeometryBatchFactory.optimize(tris);
        return tris;
View Full Code Here

TOP

Related Classes of com.jme3.math.Triangle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.