Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add script to visualize CSG mesh with plotly #63

Merged
merged 6 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ export TMP=/tmp/myname
export GEOM=mygeom
export OPTICKS_EVENT_MODE=DebugLite
```


## Visualization

Plot any volume serialized

```
scripts/plot-csg.py ../out/csg/CSGFoundry/SSim/scene/meshmerge/0/
```
25 changes: 1 addition & 24 deletions plot_csg_tree.py → g4ox/csg.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import numpy as np
import plotly.graph_objects as go

from opticks.CSG.CSGFoundry import CSGFoundry

mesh = None

def set_csg(path):
global mesh
cf = CSGFoundry.Load(path)
mesh = cf.sim.stree.mesh


def create_mesh(mesh_subdir):
vtx = mesh_subdir.vtx
tri = mesh_subdir.tri

def mesh(tri, vtx):
# close triangles and create edge coordinates
tri_close = np.hstack((tri, tri[:,0][:,np.newaxis]))
tri_sides = np.array([[None]*3 if i is None else vtx[i].tolist() for t in tri_close for i in list(t)+[None]], dtype=float)
Expand All @@ -23,14 +11,3 @@ def create_mesh(mesh_subdir):
edges = go.Scatter3d(x=tri_sides.T[0], y=tri_sides.T[1], z=tri_sides.T[2], mode='lines', line_width=5)

return faces, edges


def fig_show(data):
fig = go.Figure(data=data)
fig.show()
return fig


def fig_add(fig, data):
fig.add_traces(data)
return fig
19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.poetry]
name = "g4ox"
version = "0.0.0"
description = ""
authors = ["BNL NPPS"]
readme = "README.md"
packages = [{include = "g4ox"}]

[tool.poetry.dependencies]
python = "^3.10"
numpy = "^2.1"
plotly = "^5.22.0"
nbformat = "^5.10.4"
flask = "^3.1.0"
flask-autoindex = "^0.6.6"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
40 changes: 40 additions & 0 deletions scripts/plot-csg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3

import argparse
import logging
import pathlib as pl
import plotly.graph_objects as go
import numpy as np

from g4ox import csg

logging.basicConfig(level=logging.INFO)


def read_mesh(path_csg, path_out):
tri = np.load(path_csg/'tri.npy')
vtx = np.load(path_csg/'vtx.npy')

faces, edges = csg.mesh(tri, vtx)

logging.info(path_csg.name)

fig = go.Figure(data=[faces, edges])
html_file = path_out / (str(path_csg).replace("/", "_").replace("\\", "_").replace(".", "") + '.html')
fig.write_html(html_file, include_plotlyjs='cdn')

logging.info(f'Saved image to: {html_file}')


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('path', help="A path to CSG dir")
parser.add_argument('-o', '--outdir', default='/home/web/g4ox', help="A path to output dir")

args = parser.parse_args()

path_csg = pl.Path(args.path)
path_out = pl.Path(args.outdir)
logging.info(f'Reading data from dir: {path_csg}')

read_mesh(path_csg, path_out)
20 changes: 20 additions & 0 deletions scripts/serve-path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3

import argparse

from flask import Flask
from flask_autoindex import AutoIndex

app = Flask(__name__)

def run_autoindex(path):
AutoIndex(app, browse_root=path)

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("path", nargs='?', default='iframe_figures/', help="A path to serve")

args = parser.parse_args()

run_autoindex(args.path)
app.run(host='0.0.0.0', port=8080)