"""Planta paramétrica de um andar da UNICAP (corredor + salas + labs + banheiro + elevadores). Pensada para o RoboMovel: além do desenho, exporta os waypoints do corredor e das portas para navegação. Todos os números ficam em P/SPEC abaixo — muda lá e regera. Layout (visto de cima, X ao longo do corredor, Y atravessado): +Y (lado NORTE: salas de aula / laboratórios) ┌────┬────┬────┬─────────┐ │ S1 │ S2 │ S3 │ Lab 1 │ <- salas em fila ├────┴────┴────┴─────────┤ │ CORREDOR │ ├────┬──────┬────┬───────┤ │BAN │ ELEV │ S4 │ ... │ <- lado SUL └────┴──────┴────┴───────┘ Sala na ponta oeste (fecha o corredor) e Lab 2 na ponta leste. Saídas em modelos/planta/: planta_andar.dxf – paredes/portas/mobiliário/textos em metros planta_andar.svg – vista de topo vetorial planta_andar.png – prévia raster planta_andar_3d.png– prévia isométrica planta_andar.obj – tudo extrudado (3D, Z para cima, metros) planta_andar.json – salas, portas e waypoints (para o robô) Uso: python ferramentas/scripts/planta/planta_andar.py [--h 3.0] [--out modelos/planta] """ import argparse, json, math, os # ---------------------------------------------------------------- parâmetros P = dict( wall=0.15, # espessura das paredes internas ext=0.20, # espessura da parede externa cor=2.40, # largura do corredor h=3.00, # pé-direito door=0.90, # largura da porta door_h=2.10, # altura do vão da porta (acima disso é verga) folha=0.04, # espessura da folha da porta elev=1.60, # largura da cabine do elevador elev_gap=0.15, # parede entre as duas cabines elev_cab=0.12, # folga entre cabine e parede do poço ban_w=3.60, # banheiro: largura (ao longo do corredor) ban_d=3.20, # banheiro: profundidade cab_w=0.95, # cabine de banheiro: largura cab_d=1.35, # cabine de banheiro: profundidade cab_h=2.00, # altura das divisórias das cabines pia_d=0.55, # profundidade da bancada da pia pia_h=0.85, # altura da bancada ) # lado, tipo, nome, comprimento (m), profundidade (m), porta (m do início; None = automática) SPEC = [ ("N", "sala", "Sala 1", 7.50, 6.50, 3.20), ("N", "sala", "Sala 2", 7.50, 6.50, 3.20), ("N", "lab", "Lab 1", 9.00, 8.00, 4.50), ("S", "ban", "Banheiro", P["ban_w"], P["ban_d"], None), ("S", "elev", "Elevadores", 2 * P["elev"] + P["elev_gap"], 2.20, None), ("S", "sala", "Sala 3", 7.50, 6.50, 3.20), ("S", "sala", "Sala 4", 7.20, 6.50, 3.00), ] # salas das pontas (fecham o corredor): tipo, nome, largura Y, profundidade X, porta (do lado do corredor) WEST = ("sala", "Sala 5", 6.50, 8.50, 3.20) EAST = ("lab", "Lab 2", 8.00, 9.00, 3.50) # ---------------------------------------------------------------- geometria def wall_along(x0, y0, x1, y1, t, gaps=()): """Retângulo(s) de parede (2D) ao longo de (x0,y0)->(x1,y1) com espessura t, menos os vãos.""" horiz = abs(y1 - y0) < 1e-9 a, b = (x0, x1) if horiz else (y0, y1) segs, cur = [], a for g0, g1 in sorted(gaps): g0, g1 = max(g0, a), min(g1, b) if g1 <= g0: continue if g0 > cur: segs.append((cur, g0)) cur = max(cur, g1) if cur < b: segs.append((cur, b)) rects = [] for s0, s1 in segs: if s1 - s0 < 1e-6: continue if horiz: rects.append((s0, y0 - t / 2, s1, y0 + t / 2)) else: rects.append((x0 - t / 2, s0, x0 + t / 2, s1)) return rects def build(params=P, spec=SPEC, west=WEST, east=EAST): t, te, cw = params["wall"], params["ext"], params["cor"] dw, dh = params["door"], params["h"] cy = [-cw / 2, cw / 2] ln = sum(r[3] for r in spec if r[0] == "N") ls = sum(r[3] for r in spec if r[0] == "S") L = max(ln, ls) x0, x1 = 0.0, L rooms, walls, doors, lines2d, items, mob = [], [], [], [], [], [] def W(x0_, y0_, x1_, y1_, z0=0.0, z1=None): walls.append((x0_, y0_, x1_, y1_, z0, dh if z1 is None else z1)) def M(x0_, y0_, x1_, y1_, z0, z1): """Mobiliário / divisória leve: entra no 3D, mas é desenhado como mobiliário no 2D.""" mob.append((x0_, y0_, x1_, y1_, z0, z1)) def porta(x, y, w, o, d): """Registra uma porta e a verga (parede acima do vão).""" doors.append(dict(p=[round(x, 3), round(y, 3)], w=w, o=o, d=d)) hw = w / 2 if o == "h": W(x - hw, y - t / 2, x + hw, y + t / 2, params["door_h"], dh) # verga else: W(x - t / 2, y - hw, x + t / 2, y + hw, params["door_h"], dh) # ---- salas laterais (em fila ao longo do corredor) for side in ("N", "S"): x = x0 sgn = 1 if side == "N" else -1 y_in = cy[1] if side == "N" else cy[0] for sd, kind, name, w, d, pd in [r for r in spec if r[0] == side]: y_out = y_in + sgn * d rect = [x, min(y_in, y_out), x + w, max(y_in, y_out)] rooms.append(dict(name=name, kind=kind, side=side, rect=rect)) W(x, y_out - te / 2, x + w, y_out + te / 2) # fundo (parede externa) W(x + w - t / 2, y_in, x + w + t / 2, y_out) # divisa com a próxima if kind == "elev": for f in (0.3, 0.7): porta(x + w * f, y_in, min(dw, params["elev"] - 0.4), "h", sgn) elif kind == "ban": px = x + w / 2 # porta ao centro porta(px, y_in, dw, "h", sgn) elif pd is not None: porta(x + pd, y_in, dw, "h", sgn) x += w # ---- parede do corredor dos dois lados, com os vãos das portas for side in ("N", "S"): y = cy[1] if side == "N" else cy[0] gaps = [(d["p"][0] - d["w"] / 2, d["p"][0] + d["w"] / 2) for d in doors if d["o"] == "h" and abs(d["p"][1] - y) < 1e-6] for r in wall_along(x0, y, x1, y, t, gaps): W(*r) # ---- fechamento das extremidades de cada fila for side in ("N", "S"): rs = [r for r in rooms if r["side"] == side] if not rs: continue y_in = cy[1] if side == "N" else cy[0] prim = min(rs, key=lambda r: r["rect"][0]) ult = max(rs, key=lambda r: r["rect"][2]) yb = prim["rect"][3] if side == "N" else prim["rect"][1] ye = ult["rect"][3] if side == "N" else ult["rect"][1] if abs(yb - y_in) > 1e-6: W(prim["rect"][0] - te / 2, min(y_in, yb), prim["rect"][0] + te / 2, max(y_in, yb)) if abs(ye - y_in) > 1e-6: W(ult["rect"][2] - te / 2, min(y_in, ye), ult["rect"][2] + te / 2, max(y_in, ye)) # ---- salas das pontas (fecham o corredor) for kind, name, wy, dx, pdy in (west, east): oeste = (name == west[1]) rx = [-dx, 0] if oeste else [L, L + dx] ylo, yhi = -wy / 2, wy / 2 rooms.append(dict(name=name, kind=kind, side="W" if oeste else "E", rect=[rx[0], ylo, rx[1], yhi])) xf = rx[0] if oeste else rx[1] W(xf - te / 2, ylo, xf + te / 2, yhi) # fundo W(rx[0], ylo - te / 2, rx[1], ylo + te / 2) # lateral W(rx[0], yhi - te / 2, rx[1], yhi + te / 2) # lateral xi = rx[1] if oeste else rx[0] dy = pdy - wy / 2 porta(xi, dy, dw, "v", -1 if oeste else 1) # ---- elevadores: cabine + símbolo X + porta de correr for r in [q for q in rooms if q["kind"] == "elev"]: a, b, c, d_ = r["rect"] cx = (a + c) / 2 W(cx - t / 2, b, cx + t / 2, d_) # divisa entre os poços for s in (-1, 1): xa = a if s < 0 else cx xb = cx if s < 0 else c e = params["elev_cab"] items.append(dict(kind="elevador", rect=[xa + e, b + e, xb - e, d_ - e - 0.10])) # porta de correr: painel fino na face do corredor xm = (xa + xb) / 2 wdoor = min(dw, params["elev"] - 0.4) M(xm - wdoor / 2, d_ - 0.06, xm + wdoor / 2, d_, 0.0, params["door_h"]) # ---- banheiro: cabines + bancada + vasos for r in [q for q in rooms if q["kind"] == "ban"]: a, b, c, d_ = r["rect"] nc = 2 wc = params["cab_w"] for i in range(nc): xa = a + 0.15 + i * (wc + 0.05) xb = xa + wc ya, yb = b + 0.15, b + 0.15 + params["cab_d"] # divisórias (3 lados; frente livre) M(xa - 0.03, ya, xa + 0.03, yb, 0.0, params["cab_h"]) M(xb - 0.03, ya, xb + 0.03, yb, 0.0, params["cab_h"]) M(xa, ya - 0.03, xb, ya + 0.03, 0.0, params["cab_h"]) items.append(dict(kind="vaso", rect=[xa + 0.22, ya + 0.10, xa + 0.22 + 0.38, ya + 0.10 + 0.60])) # bancada da pia na parede oposta px0, px1 = c - 0.15 - params["pia_d"], c - 0.15 py0, py1 = b + 0.55, d_ - 0.55 M(px0, py0, px1, py1, params["pia_h"] - 0.06, params["pia_h"]) for i in range(2): yc = py0 + (py1 - py0) * (0.3 + 0.4 * i) items.append(dict(kind="pia", rect=[px0 + 0.08, yc - 0.20, px1 - 0.08, yc + 0.20])) # ---- folhas e arcos das portas (2D) for d in doors: x, y, w, o, s = d["p"][0], d["p"][1], d["w"], d["o"], d["d"] hw = w / 2 ts = [i * math.pi / 48 for i in range(49)] if o == "h": hx, hy = x - hw, y # dobradiça lines2d.append([(hx, hy), (hx, hy + s * w)]) # folha aberta lines2d.append([(hx + w * math.cos(t), y + s * w * math.sin(t)) for t in ts]) # arco d["folha"] = [hx, hy, hx + 0.04, hy + s * w] else: hx, hy = x, y - hw lines2d.append([(hx, hy), (hx + s * w, hy)]) lines2d.append([(hx + s * w * math.sin(t), hy + w * math.cos(t)) for t in ts]) d["folha"] = [hx, hy, hx + s * w, hy + 0.04] return dict(rooms=rooms, walls=walls, mob=mob, doors=doors, lines2d=lines2d, items=items, L=L, cw=cw, cy=cy) # ---------------------------------------------------------------- exportadores def dxf(g, path): o = ["0", "SECTION", "2", "ENTITIES"] def poly(pts, layer, closed=False): o.extend(["0", "POLYLINE", "8", layer, "66", "1", "70", "1" if closed else "0"]) for x, y in pts: o.extend(["0", "VERTEX", "8", layer, "10", f"{x:.3f}", "20", f"{y:.3f}", "30", "0.0"]) o.extend(["0", "SEQEND", "8", layer]) def txt(x, y, s, layer="TEXTO", hh=0.35): o.extend(["0", "TEXT", "8", layer, "10", f"{x:.3f}", "20", f"{y:.3f}", "30", "0.0", "40", str(hh), "1", s]) for x0, y0, x1, y1, z0, z1 in g["walls"]: poly([(x0, y0), (x1, y0), (x1, y1), (x0, y1)], "PAREDES", True) for r in g["rooms"]: poly([(r["rect"][0], r["rect"][1]), (r["rect"][2], r["rect"][1]), (r["rect"][2], r["rect"][3]), (r["rect"][0], r["rect"][3])], "SALAS", True) txt((r["rect"][0] + r["rect"][2]) / 2, (r["rect"][1] + r["rect"][3]) / 2, r["name"], hh=0.4) for it in g["items"]: a, b, c, d_ = it["rect"] poly([(a, b), (c, b), (c, d_), (a, d_)], "MOBILIARIO", True) if it["kind"] == "elevador": poly([(a, b), (c, d_)], "MOBILIARIO"); poly([(a, d_), (c, b)], "MOBILIARIO") for x0_, y0_, x1_, y1_, z0_, z1_ in g["mob"]: poly([(x0_, y0_), (x1_, y0_), (x1_, y1_), (x0_, y1_)], "MOBILIARIO", True) for ln in g["lines2d"]: poly(ln, "PORTAS") o.extend(["0", "ENDSEC", "0", "EOF"]) open(path, "w", encoding="latin-1", errors="replace").write("\n".join(o) + "\n") def _boxes(g): """Lista de caixas (x0,y0,x1,y1,z0,z1) de tudo que é sólido.""" out = list(g["walls"]) + list(g["mob"]) for d in g["doors"]: f = d["folha"] out.append((min(f[0], f[2]), min(f[1], f[3]), max(f[0], f[2]), max(f[1], f[3]), 0.0, P["door_h"])) return out def obj(g, path): V, F = [], [] def box(x0, y0, x1, y1, z0, z1): n = len(V) V.extend([(x0, y0, z0), (x1, y0, z0), (x1, y1, z0), (x0, y1, z0), (x0, y0, z1), (x1, y0, z1), (x1, y1, z1), (x0, y1, z1)]) for a, b, c, d_ in [(0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4), (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]: F.append((n + a, n + b, n + c, n + d_)) for x0, y0, x1, y1, z0, z1 in _boxes(g): if abs(x1 - x0) > 1e-6 and abs(y1 - y0) > 1e-6 and abs(z1 - z0) > 1e-6: box(x0, y0, x1, y1, z0, z1) with open(path, "w") as f: f.write("# planta do andar — paredes/portas/mobiliário, metros, Z para cima\n") for x, y, z in V: f.write(f"v {x:.4f} {y:.4f} {z:.4f}\n") for fc in F: f.write("f " + " ".join(str(i + 1) for i in fc) + "\n") CORES = dict(sala="#eef4fb", lab="#f3eefb", ban="#eefbf1", elev="#fdf3e6") def _extent(g): xs = [v for w in g["walls"] for v in (w[0], w[2])] + [r["rect"][i] for r in g["rooms"] for i in (0, 2)] ys = [v for w in g["walls"] for v in (w[1], w[3])] + [r["rect"][i] for r in g["rooms"] for i in (1, 3)] return min(xs), max(xs), min(ys), max(ys) def svg(g, path): x0, x1, y0, y1 = _extent(g) x0, x1, y0, y1 = x0 - 1, x1 + 1, y0 - 1, y1 + 1 s = 40 Wd, Hh = (x1 - x0) * s, (y1 - y0) * s T = lambda x, y: ((x - x0) * s, (y1 - y) * s) o = [f''] for r in g["rooms"]: a, b, c, d_ = r["rect"] px, py = T(a, d_) o.append(f'') for it in g["items"]: a, b, c, d_ = it["rect"] px, py = T(a, d_) o.append(f'') if it["kind"] == "elevador": for (ax, ay), (bx, by) in [((a, b), (c, d_)), ((a, d_), (c, b))]: (px1, py1), (px2, py2) = T(ax, ay), T(bx, by) o.append(f'') for x0_, y0_, x1_, y1_, z0_, z1_ in g["mob"]: px, py = T(x0_, y1_) o.append(f'') for x0_, y0_, x1_, y1_, z0_, z1_ in g["walls"]: px, py = T(x0_, y1_) o.append(f'') for ln in g["lines2d"]: pts = " ".join(f"{T(x, y)[0]:.1f},{T(x, y)[1]:.1f}" for x, y in ln) o.append(f'') for r in g["rooms"]: a, b, c, d_ = r["rect"] px, py = T((a + c) / 2, (b + d_) / 2) o.append(f'{r["name"]}') o.append("") open(path, "w").write("\n".join(o)) def png(g, path): import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from matplotlib.patches import Rectangle x0, x1, y0, y1 = _extent(g) x0, x1, y0, y1 = x0 - 1.5, x1 + 1.5, y0 - 1.5, y1 + 1.5 w, h = x1 - x0, y1 - y0 fig, ax = plt.subplots(figsize=(16, 16 * h / w), dpi=100) for r in g["rooms"]: a, b, c, d_ = r["rect"] ax.add_patch(Rectangle((a, b), c - a, d_ - b, fc=CORES.get(r["kind"], "#eee"), ec="none", zorder=1)) for it in g["items"]: a, b, c, d_ = it["rect"] ax.add_patch(Rectangle((a, b), c - a, d_ - b, fc="#dfe3ea", ec="#9aa0ab", lw=0.8, zorder=2)) if it["kind"] == "elevador": ax.plot([a, c], [b, d_], color="#9aa0ab", lw=0.8, zorder=3) ax.plot([a, c], [d_, b], color="#9aa0ab", lw=0.8, zorder=3) for x0_, y0_, x1_, y1_, z0_, z1_ in g["mob"]: ax.add_patch(Rectangle((x0_, y0_), x1_ - x0_, y1_ - y0_, fc="#aab2bf", ec="#7c8492", lw=0.5, zorder=3)) for x0_, y0_, x1_, y1_, z0_, z1_ in g["walls"]: ax.add_patch(Rectangle((x0_, y0_), x1_ - x0_, y1_ - y0_, fc="#333", zorder=4)) for ln in g["lines2d"]: ax.plot([p[0] for p in ln], [p[1] for p in ln], color="#e0702f", lw=1.4, zorder=5) for r in g["rooms"]: a, b, c, d_ = r["rect"] ax.text((a + c) / 2, (b + d_) / 2, r["name"], ha="center", va="center", fontsize=9, zorder=6) ax.set_aspect("equal"); ax.set_xlim(x0, x1); ax.set_ylim(y0, y1) ax.grid(alpha=.2); ax.set_title("UNICAP — planta do andar (m)") ax.set_xlabel("X (m) — ao longo do corredor"); ax.set_ylabel("Y (m)") fig.savefig(path, dpi=100); plt.close(fig) def png3d(g, path, W=1700): import numpy as np from PIL import Image, ImageDraw c30, s30 = math.cos(math.radians(30)), math.sin(math.radians(30)) V, F = [], [] def box(x0, y0, x1, y1, z0, z1): n = len(V) V.extend([(x0, y0, z0), (x1, y0, z0), (x1, y1, z0), (x0, y1, z0), (x0, y0, z1), (x1, y0, z1), (x1, y1, z1), (x0, y1, z1)]) for f in [(0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4), (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]: F.append(tuple(n + i for i in f)) xs0, xs1, ys0, ys1 = _extent(g) n0 = len(V) V.extend([(xs0 - 2, ys0 - 2, 0), (xs1 + 2, ys0 - 2, 0), (xs1 + 2, ys1 + 2, 0), (xs0 - 2, ys1 + 2, 0)]) floor = (n0, n0 + 1, n0 + 2, n0 + 3) for x0, y0, x1, y1, z0, z1 in _boxes(g): if abs(x1 - x0) > 1e-6 and abs(y1 - y0) > 1e-6 and abs(z1 - z0) > 1e-6: box(x0, y0, x1, y1, z0, z1) P3 = np.array([((x - y) * c30, (x + y) * s30 - z) for x, y, z in V]) u0, u1, v0, v1 = P3[:, 0].min(), P3[:, 0].max(), P3[:, 1].min(), P3[:, 1].max() sc = (W - 60) / (u1 - u0) Hh = int((v1 - v0) * sc) + 60 T = lambda i: (30 + (P3[i, 0] - u0) * sc, 30 + (P3[i, 1] - v0) * sc) img = Image.new("RGB", (W, Hh), "#f4f5f8") dr = ImageDraw.Draw(img) light = np.array([0.35, -0.6, 0.72]); light /= np.linalg.norm(light) for f in [floor] + sorted(F, key=lambda f: np.mean([(V[i][0] + V[i][1] + V[i][2]) for i in f])): a, b, c = (np.array(V[f[0]]), np.array(V[f[1]]), np.array(V[f[2]])) nrm = np.cross(b - a, c - a); ln = np.linalg.norm(nrm) nrm = nrm / ln if ln > 1e-9 else np.array([0, 0, 1]) if nrm[2] < 0: nrm = -nrm br = 0.55 + 0.45 * max(0.0, float(nrm @ light)) col = (232, 234, 238) if f is floor else tuple(int(min(255, v * br)) for v in np.array([150, 152, 158])) dr.polygon([T(i) for i in f], fill=col) img.save(path) # ---------------------------------------------------------------- principal def main(): ap = argparse.ArgumentParser() ap.add_argument("--out", default="modelos/planta") ap.add_argument("--h", type=float, default=P["h"]) a = ap.parse_args() P["h"] = a.h os.makedirs(a.out, exist_ok=True) g = build() dxf(g, f"{a.out}/planta_andar.dxf") obj(g, f"{a.out}/planta_andar.obj") svg(g, f"{a.out}/planta_andar.svg") png(g, f"{a.out}/planta_andar.png") png3d(g, f"{a.out}/planta_andar_3d.png") wp = dict(corredor=[dict(x=round(x, 2), y=0.0) for x in [0.4, g["L"] / 2, g["L"] - 0.4]], portas=[dict(x=d["p"][0], y=d["p"][1], largura=d["w"], eixo=d["o"]) for d in g["doors"]]) info = dict(pe_direito_m=a.h, corredor_m=dict(comprimento=round(g["L"], 2), largura=g["cw"]), portas=len(g["doors"]), salas=[dict(nome=r["name"], tipo=r["kind"], lado=r["side"], area_m2=round((r["rect"][2] - r["rect"][0]) * (r["rect"][3] - r["rect"][1]), 1)) for r in g["rooms"]], waypoints=wp) json.dump(info, open(f"{a.out}/planta_andar.json", "w"), indent=1, ensure_ascii=False) print(f"corredor {g['L']:.1f} x {g['cw']:.2f} m · {len(g['rooms'])} ambientes · " f"{len(g['doors'])} portas · {len(g['items'])} peças de mobiliário · {len(g['walls'])} sólidos") print("→", a.out) if __name__ == "__main__": main()