build: add offline two-page preview
This commit is contained in:
@@ -7,6 +7,10 @@
|
||||
|
||||

|
||||
|
||||
> **离线预览包:** 直接打开 [`preview/index.html`](preview/index.html)(占用拟合)或
|
||||
> [`preview/packing.html`](preview/packing.html)(装箱模拟)。两页都已内联全部 CSS、JS 和数据,
|
||||
> 把整个 `preview/` 文件夹拷到另一台电脑即可双击查看,不需要 Python / OCP / Web 服务器。
|
||||
|
||||
## 这个项目做了什么
|
||||
|
||||
- **读取 STEP**:用 OpenCascade(OCP 8.0)导入 `data/AURORA_A1.step`,精确计算 B-Rep
|
||||
@@ -60,7 +64,10 @@ python src/fetch_asset.py
|
||||
# 2) 运行推理,生成 web/data.js、out/report.json、out/metrics.csv
|
||||
python src/inference.py
|
||||
|
||||
# 3) 启动静态服务(推荐;也可以直接双击 web/index.html)
|
||||
# 3) 可选:重新生成两页离线预览包 preview/
|
||||
python src/build_preview.py
|
||||
|
||||
# 4) 启动静态服务(推荐;也可以直接双击 web/index.html 或 preview/index.html)
|
||||
python src/serve.py --port 8000
|
||||
# 打开 http://127.0.0.1:8000/web/index.html
|
||||
```
|
||||
@@ -233,6 +240,30 @@ surface/material occupancy,而不是完整的“车辆实体障碍物”语义
|
||||
页面数据全部内嵌在 `web/data.js`,不依赖后端或网络;OCP 只在离线推理阶段使用。
|
||||
three.js 和 OrbitControls 已放在 `web/vendor/`,可离线打开。
|
||||
|
||||
## 离线预览包(`preview/`)
|
||||
|
||||
如果要把页面发给没有 Python/OCP 的同事,直接使用仓库里的 `preview/` 文件夹:
|
||||
|
||||
```text
|
||||
preview/
|
||||
├── index.html # 占用拟合页,约 3.5 MB
|
||||
└── packing.html # 装箱模拟页,约 0.75 MB
|
||||
```
|
||||
|
||||
两页都是**完全自包含**的 HTML:`style.css`、`packing.css`、three.js、OrbitControls、
|
||||
`data.js`、`app.js`、`car-voxels.js`、`packing.js` 全部内联,页面顶部「占用拟合 / 装箱模拟」
|
||||
页签可以在两个文件之间切换。把整个 `preview/` 文件夹拷走,双击 `index.html` 即可;
|
||||
不需要安装依赖,也不需要启动服务器。
|
||||
|
||||
重新生成(修改 `web/` 或重新跑推理后):
|
||||
|
||||
```bash
|
||||
python src/build_preview.py
|
||||
```
|
||||
|
||||
> `preview/` 是构建产物,提交进仓库是为了让非开发同事直接下载使用;
|
||||
> 真正的源文件仍然是 `web/`。
|
||||
|
||||
## 装箱模拟(新页签)
|
||||
|
||||
顶部页签切到「装箱模拟」(`web/packing.html`),是一套可以玩的装箱推演:
|
||||
@@ -310,6 +341,7 @@ occupancy-fitting/
|
||||
│ ├── geometry.py # OCP 读取 / 体积 / 网格 / 三种包络体
|
||||
│ ├── voxelize.py # 射线奇偶填充 + 粗体素聚合 + 指标
|
||||
│ ├── inference.py # 主推理 CLI + Web 数据导出
|
||||
│ ├── build_preview.py # 生成两页离线预览包 preview/
|
||||
│ └── serve.py # 零依赖静态服务器
|
||||
├── web/
|
||||
│ ├── index.html
|
||||
@@ -320,6 +352,7 @@ occupancy-fitting/
|
||||
│ ├── packing.js # 浏览器内的装箱引擎 + 3D 场景
|
||||
│ ├── packing.css
|
||||
│ └── vendor/ # three.js r128 + OrbitControls(MIT)
|
||||
├── preview/ # 两页离线预览包(index.html + packing.html)
|
||||
├── android/ # WebView 壳工程,打包成 APK(见 android/README.md)
|
||||
├── out/
|
||||
│ ├── report.json # 完整结果与每种边长的指标
|
||||
|
||||
+2820
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build a two-page, fully offline preview bundle.
|
||||
|
||||
Outputs:
|
||||
preview/index.html -- occupancy fitting page
|
||||
preview/packing.html -- container packing simulator page
|
||||
|
||||
All CSS, three.js, OrbitControls, data and application JS are inlined. The
|
||||
two pages link to each other through their existing page tabs, so the whole
|
||||
bundle can be copied to another machine and opened with a double click.
|
||||
No Python / OCP / web server is required to view it.
|
||||
|
||||
ven/bin/python src/build_preview.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
WEB = ROOT / "web"
|
||||
|
||||
PAGES = {
|
||||
"index.html": {
|
||||
"css": ["style.css"],
|
||||
"js": [
|
||||
("vendor/three.min.js", "three.js r128"),
|
||||
("vendor/OrbitControls.js", "OrbitControls"),
|
||||
("data.js", "OCCUPANCY_DATA (mesh + all voxel sizes)"),
|
||||
("app.js", "occupancy application"),
|
||||
],
|
||||
},
|
||||
"packing.html": {
|
||||
"css": ["style.css", "packing.css"],
|
||||
"js": [
|
||||
("vendor/three.min.js", "three.js r128"),
|
||||
("vendor/OrbitControls.js", "OrbitControls"),
|
||||
("car-voxels.js", "car voxel data"),
|
||||
("packing.js", "container packing engine"),
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def inline_css(html: str, filename: str) -> str:
|
||||
css = (WEB / filename).read_text(encoding="utf-8")
|
||||
pattern = re.compile(
|
||||
r'<link rel="stylesheet" href="'
|
||||
+ re.escape(filename)
|
||||
+ r'(?:\?[^"]*)?"\s*/?>'
|
||||
)
|
||||
if len(pattern.findall(html)) != 1:
|
||||
raise RuntimeError(f"could not find exactly one CSS link for {filename}")
|
||||
return pattern.sub(lambda _m: f"<style>\n/* {filename} inlined */\n{css}\n</style>", html, count=1)
|
||||
|
||||
|
||||
def inline_script(html: str, src: str, label: str) -> str:
|
||||
content = (WEB / src).read_text(encoding="utf-8")
|
||||
content = content.replace("</script", "<\\/script")
|
||||
pattern = re.compile(
|
||||
r'<script src="' + re.escape(src) + r'(?:\?[^"]*)?"></script>'
|
||||
)
|
||||
if len(pattern.findall(html)) != 1:
|
||||
raise RuntimeError(f"could not find exactly one script tag for {src}")
|
||||
replacement = (
|
||||
f"<script>\n/* {label} inlined by build_preview.py */\n{content}\n</script>"
|
||||
)
|
||||
return pattern.sub(lambda _m: replacement, html, count=1)
|
||||
|
||||
|
||||
def build_page(name: str, out_dir: Path) -> Path:
|
||||
spec = PAGES[name]
|
||||
html = (WEB / name).read_text(encoding="utf-8")
|
||||
for filename in spec["css"]:
|
||||
html = inline_css(html, filename)
|
||||
for src, label in spec["js"]:
|
||||
html = inline_script(html, src, label)
|
||||
html = (
|
||||
"<!-- offline preview generated by src/build_preview.py; "
|
||||
"do not edit this file by hand -->\n" + html
|
||||
)
|
||||
out = out_dir / name
|
||||
out.write_text(html, encoding="utf-8")
|
||||
leftovers = re.findall(r'<script src="|<link rel="stylesheet"', html)
|
||||
if leftovers:
|
||||
raise RuntimeError(f"{name} still has external script/style resources")
|
||||
return out
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser(description=__doc__)
|
||||
ap.add_argument("--out-dir", type=Path, default=ROOT / "preview")
|
||||
args = ap.parse_args()
|
||||
args.out_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
total = 0
|
||||
for name in PAGES:
|
||||
out = build_page(name, args.out_dir)
|
||||
size = out.stat().st_size
|
||||
total += size
|
||||
print(f"wrote {out} ({size/1e6:.2f} MB)")
|
||||
print(f"total preview bundle: {total/1e6:.2f} MB")
|
||||
print(f"open {args.out_dir / 'index.html'} (occupancy) or {args.out_dir / 'packing.html'} (packing)")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user