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);
}
}
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;
}
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");
}
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;
}
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);
}
Aggregations