File size: 20,220 Bytes
c05974a 986980c c05974a 00b0961 c05974a 00b0961 c05974a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import matplotlib.patheffects as pe
import numpy as np
import io
from core.constants import ROOM_COLORS
# Architectural-drawing colour scheme
PLOT_OUTLINE = "#00C853" # green β outer plot boundary
SETBACK_LINE = "#E53935" # red β inner setback line
ROOM_OUTLINE = "#1E88E5" # blue β room walls
ROAD_COLOR = "#37474F" # dark slate for the road strip
BG_PAGE = "#0D1117"
BG_PLOT = "#FFFFFF" # white interior so blue/red read crisply
SETBACK_ROAD = 6.56 # 2 m on road-facing side (NBC)
SETBACK_OTHER = 3.28 # 1 m on the other three sides
def _setbacks(road_side="S"):
left = SETBACK_ROAD if road_side == "W" else SETBACK_OTHER
right = SETBACK_ROAD if road_side == "E" else SETBACK_OTHER
bottom = SETBACK_ROAD if road_side == "S" else SETBACK_OTHER
top = SETBACK_ROAD if road_side == "N" else SETBACK_OTHER
return left, right, bottom, top
def format_room_label(name):
labels = {
"external_staircase": "External\nStaircase",
"foyer": "Foyer",
"living_room": "Living",
"kitchen": "Kitchen",
"dining_room": "Dining",
"utility_area": "Utility",
"common_bathroom": "Common\nWash",
"attached_bathroom": "Attached\nWash",
"attached_bathroom_02": "Attached\nWash 2",
"master_bedroom": "Master\nBedroom",
"bedroom_01": "Bedroom 1",
"bedroom_02": "Bedroom 2",
"bedroom_03": "Bedroom 3",
"pooja_room": "Pooja",
"study": "Study",
"balcony": "Balcony",
"lift": "Lift",
"internal_corridor": "Corridor",
}
return labels.get(name, name.replace("_", "\n").title())
def _hex_to_rgb(hex_color):
hex_color = hex_color.lstrip("#")
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
def _label_color(fill_color):
r, g, b = _hex_to_rgb(fill_color)
luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255.0
return "#111111" if luminance > 0.58 else "#FFFFFF"
def _label_box_and_stroke(fill_color):
"""Return (bbox_dict, stroke_color) tuned to the room fill color.
Dark rooms get a soft light pill with dark text.
Light rooms get a soft dark pill with white text.
This avoids the muddy black halo you were seeing on small labels.
"""
r, g, b = _hex_to_rgb(fill_color)
luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255.0
if luminance > 0.58:
return (
dict(boxstyle='round,pad=0.16', facecolor=(1, 1, 1, 0.60), edgecolor='none'),
'#FFFFFF',
)
return (
dict(boxstyle='round,pad=0.16', facecolor=(0, 0, 0, 0.16), edgecolor='none'),
'#000000',
)
def _draw_plot_skeleton(ax, pw, ph, road_side="S"):
"""Draw white interior + green plot outline + red dashed setbacks +
grey road strip outside the road edge."""
left, right, bottom, top = _setbacks(road_side)
# Grey road strip outside the plot on the road side
rw = 4.0
road_rect = {
"S": (-1, -rw - 0.6, pw + 2, rw),
"N": (-1, ph + 0.6, pw + 2, rw),
"E": (pw + 0.6, -1, rw, ph + 2),
"W": (-rw - 0.6, -1, rw, ph + 2),
}.get(road_side)
if road_rect:
ax.add_patch(Rectangle((road_rect[0], road_rect[1]),
road_rect[2], road_rect[3],
facecolor=ROAD_COLOR, edgecolor='none',
alpha=0.9, zorder=1))
# White interior (paper)
ax.add_patch(Rectangle((0, 0), pw, ph,
facecolor=BG_PLOT, edgecolor='none', zorder=0))
# Green plot outline
ax.add_patch(Rectangle((0, 0), pw, ph,
facecolor='none', edgecolor=PLOT_OUTLINE,
linewidth=3.0, zorder=4))
# Red dashed setback (inner buildable area)
ax.add_patch(Rectangle((left, bottom),
pw - left - right, ph - bottom - top,
facecolor='none', edgecolor=SETBACK_LINE,
linewidth=1.5, linestyle=(0, (6, 4)), zorder=3))
# "ROAD" label on the grey strip
road_label = {"S": (pw / 2, -2.6, 0),
"N": (pw / 2, ph + 2.6, 0),
"E": (pw + 2.6, ph / 2, 90),
"W": (-2.6, ph / 2, 90)}.get(road_side, (pw / 2, -2.6, 0))
ax.text(road_label[0], road_label[1], "ROAD",
ha='center', va='center', fontsize=8,
color="#FFFFFF", fontweight='bold', rotation=road_label[2], zorder=6)
def _draw_entrance_arrow(ax, pw, ph, road_side="S", rooms=None):
"""RED TRIANGLE drawn INSIDE the foyer on its road-facing wall β that's
the main entrance door marker. Tip points into the foyer (away from
the road). Falls back to the road-edge centre if foyer is absent.
"""
foyer = None
if rooms is not None:
for r in rooms:
if getattr(r, "name", None) == "foyer":
foyer = r
break
# Triangle size: scale with foyer's short side so it fits comfortably
if foyer is not None:
short = min(foyer.w, foyer.h)
size = max(1.5, min(2.5, short * 0.45))
else:
size = 2.0
if road_side == "S":
# door on foyer's bottom wall, tip points UP into the room
cx = foyer.cx if foyer is not None else pw * 0.5
wall_y = foyer.y if foyer is not None else 0.0
base_a = (cx - size * 0.6, wall_y + 0.05)
base_b = (cx + size * 0.6, wall_y + 0.05)
tip = (cx, wall_y + size)
elif road_side == "N":
cx = foyer.cx if foyer is not None else pw * 0.5
wall_y = foyer.top if foyer is not None else ph
base_a = (cx - size * 0.6, wall_y - 0.05)
base_b = (cx + size * 0.6, wall_y - 0.05)
tip = (cx, wall_y - size)
elif road_side == "E":
cy = foyer.cy if foyer is not None else ph * 0.5
wall_x = foyer.right if foyer is not None else pw
base_a = (wall_x - 0.05, cy - size * 0.6)
base_b = (wall_x - 0.05, cy + size * 0.6)
tip = (wall_x - size, cy)
else: # W
cy = foyer.cy if foyer is not None else ph * 0.5
wall_x = foyer.x if foyer is not None else 0.0
base_a = (wall_x + 0.05, cy - size * 0.6)
base_b = (wall_x + 0.05, cy + size * 0.6)
tip = (wall_x + size, cy)
triangle = plt.Polygon([base_a, base_b, tip],
facecolor='#E53935', edgecolor='#B71C1C',
linewidth=1.2, zorder=9)
ax.add_patch(triangle)
# Spec: letters not required β just the red triangle door marker
def _draw_plot_size(ax, pw, ph):
"""Header badge + per-side dimension labels so it's obvious which
edge is `width` vs `depth` (e.g. 40 vs 60)."""
ax.text(0.2, ph + 1.0, f"Plot: {int(pw)}' x {int(ph)}'",
fontsize=8.5, fontweight='bold', color="#0D47A1",
ha='left', va='bottom', zorder=7,
bbox=dict(boxstyle='round,pad=0.2',
facecolor='#FFFFFF', edgecolor='#0D47A1', linewidth=0.8))
# Width label on the top edge
ax.text(pw / 2, ph + 0.6, f"β {int(pw)}' β",
fontsize=8, fontweight='bold', color="#0D47A1",
ha='center', va='bottom', zorder=7)
# Depth label on the right edge (rotated)
ax.text(pw + 0.4, ph / 2, f"β {int(ph)}' β",
fontsize=8, fontweight='bold', color="#0D47A1",
ha='center', va='center', rotation=90, zorder=7)
def _draw_rooms(ax, rooms, label_fs=7.0, area_fs=5.5, show_dims=True):
"""Fill rooms edge-to-edge with blue outlines. No gaps, no rounded corners."""
for room in rooms:
col = ROOM_COLORS.get(room.name, "#546E7A")
ax.add_patch(Rectangle((room.x, room.y), room.w, room.h,
facecolor=col, alpha=0.85,
edgecolor=ROOM_OUTLINE, linewidth=1.4, zorder=2))
cx = room.x + room.w / 2
cy = room.y + room.h / 2
label = format_room_label(room.name)
text_col = _label_color(col)
label_box, stroke_col = _label_box_and_stroke(col)
room_short_side = min(room.w, room.h)
fs = max(5.2, min(label_fs + 0.2, room_short_side * 0.62))
afs = max(4.2, min(area_fs, room_short_side * 0.44))
name_only = room.w * room.h < 95 or room_short_side < 6.3
show_area = room.w * room.h >= 120 and room_short_side >= 7.0
show_dim_line = show_dims and room.w * room.h >= 170 and room_short_side >= 8.5
lines = [label]
if show_area:
lines.append(f"{room.area:.0f} sqft")
if show_dim_line:
lines.append(f"{room.w:.1f}' x {room.h:.1f}'")
if name_only:
lines = [label]
text = "\n".join(lines)
ax.text(
cx,
cy,
text,
ha='center',
va='center',
fontsize=fs if len(lines) == 1 else afs + 0.5,
color=text_col,
fontweight='bold',
zorder=5,
linespacing=1.05,
bbox=label_box,
path_effects=[pe.withStroke(linewidth=0.65, foreground=stroke_col)],
)
def _draw_north_arrow(ax, pw, ph):
"""BLACK TRIANGLE pointing north, drawn OUTSIDE the plot (per spec)."""
nx = pw + 2.2
base_y = ph + 0.6
tip_y = base_y + 3.0
triangle = plt.Polygon([(nx - 1.1, base_y), (nx + 1.1, base_y),
(nx, tip_y)],
facecolor='#000000', edgecolor='#000000',
linewidth=1.0, zorder=8)
ax.add_patch(triangle)
ax.text(nx, tip_y + 0.5, "N", fontsize=10, color="#000000",
fontweight='bold', ha='center', va='bottom', zorder=8,
bbox=dict(boxstyle='round,pad=0.15', facecolor='#FFFFFF',
edgecolor='#000000', linewidth=0.6))
def draw_floorplan(fp, rank=1, show_grid=False, show_compass=True,
road_side="S", title_prefix="Floor Plan"):
"""Draw a single floor plan and return matplotlib figure."""
fig, ax = plt.subplots(figsize=(7, 7))
fig.patch.set_facecolor(BG_PAGE)
ax.set_facecolor(BG_PAGE)
pw, ph = fp.plot_w, fp.plot_h
_draw_plot_skeleton(ax, pw, ph, road_side=road_side)
_draw_rooms(ax, fp.rooms, label_fs=7.5, area_fs=6.0)
_draw_entrance_arrow(ax, pw, ph, road_side=road_side, rooms=fp.rooms)
_draw_plot_size(ax, pw, ph)
if show_compass:
_draw_north_arrow(ax, pw, ph)
# Rank badge
badge_color = {1: '#FFD700', 2: '#C0C0C0', 3: '#CD7F32'}.get(rank, '#4FC3F7')
ax.text(1.2, ph - 1.8, f"#{rank}", fontsize=18, color=badge_color,
fontweight='bold', zorder=7,
path_effects=[pe.withStroke(linewidth=3, foreground='black')])
# Score strip below plan
s = fp.scores
score_txt = (f"Fitness {s['Fitness']:.3f} "
f"Vastu {s['VS']*100:.0f}% "
f"Adjacency {s['AQ']*100:.0f}% "
f"Space {s.get('SU', 0) * 100:.0f}%")
ax.text(pw / 2, -2.0, score_txt,
ha='center', va='center', fontsize=8,
color='#B0BEC5', style='italic', zorder=5)
ax.set_xlim(-6, pw + 6)
ax.set_ylim(-7, ph + 5)
ax.set_aspect('equal')
ax.axis('off')
ax.set_title(f"{title_prefix} #{rank}", color='#4FC3F7',
fontsize=11, fontweight='bold', pad=8)
plt.tight_layout(pad=0.5)
return fig
def draw_top5_grid(plans):
"""Draw top 5 plans in a 2+2+1 grid. Returns figure."""
fig = plt.figure(figsize=(16, 14))
fig.patch.set_facecolor('#0D1117')
positions = [
(0.02, 0.52, 0.46, 0.44), # Plan 1 β top left
(0.52, 0.52, 0.46, 0.44), # Plan 2 β top right
(0.02, 0.04, 0.30, 0.44), # Plan 3 β mid left
(0.35, 0.04, 0.30, 0.44), # Plan 4 β mid centre
(0.68, 0.04, 0.30, 0.44), # Plan 5 β mid right
]
for i, (fp, (left, bottom, width, height)) in enumerate(zip(plans[:5], positions)):
ax = fig.add_axes([left, bottom, width, height])
ax.set_facecolor(BG_PAGE)
pw, ph = fp.plot_w, fp.plot_h
rank = i + 1
_draw_plot_skeleton(ax, pw, ph, road_side="S")
_draw_rooms(ax, fp.rooms,
label_fs=(6.5 if i < 2 else 5.5),
area_fs=(5.0 if i < 2 else 4.5),
show_dims=False)
# Rank + scores
s = fp.scores
badge_color = {1: '#FFD700', 2: '#C0C0C0', 3: '#CD7F32'}.get(rank, '#4FC3F7')
ax.text(1.0, ph - 1.4, f"#{rank}",
fontsize=14 if i < 2 else 11,
color=badge_color, fontweight='bold', zorder=7,
path_effects=[pe.withStroke(linewidth=2.5, foreground='black')])
score_line = (f"Vastu {s['VS']*100:.0f}% "
f"Adj {s['AQ']*100:.0f}% "
f"Space {s.get('SU', 0) * 100:.0f}% "
f"Fit {s['Fitness']:.3f}")
ax.set_title(f"Plan #{rank}\n{score_line}",
color='#4FC3F7' if i == 0 else '#B0BEC5',
fontsize=7.5, fontweight='bold', pad=4)
ax.set_xlim(-1, pw + 1)
ax.set_ylim(-1, ph + 1)
ax.set_aspect('equal')
ax.axis('off')
fig.suptitle("Top 5 Optimised Floor Plans",
fontsize=16, fontweight='bold', color='white', y=0.99)
return fig
def draw_comparison(raw_fp, opt_fp, peak_fp=None, rank=1, road_side="S",
left_title="Layout Proposer Generated",
right_title="Expert Engine Generated",
left_subtitle="Graph VAE + Geometry Constructor",
right_subtitle="Conditioned Geometry Constructor"):
"""Proposed layout vs constructed layout vs optional peak comparison."""
use_peak = peak_fp is not None
fig = plt.figure(figsize=(24, 10) if use_peak else (18, 10))
fig.patch.set_facecolor(BG_PAGE)
if use_peak:
titles = ("Layout Proposer Generated", "Expert Engine Generated",
"Peak Optimised")
subtitles = ("Graph VAE + Geometry Constructor",
"Conditioned Geometry Constructor",
"Best-of-N Constraint Search")
plans = (raw_fp, opt_fp, peak_fp)
axes_positions = [(0.035, 0.075, 0.285, 0.78),
(0.365, 0.075, 0.285, 0.78),
(0.695, 0.075, 0.285, 0.78)]
else:
titles = (left_title, right_title)
subtitles = (left_subtitle, right_subtitle)
plans = (raw_fp, opt_fp)
axes_positions = [(0.055, 0.075, 0.39, 0.78), (0.555, 0.075, 0.39, 0.78)]
for ax_pos, fp, title, subtitle in zip(axes_positions, plans, titles, subtitles):
ax = fig.add_axes(ax_pos)
ax.set_facecolor(BG_PAGE)
pw, ph = fp.plot_w, fp.plot_h
_draw_plot_skeleton(ax, pw, ph, road_side=road_side)
_draw_rooms(ax, fp.rooms, label_fs=6.2, area_fs=4.9, show_dims=True)
_draw_entrance_arrow(ax, pw, ph, road_side=road_side, rooms=fp.rooms)
_draw_plot_size(ax, pw, ph)
_draw_north_arrow(ax, pw, ph)
s = fp.scores
info = (f"Fit {s['Fitness']:.3f} "
f"Vastu {s['VS']*100:.0f}% "
f"Adj {s['AQ']*100:.0f}% "
f"Space {s.get('SU', 0)*100:.0f}%")
center_x = ax_pos[0] + ax_pos[2] / 2
fig.text(center_x, 0.91, title,
ha='center', va='center', color='#4FC3F7',
fontsize=15, fontweight='bold')
fig.text(center_x, 0.885, subtitle,
ha='center', va='center', color='#B0BEC5',
fontsize=10, fontweight='bold')
fig.text(center_x, 0.86, info,
ha='center', va='center', color='#4FC3F7',
fontsize=11, fontweight='bold')
ax.set_xlim(-6, pw + 6)
ax.set_ylim(-7, ph + 5)
ax.set_aspect('equal')
ax.axis('off')
final_fp = peak_fp if use_peak else opt_fp
gain = final_fp.scores["Fitness"] - raw_fp.scores["Fitness"]
direction = "+" if gain >= 0 else ""
fig.suptitle(
f"Plan #{rank} | Optimisation gain: {direction}{gain*100:.1f}% fitness "
f"({raw_fp.scores['Fitness']:.3f} -> {final_fp.scores['Fitness']:.3f})",
fontsize=15, fontweight='bold',
color='#00E676' if gain >= 0 else '#FF5252',
y=0.965)
return fig
def fig_to_bytes(fig):
buf = io.BytesIO()
fig.savefig(buf, format='png', dpi=150, bbox_inches='tight',
facecolor=fig.get_facecolor())
buf.seek(0)
plt.close(fig)
return buf
def draw_score_radar(fp):
"""Draw a radar chart of scores for a plan."""
categories = ['Adjacency\nQuality', 'Vastu\nScore',
'Space\nUsage', 'Area\nCompliance']
s = fp.scores
values = [s['AQ'], s['VS'], s.get('SU', 0), s['AC']]
values_plot = values + [values[0]]
angles = np.linspace(0, 2*np.pi, len(categories), endpoint=False).tolist()
angles += angles[:1]
fig, ax = plt.subplots(figsize=(4, 4), subplot_kw=dict(polar=True))
fig.patch.set_facecolor('#1A1A2E')
ax.set_facecolor('#1A1A2E')
ax.plot(angles, values_plot, 'o-', linewidth=2, color='#4FC3F7')
ax.fill(angles, values_plot, alpha=0.3, color='#4FC3F7')
ax.set_xticks(angles[:-1])
ax.set_xticklabels(categories, color='white', fontsize=7)
ax.set_ylim(0, 1)
ax.set_yticks([0.25, 0.5, 0.75, 1.0])
ax.set_yticklabels(['25%','50%','75%','100%'], color='#B0BEC5', fontsize=6)
ax.grid(color='#2A2A4A', linewidth=0.8)
ax.spines['polar'].set_color('#2A2A4A')
ax.set_title(f"Score Profile\nFitness: {s['Fitness']:.3f}",
color='#4FC3F7', fontsize=8, fontweight='bold', pad=12)
plt.tight_layout()
return fig
def draw_vastu_compass(fp):
"""Draw a compass showing room placements vs Vastu zones."""
fig, ax = plt.subplots(figsize=(5, 5))
fig.patch.set_facecolor('#1A1A2E')
ax.set_facecolor('#1A1A2E')
ax.set_xlim(-1.3, 1.3)
ax.set_ylim(-1.3, 1.3)
ax.set_aspect('equal')
ax.axis('off')
# Draw compass zones
zone_positions = {
"NW":(-0.7, 0.7), "N":(0, 1.0), "NE":(0.7, 0.7),
"W": (-1.0, 0), "Centre":(0,0),"E": (1.0, 0),
"SW":(-0.7,-0.7), "S":(0,-1.0), "SE":(0.7,-0.7),
}
zone_colors = {
"NE":"#1B5E20","N":"#2E7D32","NW":"#388E3C",
"E": "#1565C0","Centre":"#37474F","W":"#5D4037",
"SE":"#B71C1C","S":"#C62828","SW":"#6A1B9A",
}
for zone, (zx, zy) in zone_positions.items():
circle = plt.Circle((zx*0.75, zy*0.75), 0.28,
color=zone_colors.get(zone,'#37474F'),
alpha=0.5, zorder=2)
ax.add_patch(circle)
ax.text(zx*0.75, zy*0.75, zone, ha='center', va='center',
fontsize=7, color='white', fontweight='bold', zorder=3)
# Place room dots
s = fp.scores.get('vastu_details', [])
for rule_name, room_name, zone, passed in s:
if zone in zone_positions:
zx, zy = zone_positions[zone]
offset = 0.12
color = '#00E676' if passed == True else ('#FFFF00' if passed == 'partial' else '#FF5252')
ax.plot(zx*0.75 + offset, zy*0.75 + offset, 'o',
color=color, markersize=8, zorder=5)
short = room_name.replace('_', ' ')[:8]
ax.text(zx*0.75 + offset, zy*0.75 - 0.1, short,
ha='center', va='top', fontsize=5, color=color, zorder=6)
ax.set_title("Vastu Compass", color='#4FC3F7', fontsize=9, fontweight='bold')
# Legend
for label, col in [("β Correct", '#00E676'), ("~ Partial", '#FFFF00'), ("β Wrong", '#FF5252')]:
ax.plot([], [], 'o', color=col, label=label, markersize=6)
ax.legend(loc='lower center', fontsize=6.5, facecolor='#1A1A2E',
edgecolor='#4FC3F7', labelcolor='white', ncol=3,
bbox_to_anchor=(0.5, -0.08))
plt.tight_layout()
return fig
|