Search in sources :

Example 1 with Triangle

use of de.lmu.ifi.dbs.elki.math.geometry.SweepHullDelaunay2D.Triangle in project elki by elki-project.

the class VoronoiDraw method drawVoronoi.

/**
 * Draw a Voronoi diagram
 *
 * @param proj Projection
 * @param delaunay Delaunay triangulation
 * @param means Cluster means
 * @return SVG path
 */
public static SVGPath drawVoronoi(Projection2D proj, List<SweepHullDelaunay2D.Triangle> delaunay, List<double[]> means) {
    final SVGPath path = new SVGPath();
    CanvasSize viewport = proj.estimateViewport();
    for (int i = 0; i < delaunay.size(); i++) {
        SweepHullDelaunay2D.Triangle del = delaunay.get(i);
        final double[] projcx = proj.fastProjectDataToRenderSpace(del.m);
        if (del.ab > i) {
            Triangle oth = delaunay.get(del.ab);
            double[] p1 = projcx.clone(), p2 = proj.fastProjectDataToRenderSpace(oth.m);
            if (viewport.clipToMargin(p1, p2)) {
                path.moveTo(p1);
                path.drawTo(p2);
            }
        } else if (del.ab < 0) {
            double[] dirv = VMath.minus(means.get(del.a), means.get(del.b));
            VMath.rotate90Equals(dirv);
            double[] dir = proj.fastProjectRelativeDataToRenderSpace(dirv);
            final double factor = viewport.continueToMargin(projcx, dir);
            if (factor > 0) {
                path.moveTo(projcx);
                path.relativeLineTo(factor * dir[0], factor * dir[1]);
            }
        }
        if (del.bc > i) {
            Triangle oth = delaunay.get(del.bc);
            double[] p1 = projcx.clone(), p2 = proj.fastProjectDataToRenderSpace(oth.m);
            if (viewport.clipToMargin(p1, p2)) {
                path.moveTo(p1);
                path.drawTo(p2);
            }
        } else if (del.bc < 0) {
            double[] dirv = VMath.minus(means.get(del.b), means.get(del.c));
            VMath.rotate90Equals(dirv);
            double[] dir = proj.fastProjectRelativeDataToRenderSpace(dirv);
            final double factor = viewport.continueToMargin(projcx, dir);
            if (factor > 0) {
                path.moveTo(projcx);
                path.relativeLineTo(factor * dir[0], factor * dir[1]);
            }
        }
        if (del.ca > i) {
            Triangle oth = delaunay.get(del.ca);
            // No need to clone projcx here.
            double[] projca = proj.fastProjectDataToRenderSpace(oth.m);
            if (viewport.clipToMargin(projcx, projca)) {
                path.moveTo(projcx);
                path.drawTo(projca);
            }
        } else if (del.ca < 0) {
            double[] dirv = VMath.minus(means.get(del.c), means.get(del.a));
            VMath.rotate90Equals(dirv);
            double[] dir = proj.fastProjectRelativeDataToRenderSpace(dirv);
            final double factor = viewport.continueToMargin(projcx, dir);
            if (factor > 0) {
                path.moveTo(projcx);
                path.relativeLineTo(factor * dir[0], factor * dir[1]);
            }
        }
    }
    return path;
}
Also used : CanvasSize(de.lmu.ifi.dbs.elki.visualization.projections.CanvasSize) Triangle(de.lmu.ifi.dbs.elki.math.geometry.SweepHullDelaunay2D.Triangle) Triangle(de.lmu.ifi.dbs.elki.math.geometry.SweepHullDelaunay2D.Triangle) SweepHullDelaunay2D(de.lmu.ifi.dbs.elki.math.geometry.SweepHullDelaunay2D)

Aggregations

SweepHullDelaunay2D (de.lmu.ifi.dbs.elki.math.geometry.SweepHullDelaunay2D)1 Triangle (de.lmu.ifi.dbs.elki.math.geometry.SweepHullDelaunay2D.Triangle)1 CanvasSize (de.lmu.ifi.dbs.elki.visualization.projections.CanvasSize)1