Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:50:28

0001 /**
0002 
0003 ~/o/u4/tests/U4SolidMakerTest.sh
0004 
0005 **/
0006 
0007 
0008 #include "OPTICKS_LOG.hh"
0009 #include "ssys.h"
0010 #include "G4VSolid.hh"
0011 #include "U4Mesh.h"
0012 #include "U4Solid.h"
0013 #include "U4SolidMaker.hh"
0014 
0015 #include "s_csg.h"
0016 #include "sn.h"
0017 
0018 
0019 struct U4SolidMakerTest
0020 {
0021     static const char* SOLID ;
0022     static const char* TEST ;
0023     static const int   LEVEL ;
0024 
0025     static const G4VSolid* MakeSolid();
0026     static sn* Convert_(const G4VSolid* solid);
0027 
0028     static void get_node_bb(int lvid);
0029     static int Convert();
0030     static int Main(int argc, char** argv);
0031 };
0032 
0033 const char* U4SolidMakerTest::SOLID = ssys::getenvvar("SOLID", "WaterDistributer");
0034 const char* U4SolidMakerTest::TEST = ssys::getenvvar("TEST", "Convert");
0035 const int  U4SolidMakerTest::LEVEL = ssys::getenvint("LEVEL", 4 );
0036 
0037 inline int U4SolidMakerTest::Main(int argc, char** argv)
0038 {
0039     OPTICKS_LOG(argc, argv);
0040     bool ALL = strcmp(TEST, "ALL") == 0 ;
0041 
0042     int rc = 0 ;
0043     if(ALL||0==strcmp(TEST,"Convert")) rc += Convert();
0044     return rc ;
0045 }
0046 
0047 inline const G4VSolid* U4SolidMakerTest::MakeSolid()
0048 {
0049     bool can_make = U4SolidMaker::CanMake(SOLID);
0050     if(!can_make)  std::cerr << "U4SolidMakerTest::MakeSolid SOLID[" << ( SOLID ? SOLID : "-" ) << "] can_make[" << ( can_make ? "Y" : "N" ) << "]\n" ;
0051     if(!can_make) return 0 ;
0052     const G4VSolid* solid = U4SolidMaker::Make(SOLID);
0053     return solid ;
0054 }
0055 
0056 inline sn* U4SolidMakerTest::Convert_(const G4VSolid* solid )
0057 {
0058     int lvid = 0 ;
0059     int depth = 0 ;
0060     sn* nd = U4Solid::Convert(solid, lvid, depth, LEVEL);
0061     return nd ;
0062 }
0063 
0064 
0065 /**
0066 U4SolidMakerTest::get_node_bb
0067 ------------------------------
0068 
0069 See stree::get_node_bb
0070 
0071 **/
0072 
0073 
0074 inline void U4SolidMakerTest::get_node_bb(int lvid)
0075 {
0076     typedef std::array<double,6> BB ;
0077     typedef std::array<double,4> CE ;
0078     BB bb = {} ;
0079 
0080     typedef std::vector<BB> VBB ;
0081 
0082 
0083 
0084     std::vector<const sn*> bds ;
0085     sn::GetLVNodesComplete(bds, lvid);
0086     int bn = bds.size();
0087 
0088     std::vector<const sn*> lns ;
0089     sn::GetLVListnodes( lns, lvid );
0090     int ln = lns.size();
0091 
0092     std::vector<const sn*> subs ;
0093 
0094     for(int i=0 ; i < bn ; i++)
0095     {
0096         const sn* n = bds[i];
0097         //int  typecode = n ? n->typecode : CSG_ZERO ;
0098 
0099         if(n && n->is_listnode())
0100         {
0101             int num_sub = n->child.size() ;
0102             for(int j=0 ; j < num_sub ; j++)
0103             {
0104                 const sn* c = n->child[j];
0105                 subs.push_back(c);
0106             }
0107         }
0108     }
0109 
0110     int ns = subs.size();
0111 
0112 
0113     std::cout << "[U4SolidMakerTest::get_node_bb lvid " << lvid << "\n" ;
0114     std::cout << " bn " << bn << "\n" ;
0115     std::cout << " ln " << ln << "\n" ;
0116     std::cout << " ns " << ns << "\n" ;
0117 
0118     std::ostream* out = nullptr ;
0119 
0120     VBB vbb0 = {} ;
0121     VBB vbb  = {} ;
0122     VBB vbb1 = {} ;
0123 
0124     for( int i=0 ; i < ns ; i++ )
0125     {
0126         const sn* n = subs[i];
0127         bool leaf = CSG::IsLeaf(n->typecode) ;
0128         assert(leaf);
0129         if(!leaf) continue ;
0130 
0131         BB n_bb0 = {} ;
0132         n->copyBB_data(n_bb0.data()) ; // without transform
0133         vbb0.push_back(n_bb0);
0134 
0135         BB n_bb = {} ;
0136         n->copyBB_data( n_bb.data() );
0137 
0138         glm::tmat4x4<double> tc(1.) ;
0139         glm::tmat4x4<double> vc(1.) ;
0140         sn::NodeTransformProduct(n->idx(), tc, vc, false, out, nullptr );  // reverse:false
0141 
0142         stra<double>::Transform_AABB_Inplace(n_bb.data(), tc);
0143         vbb.push_back(n_bb);
0144 
0145         s_bb::IncludeAABB( bb.data(), n_bb.data(), out );
0146         vbb1.push_back(bb);
0147     }
0148 
0149     assert( vbb0.size() == vbb.size() );
0150     assert( vbb0.size() == vbb1.size() );
0151 
0152     size_t num = vbb0.size();
0153     for(size_t i=0 ; i < num ; i++ )
0154     {
0155         std::cout
0156            << std::setw(2) << i
0157            << " "
0158            << s_bb::Desc(vbb0[i].data())
0159            << " "
0160            << s_bb::Desc(vbb[i].data())
0161            << " "
0162            << s_bb::Desc(vbb1[i].data())
0163            << " "
0164            << "\n"
0165            ;
0166     }
0167 
0168 
0169 
0170     CE ce = {};
0171     s_bb::CenterExtent( ce.data(), bb.data() );
0172 
0173     std::cout << " bb " << s_bb::Desc(bb.data()) << "\n" ;
0174     std::cout << " ce " << s_bb::Desc_<double,4>(ce.data()) << "\n" ;
0175 
0176 
0177     std::cout << "]U4SolidMakerTest::get_node_bb lvid " << lvid << "\n" ;
0178 }
0179 
0180 
0181 inline int U4SolidMakerTest::Convert()
0182 {
0183     const G4VSolid* solid = MakeSolid();
0184     NPFold* fold = U4Mesh::Serialize(solid) ;
0185     fold->set_meta<std::string>("SOLID",SOLID);
0186     fold->set_meta<std::string>("desc","placeholder-desc");
0187     fold->save("$FOLD", SOLID );
0188 
0189     s_csg* _csg = new s_csg ;  // hold pools (normally stree holds _csg)
0190     assert( _csg );
0191 
0192     sn* nd = Convert_(solid);
0193 
0194     std::cout
0195         << "[U4SolidMakerTest nd.desc\n"
0196         <<   nd->desc()
0197         << "]U4SolidMakeTest nd.desc\n"
0198         ;
0199 
0200     if(LEVEL > 2 ) std::cout
0201         << "\n[U4SolidMakerTest nd->render() \n"
0202         << nd->render()
0203         << "\n]U4SolidMakerTest nd->render() \n\n"
0204         ;
0205 
0206     if(LEVEL > 3 ) std::cout
0207         << "\n[U4SolidMakerTest nd->detail_r()\n"
0208         << nd->detail_r()
0209         << "\n]U4SolidMakerTest nd->detail_r() \n\n"
0210         ;
0211 
0212     if(LEVEL > 3 ) std::cout
0213         << "\n[U4SolidMakerTest  nd->desc_prim_all() \n"
0214         << nd->desc_prim_all(false)
0215         << "\n]U4SolidMakerTest  nd->desc_prim_all() \n"
0216         ;
0217 
0218 
0219     int lvid = 0 ;
0220     get_node_bb(lvid);
0221 
0222 
0223 
0224     std::cout << "sn::Desc.0.before-delete-expect-some-nodes\n"  << sn::Desc() << "\n" ;
0225     delete nd ;
0226     std::cout << "sn::Desc.1.after-delete-expect-no-nodes\n"  << sn::Desc() << "\n" ;
0227 
0228     return 0;
0229 }
0230 
0231 
0232 int main(int argc, char** argv)
0233 {
0234     return U4SolidMakerTest::Main(argc, argv);
0235 }