Search in sources :

Example 6 with MegaFeatures

use of org.twak.tweed.gen.FeatureCache.MegaFeatures in project chordatlas by twak.

the class SolverState method miniPainter.

public ICanPaint miniPainter() {
    return new ICanPaint() {

        @Override
        public void paint(Graphics2D g, PanMouseAdaptor ma) {
            // for ( MegaFeatures f : SS.minis.keySet() ) {
            // 
            // for ( MFPoint mfp : SS.minis.get( f ) ) {
            // Line l = mfp.image.mega.megafacade;
            // 
            // spreadImages( g, ma, l, mfp,  Listz.from( mfp.left, mfp.right ) );
            // 
            // }
            // }
            // if (SS.minis == null)
            int brake = 100;
            for (HalfFace f : mesh) {
                for (HalfEdge e : f) {
                    if (e.over != null)
                        continue;
                    if (brake-- < 0)
                        break;
                    SuperEdge se = (SuperEdge) e;
                    if (se.mini == null)
                        continue;
                    List<MiniFacade> mfs = new ArrayList(se.mini);
                    // while (mfs .size() < 2)
                    // mfs.add(null);
                    spreadImages(g, ma, se.line(), se.line().fromPPram(0.5), mfs);
                }
            }
            int i = 0;
            if (minis != null)
                for (MegaFeatures f : minis.keySet()) {
                    // PaintThing.paint (f.megafacade, g, ma);
                    DumbCluster1D<MFPoint> res = GurobiSkelSolver.clusterMinis(f, minis);
                    Vector2d dir = f.megafacade.dir();
                    Vector2d out = new Vector2d(dir);
                    out.set(-out.y, out.x);
                    out.scale(ma.toZoom(2) / out.length());
                    for (Cluster<MFPoint> c : res) {
                        g.setColor(Rainbow.getColour(i++));
                        for (MFPoint mfp : c.things) {
                            Point2d pt = new Point2d(mfp);
                            pt.add(out);
                            g.setStroke(new BasicStroke(0.2f));
                            PaintThing.paint(pt, g, ma);
                            g.setStroke(new BasicStroke(0.2f));
                            for (HalfEdge e : GurobiSkelSolver.findNear(f.megafacade, mfp, mesh)) g.drawLine(ma.toX(pt.x), ma.toY(pt.y), ma.toX(e.end.x), ma.toY(e.end.y));
                            if (mfp.selectedEdge != null) {
                                g.setStroke(new BasicStroke(2f));
                                g.drawLine(ma.toX(pt.x), ma.toY(pt.y), ma.toX(mfp.selectedEdge.end.x), ma.toY(mfp.selectedEdge.end.y));
                            }
                        }
                    }
                }
            g.setStroke(new BasicStroke(1));
        }

        private void spreadImages(Graphics2D g, PanMouseAdaptor ma, Line sel, Point2d cen, List<MiniFacade> mfs) {
            Vector2d perp = sel.dir();
            perp.set(-perp.y, perp.x);
            perp.normalize();
            for (int i = 0; i < mfs.size(); i++) {
                MiniFacade mf = mfs.get(i);
                Vector2d p2 = new Vector2d(perp);
                p2.scale((i + 1) * 10);
                p2.add(cen);
                if (mf == null) {
                    g.setColor(Color.black);
                    g.fillRect(ma.toX(p2.x - 1), ma.toY(p2.y - 3), ma.toZoom(2), ma.toZoom(6));
                    continue;
                }
                double w = mf.width * 0.1;
                double h = mf.height * 0.1;
                mf.paintImage(g, ma, p2.x - w, p2.y - h, p2.x + w, p2.y + h);
            }
        }
    };
}
Also used : BasicStroke(java.awt.BasicStroke) MiniFacade(org.twak.viewTrace.facades.MiniFacade) MegaFeatures(org.twak.tweed.gen.FeatureCache.MegaFeatures) ArrayList(java.util.ArrayList) Cluster(org.twak.utils.DumbCluster1D.Cluster) HalfEdge(org.twak.utils.geom.HalfMesh2.HalfEdge) HalfFace(org.twak.utils.geom.HalfMesh2.HalfFace) DumbCluster1D(org.twak.utils.DumbCluster1D) ICanPaint(org.twak.utils.PaintThing.ICanPaint) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) ICanPaint(org.twak.utils.PaintThing.ICanPaint) Graphics2D(java.awt.Graphics2D) Line(org.twak.utils.Line) SuperLine(org.twak.viewTrace.SuperLine) Vector2d(javax.vecmath.Vector2d) Point2d(javax.vecmath.Point2d) PanMouseAdaptor(org.twak.utils.PanMouseAdaptor) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with MegaFeatures

use of org.twak.tweed.gen.FeatureCache.MegaFeatures in project chordatlas by twak.

the class Regularizer method goDebug.

public List<MiniFacade> goDebug(List<MiniFacade> in, double debugFrac, MegaFeatures megaFeatures) {
    miniFacadesUsed += in.size();
    regularised++;
    for (MiniFacade mf : in) if (mf.imageFeatures != null)
        seenImages.add(mf.imageFeatures.ortho);
    if (in.isEmpty())
        return Collections.singletonList(gridMini());
    System.out.println("starting to regularize " + in.size() + " facades...");
    in = augmentWithTween(in, megaFeatures);
    System.out.println(" adding tween for " + in.size() + " facades");
    double alpha = 0.2;
    List<MiniFacade> out;
    if (true)
        out = newFromWindows(in);
    else
        out = in.stream().map(mf -> new MiniFacade(mf)).collect(Collectors.toList());
    System.out.println(" included grids for " + out.size() + " facades...");
    if (debugFrac < 0) {
        out.add(0, new MiniFacade());
        return out;
    }
    alignMFs(out);
    if (debugFrac == 0) {
        out.add(0, new MiniFacade());
        return out;
    }
    for (MiniFacade mf : out) {
        assignFeaturesToWindows(mf.getRects(Feature.WINDOW, Feature.SHOP).stream().collect(Collectors.toList()), mf.rects);
        mf.rects.get(Feature.CORNICE).clear();
        mf.rects.get(Feature.BALCONY).clear();
        mf.rects.get(Feature.SILL).clear();
        mf.rects.get(Feature.GRID).clear();
    }
    for (MiniFacade mf : out) {
        mergeRemoveSmall(mf);
        mergeRemoveSimilar(mf);
    }
    for (Feature f : Feature.values()) for (MiniFacade mf : out) mf.rects.get(f).stream().forEach(r -> r.f = f);
    for (int i = 0; i < 50 * debugFrac; i++) {
        for (MiniFacade mf : out) for (Feature f : toReg) cluster1(mf.getRects(f), 1, alpha, Bounds.XMIN, Bounds.XMAX, Bounds.YMIN, Bounds.YMAX);
        // for (MiniFacade mf : out)
        // cluster1 ( mf.getRects(toReg), 1,  alpha, Bounds.XMIN, Bounds.XMAX, Bounds.YMIN, Bounds.YMAX);
        List<FRect> allRects = new ArrayList<>();
        for (MiniFacade mf : out) for (Feature f : toReg) allRects.addAll(mf.rects.get(f));
        for (Feature f : toReg) {
            List<FRect> allF = out.stream().flatMap(mf -> mf.rects.get(f).stream()).collect(Collectors.toList());
            cluster1(allF, 0.5, alpha, Bounds.WIDTH, Bounds.HEIGHT);
        }
        cluster1(allRects, 0.3, alpha, Bounds.XMIN, Bounds.XMAX, Bounds.YMIN, Bounds.YMAX);
        if (i % 5 == 0)
            for (MiniFacade mf : out) findNeighbours(mf.getRects(Feature.WINDOW));
        for (Dir dir : Dir.values()) clusterDeltas(allRects, 0.2, alpha, dir);
        for (MiniFacade mf : out) {
            for (FRect d : mf.rects.get(Feature.DOOR)) constrainDoor(mf, d, alpha);
            for (FRect m : mf.rects.get(Feature.MOULDING)) constrainMoulding(mf, m, alpha);
        }
        for (MiniFacade mf : out) {
            for (Feature f : toReg) {
                Iterator<FRect> rit = mf.rects.get(f).iterator();
                while (rit.hasNext()) {
                    FRect r = rit.next();
                    if (r.width <= 0.4 || r.height <= 0.4)
                        rit.remove();
                    else
                        for (FRect n : r.adjacent) if (n != null && similar(n, r)) {
                            n.attached.putAll(r.attached);
                            rit.remove();
                            break;
                        }
                }
            }
        }
    }
    if (debugFrac < 1) {
        out.add(0, new MiniFacade());
        return out;
    }
    for (MiniFacade mf : out) {
        findOuters(mf);
        mergeRemoveSmall(mf);
    }
    ids = 0;
    for (// find ids...starting with the biggest
    Feature f : // find ids...starting with the biggest
    toReg) {
        List<Pair<MiniFacade, FRect>> allRects = new ArrayList<>();
        for (MiniFacade mf : out) for (FRect r : mf.rects.get(f)) allRects.add(new Pair<>(mf, r));
        Collections.sort(allRects, new Comparator<Pair<MiniFacade, FRect>>() {

            @Override
            public int compare(Pair<MiniFacade, FRect> a_, Pair<MiniFacade, FRect> b_) {
                FRect a = a_.second(), b = b_.second();
                if (a.gridCoords != null && b.gridCoords == null)
                    return -1;
                if (b.gridCoords != null && a.gridCoords == null)
                    return 1;
                if (a.gridCoords != null && b.gridCoords != null) {
                    int out = -Integer.compare(countCoords(a.gridCoords), countCoords(b.gridCoords));
                    if (out != 0)
                        return out;
                }
                return Double.compare(a.area(), b.area());
            }

            private int countCoords(int[] gridCoords) {
                int out = 0;
                for (int i : gridCoords) if (i != -Integer.MAX_VALUE)
                    out++;
                return out;
            }
        });
        for (Pair<MiniFacade, FRect> pair : allRects) {
            FRect w = pair.second();
            if (w.id == -1)
                findId(w, f, out, pair.first(), ids++);
        }
    }
    out.add(0, combine(out));
    totalFeature += out.get(0).getRects(Feature.values()).size();
    System.out.println("done");
    return out;
}
Also used : DumbCluster1D(org.twak.utils.DumbCluster1D) Cache2(org.twak.utils.Cache2) OptionalDouble(java.util.OptionalDouble) HashMap(java.util.HashMap) CountThings(org.twak.utils.collections.CountThings) Pair(org.twak.utils.Pair) TreeSet(java.util.TreeSet) MapMapList(org.twak.utils.collections.MapMapList) ArrayList(java.util.ArrayList) Arrayz(org.twak.utils.collections.Arrayz) HashSet(java.util.HashSet) Map(java.util.Map) Mathz(org.twak.utils.Mathz) Streamz(org.twak.utils.collections.Streamz) LinkedHashSet(java.util.LinkedHashSet) InAxDouble(org.twak.utils.streams.InAxDouble) Iterator(java.util.Iterator) ImageFeatures(org.twak.tweed.gen.FeatureCache.ImageFeatures) MultiMap(org.twak.utils.collections.MultiMap) Set(java.util.Set) Vector2d(javax.vecmath.Vector2d) MegaFeatures(org.twak.tweed.gen.FeatureCache.MegaFeatures) Collectors(java.util.stream.Collectors) File(java.io.File) Cluster(org.twak.utils.DumbCluster1D.Cluster) Point2d(javax.vecmath.Point2d) List(java.util.List) DumbCluster1DImpl(org.twak.utils.DumbCluster1DImpl) Bounds(org.twak.utils.geom.DRectangle.Bounds) DRectangle(org.twak.utils.geom.DRectangle) Comparator(java.util.Comparator) Collections(java.util.Collections) Feature(org.twak.viewTrace.facades.MiniFacade.Feature) ArrayList(java.util.ArrayList) Feature(org.twak.viewTrace.facades.MiniFacade.Feature) Pair(org.twak.utils.Pair)

Example 8 with MegaFeatures

use of org.twak.tweed.gen.FeatureCache.MegaFeatures in project chordatlas by twak.

the class MiniStandalone2d method bulk.

// upload to /media/data/guerrero/projects/greeble/data/facade_sources/features
private void bulk() {
    File parentFile = new File("/media/twak/8bc5e750-9a70-4180-8eee-ced2fbba6484/data/small");
    File outDir = new File("/media/twak/8bc5e750-9a70-4180-8eee-ced2fbba6484/data/f2");
    try {
        List<File> togo = Files.walk(parentFile.toPath()).map(p -> p.toFile()).filter(f -> f.isDirectory() && new File(f, FeatureCache.PARAMETERS_YML).exists()).collect(Collectors.toList());
        new Parallel<File, Integer>(togo, new Work<File, Integer>() {

            public Integer work(File f) {
                renderAll(new MegaFeatures(f), new File(outDir, parentFile.toPath().relativize(f.toPath()).toString()).getPath());
                return 1;
            }
        }, null, true);
    // togo , new Work<File, Integer>() {
    // 
    // }, new Parallel.Complete<Integer>() {
    // 
    // Files.walk( parentFile.toPath() ).
    // map( p -> p.toFile() ).
    // filter( f -> f.isDirectory() && new File (f, "parameters.yml").exists() ).
    // forEach( f -> );
    // }, true );
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : JButton(javax.swing.JButton) ImageFeatures(org.twak.tweed.gen.FeatureCache.ImageFeatures) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) Parallel(org.twak.utils.Parallel) ListDownLayout(org.twak.utils.ui.ListDownLayout) Work(org.twak.utils.Parallel.Work) IOException(java.io.IOException) MegaFeatures(org.twak.tweed.gen.FeatureCache.MegaFeatures) Plot(org.twak.utils.ui.Plot) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) JSlider(javax.swing.JSlider) ImageIO(javax.imageio.ImageIO) JCheckBox(javax.swing.JCheckBox) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) Feature(org.twak.viewTrace.facades.MiniFacade.Feature) JPanel(javax.swing.JPanel) FeatureCache(org.twak.tweed.gen.FeatureCache) MegaFeatures(org.twak.tweed.gen.FeatureCache.MegaFeatures) Parallel(org.twak.utils.Parallel) IOException(java.io.IOException) File(java.io.File)

Example 9 with MegaFeatures

use of org.twak.tweed.gen.FeatureCache.MegaFeatures in project chordatlas by twak.

the class GurobiSkelSolver method solve.

public void solve() {
    try {
        for (HalfFace f : mesh.faces) for (HalfEdge e : f.edges()) {
            edges.add(e);
            totalEdgeLength += e.length();
        }
        print("total edge length " + totalEdgeLength);
        progress.setProgress((int) (Math.random() * 90));
        buildProblem();
        model.getEnv().set(GRB.DoubleParam.TimeLimit, GRB.INFINITY);
        long time = System.currentTimeMillis();
        new Thread(() -> {
            while (!gurobiDone) {
                try {
                    Thread.sleep(300);
                    if ((System.currentTimeMillis() - time) / 1000 > maxTimeSecs) {
                        print("time's up");
                        model.terminate();
                        progress.close();
                        return;
                    }
                } catch (InterruptedException e1) {
                }
                progress.setProgress((int) (Math.random() * 90));
                if (progress.isCanceled()) {
                    print("user cancelled");
                    model.terminate();
                }
            }
            progress.setProgress(100);
        }).start();
        try {
            String file = Tweed.SCRATCH + "problem_" + System.currentTimeMillis() + ".mps";
            new File(file).getParentFile().mkdirs();
            model.write(file);
            model.optimize();
        } finally {
            gurobiDone = true;
            progress.setProgress(100);
        }
        print("time " + (System.currentTimeMillis() - time) / 1000. + " seconds");
        if (model.get(GRB.IntAttr.Status) == GRB.Status.INFEASIBLE) {
            print("Can't solve; won't solve");
            return;
        }
        for (HalfFace f : mesh.faces) ((SuperFace) f).classification = index(faceInfo.get(f).color);
        if (minis != null)
            for (MegaFeatures mf : minis.keySet()) {
                if (mega2clusters.containsKey(mf))
                    for (MiniPtCluster pt : mega2clusters.get(mf)) {
                        for (Map.Entry<MFPoint, MiniSelectEdge> selectPt : pt.entrySet()) {
                            for (Map.Entry<HalfEdge, GRBVar> selectEdge : selectPt.getValue().edge.entrySet()) {
                                if (selectEdge.getValue().get(GRB.DoubleAttr.X) > 0.5) {
                                    HalfEdge e = selectEdge.getKey();
                                    selectPt.getKey().selectedEdge = e;
                                    selectPt.getKey().sameCluster = pt.keySet();
                                    if (e.over != null && e.next.over == null) {
                                        for (MFPoint p : pt.keySet()) ((SuperEdge) e.next).addMini(p.right);
                                        for (MFPoint p : pt.keySet()) ((SuperEdge) e.over.findBefore()).addMini(p.left);
                                    }
                                    if (e.over == null && e.next.over == null) {
                                        Line parallel = selectPt.getKey().mega.megafacade;
                                        if (parallel.absAngle(e.line()) < parallel.absAngle(e.next.line()))
                                            for (MFPoint p : pt.keySet()) ((SuperEdge) e).addMini(p.left);
                                        else
                                            for (MFPoint p : pt.keySet()) ((SuperEdge) e.next).addMini(p.right);
                                    }
                                }
                            }
                        }
                    }
            }
        if (false)
            for (HalfEdge he : edges) {
                if (he.over != null && he.next.over == null) {
                    // face comes to boundary
                    boolean viz = false;
                    EdgeVars ei = edgeInfo.get(he);
                    if (ei.edgeNoMini != null) {
                        viz = true;
                    }
                    if (viz)
                        PaintThing.debug.put("dd", new Point2d(he.end));
                }
            }
        if (globalProfs != null)
            for (HalfEdge e : edges) {
                EdgeVars ei = edgeInfo.get(e);
                GRBVar plot = ei.debug;
                if (plot != null) {
                    // && plot.get(GRB.DoubleAttr.X) > 0.5 ) {
                    Point2d o = new Point2d(e.end);
                    PaintThing.debug.put(1, o);
                }
                int p = index(ei.profile);
                ((SuperEdge) e).profI = p;
                ((SuperEdge) e).prof = p < 0 ? null : globalProfs.get(p);
            }
        dumpModelStats(edges);
        model.getEnv().dispose();
        model.dispose();
    } catch (GRBException e) {
        print("Error code: " + e.getErrorCode() + ". " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MegaFeatures(org.twak.tweed.gen.FeatureCache.MegaFeatures) HalfEdge(org.twak.utils.geom.HalfMesh2.HalfEdge) HalfFace(org.twak.utils.geom.HalfMesh2.HalfFace) GRBException(gurobi.GRBException) Line(org.twak.utils.Line) Point2d(javax.vecmath.Point2d) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) GRBVar(gurobi.GRBVar) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) MultiMap(org.twak.utils.collections.MultiMap)

Example 10 with MegaFeatures

use of org.twak.tweed.gen.FeatureCache.MegaFeatures in project chordatlas by twak.

the class SkelFootprint method buildFootprint.

public SolverState buildFootprint(List<Line> footprint, ProgressMonitor m, FeatureCache features, BlockGen blockGen) {
    MultiMap<MegaFeatures, MFPoint> minis = features == null ? null : features.createMinis(blockGen);
    Map<SuperEdge, double[]> profFit = new HashMap();
    HalfMesh2 mesh = boundMesh(footprint);
    globalProfs = null;
    Collections.sort(footprint, megaAreaComparator);
    for (Line l : footprint) {
        MegaFacade mf = ((SuperLine) l).getMega();
        if (mf.area < megaFacadeAreaThreshold)
            break;
        insert(mesh, l, 2, true, true);
        if (m.isCanceled())
            return null;
    }
    if (features != null)
        fractureOnFeatures(minis, footprint, mesh);
    m.setProgress(2);
    if (!TweedSettings.settings.useGreedyProfiles) {
        globalProfs = new ArrayList();
        findProfiles(footprint, globalProfs);
        calcProfFit(mesh, globalProfs, profFit, m);
    }
    if (FALSE && profMergeTol > 0)
        mergeOnProfiles(mesh, footprint);
    if (exitA)
        return new SolverState(mesh, minis, globalProfs, profFit, footprint);
    System.out.println("sampling...");
    for (HalfFace f : mesh) meanModeHeightColor(Loopz.from(f), (SuperFace) f, blockGen);
    pushHeightsToSmallFaces(mesh);
    for (HalfFace f : new ArrayList<>(mesh.faces)) {
        SuperFace sf = (SuperFace) f;
        if (sf.height < heightCutoff)
            sf.remove(mesh);
    }
    removeExposedFaces(mesh);
    return new SolverState(mesh, minis, globalProfs, profFit, footprint);
}
Also used : MegaFeatures(org.twak.tweed.gen.FeatureCache.MegaFeatures) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MegaFacade(org.twak.tweed.gen.ProfileGen.MegaFacade) ArrayList(java.util.ArrayList) HalfFace(org.twak.utils.geom.HalfMesh2.HalfFace) Line(org.twak.utils.Line) SuperLine(org.twak.viewTrace.SuperLine) SuperLine(org.twak.viewTrace.SuperLine) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint) HalfMesh2(org.twak.utils.geom.HalfMesh2)

Aggregations

MegaFeatures (org.twak.tweed.gen.FeatureCache.MegaFeatures)11 Point2d (javax.vecmath.Point2d)7 ImageFeatures (org.twak.tweed.gen.FeatureCache.ImageFeatures)7 File (java.io.File)6 ArrayList (java.util.ArrayList)6 Line (org.twak.utils.Line)6 HalfFace (org.twak.utils.geom.HalfMesh2.HalfFace)6 HashMap (java.util.HashMap)5 List (java.util.List)5 Vector2d (javax.vecmath.Vector2d)5 MFPoint (org.twak.tweed.gen.FeatureCache.MFPoint)5 HalfEdge (org.twak.utils.geom.HalfMesh2.HalfEdge)5 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 MultiMap (org.twak.utils.collections.MultiMap)4 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 OptionalDouble (java.util.OptionalDouble)3 Set (java.util.Set)3