Search in sources :

Example 6 with Vector2

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

the class Roof method isSameBasePoints.

public boolean isSameBasePoints(final ReadOnlyVector3 a1, final ReadOnlyVector3 a2, final ReadOnlyVector3 b1, final ReadOnlyVector3 b2) {
    final double maxOverhangDistance = MathUtils.sqrt(2 * overhangLength * overhangLength) * 2;
    final Vector2 p1a = new Vector2(a1.getX(), a1.getY());
    final Vector2 p1b = new Vector2(a2.getX(), a2.getY());
    final Vector2 p2a = new Vector2(b1.getX(), b1.getY());
    final Vector2 p2b = new Vector2(b2.getX(), b2.getY());
    return (p1a.distance(p2a) <= maxOverhangDistance && p1b.distance(p2b) <= maxOverhangDistance) || (p1a.distance(p2b) <= maxOverhangDistance && p1b.distance(p2a) <= maxOverhangDistance);
}
Also used : ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) Vector2(com.ardor3d.math.Vector2)

Example 7 with Vector2

use of com.ardor3d.math.Vector2 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 8 with Vector2

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

the class Wall method findClosestPointOnFoundation.

public Vector3 findClosestPointOnFoundation(final int x, final int y) {
    final PickedHousePart floorPick = SelectUtil.pickPart(x, y, (HousePart) null);
    if (floorPick != null) {
        final int[] lines = { 0, 1, 3, 2, 0 };
        final Vector3 p = floorPick.getPoint().clone();
        final Vector2 p_2d = new Vector2(p.getX(), p.getY());
        Vector2 newP_2d = null;
        for (int i = 0; i < lines.length - 1; i++) {
            final Vector3 p1 = container.points.get(lines[i]);
            final Vector3 p2 = container.points.get(lines[i + 1]);
            final Vector2 p1_2d = new Vector2(p1.getX(), p1.getY());
            final Vector2 p2_2d = new Vector2(p2.getX(), p2.getY());
            final Vector2 result = Util.projectPointOnLine(p_2d, p1_2d, p2_2d, true);
            if (newP_2d == null || newP_2d.distance(p_2d) > result.distance(p_2d)) {
                newP_2d = result;
            }
        }
        p.setX(newP_2d.getX());
        p.setY(newP_2d.getY());
        return p;
    } else {
        return null;
    }
}
Also used : Vector2(com.ardor3d.math.Vector2) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) CullHint(com.ardor3d.scenegraph.hint.CullHint) PolygonPoint(org.poly2tri.geometry.polygon.PolygonPoint) ArdorVector3Point(org.poly2tri.triangulation.point.ardor3d.ArdorVector3Point) PickingHint(com.ardor3d.scenegraph.hint.PickingHint) TPoint(org.poly2tri.triangulation.point.TPoint) Point(org.poly2tri.geometry.primitives.Point)

Example 9 with Vector2

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

the class MeshLib method applyHoles.

public static void applyHoles(final Node root, final List<Window> windows) {
    final Map<Window, List<ReadOnlyVector3>> holes = new HashMap<Window, List<ReadOnlyVector3>>();
    for (final Window window : windows) {
        final ArrayList<ReadOnlyVector3> hole = new ArrayList<ReadOnlyVector3>();
        hole.add(window.getAbsPoint(0).multiplyLocal(1, 1, 0));
        hole.add(window.getAbsPoint(2).multiplyLocal(1, 1, 0));
        hole.add(window.getAbsPoint(3).multiplyLocal(1, 1, 0));
        hole.add(window.getAbsPoint(1).multiplyLocal(1, 1, 0));
        holes.put(window, hole);
    }
    for (int roofIndex = 0; roofIndex < root.getChildren().size(); roofIndex++) {
        final Spatial roofPart = root.getChildren().get(roofIndex);
        if (roofPart.getSceneHints().getCullHint() != CullHint.Always) {
            final ReadOnlyVector3 normal = (ReadOnlyVector3) roofPart.getUserData();
            final AnyToXYTransform toXY = new AnyToXYTransform(normal.getX(), normal.getY(), normal.getZ());
            final XYToAnyTransform fromXY = new XYToAnyTransform(normal.getX(), normal.getY(), normal.getZ());
            final Mesh mesh = (Mesh) ((Node) roofPart).getChild(0);
            final ArrayList<ReadOnlyVector3> points3D = computeOutline(mesh.getMeshData().getVertexBuffer());
            final List<PolygonPoint> points2D = new ArrayList<PolygonPoint>();
            final ReadOnlyVector3 firstPoint = points3D.get(0);
            final double scale = Scene.getInstance().getTextureMode() == TextureMode.Simple ? 0.5 : 0.1;
            final TPoint o;
            final TPoint u;
            final TPoint v;
            if (normal.dot(Vector3.UNIT_Z) == 1) {
                o = new TPoint(firstPoint.getX(), firstPoint.getY(), firstPoint.getZ());
                u = new TPoint(1 / scale, 0, 0);
                v = new TPoint(0, 1 / scale, 0);
            } else {
                final ReadOnlyVector3 u3 = Vector3.UNIT_Z.cross(normal, null).normalizeLocal();
                final ReadOnlyVector3 ou3 = u3.divide(scale, null).add(firstPoint, null);
                final ReadOnlyVector3 ov3 = normal.cross(u3, null).divideLocal(scale).addLocal(firstPoint);
                o = new TPoint(firstPoint.getX(), firstPoint.getY(), firstPoint.getZ());
                u = new TPoint(ou3.getX(), ou3.getY(), ou3.getZ());
                v = new TPoint(ov3.getX(), ov3.getY(), ov3.getZ());
                toXY.transform(o);
                toXY.transform(u);
                toXY.transform(v);
                u.set(u.getX() - o.getX(), u.getY() - o.getY(), 0);
                v.set(v.getX() - o.getX(), v.getY() - o.getY(), 0);
            }
            final Vector2 o2 = new Vector2(firstPoint.getX(), firstPoint.getY());
            final Vector2 ou2 = o2.add(new Vector2(u.getX(), u.getY()), null);
            final Vector2 ov2 = o2.add(new Vector2(v.getX(), v.getY()), null);
            double minLineScaleU = Double.MAX_VALUE;
            double minLineScaleV = Double.MAX_VALUE;
            for (final ReadOnlyVector3 p : points3D) {
                final PolygonPoint polygonPoint = new PolygonPoint(p.getX(), p.getY(), p.getZ());
                toXY.transform(polygonPoint);
                points2D.add(polygonPoint);
                final Vector2 p2 = new Vector2(polygonPoint.getX(), polygonPoint.getY());
                final double lineScaleU = Util.projectPointOnLineScale(p2, o2, ou2);
                final double lineScaleV = Util.projectPointOnLineScale(p2, o2, ov2);
                if (lineScaleU < minLineScaleU) {
                    minLineScaleU = lineScaleU;
                }
                if (lineScaleV < minLineScaleV) {
                    minLineScaleV = lineScaleV;
                }
            }
            o2.addLocal(new Vector2(u.getX(), u.getY()).multiplyLocal(minLineScaleU));
            o2.addLocal(new Vector2(v.getX(), v.getY()).multiplyLocal(minLineScaleV));
            final PolygonWithHoles polygon = new PolygonWithHoles(points2D);
            o.set(o2.getX(), o2.getY(), 0);
            roofPart.updateWorldBound(true);
            for (final Window window : windows) {
                if (holes.get(window) == null) {
                    continue;
                }
                final List<PolygonPoint> holePolygon = new ArrayList<PolygonPoint>();
                boolean outside = false;
                for (final ReadOnlyVector3 holePoint : holes.get(window)) {
                    final PickResults pickResults = new PrimitivePickResults();
                    PickingUtil.findPick(((Node) roofPart).getChild(0), new Ray3(holePoint, Vector3.UNIT_Z), pickResults, false);
                    if (pickResults.getNumber() > 0) {
                        final ReadOnlyVector3 intersectionPoint = pickResults.getPickData(0).getIntersectionRecord().getIntersectionPoint(0);
                        final PolygonPoint polygonPoint = new PolygonPoint(intersectionPoint.getX(), intersectionPoint.getY(), intersectionPoint.getZ());
                        toXY.transform(polygonPoint);
                        holePolygon.add(polygonPoint);
                    } else {
                        outside = true;
                        break;
                    }
                }
                if (!outside) {
                    polygon.addHole(new PolygonWithHoles(holePolygon));
                    holes.remove(window);
                    window.setRoofIndex(roofIndex);
                }
            }
            final Mesh meshWithHoles = (Mesh) ((Node) roofPart).getChild(6);
            try {
                fillMeshWithPolygon(meshWithHoles, polygon, fromXY, true, o, v, u, false);
            } catch (final RuntimeException e) {
                e.printStackTrace();
                final Mesh meshWithoutHoles = (Mesh) ((Node) roofPart).getChild(0);
                meshWithHoles.setMeshData(meshWithoutHoles.getMeshData());
            }
        }
    }
}
Also used : Window(org.concord.energy3d.model.Window) AnyToXYTransform(org.poly2tri.transform.coordinate.AnyToXYTransform) HashMap(java.util.HashMap) Node(com.ardor3d.scenegraph.Node) ArrayList(java.util.ArrayList) PolygonPoint(org.poly2tri.geometry.polygon.PolygonPoint) Mesh(com.ardor3d.scenegraph.Mesh) CullHint(com.ardor3d.scenegraph.hint.CullHint) PolygonPoint(org.poly2tri.geometry.polygon.PolygonPoint) TPoint(org.poly2tri.triangulation.point.TPoint) Point(org.poly2tri.geometry.primitives.Point) TPoint(org.poly2tri.triangulation.point.TPoint) Ray3(com.ardor3d.math.Ray3) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) XYToAnyTransform(org.poly2tri.transform.coordinate.XYToAnyTransform) Spatial(com.ardor3d.scenegraph.Spatial) ReadOnlyVector2(com.ardor3d.math.type.ReadOnlyVector2) Vector2(com.ardor3d.math.Vector2) ArrayList(java.util.ArrayList) List(java.util.List) PrimitivePickResults(com.ardor3d.intersection.PrimitivePickResults) PickResults(com.ardor3d.intersection.PickResults)

Example 10 with Vector2

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

the class SelectUtil method pickPart.

public static PickedHousePart pickPart(final int x, final int y, final HousePart housePart) {
    pickResults.clear();
    final Ray3 pickRay = SceneManager.getInstance().getCamera().getPickRay(new Vector2(x, y), false, null);
    if (housePart == null) {
        PickingUtil.findPick(SceneManager.getInstance().getLand(), pickRay, pickResults, false);
    } else {
        PickingUtil.findPick(housePart.getCollisionSpatial(), pickRay, pickResults, false);
    }
    final PickedHousePart picked = getPickResultForImportedMesh();
    if (picked != null) {
        return picked;
    }
    return getPickResult(pickRay);
}
Also used : Vector2(com.ardor3d.math.Vector2) Ray3(com.ardor3d.math.Ray3) PickedHousePart(org.concord.energy3d.model.PickedHousePart)

Aggregations

Vector2 (com.ardor3d.math.Vector2)23 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)15 Vector3 (com.ardor3d.math.Vector3)12 ReadOnlyVector2 (com.ardor3d.math.type.ReadOnlyVector2)10 Ray3 (com.ardor3d.math.Ray3)9 CullHint (com.ardor3d.scenegraph.hint.CullHint)8 TPoint (org.poly2tri.triangulation.point.TPoint)7 PolygonPoint (org.poly2tri.geometry.polygon.PolygonPoint)6 Point (org.poly2tri.geometry.primitives.Point)6 ArrayList (java.util.ArrayList)5 PickingHint (com.ardor3d.scenegraph.hint.PickingHint)4 HousePart (org.concord.energy3d.model.HousePart)4 PickedHousePart (org.concord.energy3d.model.PickedHousePart)4 PickResults (com.ardor3d.intersection.PickResults)3 PrimitivePickResults (com.ardor3d.intersection.PrimitivePickResults)3 Mesh (com.ardor3d.scenegraph.Mesh)3 Node (com.ardor3d.scenegraph.Node)3 Spatial (com.ardor3d.scenegraph.Spatial)3 FloatBuffer (java.nio.FloatBuffer)3 Canvas (com.ardor3d.framework.Canvas)2