diff --git "a/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_0.stl" "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_0.stl" new file mode 100644 index 0000000..e11ec8a --- /dev/null +++ "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_0.stl" Binary files differ diff --git "a/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_1.stl" "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_1.stl" new file mode 100644 index 0000000..67a3544 --- /dev/null +++ "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_1.stl" Binary files differ diff --git "a/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_2.stl" "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_2.stl" new file mode 100644 index 0000000..2fd4cf3 --- /dev/null +++ "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_2.stl" Binary files differ diff --git "a/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_3.stl" "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_3.stl" new file mode 100644 index 0000000..9b33ced --- /dev/null +++ "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_3.stl" Binary files differ diff --git "a/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_4.stl" "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_4.stl" new file mode 100644 index 0000000..80facc2 --- /dev/null +++ "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_4.stl" Binary files differ diff --git "a/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_5.stl" "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_5.stl" new file mode 100644 index 0000000..ff2f6d6 --- /dev/null +++ "b/configs/meshes/smtias_v4/LED\343\203\242\343\202\270\343\203\245\343\203\274\343\203\253_board_5.stl" Binary files differ diff --git "a/configs/meshes/smtias_v4/\347\255\220\344\275\223.stl" "b/configs/meshes/smtias_v4/\347\255\220\344\275\223.stl" new file mode 100644 index 0000000..af8d509 --- /dev/null +++ "b/configs/meshes/smtias_v4/\347\255\220\344\275\223.stl" Binary files differ diff --git "a/configs/meshes/smtias_v4/\351\205\215\347\267\232.stl" "b/configs/meshes/smtias_v4/\351\205\215\347\267\232.stl" new file mode 100644 index 0000000..3e16159 --- /dev/null +++ "b/configs/meshes/smtias_v4/\351\205\215\347\267\232.stl" Binary files differ diff --git a/configs/scene_smtias_v4.yaml b/configs/scene_smtias_v4.yaml new file mode 100644 index 0000000..6f3fbb4 --- /dev/null +++ b/configs/scene_smtias_v4.yaml @@ -0,0 +1,411 @@ +name: smtias_v4 +description: Fusion の SmTIASv4 から書き出した筐体と LED モジュールの基板.LED は全部同じ高さ(TEST_21 結果 12) +geometry: + meshes: + - id: box_housing + path: meshes/smtias_v4/筐体.stl + unit: mm + material: printer_white + - id: evaluation_board + path: meshes/照明ムラ補正ボード.stl + unit: mm + material: printer_white + - id: led_cover_v4_0 + path: meshes/smtias_v4/LEDモジュール_board_0.stl + unit: mm + material: printer_white + - id: led_cover_v4_1 + path: meshes/smtias_v4/LEDモジュール_board_1.stl + unit: mm + material: printer_white + - id: led_cover_v4_2 + path: meshes/smtias_v4/LEDモジュール_board_2.stl + unit: mm + material: printer_white + - id: led_cover_v4_3 + path: meshes/smtias_v4/LEDモジュール_board_3.stl + unit: mm + material: printer_white + - id: led_cover_v4_4 + path: meshes/smtias_v4/LEDモジュール_board_4.stl + unit: mm + material: printer_white + - id: led_cover_v4_5 + path: meshes/smtias_v4/LEDモジュール_board_5.stl + unit: mm + material: printer_white + - id: wires + path: meshes/smtias_v4/配線.stl + unit: mm + material: holder_dark +materials: + printer_white: + type: diffuse + reflectance: + - 0.85 + - 0.85 + - 0.85 + holder_dark: + type: diffuse + reflectance: + - 0.5 + - 0.5 + - 0.5 + phone_body_dark: + type: diffuse + reflectance: + - 0.05 + - 0.05 + - 0.05 +lights: +- id: led_module0_0 + type: area + shape: rectangle + center: + - 17.0 + - -84.0 + - -38.0 + size: + - 2.8 + - 3.5 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module0_1 + type: area + shape: rectangle + center: + - 21.0 + - -84.0 + - -38.0 + size: + - 2.8 + - 3.5 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module0_2 + type: area + shape: rectangle + center: + - 25.0 + - -84.0 + - -38.0 + size: + - 2.8 + - 3.5 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module1_0 + type: area + shape: rectangle + center: + - -17.0 + - -84.0 + - -38.0 + size: + - 2.8 + - 3.5 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module1_1 + type: area + shape: rectangle + center: + - -21.0 + - -84.0 + - -38.0 + size: + - 2.8 + - 3.5 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module1_2 + type: area + shape: rectangle + center: + - -25.0 + - -84.0 + - -38.0 + size: + - 2.8 + - 3.5 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module2_0 + type: area + shape: rectangle + center: + - 74.0 + - 6.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module2_1 + type: area + shape: rectangle + center: + - 74.0 + - 10.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module2_2 + type: area + shape: rectangle + center: + - 74.0 + - 14.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module3_0 + type: area + shape: rectangle + center: + - -74.0 + - 6.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module3_1 + type: area + shape: rectangle + center: + - -74.0 + - 10.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module3_2 + type: area + shape: rectangle + center: + - -74.0 + - 14.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module4_0 + type: area + shape: rectangle + center: + - 74.0 + - 26.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module4_1 + type: area + shape: rectangle + center: + - 74.0 + - 30.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module4_2 + type: area + shape: rectangle + center: + - 74.0 + - 34.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module5_0 + type: area + shape: rectangle + center: + - -74.0 + - 26.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module5_1 + type: area + shape: rectangle + center: + - -74.0 + - 30.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +- id: led_module5_2 + type: area + shape: rectangle + center: + - -74.0 + - 34.0 + - -38.0 + size: + - 3.5 + - 2.8 + direction: + - 0.0 + - 0.0 + - -1.0 + radiance: + - 2807.2469840551203 + - 2807.2469840551203 + - 2807.2469840551203 +sensor: + type: orthographic + position: + - 0.0 + - 0.0 + - -50.0 + target: + - 0.0 + - 0.0 + - 0.0 + up: + - 0.0 + - 1.0 + - 0.0 + ortho_scale: + - 50.0 + - 52.0 + film: + width: 512 + height: 512 + rfilter: gaussian +render: + variant: llvm_ad_rgb + integrator: path + max_depth: 16 + spp: 4096 + seed: 0 diff --git a/scripts/build_fusion_v4_scene.py b/scripts/build_fusion_v4_scene.py new file mode 100644 index 0000000..82d7e08 --- /dev/null +++ b/scripts/build_fusion_v4_scene.py @@ -0,0 +1,113 @@ +"""Fusion の SmTIASv4 から書き出した筐体と LED モジュールの基板でシミュのシーンを作る. + +SmTIASv4(TEST_21 の L5 を Fusion で作ったもの)の筐体(コンポーネント「筐体」)と +LED モジュールの基板(コンポーネント「LEDモジュール」の「基板_*」)を STL に書き出したもの +(`--export-dir`.Fusion 側のスクリプトで書き出す)を configs/meshes/smtias_v4/ に写し, +LED(3 個 1 組 × 6.全部同じ高さ.TEST_21 結果 12)を Fusion の配置どおりに置いた +シーン YAML を書く. + +Fusion では顔に当たる面を Z = 0(今の装置と同じ)にしたので,箱の天井の内面は Z = -3 になる +(シミュの箱は天井の内面が Z = 0).LED は天井からの距離をシミュと揃えるため 3 mm 下げる. + +実行方法: + uv run python scripts/build_fusion_v4_scene.py --export-dir configs/_tmp_candidates/fusion_v4 + uv run python scripts/render_scene_candidate.py SmTIASv4_L5=configs/scene_smtias_v4.yaml +""" + +from __future__ import annotations + +import argparse +import math +import shutil +import sys +from dataclasses import replace +from pathlib import Path + +import trimesh +import yaml + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) + +from led_site_basis import build_modules_yaml, build_sites, parse_layout # noqa: E402 +from sweep_candidate_layouts import CANDIDATES, ROOT, build_candidate_yaml # noqa: E402 + +from sm_tias_lightsim.scene.paths import relative_mesh_path # noqa: E402 + +# LED を全部 Z −35 にそろえた配置(L5 の下の LED を Z −10 → −35・X ±36 → ±26.TEST_21 結果 12) +LAYOUT = "B_x26_z-35:1,R_y10_z-35:1,R_y30_z-35:1" +# 下の基板を研究室の材料 s10 に収まる長さにしたので,下の LED 3 個の中心を X ±26 → ±21 に寄せた +# (LED は ±17・21・25.generate_led_module_boards.BOTTOM_LEDS) +BOTTOM_X = 21.0 +CEILING_SHIFT = -3.0 # Fusion の天井の内面(Z = -3)とシミュの箱(Z = 0)の差 +MESH_DIR = ROOT / "configs" / "meshes" / "smtias_v4" +DEST = ROOT / "configs" / "scene_smtias_v4.yaml" + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("--export-dir", type=Path, required=True, + help="Fusion から書き出した housing_*.stl(前側・後側)と board_*.stl") + args = parser.parse_args(argv) + + MESH_DIR.mkdir(parents=True, exist_ok=True) + housing = MESH_DIR / "筐体.stl" + parts = sorted(args.export_dir.glob("housing_*.stl")) + if not parts: + raise ValueError(f"{args.export_dir} に housing_*.stl がありません") + # 前側と後側はつなぎ目で接するだけなので,1 つのメッシュにまとめて筐体とする + trimesh.util.concatenate([trimesh.load(p, force="mesh") for p in parts]).export(housing) + boards = [] + for src in sorted(args.export_dir.glob("board_*.stl")): + dst = MESH_DIR / f"LEDモジュール_{src.name}" + shutil.copyfile(src, dst) + boards.append(dst) + if not boards: + raise ValueError(f"{args.export_dir} に board_*.stl がありません") + + base_cand = next(c for c in CANDIDATES if c.name == "H_y90") + base_scene = build_candidate_yaml(replace(base_cand, name="led_sites_base", boards=())) + modules = [] + for site, gain in parse_layout(LAYOUT, {s.name: s for s in build_sites()}): + x, y, z = site.led + if site.wall == "bottom": + x = math.copysign(BOTTOM_X, x) # 左右対称に置いた左側は負 + modules.append((replace(site, led=(x, y, z + CEILING_SHIFT)), gain)) + tmp = build_modules_yaml("smtias_v4_tmp", modules, base_scene, "") + + config = yaml.safe_load(tmp.read_text(encoding="utf-8")) + meshes = [] + for mesh in config["geometry"]["meshes"]: + if mesh["id"].startswith("led_cover_") or mesh["id"] == "phone_window": + continue # 後ろの面は今の装置と同じ直径 10 mm のカメラの穴だけ(大きな窓は無い) + src = (tmp.parent / mesh["path"]).resolve() + if mesh["id"] == "box_housing": + src = housing + elif "_tmp_" in str(src): # サーバへ送らない一時フォルダのメッシュは写しておく + dst = MESH_DIR / src.name + shutil.copyfile(src, dst) + src = dst + mesh["path"] = relative_mesh_path(src, DEST.parent) + meshes.append(mesh) + for k, path in enumerate(boards): + meshes.append({"id": f"led_cover_v4_{k}", "path": relative_mesh_path(path, DEST.parent), + "unit": "mm", "material": "printer_white"}) + # 配線(Fusion の「配線」.黒い被覆の線を想定し,光を返しにくい暗い材質にする=一番悪い場合) + wires = sorted(args.export_dir.glob("wire_*.stl")) + if wires: + wire_mesh = MESH_DIR / "配線.stl" + trimesh.util.concatenate([trimesh.load(p, force="mesh") for p in wires]).export(wire_mesh) + meshes.append({"id": "wires", "path": relative_mesh_path(wire_mesh, DEST.parent), + "unit": "mm", "material": "holder_dark"}) + config["geometry"]["meshes"] = meshes + config["name"] = "smtias_v4" + config["description"] = ("Fusion の SmTIASv4 から書き出した筐体と LED モジュールの基板." + "LED は全部同じ高さ(TEST_21 結果 12)") + DEST.write_text(yaml.safe_dump(config, allow_unicode=True, sort_keys=False), + encoding="utf-8", newline="\n") + print(f"[saved] {DEST}(基板 {len(boards)} 枚・LED {len(config['lights'])} 個)") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/led_site_basis.py b/scripts/led_site_basis.py index 16778ea..e7c6208 100644 --- a/scripts/led_site_basis.py +++ b/scripts/led_site_basis.py @@ -157,6 +157,14 @@ for y in np.arange(0.0, 40.1, 5.0): sites.append(Site(f"R_y{y:g}_z{z:g}", "right", (wall_x, float(y), z), (0.0, 1.0), (-1.0, 0.0))) + # LED の Z をそろえる案(全部 Z −35 前後): 下の壁の Z −30〜−45 と横の壁の Z −45 + for z in (-30.0, -40.0, -45.0): + for x in (0.0, 13.0, 26.0, 39.0, 52.0): + sites.append(Site(f"B_x{x:g}_z{z:g}", "bottom", (x, wall_y, z), (1.0, 0.0), + (0.0, 1.0))) + for y in np.arange(0.0, 40.1, 5.0): + sites.append(Site(f"R_y{y:g}_z-45", "right", (wall_x, float(y), -45.0), (0.0, 1.0), + (-1.0, 0.0))) unique = {s.name: s for s in sites} return [s for s in unique.values() if module_fits(s) and camera_block(s) == 0.0] diff --git a/scripts/render_scene_candidate.py b/scripts/render_scene_candidate.py new file mode 100644 index 0000000..2cd277c --- /dev/null +++ b/scripts/render_scene_candidate.py @@ -0,0 +1,75 @@ +"""任意のシーン YAML を,候補比較(TEST_21)と同じ指標で seed 3 本レンダして集計する. + +結果は output/candidate_layouts/<名前>/s{seed}/ と summary_scene_<名前…>.csv に書くので, +scripts/build_candidate_comparison.py でほかの候補と並べて比べられる.Fusion から取り込んだ +シーンを候補と比べるときに使う. + +実行方法(研究室サーバで): + uv run python scripts/render_scene_candidate.py SmTIASv4_L5=configs/scene_smtias_v4.yaml +""" + +from __future__ import annotations + +import argparse +import csv +import sys +from dataclasses import replace +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) + +import yaml # noqa: E402 +from sweep_candidate_layouts import ( # noqa: E402 + CANDIDATES, + OUTPUT_BASE, + ROOT, + SUMMARY_COLUMNS, + TMP_DIR, + run_candidate, +) + +from sm_tias_lightsim.scene.paths import relative_mesh_path # noqa: E402 + + +def copy_to_tmp(scene: Path, name: str) -> Path: + """seed ごとの YAML を configs/ に散らかさないよう,シーンを一時フォルダへ写す.""" + config = yaml.safe_load(scene.read_text(encoding="utf-8")) + dest = TMP_DIR / f"scene_{name}.yaml" + dest.parent.mkdir(parents=True, exist_ok=True) + for mesh in config["geometry"]["meshes"]: + mesh["path"] = relative_mesh_path((scene.parent / mesh["path"]).resolve(), dest.parent) + dest.write_text(yaml.safe_dump(config, allow_unicode=True, sort_keys=False), + encoding="utf-8", newline="\n") + return dest + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("scenes", nargs="+", metavar="NAME=YAML") + parser.add_argument("--seeds", nargs="+", type=int, default=[0, 1, 2]) + args = parser.parse_args(argv) + + template = CANDIDATES[0] + names = [spec.split("=", 1)[0] for spec in args.scenes] + summary = OUTPUT_BASE / f"summary_scene_{'_'.join(names)}.csv" + OUTPUT_BASE.mkdir(parents=True, exist_ok=True) + with summary.open("w", newline="", encoding="utf-8") as f: + writer = csv.DictWriter(f, fieldnames=SUMMARY_COLUMNS) + writer.writeheader() + for spec in args.scenes: + name, path = spec.split("=", 1) + scene = (ROOT / path).resolve() + if not scene.exists(): + raise ValueError(f"シーンがありません: {scene}") + cand = replace(template, name=name, note=f"シーン {path}") + tmp = copy_to_tmp(scene, name) + for seed in args.seeds: + writer.writerow(run_candidate(cand, tmp, seed)) + f.flush() + print(f"[done] {summary.relative_to(ROOT)}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())