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 """
0022 translate_gdml.py
0023 =================
0024 
0025 Usage::
0026 
0027    tboolean-
0028 
0029    tboolean-0-             
0030    tboolean-gds-             
0031    tboolean-oav-             
0032    tboolean-iav-             
0033    tboolean-pmt-             
0034        see logging from this script, parsing GDML and translating subtree into NCSG
0035 
0036 
0037    tboolean-gdml- --gsel 0 --gmaxdepth 2    # world is too big for easy navigation
0038    tboolean-gdml- --gsel 1 --gmaxdepth 1
0039    
0040    tboolean-0
0041    tboolean-gds             
0042    tboolean-oav             
0043    tboolean-iav             
0044    tboolean-pmt             
0045        subtree visualization of polygonization and raytrace 
0046 
0047    tboolean-gdml-scan         
0048    # NCSGScanTest load single solid and SDF scan a line segment thru geometry
0049 
0050    tboolean-0-deserialize  
0051    # NCSGDeserializeTest all solids within subtree, eg 5 solids for /dd/Geometry/PMT/lvPmtHemi0x
0052 
0053    tboolean-gdml-ip
0054    # jump into ipython running this script to provide GDML tree ready for querying 
0055 
0056 
0057 
0058 * TODO: split the translation interface sensitivity from rotation sensitivity
0059 
0060 
0061 """
0062 
0063 import numpy as np
0064 import os, logging, sys
0065 log = logging.getLogger(__name__)
0066 
0067 from opticks.ana.base import opticks_main
0068 from opticks.analytic.treebase import Tree
0069 from opticks.analytic.gdml import GDML
0070 from opticks.analytic.polyconfig import PolyConfig
0071 from opticks.analytic.csg import CSG  
0072 from opticks.analytic.sc import Sc
0073 
0074 
0075 if __name__ == '__main__':
0076 
0077     args = opticks_main()
0078 
0079     gsel = args.gsel            # string representing target node index integer or lvname
0080     gmaxnode = args.gmaxnode    # limit subtree node count
0081     gmaxdepth = args.gmaxdepth  # limit subtree node depth from the target node
0082     gidx = args.gidx            # target selection index, used when the gsel-ection yields multiple nodes eg when using lvname selection 
0083 
0084     log.info(" gsel:%s gidx:%s gmaxnode:%s gmaxdepth:%s " % (gsel, gidx, gmaxnode, gmaxdepth))
0085 
0086 
0087     gdml = GDML.parse()
0088     tree = Tree(gdml.world)
0089 
0090     subtree = tree.subtree(gsel, maxdepth=gmaxdepth, maxnode=gmaxnode, idx=gidx)
0091     # just a flat list of nodes
0092 
0093     log.info(" subtree %s nodes " % len(subtree) ) 
0094 
0095     cns = []
0096      
0097     for i, node in enumerate(subtree): 
0098 
0099         solid = node.lv.solid
0100         if i % 100 == 0:log.info("[%2d] converting solid %r " % (i,solid.name))
0101 
0102         cn = Sc.translate_lv(node.lv, maxcsgheight=4)
0103 
0104         skip_transform = i == 0  # skip first node transform which is placement of targetNode within its parent 
0105         if skip_transform:
0106             log.warning("skipping transform")
0107         else: 
0108             cn.transform = node.pv.transform
0109         pass 
0110 
0111         cn.meta.update(node.meta)
0112 
0113         log.info("cn.meta %r " % cn.meta )
0114 
0115         sys.stderr.write("\n".join(["","",solid.name,repr(cn),str(cn.txt)])) 
0116 
0117         cn.boundary = args.testobject
0118         cn.node = node
0119 
0120         cns.append(cn)
0121     pass
0122 
0123 
0124     container = None
0125     if args.disco:
0126         log.info("--disco option skipping container")
0127     else:
0128         log.info("adding container")
0129         container = CSG("box", name="container")
0130         container.boundary = args.container
0131         container.meta.update(PolyConfig("CONTAINER").meta)
0132     pass
0133 
0134     objs = []
0135     if not container is None:
0136         objs.append(container)
0137     pass
0138     objs.extend(cns)
0139 
0140     CSG.Serialize(objs, args.csgpath, outmeta=True )  
0141 
0142