Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // ./CSGFoundryTest.sh
0002 
0003 #include <iostream>
0004 #include <cassert>
0005 #include <csignal>
0006 
0007 #include "scuda.h"
0008 #include "sqat4.h"
0009 #include "OPTICKS_LOG.hh"
0010 #include "SSim.hh"
0011 
0012 #include "ssys.h"
0013 #include "spath.h"
0014 
0015 
0016 #include "CSGFoundry.h"
0017 #include "CSGMaker.h"
0018 
0019 
0020 
0021 void test_layered()
0022 {
0023     CSGFoundry fd ;
0024     CSGMaker* mk = fd.maker ;
0025 
0026     CSGSolid* s0 = mk->makeLayered("sphere", 100.f, 10 );
0027     CSGSolid* s1 = mk->makeLayered("sphere", 1000.f, 10 );
0028     CSGSolid* s2 = mk->makeLayered("sphere", 50.f, 5 );
0029     CSGSolid* s3 = mk->makeSphere() ;
0030 
0031     std::cout
0032        << " s0 " << ( s0 ? "Y" : "N" )
0033        << " s1 " << ( s1 ? "Y" : "N" )
0034        << " s2 " << ( s2 ? "Y" : "N" )
0035        << " s3 " << ( s3 ? "Y" : "N" )
0036        << std::endl
0037        ;
0038 
0039     fd.dump();
0040 
0041     assert( fd.getSolidIdx(s0) == 0 );
0042     assert( fd.getSolidIdx(s1) == 1 );
0043     assert( fd.getSolidIdx(s2) == 2 );
0044     assert( fd.getSolidIdx(s3) == 3 );
0045 
0046     fd.save("/tmp", "FoundryTest_" );
0047 }
0048 
0049 void test_PrimSpec()
0050 {
0051     CSGFoundry fd ;
0052     fd.maker->makeDemoSolids();
0053     for(unsigned i = 0 ; i < fd.solid.size() ; i++ )
0054     {
0055         unsigned solidIdx = i ;
0056         std::cout << "solidIdx " << solidIdx << std::endl ;
0057         SCSGPrimSpec ps = fd.getPrimSpec(solidIdx);
0058         ps.dump("");
0059     }
0060 
0061     std::string fdd = fd.desc();
0062     std::cout << fdd << std::endl ;
0063 }
0064 
0065 void test_addTran()
0066 {
0067     CSGFoundry fd ;
0068     const Tran<double>* tr = Tran<double>::make_translate( 100., 200., 300. ) ;
0069     unsigned idx = fd.addTran( tr );   // this idx is 0-based
0070     std::cout << "test_addTran idx " << idx << std::endl ;
0071     assert( idx == 0u );
0072     const qat4* t = fd.getTran(idx) ;
0073     const qat4* v = fd.getItra(idx) ;
0074 
0075     std::cout << "idx " << idx << std::endl ;
0076     std::cout << "t" << *t << std::endl ;
0077     std::cout << "v" << *v << std::endl ;
0078 }
0079 
0080 void test_makeClustered()
0081 {
0082     std::cout << "[test_makeClustered" << std::endl ;
0083     CSGFoundry fd ;
0084     bool inbox = false ;
0085     fd.maker->makeClustered("sphe", -1,2,1, -1,2,1, -1,2,1, 1000., inbox );
0086     fd.dumpPrim(0);
0087     std::cout << "]test_makeClustered" << std::endl ;
0088 }
0089 
0090 void test_Load()
0091 {
0092     CSGFoundry fd ;
0093     fd.maker->makeDemoSolids();
0094     fd.addInstancePlaceholder() ;  // Loading fails when no inst
0095 
0096 
0097     const char* dir = spath::Resolve("$TMP/CSGFoundryTest/test_Load") ;
0098     const char* rel = "CSGFoundry" ;
0099     fd.save(dir, rel );
0100 
0101     CSGFoundry* fdl = CSGFoundry::Load(dir, rel);
0102     fdl->dump();
0103 
0104     int cmp = CSGFoundry::Compare(&fd, fdl);
0105     std::cout << "test_Load " << cmp << std::endl ;
0106 
0107     std::cout << "fd.desc()  " << fd.desc() << std::endl ;
0108     std::cout << "fdl->desc() " << fdl->desc() << std::endl ;
0109 
0110 }
0111 
0112 void test_Compare()
0113 {
0114     CSGFoundry fd ;
0115     fd.maker->makeDemoSolids();
0116 
0117     int cmp = CSGFoundry::Compare(&fd, &fd);
0118     std::cout << "test_Compare " << cmp << std::endl ;
0119 }
0120 
0121 void test_getInstanceTransformsGAS()
0122 {
0123     CSGFoundry fd ;
0124     fd.maker->makeDemoGrid();
0125     fd.inst_find_unique();
0126     LOG(info) << fd.descGAS() ;
0127 
0128     unsigned gas_idx = fd.getNumSolid()/2 ;
0129 
0130     std::vector<qat4> sel ;
0131     fd.getInstanceTransformsGAS(sel, gas_idx );
0132 
0133     LOG(info)
0134         << " gas_idx " << gas_idx
0135         << " sel.size " << sel.size()
0136         ;
0137 
0138     qat4::dump(sel);
0139 
0140 }
0141 
0142 void test_getInstance_with_GAS_ordinal()
0143 {
0144     CSGFoundry fd ;
0145     fd.maker->makeDemoGrid();
0146     LOG(info) << fd.descGAS() ;
0147 
0148     unsigned gas_idx = fd.getNumSolid()/2 ;
0149     unsigned ordinal = 0 ;
0150 
0151     const qat4* q = fd.getInstance_with_GAS_ordinal(gas_idx, ordinal);
0152 
0153     assert(q) ;
0154     LOG(info) << *q ;
0155 }
0156 
0157 void test_setMeta_getMeta()
0158 {
0159     LOG(info) ;
0160 
0161     CSGFoundry fd ;
0162 
0163     int i0 = -101 ;
0164     int i1 = -1010 ;
0165     unsigned u = 202 ;
0166     float f = 42.f ;
0167     double d = 420. ;
0168     std::string s0 = "string0" ;
0169     std::string s1 = "string1" ;
0170 
0171     fd.setMeta("i0", i0);
0172     fd.setMeta("i1", i1);
0173     fd.setMeta("u", u);
0174     fd.setMeta("f", f );
0175     fd.setMeta("d", d );
0176     fd.setMeta("s0", s0 );
0177     fd.setMeta("s1", s1 );
0178 
0179     int i0_ = fd.getMeta("i0", 0);
0180     int i1_ = fd.getMeta("i1", 0);
0181     unsigned u_ = fd.getMeta("u", 0u);
0182     float f_ = fd.getMeta("f", 0.f);
0183     double d_ = fd.getMeta("d", 0.);
0184     std::string  s0_ = fd.getMeta<std::string>("s0", "");
0185     std::string  s1_ = fd.getMeta<std::string>("s1", "");
0186 
0187 
0188     bool i0_expect = i0 == i0_ ;
0189     bool i1_expect = i1 == i1_ ;
0190     bool u_expect = u == u_ ;
0191     bool f_expect = f == f_ ;
0192     bool d_expect = d == d_ ;
0193     bool s0_expect = strcmp(s0.c_str(), s0_.c_str()) == 0  ;
0194     bool s1_expect = strcmp(s1.c_str(), s1_.c_str()) == 0  ;
0195 
0196     assert( i0_expect );
0197     assert( i1_expect );
0198     assert( u_expect );
0199     assert( f_expect );
0200     assert( d_expect );
0201     assert( s0_expect );
0202     assert( s1_expect );
0203 
0204     if(!i0_expect) std::raise(SIGINT);
0205     if(!i1_expect) std::raise(SIGINT);
0206     if(!u_expect) std::raise(SIGINT);
0207     if(!f_expect) std::raise(SIGINT);
0208     if(!d_expect) std::raise(SIGINT);
0209     if(!s0_expect) std::raise(SIGINT);
0210     if(!s1_expect) std::raise(SIGINT);
0211 
0212 }
0213 
0214 void test_setPrimBoundary()
0215 {
0216     CSGFoundry* fd = CSGFoundry::Load() ;
0217     unsigned numPrim = fd->getNumPrim();
0218     unsigned primIdx = numPrim - 1 ;
0219     unsigned b0 = fd->getPrimBoundary(primIdx);
0220 
0221     LOG(info)
0222         << " numPrim " << numPrim
0223         << " primIdx " << primIdx
0224         << " b0 " << b0
0225 #ifdef WITH_FOREIGN
0226         << " bndname " << fd->getBndName(b0)
0227 #endif
0228         ;
0229 
0230     std::cout << fd->detailPrim(primIdx) << std::endl ;
0231 
0232     fd->setPrimBoundary( primIdx, 0u );
0233     std::cout << fd->detailPrim(primIdx) << std::endl ;
0234 
0235 #ifdef WITH_FOREIGN
0236     fd->setPrimBoundary(primIdx, "Water///Acrylic" );
0237     std::cout << fd->detailPrim(primIdx) << std::endl ;
0238 #endif
0239 
0240     fd->setPrimBoundary( primIdx, b0 );
0241     std::cout << fd->detailPrim(primIdx) << std::endl ;
0242 
0243 }
0244 
0245 
0246 void test_getPrimName()
0247 {
0248     SSim::Create();
0249 
0250     CSGFoundry* fd = CSGFoundry::Load() ;
0251     std::vector<std::string> pname ;
0252     fd->getPrimName(pname);
0253 
0254     LOG(info) << " pname.size " << pname.size() ;
0255     for(size_t i=0 ; i < pname.size() ; i++)
0256         std::cout << std::setw(6) << i << " : " << pname[i] << "\n" ;
0257 
0258     LOG(info) << " pname.size " << pname.size() ;
0259 
0260 }
0261 
0262 
0263 void test_Load_Save()
0264 {
0265     LOG(info) << "[" ;
0266     SSim::Create();
0267     CSGFoundry* fd = CSGFoundry::Load() ;
0268 
0269     //const char* opt = "meshname,prim" ;
0270     //fd->setSaveOpt(opt);
0271     //LOG(info) << " opt[" << opt << "]" ;
0272 
0273     const char* out = "$TMP/CSG/CSGFoundryTest/test_Load_Save";
0274     fd->save(out);
0275     LOG(info) << " out[" << out << "]" ;
0276 
0277     LOG(info) << "]" ;
0278 }
0279 
0280 
0281 
0282 int main(int argc, char** argv)
0283 {
0284     OPTICKS_LOG(argc, argv);
0285 
0286     //const char* DEF = "ALL" ;
0287     //const char* DEF = "Load_Save" ;
0288     const char* DEF = "getPrimName" ;
0289     const char* TEST = ssys::getenvvar("TEST", DEF) ;
0290     bool ALL = TEST && strcmp(TEST, "ALL") == 0 ;
0291 
0292     if(ALL||strcmp(TEST,"layered")==0) test_layered();
0293     if(ALL||strcmp(TEST,"PrimSpec")==0) test_PrimSpec();
0294     if(ALL||strcmp(TEST,"addTran")==0) test_addTran();
0295     if(ALL||strcmp(TEST,"makeClustered")==0) test_makeClustered();
0296     if(ALL||strcmp(TEST,"Compare")==0) test_Compare();
0297     if(ALL||strcmp(TEST,"Load")==0) test_Load();
0298     if(ALL||strcmp(TEST,"getInstanceTransformsGAS")==0) test_getInstanceTransformsGAS();
0299     if(ALL||strcmp(TEST,"getInstance_with_GAS_ordinal")==0) test_getInstance_with_GAS_ordinal();
0300     if(ALL||strcmp(TEST,"setMeta_getMeta")==0) test_setMeta_getMeta();
0301     if(ALL||strcmp(TEST,"getPrimName")==0) test_getPrimName();
0302     if(ALL||strcmp(TEST,"Load_Save")==0) test_Load_Save();
0303 
0304 
0305     return 0 ;
0306 }