Search in sources :

Example 1 with PickData

use of com.ardor3d.intersection.PickData in project energy3d by concord-consortium.

the class Sensor method drawMesh.

@Override
protected void drawMesh() {
    if (container == null) {
        return;
    }
    if (container instanceof Roof) {
        final PickResults pickResults = new PrimitivePickResults();
        final Ray3 ray = new Ray3(getAbsPoint(0).addLocal(0, 0, 1000), Vector3.NEG_UNIT_Z);
        PickingUtil.findPick(container.getRoot(), ray, pickResults, false);
        if (pickResults.getNumber() != 0) {
            final PickData pickData = pickResults.getPickData(0);
            final Vector3 p = pickData.getIntersectionRecord().getIntersectionPoint(0);
            points.get(0).setZ(p.getZ());
            final UserData userData = (UserData) ((Spatial) pickData.getTarget()).getUserData();
            final int roofPartIndex = userData.getEditPointIndex();
            normal = (ReadOnlyVector3) ((Roof) container).getRoofPartsRoot().getChild(roofPartIndex).getUserData();
        }
    } else {
        normal = container.getNormal();
    }
    updateEditShapes();
    final double annotationScale = Scene.getInstance().getAnnotationScale();
    // last arg sets close to zero so the sensor doesn't cast shadow
    surround.setData(Vector3.ZERO, WIDTH / 2.0 / annotationScale, HEIGHT / 2.0 / annotationScale, 0.02);
    surround.updateModelBound();
    final FloatBuffer boxVertexBuffer = surround.getMeshData().getVertexBuffer();
    final FloatBuffer vertexBuffer = mesh.getMeshData().getVertexBuffer();
    final FloatBuffer textureBuffer = mesh.getMeshData().getTextureBuffer(0);
    final FloatBuffer outlineBuffer = outlineMesh.getMeshData().getVertexBuffer();
    vertexBuffer.rewind();
    outlineBuffer.rewind();
    textureBuffer.rewind();
    int i = 8 * 3;
    vertexBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    textureBuffer.put(1).put(0);
    outlineBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    i += 3;
    vertexBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    textureBuffer.put(0).put(0);
    outlineBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    outlineBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    i += 3;
    vertexBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    vertexBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    textureBuffer.put(0).put(1);
    textureBuffer.put(0).put(1);
    outlineBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    outlineBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    i += 3;
    vertexBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    textureBuffer.put(1).put(1);
    outlineBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    outlineBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    i = 8 * 3;
    vertexBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    textureBuffer.put(1).put(0);
    outlineBuffer.put(boxVertexBuffer.get(i)).put(boxVertexBuffer.get(i + 1)).put(boxVertexBuffer.get(i + 2));
    mesh.updateModelBound();
    outlineMesh.updateModelBound();
    mesh.setTranslation(getAbsPoint(0));
    if (normal != null) {
        // FIXME: Sometimes normal is null
        if (Util.isEqual(normal, Vector3.UNIT_Z)) {
            mesh.setRotation(new Matrix3());
        } else {
            mesh.setRotation(new Matrix3().lookAt(normal, Vector3.UNIT_Z));
        }
    }
    surround.setTranslation(mesh.getTranslation());
    surround.setRotation(mesh.getRotation());
    outlineMesh.setTranslation(mesh.getTranslation());
    outlineMesh.setRotation(mesh.getRotation());
    final ReadOnlyVector3 translation = mesh.getTranslation();
    label.setText("" + getId());
    if (normal != null) {
        final double labelOffset = 1.0;
        label.setTranslation(translation.getX() + labelOffset * normal.getX(), translation.getY() + labelOffset * normal.getY(), translation.getZ() + labelOffset * normal.getZ());
    }
}
Also used : PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) PickResults(com.ardor3d.intersection.PickResults) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) FloatBuffer(java.nio.FloatBuffer) PickData(com.ardor3d.intersection.PickData) Ray3(com.ardor3d.math.Ray3) Matrix3(com.ardor3d.math.Matrix3)

Example 2 with PickData

use of com.ardor3d.intersection.PickData in project energy3d by concord-consortium.

the class SelectUtil method getPickResultForImportedMesh.

// if this is an imported mesh, do it here. getPickResult method below returns incorrect result.
private static PickedHousePart getPickResultForImportedMesh() {
    if (pickResults.getNumber() > 0) {
        final PickData pick = pickResults.getPickData(0);
        final Pickable pickable = pick.getTarget();
        if (pickable instanceof Mesh) {
            final Mesh m = (Mesh) pickable;
            final UserData u = (UserData) m.getUserData();
            // the user data of land can be null
            if (u != null && u.isImported()) {
                return new PickedHousePart(u, pick.getIntersectionRecord().getIntersectionPoint(0), u.getRotatedNormal() == null ? u.getNormal() : u.getRotatedNormal());
            }
        }
    }
    return null;
}
Also used : UserData(org.concord.energy3d.model.UserData) Pickable(com.ardor3d.intersection.Pickable) Mesh(com.ardor3d.scenegraph.Mesh) PickData(com.ardor3d.intersection.PickData) PickedHousePart(org.concord.energy3d.model.PickedHousePart)

Example 3 with PickData

use of com.ardor3d.intersection.PickData in project energy3d by concord-consortium.

the class SelectUtil method getPickResult.

private static PickedHousePart getPickResult(final Ray3 pickRay) {
    PickedHousePart pickedHousePart = null;
    double polyDist = Double.MAX_VALUE;
    double pointDist = Double.MAX_VALUE;
    int objCounter = 0;
    HousePart prevHousePart = null;
    final long pickLayer = SelectUtil.pickLayer == -1 ? -1 : SelectUtil.pickLayer % Math.max(1, pickResults.getNumber());
    for (int i = 0; i < pickResults.getNumber(); i++) {
        final PickData pick = pickResults.getPickData(i);
        if (pick.getIntersectionRecord().getNumberOfIntersections() == 0) {
            continue;
        }
        final Object obj = ((Mesh) pick.getTarget()).getUserData();
        UserData userData = null;
        if (obj instanceof UserData) {
            // FIXME: Note that userData can be null if the pick is the land
            userData = (UserData) obj;
            if (userData.getHousePart() != prevHousePart) {
                objCounter++;
                prevHousePart = userData.getHousePart();
            }
        } else if (pickLayer != -1) {
            continue;
        }
        if (pickLayer != -1 && objCounter - 1 != pickLayer) {
            continue;
        }
        final Vector3 intersectionPoint = pick.getIntersectionRecord().getIntersectionPoint(0);
        final PickedHousePart picked_i = new PickedHousePart(userData, intersectionPoint, pick.getIntersectionRecord().getIntersectionNormal(0));
        double polyDist_i = pick.getIntersectionRecord().getClosestDistance();
        if (userData != null && userData.getHousePart() instanceof Window) {
            // give more priority to window (especially skylight)
            polyDist_i -= 0.2;
        }
        double pointDist_i = Double.MAX_VALUE;
        if (userData != null && polyDist_i - polyDist < 0.1) {
            for (int j = 0; j < userData.getHousePart().getPoints().size(); j++) {
                final Vector3 p = userData.getHousePart().getAbsPoint(j);
                pointDist_i = p.distance(intersectionPoint);
                double adjust = 0;
                if (userData.getHousePart().isFirstPointInserted()) {
                    // to avoid IndexOutOfBoundsException: Index: 2, Size: 2
                    if (userData.getHousePart().getNormal() != null) {
                        adjust -= Math.abs(userData.getHousePart().getNormal().negate(null).dot(pickRay.getDirection()) / 10.0);
                    }
                }
                if (userData.getHousePart() == SceneManager.getInstance().getSelectedPart()) {
                    // give more priority because the object is selected
                    adjust -= 0.1;
                }
                if (userData.isEditPoint()) {
                    // give more priority because this is an edit point
                    adjust -= 0.1;
                }
                if (userData.isEditPoint() && userData.getHousePart() instanceof Foundation && ((Foundation) userData.getHousePart()).isResizeHouseMode()) {
                    adjust -= 0.1;
                }
                pointDist_i += adjust;
                if (pointDist_i < pointDist && (userData.getEditPointIndex() != -1 || pickedHousePart == null || pickedHousePart.getUserData() == null || pickedHousePart.getUserData().getEditPointIndex() == -1)) {
                    pickedHousePart = picked_i;
                    polyDist = polyDist_i;
                    pointDist = pointDist_i;
                }
            }
        }
        if (pickedHousePart == null || polyDist_i < polyDist) {
            pickedHousePart = picked_i;
            polyDist = polyDist_i;
            pointDist = pointDist_i;
        }
    }
    return pickedHousePart;
}
Also used : Window(org.concord.energy3d.model.Window) UserData(org.concord.energy3d.model.UserData) Mesh(com.ardor3d.scenegraph.Mesh) Vector3(com.ardor3d.math.Vector3) Foundation(org.concord.energy3d.model.Foundation) PickData(com.ardor3d.intersection.PickData) PickedHousePart(org.concord.energy3d.model.PickedHousePart) HousePart(org.concord.energy3d.model.HousePart) PickedHousePart(org.concord.energy3d.model.PickedHousePart)

Example 4 with PickData

use of com.ardor3d.intersection.PickData in project energy3d by concord-consortium.

the class HousePart method computeNormalAndKeepOnSurface.

protected ReadOnlyVector3 computeNormalAndKeepOnSurface() {
    if (container == null) {
        return null;
    }
    if (container instanceof Rack) {
        final Rack rack = (Rack) container;
        final PickResults pickResults = new PrimitivePickResults();
        final Ray3 ray = new Ray3(getAbsPoint(0).multiplyLocal(1, 1, 0), Vector3.UNIT_Z);
        PickingUtil.findPick(container.getCollisionSpatial(), ray, pickResults, false);
        if (pickResults.getNumber() != 0) {
            final PickData pickData = pickResults.getPickData(0);
            final Vector3 p = pickData.getIntersectionRecord().getIntersectionPoint(0);
            points.get(0).setZ(p.getZ());
        } else {
            if (rack.getBaseHeight() < Math.abs(0.5 * rack.getRackHeight() / Scene.getInstance().getAnnotationScale() * Math.sin(Math.toRadians(rack.getTiltAngle())))) {
                final Ray3 ray2 = new Ray3(getAbsPoint(0).multiplyLocal(1, 1, 0), Vector3.NEG_UNIT_Z);
                PickingUtil.findPick(container.getCollisionSpatial(), ray2, pickResults, false);
                if (pickResults.getNumber() != 0) {
                    final PickData pickData = pickResults.getPickData(0);
                    final Vector3 p = pickData.getIntersectionRecord().getIntersectionPoint(0);
                    points.get(0).setZ(p.getZ());
                }
            }
        }
        return rack.getNormal();
    } else if (container instanceof Roof) {
        final Roof roof = (Roof) container;
        final int[] editPointToRoofIndex = new int[points.size()];
        final PickResults pickResults = new PrimitivePickResults();
        for (int i = 0; i < points.size(); i++) {
            pickResults.clear();
            final Ray3 ray = new Ray3(getAbsPoint(i).multiplyLocal(1, 1, 0), Vector3.UNIT_Z);
            for (final Spatial roofPart : roof.getRoofPartsRoot().getChildren()) {
                if (roofPart.getSceneHints().getCullHint() != CullHint.Always) {
                    PickingUtil.findPick(((Node) roofPart).getChild(0), ray, pickResults, false);
                    if (pickResults.getNumber() != 0) {
                        break;
                    }
                }
            }
            if (pickResults.getNumber() != 0) {
                final PickData pickData = pickResults.getPickData(0);
                final Vector3 p = pickData.getIntersectionRecord().getIntersectionPoint(0);
                points.get(i).setZ(p.getZ());
                final UserData userData = (UserData) ((Spatial) pickData.getTarget()).getUserData();
                final int roofPartIndex = userData.getEditPointIndex();
                editPointToRoofIndex[i] = roofPartIndex;
            }
            // find roofPart with most edit points on it
            containerRoofIndex = editPointToRoofIndex[0];
            if (points.size() > 1) {
                containerRoofIndex = 0;
                final Map<Integer, Integer> counts = new HashMap<Integer, Integer>(points.size());
                for (final int roofIndex : editPointToRoofIndex) {
                    counts.put(roofIndex, counts.get(roofIndex) == null ? 1 : counts.get(roofIndex) + 1);
                }
                int highestCount = 0;
                for (final int roofIndex : editPointToRoofIndex) {
                    if (counts.get(roofIndex) > highestCount) {
                        highestCount = counts.get(roofIndex);
                        containerRoofIndex = roofIndex;
                    }
                }
            }
        }
        return (ReadOnlyVector3) roof.getRoofPartsRoot().getChild(containerRoofIndex).getUserData();
    } else if (container instanceof Foundation) {
        final Foundation foundation = (Foundation) container;
        final List<Node> nodes = foundation.getImportedNodes();
        if (nodes != null) {
            final Map<Vector3, ReadOnlyVector3> intersections = new HashMap<Vector3, ReadOnlyVector3>();
            final PickResults pickResults = new PrimitivePickResults();
            for (final Node n : nodes) {
                for (final Spatial s : n.getChildren()) {
                    if (s instanceof Mesh) {
                        final Mesh m = (Mesh) s;
                        pickResults.clear();
                        PickingUtil.findPick(m, new Ray3(getAbsPoint(0).multiplyLocal(1, 1, 0), Vector3.UNIT_Z), pickResults, false);
                        if (pickResults.getNumber() > 0) {
                            intersections.put(pickResults.getPickData(0).getIntersectionRecord().getIntersectionPoint(0), ((UserData) m.getUserData()).getNormal());
                        }
                    }
                }
            }
            if (!intersections.isEmpty()) {
                double zmax = -Double.MAX_VALUE;
                ReadOnlyVector3 normal = null;
                for (final Vector3 v : intersections.keySet()) {
                    if (v.getZ() > zmax) {
                        zmax = v.getZ();
                        normal = intersections.get(v);
                    }
                }
                if (normal != null) {
                    pickedNormal = normal;
                    return normal;
                }
            }
        }
    }
    return container.getNormal();
}
Also used : HashMap(java.util.HashMap) Node(com.ardor3d.scenegraph.Node) Mesh(com.ardor3d.scenegraph.Mesh) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) PickData(com.ardor3d.intersection.PickData) Ray3(com.ardor3d.math.Ray3) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Spatial(com.ardor3d.scenegraph.Spatial) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) PickResults(com.ardor3d.intersection.PickResults) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

PickData (com.ardor3d.intersection.PickData)4 Vector3 (com.ardor3d.math.Vector3)3 Mesh (com.ardor3d.scenegraph.Mesh)3 PickResults (com.ardor3d.intersection.PickResults)2 PrimitivePickResults (com.ardor3d.intersection.PrimitivePickResults)2 Ray3 (com.ardor3d.math.Ray3)2 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)2 PickedHousePart (org.concord.energy3d.model.PickedHousePart)2 UserData (org.concord.energy3d.model.UserData)2 Pickable (com.ardor3d.intersection.Pickable)1 Matrix3 (com.ardor3d.math.Matrix3)1 Node (com.ardor3d.scenegraph.Node)1 Spatial (com.ardor3d.scenegraph.Spatial)1 FloatBuffer (java.nio.FloatBuffer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Foundation (org.concord.energy3d.model.Foundation)1 HousePart (org.concord.energy3d.model.HousePart)1 Window (org.concord.energy3d.model.Window)1