Search in sources :

Example 1 with Polygon

use of org.poly2tri.geometry.polygon.Polygon in project energy3d by concord-consortium.

the class MeshLib method fillMeshWithPolygon.

// private static boolean isAlmost180(final ReadOnlyVector3 p1, final ReadOnlyVector3 p2, final ReadOnlyVector3 p3) {
// return Math.abs(p1.subtract(p2, null).normalizeLocal().smallestAngleBetween(p3.subtract(p1, null).normalizeLocal())) > Math.PI - Math.PI / 180.0;
// }
public static void fillMeshWithPolygon(final Mesh mesh, final PolygonWithHoles polygon, final CoordinateTransform fromXY, final boolean generateNormals, final TPoint o, final TPoint u, final TPoint v, final boolean isWall) {
    /* round all points */
    for (final Point p : polygon.getPoints()) {
        p.set(Util.round(p.getX()), Util.round(p.getY()), Util.round(p.getZ()));
    }
    if (polygon.getHoles() != null) {
        for (final Polygon hole : polygon.getHoles()) {
            for (final Point p : hole.getPoints()) {
                p.set(Util.round(p.getX()), Util.round(p.getY()), Util.round(p.getZ()));
            }
        }
    }
    /* remove holes that collide with polygon or other holes */
    if (polygon.getHoles() != null) {
        // ensure polygon doesn't collide with holes
        final Path2D polygonPath = Util.makePath2D(polygon.getPoints());
        final Map<Polygon, Object> skipHoles = new HashMap<Polygon, Object>();
        for (final Polygon hole : polygon.getHoles()) {
            for (final Point p : hole.getPoints()) {
                if (!polygonPath.contains(new Point2D.Double(p.getX(), p.getY()))) {
                    skipHoles.put(hole, null);
                    break;
                }
            }
        }
        // ensure holes don't collide with each other
        for (int i = 0; i < polygon.getHoles().size(); i++) {
            final Polygon hole1 = polygon.getHoles().get(i);
            if (skipHoles.containsKey(hole1)) {
                continue;
            }
            for (int j = i + 1; j < polygon.getHoles().size(); j++) {
                final Polygon hole2 = polygon.getHoles().get(j);
                if (skipHoles.containsKey(hole2)) {
                    continue;
                }
                boolean found = false;
                for (final Point p : hole2.getPoints()) {
                    if (Util.insidePolygon(p, hole1.getPoints())) {
                        skipHoles.put(hole2, null);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    final int n1 = hole1.getPoints().size();
                    for (int i1 = 0; i1 < n1; i1++) {
                        final Point l1p1 = hole1.getPoints().get(i1);
                        final Point l1p2 = hole1.getPoints().get((i1 + 1) % n1);
                        final Line2D line1 = new Line2D.Double(l1p1.getX(), l1p1.getY(), l1p2.getX(), l1p2.getY());
                        found = false;
                        final int n2 = hole2.getPoints().size();
                        for (int i2 = 0; i2 < n2; i2++) {
                            final Point l2p1 = hole2.getPoints().get(i2);
                            final Point l2p2 = hole2.getPoints().get((i2 + 1) % n2);
                            final Line2D line2 = new Line2D.Double(l2p1.getX(), l2p1.getY(), l2p2.getX(), l2p2.getY());
                            if (line2.intersectsLine(line1)) {
                                skipHoles.put(hole2, null);
                                found = true;
                                break;
                            }
                        }
                        if (found) {
                            break;
                        }
                    }
                }
            }
        }
        for (final Polygon hole : skipHoles.keySet()) {
            polygon.getHoles().remove(hole);
        }
    }
    try {
        Poly2Tri.triangulate(polygon);
        if (fromXY == null) {
            ArdorMeshMapper.updateTriangleMesh(mesh, polygon);
        } else {
            ArdorMeshMapper.updateTriangleMesh(mesh, polygon, fromXY);
        }
        if (generateNormals) {
            if (fromXY == null) {
                ArdorMeshMapper.updateFaceNormals(mesh, polygon.getTriangles());
            } else {
                ArdorMeshMapper.updateFaceNormals(mesh, polygon.getTriangles(), fromXY);
            }
        }
        if (o != null) {
            ArdorMeshMapper.updateTextureCoordinates(mesh, polygon.getTriangles(), 1.0, o, u, v);
        }
        mesh.getMeshData().updateVertexCount();
        mesh.updateModelBound();
    } catch (final RuntimeException e) {
        System.err.println("Points:");
        for (final Point p : polygon.getPoints()) {
            System.err.println(p);
        }
        System.err.println("Holes:");
        if (polygon.getHoles() != null) {
            for (final Polygon hole : polygon.getHoles()) {
                for (final Point p : hole.getPoints()) {
                    System.err.println(p);
                }
            }
        }
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) Path2D(java.awt.geom.Path2D) PolygonPoint(org.poly2tri.geometry.polygon.PolygonPoint) TPoint(org.poly2tri.triangulation.point.TPoint) Point(org.poly2tri.geometry.primitives.Point) Polygon(org.poly2tri.geometry.polygon.Polygon) Line2D(java.awt.geom.Line2D) CullHint(com.ardor3d.scenegraph.hint.CullHint) PolygonPoint(org.poly2tri.geometry.polygon.PolygonPoint) TPoint(org.poly2tri.triangulation.point.TPoint) Point(org.poly2tri.geometry.primitives.Point)

Example 2 with Polygon

use of org.poly2tri.geometry.polygon.Polygon in project energy3d by concord-consortium.

the class Wall method drawPolygon.

private void drawPolygon(final List<List<Vector3>> wallAndWindowsPoints, final Mesh mesh, final boolean drawHoles, final boolean normal, final boolean texture) {
    final List<PolygonPoint> polygonPoints = new ArrayList<PolygonPoint>(wallAndWindowsPoints.get(0).size());
    for (final Vector3 p : wallAndWindowsPoints.get(0)) {
        final PolygonPoint tp = new PolygonPoint(p.getX(), p.getY(), p.getZ());
        toXY.transform(tp);
        polygonPoints.add(tp);
    }
    final PolygonWithHoles polygon = new PolygonWithHoles(polygonPoints);
    if (drawHoles) {
        for (int i = 1; i < wallAndWindowsPoints.size(); i++) {
            final List<PolygonPoint> holePoints = new ArrayList<PolygonPoint>(wallAndWindowsPoints.get(i).size());
            for (final Vector3 p : wallAndWindowsPoints.get(i)) {
                final PolygonPoint tp = new PolygonPoint(p.getX(), p.getY(), p.getZ());
                toXY.transform(tp);
                holePoints.add(tp);
            }
            polygon.addHole(new Polygon(holePoints));
        }
    }
    if (texture) {
        final double scale = Scene.getInstance().getTextureMode() == TextureMode.Simple ? 1.0 : 8.0;
        final boolean fullTexture = Scene.getInstance().getTextureMode() == TextureMode.Full;
        final ReadOnlyVector3 p0 = getAbsPoint(0);
        final ReadOnlyVector3 p01 = getAbsPoint(1).subtractLocal(p0).normalizeLocal().multiplyLocal(scale * (fullTexture ? 1.5 : 1.0));
        final ReadOnlyVector3 p02 = getAbsPoint(2).subtractLocal(p0).normalizeLocal().multiplyLocal(scale * (fullTexture ? 2.0 : 1.0));
        final TPoint o = new TPoint(p0.getX(), p0.getY(), p0.getZ());
        final TPoint u = new TPoint(p01.getX(), p01.getY(), p01.getZ());
        final TPoint v = new TPoint(p02.getX(), p02.getY(), p02.getZ());
        toXY.transform(o);
        toXY.transform(u);
        MeshLib.fillMeshWithPolygon(mesh, polygon, fromXY, normal, o, u, v, true);
    } else {
        MeshLib.fillMeshWithPolygon(mesh, polygon, fromXY, normal, null, null, null, true);
    }
}
Also used : ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) PolygonPoint(org.poly2tri.geometry.polygon.PolygonPoint) ArrayList(java.util.ArrayList) PolygonWithHoles(org.concord.energy3d.util.PolygonWithHoles) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) Polygon(org.poly2tri.geometry.polygon.Polygon) 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) TPoint(org.poly2tri.triangulation.point.TPoint)

Aggregations

CullHint (com.ardor3d.scenegraph.hint.CullHint)2 Polygon (org.poly2tri.geometry.polygon.Polygon)2 PolygonPoint (org.poly2tri.geometry.polygon.PolygonPoint)2 Point (org.poly2tri.geometry.primitives.Point)2 TPoint (org.poly2tri.triangulation.point.TPoint)2 Vector3 (com.ardor3d.math.Vector3)1 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)1 PickingHint (com.ardor3d.scenegraph.hint.PickingHint)1 Line2D (java.awt.geom.Line2D)1 Path2D (java.awt.geom.Path2D)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 PolygonWithHoles (org.concord.energy3d.util.PolygonWithHoles)1 ArdorVector3Point (org.poly2tri.triangulation.point.ardor3d.ArdorVector3Point)1