Skip to content

Single place to convert your 2D floor plans to 3d model (no LLM)

Python

Furniture: doorways without doors kept clear, plants at the walls, smaller living rooms less crowded

23f2005639477178bae8ad
diff --git a/AGENTS.md b/AGENTS.md
index ee520f6..ea70dd5 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -50,7 +50,7 @@ Flow: upload -> `run_detect` -> `plan.json` -> review page edits it -> `POST …
## Tests: run them before you finish
```bash
-tests/run_all.sh # detector on the sample, quantities, themes, server guards, furniture clashes, stair and wall walking
+tests/run_all.sh # detector on the sample, quantities, themes, furniture rules, server guards, furniture clashes, stair and wall walking
python3 tests/test_sample.py # the sample must be read exactly
CHROME=/path/to/chromium node tests/test_site.mjs examples/sample_plan.pdf http://127.0.0.1:8080
```
diff --git a/furnish.py b/furnish.py
index b9b7b77..d675ddf 100644
--- a/furnish.py
+++ b/furnish.py
@@ -86,16 +86,27 @@ class Room:
face = {"N": y1, "S": y0, "E": x1, "W": x0}[side]
# wall coverage along this side (open-plan boundaries have no wall)
cov = [] # walls, and windows (a counter or bed can stand under a sill; tall items are cut below)
- for w in walls + [list(o["rect"]) + [0] for o in ops if o["type"] == "window"]:
+ shut = [] # anything on the edge: walls of any height, windows, doors
+ for w, full in [(w, w[4] == 0) for w in walls] + [(list(o["rect"]) + [0], o["type"] == "window") for o in ops]:
if side in "NS":
near_ = (w[2] - 0.6 <= face <= w[3] + 0.6) and (w[2] >= face - 0.25 if side == "N" else w[3] <= face + 0.25)
- if near_ and w[4] == 0:
- cov.append((max(w[0], along[0]), min(w[1], along[1])))
+ iv = (max(w[0], along[0]), min(w[1], along[1]))
else:
near_ = (w[0] - 0.6 <= face <= w[1] + 0.6) and (w[0] >= face - 0.25 if side == "E" else w[1] <= face + 0.25)
- if near_ and w[4] == 0:
- cov.append((max(w[2], along[0]), min(w[3], along[1])))
+ iv = (max(w[2], along[0]), min(w[3], along[1]))
+ if near_:
+ shut.append(iv)
+ if full:
+ cov.append(iv)
cov = union_iv([c for c in cov if c[1] - c[0] > 0.2])
+ # a door-wide gap in a wall is a doorway all the same: keep it clear (a wider one, or a whole open side,
+ # is open plan, not a way through)
+ p = along[0]
+ for a, b in union_iv([c for c in shut if c[1] - c[0] > 0.2]) + [(along[1], along[1])]:
+ if 2.0 <= a - p <= min(5.0, 0.9 * (along[1] - along[0])):
+ self.keepout.append({"N": (p, a, y1 - 3.0, y1), "S": (p, a, y0, y0 + 3.0),
+ "E": (x1 - 3.0, x1, p, a), "W": (x0, x0 + 3.0, p, a)}[side])
+ p = max(p, b)
low, tall = cov, cov
for o in ops:
r = o["rect"]
@@ -160,7 +171,7 @@ class Room:
c = b - gap - w / 2
else:
c = (a + b) / 2
- for c in (c, a + gap + w / 2, b - gap - w / 2):
+ for c in (c, a + gap + w / 2, b - gap - w / 2, (a + b) / 2):
if side == "N":
box, xy = (c - w / 2, c + w / 2, y1 - d, y1), (c, y1 - d / 2)
elif side == "S":
@@ -481,7 +492,7 @@ def kitchen(R):
if hob is not None:
segs = subtract_iv(segs, hob - 1.4, hob + 1.4)
for u, v in segs:
- if v - u >= 1.5:
+ if v - u >= 1.25:
box = {"N": (u, v, y1 - 1.1, y1), "S": (u, v, y0, y0 + 1.1), "E": (x1 - 1.1, x1, u, v), "W": (x0, x0 + 1.1, u, v)}[sd]
R.add("wall_cab2", (0, 0, 0, 0), *box, FRONT[sd], solid=False)
fridge(R)
@@ -496,17 +507,28 @@ def fridge(R):
return
-def living(R):
+def living(R, extras=None):
+ """sofa, coffee table and rug, a TV unit opposite; an armchair and a corner piece only in a room big enough for them
+ (extras: None = by the room's size)"""
+ if extras is None:
+ extras = R.w * R.d >= 150 # about 12 x 12 ft
+ unit_d = R.sizes("tv_unit", [(5.0, 1.4)])[0][1]
for side in R.sides_by("long"):
+ depth = R.w if side in "EW" else R.d # from the sofa's wall to the one opposite
for w, sd, skw in R.sizes("sofa", [(7.0, 3.0), (6.0, 3.0), (5.0, 3.0)]):
p = R.spot(side, w, sd)
if not p:
continue
x, y, rot, box = p
- for cw, cd, ckw in R.sizes("coffee_table", [(3.6, 2.0)]):
+ tables = R.sizes("coffee_table", [(3.6, 2.0)])
+ m = themes.size(R.T, "coffee_table")
+ if m: # a smaller copy of the theme's own table before a plain one
+ tables[3:3] = [(m[0] * k, m[1] * k, {}) for k in (0.7, 0.6)]
+ for cw, cd, ckw in tables:
ct = front_offset(side, (x, y), sd / 2 + 0.5 + cd / 2)
cbox = rect_at(ct, cw, cd, side)
- if R.fits(cbox):
+ # a 2 ft walk between the table and the unit opposite
+ if R.fits(cbox) and depth - (sd + 0.5 + cd) >= unit_d + 2.0:
break
else:
continue
@@ -516,7 +538,7 @@ def living(R):
R.add("rug", rbox, *rbox, solid=False)
R.hang("art", side, x if side in "NS" else y, R.height("sofa", 3.0) + 0.7)
# an armchair at one end of the coffee table, facing it
- if themes.has(R.T, "armchair"):
+ if extras and themes.has(R.T, "armchair"):
aw, ad, _ = R.sizes("armchair", [])[0]
for s in (1, -1):
along = cw / 2 + 0.6 + ad / 2
@@ -530,7 +552,8 @@ def living(R):
q = R.spot(OPP[side], tw, td)
if q:
R.add("tv_unit", q[3], q[0], q[1], q[2], **tkw)
- top, cq = 1.9 if tkw else R.height("tv_unit", 1.9), q[0] if side in "NS" else q[1]
+ m, cq = themes.size(R.T, "tv_unit"), q[0] if side in "NS" else q[1]
+ top = m[2] * tw / m[0] if m and not tkw else 1.9 # a model is scaled to its box, its top with it
if themes.has(R.T, "decor"): # on the unit: beside the TV, or in the middle with no TV
dw = themes.size(R.T, "decor")[0] if themes.size(R.T, "decor") else 0.7
off = (tw / 2 - dw / 2 - 0.2) * (1 if R.T["tv"] else 0)
@@ -539,7 +562,8 @@ def living(R):
if not R.T["tv"]:
R.hang("mirror", OPP[side], cq, top + 0.9)
break
- corner(R)
+ if extras:
+ corner(R)
return
@@ -565,7 +589,7 @@ def desk(R):
def dining(R):
if R.zone.get("area", R.w * R.d) >= 150: # a big "DRG./DIN." room is drawing + dining: sofa set first
- living(R)
+ living(R, extras=R.w * R.d >= 250) # (the table needs about 100 sq ft of it)
if not dining_table(R): # no room left beside the sofa: the table goes in the L's other arm
r = max_rect(R.zone["rects"], exclude=R.rect)
if r and min(r[1] - r[0], r[3] - r[2]) >= 6.0:
@@ -594,9 +618,10 @@ def dining_table(R):
cy = R.rect[2] + R.d * (u if long_x else t)
a, b = (tw, tl) if long_x else (tl, tw)
box = (cx - a / 2 - m, cx + a / 2 + m, cy - b / 2 - m, cy + b / 2 + m)
- if R.fits(box):
- R.add("dining_set", (cx - a / 2 - 1.6, cx + a / 2 + 1.6, cy - b / 2 - 1.6, cy + b / 2 + 1.6),
- cx, cy, 0 if long_x else 90, w=tw, d=tl, seats=n, **piece)
+ seats = (cx - a / 2 - 1.6, cx + a / 2 + 1.6, cy - b / 2 - 1.6, cy + b / 2 + 1.6)
+ # room to pull the chairs out from walls and furniture; walkways need only stay clear of the chairs
+ if R.fits(box, ignore_keepout=True) and R.fits(seats):
+ R.add("dining_set", seats, cx, cy, 0 if long_x else 90, w=tw, d=tl, seats=n, **piece)
return True
return False
@@ -652,14 +677,23 @@ def garden(R):
def plants(R, n):
+ """potted plants in the corners, then along the long sides, never out in the middle of the floor"""
x0, x1, y0, y1 = R.rect
- long_x = R.w >= R.d
- for k in range(n):
- t = (k + 0.5) / n
- x, y = (x0 + (x1 - x0) * t, (y0 + y1) / 2) if long_x else ((x0 + x1) / 2, y0 + (y1 - y0) * t)
- box = (x - 0.8, x + 0.8, y - 0.8, y + 0.8)
- if R.fits(box):
- R.add("plant", box, x, y, k * 70)
+ m = 0.9 # pot radius 0.75 and a little air
+ spots = [(x0 + m, y0 + m), (x1 - m, y1 - m), (x1 - m, y0 + m), (x0 + m, y1 - m)]
+ k = int(max(R.w, R.d) / 6)
+ for i in range(1, k):
+ t = i / k
+ spots += [(x0 + R.w * t, y0 + m), (x0 + R.w * t, y1 - m)] if R.w >= R.d else [(x0 + m, y0 + R.d * t), (x1 - m, y0 + R.d * t)]
+ box = lambda x, y: (x - 0.8, x + 0.8, y - 0.8, y + 0.8)
+ placed = []
+ for k in range(n): # each one as far from the others as it can be
+ free = [q for q in spots if R.fits(box(*q))]
+ if not free:
+ return
+ x, y = max(free, key=lambda q: min((abs(q[0] - p[0]) + abs(q[1] - p[1]) for p in placed), default=0))
+ R.add("plant", box(x, y), x, y, k * 70)
+ placed.append((x, y))
RULES = dict(dress=dress, bedroom=bedroom, toilet=toilet, kitchen=kitchen, living=living, dining=dining, puja=puja, wash=wash,
diff --git a/tests/run_all.sh b/tests/run_all.sh
index 224f0c2..f307771 100755
--- a/tests/run_all.sh
+++ b/tests/run_all.sh
@@ -8,6 +8,7 @@ python3 tests/test_detect.py
python3 tests/test_server.py
python3 tests/test_quantities.py
python3 tests/test_themes.py
+python3 tests/test_furnish.py
python3 detect.py examples/sample_plan.pdf /tmp/planto3d_sample.json > /dev/null
for p in /tmp/planto3d_sample.json jobs/*/plan.json; do
[ -f "$p" ] || continue
diff --git a/tests/test_furnish.py b/tests/test_furnish.py
new file mode 100644
index 0000000..c2993bd
--- /dev/null
+++ b/tests/test_furnish.py
@@ -0,0 +1,61 @@
+"""Furniture rules on small made-up rooms: a door-wide gap in a wall is kept clear like a doorway, plants stand at the
+walls, a small living room gets no armchair or corner piece, and the vase stands on the unit at its scaled height.
+python3 tests/test_furnish.py"""
+import os, sys
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+import furnish, themes # noqa: E402
+from furnish import overlap # noqa: E402
+
+T = 0.33 # wall thickness
+
+
+def room(kind, w, d, gaps=(), doors=()):
+ """a w x d room of one type, walled all round; gaps: (x0, x1) left open in the north wall; doors: (x0, x1) there"""
+ holes = sorted(list(gaps) + list(doors))
+ north, x = [], 0.0
+ for a, b in holes:
+ north.append([x, a, d, d + T, 0])
+ x = b
+ north.append([x, w, d, d + T, 0])
+ walls = [[-T, w + T, -T, 0, 0], [-T, 0, 0, d, 0], [w, w + T, 0, d, 0]] + north
+ ops = [dict(type="door", rect=[a, b, d, d + T], axis="x") for a, b in doors]
+ return dict(walls=walls, openings=ops, rooms=[dict(type=kind, label=kind, rects=[[0, w, 0, d]])], stairs=[], floor_height=10.0)
+
+
+def items(floor, theme="royal"):
+ its = furnish.furnish_floor(floor, themes.load(theme))
+ assert not furnish.clashes(its, floor), furnish.clashes(its, floor)
+ return its
+
+
+# a kitchen with a door and, beside it, a gap with no door: the fridge keeps 3 ft clear of both
+k = room("kitchen", 7.5, 10.5, gaps=[(4.0, 7.5)], doors=[(1.2, 3.7)])
+fridge = [it for it in items(k) if it["fn"] == "fridge2"]
+assert fridge and not overlap(fridge[0]["box"], (4.0, 7.5, 7.5, 10.5), 0.02), fridge
+# a whole side open to the next room is open plan, not a doorway: it keeps nothing away
+assert furnish.Room(room("puja", 7.5, 3.0, gaps=[(0.0, 7.5)])["rooms"][0], room("puja", 7.5, 3.0, gaps=[(0.0, 7.5)])).keepout == []
+print("ok furnish: a gap in a wall is kept clear like a doorway")
+
+# plants: in the corners and along the sides, never out in the middle
+b = room("balcony", 26.0, 8.0)
+for it in items(b):
+ if it["fn"] == "plant":
+ x0, x1, y0, y1 = it["box"]
+ assert min(x0, 26.0 - x1, y0, 8.0 - y1) < 0.2, it["box"]
+print("ok furnish: plants stand at the walls")
+
+# a 10 x 10 living room: sofa, table and a unit; the armchair and the corner piece only in a bigger one
+small = {it["fn"] for it in items(room("living", 10.0, 10.0))}
+big = {it["fn"] for it in items(room("living", 14.0, 14.0))}
+assert {"sofa", "coffee_table", "tv_unit"} <= small and not small & {"armchair", "corner"}, small
+assert {"armchair", "corner"} <= big, big
+# the vase stands on the unit as built: a model scaled down to its box is lower than the library says
+for n in (10.0, 12.0, 14.0):
+ its = items(room("living", n, n))
+ unit = [it for it in its if it["fn"] == "tv_unit"]
+ for vase in (it for it in its if it["fn"] == "decor"):
+ w, _, h = themes.size(themes.load("royal"), "tv_unit")
+ x0, x1, y0, y1 = unit[0]["box"]
+ assert abs(vase["kw"]["z"] - h * max(x1 - x0, y1 - y0) / w) < 0.02, (n, vase["kw"], unit[0]["box"])
+print("ok furnish: a small living room is not crowded, the vase stands on its unit")