phg-vis 1.3.2__py3-none-any.whl → 1.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
phg/web_three.py ADDED
@@ -0,0 +1,172 @@
1
+ """
2
+ Three.js web viewer for PHG OBJ exports.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import html
8
+ import os
9
+ from functools import partial
10
+ from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
11
+ from typing import Optional
12
+
13
+
14
+ def _relpath_for_url(path: str, base_dir: str) -> str:
15
+ rel = os.path.relpath(os.path.abspath(path), base_dir)
16
+ return rel.replace("\\", "/")
17
+
18
+
19
+ def write_three_view_html(obj_path: str, out_html: str, title: str = "PHG Web Viewer") -> str:
20
+ base_dir = os.path.dirname(os.path.abspath(out_html))
21
+ rel_obj = _relpath_for_url(obj_path, base_dir)
22
+
23
+ doc = f"""<!DOCTYPE html>
24
+ <html>
25
+ <head>
26
+ <meta charset="utf-8" />
27
+ <title>{html.escape(title)}</title>
28
+ <style>
29
+ :root {{
30
+ --bg: #f6f3ee;
31
+ --panel: #ffffff;
32
+ --text: #1d1d1f;
33
+ --line: #dcd7cf;
34
+ --accent: #1b5e6b;
35
+ }}
36
+ * {{ box-sizing: border-box; }}
37
+ body {{
38
+ margin: 0;
39
+ background: var(--bg);
40
+ color: var(--text);
41
+ font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
42
+ }}
43
+ #app {{
44
+ position: fixed;
45
+ inset: 0;
46
+ }}
47
+ canvas {{ display: block; }}
48
+ .hud {{
49
+ position: absolute;
50
+ top: 16px;
51
+ left: 16px;
52
+ background: rgba(255,255,255,0.9);
53
+ border: 1px solid var(--line);
54
+ border-radius: 10px;
55
+ padding: 10px 12px;
56
+ box-shadow: 0 6px 18px rgba(0,0,0,0.08);
57
+ font-size: 12px;
58
+ line-height: 1.5;
59
+ }}
60
+ .hud strong {{ color: var(--accent); }}
61
+ </style>
62
+ </head>
63
+ <body>
64
+ <div id="app"></div>
65
+ <div class="hud">
66
+ <div><strong>PHG Web Viewer</strong></div>
67
+ <div>OBJ: {html.escape(os.path.basename(obj_path))}</div>
68
+ <div>Controls: LMB rotate, RMB pan, wheel zoom</div>
69
+ </div>
70
+ <script type="importmap">
71
+ {{
72
+ "imports": {{
73
+ "three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
74
+ "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"
75
+ }}
76
+ }}
77
+ </script>
78
+ <script type="module">
79
+ import * as THREE from "three";
80
+ import {{ OrbitControls }} from "three/addons/controls/OrbitControls.js";
81
+ import {{ OBJLoader }} from "three/addons/loaders/OBJLoader.js";
82
+
83
+ const container = document.getElementById("app");
84
+ const scene = new THREE.Scene();
85
+ scene.background = new THREE.Color(0xf6f3ee);
86
+
87
+ const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000);
88
+ camera.position.set(6, 6, 10);
89
+
90
+ const renderer = new THREE.WebGLRenderer({{ antialias: true }});
91
+ renderer.setPixelRatio(window.devicePixelRatio || 1);
92
+ renderer.setSize(window.innerWidth, window.innerHeight);
93
+ container.appendChild(renderer.domElement);
94
+
95
+ const controls = new OrbitControls(camera, renderer.domElement);
96
+ controls.enableDamping = true;
97
+ controls.dampingFactor = 0.08;
98
+
99
+ const lightA = new THREE.DirectionalLight(0xffffff, 0.9);
100
+ lightA.position.set(6, 10, 8);
101
+ scene.add(lightA);
102
+ scene.add(new THREE.AmbientLight(0xffffff, 0.35));
103
+
104
+ const loader = new OBJLoader();
105
+ loader.load("{html.escape(rel_obj)}", (obj) => {{
106
+ obj.traverse((child) => {{
107
+ if (child.isMesh) {{
108
+ child.material = new THREE.MeshStandardMaterial({{
109
+ color: 0x9fb3c8,
110
+ roughness: 0.55,
111
+ metalness: 0.05
112
+ }});
113
+ }}
114
+ }});
115
+ scene.add(obj);
116
+
117
+ const box = new THREE.Box3().setFromObject(obj);
118
+ const size = box.getSize(new THREE.Vector3());
119
+ const center = box.getCenter(new THREE.Vector3());
120
+ const maxDim = Math.max(size.x, size.y, size.z) || 1;
121
+ const camDist = maxDim * 2.2;
122
+ camera.position.set(center.x + camDist, center.y + camDist, center.z + camDist);
123
+ controls.target.copy(center);
124
+ controls.update();
125
+ }});
126
+
127
+ function onResize() {{
128
+ camera.aspect = window.innerWidth / window.innerHeight;
129
+ camera.updateProjectionMatrix();
130
+ renderer.setSize(window.innerWidth, window.innerHeight);
131
+ }}
132
+ window.addEventListener("resize", onResize);
133
+
134
+ function animate() {{
135
+ requestAnimationFrame(animate);
136
+ controls.update();
137
+ renderer.render(scene, camera);
138
+ }}
139
+ animate();
140
+ </script>
141
+ </body>
142
+ </html>
143
+ """
144
+ with open(out_html, "w", encoding="utf-8") as f:
145
+ f.write(doc)
146
+ return out_html
147
+
148
+
149
+ def serve_three_view(
150
+ obj_path: str,
151
+ html_path: Optional[str] = None,
152
+ host: str = "127.0.0.1",
153
+ port: int = 8766,
154
+ title: str = "PHG Web Viewer",
155
+ ) -> str:
156
+ if html_path is None:
157
+ base, _ = os.path.splitext(obj_path)
158
+ html_path = base + ".html"
159
+ write_three_view_html(obj_path, html_path, title=title)
160
+
161
+ directory = os.path.dirname(os.path.abspath(html_path)) or os.getcwd()
162
+ handler = partial(SimpleHTTPRequestHandler, directory=directory)
163
+ server = ThreadingHTTPServer((host, port), handler)
164
+ url = f"http://{host}:{port}/{os.path.basename(html_path)}"
165
+ print(url)
166
+ try:
167
+ server.serve_forever()
168
+ except KeyboardInterrupt:
169
+ pass
170
+ finally:
171
+ server.server_close()
172
+ return url
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: phg_vis
3
- Version: 1.3.2
3
+ Version: 1.4.1
4
4
  Summary: A package for the PHG modeling language and 3D visualization tool.
5
5
  Home-page: https://github.com/panguojun/Coordinate-System
6
6
  Author: romeosoft
@@ -12,6 +12,11 @@ Classifier: Operating System :: Microsoft :: Windows
12
12
  Requires-Python: >=3.8
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE
15
+ Requires-Dist: psutil
16
+ Provides-Extra: shader
17
+ Requires-Dist: numpy; extra == "shader"
18
+ Requires-Dist: pygame; extra == "shader"
19
+ Requires-Dist: PyOpenGL; extra == "shader"
15
20
 
16
21
  # PHG - Python Hypergraphics Library
17
22
 
@@ -27,6 +32,8 @@ PHG (Python Hypergraphics) is a powerful 3D modeling and visualization library t
27
32
  - **Pipe Visualization** - Specialized system for visualizing pipe structures with world/local coordinate support
28
33
  - **Shader Conversion** - Convert PHG scripts to GLSL shaders for GPU rendering
29
34
  - **Dual Rendering Modes** - Both real-time visualization (vis.exe) and offscreen rendering
35
+ - **AI Parser Bridge** - Use DGE++ PHG parser to normalize/enhance scripts before visualization
36
+ - **Web Viewer** - Export OBJ and view in a three.js browser viewer
30
37
  - **Python Integration** - Comprehensive Python API for all features
31
38
 
32
39
  ---
@@ -42,6 +49,11 @@ Or install from source:
42
49
  pip install phg
43
50
  ```
44
51
 
52
+ Shader renderer dependencies (optional):
53
+ ```bash
54
+ pip install phg_vis[shader]
55
+ ```
56
+
45
57
  ---
46
58
 
47
59
  ## 🚀 Quick Start
@@ -86,6 +98,49 @@ phg.image(scene, filename="tower.png")
86
98
  phg.image("sphere(5);|||view=2 width=1920 height=1080", filename="top_view.png")
87
99
  ```
88
100
 
101
+ ---
102
+
103
+ ## ✨ Unified Visualization API
104
+
105
+ ```python
106
+ import phg
107
+
108
+ # Traditional vis.exe
109
+ phg.visualize("box(10);", mode="vis")
110
+
111
+ # Shader ray-march preview (GLSL)
112
+ phg.visualize("sphere(5);", mode="shader", width=800, height=600)
113
+
114
+ # Web (three.js) viewer
115
+ phg.visualize("box(10);", mode="web", out_html="view.html")
116
+ ```
117
+
118
+ ### AI Parser Bridge (for improved PHG)
119
+ Use DGE++ PHG parser to normalize/enhance scripts before visualization:
120
+
121
+ ```python
122
+ import phg
123
+
124
+ phg.visualize(script, mode="vis", ai=True)
125
+ phg.visualize(script, mode="shader", ai=True)
126
+ phg.visualize(script, mode="web", ai=True, out_html="ai_view.html")
127
+ ```
128
+
129
+ Environment override (optional):
130
+ ```
131
+ set PHG_AI_ROOT=C:\Users\18858\Documents\_AILab\DGE++\PHG
132
+ ```
133
+
134
+ Web live server (serves HTML + OBJ):
135
+ ```python
136
+ phg.web_serve(script, host="127.0.0.1", port=8766, ai_root=None)
137
+ ```
138
+
139
+ Notes for web viewer:
140
+ - Uses three.js CDN (requires network access)
141
+ - Uses AI bridge to export OBJ; ensure DGE++ is available
142
+ - Use `web_serve` to avoid file:// loader restrictions
143
+
89
144
  ---
90
145
 
91
146
  ## 📖 Syntax Reference
@@ -1,8 +1,11 @@
1
- phg/__init__.py,sha256=kH5hF-vqbLqzBE_xkOYabiYFtdb4CXtlwl67uPbDHpo,10767
1
+ phg/__init__.py,sha256=13T-d99gAO94paZ9gWXLvJ8pIWkWF7RY6Ee3thBK8BY,13241
2
+ phg/ai_bridge.py,sha256=6cAewSwrruS-Sdr6ffhqYP6ti72FZz8FhkSuLHfMpSA,6671
3
+ phg/gcu_api.py,sha256=yjCWAJPyIAL_fAdj2umX39wNmw56-LmkcmUPqt3V8DI,22957
2
4
  phg/phg_to_shader.py,sha256=rb_LnGE1Py08oz72ES5CnjyCDbjVWDU3If0cYZq4SUU,15172
3
- phg/pipe_string_phg.py,sha256=xoG8AtHRae_sOskSZxTc7lBgwnemv1u8Ko9GvnvHr8Q,16257
4
- phg/shader_render.py,sha256=_T8IFp1h5Hcj8TMMHwFMbxhGdHLnOqka7pJ4pde7zMA,16780
5
- phg/visphg.py,sha256=CtALoNJs0eDT8wnXk_76XG4UsOcV6cWucvWqz7AhOSc,1923
5
+ phg/pipe_string_phg.py,sha256=9YW5wewLUmyqGUnYNBYpCyk_SMS6fBfyPWTnniy6xdY,16589
6
+ phg/shader_render.py,sha256=ncQQHp5OMlOVeTsoIHOL2xnc6SQct7B8YRKPA-D9FlM,17278
7
+ phg/visphg.py,sha256=TpT6icBoBmPCbCm212qKC9zft9MQ2UK2dWvmgnlupwQ,2163
8
+ phg/web_three.py,sha256=LU6zi1kxk5ARMKV-xCo-1sKVEJCq5a2oWhJQ4lpCZEw,5086
6
9
  phg/vis/GCU.dll,sha256=k_slWZ5nOzZNi9zCe0_SKc0F7zXoLyRuRDomyirPY4I,2687488
7
10
  phg/vis/dragpad.exe,sha256=4qWG4VH1vginIGpKkBHwEBFlpTY8G6xPejeloJurNkE,51712
8
11
  phg/vis/dragpad_config.ini,sha256=MD5no-RjzzcBxyz3JWuI88qH5Q4WVivRLqk_1GE5VQY,78
@@ -15,8 +18,8 @@ phg/vis/vis.exe,sha256=9LGgETiO4lwhz_gJzdUg6gPP9EwGLT5SuLjN3wR2sso,1388032
15
18
  phg/vis/vis.ini,sha256=IjS0Av2BqresPR2oGCOoq5zliXN2wFRMK3rXcU15vKs,161
16
19
  phg/vis/zlib1.dll,sha256=oVw_r326ve3oveVZhlSiF8BloZQ6lfqXJEAhUYfMjOQ,89088
17
20
  phg/vis/imgui/main.lua,sha256=AIB5dpGrPHXuRiNW7Bg7eu5Lpi2DDcn6n4JS7OS1Hdk,4742
18
- phg_vis-1.3.2.dist-info/LICENSE,sha256=tDnRkJxBYPzWdfh2gArRqrUPJxQZRZHJVs68qqBHIq4,1083
19
- phg_vis-1.3.2.dist-info/METADATA,sha256=gMSdSe_22WeBMCBSxhLdpIYV4EbiP7NhJPVf_FnKQKU,17945
20
- phg_vis-1.3.2.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
21
- phg_vis-1.3.2.dist-info/top_level.txt,sha256=5GGhpTP8yi-zTXwW2HrI-zcxKxIM_nHXhL7r0iIDE_k,4
22
- phg_vis-1.3.2.dist-info/RECORD,,
21
+ phg_vis-1.4.1.dist-info/LICENSE,sha256=tDnRkJxBYPzWdfh2gArRqrUPJxQZRZHJVs68qqBHIq4,1083
22
+ phg_vis-1.4.1.dist-info/METADATA,sha256=PC4avYS-uAGQBOTVMUDm4vozjYR-ffVa37w4yhJ0WIQ,19424
23
+ phg_vis-1.4.1.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
24
+ phg_vis-1.4.1.dist-info/top_level.txt,sha256=5GGhpTP8yi-zTXwW2HrI-zcxKxIM_nHXhL7r0iIDE_k,4
25
+ phg_vis-1.4.1.dist-info/RECORD,,