Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:52

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 import logging, os
0022 log = logging.getLogger(__name__)
0023 
0024 template_head = r"""
0025 join(){ local IFS="$1"; shift; echo "$*"; }
0026 
0027 tbool%(name)s-vi(){   vi $BASH_SOURCE ; }
0028 tbool%(name)s-env(){  olocal- ;  } 
0029 tbool%(name)s-tag(){  echo 1 ; }
0030 tbool%(name)s-det(){  echo tbool ; }
0031 tbool%(name)s-src(){  echo torch ; }
0032 tbool%(name)s-args(){ echo  --det $(tbool%(name)s-det) --src $(tbool%(name)s-src) ; }
0033 
0034 tbool%(name)s-torchconfig()
0035 {
0036     local identity=1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000
0037 
0038     #local photons=1000000
0039     local photons=100000
0040     #local photons=1
0041 
0042     local torch_config_sphere=(
0043                  type=sphere
0044                  photons=10000
0045                  frame=-1
0046                  transform=1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,0.000,1.000,0.000,0.000,0.000,1000.000,1.000
0047                  source=0,0,0
0048                  target=0,0,1
0049                  time=0.1
0050                  radius=100
0051                  distance=400
0052                  zenithazimuth=0,1,0,1
0053                  material=GdDopedLS
0054                  wavelength=500
0055                )
0056 
0057     echo "$(join _ ${torch_config_sphere[@]})" 
0058 }
0059 
0060 
0061 tbool%(name)s-op()
0062 {
0063     local msg="=== $FUNCNAME :"
0064     local cmdline=$*
0065     local testconfig=$TESTCONFIG
0066 
0067     op.sh  \
0068             $cmdline \
0069             --animtimemax 20 \
0070             --timemax 20 \
0071             --geocenter \
0072             --eye 1,0,0 \
0073             --dbganalytic \
0074             --test --testconfig "$testconfig" \
0075             --torch --torchconfig "$(tbool%(name)s-torchconfig)" \
0076             --torchdbg \
0077             --tag $(tbool%(name)s-tag) --cat $(tbool%(name)s-det) \
0078             --rendermode +global \
0079             --save 
0080 }
0081 
0082 tbool%(name)s(){ TESTCONFIG=$($FUNCNAME-) tbool%(name)s-op $* ; }
0083 tbool%(name)s-(){ $FUNCNAME- | python $* ; } 
0084 tbool%(name)s--(){ cat << EOP
0085 
0086 import logging
0087 import numpy as np
0088 log = logging.getLogger(__name__)
0089 from opticks.ana.base import opticks_main
0090 from opticks.analytic.csg import CSG  
0091 from opticks.analytic.sc import Sc
0092 
0093 args = opticks_main(csgpath="$TMP/tbool/%(name)s")
0094 
0095 CSG.boundary = args.testobject
0096 CSG.kwa = dict(verbosity="0", poly="IM", resolution="20")
0097 #CSG.kwa = dict(verbosity="0", poly="HY", level="5")
0098 """
0099 
0100 template_tail = r"""
0101 raw = %(root)s
0102 
0103 raw.dump("raw")
0104 
0105 maxcsgheight = 4
0106 maxcsgheight2 = 5
0107 obj = Sc.optimize_csg(raw, maxcsgheight, maxcsgheight2 ) 
0108 
0109 obj.dump("optimized")
0110 
0111 #uobjs = [raw]
0112 uobjs = [obj]
0113 
0114 con = CSG("box",  param=[0,0,0,10], container="1", containerscale="2", boundary=args.container , poly="IM", resolution="20" )
0115 CSG.Serialize([con]+uobjs, args  )
0116 
0117 EOP
0118 }
0119 """
0120 
0121 template_body = r"""
0122 %(body)s
0123 """
0124 
0125 
0126 test_body = r"""
0127 
0128 a = CSG("sphere", param=[0,0,0,500] )
0129 
0130 
0131 """
0132 
0133 from opticks.ana.base import now_
0134 
0135 
0136 class TBoolBashFunction(dict):
0137      """
0138      This is used from opticks.analytic.csg with the "body" geometry 
0139      provided by the recursive as_code function::
0140 
0141         1335     def as_tbool(self, name="esr"):
0142         1336         tbf = TBoolBashFunction(name=name, root=self.alabel, body=self.as_code(lang="py")  )
0143         1337         return str(tbf)
0144 
0145 
0146      """
0147      def __init__(self, *args, **kwa):
0148          dict.__init__(self, *args, **kwa)
0149 
0150      def _get_stamp(self):
0151          return "# generated by tbool.py : %s " % ( now_() )
0152      stamp = property(_get_stamp)
0153 
0154      def _get_runline(self):
0155          return "\n".join([
0156                    "# opticks-;opticks-tbool %(name)s " % self,
0157                    "# opticks-;opticks-tbool-vi %(name)s " % self, ""])
0158 
0159      runline = property(_get_runline)
0160 
0161 
0162      head = property(lambda self:template_head % self)
0163      body = property(lambda self:template_body % self)
0164      tail = property(lambda self:template_tail % self)
0165 
0166      path = property(lambda self:os.path.expandvars("$TMP/tbool%(name)s.bash" % self))
0167 
0168      def save(self):
0169          log.info("saving to %s " % self.path) 
0170          file(self.path,"w").write(str(self))
0171 
0172      def test(self):
0173          print(self.stamp)
0174          print(self.head)
0175          print(self.body)
0176          print(self.tail)
0177 
0178      def __str__(self):
0179          return "\n".join([self.stamp,self.runline, self.head, self.stamp,self.runline,  self.body, self.tail])  
0180 
0181 
0182 if __name__ == '__main__':
0183 
0184      logging.basicConfig(level=logging.INFO)
0185 
0186      tbf = TBoolBashFunction(name="0", root="a", body=test_body)
0187      tbf.test()
0188 
0189      print(tbf) 
0190 
0191      tbf.save()
0192 
0193