File size: 4,653 Bytes
1ca7ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Render the exact shapes that entered CFD: AnchorGen@60 vs PhysGen@60.

Per case, AG and PG meshes SHARE one parallel-projection camera (union bbox
center + scale) per view, so relative size/deformation is faithful. Views:
oblique (3D form), side (matches SIDE sketch silhouette), front (width).
Labels carry the real OpenFOAM9 pressure Cd (cd_pressure) for each mesh.
Convention copied from render_compare20.py (color #789bb7, parallel_scale=0.58*extent).
"""
from pathlib import Path
import json
import numpy as np
import pyvista as pv
from PIL import Image, ImageDraw, ImageFont

CFD = Path("/mnt/cvlab/scratch/cvlab/home/hantzhan/cfd_workflow")
OUT = CFD / "render_agpg60_cfd_shape_20260925"
(OUT / "panels").mkdir(parents=True, exist_ok=True)

CASES = ["pair0008_source001", "pair0116_source019", "pair0330_source055"]

def ag_mesh(c):
    return json.loads((CFD/"anchorgen_side60_20260923_v3/results"/c/"PREPARED.json").read_text())["states"]["final60"]["input"]["path"]
def pg_mesh(c):
    return json.loads((CFD/"physgen60_priority_20260924_v1/results"/c/"physgen60/PREPARED.json").read_text())["input_source"]["mesh"]
def ag_cdp(c):
    return json.loads((CFD/"anchorgen_side60_20260923_v3/results"/c/"final60/CFD_RESULT.json").read_text())["cd_pressure"]
def pg_cdp(c):
    return json.loads((CFD/"physgen60_priority_20260924_v1/results"/c/"physgen60/CFD_RESULT.json").read_text())["cd_pressure"]

VIEWS = [("oblique", (1.0, -1.0, 0.65)), ("side", (0.0, -1.0, 0.0)), ("front", (-1.0, 0.0, 0.0))]
MESH_COLOR = "#789bb7"
PW, PH = 360, 320
HEAD_H, LAB_H = 30, 40
NCOL = len(VIEWS) * 2                     # AG,PG per view
ROW_H = HEAD_H + LAB_H + PH
SHEET_W = NCOL * PW

try:
    F_HEAD = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
    F_LAB = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 15)
    F_SUB = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 13)
except OSError:
    F_HEAD = F_LAB = F_SUB = ImageFont.load_default()

def bounds_union(meshes):
    b = np.array([m.bounds for m in meshes])
    lo = b[:, [0, 2, 4]].min(0); hi = b[:, [1, 3, 5]].max(0)
    return (lo + hi) / 2.0, float((hi - lo).max())

def render_mesh(mesh, direction, center, extent):
    p = pv.Plotter(off_screen=True, window_size=(PW - 16, PH))
    p.set_background("white")
    p.add_mesh(mesh, color=MESH_COLOR, smooth_shading=True, specular=0.12)
    d = np.array(direction, float); d /= np.linalg.norm(d)
    pos = center + d * extent * 3.0
    p.camera_position = [tuple(pos), tuple(center), (0.0, 0.0, 1.0)]
    p.enable_parallel_projection()
    p.camera.parallel_scale = 0.58 * extent
    shot = p.screenshot(return_img=True); p.close()
    return Image.fromarray(shot).convert("RGB")

qc = []
rows = []
for c in CASES:
    agp, pgp = ag_mesh(c), pg_mesh(c)
    agcd, pgcd = ag_cdp(c), pg_cdp(c)
    ag, pg = pv.read(agp), pv.read(pgp)
    center, extent = bounds_union([ag, pg])
    winner = "AnchorGen" if agcd < pgcd else "PhysGen"
    row = Image.new("RGB", (SHEET_W, ROW_H), "white")
    dr = ImageDraw.Draw(row)
    dr.text((10, 5), f"{c}   AG60 cd_p={agcd:.5f}   PG60 cd_p={pgcd:.5f}   lower(better)={winner}",
            fill="black", font=F_HEAD)
    col = 0
    for vname, direction in VIEWS:
        for tag, mesh, cd in [("AnchorGen@60", ag, agcd), ("PhysGen@60", pg, pgcd)]:
            img = render_mesh(mesh, direction, center, extent)
            x0 = col * PW
            lab_color = "red" if ((tag.startswith("AnchorGen")) == (agcd < pgcd)) else "black"
            dr.text((x0 + 8, HEAD_H + 2), f"{tag}", fill=lab_color, font=F_LAB)
            dr.text((x0 + 8, HEAD_H + 20), f"{vname}  cd_p {cd:.5f}", fill="#333333", font=F_SUB)
            row.paste(img, (x0 + (PW - img.width)//2, HEAD_H + LAB_H + (PH - img.height)//2))
            img.save(OUT/"panels"/f"{c}_{tag.split('@')[0]}_{vname}.png")
            col += 1
    row.save(OUT / f"row_{c}.png")
    rows.append(row)
    qc.append(dict(case=c, ag_mesh=agp, pg_mesh=pgp, ag_cd_pressure=agcd, pg_cd_pressure=pgcd,
                   ag_points=int(ag.n_points), ag_faces=int(ag.n_cells),
                   pg_points=int(pg.n_points), pg_faces=int(pg.n_cells),
                   union_extent=extent, lower_drag=winner))
    print("RENDERED", c, flush=True)

sheet = Image.new("RGB", (SHEET_W, ROW_H * len(rows)), "white")
for i, im in enumerate(rows):
    sheet.paste(im, (0, i * ROW_H))
sheet.save(OUT / "overview_ag60_vs_pg60_cfd_shape.png")
(OUT / "QC.json").write_text(json.dumps(qc, indent=2) + "\n")
print("DONE ->", OUT / "overview_ag60_vs_pg60_cfd_shape.png")