Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #!/usr/bin/env python
0002 
0003 import numpy as np
0004 
0005 ENTER = 0
0006 EXIT = 1
0007 MISS = 2 
0008 
0009 STATE = { ENTER:"ENTER", EXIT:"EXIT", MISS:"MISS" }
0010 
0011 
0012 def dump(pending):
0013     print(" pending : %s " % bin(pending))
0014     for i in range(8):
0015         if (pending >> i) & 0x1 == 1:
0016             print("bit %i is set " % i)
0017         pass
0018     pass
0019 
0020 """
0021 
0022     |-----|
0023         |-------|
0024               |------|
0025              
0026 """
0027 
0028 
0029 class MockSolid(object):
0030     def __init__(self):
0031         n = 8 
0032         a = np.zeros( [n,2], dtype=np.float32 )
0033         c = np.linspace(0,70,8)  
0034         a[:,0] = c - 6     
0035         a[:,1] = c + 6  
0036 
0037         a[-1] = (100,110)  # make a disjoint 
0038 
0039         self.a = a 
0040         self.n = len(a) 
0041 
0042     def intersect_pending(self, tmin ):
0043         """
0044         pending approach has to keep repeating getting entrance distances
0045         better to collect the enters and sort to avoid that 
0046         """
0047 
0048         a = self.a 
0049         pending = np.uint8( (0x1 << 8) - 1 )
0050         farthest_exit = 0
0051 
0052         stage = "init"   
0053         print("  %20s :   pending  : %s  farthest_exit : %s    " % (stage, bin(pending), farthest_exit) )
0054 
0055         for i in range(len(a)):
0056              t, state = self.intersect_sub(i, tmin)
0057              if state == EXIT:
0058                  if t > farthest_exit: 
0059                      farthest_exit = t   
0060                  pass
0061              pass   
0062              mask = np.uint8(0x1 << i)
0063              if state == EXIT or state == MISS: pending &= ~mask 
0064         pass
0065 
0066         stage = "after pass1"
0067         print("  %20s :   pending  : %s  farthest_exit : %s    " % (stage, bin(pending), farthest_exit) )
0068 
0069         loop = 0 
0070 
0071         while pending:
0072             loop += 1 
0073             initial_farthest_exit = farthest_exit
0074             for i in range(len(a)): 
0075                 mask = np.uint8(0x1 << i)
0076                 if (pending >> i) & 0x1 == 1:
0077                     t_enter, state = self.intersect_sub(i, tmin)
0078                     assert state == ENTER 
0079                     print("t_enter %s  farthest_exit %s " % (t_enter,farthest_exit) )
0080                     if t_enter < farthest_exit:
0081                         t_advanced = t_enter+0.0001
0082                         t_exit, state = self.intersect_sub(i, t_advanced)
0083                         assert state == EXIT
0084                         print("t_exit %s  farthest_exit %s  " % (t_exit,farthest_exit) )
0085                         if t_exit > farthest_exit: 
0086                             farthest_exit = t_exit         
0087                         pass
0088                         pending &= ~mask
0089                     pass
0090                 pass
0091             pass
0092             stage = "loop %d " % loop
0093             print("  %20s :   pending  : %s  farthest_exit : %s    " % (stage, bin(pending), farthest_exit) )
0094             if farthest_exit == initial_farthest_exit: break 
0095         pass
0096         return farthest_exit
0097 
0098     def intersect_sorting(self, tmin ):
0099         a = self.a 
0100         farthest_exit = 0
0101 
0102         stage = "init"   
0103         print("  %20s :  farthest_exit : %s    " % (stage, farthest_exit) )
0104 
0105         enter = np.zeros(8, dtype=np.float32) 
0106 
0107         # 1st pass : find farthest_exit and collect enter distances 
0108         for i in range(len(a)):
0109              enter[i] = np.nan    # argsort treats nan and inf as large values
0110              t, state = self.intersect_sub(i, tmin)
0111              if state == EXIT:
0112                  if t > farthest_exit: 
0113                      farthest_exit = t   
0114                  pass
0115              elif state == ENTER:
0116                  enter[i] = t 
0117              pass   
0118         pass
0119 
0120         stage = "after pass1"
0121         print("  %20s :   s  farthest_exit : %s    " % (stage, farthest_exit) )
0122 
0123         # only need to sort the enter, but need to maintain state on all 
0124         idx = np.argsort(enter)   
0125 
0126         for i in range(len(a)):
0127             j = idx[i]
0128             t_enter = enter[j]
0129             if not np.isnan(t_enter):
0130                 if t_enter < farthest_exit:
0131                     t_advanced = t_enter+0.0001
0132                     t_exit, state = self.intersect_sub(j, t_advanced)
0133                     assert state == EXIT
0134                     if t_exit > farthest_exit: 
0135                         farthest_exit = t_exit         
0136                     pass
0137                 pass
0138             pass
0139         pass
0140         stage = "fin"
0141         print("  %20s :   s  farthest_exit : %s    " % (stage, farthest_exit) )
0142         return farthest_exit
0143 
0144     def intersect_sub(self, i, t):
0145         """
0146 
0147         t < t0 
0148             Enter 
0149         t0 <= t < t1 
0150             Exit
0151         t > t1
0152             Miss
0153 
0154         ::
0155                
0156                    +------------+ 
0157                    |            | 
0158                    |            | 
0159                    |            | 
0160                    |            | 
0161                    +------------+ 
0162                    t0           t1 
0163 
0164         """
0165         a = self.a
0166         n = self.n
0167         assert i < n  
0168 
0169         tbeg = t 
0170 
0171         t0, t1 = a[i]
0172 
0173         if t < t0:
0174             t = t0 
0175             state = ENTER 
0176         elif t < t1:
0177             t = t1
0178             state = EXIT
0179         else:
0180             t = np.inf
0181             state = MISS
0182         pass
0183         print("intersect_sub i %2d  tbeg %10.4f  t0 %10.4f t1 %10.4f   state %s " % (i, tbeg, t0, t1, STATE[state] ))
0184         return t, state
0185 
0186 
0187 def test_pending():
0188     pending = np.uint8( (0x1 << 8) - 1 )
0189     dump(pending)
0190     i = 0 
0191     while pending:
0192         pending &= ~(0x1 << i)
0193         dump(pending)
0194         i += 1 
0195     pass
0196 
0197 
0198 if __name__ == '__main__':
0199     ms = MockSolid()
0200     #ms.intersect_pending(0)
0201     ms.intersect_sorting(0)
0202 
0203     #test_pending()
0204