File indexing completed on 2026-04-10 07:50:29
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include "OPTICKS_LOG.hh"
0026 #include "ssys.h"
0027 #include "stra.h"
0028 #include "SSim.hh"
0029 #include "U4VolumeMaker.hh"
0030 #include "U4Tree.h"
0031
0032
0033 struct U4TreeCreateSSimTest
0034 {
0035 static constexpr const char* _LEVEL = "U4TreeCreateSSimTest__LEVEL" ;
0036 int level ;
0037 SSim* sim ;
0038 stree* st ;
0039 U4Tree* tr ;
0040
0041 bool starting = true ;
0042 const char* q_soname ;
0043 int q_lvid ;
0044 int q_lvid_ordinal = 0 ;
0045 const snode* q_node ;
0046 const sn* q_nd ;
0047 int q_nd_num_child ;
0048
0049 U4TreeCreateSSimTest( const G4VPhysicalVolume* world );
0050 void init();
0051 std::string desc(const char* label) const ;
0052
0053 int find_lvid() const;
0054 int pick_lvid_ordinal_node() const ;
0055 int get_combined_transform() const;
0056 int get_combined_tran_and_aabb() const;
0057 int GetLVListnodes() const ;
0058
0059 int run_TEST_method() const ;
0060 static int Main(int argc, char** argv);
0061
0062 };
0063
0064 U4TreeCreateSSimTest::U4TreeCreateSSimTest(const G4VPhysicalVolume* world )
0065 :
0066 level(ssys::getenvint(_LEVEL,2)),
0067 sim(SSim::Create()),
0068 st(sim->tree),
0069 tr(world ? U4Tree::Create(st, world) : nullptr),
0070 q_soname(ssys::getenvvar("SONAME", nullptr)),
0071 q_lvid( q_soname ? st->find_lvid(q_soname, starting) : -1 ),
0072 q_node( q_lvid > -1 ? st->pick_lvid_ordinal_node(q_lvid, q_lvid_ordinal, 'N' ) : nullptr ),
0073 q_nd(q_lvid > -1 ? sn::GetLVRoot(q_lvid) : nullptr ),
0074 q_nd_num_child( q_nd ? q_nd->num_child() : -1)
0075 {
0076 init();
0077 }
0078
0079 void U4TreeCreateSSimTest::init()
0080 {
0081 assert(tr);
0082 sim->initSceneFromTree();
0083 std::cerr << " save SSim to $FOLD " << std::endl ;
0084 sim->save("$FOLD");
0085 LOG(info) << " sim.tree.desc " << std::endl << st->desc() ;
0086 }
0087
0088
0089 std::string U4TreeCreateSSimTest::desc(const char* label) const
0090 {
0091 std::stringstream ss ;
0092 ss << label << "\n"
0093 << " q_soname " << ( q_soname ? q_soname : "-" )
0094 << " q_lvid " << q_lvid
0095 << " q_lvid_ordinal " << q_lvid_ordinal
0096 << " q_node.desc\n"
0097 << ( q_node ? q_node->desc() : "-" )
0098 << "\n"
0099 << " q_nd.desc\n"
0100 << ( q_nd ? q_nd->desc() : "-" )
0101 << "\n"
0102 << " q_nd_num_child " << q_nd_num_child
0103 << "\n"
0104 ;
0105
0106 std::string str = ss.str() ;
0107 return str ;
0108 }
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 int U4TreeCreateSSimTest::find_lvid() const
0121 {
0122 int rc = (q_soname == nullptr || q_lvid > -1 ) ? 0 : 1 ;
0123 std::cout
0124 << desc("U4TreeCreateSSimTest::find_lvid")
0125 << " rc " << rc
0126 << "\n"
0127 ;
0128
0129 return rc ;
0130 }
0131
0132 int U4TreeCreateSSimTest::pick_lvid_ordinal_node() const
0133 {
0134 int rc = q_node == nullptr ? 1 : 0 ;
0135 std::cout
0136 << desc("U4TreeCreateSSimTest::pick_lvid_ordinal_node")
0137 << " rc " << rc
0138 << "\n"
0139 ;
0140 return rc ;
0141 }
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 int U4TreeCreateSSimTest::get_combined_transform() const
0156 {
0157 if(!q_node) return 1 ;
0158 if(!q_nd) return 1 ;
0159
0160 std::cout << desc("U4TreeCreateSSimTest::get_combined_transform") ;
0161
0162 for(int i=0 ; i < 3 ; i++)
0163 {
0164 for(int j=0 ; j < q_nd_num_child ; j++)
0165 {
0166 sn* c = q_nd->get_child(j);
0167 if( i == 0 ) std::cout << " sn::get_child/desc " << std::setw(3) << j << " : " << c->desc() << "\n" ;
0168 if( i == 1 ) std::cout << " sn::get_child/desc_prim " << std::setw(3) << j << " : " << c->desc_prim() << "\n" ;
0169 if( i == 2 ) std::cout << " sn::get_child/descXF " << std::setw(3) << j << " : " << c->descXF() << "\n" ;
0170 }
0171 std::cout << "\n" ;
0172 }
0173
0174 for(int j=-1 ; j < q_nd_num_child ; j++)
0175 {
0176 const sn* n = j == -1 ? q_nd : q_nd->get_child(j) ;
0177
0178 glm::tmat4x4<double> t(1.);
0179 glm::tmat4x4<double> v(1.);
0180 std::stringstream ss ;
0181 std::ostream* out = level > 2 ? &ss : nullptr ;
0182 stree::VTR* t_stack = nullptr ;
0183
0184 st->get_combined_transform(t,v,*q_node,n, out, t_stack ) ;
0185 std::string str = out ? ss.str() : "-" ;
0186
0187 std::cout
0188 << " q_nd+child " << std::setw(3) << j << " : " << n->desc()
0189 << "\n"
0190 << str
0191 << "\n"
0192 << stra<double>::Desc(t,v, "t", "v")
0193 << "\n"
0194 ;
0195 }
0196
0197 return 0 ;
0198 }
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210 int U4TreeCreateSSimTest::get_combined_tran_and_aabb() const
0211 {
0212 if(!q_node) return 1 ;
0213 if(!q_nd) return 1 ;
0214
0215 std::cout << desc("U4TreeCreateSSimTest::get_combined_tran_and_aabb") ;
0216
0217
0218 for(int j=-1 ; j < q_nd_num_child ; j++)
0219 {
0220 const sn* nd = j == -1 ? q_nd : q_nd->get_child(j) ;
0221
0222 int typecode = nd ? nd->typecode : CSG_ZERO ;
0223 bool leaf = CSG::IsLeaf(typecode) ;
0224
0225 std::stringstream ss ;
0226 std::ostream* out = level > 2 ? &ss : nullptr ;
0227 stree::VTR* t_stack = nullptr ;
0228
0229 std::array<double,6> bb ;
0230 double* aabb = leaf ? bb.data() : nullptr ;
0231
0232 const Tran<double>* tran = leaf ? st->get_combined_tran_and_aabb(aabb,*q_node, nd, out, t_stack ) : nullptr ;
0233
0234 std::string str = out ? ss.str() : "-" ;
0235 std::cout
0236 << " q_nd+child " << std::setw(3) << j << " : " << nd->desc()
0237 << "\n"
0238 << str
0239 << "\n"
0240 << ( tran ? stra<double>::Desc(tran->t,tran->v, "t", "v") : "no-tran-for-non-leaf" )
0241 << "\n"
0242 << ( aabb ? s_bb::Desc(aabb) : "no-aabb-for-non-leaf" )
0243 << "\n"
0244 ;
0245 }
0246
0247
0248 return 0 ;
0249 }
0250
0251 int U4TreeCreateSSimTest::GetLVListnodes() const
0252 {
0253 std::vector<const sn*> lns ;
0254 sn::GetLVListnodes( lns, q_lvid );
0255 int num_lns = lns.size();
0256
0257 int child_total = sn::GetChildTotal(lns) ;
0258
0259
0260 std::cout
0261 << desc("U4TreeCreateSSimTest::GetLVListnodes")
0262 << " num_lns " << num_lns
0263 << " child_total " << child_total
0264 << "\n"
0265 ;
0266
0267 for(int i=0 ; i < num_lns ; i++)
0268 {
0269 const sn* n = lns[i];
0270 std::cout
0271 << std::setw(3) << i << " : " << n->desc() << "\n"
0272 ;
0273 }
0274
0275 return 0 ;
0276 }
0277
0278
0279 int U4TreeCreateSSimTest::run_TEST_method() const
0280 {
0281 const char* TEST = ssys::getenvvar("TEST", "NONE") ;
0282 int rc = 0 ;
0283 if( strcmp(TEST, "find_lvid") == 0 ) rc = find_lvid();
0284 else if(strcmp(TEST, "pick_lvid_ordinal_node") == 0 ) rc = pick_lvid_ordinal_node();
0285 else if(strcmp(TEST, "get_combined_transform") == 0 ) rc = get_combined_transform();
0286 else if(strcmp(TEST, "get_combined_tran_and_aabb") == 0 ) rc = get_combined_tran_and_aabb();
0287 else if(strcmp(TEST, "GetLVListnodes") == 0 ) rc = GetLVListnodes();
0288 else std::cout << "U4TreeCreateSSimTest::run_TEST_method no-impl for TEST[" << ( TEST ? TEST : "-" ) << "]" ;
0289 return rc ;
0290 }
0291
0292 int U4TreeCreateSSimTest::Main(int argc, char** argv)
0293 {
0294 OPTICKS_LOG(argc, argv);
0295 const G4VPhysicalVolume* world = U4VolumeMaker::PV() ;
0296 LOG_IF(error, world == nullptr) << " FAILED TO CREATE world with U4VolumeMaker::PV " ;
0297 if(world == nullptr) return 0 ;
0298
0299 U4TreeCreateSSimTest test(world);
0300 return test.run_TEST_method() ;
0301 }
0302
0303
0304 int main(int argc, char** argv)
0305 {
0306 return U4TreeCreateSSimTest::Main(argc, argv);
0307 }