Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:00

0001 #!/usr/bin/env python
0002 
0003 import os, logging, numpy as np
0004 log = logging.getLogger(__name__)
0005 
0006 try:
0007     import pyvista as pv
0008 except ImportError:
0009     pv = None
0010 pass
0011 
0012 class Scan(object):
0013     @classmethod
0014     def Create(cls, n, modes):
0015         scan = cls()
0016         scan.ipos = np.zeros( (n*len(modes), 3) ) 
0017         scan.iray = np.zeros( (n*len(modes), 2, 3) ) 
0018         scan.isec = np.zeros( (n*len(modes), 4) ) 
0019         scan.topline = "created"
0020         scan.botline = ""
0021         scan.thirdline = ""
0022         scan.fold = None
0023         return scan 
0024 
0025     @classmethod
0026     def Load(cls, fold):
0027         scan = cls()
0028         log.info("loading from %s " % fold )
0029         scan.ipos = np.load(os.path.join(fold, "ipos.npy"))
0030         scan.iray = np.load(os.path.join(fold, "iray.npy"))
0031         scan.isec = np.load(os.path.join(fold, "isec.npy"))
0032         scan.topline = fold 
0033         scan.botline = ""
0034         scan.thirdline = ""
0035         scan.fold = fold
0036         return scan 
0037  
0038     @classmethod
0039     def XY(cls, geom, n=100, modes=[0,1,2,3], t_min=0, shifted=True ):
0040         scan = cls.Create(n, modes)
0041         offset = 0 
0042         for mode in modes: 
0043             for i in range(n):
0044                 j = i - n/2 
0045                 if mode == 0:  # shoot upwards from X axis, or shifted line
0046                     dx = 0
0047                     dy = 1
0048                     ox = j*0.1
0049                     oy = -10. if shifted else 0. 
0050                 elif mode == 1: # shoot downwards from X axis, or shifted line
0051                     dx = 0
0052                     dy = -1 
0053                     ox = j*0.1
0054                     oy = 10. if shifted else 0.  
0055                 elif mode == 2: # shoot to right from Y axis, or shifted line 
0056                     dx = 1
0057                     dy = 0 
0058                     ox = -10. if shifted else 0. 
0059                     oy = j*0.1 
0060                 elif mode == 3: # shoot to left from Y axis, or shifted line
0061                     dx = -1
0062                     dy = 0 
0063                     ox = 10. if shifted else 0.
0064                     oy = j*0.1 
0065                 pass
0066 
0067                 ray_origin    = np.array( [ox, oy, 0 ] )    
0068                 ray_direction = np.array( [dx, dy, 0 ] )    
0069                 isect = geom.intersect( ray_origin, ray_direction, t_min )
0070 
0071                 scan.iray[i+offset,0] = ray_origin
0072                 scan.iray[i+offset,1] = ray_direction
0073 
0074                 if not isect is None:
0075                     scan.ipos[i+offset] = isect[3]*ray_direction + ray_origin 
0076                     scan.isec[i+offset] = isect 
0077                 pass
0078             pass
0079             offset += n 
0080         pass
0081         return scan
0082 
0083     @classmethod
0084     def Plot(cls, scan):
0085     
0086         size = np.array([1280, 720])
0087         pl = pv.Plotter(window_size=size*2 )
0088         pl.view_xy() 
0089 
0090         pl.add_text(scan.topline, position="upper_left")
0091         pl.add_text(scan.botline, position="lower_left")
0092         pl.add_text(scan.thirdline, position="lower_right")
0093 
0094 
0095         limit = 100. 
0096         mask = np.logical_and( np.abs(scan.ipos[:,1]) < limit , np.abs(scan.ipos[:,0]) < limit )   
0097 
0098         isect_normals = True 
0099         if isect_normals:
0100             pl.add_arrows( scan.ipos[mask], scan.isec[mask, :3],  color="white" )  
0101         else:
0102             pl.add_points( scan.ipos[mask],  color="white" )  
0103         pass
0104         pl.add_points( scan.iray[:,0],  color="red" )  
0105 
0106         look = (0,0,0)
0107         up = (0,1,0)
0108         eye = (0,0, 10)
0109 
0110         pl.set_focus(    look )
0111         pl.set_viewup(   up )
0112         pl.set_position( eye, reset=True )   ## for reset=True to succeed to auto-set the view, must do this after add_points etc.. 
0113         pl.camera.Zoom(1)
0114         pl.show_grid()
0115 
0116         outpath = None
0117         if not scan.fold is None:
0118             outpath = os.path.join(scan.fold, "figs", "scanplot.png")
0119             outfold = os.path.dirname(outpath)
0120             if not os.path.isdir(outfold):
0121                 os.makedirs(outfold)
0122             pass
0123         pass
0124         if not outpath is None:
0125             log.info("screenshot outpath %s " % outpath)  
0126             cp = pl.show(screenshot=outpath)
0127         else:
0128             cp = pl.show()
0129         pass
0130         return cp 
0131 
0132