Search in sources :

Example 11 with DRectangle

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

the class Regularizer method clusterDeltas.

private void clusterDeltas(List<FRect> rects, double tol, double alpha, Dir dir) {
    if (rects.isEmpty())
        return;
    List<FRect> toCluster = new ArrayList<>();
    for (FRect r : rects) {
        FRect da = r.getAdj(dir);
        if (// only strong adjacencies
        da != null && da.getAdj(dir.opposite) == r)
            toCluster.add(r);
    }
    DumbCluster1D<FRect> clusterer = new DumbCluster1D<FRect>(tol, toCluster) {

        @Override
        public double toDouble(FRect rect) {
            return rect.distanceToAdjacent(dir);
        }
    };
    Bounds b = (dir == Dir.L || dir == Dir.R) ? Bounds.XCEN : Bounds.YCEN;
    int moveDirection = dir == Dir.L || dir == Dir.D ? 1 : -1;
    Map<FRect, Double> desiredSpacings = new HashMap<>();
    for (DumbCluster1D.Cluster<FRect> e : clusterer) {
        for (FRect rect : e.things) desiredSpacings.put(rect, e.mean);
    }
    Collections.sort(rects, DRectangle.comparator(b, dir == Dir.L || dir == Dir.D ? false : true));
    for (FRect r : rects) {
        Double spacing = desiredSpacings.get(r);
        if (spacing == null)
            continue;
        DRectangle adj = r.getAdj(dir);
        adj.set(b, adj.get(b) - (spacing - r.distanceToAdjacent(dir)) * alpha * moveDirection);
    }
}
Also used : DRectangle(org.twak.utils.geom.DRectangle) HashMap(java.util.HashMap) Bounds(org.twak.utils.geom.DRectangle.Bounds) ArrayList(java.util.ArrayList) DumbCluster1D(org.twak.utils.DumbCluster1D) OptionalDouble(java.util.OptionalDouble) InAxDouble(org.twak.utils.streams.InAxDouble)

Example 12 with DRectangle

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

the class Regularizer method similarMerge.

private static boolean similarMerge(FRect n, FRect r) {
    DRectangle i = n.intersect(r);
    double exactArea = n.area() + r.area() - (i == null ? 0 : i.area());
    double unionArea = n.union(r).area();
    return exactArea / unionArea > 0.5 && i != null || exactArea / unionArea > 0.9;
}
Also used : DRectangle(org.twak.utils.geom.DRectangle)

Example 13 with DRectangle

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

the class Regularizer method assignFeaturesToWindows.

private void assignFeaturesToWindows(List<FRect> windows, MultiMap<Feature, FRect> rects) {
    int count = 0;
    for (Feature f : new Feature[] { Feature.CORNICE, Feature.SILL, Feature.BALCONY }) {
        for (FRect r : rects.get(f)) {
            for (FRect w : windows) {
                DRectangle bounds = new DRectangle(w);
                bounds.height = bounds.height * 0.5;
                switch(f) {
                    case SILL:
                        bounds.y = bounds.x - bounds.height;
                        break;
                    case BALCONY:
                        bounds.y = bounds.x;
                        break;
                    case CORNICE:
                        bounds.y = bounds.getMaxY();
                        break;
                    default:
                        break;
                }
                if (r.intersects(w)) {
                    w.attached.put(f, r);
                    count++;
                }
            }
            FRect win = nearest(windows, r, 2);
            if (win != null)
                win.attached.put(f, r);
        }
    }
// System.out.println("atatched " + count +" cornicesesese");
}
Also used : DRectangle(org.twak.utils.geom.DRectangle) Feature(org.twak.viewTrace.facades.MiniFacade.Feature)

Example 14 with DRectangle

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

the class Regularizer method combine.

private MiniFacade combine(List<MiniFacade> in) {
    MiniFacade out = new MiniFacade();
    out.left = lp;
    out.width = rp - lp;
    out.imageFeatures = in.get(0).imageFeatures;
    out.color = new double[] { 0, 0, 0, 1 };
    out.groundColor = new double[] { 0, 0, 0, 1 };
    int gcc = 0;
    for (MiniFacade mf : in) for (int i = 0; i < 3; i++) {
        out.color[i] += mf.color[i];
        if (mf.groundColor != null) {
            out.groundColor[i] += mf.groundColor[i];
            gcc++;
        }
    }
    for (int i = 0; i < 3; i++) {
        out.color[i] /= in.size();
        if (gcc > 0)
            out.groundColor[i] /= gcc;
    }
    Cache2<Outer, Integer, List<FRect>> corniceX = new ArrayCache2();
    Cache2<Outer, Integer, List<FRect>> sillX = new ArrayCache2();
    Cache2<Outer, Integer, List<FRect>> balX = new ArrayCache2();
    out.height = in.stream().mapToDouble(mf -> mf.height).average().getAsDouble();
    out.groundFloorHeight = in.stream().mapToDouble(mf -> mf.groundFloorHeight).average().getAsDouble();
    for (int i = 0; i < ids; i++) {
        int yay = 0, nay = 0;
        int ii = i;
        List<FRect> found = in.stream().map(mf -> m2i2r.get(mf, ii)).filter(x -> x != null).flatMap(l -> l.stream()).collect(Collectors.toList());
        Point2d avg = found.stream().map(r -> r.getCenter()).collect(new Point2DMeanCollector());
        for (MiniFacade mf : in) {
            List<FRect> r = m2i2r.get(mf, i);
            if (mf.contains(avg)) {
                if (r.isEmpty()) {
                    if (mf.left + 3 < avg.x && mf.left + mf.width - 3 > avg.x)
                        nay++;
                } else
                    yay++;
            }
        }
        if (yay >= nay) {
            // if we believe it exists add it as average of observed sizes
            FRect o;
            if (dimensionSpread(found) > 1.4) {
                // scattered -> union (typically shop windows)
                o = new FRect(found.get(0));
                for (FRect n : found) o.setFrom(o.union(n));
            } else
                // about same size: average position: windows on a grid
                o = new FRect(average(found.toArray(new FRect[found.size()])));
            {
                FRect t = found.get(0);
                o.f = t.f;
                o.id = i;
                o.outer = null;
                o.attachedHeight.get(Feature.SILL).d = averageAttached(o, Feature.SILL, found);
                o.attachedHeight.get(Feature.CORNICE).d = averageAttached(o, Feature.CORNICE, found);
                o.attachedHeight.get(Feature.BALCONY).d = averageAttached(o, Feature.BALCONY, found);
                if (t.f == Feature.WINDOW || t.f == Feature.SHOP) {
                    for (FRect r : found) {
                        corniceX.get(r.outer, r.yi).add(o);
                        sillX.get(r.outer, r.yi).add(o);
                        balX.get(r.outer, r.yi).add(o);
                    }
                }
            }
            out.rects.put(o.f, o);
        }
    }
    spreadAttachedOverGrid(Feature.SILL, sillX);
    spreadAttachedOverGrid(Feature.CORNICE, corniceX);
    spreadAttachedOverGrid(Feature.BALCONY, balX);
    fixOverlaps(out);
    mergeRemoveSmall(out);
    DRectangle mr = out.getAsRect();
    // ensure everything is comfortably within the bounds
    mr.width -= 0.2;
    mr.x += 0.1;
    for (Feature f : Feature.values()) {
        // clip to all
        Iterator<FRect> rit = out.rects.get(f).iterator();
        while (rit.hasNext()) {
            FRect r = rit.next();
            DRectangle section = r.intersect(mr);
            if (section == null || section.area() < 0.5)
                rit.remove();
            else
                r.setFrom(section);
        }
    }
    {
        // door height
        Double hf = Double.valueOf(0);
        while (out.groundFloorHeight < 6 && ((hf = horizontalEmpty(out, out.groundFloorHeight)) != null)) out.groundFloorHeight = hf + 0.3;
        if (out.groundFloorHeight >= 6)
            // no ground floor!
            out.groundFloorHeight = 0;
    }
    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) DRectangle(org.twak.utils.geom.DRectangle) Feature(org.twak.viewTrace.facades.MiniFacade.Feature) OptionalDouble(java.util.OptionalDouble) InAxDouble(org.twak.utils.streams.InAxDouble) Point2d(javax.vecmath.Point2d) MapMapList(org.twak.utils.collections.MapMapList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with DRectangle

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

the class Regularizer method averageAttached.

private double averageAttached(FRect o, Feature sillF, List<FRect> found) {
    int count = 0;
    double total = 0;
    for (FRect f : found) {
        DRectangle bounds = union(f.attached.get(sillF));
        if (bounds != null) {
            total += Math.abs(bounds.getMaxX() - f.getMaxX());
            count++;
        }
    }
    return count == 0 ? 0 : Mathz.clamp(total / count, 0.2, o.height / 2);
}
Also used : DRectangle(org.twak.utils.geom.DRectangle)

Aggregations

DRectangle (org.twak.utils.geom.DRectangle)21 Point2d (javax.vecmath.Point2d)7 ArrayList (java.util.ArrayList)6 List (java.util.List)4 MeshBuilder (org.twak.siteplan.jme.MeshBuilder)4 Feature (org.twak.viewTrace.facades.MiniFacade.Feature)4 Vector3f (com.jme3.math.Vector3f)3 File (java.io.File)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 Matrix4d (javax.vecmath.Matrix4d)3 Point3d (javax.vecmath.Point3d)3 Vector2d (javax.vecmath.Vector2d)3 Vector3d (javax.vecmath.Vector3d)3 Griddable (org.twak.viewTrace.facades.Grid.Griddable)3 Material (com.jme3.material.Material)2 Geometry (com.jme3.scene.Geometry)2 Map (java.util.Map)2 OptionalDouble (java.util.OptionalDouble)2 Window (org.twak.tweed.gen.WindowGen.Window)2