Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:49:37

0001 #!/usr/bin/env python3
0002 
0003 import argparse
0004 import logging
0005 import pathlib as pl
0006 import plotly.graph_objects as go
0007 import numpy as np
0008 
0009 from optiphy import csg
0010 
0011 logging.basicConfig(level=logging.INFO)
0012 
0013 
0014 def read_mesh(path_csg, path_rec, path_out):
0015     tri = np.load(path_csg/'tri.npy')
0016     vtx = np.load(path_csg/'vtx.npy')
0017 
0018     rays = []
0019     if path_rec:
0020         rec = np.load(path_rec/'record.npy')
0021         rays = csg.rays(rec)
0022 
0023     faces, edges = csg.mesh(tri, vtx)
0024 
0025     logging.info(path_csg.name)
0026 
0027     fig = go.Figure(data=[faces, edges, *rays])
0028     html_file = path_out / (str(path_csg).replace("/", "_").replace("\\", "_").replace(".", "") + '.html')
0029     fig.update_layout(scene_camera=dict( eye=dict(x=0, y=0, z=2), up=dict(x=0, y=1, z=0) ))
0030     fig.write_html(html_file, include_plotlyjs='cdn')
0031 
0032     logging.info(f'Saved image to: {html_file}')
0033 
0034 
0035 def read_meshgroup(path_csg: pl.Path, path_rec: pl.Path, path_out: pl.Path):
0036     name_index = path_csg/'NPFold_names.txt'
0037 
0038     volume_names = []
0039 
0040     if name_index.is_file():
0041         with name_index.open('r') as f:
0042             volume_names = f.read().splitlines()
0043     else:
0044         logging.fatal(f"NPFold_names.txt not found in {path_csg}")
0045 
0046     logging.debug(volume_names)
0047 
0048     fig = go.Figure()
0049 
0050     for subdir_idx, volume_name in enumerate(volume_names):
0051         tri_file = path_csg / f'{subdir_idx}/tri.npy'
0052         vtx_file = path_csg / f'{subdir_idx}/vtx.npy'
0053 
0054         if tri_file.is_file() and vtx_file.is_file():
0055             tri = np.load(tri_file)
0056             vtx = np.load(vtx_file)
0057 
0058             faces, edges = csg.mesh(tri, vtx, 'legendonly' if subdir_idx == 0 else True)
0059             faces.name = f'{volume_name} faces'
0060             edges.name = f'{volume_name} edges'
0061 
0062             fig.add_traces([faces, edges])
0063 
0064     rays = []
0065     suffix = ''
0066     if path_rec:
0067         rec = np.load(path_rec/'record.npy')
0068         rays = csg.rays(rec)
0069         fig.add_traces(rays)
0070         suffix = '_' + str(path_rec).replace("/", "_").replace("\\", "_").replace(".", "")
0071 
0072     path_out.mkdir(parents=True, exist_ok=True)
0073     html_file = path_out / (str(path_csg).replace("/", "_").replace("\\", "_").replace(".", "") + suffix + '.html')
0074     fig.update_layout(scene_camera=dict( eye=dict(x=-2, y=0, z=0), up=dict(x=0, y=1, z=0) ), scene_dragmode='orbit')
0075     fig.write_html(html_file, include_plotlyjs='cdn')
0076 
0077     logging.info(f'Saved image to: {html_file}')
0078 
0079 
0080 def main():
0081     parser = argparse.ArgumentParser()
0082     parser.add_argument('path', help="A path to CSG dir with tri.npy and vtx.npy files")
0083     parser.add_argument('-r', '--rays', help="A path to record.npy file")
0084     parser.add_argument('-o', '--outdir', default='/home/web/optiphy', help="A path to output dir")
0085 
0086     args = parser.parse_args()
0087 
0088     path_csg = pl.Path(args.path)
0089     path_rec = pl.Path(args.rays) if args.rays else None
0090     path_out = pl.Path(args.outdir)
0091     logging.info(f'Reading data from dir: {path_csg}')
0092 
0093     read_meshgroup(path_csg, path_rec, path_out)
0094 
0095 
0096 if __name__ == '__main__':
0097     main()