Search in sources :

Example 56 with Spatial

use of com.ardor3d.scenegraph.Spatial 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)

Example 57 with Spatial

use of com.ardor3d.scenegraph.Spatial in project energy3d by concord-consortium.

the class MeshLocator method find.

public Mesh find() {
    if (foundation.getImportedNodes() == null || foundation.getImportedNodes().isEmpty()) {
        return null;
    }
    Node node = null;
    for (final Node n : foundation.getImportedNodes()) {
        if (n.getNumberOfChildren() > 0) {
            final Spatial s = n.getChild(0);
            final Mesh m = (Mesh) s;
            final UserData u = (UserData) m.getUserData();
            if (u.getNodeIndex() == nodeIndex) {
                node = n;
                break;
            }
        }
    }
    if (node != null) {
        for (final Spatial s : node.getChildren()) {
            final Mesh m = (Mesh) s;
            final UserData u = (UserData) m.getUserData();
            if (u.getMeshIndex() == meshIndex) {
                return m;
            }
        }
    }
    return null;
}
Also used : Spatial(com.ardor3d.scenegraph.Spatial) Node(com.ardor3d.scenegraph.Node) Mesh(com.ardor3d.scenegraph.Mesh)

Aggregations

Spatial (com.ardor3d.scenegraph.Spatial)57 Mesh (com.ardor3d.scenegraph.Mesh)36 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)33 Node (com.ardor3d.scenegraph.Node)26 CullHint (com.ardor3d.scenegraph.hint.CullHint)26 Vector3 (com.ardor3d.math.Vector3)24 TPoint (org.poly2tri.triangulation.point.TPoint)18 PickResults (com.ardor3d.intersection.PickResults)15 PrimitivePickResults (com.ardor3d.intersection.PrimitivePickResults)15 Ray3 (com.ardor3d.math.Ray3)15 Point (org.poly2tri.geometry.primitives.Point)14 FloatBuffer (java.nio.FloatBuffer)13 ArrayList (java.util.ArrayList)12 HousePart (org.concord.energy3d.model.HousePart)12 CancellationException (java.util.concurrent.CancellationException)10 Foundation (org.concord.energy3d.model.Foundation)10 Roof (org.concord.energy3d.model.Roof)9 Calendar (java.util.Calendar)7 UserData (org.concord.energy3d.model.UserData)6 Window (org.concord.energy3d.model.Window)6