Search in sources :

Example 1 with ReadOnlyVector2

use of com.ardor3d.math.type.ReadOnlyVector2 in project energy3d by concord-consortium.

the class SceneManager method initMouse.

private void initMouse() {
    logicalLayer.registerTrigger(new InputTrigger(new MouseButtonPressedCondition(MouseButton.LEFT), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            ((Component) canvas).requestFocusInWindow();
            if (Config.isMac()) {
                // control-click is mouse right-click on the Mac, skip
                final KeyboardState ks = inputStates.getCurrent().getKeyboardState();
                if (ks.isDown(Key.LCONTROL) || ks.isDown(Key.RCONTROL)) {
                    return;
                }
            }
            if (firstClickState == null) {
                firstClickState = inputStates;
                mousePressed(inputStates.getCurrent().getMouseState(), inputStates.getCurrent().getKeyboardState());
            } else {
                firstClickState = null;
                mouseReleased(inputStates.getCurrent().getMouseState());
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new MouseButtonReleasedCondition(MouseButton.LEFT), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (Config.isMac()) {
                // control-click is mouse right-click on the Mac, skip
                final KeyboardState ks = inputStates.getCurrent().getKeyboardState();
                if (ks.isDown(Key.LCONTROL) || ks.isDown(Key.RCONTROL)) {
                    return;
                }
            }
            // if editing object using select or resize then only mouse drag is allowed
            if (operation == Operation.SELECT || operation == Operation.RESIZE) {
                firstClickState = null;
                mouseReleased(inputStates.getCurrent().getMouseState());
            } else if (firstClickState != null) {
                final MouseState mouseState = inputStates.getCurrent().getMouseState();
                final MouseState prevMouseState = firstClickState.getCurrent().getMouseState();
                final ReadOnlyVector2 p1 = new Vector2(prevMouseState.getX(), prevMouseState.getY());
                final ReadOnlyVector2 p2 = new Vector2(mouseState.getX(), mouseState.getY());
                if (!(selectedPart instanceof Foundation || selectedPart instanceof Wall || selectedPart instanceof Window || selectedPart instanceof Door) || p1.distance(p2) > 10) {
                    firstClickState = null;
                    mouseReleased(inputStates.getCurrent().getMouseState());
                }
            }
        }
    }));
    ((Component) canvas).addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(final MouseEvent e) {
            if (Util.isRightClick(e)) {
                mouseRightClicked(e);
            }
        }

        @Override
        public void mouseReleased(final MouseEvent e) {
            if (Util.isRightClick(e)) {
                if (cameraChanged) {
                    TimeSeriesLogger.getInstance().logCamera("Pan");
                    cameraChanged = false;
                }
            }
        }
    });
    ((Component) canvas).addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseDragged(final MouseEvent e) {
            EnergyPanel.getInstance().update();
            cameraChanged = true;
        }
    });
    ((Component) canvas).addMouseWheelListener(new MouseWheelListener() {

        @Override
        public void mouseWheelMoved(final MouseWheelEvent e) {
            TimeSeriesLogger.getInstance().logCamera("Zoom");
        }
    });
    logicalLayer.registerTrigger(new InputTrigger(new MouseMovedCondition(), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            refresh = true;
            mouseState = inputStates.getCurrent().getMouseState();
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new MouseButtonClickedCondition(MouseButton.LEFT), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (Config.isMac()) {
                // control-click is mouse right-click on the Mac, skip
                final KeyboardState ks = inputStates.getCurrent().getKeyboardState();
                if (ks.isDown(Key.LCONTROL) || ks.isDown(Key.RCONTROL)) {
                    return;
                }
            }
            if (!isTopView() && inputStates.getCurrent().getMouseState().getClickCount(MouseButton.LEFT) == 2) {
                if (PrintController.getInstance().isPrintPreview()) {
                    final MouseState mouse = inputStates.getCurrent().getMouseState();
                    final Ray3 pickRay = Camera.getCurrentCamera().getPickRay(new Vector2(mouse.getX(), mouse.getY()), false, null);
                    final PickResults pickResults = new PrimitivePickResults();
                    PickingUtil.findPick(PrintController.getInstance().getPagesRoot(), pickRay, pickResults, false);
                    if (pickResults.getNumber() > 0) {
                        cameraControl.zoomAtPoint(pickResults.getPickData(0).getIntersectionRecord().getIntersectionPoint(0));
                    }
                } else {
                    final PickedHousePart pickedHousePart = SelectUtil.pickPart(inputStates.getCurrent().getMouseState().getX(), inputStates.getCurrent().getMouseState().getY());
                    if (pickedHousePart != null) {
                        cameraControl.zoomAtPoint(pickedHousePart.getPoint());
                    }
                }
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.LSHIFT), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
        // SelectUtil.setPickLayer(0);
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.LSHIFT), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
        // SelectUtil.setPickLayer(-1);
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.DELETE), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            deleteCurrentSelection();
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.BACK), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            deleteCurrentSelection();
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyHeldCondition(Key.ESCAPE), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            hideAllEditPoints();
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.ZERO), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            final KeyboardState ks = inputStates.getCurrent().getKeyboardState();
            if (Config.isMac()) {
                if (ks.isDown(Key.LMETA) || ks.isDown(Key.RMETA)) {
                    resetCamera();
                }
            } else {
                if (ks.isDown(Key.LCONTROL) || ks.isDown(Key.RCONTROL)) {
                    resetCamera();
                }
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.I), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            System.out.println("---- Parts: ------------------------");
            System.out.println("size = " + Scene.getInstance().getParts().size());
            for (final HousePart part : Scene.getInstance().getParts()) {
                System.out.println(part);
            }
            System.out.println("---- Scene: ------------------------");
            System.out.println("size = " + Scene.getOriginalHouseRoot().getNumberOfChildren());
            for (final Spatial mesh : Scene.getOriginalHouseRoot().getChildren()) {
                System.out.println(mesh);
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.R), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            Scene.getInstance().redrawAll(true);
        }
    }));
    // Run/pause model replay
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.SPACE), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (PlayControl.active) {
                PlayControl.replaying = !PlayControl.replaying;
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.LEFT), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (PlayControl.active) {
                PlayControl.replaying = false;
                PlayControl.backward = true;
            }
            if (isTopView()) {
                if (tooManyPartsToMove()) {
                    moveWithKey(inputStates.getCurrent().getKeyboardState(), new Vector3(-1, 0, 0));
                } else {
                    if (arrowKeyHolderTask != null) {
                        arrowKeyHolderTask.cancel();
                    }
                    arrowKeyHolderTask = new KeyHolderTask(inputStates.getCurrent().getKeyboardState(), new Vector3(-1, 0, 0));
                    keyHolder.scheduleAtFixedRate(arrowKeyHolderTask, 0, keyHolderInterval);
                }
            } else {
                if (selectedPart instanceof Window) {
                    final Vector3 v = selectedPart.getNormal().clone();
                    v.crossLocal(Vector3.UNIT_Z);
                    moveWithKey(inputStates.getCurrent().getKeyboardState(), v);
                    Scene.getInstance().redrawAll();
                }
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.LEFT), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (arrowKeyHolderTask != null) {
                arrowKeyHolderTask.cancel();
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.UP), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (PlayControl.active) {
                PlayControl.replaying = false;
                PlayControl.backward = true;
            }
            if (isTopView()) {
                if (tooManyPartsToMove()) {
                    moveWithKey(inputStates.getCurrent().getKeyboardState(), new Vector3(0, 1, 0));
                } else {
                    if (arrowKeyHolderTask != null) {
                        arrowKeyHolderTask.cancel();
                    }
                    arrowKeyHolderTask = new KeyHolderTask(inputStates.getCurrent().getKeyboardState(), new Vector3(0, 1, 0));
                    keyHolder.scheduleAtFixedRate(arrowKeyHolderTask, 0, keyHolderInterval);
                }
            } else {
                if (selectedPart instanceof Window) {
                    final Vector3 n = selectedPart.getNormal().clone();
                    final Vector3 v = n.cross(Vector3.UNIT_Z, null);
                    moveWithKey(inputStates.getCurrent().getKeyboardState(), v.crossLocal(n));
                    Scene.getInstance().redrawAll();
                }
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.UP), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (arrowKeyHolderTask != null) {
                arrowKeyHolderTask.cancel();
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.RIGHT), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (PlayControl.active) {
                PlayControl.replaying = false;
                PlayControl.forward = true;
            }
            if (isTopView()) {
                if (tooManyPartsToMove()) {
                    moveWithKey(inputStates.getCurrent().getKeyboardState(), new Vector3(1, 0, 0));
                } else {
                    if (arrowKeyHolderTask != null) {
                        arrowKeyHolderTask.cancel();
                    }
                    arrowKeyHolderTask = new KeyHolderTask(inputStates.getCurrent().getKeyboardState(), new Vector3(1, 0, 0));
                    keyHolder.scheduleAtFixedRate(arrowKeyHolderTask, 0, keyHolderInterval);
                }
            } else {
                if (selectedPart instanceof Window) {
                    final Vector3 v = selectedPart.getNormal().clone();
                    v.crossLocal(Vector3.UNIT_Z).negateLocal();
                    moveWithKey(inputStates.getCurrent().getKeyboardState(), v);
                    Scene.getInstance().redrawAll();
                }
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.RIGHT), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (arrowKeyHolderTask != null) {
                arrowKeyHolderTask.cancel();
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.DOWN), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (PlayControl.active) {
                PlayControl.replaying = false;
                PlayControl.forward = true;
            }
            if (isTopView()) {
                if (tooManyPartsToMove()) {
                    moveWithKey(inputStates.getCurrent().getKeyboardState(), new Vector3(0, -1, 0));
                } else {
                    if (arrowKeyHolderTask != null) {
                        arrowKeyHolderTask.cancel();
                    }
                    arrowKeyHolderTask = new KeyHolderTask(inputStates.getCurrent().getKeyboardState(), new Vector3(0, -1, 0));
                    keyHolder.scheduleAtFixedRate(arrowKeyHolderTask, 0, keyHolderInterval);
                }
            } else {
                if (selectedPart instanceof Window) {
                    final Vector3 n = selectedPart.getNormal().clone();
                    final Vector3 v = n.cross(Vector3.UNIT_Z, null).negateLocal();
                    moveWithKey(inputStates.getCurrent().getKeyboardState(), v.crossLocal(n));
                    Scene.getInstance().redrawAll();
                }
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.DOWN), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (arrowKeyHolderTask != null) {
                arrowKeyHolderTask.cancel();
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.ESCAPE), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            PlayControl.active = false;
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.W), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (tooManyPartsToMove()) {
                moveWithKey(inputStates.getCurrent().getKeyboardState(), new Vector3(-1, 0, 0));
            } else {
                if (arrowKeyHolderTask != null) {
                    arrowKeyHolderTask.cancel();
                }
                arrowKeyHolderTask = new KeyHolderTask(inputStates.getCurrent().getKeyboardState(), new Vector3(-1, 0, 0));
                keyHolder.scheduleAtFixedRate(arrowKeyHolderTask, 0, keyHolderInterval);
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.W), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (arrowKeyHolderTask != null) {
                arrowKeyHolderTask.cancel();
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.E), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (tooManyPartsToMove()) {
                moveWithKey(inputStates.getCurrent().getKeyboardState(), new Vector3(1, 0, 0));
            } else {
                if (arrowKeyHolderTask != null) {
                    arrowKeyHolderTask.cancel();
                }
                arrowKeyHolderTask = new KeyHolderTask(inputStates.getCurrent().getKeyboardState(), new Vector3(1, 0, 0));
                keyHolder.scheduleAtFixedRate(arrowKeyHolderTask, 0, keyHolderInterval);
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.E), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (arrowKeyHolderTask != null) {
                arrowKeyHolderTask.cancel();
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.S), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (tooManyPartsToMove()) {
                moveWithKey(inputStates.getCurrent().getKeyboardState(), new Vector3(0, -1, 0));
            } else {
                if (arrowKeyHolderTask != null) {
                    arrowKeyHolderTask.cancel();
                }
                arrowKeyHolderTask = new KeyHolderTask(inputStates.getCurrent().getKeyboardState(), new Vector3(0, -1, 0));
                keyHolder.scheduleAtFixedRate(arrowKeyHolderTask, 0, keyHolderInterval);
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.S), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (arrowKeyHolderTask != null) {
                arrowKeyHolderTask.cancel();
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.N), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (tooManyPartsToMove()) {
                moveWithKey(inputStates.getCurrent().getKeyboardState(), new Vector3(0, 1, 0));
            } else {
                if (arrowKeyHolderTask != null) {
                    arrowKeyHolderTask.cancel();
                }
                arrowKeyHolderTask = new KeyHolderTask(inputStates.getCurrent().getKeyboardState(), new Vector3(0, 1, 0));
                keyHolder.scheduleAtFixedRate(arrowKeyHolderTask, 0, keyHolderInterval);
            }
        }
    }));
    logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.N), new TriggerAction() {

        @Override
        public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
            if (arrowKeyHolderTask != null) {
                arrowKeyHolderTask.cancel();
            }
        }
    }));
}
Also used : TriggerAction(com.ardor3d.input.logical.TriggerAction) MouseButtonReleasedCondition(com.ardor3d.input.logical.MouseButtonReleasedCondition) Wall(org.concord.energy3d.model.Wall) KeyPressedCondition(com.ardor3d.input.logical.KeyPressedCondition) InputTrigger(com.ardor3d.input.logical.InputTrigger) MouseButtonPressedCondition(com.ardor3d.input.logical.MouseButtonPressedCondition) KeyReleasedCondition(com.ardor3d.input.logical.KeyReleasedCondition) MouseWheelListener(java.awt.event.MouseWheelListener) Ray3(com.ardor3d.math.Ray3) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) KeyboardState(com.ardor3d.input.KeyboardState) Foundation(org.concord.energy3d.model.Foundation) Component(java.awt.Component) PickedHousePart(org.concord.energy3d.model.PickedHousePart) HousePart(org.concord.energy3d.model.HousePart) PickedHousePart(org.concord.energy3d.model.PickedHousePart) Window(org.concord.energy3d.model.Window) MouseEvent(java.awt.event.MouseEvent) MouseMovedCondition(com.ardor3d.input.logical.MouseMovedCondition) Canvas(com.ardor3d.framework.Canvas) MouseAdapter(java.awt.event.MouseAdapter) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) MouseButtonClickedCondition(com.ardor3d.input.logical.MouseButtonClickedCondition) Door(org.concord.energy3d.model.Door) ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) MouseMotionAdapter(java.awt.event.MouseMotionAdapter) MouseWheelEvent(java.awt.event.MouseWheelEvent) ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) Vector2(com.ardor3d.math.Vector2) Spatial(com.ardor3d.scenegraph.Spatial) MouseState(com.ardor3d.input.MouseState) TwoInputStates(com.ardor3d.input.logical.TwoInputStates) PickResults(com.ardor3d.intersection.PickResults) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) KeyHeldCondition(com.ardor3d.input.logical.KeyHeldCondition)

Example 2 with ReadOnlyVector2

use of com.ardor3d.math.type.ReadOnlyVector2 in project energy3d by concord-consortium.

the class Roof method computeGableEditPoints.

private void computeGableEditPoints() {
    if (gableEditPointToWallMap == null) {
        return;
    }
    for (final int editPointIndex : gableEditPointToWallMap.keySet()) {
        // if (editPointIndex >= points.size()) {
        // gableEditPointToWallMap.clear();
        // break;
        // }
        final Vector3 editPoint = getAbsPoint(editPointIndex);
        final List<Wall> gableWalls = gableEditPointToWallMap.get(editPointIndex);
        final List<ReadOnlyVector3> wallPoints = new ArrayList<ReadOnlyVector3>(gableWalls.size() * 2);
        final List<ReadOnlyVector3> wallNormals = new ArrayList<ReadOnlyVector3>(gableWalls.size() * 2);
        for (final Wall wall : gableWalls) {
            addPointToPolygon(wall.getAbsPoint(0), wall.getNormal(), wallPoints, wallNormals);
            addPointToPolygon(wall.getAbsPoint(2), wall.getNormal(), wallPoints, wallNormals);
        }
        applyOverhang(wallPoints, wallNormals);
        if (gableWalls.size() == 1) {
            final ReadOnlyVector2 p2D = Util.snapToPolygon(editPoint, wallPoints, wallNormals);
            editPoint.setX(p2D.getX());
            editPoint.setY(p2D.getY());
        } else if (gableWalls.size() > 1) {
            final Vector3 p0 = gableWalls.get(0).getAbsPoint(0);
            final Wall secondWall = gableWalls.get(1);
            final ReadOnlyVector3 cornerPoint;
            final int cornerIndex;
            if (Util.isEqual(p0, secondWall.getAbsPoint(0)) || Util.isEqual(p0, secondWall.getAbsPoint(2))) {
                cornerIndex = 0;
            } else {
                cornerIndex = 1;
            }
            cornerPoint = wallPoints.get(cornerIndex).subtract(wallNormals.get(cornerIndex).multiply(0.01, null), null);
            editPoint.setX(cornerPoint.getX());
            editPoint.setY(cornerPoint.getY());
        }
        points.get(editPointIndex).set(toRelative(editPoint));
    }
}
Also used : ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) ArrayList(java.util.ArrayList) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) CullHint(com.ardor3d.scenegraph.hint.CullHint) TPoint(org.poly2tri.triangulation.point.TPoint) TriangulationPoint(org.poly2tri.triangulation.TriangulationPoint) PolygonPoint(org.poly2tri.geometry.polygon.PolygonPoint)

Example 3 with ReadOnlyVector2

use of com.ardor3d.math.type.ReadOnlyVector2 in project energy3d by concord-consortium.

the class Roof method ensureEditPointsInside.

private void ensureEditPointsInside() {
    for (int i = 1; i < points.size(); i++) {
        final Vector3 editPoint = getAbsPoint(i);
        final Vector2 p = new Vector2(editPoint.getX(), editPoint.getY());
        if (!insideWallsPolygon(editPoint)) {
            double closestDistance = Double.MAX_VALUE;
            int closestIndex = 0;
            for (int j = 0; j < wallUpperPoints.size(); j++) {
                final Vector2 l1 = new Vector2(wallUpperPoints.get(j).getX(), wallUpperPoints.get(j).getY());
                final Vector2 l2 = new Vector2(wallUpperPoints.get((j + 1) % wallUpperPoints.size()).getX(), wallUpperPoints.get((j + 1) % wallUpperPoints.size()).getY());
                final double distance = p.distance(l1) + p.distance(l2);
                if (distance < closestDistance) {
                    closestDistance = distance;
                    closestIndex = j;
                }
            }
            final List<ReadOnlyVector3> wallPoints = new ArrayList<ReadOnlyVector3>(2);
            wallPoints.add(wallUpperPoints.get(closestIndex));
            wallPoints.add(wallUpperPoints.get((closestIndex + 1) % wallUpperPoints.size()));
            final ReadOnlyVector2 p2D = Util.snapToPolygon(editPoint, wallPoints, null);
            editPoint.setX(p2D.getX());
            editPoint.setY(p2D.getY());
            if (closestIndex < walls.size()) {
                editPoint.subtractLocal(walls.get(closestIndex).getNormal().multiply(0.01, null));
            }
            points.get(i).set(toRelative(editPoint));
        }
    }
}
Also used : ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) Vector2(com.ardor3d.math.Vector2) ArrayList(java.util.ArrayList) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) CullHint(com.ardor3d.scenegraph.hint.CullHint) TPoint(org.poly2tri.triangulation.point.TPoint) TriangulationPoint(org.poly2tri.triangulation.TriangulationPoint) PolygonPoint(org.poly2tri.geometry.polygon.PolygonPoint)

Example 4 with ReadOnlyVector2

use of com.ardor3d.math.type.ReadOnlyVector2 in project energy3d by concord-consortium.

the class SolarRadiation method initMeshTextureData.

private MeshDataStore initMeshTextureData(final Mesh drawMesh, final Mesh collisionMesh, final ReadOnlyVector3 normal, final boolean updateTexture) {
    final MeshDataStore data = new MeshDataStore();
    if (normal != null) {
        final AnyToXYTransform toXY = new AnyToXYTransform(normal.getX(), normal.getY(), normal.getZ());
        final XYToAnyTransform fromXY = new XYToAnyTransform(normal.getX(), normal.getY(), normal.getZ());
        final FloatBuffer vertexBuffer = collisionMesh.getMeshData().getVertexBuffer();
        vertexBuffer.rewind();
        double minX, minY, maxX, maxY;
        minX = minY = Double.POSITIVE_INFINITY;
        maxX = maxY = Double.NEGATIVE_INFINITY;
        double z = Double.NaN;
        final List<ReadOnlyVector2> points = new ArrayList<ReadOnlyVector2>(vertexBuffer.limit() / 3);
        while (vertexBuffer.hasRemaining()) {
            final Vector3 pWorld = drawMesh.localToWorld(new Vector3(vertexBuffer.get(), vertexBuffer.get(), vertexBuffer.get()), null);
            final Point p = new TPoint(pWorld.getX(), pWorld.getY(), pWorld.getZ());
            toXY.transform(p);
            if (p.getX() < minX) {
                minX = p.getX();
            }
            if (p.getX() > maxX) {
                maxX = p.getX();
            }
            if (p.getY() < minY) {
                minY = p.getY();
            }
            if (p.getY() > maxY) {
                maxY = p.getY();
            }
            if (Double.isNaN(z)) {
                z = p.getZ();
            }
            points.add(new Vector2(p.getX(), p.getY()));
        }
        final Point tmp = new TPoint(minX, minY, z);
        fromXY.transform(tmp);
        data.p0 = new Vector3(tmp.getX(), tmp.getY(), tmp.getZ());
        tmp.set(minX, maxY, z);
        fromXY.transform(tmp);
        data.p1 = new Vector3(tmp.getX(), tmp.getY(), tmp.getZ());
        tmp.set(maxX, minY, z);
        fromXY.transform(tmp);
        data.p2 = new Vector3(tmp.getX(), tmp.getY(), tmp.getZ());
        final double solarStep = Scene.getInstance().getSolarStep();
        data.rows = Math.max(1, (int) Math.ceil(data.p1.subtract(data.p0, null).length() / solarStep));
        data.cols = Math.max(1, (int) Math.ceil(data.p2.subtract(data.p0, null).length() / solarStep));
        data.dailySolarIntensity = new double[Util.roundToPowerOfTwo(data.rows)][Util.roundToPowerOfTwo(data.cols)];
        final ReadOnlyVector2 originXY = new Vector2(minX, minY);
        final ReadOnlyVector2 uXY = new Vector2(maxX - minX, 0).normalizeLocal();
        final ReadOnlyVector2 vXY = new Vector2(0, maxY - minY).normalizeLocal();
        final int nrow = data.dailySolarIntensity.length;
        final int ncol = data.dailySolarIntensity[0].length;
        for (int row = 0; row < nrow; row++) {
            for (int col = 0; col < ncol; col++) {
                if (row >= data.rows || col >= data.cols) {
                    // overshot cells
                    data.dailySolarIntensity[row][col] = -1;
                } else {
                    final ReadOnlyVector2 p = originXY.add(uXY.multiply(col * solarStep, null), null).add(vXY.multiply(row * solarStep, null), null);
                    boolean isInside = false;
                    final int numberOfPoints = points.size();
                    if (numberOfPoints >= 3) {
                        // FIXME: sometimes we can end up with less than three points
                        for (int i = 0; i < numberOfPoints; i += 3) {
                            if (i + 2 < points.size()) {
                                if (Util.isPointInsideTriangle(p, points.get(i), points.get(i + 1), points.get(i + 2))) {
                                    isInside = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!isInside && col > 0 && row > 0) {
                        // must at least include one column or row
                        data.dailySolarIntensity[row][col] = -1;
                    }
                }
            }
        }
        data.u = data.p2.subtract(data.p0, null).normalizeLocal();
        data.v = data.p1.subtract(data.p0, null).normalizeLocal();
        onMesh.put(drawMesh, data);
        if (updateTexture) {
            updateTextureCoords(drawMesh);
        }
    }
    return data;
}
Also used : AnyToXYTransform(org.poly2tri.transform.coordinate.AnyToXYTransform) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) TPoint(org.poly2tri.triangulation.point.TPoint) Point(org.poly2tri.geometry.primitives.Point) TPoint(org.poly2tri.triangulation.point.TPoint) CullHint(com.ardor3d.scenegraph.hint.CullHint) TPoint(org.poly2tri.triangulation.point.TPoint) Point(org.poly2tri.geometry.primitives.Point) ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) XYToAnyTransform(org.poly2tri.transform.coordinate.XYToAnyTransform) ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) Vector2(com.ardor3d.math.Vector2)

Example 5 with ReadOnlyVector2

use of com.ardor3d.math.type.ReadOnlyVector2 in project energy3d by concord-consortium.

the class TriangleMeshLib method createMeshes.

private static List<Mesh> createMeshes(final ArrayList<GroupData> groups) {
    final List<Mesh> results = new ArrayList<Mesh>();
    for (final GroupData group : groups) {
        final Mesh mesh = new Mesh();
        mesh.setUserData(group.key);
        mesh.setModelBound(new BoundingBox());
        results.add(mesh);
        final FloatBuffer vertexBuffer = BufferUtils.createVector3Buffer(group.vertices.size());
        mesh.getMeshData().setVertexBuffer(vertexBuffer);
        for (final ReadOnlyVector3 v : group.vertices) {
            vertexBuffer.put(v.getXf()).put(v.getYf()).put(v.getZf());
        }
        final FloatBuffer normalBuffer = BufferUtils.createFloatBuffer(vertexBuffer.limit());
        mesh.getMeshData().setNormalBuffer(normalBuffer);
        for (final ReadOnlyVector3 v : group.normals) {
            normalBuffer.put(v.getXf()).put(v.getYf()).put(v.getZf());
        }
        if (!group.textures.isEmpty()) {
            final FloatBuffer textureBuffer = BufferUtils.createVector2Buffer(group.textures.size());
            mesh.getMeshData().setTextureBuffer(textureBuffer, 0);
            for (final ReadOnlyVector2 v : group.textures) {
                textureBuffer.put(v.getXf()).put(v.getYf());
            }
            if (group.textureImage != null) {
                final Texture texture = TextureManager.loadFromImage(group.textureImage, Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessNoCompressedFormat);
                final TextureState ts = new TextureState();
                ts.setTexture(texture);
                mesh.setRenderState(ts);
            }
        }
        mesh.updateModelBound();
    }
    return results;
}
Also used : ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) TextureState(com.ardor3d.renderer.state.TextureState) BoundingBox(com.ardor3d.bounding.BoundingBox) ArrayList(java.util.ArrayList) Mesh(com.ardor3d.scenegraph.Mesh) FloatBuffer(java.nio.FloatBuffer) Texture(com.ardor3d.image.Texture) GroupData(org.concord.energy3d.util.MeshLib.GroupData)

Aggregations

ReadOnlyVector2 (com.ardor3d.math.type.ReadOnlyVector2)7 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)6 Vector3 (com.ardor3d.math.Vector3)5 ArrayList (java.util.ArrayList)5 Vector2 (com.ardor3d.math.Vector2)4 CullHint (com.ardor3d.scenegraph.hint.CullHint)3 TPoint (org.poly2tri.triangulation.point.TPoint)3 FloatBuffer (java.nio.FloatBuffer)2 PolygonPoint (org.poly2tri.geometry.polygon.PolygonPoint)2 TriangulationPoint (org.poly2tri.triangulation.TriangulationPoint)2 BoundingBox (com.ardor3d.bounding.BoundingBox)1 Canvas (com.ardor3d.framework.Canvas)1 Texture (com.ardor3d.image.Texture)1 KeyboardState (com.ardor3d.input.KeyboardState)1 MouseState (com.ardor3d.input.MouseState)1 InputTrigger (com.ardor3d.input.logical.InputTrigger)1 KeyHeldCondition (com.ardor3d.input.logical.KeyHeldCondition)1 KeyPressedCondition (com.ardor3d.input.logical.KeyPressedCondition)1 KeyReleasedCondition (com.ardor3d.input.logical.KeyReleasedCondition)1 MouseButtonClickedCondition (com.ardor3d.input.logical.MouseButtonClickedCondition)1