"""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 PARAMS/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/textos em metros (camadas PAREDES, PORTAS, SALAS, TEXTO) planta_andar.svg – vista de topo vetorial planta_andar.png – prévia raster planta_andar.obj – paredes extrudadas (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 elev=1.60, # lado da cabine do elevador elev_gap=0.10, # parede entre as duas cabines ban_w=3.20, # banheiro: largura (ao longo do corredor) ban_d=3.00, # banheiro: profundidade ) # lado, tipo, nome, comprimento (m), profundidade (m), porta (m do início; None = sem porta) 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.20), ("S", "ban", "Banheiro", P["ban_w"], P["ban_d"], 1.40), ("S", "elev", "Elevadores", 2 * P["elev"] + P["elev_gap"], 2.00, 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) WEST = ("sala", "Sala 5", 6.50, 8.50, 3.20) # (tipo, nome, largura Y, profundidade X, porta do lado leste) EAST = ("lab", "Lab 2", 8.00, 9.00, 3.50) C0 = 0.28209479177387814 # não usado aqui; mantido por simetria com os outros scripts # ---------------------------------------------------------------- geometria def wall_along(x0, y0, x1, y1, t, gaps=()): """Retângulo(s) de parede 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) # recorta o vão ao trecho da parede 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, ew = params["wall"], params["ext"], params["cor"], params["elev"] dw = params["door"] cy = [-cw / 2, cw / 2] # comprimento do corredor = max(soma N, soma S) 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 = [], [], [] # ---- 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 rooms.append(dict(name=name, kind=kind, side=side, rect=[x, min(y_in, y_out), x + w, max(y_in, y_out)])) walls += wall_along(x, y_out, x + w, y_out, te) # fundo (parede externa) walls += wall_along(x + w, y_in, x + w, y_out, t) # divisa com a sala seguinte if kind == "elev": # uma porta por cabine for f in (0.3, 0.7): doors.append(dict(room=f"{name} {1 if f < .5 else 2}", side=side, o="h", p=[round(x + w * f, 3), round(y_in, 3)], w=min(dw, ew))) elif pd is not None: doors.append(dict(room=name, side=side, o="h", p=[round(x + pd, 3), round(y_in, 3)], w=dw)) 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["side"] == side] walls += wall_along(x0, y, x1, y, t, gaps) # ---- fechamento das extremidades de cada fila (senão a envoltória fica aberta) 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: walls += wall_along(prim["rect"][0], min(y_in, yb), prim["rect"][0], max(y_in, yb), te) if abs(ye - y_in) > 1e-6: walls += wall_along(ult["rect"][2], min(y_in, ye), ult["rect"][2], max(y_in, ye), te) # ---- 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] walls += wall_along(xf, ylo, xf, yhi, te) # fundo (externo) walls += wall_along(rx[0], ylo, rx[1], ylo, te) # lateral walls += wall_along(rx[0], yhi, rx[1], yhi, te) # lateral xi = rx[1] if oeste else rx[0] dy = pdy - wy / 2 # centro da porta (coordenada Y) doors.append(dict(room=name, side="W" if oeste else "E", o="v", p=[round(xi, 3), round(dy, 3)], w=dw)) walls += wall_along(xi, ylo, xi, yhi, t, [(dy - dw / 2, dy + dw / 2)]) # ---- elevadores: divisa entre as duas cabines for r in [q for q in rooms if q["kind"] == "elev"]: cx = (r["rect"][0] + r["rect"][2]) / 2 walls += wall_along(cx, r["rect"][1], cx, r["rect"][3], t) return dict(rooms=rooms, walls=walls, doors=doors, L=L, cw=cw, cy=cy) # ---------------------------------------------------------------- exportadores def dxf(g, path): o = ["0", "SECTION", "2", "ENTITIES"] def poly(pts, layer, closed=True): 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 in g["walls"]: poly([(x0, y0), (x1, y0), (x1, y1), (x0, y1)], "PAREDES") for r in g["rooms"]: x0, y0, x1, y1 = r["rect"] poly([(x0, y0), (x1, y0), (x1, y1), (x0, y1)], "SALAS", True) txt((x0 + x1) / 2, (y0 + y1) / 2, r["name"], hh=0.4) for d in g["doors"]: x, y = d["p"] if d.get("o") == "v": pts = [(x - 0.05, y - d["w"] / 2), (x + 0.05, y - d["w"] / 2), (x + 0.05, y + d["w"] / 2), (x - 0.05, y + d["w"] / 2)] else: pts = [(x - d["w"] / 2, y - 0.05), (x + d["w"] / 2, y - 0.05), (x + d["w"] / 2, y + 0.05), (x - d["w"] / 2, y + 0.05)] poly(pts, "PORTAS") o.extend(["0", "ENDSEC", "0", "EOF"]) open(path, "w", encoding="latin-1", errors="replace").write("\n".join(o) + "\n") def obj(g, path, h=3.0): 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 in g["walls"]: box(x0, y0, x1, y1, 0.0, h) with open(path, "w") as f: f.write("# planta do andar — paredes, 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") def svg(g, path): xs = [r["rect"][0] for r in g["rooms"]] + [r["rect"][2] for r in g["rooms"]] + \ [w[0] for w in g["walls"]] + [w[2] for w in g["walls"]] ys = [r["rect"][1] for r in g["rooms"]] + [r["rect"][3] for r in g["rooms"]] + \ [w[1] for w in g["walls"]] + [w[3] for w in g["walls"]] x0, x1, y0, y1 = min(xs) - 1, max(xs) + 1, min(ys) - 1, max(ys) + 1 s = 40 # px por metro W, 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 x0_, y0_, x1_, y1_ in g["walls"]: px, py = T(x0_, y1_) o.append(f'') for d in g["doors"]: x, y = d["p"] if d.get("o") == "v": px, py = T(x - 0.06, y + d["w"] / 2) o.append(f'') else: px, py = T(x - d["w"] / 2, y + 0.06) o.append(f'') for r in g["rooms"]: a, b, c, dd = r["rect"] px, py = T((a + c) / 2, (b + dd) / 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 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)] x0, x1, y0, y1 = min(xs) - 1.5, max(xs) + 1.5, min(ys) - 1.5, max(ys) + 1.5 w, h = x1 - x0, y1 - y0 fig, ax = plt.subplots(figsize=(15, 15 * h / w), dpi=100) cores = dict(sala="#eef4fb", lab="#f3eefb", ban="#eefbf1", elev="#fdf3e6") 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="#999", lw=0.4, zorder=1)) ax.text((a + c) / 2, (b + d) / 2, r["name"], ha="center", va="center", fontsize=9, zorder=4) for x0_, y0_, x1_, y1_ in g["walls"]: ax.add_patch(Rectangle((x0_, y0_), x1_ - x0_, y1_ - y0_, fc="#333", zorder=3)) for d in g["doors"]: x, y = d["p"] if d.get("o") == "v": ax.add_patch(Rectangle((x - 0.08, y - d["w"] / 2), 0.16, d["w"], fc="#e0702f", zorder=5)) else: ax.add_patch(Rectangle((x - d["w"] / 2, y - 0.08), d["w"], 0.16, fc="#e0702f", zorder=5)) ax.set_aspect("equal") ax.set_xlim(x0, x1) ax.set_ylim(y0, y1) ax.grid(alpha=.25) 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, h=3.0, W=1700): """Vista isométrica das paredes extrudadas (projeção própria, sem matplotlib 3D).""" import numpy as np from PIL import Image, ImageDraw c30, s30 = math.cos(math.radians(30)), math.sin(math.radians(30)) def proj(p): x, y, z = p return ((x - y) * c30, (x + y) * s30 - z) 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)) 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)] x0, x1, y0, y1 = min(xs) - 2, max(xs) + 2, min(ys) - 2, max(ys) + 2 # piso n0 = len(V) V.extend([(x0, y0, 0), (x1, y0, 0), (x1, y1, 0), (x0, y1, 0)]) floor = (n0, n0 + 1, n0 + 2, n0 + 3) for w in g["walls"]: box(w[0], w[1], w[2], w[3], 0.0, h) P = np.array([proj(v) for v in V]) u0, u1, v0, v1 = P[:, 0].min(), P[:, 0].max(), P[:, 1].min(), P[:, 1].max() sc = (W - 60) / (u1 - u0) Hh = int((v1 - v0) * sc) + 60 T = lambda i: (30 + (P[i, 0] - u0) * sc, 30 + (P[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) ordem = [floor] + sorted(F, key=lambda f: np.mean([(V[i][0] + V[i][1] + V[i][2]) for i in f])) for f in ordem: 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)) if f is floor: col = (232, 234, 238) else: base = np.array([150, 152, 158]) col = tuple(int(min(255, v * br)) for v in base) 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() 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", a.h) 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", a.h) # waypoints para o robô: linha do corredor + centro das portas wy = 0.0 wp = dict(corredor=[dict(x=round(x, 2), y=wy) for x in [0.4, g["L"] / 2, g["L"] - 0.4]], portas=[dict(sala=d["room"], x=round(d["p"][0], 2), y=round(d["p"][1], 2)) for d in g["doors"]]) info = dict(pe_direito_m=a.h, corredor_m=dict(comprimento=round(g["L"], 2), largura=g["cw"]), 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['walls'])} trechos de parede") print("→", a.out) if __name__ == "__main__": main()