Search in sources :

Example 1 with LinearForm

use of org.twak.utils.geom.LinearForm in project chordatlas by twak.

the class Concarnie method apply.

private List<Problem> apply(Problem problem, int count) {
    if (depth > 10 || problem.soup.isEmpty()) {
        problem.addPortal();
        return Collections.emptyList();
    }
    Set<Line> portals = new HashSet<>();
    Set<Line> in = new HashSet<>(problem.soup);
    for (Line sl : problem.hull) {
        RangeMerge<Line> rm = new RangeMerge<>(P.CON_TOL, P.CON_TOL);
        LinearForm lf = new LinearForm(sl);
        for (Line l : problem.soup) {
            if (onHull(sl, l)) {
                double pps = lf.findPParam(l.start), ppe = lf.findPParam(l.end);
                rm.add(pps, ppe, l);
                in.remove(l);
            }
        }
        List<Double> rmGet = rm.get();
        if (rmGet.isEmpty()) {
            if (!sl.start.equals(sl.end)) {
                if (Double.isNaN(sl.start.x))
                    System.out.println("help!");
                // whole thing is portal
                portals.add(sl);
            }
        } else {
            List<Point2d> occupied = new ArrayList();
            {
                double lf1 = lf.findPParam(sl.start), lf2 = lf.findPParam(sl.end);
                if (lf1 > lf2) {
                    double tmp = lf1;
                    lf1 = lf2;
                    lf2 = tmp;
                }
                for (double d : rmGet) {
                    d = Mathz.clamp(d, lf1, lf2);
                    occupied.add(lf.fromPParam(d));
                }
            }
            boolean onHull = false;
            {
                Point2d snapE = occupied.get(0), snapS = occupied.get(occupied.size() - 1);
                if (snapS.distance(sl.start) < P.CON_TOL)
                    snapS.set(sl.start);
                else {
                    occupied.add(new Point2d(sl.start));
                }
                if (snapE.distance(sl.end) < P.CON_TOL)
                    snapE.set(sl.end);
                else {
                    occupied.add(0, new Point2d(sl.end));
                    onHull = true;
                }
            }
            for (Pair<Point2d, Point2d> pair : new ConsecutiveItPairs<Point2d>(occupied)) {
                onHull = !onHull;
                if (pair.first().equals(pair.second()))
                    continue;
                Line line = new Line(pair.first(), pair.second());
                if (onHull) {
                    if (depth % 2 == 0)
                        line = line.reverse();
                    graph.add(line);
                } else {
                    portals.add(line.reverse());
                }
            }
        }
    }
    if (in.size() == problem.soup.size()) {
        // we didn't do anything! remove something, anything...
        List<Line> d = new ArrayList(in);
        Collections.sort(d, (a, b) -> Double.compare(a.length(), b.length()));
        for (// remove the shortest 1/3 lines
        int i = 0; // remove the shortest 1/3 lines
        i < Math.max(1, in.size() * 0.33); // remove the shortest 1/3 lines
        i++) in.remove(d.get(i));
    }
    List<Portal> mergedPortals = mergeConsecutive(portals);
    // assign each member of in to a portal
    MultiMapSet<Portal, Line> subproblems = new MultiMapSet();
    if (!mergedPortals.isEmpty()) {
        // O(n^3) closest-clique assignment
        MultiMapSet<Portal, Line> sub2 = new MultiMapSet();
        for (Portal p : mergedPortals) sub2.putAll(p, p.lines);
        while (!in.isEmpty()) {
            double bestDist = Double.MAX_VALUE;
            Portal bestP = null;
            Line bestL = null;
            for (Line l : in) for (Portal p : sub2.keySet()) for (Line sl : sub2.get(p)) {
                double dlsl = l.distance(sl);
                if (// ignore lines a long way away
                dlsl > Math.max(P.CON_TOL * 3, p.length * 0.5))
                    continue;
                double dist = dlsl + 0.1 * l.distance(p.summary);
                if (dist < bestDist) {
                    bestP = p;
                    bestDist = dist;
                    bestL = l;
                }
            }
            if (bestL == null)
                break;
            in.remove(bestL);
            double lenBestL = bestL.length();
            if (lenBestL > P.CON_TOL && lenBestL > 0.5 * bestP.summary.length()) {
                in.add(new Line(bestL.start, bestL.fromPPram(0.5)));
                in.add(new Line(bestL.fromPPram(0.5), bestL.end));
            } else {
                subproblems.put(bestP, bestL);
                sub2.put(bestP, bestL);
            }
        }
    } else {
        mergedPortals.add(null);
        subproblems.putAll(null, in);
    }
    return mergedPortals.stream().map(x -> new Problem(x == null ? Collections.emptyList() : x.lines, subproblems.get(x))).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) Location(org.apache.commons.math3.geometry.partitioning.Region.Location) ConsecutivePairs(org.twak.utils.collections.ConsecutivePairs) Segment(org.apache.commons.math3.geometry.euclidean.twod.Segment) ConvexHull2D(org.apache.commons.math3.geometry.euclidean.twod.hull.ConvexHull2D) MultiMapSet(org.twak.utils.collections.MultiMapSet) HashMap(java.util.HashMap) Graph2D(org.twak.utils.geom.Graph2D) Pair(org.twak.utils.Pair) ConsecutiveItPairs(org.twak.utils.collections.ConsecutiveItPairs) Vector2D(org.apache.commons.math3.geometry.euclidean.twod.Vector2D) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Loop(org.twak.utils.collections.Loop) Map(java.util.Map) Mathz(org.twak.utils.Mathz) PaintThing(org.twak.utils.PaintThing) Euclidean2D(org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D) MathIllegalArgumentException(org.apache.commons.math3.exception.MathIllegalArgumentException) LoopL(org.twak.utils.collections.LoopL) UnionWalker(org.twak.utils.geom.UnionWalker) Iterator(java.util.Iterator) Collection(java.util.Collection) Line(org.twak.utils.Line) Set(java.util.Set) MonotoneChain(org.apache.commons.math3.geometry.euclidean.twod.hull.MonotoneChain) Region(org.apache.commons.math3.geometry.partitioning.Region) Vector2d(javax.vecmath.Vector2d) LinearForm(org.twak.utils.geom.LinearForm) Collectors(java.util.stream.Collectors) Point2d(javax.vecmath.Point2d) List(java.util.List) Paint(java.awt.Paint) ItComb(org.twak.utils.collections.ItComb) Loopable(org.twak.utils.collections.Loopable) InsufficientDataException(org.apache.commons.math3.exception.InsufficientDataException) Anglez(org.twak.utils.geom.Anglez) Collections(java.util.Collections) ConsecutiveItPairs(org.twak.utils.collections.ConsecutiveItPairs) ArrayList(java.util.ArrayList) LinearForm(org.twak.utils.geom.LinearForm) Paint(java.awt.Paint) Line(org.twak.utils.Line) Point2d(javax.vecmath.Point2d) HashSet(java.util.HashSet) MultiMapSet(org.twak.utils.collections.MultiMapSet)

Example 2 with LinearForm

use of org.twak.utils.geom.LinearForm in project chordatlas by twak.

the class FindLines method regress.

private Line regress(Set<Line> things, LinearForm lfDir) {
    SimpleRegression fit = new SimpleRegression();
    // regression isn't happy on lines with infinite slope: so swap params!
    boolean flip = Math.abs(lfDir.x) > Math.abs(lfDir.y);
    for (Line l : things) if (flip) {
        fit.addData(l.start.y, l.start.x);
        fit.addData(l.end.y, l.end.x);
    } else {
        fit.addData(l.start.x, l.start.y);
        fit.addData(l.end.x, l.end.y);
    }
    double intercept = fit.getIntercept(), slope = fit.getSlope();
    if (Double.isNaN(intercept))
        return null;
    LinearForm lf;
    if (flip)
        lf = new LinearForm(1, -slope);
    else
        lf = new LinearForm(-slope, 1);
    if (lf.unitVector().angle(lfDir.unitVector()) > Math.PI / 2) {
        // if regression is pointing wrong way, flip
        lf.x = -lf.x;
        lf.y = -lf.y;
    }
    if (flip)
        lf.findC(intercept, 0);
    else
        lf.findC(0, intercept);
    double[] minMax = things.stream().map(x -> new double[] { lf.findPParam(x.start), lf.findPParam(x.end) }).collect(new InAxDoubleArray());
    // do regression
    return new Line(lf, minMax[0], minMax[1]);
}
Also used : Line(org.twak.utils.Line) Iterator(java.util.Iterator) Line(org.twak.utils.Line) Set(java.util.Set) MultiMapSet(org.twak.utils.collections.MultiMapSet) HashMap(java.util.HashMap) Vector2d(javax.vecmath.Vector2d) Pair(org.twak.utils.Pair) LinearForm(org.twak.utils.geom.LinearForm) ConsecutiveItPairs(org.twak.utils.collections.ConsecutiveItPairs) ArrayList(java.util.ArrayList) Point2d(javax.vecmath.Point2d) List(java.util.List) SimpleRegression(org.apache.commons.math3.stat.regression.SimpleRegression) Map(java.util.Map) Anglez(org.twak.utils.geom.Anglez) InAxDoubleArray(org.twak.utils.streams.InAxDoubleArray) Result(org.twak.viewTrace.RangeMerge.Result) InAxDoubleArray(org.twak.utils.streams.InAxDoubleArray) SimpleRegression(org.apache.commons.math3.stat.regression.SimpleRegression) LinearForm(org.twak.utils.geom.LinearForm)

Example 3 with LinearForm

use of org.twak.utils.geom.LinearForm in project chordatlas by twak.

the class Prof method parameterize.

public static // all profs from a single profile-edge are in the same 2D space, with strange x-origin
Prof parameterize(// all profs from a single profile-edge are in the same 2D space, with strange x-origin
List<Prof> in) {
    // double toProfileEdge;
    // {
    // Prof eg = in.iterator().next();
    // Point3d p = Pointz.to3( profileEdge.start );
    // toProfileEdge = eg.to2d( p ).x;
    // }
    double avgMinY = in.stream().filter(p -> p != null).mapToDouble(p -> p.get(0).y).average().getAsDouble();
    Set<Line> lines = new HashSet<>();
    for (Prof p : in) for (int i = 1; i < p.size(); i++) lines.add(new Line(p.get(i - 1), p.get(i)));
    SliceParameters P = new SliceParameters(5);
    P.FL_REGRESS = true;
    P.FL_BINS = 20;
    // double A = 0.4; // simple = 0.4
    // double B = 0.1; // simple = 1;
    // simple = 0.4
    double A = 0.2;
    // simple = 0.1;
    double B = 0.3;
    // simple = 0.1; rotate threshold, radians
    double C = 0.1;
    double firstFloorHeight = 2;
    P.MIN_LINES = Math.max(1, in.size() * A);
    lines = new FindLines(lines, P) {

        protected double nextAngle(Set<Line> remaining, int iteration) {
            double delta = Math.PI / P.FL_BINS;
            // angle bin
            Bin<Line> aBin = new Bin(-Math.PI - delta, Math.PI + delta, P.FL_BINS * 2, true);
            for (Line l : remaining) {
                double len = l.length();
                double angle = l.aTan2();
                aBin.add(angle, len, l);
            }
            // return MUtils.PI2;
            if (iteration < 1 && aBin.getWeight(Mathz.PI2) >= 10)
                return Mathz.PI2;
            int aBinI = aBin.maxI();
            return aBin.val(aBinI);
        }

        protected double getTolNearLine(Point2d p) {
            return P.FL_NEAR_LINE * (p.y < avgMinY + firstFloorHeight ? 5 : B);
        }

        protected double getTolNearLine2(Point2d p) {
            return P.FL_NEAR_LINE_2 * (p.y < avgMinY + firstFloorHeight ? 10 : B);
        }

        protected double getTolRemoveAngle(Line l) {
            return l.start.y < avgMinY + firstFloorHeight ? Math.PI * 0.5 : Math.PI * 0.2;
        }
    }.result.all;
    List<Line> llines = // is rubbish
    lines.stream().filter(l -> l.lengthSquared() > 0.001).filter(// is floor polygon
    l -> l.end.y > avgMinY + 1 || Math.abs(l.start.y - l.end.y) > 0.1).collect(Collectors.toList());
    Prof clean = new Prof(in.get(in.size() / 2));
    clean.clear();
    if (llines.isEmpty()) {
        clean.add(new Point2d(0, 0));
        clean.add(new Point2d(0, 1));
        return clean;
    }
    for (int i = 0; i < llines.size(); i++) {
        Line l = llines.get(i);
        double angle = l.aTan2();
        if (angle < Mathz.PI2 + C && angle > Mathz.PI2 - C)
            llines.set(i, FindLines.rotateToAngle(l, l.fromPPram(0.5), Mathz.PI2));
    }
    // llines.stream().filter( l -> l.start.y > l.end.y ).forEach( l -> l.reverseLocal() );
    Collections.sort(llines, new Comparator<Line>() {

        public int compare(Line o1, Line o2) {
            return Double.compare(o1.fromPPram(0.2).y, o2.fromPPram(0.2).y);
        }
    });
    // for (Line l : llines)
    // PaintThing.debug( new Color(170,0,255), 2f, new Line( l.start.x+5, -l.start.y, l.end.x+5, -l.end.y ) );
    Line lastL = null;
    Point2d lastP = new Point2d(0, -Double.MAX_VALUE);
    for (Line l : llines) {
        // if (c >= 6)
        // continue;
        // if ( c== 5)
        // System.out.println("here");
        // c++;
        Point2d mid = l.fromPPram(0.5);
        if (!(lastL != null && !lastL.isOnLeft(mid) || (lastP.y == -Double.MAX_VALUE || (mid.y >= lastP.y - 0.5 && mid.x <= lastP.x + 0.5))))
            continue;
        boolean startAbove = l.start.y >= lastP.y && l.start.x <= lastP.x, endAbove = l.end.y >= lastP.y && l.end.x <= lastP.x;
        if (l.end.y < l.start.y)
            l.end.y = l.start.y;
        if (startAbove && endAbove) {
        // okay
        } else {
            if (lastL != null && l.start.distanceSquared(lastP) < 9) {
                Point2d sec = lastL.intersects(l, false);
                if (sec != null && sec.distanceSquared(lastP) < 9 && sec.x <= lastL.start.x && sec.y >= lastL.start.y) {
                    clean.remove(clean.size() - 1);
                    clean.add(sec);
                    lastP = sec;
                    l.start = sec;
                } else if (l.start.x < lastP.x) {
                    sec = new LinearForm(new Vector2d(1, 0)).findC(l.start).intersect(new LinearForm(lastL));
                    if (sec != null && sec.distanceSquared(lastP) < 9) {
                        clean.remove(clean.size() - 1);
                        clean.add(sec);
                        lastP = sec;
                    }
                }
            }
            if (l.start.x > lastP.x + 0.01 || l.end.x > lastP.x + 0.01) {
                Point2d sec = new LinearForm(new Vector2d(0, 1)).findC(new Point2d(lastP.x, 0)).intersect(new LinearForm(l));
                if (sec != null && sec.distanceSquared(lastP) < 9 && sec.distanceSquared(l.start) < 9) {
                    if (l.start.x > lastP.x)
                        l.start = sec;
                    else
                        l.end = sec;
                }
            }
        }
        if (lastL != null && l.start.distanceSquared(lastP) < 4) {
            Point2d sec = lastL.intersects(l, false);
            if (sec != null && (sec.distanceSquared(lastP) < 4 || Math.abs(sec.y - lastP.y) < 1) && sec.x <= lastL.start.x && sec.y >= lastL.start.y) {
                clean.remove(clean.size() - 1);
                clean.add(sec);
                lastP = sec;
                l.start = sec;
            } else if (l.start.x < lastP.x) {
                sec = new LinearForm(new Vector2d(1, 0)).findC(l.start).intersect(new LinearForm(lastL));
                if (sec != null && (sec.distanceSquared(lastP) < 4 || Math.abs(sec.y - lastP.y) < 1)) {
                    clean.remove(clean.size() - 1);
                    clean.add(sec);
                    lastP = sec;
                // l.start = sec;
                }
            }
        }
        if (lastP.y - l.end.y < 3 && l.end.x - lastP.x < 3) {
            for (Point2d pt : l.points()) {
                pt.x = Math.min(pt.x, lastP.x);
                pt.y = Math.max(pt.y, lastP.y);
            }
            if (!l.start.equals(l.end))
                for (Point2d pt : l.points()) {
                    // if (c == 2)
                    // PaintThing.debug.put(1, new Point2d ( pt.x, -pt.y ) );
                    pt = new Point2d(pt);
                    pt.x = Math.min(pt.x, lastP.x);
                    pt.y = Mathz.max(0, pt.y, lastP.y);
                    if (clean.isEmpty() && pt.y > 0.2) {
                        clean.add(new Point2d(pt.x, 0));
                    }
                    if (lastP != null && pt.distanceSquared(lastP) > 0.02) {
                        clean.add(pt);
                    }
                    lastP = clean.get(clean.size() - 1);
                    if (clean.size() >= 3) {
                        Point2d a = clean.get(clean.size() - 1), b = clean.get(clean.size() - 2), c = clean.get(clean.size() - 3);
                        if (Math.abs(Mathz.area(c, b, a)) < 0.1 || Mathz.absAngleBetween(a, b, c) < 0.1)
                            clean.remove(clean.size() - 2);
                    }
                }
        }
        if (clean.size() >= 2)
            lastL = new Line(clean.get(clean.size() - 2), clean.get(clean.size() - 1));
    }
    return clean;
}
Also used : ConsecutivePairs(org.twak.utils.collections.ConsecutivePairs) Matrix4d(javax.vecmath.Matrix4d) ConsecutiveItPairs(org.twak.utils.collections.ConsecutiveItPairs) SliceParameters(org.twak.viewTrace.SliceParameters) Arrayz(org.twak.utils.collections.Arrayz) Node(com.jme3.scene.Node) ObjRead(org.twak.utils.geom.ObjRead) Map(java.util.Map) Cache(org.twak.utils.Cache) Material(com.jme3.material.Material) Point3d(javax.vecmath.Point3d) VertexBuffer(com.jme3.scene.VertexBuffer) IdentityHashMap(java.util.IdentityHashMap) Collection(java.util.Collection) Line(org.twak.utils.Line) FindLines(org.twak.viewTrace.FindLines) Set(java.util.Set) Vector2d(javax.vecmath.Vector2d) LinearForm(org.twak.utils.geom.LinearForm) Collectors(java.util.stream.Collectors) List(java.util.List) Rainbow(org.twak.utils.ui.Rainbow) Line3d(org.twak.utils.geom.Line3d) Mesh(com.jme3.scene.Mesh) Geometry(com.jme3.scene.Geometry) DBSCANClusterer(org.apache.commons.math3.ml.clustering.DBSCANClusterer) LinearForm3D(org.twak.utils.geom.LinearForm3D) Bin(org.twak.viewTrace.Bin) Pair(org.twak.utils.Pair) Vector3d(javax.vecmath.Vector3d) Clusterable(org.apache.commons.math3.ml.clustering.Clusterable) Tweed(org.twak.tweed.Tweed) ArrayList(java.util.ArrayList) TweedSettings(org.twak.tweed.TweedSettings) HashSet(java.util.HashSet) PanMouseAdaptor(org.twak.utils.PanMouseAdaptor) Graphics2D(java.awt.Graphics2D) ICanPaintU(org.twak.utils.PaintThing.ICanPaintU) Mathz(org.twak.utils.Mathz) PaintThing(org.twak.utils.PaintThing) Iterator(java.util.Iterator) Point2d(javax.vecmath.Point2d) SuperLine(org.twak.viewTrace.SuperLine) Cluster(org.apache.commons.math3.ml.clustering.Cluster) ColorRGBA(com.jme3.math.ColorRGBA) Comparator(java.util.Comparator) InAxDoubleArray(org.twak.utils.streams.InAxDoubleArray) Collections(java.util.Collections) ObjSlice(org.twak.viewTrace.ObjSlice) FindLines(org.twak.viewTrace.FindLines) Bin(org.twak.viewTrace.Bin) LinearForm(org.twak.utils.geom.LinearForm) Line(org.twak.utils.Line) SuperLine(org.twak.viewTrace.SuperLine) Vector2d(javax.vecmath.Vector2d) SliceParameters(org.twak.viewTrace.SliceParameters) Point2d(javax.vecmath.Point2d) HashSet(java.util.HashSet)

Example 4 with LinearForm

use of org.twak.utils.geom.LinearForm in project chordatlas by twak.

the class Prof method buildProfile.

public static Prof buildProfile(ObjRead mesh, Line3d oLine, Point3d cen, double minH, double maxH, double minD, double maxD, Tweed tweed, Node dbg) {
    Prof monotonic = buildProfile(oLine, cen);
    Vector3d dir = oLine.dir();
    dir.normalize();
    Vector3d sliceNormal = new Vector3d(dir.x, 0, dir.z);
    LinearForm3D lf = new LinearForm3D(sliceNormal, cen);
    List<Line3d> lines = ObjSlice.sliceTri(mesh, lf, 0.5, new Vector3d(-dir.z, 0, dir.x), Math.PI / 2 + 0.1);
    // dbg.attachChild( Jme3z.lines( tweed, lines, ColorRGBA.Blue, 2 ) );
    Line3d first = null;
    double closestStart = Double.MAX_VALUE;
    for (Line3d l : lines) {
        if (l.start.y > l.end.y)
            l.reverse();
        double dist = l.distanceSquared(cen);
        if (dist < closestStart) {
            closestStart = dist;
            first = l;
        }
    }
    if (first == null) {
        return null;
    // lines.clear();
    // monotonic.add( cen );
    // monotonic.add( new Point3d( cen.x, cen.y - 500, cen.z ) );
    } else {
        climb(lines, first, monotonic, maxH, true);
        climb(lines, first, monotonic, minH, false);
    }
    {
        double tol = 0.2;
        minD -= tol;
        maxD += tol;
        LinearForm min = new LinearForm(Mathz.UP).findC(new Point2d(minD, 0));
        LinearForm max = new LinearForm(Mathz.UP).findC(new Point2d(maxD, 0));
        for (int i = 0; i < monotonic.size() - 1; i++) {
            Point2d a = monotonic.get(i), b = monotonic.get(i + 1);
            if (a.x < minD && b.x < minD) {
                monotonic.remove(i);
                i--;
            } else if (a.x < minD) {
                monotonic.set(i, new LinearForm(new Line(a, b)).intersect(min));
            } else if (b.x < minD) {
                monotonic.set(i + 1, new LinearForm(new Line(a, b)).intersect(min));
                b.x = minD + Math.ulp(minD);
            }
            if (a.x > maxD && b.x > maxD) {
                monotonic.remove(i);
                i--;
            } else if (a.x > maxD) {
                monotonic.set(i, new LinearForm(new Line(a, b)).intersect(max));
            } else if (b.x > maxD) {
                monotonic.set(i + 1, new LinearForm(new Line(a, b)).intersect(max));
                b.x = maxD - Math.ulp(maxD);
            }
        }
    }
    return monotonic;
}
Also used : Line(org.twak.utils.Line) SuperLine(org.twak.viewTrace.SuperLine) Vector3d(javax.vecmath.Vector3d) Point2d(javax.vecmath.Point2d) LinearForm(org.twak.utils.geom.LinearForm) LinearForm3D(org.twak.utils.geom.LinearForm3D) Line3d(org.twak.utils.geom.Line3d)

Example 5 with LinearForm

use of org.twak.utils.geom.LinearForm in project chordatlas by twak.

the class CutHoles method cutHoles.

public static void cutHoles(LoopL<Point2d> out, double tol, Map<Point2d, Line> created) {
    MultiMap<Boolean, Loop<Point2d>> holeToLoop = new MultiMap<>();
    Iterator<Loop<Point2d>> lit = out.iterator();
    while (lit.hasNext()) {
        // a hole can be a backwards loop...
        Loop<Point2d> loop = lit.next();
        double area = Loopz.area(loop);
        if (Math.abs(area) < tol * tol)
            lit.remove();
        boolean isHole = area > 0;
        holeToLoop.put(isHole, loop);
        for (Loop<Point2d> h : loop.holes) {
            if (Loopz.area(h) > 0)
                h.reverse();
            holeToLoop.put(false, h);
        }
    }
    for (Loop<Point2d> hole : holeToLoop.get(false)) {
        Point2d origin = new Point2d(Double.MAX_VALUE, 0);
        Loopable<Point2d> originL = null;
        for (Loopable<Point2d> p : hole.loopableIterator()) {
            if (p.get().x < origin.x) {
                originL = p;
                origin = originL.get();
            }
        }
        LinearForm ray = new LinearForm(0, 1);
        ray.findC(origin);
        double nearestD = Double.MAX_VALUE;
        Loopable<Point2d> nearestL = null;
        Point2d nearestH = null;
        for (Loop<Point2d> loop : out) {
            for (Loopable<Point2d> line : loop.loopableIterator()) {
                Point2d a = line.get(), b = line.getNext().get();
                if (a.y > origin.y && b.y < origin.y || a.y < origin.y && b.y > origin.y) {
                    Point2d hit = new Line(a, b).intersects(ray);
                    if (hit != null && hit.x < origin.x && (origin.x - hit.x < nearestD)) {
                        nearestD = origin.x - hit.x;
                        nearestL = line;
                        nearestH = hit;
                    }
                }
            }
        }
        if (nearestH == null)
            System.err.println("failed to find outer ring for hole");
        else {
            if (created != null)
                created.put(nearestH, new Line(nearestL.get(), nearestL.getNext().get()));
            Loopable<Point2d> hitPtF = new Loopable<Point2d>(nearestH), hitPtS = new Loopable<Point2d>(nearestH), originL2 = new Loopable(origin);
            hitPtS.setNext(nearestL.getNext());
            hitPtF.setPrev(nearestL);
            hitPtS.getNext().setPrev(hitPtS);
            hitPtF.getPrev().setNext(hitPtF);
            originL.getNext().setPrev(originL2);
            originL2.setNext(originL.getNext());
            originL.setNext(hitPtS);
            hitPtS.setPrev(originL);
            hitPtF.setNext(originL2);
            originL2.setPrev(hitPtF);
        }
        out.remove(hole);
    }
}
Also used : Loop(org.twak.utils.collections.Loop) LinearForm(org.twak.utils.geom.LinearForm) Line(org.twak.utils.Line) Loopable(org.twak.utils.collections.Loopable) MultiMap(org.twak.utils.collections.MultiMap) Point2d(javax.vecmath.Point2d)

Aggregations

LinearForm (org.twak.utils.geom.LinearForm)10 Point2d (javax.vecmath.Point2d)9 Line (org.twak.utils.Line)9 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Map (java.util.Map)4 Set (java.util.Set)4 Vector2d (javax.vecmath.Vector2d)4 Vector3d (javax.vecmath.Vector3d)4 Pair (org.twak.utils.Pair)4 LinearForm3D (org.twak.utils.geom.LinearForm3D)4 Collection (java.util.Collection)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 Matrix4d (javax.vecmath.Matrix4d)3 Point3d (javax.vecmath.Point3d)3 Mathz (org.twak.utils.Mathz)3