Search in sources :

Example 6 with MultiMap

use of org.twak.utils.collections.MultiMap 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)

Example 7 with MultiMap

use of org.twak.utils.collections.MultiMap in project chordatlas by twak.

the class FeatureCache method createMinis.

public MultiMap<MegaFeatures, MFPoint> createMinis(BlockGen blockGen) {
    MultiMap<MegaFeatures, MFPoint> out = new MultiMap();
    for (MegaFeatures m : getBlock(blockGen.center).getFeatures()) {
        double mLen = m.megafacade.length();
        for (ImageFeatures i : m.features) {
            for (int mi = 0; mi <= i.miniFacades.size(); mi++) {
                MiniFacade n = mi < i.miniFacades.size() ? i.miniFacades.get(mi) : null, p = (mi - 1) >= 0 ? i.miniFacades.get(mi - 1) : null;
                if (// skinny mf filter
                n != null && (n.width < MFWidthTol || (n.rects.countValue() == 0 && n.width < MFWidthTol * 3)))
                    n = null;
                if (p != null && (p.width < MFWidthTol || (p.rects.countValue() == 0 && p.width < MFWidthTol * 3)))
                    p = null;
                if (n == null && p == null || n == null && p.softRight || p == null && n.softLeft || p != null && n != null && p.softRight && n.softLeft)
                    continue;
                double ptDist = n == null ? (p.left + p.width) : n.left;
                Point2d pt = m.megafacade.fromPPram(ptDist / mLen);
                double covTol = 2;
                Set<ImageFeatures> covering = m.features.stream().filter(ii -> ii.start + covTol < ptDist && ii.end > ptDist + covTol).collect(Collectors.toSet());
                // stuff beyond the end of the facade
                double pa = m.megafacade.findPPram(pt) * mLen;
                if (pa < -5 || pa > mLen + 5)
                    continue;
                out.put(m, new MFPoint(pt, covering, m, i, p, n));
            }
        }
    }
    return out;
}
Also used : Color(java.awt.Color) XStream(com.thoughtworks.xstream.XStream) Arrays(java.util.Arrays) Rectangle2D(java.awt.geom.Rectangle2D) FacadeTool(org.twak.tweed.tools.FacadeTool) MiniFacade(org.twak.viewTrace.facades.MiniFacade) Tweed(org.twak.tweed.Tweed) ArrayList(java.util.ArrayList) TweedSettings(org.twak.tweed.TweedSettings) LinkedHashMap(java.util.LinkedHashMap) AlphaComposite(java.awt.AlphaComposite) PanMouseAdaptor(org.twak.utils.PanMouseAdaptor) Spatial(com.jme3.scene.Spatial) Graphics2D(java.awt.Graphics2D) Map(java.util.Map) ICanPaintU(org.twak.utils.PaintThing.ICanPaintU) ImageIO(javax.imageio.ImageIO) PaintThing(org.twak.utils.PaintThing) JComponent(javax.swing.JComponent) ICanPaint(org.twak.utils.PaintThing.ICanPaint) Composite(java.awt.Composite) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) MultiMap(org.twak.utils.collections.MultiMap) Line(org.twak.utils.Line) HalfEdge(org.twak.utils.geom.HalfMesh2.HalfEdge) Set(java.util.Set) IOException(java.io.IOException) JOptionPane(javax.swing.JOptionPane) Collectors(java.util.stream.Collectors) File(java.io.File) Point2d(javax.vecmath.Point2d) List(java.util.List) Paths(java.nio.file.Paths) JLabel(javax.swing.JLabel) YamlReader(com.esotericsoftware.yamlbeans.YamlReader) Optional(java.util.Optional) FileReader(java.io.FileReader) MultiMap(org.twak.utils.collections.MultiMap) MiniFacade(org.twak.viewTrace.facades.MiniFacade) Point2d(javax.vecmath.Point2d) ICanPaint(org.twak.utils.PaintThing.ICanPaint)

Example 8 with MultiMap

use of org.twak.utils.collections.MultiMap in project chordatlas by twak.

the class SkelFootprint method profileRuns.

private static void profileRuns(SuperLine sl, MultiMap<SuperLine, List<Prof>> profSets) {
    MegaFacade mf = sl.getMega();
    Cache<Integer, Double> distance2Next = new Cache<Integer, Double>() {

        @Override
        public Double create(Integer i) {
            Prof pc = mf.profiles.get(i), pn = mf.profiles.get(i + 1);
            if (pc == null || pn == null)
                return 1e6;
            return pc.distance(pn, true, false, false);
        }
    };
    // i -> mf.profiles.get( i ).distance( mf.profiles.get(i+1), true ));
    int start = mf.hExtentMin;
    for (int i = mf.hExtentMin; i < mf.hExtentMax; i++) {
        if (distance2Next.get(i) > 4 || i == mf.hExtentMax - 1) {
            // if ( (Math.random() > 0.95 || i == mf.hExtentMax - 1)  ){//0.5 / ProfileGen.HORIZ_SAMPLE_DIST) {
            if (i - start > 0.5 / TweedSettings.settings.profileHSampleDist) {
                List<Prof> lp = IntStream.range(start, i + 1).mapToObj(p -> mf.profiles.get(p)).filter(p -> p != null).collect(Collectors.toList());
                if (lp != null && !lp.isEmpty())
                    profSets.put(sl, lp);
            }
            start = i + 1;
        // i++;
        // }
        }
    }
// System.out.println( (mf.hExtentMax - mf.hExtentMin)+  " mm " + min+ " / " + max +" found " + profSets.size() );
}
Also used : Color(java.awt.Color) XStream(com.thoughtworks.xstream.XStream) Arrays(java.util.Arrays) FloatBuffer(java.nio.FloatBuffer) HalfFace(org.twak.utils.geom.HalfMesh2.HalfFace) Type(com.jme3.scene.VertexBuffer.Type) Arrayz(org.twak.utils.collections.Arrayz) Loop(org.twak.utils.collections.Loop) Node(com.jme3.scene.Node) SkelGen(org.twak.tweed.gen.skel.SkelGen) MutableDouble(org.twak.utils.MutableDouble) ColorRGBAPainter(org.twak.viewTrace.ColorRGBAPainter) Map(java.util.Map) Cache(org.twak.utils.Cache) Material(com.jme3.material.Material) ChangeListener(javax.swing.event.ChangeListener) Streamz(org.twak.utils.collections.Streamz) Point3d(javax.vecmath.Point3d) ChangeEvent(javax.swing.event.ChangeEvent) VertexBuffer(com.jme3.scene.VertexBuffer) LoopL(org.twak.utils.collections.LoopL) Predicate(java.util.function.Predicate) Line(org.twak.utils.Line) HalfEdge(org.twak.utils.geom.HalfMesh2.HalfEdge) Set(java.util.Set) HalfMesh2(org.twak.utils.geom.HalfMesh2) CollisionResult(com.jme3.collision.CollisionResult) Vector2d(javax.vecmath.Vector2d) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) List(java.util.List) JSlider(javax.swing.JSlider) Optional(java.util.Optional) Rainbow(org.twak.utils.ui.Rainbow) CollisionResults(com.jme3.collision.CollisionResults) Mesh(com.jme3.scene.Mesh) Geometry(com.jme3.scene.Geometry) IntStream(java.util.stream.IntStream) ActionListener(java.awt.event.ActionListener) DBSCANClusterer(org.apache.commons.math3.ml.clustering.DBSCANClusterer) Vector2f(com.jme3.math.Vector2f) HashMap(java.util.HashMap) MiniFacade(org.twak.viewTrace.facades.MiniFacade) Cach(org.twak.utils.Cach) Plot(org.twak.utils.ui.Plot) SwingConstants(javax.swing.SwingConstants) TreeSet(java.util.TreeSet) Clusterable(org.apache.commons.math3.ml.clustering.Clusterable) Tweed(org.twak.tweed.Tweed) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) ArrayList(java.util.ArrayList) TweedSettings(org.twak.tweed.TweedSettings) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Mathz(org.twak.utils.Mathz) PaintThing(org.twak.utils.PaintThing) NoSuchElementException(java.util.NoSuchElementException) ProgressMonitor(javax.swing.ProgressMonitor) LinkedHashSet(java.util.LinkedHashSet) MatParam(com.jme3.material.MatParam) JButton(javax.swing.JButton) Texture2D(com.jme3.texture.Texture2D) Iterator(java.util.Iterator) MultiMap(org.twak.utils.collections.MultiMap) BufferUtils(com.jme3.util.BufferUtils) Vector3f(com.jme3.math.Vector3f) MeshBuilder(org.twak.siteplan.jme.MeshBuilder) MegaFacade(org.twak.tweed.gen.ProfileGen.MegaFacade) ModeCollector(org.twak.viewTrace.ModeCollector) JOptionPane(javax.swing.JOptionPane) MegaFeatures(org.twak.tweed.gen.FeatureCache.MegaFeatures) ActionEvent(java.awt.event.ActionEvent) File(java.io.File) Loopz(org.twak.utils.collections.Loopz) Point2d(javax.vecmath.Point2d) Jme3z(org.twak.siteplan.jme.Jme3z) Ray(com.jme3.math.Ray) SuperLine(org.twak.viewTrace.SuperLine) LineHeight(org.twak.viewTrace.facades.LineHeight) Format(com.jme3.scene.VertexBuffer.Format) Cluster(org.apache.commons.math3.ml.clustering.Cluster) ColorRGBA(com.jme3.math.ColorRGBA) FileReader(java.io.FileReader) ImageRaster(com.jme3.texture.image.ImageRaster) Comparator(java.util.Comparator) Collections(java.util.Collections) MegaFacade(org.twak.tweed.gen.ProfileGen.MegaFacade) MutableDouble(org.twak.utils.MutableDouble) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) Cache(org.twak.utils.Cache)

Aggregations

MultiMap (org.twak.utils.collections.MultiMap)8 List (java.util.List)6 ArrayList (java.util.ArrayList)5 Line (org.twak.utils.Line)5 Point2d (javax.vecmath.Point2d)4 Color (java.awt.Color)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Point3d (javax.vecmath.Point3d)3 Loop (org.twak.utils.collections.Loop)3 Material (com.jme3.material.Material)2 Geometry (com.jme3.scene.Geometry)2 XStream (com.thoughtworks.xstream.XStream)2 File (java.io.File)2 FileReader (java.io.FileReader)2 Arrays (java.util.Arrays)2 Optional (java.util.Optional)2 JOptionPane (javax.swing.JOptionPane)2